File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1511: download - view: text, annotated - select for diffs
Fri Jun 2 01:20:29 2023 UTC (13 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bugs 6754 and 6907
  - Return of grades to launcher CMS supported for resources or folders
    accessed via LTI-mediated deep link.
  - Support option: "Encrypt stored consumer secrets defined in domain"
  - Signing of LTI payloads for roster retrieval, passback of grades,
    and callback to logput launcher CMS session all now occur on
    primary library server for course's domain.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1511 2023/06/02 01:20:29 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonnet.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: This file is an interface to the lonc processes of
   39: the LON-CAPA network as well as set of elaborated functions for handling information
   40: necessary for navigating through a given cluster of LON-CAPA machines within a
   41: domain. There are over 40 specialized functions in this module which handle the
   42: reading and transmission of metadata, user information (ids, names, environments, roles,
   43: logs), file information (storage, reading, directories, extensions, replication, embedded
   44: styles and descriptors), educational resources (course descriptions, section names and
   45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
   46: and from more descriptive phrases or explanations.
   47: 
   48: This is part of the LearningOnline Network with CAPA project
   49: described at http://www.lon-capa.org.
   50: 
   51: =head1 Package Variables
   52: 
   53: These are largely undocumented, so if you decipher one please note it here.
   54: 
   55: =over 4
   56: 
   57: =item $processmarker
   58: 
   59: Contains the time this process was started and this servers host id.
   60: 
   61: =item $dumpcount
   62: 
   63: Counts the number of times a message log flush has been attempted (regardless
   64: of success) by this process.  Used as part of the filename when messages are
   65: delayed.
   66: 
   67: =back
   68: 
   69: =cut
   70: 
   71: package Apache::lonnet;
   72: 
   73: use strict;
   74: use HTTP::Date;
   75: use Image::Magick;
   76: use CGI::Cookie;
   77: 
   78: use Encode;
   79: 
   80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
   81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
   82:             %managerstab $passwdmin);
   83: 
   84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
   85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
   86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
   87:     %courseownerbuf, %coursetypebuf,$locknum);
   88: 
   89: use IO::Socket;
   90: use GDBM_File;
   91: use HTML::LCParser;
   92: use Fcntl qw(:flock);
   93: use Storable qw(thaw nfreeze);
   94: use Time::HiRes qw( sleep gettimeofday tv_interval );
   95: use Cache::Memcached;
   96: use Digest::MD5;
   97: use Math::Random;
   98: use File::MMagic;
   99: use Net::CIDR;
  100: use Sys::Hostname::FQDN();
  101: use LONCAPA qw(:DEFAULT :match);
  102: use LONCAPA::Configuration;
  103: use LONCAPA::lonmetadata;
  104: use LONCAPA::Lond;
  105: use LONCAPA::LWPReq;
  106: use LONCAPA::transliterate;
  107: 
  108: use File::Copy;
  109: 
  110: my $readit;
  111: my $max_connection_retries = 20;     # Or some such value.
  112: 
  113: require Exporter;
  114: 
  115: our @ISA = qw (Exporter);
  116: our @EXPORT = qw(%env);
  117: 
  118: 
  119: # ------------------------------------ Logging (parameters, docs, slots, roles)
  120: {
  121:     my $logid;
  122:     sub write_log {
  123: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  124:         if ($context eq 'course') {
  125:             if (($cnum eq '') || ($cdom eq '')) {
  126:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  127:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  128:             }
  129:         }
  130: 	$logid ++;
  131:         my $now = time();
  132: 	my $id=$now.'00000'.$$.'00000'.$logid;
  133:         my $ip = &get_requestor_ip();
  134:         my $logentry = { 
  135:                           $id => {
  136:                                    'exe_uname' => $env{'user.name'},
  137:                                    'exe_udom'  => $env{'user.domain'},
  138:                                    'exe_time'  => $now,
  139:                                    'exe_ip'    => $ip,
  140:                                    'delflag'   => $delflag,
  141:                                    'logentry'  => $storehash,
  142:                                    'uname'     => $uname,
  143:                                    'udom'      => $udom,
  144:                                   }
  145:                        };
  146: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  147:     }
  148: }
  149: 
  150: sub logtouch {
  151:     my $execdir=$perlvar{'lonDaemons'};
  152:     unless (-e "$execdir/logs/lonnet.log") {	
  153: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  154: 	close $fh;
  155:     }
  156:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  157:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  158: }
  159: 
  160: sub logthis {
  161:     my $message=shift;
  162:     my $execdir=$perlvar{'lonDaemons'};
  163:     my $now=time;
  164:     my $local=localtime($now);
  165:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  166: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  167: 	print $fh $logstring;
  168: 	close($fh);
  169:     }
  170:     return 1;
  171: }
  172: 
  173: sub logperm {
  174:     my $message=shift;
  175:     my $execdir=$perlvar{'lonDaemons'};
  176:     my $now=time;
  177:     my $local=localtime($now);
  178:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  179: 	print $fh "$now:$message:$local\n";
  180: 	close($fh);
  181:     }
  182:     return 1;
  183: }
  184: 
  185: sub create_connection {
  186:     my ($hostname,$lonid) = @_;
  187:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  188: 				     Type    => SOCK_STREAM,
  189: 				     Timeout => 10);
  190:     return 0 if (!$client);
  191:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  192:     my $result = <$client>;
  193:     chomp($result);
  194:     return 1 if ($result eq 'done');
  195:     return 0;
  196: }
  197: 
  198: sub get_server_timezone {
  199:     my ($cnum,$cdom) = @_;
  200:     my $home=&homeserver($cnum,$cdom);
  201:     if ($home ne 'no_host') {
  202:         my $cachetime = 24*3600;
  203:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  204:         if (defined($cached)) {
  205:             return $timezone;
  206:         } else {
  207:             my $timezone = &reply('servertimezone',$home);
  208:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  209:         }
  210:     }
  211: }
  212: 
  213: sub get_server_distarch {
  214:     my ($lonhost,$ignore_cache) = @_;
  215:     if (defined($lonhost)) {
  216:         if (!defined(&hostname($lonhost))) {
  217:             return;
  218:         }
  219:         my $cachetime = 12*3600;
  220:         if (!$ignore_cache) {
  221:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  222:             if (defined($cached)) {
  223:                 return $distarch;
  224:             }
  225:         }
  226:         my $rep = &reply('serverdistarch',$lonhost);
  227:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  228:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  229:                 $rep eq '') {
  230:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  231:         }
  232:     }
  233:     return;
  234: }
  235: 
  236: sub get_servercerts_info {
  237:     my ($lonhost,$hostname,$context) = @_;
  238:     return if ($lonhost eq '');
  239:     if ($hostname eq '') {
  240:         $hostname = &hostname($lonhost);
  241:     }
  242:     return if ($hostname eq '');
  243:     my ($rep,$uselocal);
  244:     if ($context eq 'install') {
  245:         $uselocal = 1;
  246:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  247:         $uselocal = 1;
  248:     }
  249:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  250:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  251:         if ($distro eq '') {
  252:             $uselocal = 0;
  253:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  254:             if ($1 < 6) {
  255:                 $uselocal = 0;
  256:             }
  257:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  258:             if ($1 < 12) {
  259:                 $uselocal = 0;
  260:             }
  261:         }
  262:     }
  263:     if ($uselocal) {
  264:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  265:     } else {
  266:         $rep=&reply('servercerts',$lonhost);
  267:     }
  268:     my ($result,%returnhash);
  269:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  270:         ($rep eq 'unknown_cmd')) {
  271:         $result = $rep;
  272:     } else {
  273:         $result = 'ok';
  274:         my @pairs=split(/\&/,$rep);
  275:         foreach my $item (@pairs) {
  276:             my ($key,$value)=split(/=/,$item,2);
  277:             my $what = &unescape($key);
  278:             $returnhash{$what}=&thaw_unescape($value);
  279:         }
  280:     }
  281:     return ($result,\%returnhash);
  282: }
  283: 
  284: sub get_server_loncaparev {
  285:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  286:     if (defined($lonhost)) {
  287:         if (!defined(&hostname($lonhost))) {
  288:             undef($lonhost);
  289:         }
  290:     }
  291:     if (!defined($lonhost)) {
  292:         if (defined(&domain($dom,'primary'))) {
  293:             $lonhost=&domain($dom,'primary');
  294:             if ($lonhost eq 'no_host') {
  295:                 undef($lonhost);
  296:             }
  297:         }
  298:     }
  299:     if (defined($lonhost)) {
  300:         my $cachetime = 12*3600;
  301:         if (!$ignore_cache) {
  302:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  303:             if (defined($cached)) {
  304:                 return $loncaparev;
  305:             }
  306:         }
  307:         my ($answer,$loncaparev);
  308:         my @ids=&current_machine_ids();
  309:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  310:             $answer = $perlvar{'lonVersion'};
  311:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  312:                 $loncaparev = $1;
  313:             }
  314:         } else {
  315:             $answer = &reply('serverloncaparev',$lonhost);
  316:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  317:                 if ($caller eq 'loncron') {
  318:                     my $hostname = &hostname($lonhost);
  319:                     my $protocol = $protocol{$lonhost};
  320:                     $protocol = 'http' if ($protocol ne 'https');
  321:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  322:                     my $request=new HTTP::Request('GET',$url);
  323:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  324:                     unless ($response->is_error()) {
  325:                         my $content = $response->content;
  326:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  327:                             $loncaparev = $1;
  328:                         }
  329:                     }
  330:                 } else {
  331:                     $loncaparev = $loncaparevs{$lonhost};
  332:                 }
  333:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  334:                 $loncaparev = $1;
  335:             }
  336:         }
  337:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  338:     }
  339: }
  340: 
  341: sub get_server_homeID {
  342:     my ($hostname,$ignore_cache,$caller) = @_;
  343:     unless ($ignore_cache) {
  344:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  345:         if (defined($cached)) {
  346:             return $serverhomeID;
  347:         }
  348:     }
  349:     my $cachetime = 12*3600;
  350:     my $serverhomeID;
  351:     if ($caller eq 'loncron') { 
  352:         my @machine_ids = &machine_ids($hostname);
  353:         foreach my $id (@machine_ids) {
  354:             my $response = &reply('serverhomeID',$id);
  355:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  356:                 $serverhomeID = $response;
  357:                 last;
  358:             }
  359:         }
  360:         if ($serverhomeID eq '') {
  361:             $serverhomeID = $machine_ids[-1];
  362:         }
  363:     } else {
  364:         $serverhomeID = $serverhomeIDs{$hostname};
  365:     }
  366:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  367: }
  368: 
  369: sub get_remote_globals {
  370:     my ($lonhost,$whathash,$ignore_cache) = @_;
  371:     my ($result,%returnhash,%whatneeded);
  372:     if (ref($whathash) eq 'HASH') {
  373:         foreach my $what (sort(keys(%{$whathash}))) {
  374:             my $hashid = $lonhost.'-'.$what;
  375:             my ($response,$cached);
  376:             unless ($ignore_cache) {
  377:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  378:             }
  379:             if (defined($cached)) {
  380:                 $returnhash{$what} = $response;
  381:             } else {
  382:                 $whatneeded{$what} = 1;
  383:             }
  384:         }
  385:         if (keys(%whatneeded) == 0) {
  386:             $result = 'ok';
  387:         } else {
  388:             my $requested = &freeze_escape(\%whatneeded);
  389:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  390:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  391:                 ($rep eq 'unknown_cmd')) {
  392:                 $result = $rep;
  393:             } else {
  394:                 $result = 'ok';
  395:                 my @pairs=split(/\&/,$rep);
  396:                 foreach my $item (@pairs) {
  397:                     my ($key,$value)=split(/=/,$item,2);
  398:                     my $what = &unescape($key);
  399:                     my $hashid = $lonhost.'-'.$what;
  400:                     $returnhash{$what}=&thaw_unescape($value);
  401:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  402:                 }
  403:             }
  404:         }
  405:     }
  406:     return ($result,\%returnhash);
  407: }
  408: 
  409: sub remote_devalidate_cache {
  410:     my ($lonhost,$cachekeys) = @_;
  411:     my $items;
  412:     return unless (ref($cachekeys) eq 'ARRAY');
  413:     my $cachestr = join('&',@{$cachekeys});
  414:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  415:     return $response;
  416: }
  417: 
  418: sub sign_lti {
  419:     my ($cdom,$cnum,$crsdef,$type,$context,$url,$ltinum,$keynum,$paramsref,$inforef) = @_;
  420:     my $chome;
  421:     if (&domain($cdom) ne '') {
  422:         if ($crsdef) {
  423:             $chome = &homeserver($cnum,$cdom);
  424:         } else {
  425:             $chome = &domain($cdom,'primary');
  426:         }
  427:     }
  428:     if ($cdom && $chome && ($chome ne 'no_host')) {
  429:         if ((ref($paramsref) eq 'HASH') &&
  430:             (ref($inforef) eq 'HASH')) {
  431:             my $rep;
  432:             if (grep { $_ eq $chome } &current_machine_ids()) {
  433:                 # domain information is hosted on this machine
  434:                 $rep =
  435:                     &LONCAPA::Lond::sign_lti_payload($cdom,$cnum,$crsdef,$type,
  436:                                                      $context,$url,$ltinum,$keynum,
  437:                                                      $perlvar{'lonVersion'},
  438:                                                      $paramsref,$inforef);
  439:                 if (ref($rep) eq 'HASH') {
  440:                     return ('ok',$rep);
  441:                 }
  442:             } else {
  443:                 my ($escurl,$params,$info);
  444:                 $escurl = &escape($url);
  445:                 if (ref($paramsref) eq 'HASH') {
  446:                     $params = &freeze_escape($paramsref);
  447:                 }
  448:                 if (ref($inforef) eq 'HASH') {
  449:                     $info = &freeze_escape($inforef);
  450:                 }
  451:                 $rep=&reply("encrypt:signlti:$cdom:$cnum:$crsdef:$type:$context:$escurl:$ltinum:$keynum:$params:$info",$chome);
  452:             }
  453:             if (($rep eq '') || ($rep =~ /^con_lost|error|no_such_host|unknown_cmd/i)) {
  454:                 return ();
  455:             } elsif (($inforef->{'respfmt'} eq 'to_post_body') ||
  456:                      ($inforef->{'respfmt'} eq 'to_authorization_header')) {
  457:                 return ('ok',$rep);
  458:             } else {
  459:                 my %returnhash;
  460:                 foreach my $item (split(/\&/,$rep)) {
  461:                     my ($name,$value)=split(/\=/,$item);
  462:                     $returnhash{&unescape($name)}=&thaw_unescape($value);
  463:                 }
  464:                 return('ok',\%returnhash);
  465:             }
  466:         } else {
  467:             return ();
  468:         }
  469:     } else {
  470:         return ();
  471:         &logthis("sign_lti failed - no homeserver and/or domain ($cdom) ($chome)");
  472:     }
  473: }
  474: 
  475: # -------------------------------------------------- Non-critical communication
  476: sub subreply {
  477:     my ($cmd,$server)=@_;
  478:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  479:     #
  480:     #  With loncnew process trimming, there's a timing hole between lonc server
  481:     #  process exit and the master server picking up the listen on the AF_UNIX
  482:     #  socket.  In that time interval, a lock file will exist:
  483: 
  484:     my $lockfile=$peerfile.".lock";
  485:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  486: 	sleep(0.1);
  487:     }
  488:     # At this point, either a loncnew parent is listening or an old lonc
  489:     # or loncnew child is listening so we can connect or everything's dead.
  490:     #
  491:     #   We'll give the connection a few tries before abandoning it.  If
  492:     #   connection is not possible, we'll con_lost back to the client.
  493:     #   
  494:     my $client;
  495:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  496: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  497: 				      Type    => SOCK_STREAM,
  498: 				      Timeout => 10);
  499: 	if ($client) {
  500: 	    last;		# Connected!
  501: 	} else {
  502: 	    &create_connection(&hostname($server),$server);
  503: 	}
  504:         sleep(0.1);	# Try again later if failed connection.
  505:     }
  506:     my $answer;
  507:     if ($client) {
  508: 	print $client "sethost:$server:$cmd\n";
  509: 	$answer=<$client>;
  510: 	if (!$answer) { $answer="con_lost"; }
  511: 	chomp($answer);
  512:     } else {
  513: 	$answer = 'con_lost';	# Failed connection.
  514:     }
  515:     return $answer;
  516: }
  517: 
  518: sub reply {
  519:     my ($cmd,$server)=@_;
  520:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  521:     my $answer=subreply($cmd,$server);
  522:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  523:         my $logged = $cmd;
  524:         if ($cmd =~ /^encrypt:([^:]+):/) {
  525:             my $subcmd = $1;
  526:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  527:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  528:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades') ||
  529:                 ($subcmd eq 'put')) {
  530:                 (undef,undef,my @rest) = split(/:/,$cmd);
  531:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  532:                     splice(@rest,2,1,'Hidden');
  533:                 } elsif ($subcmd eq 'passwd') {
  534:                     splice(@rest,2,2,('Hidden','Hidden'));
  535:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  536:                          ($subcmd eq 'autoexportgrades') || ($subcmd eq 'put')) {
  537:                     splice(@rest,3,1,'Hidden');
  538:                 }
  539:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  540:             }
  541:         }
  542:         &logthis("<font color=\"blue\">WARNING:".
  543:                  " $logged to $server returned $answer</font>");
  544:     }
  545:     return $answer;
  546: }
  547: 
  548: # ----------------------------------------------------------- Send USR1 to lonc
  549: 
  550: sub reconlonc {
  551:     my ($lonid) = @_;
  552:     if ($lonid) {
  553:         my $hostname = &hostname($lonid);
  554: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  555: 	if ($hostname && -e $peerfile) {
  556: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  557: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  558: 					     Type    => SOCK_STREAM,
  559: 					     Timeout => 10);
  560: 	    if ($client) {
  561: 		print $client ("reset_retries\n");
  562: 		my $answer=<$client>;
  563: 		#reset just this one.
  564: 	    }
  565: 	}
  566: 	return;
  567:     }
  568: 
  569:     &logthis("Trying to reconnect lonc");
  570:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  571:     if (open(my $fh,"<",$loncfile)) {
  572: 	my $loncpid=<$fh>;
  573:         chomp($loncpid);
  574:         if (kill 0 => $loncpid) {
  575: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  576:             kill USR1 => $loncpid;
  577:             sleep 1;
  578:         } else {
  579: 	    &logthis(
  580:                "<font color=\"blue\">WARNING:".
  581:                " lonc at pid $loncpid not responding, giving up</font>");
  582:         }
  583:     } else {
  584: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  585:     }
  586: }
  587: 
  588: # ------------------------------------------------------ Critical communication
  589: 
  590: sub critical {
  591:     my ($cmd,$server)=@_;
  592:     unless (&hostname($server)) {
  593:         &logthis("<font color=\"blue\">WARNING:".
  594:                " Critical message to unknown server ($server)</font>");
  595:         return 'no_such_host';
  596:     }
  597:     my $answer=reply($cmd,$server);
  598:     if ($answer eq 'con_lost') {
  599: 	&reconlonc($server);
  600: 	my $answer=reply($cmd,$server);
  601:         if ($answer eq 'con_lost') {
  602:             my $now=time;
  603:             my $middlename=$cmd;
  604:             $middlename=substr($middlename,0,16);
  605:             $middlename=~s/\W//g;
  606:             my $dfilename=
  607:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  608:             $dumpcount++;
  609:             {
  610: 		my $dfh;
  611: 		if (open($dfh,">",$dfilename)) {
  612: 		    print $dfh "$cmd\n"; 
  613: 		    close($dfh);
  614: 		}
  615:             }
  616:             sleep 1;
  617:             my $wcmd='';
  618:             {
  619: 		my $dfh;
  620: 		if (open($dfh,"<",$dfilename)) {
  621: 		    $wcmd=<$dfh>; 
  622: 		    close($dfh);
  623: 		}
  624:             }
  625:             chomp($wcmd);
  626:             if ($wcmd eq $cmd) {
  627: 		&logthis("<font color=\"blue\">WARNING: ".
  628:                          "Connection buffer $dfilename: $cmd</font>");
  629:                 &logperm("D:$server:$cmd");
  630: 	        return 'con_delayed';
  631:             } else {
  632:                 &logthis("<font color=\"red\">CRITICAL:"
  633:                         ." Critical connection failed: $server $cmd</font>");
  634:                 &logperm("F:$server:$cmd");
  635:                 return 'con_failed';
  636:             }
  637:         }
  638:     }
  639:     return $answer;
  640: }
  641: 
  642: # ------------------------------------------- check if return value is an error
  643: 
  644: sub error {
  645:     my ($result) = @_;
  646:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  647: 	if ($2 == 2) { return undef; }
  648: 	return $1;
  649:     }
  650:     return undef;
  651: }
  652: 
  653: sub convert_and_load_session_env {
  654:     my ($lonidsdir,$handle)=@_;
  655:     my @profile;
  656:     {
  657: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  658: 	if (!$opened) {
  659: 	    return 0;
  660: 	}
  661: 	flock($idf,LOCK_SH);
  662: 	@profile=<$idf>;
  663: 	close($idf);
  664:     }
  665:     my %temp_env;
  666:     foreach my $line (@profile) {
  667: 	if ($line !~ m/=/) {
  668: 	    return 0;
  669: 	}
  670: 	chomp($line);
  671: 	my ($envname,$envvalue)=split(/=/,$line,2);
  672: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  673:     }
  674:     unlink("$lonidsdir/$handle.id");
  675:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  676: 	    0640)) {
  677: 	%disk_env = %temp_env;
  678: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  679: 	untie(%disk_env);
  680:     }
  681:     return 1;
  682: }
  683: 
  684: # ------------------------------------------- Transfer profile into environment
  685: my $env_loaded;
  686: sub transfer_profile_to_env {
  687:     my ($lonidsdir,$handle,$force_transfer) = @_;
  688:     if (!$force_transfer && $env_loaded) { return; } 
  689: 
  690:     if (!defined($lonidsdir)) {
  691: 	$lonidsdir = $perlvar{'lonIDsDir'};
  692:     }
  693:     if (!defined($handle)) {
  694:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  695:     }
  696: 
  697:     my $convert;
  698:     {
  699:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  700: 	if (!$opened) {
  701: 	    return;
  702: 	}
  703: 	flock($idf,LOCK_SH);
  704: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  705: 		&GDBM_READER(),0640)) {
  706: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  707: 	    untie(%disk_env);
  708: 	} else {
  709: 	    $convert = 1;
  710: 	}
  711:     }
  712:     if ($convert) {
  713: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  714: 	    &logthis("Failed to load session, or convert session.");
  715: 	}
  716:     }
  717: 
  718:     my %remove;
  719:     while ( my $envname = each(%env) ) {
  720:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  721:             if ($time < time-300) {
  722:                 $remove{$key}++;
  723:             }
  724:         }
  725:     }
  726: 
  727:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  728:     $env_loaded=1;
  729:     foreach my $expired_key (keys(%remove)) {
  730:         &delenv($expired_key);
  731:     }
  732: }
  733: 
  734: # ---------------------------------------------------- Check for valid session 
  735: sub check_for_valid_session {
  736:     my ($r,$name,$userhashref,$domref) = @_;
  737:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  738:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  739:     if ($name eq 'lonDAV') {
  740:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  741:     } else {
  742:         $lonidsdir=$r->dir_config('lonIDsDir');
  743:         if ($name eq '') {
  744:             $name = 'lonID';
  745:         }
  746:     }
  747:     if ($name eq 'lonID') {
  748:         $secure = 'lonSID';
  749:         $linkname = 'lonLinkID';
  750:         $pubname = 'lonPubID';
  751:         if (exists($cookies{$secure})) {
  752:             $lonid=$cookies{$secure};
  753:         } elsif (exists($cookies{$name})) {
  754:             $lonid=$cookies{$name};
  755:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  756:             $lonid=$cookies{$linkname};
  757:         } elsif (exists($cookies{$pubname})) {
  758:             $lonid=$cookies{$pubname};
  759:         }
  760:     } else {
  761:         $lonid=$cookies{$name};
  762:     }
  763:     return undef if (!$lonid);
  764: 
  765:     my $handle=&LONCAPA::clean_handle($lonid->value);
  766:     if (-l "$lonidsdir/$handle.id") {
  767:         my $link = readlink("$lonidsdir/$handle.id");
  768:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  769:             $handle = $1;
  770:         }
  771:     }
  772:     if (!-e "$lonidsdir/$handle.id") {
  773:         if ((ref($domref)) && ($name eq 'lonID') && 
  774:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  775:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  776:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  777:                 $$domref = $possudom;
  778:             }
  779:         }
  780:         return undef;
  781:     }
  782: 
  783:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  784:     return undef if (!$opened);
  785: 
  786:     flock($idf,LOCK_SH);
  787:     my %disk_env;
  788:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  789: 	    &GDBM_READER(),0640)) {
  790: 	return undef;	
  791:     }
  792: 
  793:     if (!defined($disk_env{'user.name'})
  794: 	|| !defined($disk_env{'user.domain'})) {
  795:         untie(%disk_env);
  796: 	return undef;
  797:     }
  798: 
  799:     if (ref($userhashref) eq 'HASH') {
  800:         $userhashref->{'name'} = $disk_env{'user.name'};
  801:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  802:         if ($disk_env{'request.role'}) {
  803:             $userhashref->{'role'} = $disk_env{'request.role'};
  804:         }
  805:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  806:         if ($userhashref->{'lti'}) {
  807:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  808:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  809:         }
  810:     }
  811:     untie(%disk_env);
  812: 
  813:     return $handle;
  814: }
  815: 
  816: sub timed_flock {
  817:     my ($file,$lock_type) = @_;
  818:     my $failed=0;
  819:     eval {
  820: 	local $SIG{__DIE__}='DEFAULT';
  821: 	local $SIG{ALRM}=sub {
  822: 	    $failed=1;
  823: 	    die("failed lock");
  824: 	};
  825: 	alarm(13);
  826: 	flock($file,$lock_type);
  827: 	alarm(0);
  828:     };
  829:     if ($failed) {
  830: 	return undef;
  831:     } else {
  832: 	return 1;
  833:     }
  834: }
  835: 
  836: sub get_sessionfile_vars {
  837:     my ($handle,$lonidsdir,$storearr) = @_;
  838:     my %returnhash;
  839:     unless (ref($storearr) eq 'ARRAY') {
  840:         return %returnhash;
  841:     }
  842:     if (-l "$lonidsdir/$handle.id") {
  843:         my $link = readlink("$lonidsdir/$handle.id");
  844:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  845:             $handle = $1;
  846:         }
  847:     }
  848:     if ((-e "$lonidsdir/$handle.id") &&
  849:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  850:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  851:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  852:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  853:                 flock($idf,LOCK_SH);
  854:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  855:                         &GDBM_READER(),0640)) {
  856:                     foreach my $item (@{$storearr}) {
  857:                         $returnhash{$item} = $disk_env{$item};
  858:                     }
  859:                     untie(%disk_env);
  860:                 }
  861:             }
  862:         }
  863:     }
  864:     return %returnhash;
  865: }
  866: 
  867: # ---------------------------------------------------------- Append Environment
  868: 
  869: sub appenv {
  870:     my ($newenv,$roles) = @_;
  871:     if (ref($newenv) eq 'HASH') {
  872:         foreach my $key (keys(%{$newenv})) {
  873:             my $refused = 0;
  874: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  875:                 $refused = 1;
  876:                 if (ref($roles) eq 'ARRAY') {
  877:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  878:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  879:                         $refused = 0;
  880:                     }
  881:                 }
  882:             }
  883:             if ($refused) {
  884:                 &logthis("<font color=\"blue\">WARNING: ".
  885:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  886:                          .'</font>');
  887: 	        delete($newenv->{$key});
  888:             } else {
  889:                 $env{$key}=$newenv->{$key};
  890:             }
  891:         }
  892:         my $lonids = $perlvar{'lonIDsDir'};
  893:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  894:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  895:             if ($opened
  896: 	        && &timed_flock($env_file,LOCK_EX)
  897: 	        &&
  898: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  899: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  900: 	        while (my ($key,$value) = each(%{$newenv})) {
  901: 	            $disk_env{$key} = $value;
  902: 	        }
  903: 	        untie(%disk_env);
  904:             }
  905:         }
  906:     }
  907:     return 'ok';
  908: }
  909: # ----------------------------------------------------- Delete from Environment
  910: 
  911: sub delenv {
  912:     my ($delthis,$regexp,$roles) = @_;
  913:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  914:         my $refused = 1;
  915:         if (ref($roles) eq 'ARRAY') {
  916:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  917:             if (grep(/^\Q$role\E$/,@{$roles})) {
  918:                 $refused = 0;
  919:             }
  920:         }
  921:         if ($refused) {
  922:             &logthis("<font color=\"blue\">WARNING: ".
  923:                      "Attempt to delete from environment ".$delthis);
  924:             return 'error';
  925:         }
  926:     }
  927:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  928:     if ($opened
  929: 	&& &timed_flock($env_file,LOCK_EX)
  930: 	&&
  931: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  932: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  933: 	foreach my $key (keys(%disk_env)) {
  934: 	    if ($regexp) {
  935:                 if ($key=~/^$delthis/) {
  936:                     delete($env{$key});
  937:                     delete($disk_env{$key});
  938:                 } 
  939:             } else {
  940:                 if ($key=~/^\Q$delthis\E/) {
  941: 		    delete($env{$key});
  942: 		    delete($disk_env{$key});
  943: 	        }
  944:             }
  945: 	}
  946: 	untie(%disk_env);
  947:     }
  948:     return 'ok';
  949: }
  950: 
  951: sub get_env_multiple {
  952:     my ($name) = @_;
  953:     my @values;
  954:     if (defined($env{$name})) {
  955:         # exists is it an array
  956:         if (ref($env{$name})) {
  957:             @values=@{ $env{$name} };
  958:         } else {
  959:             $values[0]=$env{$name};
  960:         }
  961:     }
  962:     return(@values);
  963: }
  964: 
  965: # ------------------------------------------------------------------- Locking
  966: 
  967: sub set_lock {
  968:     my ($text)=@_;
  969:     $locknum++;
  970:     my $id=$$.'-'.$locknum;
  971:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  972:              'session.lock.'.$id => $text});
  973:     return $id;
  974: }
  975: 
  976: sub get_locks {
  977:     my $num=0;
  978:     my %texts=();
  979:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  980:        if ($lock=~/\w/) {
  981:           $num++;
  982:           $texts{$lock}=$env{'session.lock.'.$lock};
  983:        }
  984:    }
  985:    return ($num,%texts);
  986: }
  987: 
  988: sub remove_lock {
  989:     my ($id)=@_;
  990:     my $newlocks='';
  991:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  992:        if (($lock=~/\w/) && ($lock ne $id)) {
  993:           $newlocks.=','.$lock;
  994:        }
  995:     }
  996:     &appenv({'session.locks' => $newlocks});
  997:     &delenv('session.lock.'.$id);
  998: }
  999: 
 1000: sub remove_all_locks {
 1001:     my $activelocks=$env{'session.locks'};
 1002:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
 1003:        if ($lock=~/\w/) {
 1004:           &remove_lock($lock);
 1005:        }
 1006:     }
 1007: }
 1008: 
 1009: 
 1010: # ------------------------------------------ Find out current server userload
 1011: sub userload {
 1012:     my $numusers=0;
 1013:     {
 1014: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
 1015: 	my $filename;
 1016: 	my $curtime=time;
 1017: 	while ($filename=readdir(LONIDS)) {
 1018: 	    next if ($filename eq '.' || $filename eq '..');
 1019: 	    next if ($filename =~ /publicuser_\d+\.id/);
 1020:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
 1021: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
 1022: 	    if ($curtime-$mtime < 1800) { $numusers++; }
 1023: 	}
 1024: 	closedir(LONIDS);
 1025:     }
 1026:     my $userloadpercent=0;
 1027:     my $maxuserload=$perlvar{'lonUserLoadLim'};
 1028:     if ($maxuserload) {
 1029: 	$userloadpercent=100*$numusers/$maxuserload;
 1030:     }
 1031:     $userloadpercent=sprintf("%.2f",$userloadpercent);
 1032:     return $userloadpercent;
 1033: }
 1034: 
 1035: # ------------------------------ Find server with least workload from spare.tab
 1036: 
 1037: sub spareserver {
 1038:     my ($r,$loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
 1039:     my $spare_server;
 1040:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
 1041:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
 1042:                                                      :  $userloadpercent;
 1043:     my ($uint_dom,$remotesessions);
 1044:     if (($udom ne '') && (&domain($udom) ne '')) {
 1045:         my $uprimary_id = &domain($udom,'primary');
 1046:         $uint_dom = &internet_dom($uprimary_id);
 1047:         my %udomdefaults = &get_domain_defaults($udom);
 1048:         $remotesessions = $udomdefaults{'remotesessions'};
 1049:     }
 1050:     my $spareshash = &this_host_spares($udom);
 1051:     if (ref($spareshash) eq 'HASH') {
 1052:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1053:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1054:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
 1055:                                              $try_server));
 1056: 	        ($spare_server, $lowest_load) =
 1057: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
 1058:             }
 1059:         }
 1060: 
 1061:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
 1062: 
 1063:         if (!$found_server) {
 1064:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1065: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1066:                     next unless (&spare_can_host($udom,$uint_dom,
 1067:                                                  $remotesessions,$try_server));
 1068: 	            ($spare_server, $lowest_load) =
 1069: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1070:                 }
 1071: 	    }
 1072:         }
 1073:     }
 1074: 
 1075:     if (!$want_server_name) {
 1076:         if (defined($spare_server)) {
 1077:             my $hostname = &hostname($spare_server);
 1078:             if (defined($hostname)) {
 1079:                 my $protocol = 'http';
 1080:                 if ($protocol{$spare_server} eq 'https') {
 1081:                     $protocol = $protocol{$spare_server};
 1082:                 }
 1083:                 my $alias = &use_proxy_alias($r,$spare_server);
 1084:                 $hostname = $alias if ($alias ne '');
 1085: 	        $spare_server = $protocol.'://'.$hostname;
 1086:             }
 1087:         }
 1088:     }
 1089:     return $spare_server;
 1090: }
 1091: 
 1092: sub compare_server_load {
 1093:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1094: 
 1095:     if ($required) {
 1096:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1097:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1098:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1099:         if (($major eq '' && $minor eq '') ||
 1100:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1101:             return ($spare_server,$lowest_load);
 1102:         }
 1103:     }
 1104: 
 1105:     my $loadans     = &reply('load',    $try_server);
 1106:     my $userloadans = &reply('userload',$try_server);
 1107: 
 1108:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1109: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1110:     }
 1111: 
 1112:     my $load;
 1113:     if ($loadans =~ /\d/) {
 1114: 	if ($userloadans =~ /\d/) {
 1115: 	    #both are numbers, pick the bigger one
 1116: 	    $load = ($loadans > $userloadans) ? $loadans 
 1117: 		                              : $userloadans;
 1118: 	} else {
 1119: 	    $load = $loadans;
 1120: 	}
 1121:     } else {
 1122: 	$load = $userloadans;
 1123:     }
 1124: 
 1125:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1126: 	$spare_server = $try_server;
 1127: 	$lowest_load  = $load;
 1128:     }
 1129:     return ($spare_server,$lowest_load);
 1130: }
 1131: 
 1132: # --------------------------- ask offload servers if user already has a session
 1133: sub find_existing_session {
 1134:     my ($udom,$uname) = @_;
 1135:     my $spareshash = &this_host_spares($udom);
 1136:     if (ref($spareshash) eq 'HASH') {
 1137:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1138:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1139:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1140:             }
 1141:         }
 1142:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1143:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1144:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1145:             }
 1146:         }
 1147:     }
 1148:     return;
 1149: }
 1150: 
 1151: sub delusersession {
 1152:     my ($lonid,$udom,$uname) = @_;
 1153:     my $uprimary_id = &domain($udom,'primary');
 1154:     my $uintdom = &internet_dom($uprimary_id);
 1155:     my $intdom = &internet_dom($lonid);
 1156:     my $serverhomedom = &host_domain($lonid);
 1157:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1158:         return &reply(join(':','delusersession',
 1159:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1160:     }
 1161:     return;
 1162: }
 1163: 
 1164: # check if user's browser sent load balancer cookie and server still has session
 1165: # and is not overloaded.
 1166: sub check_for_balancer_cookie {
 1167:     my ($r,$update_mtime) = @_;
 1168:     my ($otherserver,$cookie);
 1169:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1170:     if (exists($cookies{'balanceID'})) {
 1171:         my $balid = $cookies{'balanceID'};
 1172:         $cookie=&LONCAPA::clean_handle($balid->value);
 1173:         my $balancedir=$r->dir_config('lonBalanceDir');
 1174:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1175:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1176:                 my ($possudom,$possuname) = ($1,$2);
 1177:                 my $has_session = 0;
 1178:                 if ((&domain($possudom) ne '') &&
 1179:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1180:                     my $try_server;
 1181:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1182:                     if ($opened) {
 1183:                         flock($idf,LOCK_SH);
 1184:                         while (my $line = <$idf>) {
 1185:                             chomp($line);
 1186:                             if (&hostname($line) ne '') {
 1187:                                 $try_server = $line;
 1188:                                 last;
 1189:                             }
 1190:                         }
 1191:                         close($idf);
 1192:                         if (($try_server) &&
 1193:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1194:                             my $lowest_load = 30000;
 1195:                             ($otherserver,$lowest_load) =
 1196:                                 &compare_server_load($try_server,undef,$lowest_load);
 1197:                             if ($otherserver ne '' && $lowest_load < 100) {
 1198:                                 $has_session = 1;
 1199:                             } else {
 1200:                                 undef($otherserver);
 1201:                             }
 1202:                         }
 1203:                     }
 1204:                 }
 1205:                 if ($has_session) {
 1206:                     if ($update_mtime) {
 1207:                         my $atime = my $mtime = time;
 1208:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1209:                     }
 1210:                 } else {
 1211:                     unlink("$balancedir/$cookie.id");
 1212:                 }
 1213:             }
 1214:         }
 1215:     }
 1216:     return ($otherserver,$cookie);
 1217: }
 1218: 
 1219: sub updatebalcookie {
 1220:     my ($cookie,$balancer,$lastentry)=@_;
 1221:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1222:         my ($udom,$uname) = ($1,$2);
 1223:         my $uprimary_id = &domain($udom,'primary');
 1224:         my $uintdom = &internet_dom($uprimary_id);
 1225:         my $intdom = &internet_dom($balancer);
 1226:         my $serverhomedom = &host_domain($balancer);
 1227:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1228:             return &reply('updatebalcookie:'.&escape($cookie).':'.&escape($lastentry),$balancer);
 1229:         }
 1230:     }
 1231:     return;
 1232: }
 1233: 
 1234: sub delbalcookie {
 1235:     my ($cookie,$balancer) =@_;
 1236:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1237:         my ($udom,$uname) = ($1,$2);
 1238:         my $uprimary_id = &domain($udom,'primary');
 1239:         my $uintdom = &internet_dom($uprimary_id);
 1240:         my $intdom = &internet_dom($balancer);
 1241:         my $serverhomedom = &host_domain($balancer);
 1242:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1243:             return &reply('delbalcookie:'.&escape($cookie),$balancer);
 1244:         }
 1245:     }
 1246: }
 1247: 
 1248: # -------------------------------- ask if server already has a session for user
 1249: sub has_user_session {
 1250:     my ($lonid,$udom,$uname) = @_;
 1251:     my $result = &reply(join(':','userhassession',
 1252: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1253:     return 1 if ($result eq 'ok');
 1254: 
 1255:     return 0;
 1256: }
 1257: 
 1258: # --------- determine least loaded server in a user's domain which allows login
 1259: 
 1260: sub choose_server {
 1261:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1262:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1263:     my %servers = &get_servers($udom);
 1264:     my $lowest_load = 30000;
 1265:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1266:     if ($skiploadbal) {
 1267:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1268:         unless (defined($cached)) {
 1269:             my $cachetime = 60*60*24;
 1270:             my %domconfig =
 1271:                 &get_dom('configuration',['loadbalancing'],$udom);
 1272:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1273:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1274:                                            $cachetime);
 1275:             }
 1276:         }
 1277:     }
 1278:     foreach my $lonhost (keys(%servers)) {
 1279:         if ($skiploadbal) {
 1280:             if (ref($balancers) eq 'HASH') {
 1281:                 next if (exists($balancers->{$lonhost}));
 1282:             }
 1283:         }
 1284:         my $loginvia;
 1285:         if ($checkloginvia) {
 1286:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1287:             if ($loginvia) {
 1288:                 my ($server,$path) = split(/:/,$loginvia);
 1289:                 ($login_host, $lowest_load) =
 1290:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1291:                 if ($login_host eq $server) {
 1292:                     $portal_path = $path;
 1293:                     $isredirect = 1;
 1294:                 }
 1295:             } else {
 1296:                 ($login_host, $lowest_load) =
 1297:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1298:                 if ($login_host eq $lonhost) {
 1299:                     $portal_path = '';
 1300:                     $isredirect = ''; 
 1301:                 }
 1302:             }
 1303:         } else {
 1304:             ($login_host, $lowest_load) =
 1305:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1306:         }
 1307:     }
 1308:     if ($login_host ne '') {
 1309:         $hostname = &hostname($login_host);
 1310:     }
 1311:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1312: }
 1313: 
 1314: sub get_course_sessions {
 1315:     my ($cnum,$cdom,$lastactivity) = @_;
 1316:     my %servers = &internet_dom_servers($cdom);
 1317:     my %returnhash;
 1318:     foreach my $server (sort(keys(%servers))) {
 1319:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1320:         my @pairs=split(/\&/,$rep);
 1321:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1322:             foreach my $item (@pairs) {
 1323:                 my ($key,$value)=split(/=/,$item,2);
 1324:                 $key = &unescape($key);
 1325:                 next if ($key =~ /^error: 2 /);
 1326:                 if (exists($returnhash{$key})) {
 1327:                     next if ($value < $returnhash{$key});
 1328:                 }
 1329:                 $returnhash{$key}=$value;
 1330:             }
 1331:         }
 1332:     }
 1333:     return %returnhash;
 1334: }
 1335: 
 1336: # --------------------------------------------- Try to change a user's password
 1337: 
 1338: sub changepass {
 1339:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1340:     $currentpass = &escape($currentpass);
 1341:     $newpass     = &escape($newpass);
 1342:     my $lonhost = $perlvar{'lonHostID'};
 1343:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1344: 		       $server);
 1345:     if (! $answer) {
 1346: 	&logthis("No reply on password change request to $server ".
 1347: 		 "by $uname in domain $udom.");
 1348:     } elsif ($answer =~ "^ok") {
 1349:         &logthis("$uname in $udom successfully changed their password ".
 1350: 		 "on $server.");
 1351:     } elsif ($answer =~ "^pwchange_failure") {
 1352: 	&logthis("$uname in $udom was unable to change their password ".
 1353: 		 "on $server.  The action was blocked by either lcpasswd ".
 1354: 		 "or pwchange");
 1355:     } elsif ($answer =~ "^non_authorized") {
 1356:         &logthis("$uname in $udom did not get their password correct when ".
 1357: 		 "attempting to change it on $server.");
 1358:     } elsif ($answer =~ "^auth_mode_error") {
 1359:         &logthis("$uname in $udom attempted to change their password despite ".
 1360: 		 "not being locally or internally authenticated on $server.");
 1361:     } elsif ($answer =~ "^unknown_user") {
 1362:         &logthis("$uname in $udom attempted to change their password ".
 1363: 		 "on $server but were unable to because $server is not ".
 1364: 		 "their home server.");
 1365:     } elsif ($answer =~ "^refused") {
 1366: 	&logthis("$server refused to change $uname in $udom password because ".
 1367: 		 "it was sent an unencrypted request to change the password.");
 1368:     } elsif ($answer =~ "invalid_client") {
 1369:         &logthis("$server refused to change $uname in $udom password because ".
 1370:                  "it was a reset by e-mail originating from an invalid server.");
 1371:     } elsif ($answer =~ "^prioruse") {
 1372:        &logthis("$server refused to change $uname in $udom password because ".
 1373:                 "the password had been used before");
 1374:     }
 1375:     return $answer;
 1376: }
 1377: 
 1378: # ----------------------- Try to determine user's current authentication scheme
 1379: 
 1380: sub queryauthenticate {
 1381:     my ($uname,$udom)=@_;
 1382:     my $uhome=&homeserver($uname,$udom);
 1383:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1384: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1385: 	return 'no_host';
 1386:     }
 1387:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1388:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1389: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1390:     }
 1391:     return $answer;
 1392: }
 1393: 
 1394: # --------- Try to authenticate user from domain's lib servers (first this one)
 1395: 
 1396: sub authenticate {
 1397:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1398:     $upass=&escape($upass);
 1399:     $uname= &LONCAPA::clean_username($uname);
 1400:     my $uhome=&homeserver($uname,$udom,1);
 1401:     my $newhome;
 1402:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1403: # Maybe the machine was offline and only re-appeared again recently?
 1404:         &reconlonc();
 1405: # One more
 1406: 	$uhome=&homeserver($uname,$udom,1);
 1407:         if (($uhome eq 'no_host') && $checkdefauth) {
 1408:             if (defined(&domain($udom,'primary'))) {
 1409:                 $newhome=&domain($udom,'primary');
 1410:             }
 1411:             if ($newhome ne '') {
 1412:                 $uhome = $newhome;
 1413:             }
 1414:         }
 1415: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1416: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1417: 	    return 'no_host';
 1418:         }
 1419:     }
 1420:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1421:     if ($answer eq 'authorized') {
 1422:         if ($newhome) {
 1423:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1424:             return 'no_account_on_host'; 
 1425:         } else {
 1426:             &logthis("User $uname at $udom authorized by $uhome");
 1427:             return $uhome;
 1428:         }
 1429:     }
 1430:     if ($answer eq 'non_authorized') {
 1431: 	&logthis("User $uname at $udom rejected by $uhome");
 1432: 	return 'no_host';
 1433:     }
 1434:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1435:     return 'no_host';
 1436: }
 1437: 
 1438: sub can_switchserver {
 1439:     my ($udom,$home) = @_;
 1440:     my ($canswitch,@intdoms);
 1441:     my $internet_names = &get_internet_names($home);
 1442:     if (ref($internet_names) eq 'ARRAY') {
 1443:         @intdoms = @{$internet_names};
 1444:     }
 1445:     my $uint_dom = &internet_dom(&domain($udom,'primary'));
 1446:     if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1447:         $canswitch = 1;
 1448:     } else {
 1449:          my $serverhomeID = &get_server_homeID(&hostname($home));
 1450:          my $serverhomedom = &host_domain($serverhomeID);
 1451:          my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1452:          my %udomdefaults = &get_domain_defaults($udom);
 1453:          my $remoterev = &get_server_loncaparev('',$home);
 1454:          $canswitch = &can_host_session($udom,$home,$remoterev,
 1455:                                         $udomdefaults{'remotesessions'},
 1456:                                         $defdomdefaults{'hostedsessions'});
 1457:     }
 1458:     return $canswitch;
 1459: }
 1460: 
 1461: sub can_host_session {
 1462:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1463:     my $canhost = 1;
 1464:     my $host_idn = &internet_dom($lonhost);
 1465:     if (ref($remotesessions) eq 'HASH') {
 1466:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1467:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1468:                 $canhost = 0;
 1469:             } else {
 1470:                 $canhost = 1;
 1471:             }
 1472:         }
 1473:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1474:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1475:                 $canhost = 1;
 1476:             } else {
 1477:                 $canhost = 0;
 1478:             }
 1479:         }
 1480:         if ($canhost) {
 1481:             if ($remotesessions->{'version'} ne '') {
 1482:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1483:                 if ($reqmajor ne '' && $reqminor ne '') {
 1484:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1485:                         my $major = $1;
 1486:                         my $minor = $2;
 1487:                         if (($major < $reqmajor ) ||
 1488:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1489:                             $canhost = 0;
 1490:                         }
 1491:                     } else {
 1492:                         $canhost = 0;
 1493:                     }
 1494:                 }
 1495:             }
 1496:         }
 1497:     }
 1498:     if ($canhost) {
 1499:         if (ref($hostedsessions) eq 'HASH') {
 1500:             my $uprimary_id = &domain($udom,'primary');
 1501:             my $uint_dom = &internet_dom($uprimary_id);
 1502:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1503:                 if (($uint_dom ne '') && 
 1504:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1505:                     $canhost = 0;
 1506:                 } else {
 1507:                     $canhost = 1;
 1508:                 }
 1509:             }
 1510:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1511:                 if (($uint_dom ne '') && 
 1512:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1513:                     $canhost = 1;
 1514:                 } else {
 1515:                     $canhost = 0;
 1516:                 }
 1517:             }
 1518:         }
 1519:     }
 1520:     return $canhost;
 1521: }
 1522: 
 1523: sub spare_can_host {
 1524:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1525:     my $canhost=1;
 1526:     my $try_server_hostname = &hostname($try_server);
 1527:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1528:     my $serverhomedom = &host_domain($serverhomeID);
 1529:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1530:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1531:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1532:             $canhost = 0;
 1533:         }
 1534:     }
 1535:     if ($canhost) {
 1536:         if (ref($defdomdefaults{'offloadoth'}) eq 'HASH') {
 1537:             if ($defdomdefaults{'offloadoth'}{$try_server}) {
 1538:                 unless (&shared_institution($udom,$try_server)) {
 1539:                     $canhost = 0;
 1540:                 }
 1541:             }
 1542:         }
 1543:     }
 1544:     if (($canhost) && ($uint_dom)) {
 1545:         my @intdoms;
 1546:         my $internet_names = &get_internet_names($try_server);
 1547:         if (ref($internet_names) eq 'ARRAY') {
 1548:             @intdoms = @{$internet_names};
 1549:         }
 1550:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1551:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1552:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1553:                                          $remotesessions,
 1554:                                          $defdomdefaults{'hostedsessions'});
 1555:         }
 1556:     }
 1557:     return $canhost;
 1558: }
 1559: 
 1560: sub this_host_spares {
 1561:     my ($dom) = @_;
 1562:     my ($dom_in_use,$lonhost_in_use,$result);
 1563:     my @hosts = &current_machine_ids();
 1564:     foreach my $lonhost (@hosts) {
 1565:         if (&host_domain($lonhost) eq $dom) {
 1566:             $dom_in_use = $dom;
 1567:             $lonhost_in_use = $lonhost;
 1568:             last;
 1569:         }
 1570:     }
 1571:     if ($dom_in_use ne '') {
 1572:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1573:     }
 1574:     if (ref($result) ne 'HASH') {
 1575:         $lonhost_in_use = $perlvar{'lonHostID'};
 1576:         $dom_in_use = &host_domain($lonhost_in_use);
 1577:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1578:         if (ref($result) ne 'HASH') {
 1579:             $result = \%spareid;
 1580:         }
 1581:     }
 1582:     return $result;
 1583: }
 1584: 
 1585: sub spares_for_offload  {
 1586:     my ($dom_in_use,$lonhost_in_use) = @_;
 1587:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1588:     if (defined($cached)) {
 1589:         return $result;
 1590:     } else {
 1591:         my $cachetime = 60*60*24;
 1592:         my %domconfig =
 1593:             &get_dom('configuration',['usersessions'],$dom_in_use);
 1594:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1595:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1596:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1597:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1598:                 }
 1599:             }
 1600:         }
 1601:     }
 1602:     return;
 1603: }
 1604: 
 1605: sub get_lonbalancer_config {
 1606:     my ($servers) = @_;
 1607:     my ($currbalancer,$currtargets);
 1608:     if (ref($servers) eq 'HASH') {
 1609:         foreach my $server (keys(%{$servers})) {
 1610:             my %what = (
 1611:                          spareid => 1,
 1612:                          perlvar => 1,
 1613:                        );
 1614:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1615:             if ($result eq 'ok') {
 1616:                 if (ref($returnhash) eq 'HASH') {
 1617:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1618:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1619:                             $currbalancer = $server;
 1620:                             $currtargets = {};
 1621:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1622:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1623:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1624:                                 }
 1625:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1626:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1627:                                 }
 1628:                             }
 1629:                             last;
 1630:                         }
 1631:                     }
 1632:                 }
 1633:             }
 1634:         }
 1635:     }
 1636:     return ($currbalancer,$currtargets);
 1637: }
 1638: 
 1639: sub check_loadbalancing {
 1640:     my ($uname,$udom,$caller) = @_;
 1641:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1642:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1643:     my $lonhost = $perlvar{'lonHostID'};
 1644:     my @hosts = &current_machine_ids();
 1645:     my $uprimary_id = &domain($udom,'primary');
 1646:     my $uintdom = &internet_dom($uprimary_id);
 1647:     my $intdom = &internet_dom($lonhost);
 1648:     my $serverhomedom = &host_domain($lonhost);
 1649:     my $domneedscache;
 1650:     my $cachetime = 60*60*24;
 1651: 
 1652:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1653:         $dom_in_use = $udom;
 1654:         $homeintdom = 1;
 1655:     } else {
 1656:         $dom_in_use = $serverhomedom;
 1657:     }
 1658:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1659:     unless (defined($cached)) {
 1660:         my %domconfig =
 1661:             &get_dom('configuration',['loadbalancing'],$dom_in_use);
 1662:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1663:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1664:         } else {
 1665:             $domneedscache = $dom_in_use;
 1666:         }
 1667:     }
 1668:     if (ref($result) eq 'HASH') {
 1669:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1670:             &check_balancer_result($result,@hosts);
 1671:         if ($is_balancer) {
 1672:             if (ref($currrules) eq 'HASH') {
 1673:                 if ($homeintdom) {
 1674:                     if ($uname ne '') {
 1675:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1676:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1677:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1678:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1679:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1680:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1681:                             }
 1682:                         }
 1683:                         if ($rule_in_effect eq '') {
 1684:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1685:                             if ($userenv{'inststatus'} ne '') {
 1686:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1687:                                 my ($othertitle,$usertypes,$types) =
 1688:                                     &Apache::loncommon::sorted_inst_types($udom);
 1689:                                 if (ref($types) eq 'ARRAY') {
 1690:                                     foreach my $type (@{$types}) {
 1691:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1692:                                             if (exists($currrules->{$type})) {
 1693:                                                 $rule_in_effect = $currrules->{$type};
 1694:                                             }
 1695:                                         }
 1696:                                     }
 1697:                                 }
 1698:                             } else {
 1699:                                 if (exists($currrules->{'default'})) {
 1700:                                     $rule_in_effect = $currrules->{'default'};
 1701:                                 }
 1702:                             }
 1703:                         }
 1704:                     } else {
 1705:                         if (exists($currrules->{'default'})) {
 1706:                             $rule_in_effect = $currrules->{'default'};
 1707:                         }
 1708:                     }
 1709:                 } else {
 1710:                     if ($currrules->{'_LC_external'} ne '') {
 1711:                         $rule_in_effect = $currrules->{'_LC_external'};
 1712:                     }
 1713:                 }
 1714:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1715:                                                        $uname,$udom);
 1716:             }
 1717:         }
 1718:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1719:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1720:         unless (defined($cached)) {
 1721:             my %domconfig =
 1722:                 &get_dom('configuration',['loadbalancing'],$serverhomedom);
 1723:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1724:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1725:             } else {
 1726:                 $domneedscache = $serverhomedom;
 1727:             }
 1728:         }
 1729:         if (ref($result) eq 'HASH') {
 1730:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1731:                 &check_balancer_result($result,@hosts);
 1732:             if ($is_balancer) {
 1733:                 if (ref($currrules) eq 'HASH') {
 1734:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1735:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1736:                     }
 1737:                 }
 1738:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1739:                                                        $uname,$udom);
 1740:             }
 1741:         } else {
 1742:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1743:                 $is_balancer = 1;
 1744:                 $offloadto = &this_host_spares($dom_in_use);
 1745:             }
 1746:             unless (defined($cached)) {
 1747:                 $domneedscache = $serverhomedom;
 1748:             }
 1749:         }
 1750:     } else {
 1751:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1752:             $is_balancer = 1;
 1753:             $offloadto = &this_host_spares($dom_in_use);
 1754:         }
 1755:         unless (defined($cached)) {
 1756:             $domneedscache = $serverhomedom;
 1757:         }
 1758:     }
 1759:     if ($domneedscache) {
 1760:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1761:     }
 1762:     if (($is_balancer) && ($caller ne 'switchserver')) {
 1763:         my $lowest_load = 30000;
 1764:         if (ref($offloadto) eq 'HASH') {
 1765:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1766:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1767:                     ($otherserver,$lowest_load) =
 1768:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1769:                 }
 1770:             }
 1771:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1772: 
 1773:             if (!$found_server) {
 1774:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1775:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1776:                         ($otherserver,$lowest_load) =
 1777:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1778:                     }
 1779:                 }
 1780:             }
 1781:         } elsif (ref($offloadto) eq 'ARRAY') {
 1782:             if (@{$offloadto} == 1) {
 1783:                 $otherserver = $offloadto->[0];
 1784:             } elsif (@{$offloadto} > 1) {
 1785:                 foreach my $try_server (@{$offloadto}) {
 1786:                     ($otherserver,$lowest_load) =
 1787:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1788:                 }
 1789:             }
 1790:         }
 1791:         unless ($caller eq 'login') {
 1792:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1793:                 $is_balancer = 0;
 1794:                 if ($uname ne '' && $udom ne '') {
 1795:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1796:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1797:                                  'user.loadbalcheck.time' => time});
 1798:                     }
 1799:                 }
 1800:             }
 1801:         }
 1802:     }
 1803:     if (($is_balancer) && (!$homeintdom)) {
 1804:         undef($setcookie);
 1805:     }
 1806:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1807: }
 1808: 
 1809: sub check_balancer_result {
 1810:     my ($result,@hosts) = @_;
 1811:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1812:     if (ref($result) eq 'HASH') {
 1813:         if ($result->{'lonhost'} ne '') {
 1814:             my $currbalancer = $result->{'lonhost'};
 1815:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1816:                 $is_balancer = 1;
 1817:                 $currtargets = $result->{'targets'};
 1818:                 $currrules = $result->{'rules'};
 1819:             }
 1820:             $dom_balancers = $currbalancer;
 1821:         } else {
 1822:             if (keys(%{$result})) {
 1823:                 foreach my $key (keys(%{$result})) {
 1824:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1825:                         (ref($result->{$key}) eq 'HASH')) {
 1826:                         $is_balancer = 1;
 1827:                         $currrules = $result->{$key}{'rules'};
 1828:                         $currtargets = $result->{$key}{'targets'};
 1829:                         $setcookie = $result->{$key}{'cookie'};
 1830:                         last;
 1831:                     }
 1832:                 }
 1833:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1834:             }
 1835:         }
 1836:     }
 1837:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1838: }
 1839: 
 1840: sub get_loadbalancer_targets {
 1841:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1842:     my $offloadto;
 1843:     if ($rule_in_effect eq 'none') {
 1844:         return [$perlvar{'lonHostID'}];
 1845:     } elsif ($rule_in_effect eq '') {
 1846:         $offloadto = $currtargets;
 1847:     } else {
 1848:         if ($rule_in_effect eq 'homeserver') {
 1849:             my $homeserver = &homeserver($uname,$udom);
 1850:             if ($homeserver ne 'no_host') {
 1851:                 $offloadto = [$homeserver];
 1852:             }
 1853:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1854:             my %domconfig =
 1855:                 &get_dom('configuration',['loadbalancing'],$udom);
 1856:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1857:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1858:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1859:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1860:                     }
 1861:                 }
 1862:             } else {
 1863:                 my %servers = &internet_dom_servers($udom);
 1864:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1865:                 if (&hostname($remotebalancer) ne '') {
 1866:                     $offloadto = [$remotebalancer];
 1867:                 }
 1868:             }
 1869:         } elsif (&hostname($rule_in_effect) ne '') {
 1870:             $offloadto = [$rule_in_effect];
 1871:         }
 1872:     }
 1873:     return $offloadto;
 1874: }
 1875: 
 1876: sub internet_dom_servers {
 1877:     my ($dom) = @_;
 1878:     my (%uniqservers,%servers);
 1879:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1880:     my @machinedoms = &machine_domains($primaryserver);
 1881:     foreach my $mdom (@machinedoms) {
 1882:         my %currservers = %servers;
 1883:         my %server = &get_servers($mdom);
 1884:         %servers = (%currservers,%server);
 1885:     }
 1886:     my %by_hostname;
 1887:     foreach my $id (keys(%servers)) {
 1888:         push(@{$by_hostname{$servers{$id}}},$id);
 1889:     }
 1890:     foreach my $hostname (sort(keys(%by_hostname))) {
 1891:         if (@{$by_hostname{$hostname}} > 1) {
 1892:             my $match = 0;
 1893:             foreach my $id (@{$by_hostname{$hostname}}) {
 1894:                 if (&host_domain($id) eq $dom) {
 1895:                     $uniqservers{$id} = $hostname;
 1896:                     $match = 1;
 1897:                 }
 1898:             }
 1899:             unless ($match) {
 1900:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1901:             }
 1902:         } else {
 1903:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1904:         }
 1905:     }
 1906:     return %uniqservers;
 1907: }
 1908: 
 1909: sub trusted_domains {
 1910:     my ($cmdtype,$calldom) = @_;
 1911:     my ($trusted,$untrusted);
 1912:     if (&domain($calldom) eq '') {
 1913:         return ($trusted,$untrusted);
 1914:     }
 1915:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1916:         return ($trusted,$untrusted);
 1917:     }
 1918:     my $callprimary = &domain($calldom,'primary');
 1919:     my $intcalldom = &internet_dom($callprimary);
 1920:     if ($intcalldom eq '') {
 1921:         return ($trusted,$untrusted);
 1922:     }
 1923: 
 1924:     my ($trustconfig,$cached)=&is_cached_new('trust',$calldom);
 1925:     unless (defined($cached)) {
 1926:         my %domconfig = &get_dom('configuration',['trust'],$calldom);
 1927:         &do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1928:         $trustconfig = $domconfig{'trust'};
 1929:     }
 1930:     if (ref($trustconfig)) {
 1931:         my (%possexc,%possinc,@allexc,@allinc); 
 1932:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1933:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1934:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1935:             }
 1936:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1937:                 $possinc{$intcalldom} = 1;
 1938:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1939:             }
 1940:         }
 1941:         if (keys(%possexc)) {
 1942:             if (keys(%possinc)) {
 1943:                 foreach my $key (sort(keys(%possexc))) {
 1944:                     next if ($key eq $intcalldom);
 1945:                     unless ($possinc{$key}) {
 1946:                         push(@allexc,$key);
 1947:                     }
 1948:                 }
 1949:             } else {
 1950:                 @allexc = sort(keys(%possexc));
 1951:             }
 1952:         }
 1953:         if (keys(%possinc)) {
 1954:             $possinc{$intcalldom} = 1;
 1955:             @allinc = sort(keys(%possinc));
 1956:         }
 1957:         if ((@allexc > 0) || (@allinc > 0)) {
 1958:             my %doms_by_intdom;
 1959:             my %allintdoms = &all_host_intdom();
 1960:             my %alldoms = &all_host_domain();
 1961:             foreach my $key (%allintdoms) {
 1962:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1963:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1964:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1965:                     }
 1966:                 } else {
 1967:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1968:                 }
 1969:             }
 1970:             foreach my $exc (@allexc) {
 1971:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1972:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1973:                 }
 1974:             }
 1975:             foreach my $inc (@allinc) {
 1976:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1977:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1978:                 }
 1979:             }
 1980:         }
 1981:     }
 1982:     return ($trusted,$untrusted);
 1983: }
 1984: 
 1985: sub will_trust {
 1986:     my ($cmdtype,$domain,$possdom) = @_;
 1987:     return 1 if ($domain eq $possdom);
 1988:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1989:     my $willtrust; 
 1990:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1991:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1992:             $willtrust = 1;
 1993:         }
 1994:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1995:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1996:             $willtrust = 1;
 1997:         }
 1998:     } else {
 1999:         $willtrust = 1;
 2000:     }
 2001:     return $willtrust;
 2002: }
 2003: 
 2004: # ---------------------- Find the homebase for a user from domain's lib servers
 2005: 
 2006: my %homecache;
 2007: sub homeserver {
 2008:     my ($uname,$udom,$ignoreBadCache)=@_;
 2009:     my $index="$uname:$udom";
 2010: 
 2011:     if (exists($homecache{$index})) { return $homecache{$index}; }
 2012: 
 2013:     my %servers = &get_servers($udom,'library');
 2014:     foreach my $tryserver (keys(%servers)) {
 2015:         next if ($ignoreBadCache ne 'true' && 
 2016: 		 exists($badServerCache{$tryserver}));
 2017: 
 2018: 	my $answer=reply("home:$udom:$uname",$tryserver);
 2019: 	if ($answer eq 'found') {
 2020: 	    delete($badServerCache{$tryserver}); 
 2021: 	    return $homecache{$index}=$tryserver;
 2022: 	} elsif ($answer eq 'no_host') {
 2023: 	    $badServerCache{$tryserver}=1;
 2024: 	}
 2025:     }    
 2026:     return 'no_host';
 2027: }
 2028: 
 2029: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 2030: 
 2031: sub idget {
 2032:     my ($udom,$idsref,$namespace)=@_;
 2033:     my %returnhash=();
 2034:     my @ids=(); 
 2035:     if (ref($idsref) eq 'ARRAY') {
 2036:         @ids = @{$idsref};
 2037:     } else {
 2038:         return %returnhash; 
 2039:     }
 2040:     if ($namespace eq '') {
 2041:         $namespace = 'ids';
 2042:     }
 2043:     
 2044:     my %servers = &get_servers($udom,'library');
 2045:     foreach my $tryserver (keys(%servers)) {
 2046: 	my $idlist=join('&', map { &escape($_); } @ids);
 2047: 	if ($namespace eq 'ids') {
 2048: 	    $idlist=~tr/A-Z/a-z/;
 2049: 	}
 2050: 	my $reply;
 2051: 	if ($namespace eq 'ids') {
 2052: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 2053: 	} else {
 2054: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 2055: 	}
 2056: 	my @answer=();
 2057: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 2058: 	    @answer=split(/\&/,$reply);
 2059: 	}                    ;
 2060: 	my $i;
 2061: 	for ($i=0;$i<=$#ids;$i++) {
 2062: 	    if ($answer[$i]) {
 2063: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 2064: 	    }
 2065: 	}
 2066:     }
 2067:     return %returnhash;
 2068: }
 2069: 
 2070: # ------------------------------------- Find the IDs behind a list of usernames
 2071: 
 2072: sub idrget {
 2073:     my ($udom,@unames)=@_;
 2074:     my %returnhash=();
 2075:     foreach my $uname (@unames) {
 2076:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 2077:     }
 2078:     return %returnhash;
 2079: }
 2080: 
 2081: # Store away a list of names and associated student/employee IDs or clicker IDs
 2082: 
 2083: sub idput {
 2084:     my ($udom,$idsref,$uhom,$namespace)=@_;
 2085:     my %servers=();
 2086:     my %ids=();
 2087:     my %byid = ();
 2088:     if (ref($idsref) eq 'HASH') {
 2089:         %ids=%{$idsref};
 2090:     }
 2091:     if ($namespace eq '') {
 2092:         $namespace = 'ids'; 
 2093:     }
 2094:     foreach my $uname (keys(%ids)) {
 2095: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 2096:         if ($uhom eq '') {
 2097:             $uhom=&homeserver($uname,$udom);
 2098:         }
 2099:         if ($uhom ne 'no_host') {
 2100:             my $esc_unam=&escape($uname);
 2101:             if ($namespace eq 'ids') {
 2102:                 my $id=&escape($ids{$uname});
 2103:                 $id=~tr/A-Z/a-z/;
 2104:                 my $esc_unam=&escape($uname);
 2105:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 2106:             } else {
 2107:                 my @currids = split(/,/,$ids{$uname});
 2108:                 foreach my $id (@currids) {
 2109:                     $byid{$uhom}{$id} .= $uname.',';
 2110:                 }
 2111:             }
 2112:         }
 2113:     }
 2114:     if ($namespace eq 'clickers') {
 2115:         foreach my $server (keys(%byid)) {
 2116:             if (ref($byid{$server}) eq 'HASH') {
 2117:                 foreach my $id (keys(%{$byid{$server}})) {
 2118:                     $byid{$server} =~ s/,$//;
 2119:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2120:                 }
 2121:             }
 2122:         }
 2123:     }
 2124:     foreach my $server (keys(%servers)) {
 2125:         $servers{$server} =~ s/\&$//;
 2126:         if ($namespace eq 'ids') {     
 2127:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2128:         } else {
 2129:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2130:         }
 2131:     }
 2132: }
 2133: 
 2134: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2135: 
 2136: sub iddel {
 2137:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2138:     my %result=();
 2139:     my %ids=();
 2140:     my %byid = ();
 2141:     if (ref($idshashref) eq 'HASH') {
 2142:         %ids=%{$idshashref};
 2143:     } else {
 2144:         return %result;
 2145:     }
 2146:     if ($namespace eq '') {
 2147:         $namespace = 'ids';
 2148:     }
 2149:     my %servers=();
 2150:     while (my ($id,$unamestr) = each(%ids)) {
 2151:         if ($namespace eq 'ids') {
 2152:             my $uhom = $uhome;
 2153:             if ($uhom eq '') { 
 2154:                 $uhom=&homeserver($unamestr,$udom);
 2155:             }
 2156:             if ($uhom ne 'no_host') {
 2157:                 $servers{$uhom}.='&'.&escape($id);
 2158:             }
 2159:          } else {
 2160:             my @curritems = split(/,/,$ids{$id});
 2161:             foreach my $uname (@curritems) {
 2162:                 my $uhom = $uhome;
 2163:                 if ($uhom eq '') {
 2164:                     $uhom=&homeserver($uname,$udom);
 2165:                 }
 2166:                 if ($uhom ne 'no_host') { 
 2167:                     $byid{$uhom}{$id} .= $uname.',';
 2168:                 }
 2169:             }
 2170:         }
 2171:     }
 2172:     if ($namespace eq 'clickers') {
 2173:         foreach my $server (keys(%byid)) {
 2174:             if (ref($byid{$server}) eq 'HASH') {
 2175:                 foreach my $id (keys(%{$byid{$server}})) {
 2176:                     $byid{$server}{$id} =~ s/,$//;
 2177:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2178:                 }
 2179:             }
 2180:         }
 2181:     }
 2182:     foreach my $server (keys(%servers)) {
 2183:         $servers{$server} =~ s/\&$//;
 2184:         if ($namespace eq 'ids') {
 2185:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2186:         } elsif ($namespace eq 'clickers') {
 2187:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2188:         }
 2189:     }
 2190:     return %result;
 2191: }
 2192: 
 2193: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2194: 
 2195: sub updateclickers {
 2196:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2197:     my %clickers;
 2198:     if (ref($idshashref) eq 'HASH') {
 2199:         %clickers=%{$idshashref};
 2200:     } else {
 2201:         return;
 2202:     }
 2203:     my $items='';
 2204:     foreach my $item (keys(%clickers)) {
 2205:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2206:     }
 2207:     $items=~s/\&$//;
 2208:     my $request = "updateclickers:$udom:$action:$items";
 2209:     if ($critical) {
 2210:         return &critical($request,$uhome);
 2211:     } else {
 2212:         return &reply($request,$uhome);
 2213:     }
 2214: }
 2215: 
 2216: # ------------------------------dump from db file owned by domainconfig user
 2217: sub dump_dom {
 2218:     my ($namespace, $udom, $regexp) = @_;
 2219: 
 2220:     $udom ||= $env{'user.domain'};
 2221: 
 2222:     return () unless $udom;
 2223: 
 2224:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2225: }
 2226: 
 2227: # ------------------------------------------ get items from domain db files   
 2228: 
 2229: sub get_dom {
 2230:     my ($namespace,$storearr,$udom,$uhome,$encrypt)=@_;
 2231:     return if ($udom eq 'public');
 2232:     my $items='';
 2233:     foreach my $item (@$storearr) {
 2234:         $items.=&escape($item).'&';
 2235:     }
 2236:     $items=~s/\&$//;
 2237:     if (!$udom) {
 2238:         $udom=$env{'user.domain'};
 2239:         return if ($udom eq 'public');
 2240:         if (defined(&domain($udom,'primary'))) {
 2241:             $uhome=&domain($udom,'primary');
 2242:         } else {
 2243:             undef($uhome);
 2244:         }
 2245:     } else {
 2246:         if (!$uhome) {
 2247:             if (defined(&domain($udom,'primary'))) {
 2248:                 $uhome=&domain($udom,'primary');
 2249:             }
 2250:         }
 2251:     }
 2252:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2253:         my $rep;
 2254:         if (grep { $_ eq $uhome } &current_machine_ids()) {
 2255:             # domain information is hosted on this machine
 2256:             $rep = &LONCAPA::Lond::get_dom("getdom:$udom:$namespace:$items");
 2257:         } else {
 2258:             if ($encrypt) {
 2259:                 $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2260:             } else {
 2261:                 $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2262:             }
 2263:         }
 2264:         my %returnhash;
 2265:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2266:             return %returnhash;
 2267:         }
 2268:         my @pairs=split(/\&/,$rep);
 2269:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2270:             return @pairs;
 2271:         }
 2272:         my $i=0;
 2273:         foreach my $item (@$storearr) {
 2274:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2275:             $i++;
 2276:         }
 2277:         return %returnhash;
 2278:     } else {
 2279:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2280:     }
 2281: }
 2282: 
 2283: # -------------------------------------------- put items in domain db files 
 2284: 
 2285: sub put_dom {
 2286:     my ($namespace,$storehash,$udom,$uhome,$encrypt)=@_;
 2287:     if (!$udom) {
 2288:         $udom=$env{'user.domain'};
 2289:         if (defined(&domain($udom,'primary'))) {
 2290:             $uhome=&domain($udom,'primary');
 2291:         } else {
 2292:             undef($uhome);
 2293:         }
 2294:     } else {
 2295:         if (!$uhome) {
 2296:             if (defined(&domain($udom,'primary'))) {
 2297:                 $uhome=&domain($udom,'primary');
 2298:             }
 2299:         }
 2300:     } 
 2301:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2302:         my $items='';
 2303:         foreach my $item (keys(%$storehash)) {
 2304:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2305:         }
 2306:         $items=~s/\&$//;
 2307:         if ($encrypt) {
 2308:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2309:         } else {
 2310:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2311:         }
 2312:     } else {
 2313:         &logthis("put_dom failed - no homeserver and/or domain");
 2314:     }
 2315: }
 2316: 
 2317: # --------------------- newput for items in db file owned by domainconfig user
 2318: sub newput_dom {
 2319:     my ($namespace,$storehash,$udom) = @_;
 2320:     my $result;
 2321:     if (!$udom) {
 2322:         $udom=$env{'user.domain'};
 2323:     }
 2324:     if ($udom) {
 2325:         my $uname = &get_domainconfiguser($udom);
 2326:         $result = &newput($namespace,$storehash,$udom,$uname);
 2327:     }
 2328:     return $result;
 2329: }
 2330: 
 2331: # --------------------- delete for items in db file owned by domainconfig user
 2332: sub del_dom {
 2333:     my ($namespace,$storearr,$udom)=@_;
 2334:     if (ref($storearr) eq 'ARRAY') {
 2335:         if (!$udom) {
 2336:             $udom=$env{'user.domain'};
 2337:         }
 2338:         if ($udom) {
 2339:             my $uname = &get_domainconfiguser($udom); 
 2340:             return &del($namespace,$storearr,$udom,$uname);
 2341:         }
 2342:     }
 2343: }
 2344: 
 2345: sub store_dom {
 2346:     my ($storehash,$id,$namespace,$dom,$home,$encrypt) = @_;
 2347:     $$storehash{'ip'}=&get_requestor_ip();
 2348:     $$storehash{'host'}=$perlvar{'lonHostID'};
 2349:     my $namevalue='';
 2350:     foreach my $key (keys(%{$storehash})) {
 2351:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 2352:     }
 2353:     $namevalue=~s/\&$//;
 2354:     if (grep { $_ eq $home } current_machine_ids()) {
 2355:         return LONCAPA::Lond::store_dom("storedom:$dom:$namespace:$id:$namevalue");
 2356:     } else {
 2357:         if ($namespace eq 'private') {
 2358:             return 'refused';
 2359:         } elsif ($encrypt) {
 2360:             return reply("encrypt:storedom:$dom:$namespace:$id:$namevalue",$home);
 2361:         } else {
 2362:             return reply("storedom:$dom:$namespace:$id:$namevalue",$home);
 2363:         }
 2364:     }
 2365: }
 2366: 
 2367: sub restore_dom {
 2368:     my ($id,$namespace,$dom,$home,$encrypt) = @_;
 2369:     my $answer;
 2370:     if (grep { $_ eq $home } current_machine_ids()) {
 2371:         $answer = LONCAPA::Lond::restore_dom("restoredom:$dom:$namespace:$id");
 2372:     } elsif ($namespace ne 'private') {
 2373:         if ($encrypt) {
 2374:             $answer=&reply("encrypt:restoredom:$dom:$namespace:$id",$home);
 2375:         } else {
 2376:             $answer=&reply("restoredom:$dom:$namespace:$id",$home);
 2377:         }
 2378:     }
 2379:     my %returnhash=();
 2380:     unless (($answer eq '') || ($answer eq 'con_lost') || ($answer eq 'refused') || 
 2381:             ($answer eq 'unknown_cmd') || ($answer eq 'rejected')) {
 2382:         foreach my $line (split(/\&/,$answer)) {
 2383:             my ($name,$value)=split(/\=/,$line);
 2384:             $returnhash{&unescape($name)}=&thaw_unescape($value);
 2385:         }
 2386:         my $version;
 2387:         for ($version=1;$version<=$returnhash{'version'};$version++) {
 2388:             foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 2389:                 $returnhash{$item}=$returnhash{$version.':'.$item};
 2390:             }
 2391:         }
 2392:     }
 2393:     return %returnhash;
 2394: }
 2395: 
 2396: # ----------------------------------construct domainconfig user for a domain 
 2397: sub get_domainconfiguser {
 2398:     my ($udom) = @_;
 2399:     return $udom.'-domainconfig';
 2400: }
 2401: 
 2402: sub retrieve_inst_usertypes {
 2403:     my ($udom) = @_;
 2404:     my (%returnhash,@order);
 2405:     my %domdefs = &get_domain_defaults($udom);
 2406:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2407:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2408:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2409:     } else {
 2410:         if (defined(&domain($udom,'primary'))) {
 2411:             my $uhome=&domain($udom,'primary');
 2412:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2413:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2414:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2415:                 return (\%returnhash,\@order);
 2416:             }
 2417:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2418:             my @pairs=split(/\&/,$hashitems);
 2419:             foreach my $item (@pairs) {
 2420:                 my ($key,$value)=split(/=/,$item,2);
 2421:                 $key = &unescape($key);
 2422:                 next if ($key =~ /^error: 2 /);
 2423:                 $returnhash{$key}=&thaw_unescape($value);
 2424:             }
 2425:             my @esc_order = split(/\&/,$orderitems);
 2426:             foreach my $item (@esc_order) {
 2427:                 push(@order,&unescape($item));
 2428:             }
 2429:         } else {
 2430:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2431:         }
 2432:         return (\%returnhash,\@order);
 2433:     }
 2434: }
 2435: 
 2436: sub is_domainimage {
 2437:     my ($url) = @_;
 2438:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo|login)/+[^/]-) {
 2439:         if (&domain($1) ne '') {
 2440:             return '1';
 2441:         }
 2442:     }
 2443:     return;
 2444: }
 2445: 
 2446: sub inst_directory_query {
 2447:     my ($srch) = @_;
 2448:     my $udom = $srch->{'srchdomain'};
 2449:     my %results;
 2450:     my $homeserver = &domain($udom,'primary');
 2451:     my $outcome;
 2452:     if ($homeserver ne '') {
 2453:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2454:             if ($srch->{'srchby'} eq 'email') {
 2455:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2456:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2457:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2458:                     (($major == 2) && ($minor < 12))) {
 2459:                     return;
 2460:                 }
 2461:             }
 2462:         }
 2463: 	my $queryid=&reply("querysend:instdirsearch:".
 2464: 			   &escape($srch->{'srchby'}).':'.
 2465: 			   &escape($srch->{'srchterm'}).':'.
 2466: 			   &escape($srch->{'srchtype'}),$homeserver);
 2467: 	my $host=&hostname($homeserver);
 2468: 	if ($queryid !~/^\Q$host\E\_/) {
 2469: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2470: 	    return;
 2471: 	}
 2472: 	my $response = &get_query_reply($queryid);
 2473: 	my $maxtries = 5;
 2474: 	my $tries = 1;
 2475: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2476: 	    $response = &get_query_reply($queryid);
 2477: 	    $tries ++;
 2478: 	}
 2479: 
 2480:         if (!&error($response) && $response ne 'refused') {
 2481:             if ($response eq 'unavailable') {
 2482:                 $outcome = $response;
 2483:             } else {
 2484:                 $outcome = 'ok';
 2485:                 my @matches = split(/\n/,$response);
 2486:                 foreach my $match (@matches) {
 2487:                     my ($key,$value) = split(/=/,$match);
 2488:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2489:                 }
 2490:             }
 2491:         }
 2492:     }
 2493:     return ($outcome,%results);
 2494: }
 2495: 
 2496: sub usersearch {
 2497:     my ($srch) = @_;
 2498:     my $dom = $srch->{'srchdomain'};
 2499:     my %results;
 2500:     my %libserv = &all_library();
 2501:     my $query = 'usersearch';
 2502:     foreach my $tryserver (keys(%libserv)) {
 2503:         if (&host_domain($tryserver) eq $dom) {
 2504:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2505:                 if ($srch->{'srchby'} eq 'email') {
 2506:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2507:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2508:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2509:                              (($major == 2) && ($minor < 12)));
 2510:                 }
 2511:             }
 2512:             my $host=&hostname($tryserver);
 2513:             my $queryid=
 2514:                 &reply("querysend:".&escape($query).':'.
 2515:                        &escape($srch->{'srchby'}).':'.
 2516:                        &escape($srch->{'srchtype'}).':'.
 2517:                        &escape($srch->{'srchterm'}),$tryserver);
 2518:             if ($queryid !~/^\Q$host\E\_/) {
 2519:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2520:                 next;
 2521:             }
 2522:             my $reply = &get_query_reply($queryid);
 2523:             my $maxtries = 1;
 2524:             my $tries = 1;
 2525:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2526:                 $reply = &get_query_reply($queryid);
 2527:                 $tries ++;
 2528:             }
 2529:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2530:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2531:             } else {
 2532:                 my @matches;
 2533:                 if ($reply =~ /\n/) {
 2534:                     @matches = split(/\n/,$reply);
 2535:                 } else {
 2536:                     @matches = split(/\&/,$reply);
 2537:                 }
 2538:                 foreach my $match (@matches) {
 2539:                     my ($uname,$udom,%userhash);
 2540:                     foreach my $entry (split(/:/,$match)) {
 2541:                         my ($key,$value) =
 2542:                             map {&unescape($_);} split(/=/,$entry);
 2543:                         $userhash{$key} = $value;
 2544:                         if ($key eq 'username') {
 2545:                             $uname = $value;
 2546:                         } elsif ($key eq 'domain') {
 2547:                             $udom = $value;
 2548:                         }
 2549:                     }
 2550:                     $results{$uname.':'.$udom} = \%userhash;
 2551:                 }
 2552:             }
 2553:         }
 2554:     }
 2555:     return %results;
 2556: }
 2557: 
 2558: sub get_instuser {
 2559:     my ($udom,$uname,$id) = @_;
 2560:     my $homeserver = &domain($udom,'primary');
 2561:     my ($outcome,%results);
 2562:     if ($homeserver ne '') {
 2563:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2564:                            &escape($id).':'.&escape($udom),$homeserver);
 2565:         my $host=&hostname($homeserver);
 2566:         if ($queryid !~/^\Q$host\E\_/) {
 2567:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2568:             return;
 2569:         }
 2570:         my $response = &get_query_reply($queryid);
 2571:         my $maxtries = 5;
 2572:         my $tries = 1;
 2573:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2574:             $response = &get_query_reply($queryid);
 2575:             $tries ++;
 2576:         }
 2577:         if (!&error($response) && $response ne 'refused') {
 2578:             if ($response eq 'unavailable') {
 2579:                 $outcome = $response;
 2580:             } else {
 2581:                 $outcome = 'ok';
 2582:                 my @matches = split(/\n/,$response);
 2583:                 foreach my $match (@matches) {
 2584:                     my ($key,$value) = split(/=/,$match);
 2585:                     $results{&unescape($key)} = &thaw_unescape($value);
 2586:                 }
 2587:             }
 2588:         }
 2589:     }
 2590:     my %userinfo;
 2591:     if (ref($results{$uname}) eq 'HASH') {
 2592:         %userinfo = %{$results{$uname}};
 2593:     } 
 2594:     return ($outcome,%userinfo);
 2595: }
 2596: 
 2597: sub get_multiple_instusers {
 2598:     my ($udom,$users,$caller) = @_;
 2599:     my ($outcome,$results);
 2600:     if (ref($users) eq 'HASH') {
 2601:         my $count = keys(%{$users}); 
 2602:         my $requested = &freeze_escape($users);
 2603:         my $homeserver = &domain($udom,'primary');
 2604:         if ($homeserver ne '') {
 2605:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2606:             my $host=&hostname($homeserver);
 2607:             if ($queryid !~/^\Q$host\E\_/) {
 2608:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2609:                          ' for host: '.$homeserver.'in domain '.$udom);
 2610:                 return ($outcome,$results);
 2611:             }
 2612:             my $response = &get_query_reply($queryid);
 2613:             my $maxtries = 5;
 2614:             if ($count > 100) {
 2615:                 $maxtries = 1+int($count/20);
 2616:             }
 2617:             my $tries = 1;
 2618:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2619:                 $response = &get_query_reply($queryid);
 2620:                 $tries ++;
 2621:             }
 2622:             if ($response eq '') {
 2623:                 $results = {};
 2624:                 foreach my $key (keys(%{$users})) {
 2625:                     my ($uname,$id);
 2626:                     if ($caller eq 'id') {
 2627:                         $id = $key;
 2628:                     } else {
 2629:                         $uname = $key;
 2630:                     }
 2631:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2632:                     $outcome = $resp;
 2633:                     if ($resp eq 'ok') {
 2634:                         %{$results} = (%{$results}, %info);
 2635:                     } else {
 2636:                         last;
 2637:                     }
 2638:                 }
 2639:             } elsif(!&error($response) && ($response ne 'refused')) {
 2640:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2641:                     $outcome = $response;
 2642:                 } else {
 2643:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2644:                     if ($outcome eq 'ok') {
 2645:                         $results = &thaw_unescape($userdata); 
 2646:                     }
 2647:                 }
 2648:             }
 2649:         }
 2650:     }
 2651:     return ($outcome,$results);
 2652: }
 2653: 
 2654: sub inst_rulecheck {
 2655:     my ($udom,$uname,$id,$item,$rules) = @_;
 2656:     my %returnhash;
 2657:     if ($udom ne '') {
 2658:         if (ref($rules) eq 'ARRAY') {
 2659:             @{$rules} = map {&escape($_);} (@{$rules});
 2660:             my $rulestr = join(':',@{$rules});
 2661:             my $homeserver=&domain($udom,'primary');
 2662:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2663:                 my $response;
 2664:                 if ($item eq 'username') {                
 2665:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2666:                                               ':'.&escape($uname).':'.$rulestr,
 2667:                                               $homeserver));
 2668:                 } elsif ($item eq 'id') {
 2669:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2670:                                               ':'.&escape($id).':'.$rulestr,
 2671:                                               $homeserver));
 2672:                 } elsif ($item eq 'selfcreate') {
 2673:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2674:                                                &escape($udom).':'.&escape($uname).
 2675:                                               ':'.$rulestr,$homeserver));
 2676:                 } elsif ($item eq 'unamemap') {
 2677:                     $response=&unescape(&reply('instunamemapcheck:'.
 2678:                                                &escape($udom).':'.&escape($uname).
 2679:                                               ':'.$rulestr,$homeserver));
 2680:                 }
 2681:                 if ($response ne 'refused') {
 2682:                     my @pairs=split(/\&/,$response);
 2683:                     foreach my $item (@pairs) {
 2684:                         my ($key,$value)=split(/=/,$item,2);
 2685:                         $key = &unescape($key);
 2686:                         next if ($key =~ /^error: 2 /);
 2687:                         $returnhash{$key}=&thaw_unescape($value);
 2688:                     }
 2689:                 }
 2690:             }
 2691:         }
 2692:     }
 2693:     return %returnhash;
 2694: }
 2695: 
 2696: sub inst_userrules {
 2697:     my ($udom,$check) = @_;
 2698:     my (%ruleshash,@ruleorder);
 2699:     if ($udom ne '') {
 2700:         my $homeserver=&domain($udom,'primary');
 2701:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2702:             my $response;
 2703:             if ($check eq 'id') {
 2704:                 $response=&reply('instidrules:'.&escape($udom),
 2705:                                  $homeserver);
 2706:             } elsif ($check eq 'email') {
 2707:                 $response=&reply('instemailrules:'.&escape($udom),
 2708:                                  $homeserver);
 2709:             } elsif ($check eq 'unamemap') {
 2710:                 $response=&reply('unamemaprules:'.&escape($udom),
 2711:                                  $homeserver); 
 2712:             } else {
 2713:                 $response=&reply('instuserrules:'.&escape($udom),
 2714:                                  $homeserver);
 2715:             }
 2716:             if (($response ne 'refused') && ($response ne 'error') && 
 2717:                 ($response ne 'unknown_cmd') && 
 2718:                 ($response ne 'no_such_host')) {
 2719:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2720:                 my @pairs=split(/\&/,$hashitems);
 2721:                 foreach my $item (@pairs) {
 2722:                     my ($key,$value)=split(/=/,$item,2);
 2723:                     $key = &unescape($key);
 2724:                     next if ($key =~ /^error: 2 /);
 2725:                     $ruleshash{$key}=&thaw_unescape($value);
 2726:                 }
 2727:                 my @esc_order = split(/\&/,$orderitems);
 2728:                 foreach my $item (@esc_order) {
 2729:                     push(@ruleorder,&unescape($item));
 2730:                 }
 2731:             }
 2732:         }
 2733:     }
 2734:     return (\%ruleshash,\@ruleorder);
 2735: }
 2736: 
 2737: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2738: 
 2739: sub get_domain_defaults {
 2740:     my ($domain,$ignore_cache) = @_;
 2741:     return if (($domain eq '') || ($domain eq 'public'));
 2742:     my $cachetime = 60*60*24;
 2743:     unless ($ignore_cache) {
 2744:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2745:         if (defined($cached)) {
 2746:             if (ref($result) eq 'HASH') {
 2747:                 return %{$result};
 2748:             }
 2749:         }
 2750:     }
 2751:     my %domdefaults;
 2752:     my %domconfig =
 2753:          &get_dom('configuration',['defaults','quotas',
 2754:                                   'requestcourses','inststatus',
 2755:                                   'coursedefaults','usersessions',
 2756:                                   'requestauthor','selfenrollment',
 2757:                                   'coursecategories','ssl','autoenroll',
 2758:                                   'trust','helpsettings','wafproxy',
 2759:                                   'ltisec','toolsec','domexttool',
 2760:                                   'exttool'],$domain);
 2761:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2762:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2763:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2764:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2765:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2766:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2767:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2768:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2769:         $domdefaults{'portal_def_email'} = $domconfig{'defaults'}{'portal_def_email'};
 2770:         $domdefaults{'portal_def_web'} = $domconfig{'defaults'}{'portal_def_web'};
 2771:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2772:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2773:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2774:         $domdefaults{'unamemap_rule'} = $domconfig{'defaults'}{'unamemap_rule'};
 2775:     } else {
 2776:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2777:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2778:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2779:     }
 2780:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2781:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2782:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2783:         } else {
 2784:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2785:         }
 2786:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2787:         foreach my $item (@usertools) {
 2788:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2789:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2790:             }
 2791:         }
 2792:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2793:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2794:         }
 2795:     }
 2796:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2797:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2798:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2799:         }
 2800:     }
 2801:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2802:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2803:     }
 2804:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2805:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2806:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2807:         }
 2808:     }
 2809:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2810:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2811:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2812:         $domdefaults{'inline_chem'} = $domconfig{'coursedefaults'}{'inline_chem'};
 2813:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2814:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2815:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2816:         }
 2817:         foreach my $type (@coursetypes) {
 2818:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2819:                 unless ($type eq 'community') {
 2820:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2821:                 }
 2822:             }
 2823:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2824:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2825:             }
 2826:             if ($domdefaults{'postsubmit'} eq 'on') {
 2827:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2828:                     $domdefaults{$type.'postsubtimeout'} = 
 2829:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2830:                 }
 2831:             }
 2832:             if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
 2833:                 $domdefaults{$type.'domexttool'} = $domconfig{'coursedefaults'}{'domexttool'}{$type};
 2834:             } else {
 2835:                 $domdefaults{$type.'domexttool'} = 1;
 2836:             }
 2837:             if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
 2838:                 $domdefaults{$type.'exttool'} = $domconfig{'coursedefaults'}{'exttool'}{$type};
 2839:             } else {
 2840:                 $domdefaults{$type.'exttool'} = 0;
 2841:             }
 2842:         }
 2843:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2844:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2845:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2846:                 if (@clonecodes) {
 2847:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2848:                 }
 2849:             }
 2850:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2851:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2852:         }
 2853:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2854:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2855:         }
 2856:         if (exists($domconfig{'coursedefaults'}{'ltiauth'})) {
 2857:             $domdefaults{'crsltiauth'} = $domconfig{'coursedefaults'}{'ltiauth'};
 2858:         }
 2859:     }
 2860:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2861:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2862:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2863:         }
 2864:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2865:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2866:         }
 2867:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2868:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2869:         }
 2870:         if (ref($domconfig{'usersessions'}{'offloadoth'}) eq 'HASH') {
 2871:             $domdefaults{'offloadoth'} = $domconfig{'usersessions'}{'offloadoth'};
 2872:         }
 2873:     }
 2874:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2875:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2876:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2877:                             'approval','limit');
 2878:             foreach my $type (@coursetypes) {
 2879:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2880:                     my @mgrdc = ();
 2881:                     foreach my $item (@settings) {
 2882:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2883:                             push(@mgrdc,$item);
 2884:                         }
 2885:                     }
 2886:                     if (@mgrdc) {
 2887:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2888:                     }
 2889:                 }
 2890:             }
 2891:         }
 2892:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2893:             foreach my $type (@coursetypes) {
 2894:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2895:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2896:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2897:                     }
 2898:                 }
 2899:             }
 2900:         }
 2901:     }
 2902:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2903:         $domdefaults{'catauth'} = 'std';
 2904:         $domdefaults{'catunauth'} = 'std';
 2905:         if ($domconfig{'coursecategories'}{'auth'}) {
 2906:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2907:         }
 2908:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2909:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2910:         }
 2911:     }
 2912:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2913:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2914:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2915:         }
 2916:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2917:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2918:         }
 2919:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2920:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2921:         }
 2922:     }
 2923:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2924:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2925:         foreach my $prefix (@prefixes) {
 2926:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2927:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2928:             }
 2929:         }
 2930:     }
 2931:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2932:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2933:         $domdefaults{'failsafe'} = $domconfig{'autoenroll'}{'failsafe'};
 2934:     }
 2935:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2936:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2937:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2938:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2939:         }
 2940:     }
 2941:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
 2942:         foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
 2943:             if ($domconfig{'wafproxy'}{$item}) {
 2944:                 $domdefaults{'waf_'.$item} = $domconfig{'wafproxy'}{$item};
 2945:             }
 2946:         }
 2947:     }
 2948:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
 2949:         if (ref($domconfig{'ltisec'}{'encrypt'}) eq 'HASH') {
 2950:             $domdefaults{'linkprotenc_crs'} = $domconfig{'ltisec'}{'encrypt'}{'crs'};
 2951:             $domdefaults{'linkprotenc_dom'} = $domconfig{'ltisec'}{'encrypt'}{'dom'};
 2952:             $domdefaults{'ltienc_consumers'} = $domconfig{'ltisec'}{'encrypt'}{'consumers'};
 2953:         }
 2954:         if (ref($domconfig{'ltisec'}{'private'}) eq 'HASH') {
 2955:             if (ref($domconfig{'ltisec'}{'private'}{'keys'}) eq 'ARRAY') {
 2956:                 $domdefaults{'ltiprivhosts'} = $domconfig{'ltisec'}{'private'}{'keys'};
 2957:             }
 2958:         }
 2959:     }
 2960:     if (ref($domconfig{'toolsec'}) eq 'HASH') {
 2961:         if (ref($domconfig{'toolsec'}{'encrypt'}) eq 'HASH') {
 2962:             $domdefaults{'toolenc_crs'} = $domconfig{'toolsec'}{'encrypt'}{'crs'};
 2963:             $domdefaults{'toolenc_dom'} = $domconfig{'toolsec'}{'encrypt'}{'dom'};
 2964:         }
 2965:         if (ref($domconfig{'toolsec'}{'private'}) eq 'HASH') {
 2966:             if (ref($domconfig{'toolsec'}{'private'}{'keys'}) eq 'ARRAY') {
 2967:                 $domdefaults{'toolprivhosts'} = $domconfig{'toolsec'}{'private'}{'keys'};
 2968:             }
 2969:         }
 2970:     }
 2971:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2972:     return %domdefaults;
 2973: }
 2974: 
 2975: sub get_dom_cats {
 2976:     my ($dom) = @_;
 2977:     return unless (&domain($dom));
 2978:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2979:     unless (defined($cached)) {
 2980:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2981:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2982:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2983:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2984:             } else {
 2985:                 $cats = {};
 2986:             }
 2987:         } else {
 2988:             $cats = {};
 2989:         }
 2990:         &do_cache_new('cats',$dom,$cats,3600);
 2991:     }
 2992:     return $cats;
 2993: }
 2994: 
 2995: sub get_dom_instcats {
 2996:     my ($dom) = @_;
 2997:     return unless (&domain($dom));
 2998:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2999:     unless (defined($cached)) {
 3000:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 3001:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 3002:         if ($totcodes > 0) {
 3003:             my $caller = 'global';
 3004:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 3005:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 3006:                 $instcats = {
 3007:                                 totcodes => $totcodes,
 3008:                                 codes => \%codes,
 3009:                                 codetitles => \@codetitles,
 3010:                                 cat_titles => \%cat_titles,
 3011:                                 cat_order => \%cat_order,
 3012:                             };
 3013:                 &do_cache_new('instcats',$dom,$instcats,3600);
 3014:             }
 3015:         }
 3016:     }
 3017:     return $instcats;
 3018: }
 3019: 
 3020: sub retrieve_instcodes {
 3021:     my ($coursecodes,$dom) = @_;
 3022:     my $totcodes;
 3023:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 3024:     foreach my $course (keys(%courses)) {
 3025:         if (ref($courses{$course}) eq 'HASH') {
 3026:             if ($courses{$course}{'inst_code'} ne '') {
 3027:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 3028:                 $totcodes ++;
 3029:             }
 3030:         }
 3031:     }
 3032:     return $totcodes;
 3033: }
 3034: 
 3035: sub course_portal_url {
 3036:     my ($cnum,$cdom,$r) = @_;
 3037:     my $chome = &homeserver($cnum,$cdom);
 3038:     my $hostname = &hostname($chome);
 3039:     my $protocol = $protocol{$chome};
 3040:     $protocol = 'http' if ($protocol ne 'https');
 3041:     my %domdefaults = &get_domain_defaults($cdom);
 3042:     my $firsturl;
 3043:     if ($domdefaults{'portal_def'}) {
 3044:         $firsturl = $domdefaults{'portal_def'};
 3045:     } else {
 3046:         my $alias = &use_proxy_alias($r,$chome);
 3047:         $hostname = $alias if ($alias ne '');
 3048:         $firsturl = $protocol.'://'.$hostname;
 3049:     }
 3050:     return $firsturl;
 3051: }
 3052: 
 3053: sub url_prefix {
 3054:     my ($r,$dom,$home,$context) = @_;
 3055:     my $prefix;
 3056:     my %domdefs = &get_domain_defaults($dom);
 3057:     if ($domdefs{'portal_def'} && $domdefs{'portal_def_'.$context}) {
 3058:         if ($domdefs{'portal_def'} =~ m{^(https?://[^/]+)}) {
 3059:             $prefix = $1;
 3060:         }
 3061:     }
 3062:     if ($prefix eq '') {
 3063:         my $hostname = &hostname($home);
 3064:         my $protocol = $protocol{$home};
 3065:         $protocol = 'http' if ($protocol{$home} ne 'https');
 3066:         my $alias = &use_proxy_alias($r,$home);
 3067:         $hostname = $alias if ($alias ne '');
 3068:         $prefix = $protocol.'://'.$hostname;
 3069:     }
 3070:     return $prefix;
 3071: }
 3072: 
 3073: # --------------------------------------------- Get domain config for passwords
 3074: 
 3075: sub get_passwdconf {
 3076:     my ($dom) = @_;
 3077:     my (%passwdconf,$gotconf,$lookup);
 3078:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 3079:     if (defined($cached)) {
 3080:         if (ref($result) eq 'HASH') {
 3081:             %passwdconf = %{$result};
 3082:             $gotconf = 1;
 3083:         }
 3084:     }
 3085:     unless ($gotconf) {
 3086:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 3087:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 3088:             %passwdconf = %{$domconfig{'passwords'}};
 3089:         }
 3090:         my $cachetime = 24*60*60;
 3091:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 3092:     }
 3093:     return %passwdconf;
 3094: }
 3095: 
 3096: # --------------------------------------------------- Assign a key to a student
 3097: 
 3098: sub assign_access_key {
 3099: #
 3100: # a valid key looks like uname:udom#comments
 3101: # comments are being appended
 3102: #
 3103:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 3104:     $kdom=
 3105:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 3106:     $knum=
 3107:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 3108:     $cdom=
 3109:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3110:     $cnum=
 3111:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3112:     $udom=$env{'user.name'} unless (defined($udom));
 3113:     $uname=$env{'user.domain'} unless (defined($uname));
 3114:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 3115:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 3116:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 3117:                                                   # assigned to this person
 3118:                                                   # - this should not happen,
 3119:                                                   # unless something went wrong
 3120:                                                   # the first time around
 3121: # ready to assign
 3122:         $logentry=$1.'; '.$logentry;
 3123:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 3124:                                                  $kdom,$knum) eq 'ok') {
 3125: # key now belongs to user
 3126: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 3127:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 3128:                 &appenv({'environment.'.$envkey => $ckey});
 3129:                 return 'ok';
 3130:             } else {
 3131:                 return 
 3132:   'error: Count not permanently assign key, will need to be re-entered later.';
 3133: 	    }
 3134:         } else {
 3135:             return 'error: Could not assign key, try again later.';
 3136:         }
 3137:     } elsif (!$existing{$ckey}) {
 3138: # the key does not exist
 3139: 	return 'error: The key does not exist';
 3140:     } else {
 3141: # the key is somebody else's
 3142: 	return 'error: The key is already in use';
 3143:     }
 3144: }
 3145: 
 3146: # ------------------------------------------ put an additional comment on a key
 3147: 
 3148: sub comment_access_key {
 3149: #
 3150: # a valid key looks like uname:udom#comments
 3151: # comments are being appended
 3152: #
 3153:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 3154:     $cdom=
 3155:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3156:     $cnum=
 3157:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3158:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3159:     if ($existing{$ckey}) {
 3160:         $existing{$ckey}.='; '.$logentry;
 3161: # ready to assign
 3162:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 3163:                                                  $cdom,$cnum) eq 'ok') {
 3164: 	    return 'ok';
 3165:         } else {
 3166: 	    return 'error: Count not store comment.';
 3167:         }
 3168:     } else {
 3169: # the key does not exist
 3170: 	return 'error: The key does not exist';
 3171:     }
 3172: }
 3173: 
 3174: # ------------------------------------------------------ Generate a set of keys
 3175: 
 3176: sub generate_access_keys {
 3177:     my ($number,$cdom,$cnum,$logentry)=@_;
 3178:     $cdom=
 3179:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3180:     $cnum=
 3181:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3182:     unless (&allowed('mky',$cdom)) { return 0; }
 3183:     unless (($cdom) && ($cnum)) { return 0; }
 3184:     if ($number>10000) { return 0; }
 3185:     sleep(2); # make sure don't get same seed twice
 3186:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 3187:     my $total=0;
 3188:     for (my $i=1;$i<=$number;$i++) {
 3189:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 3190:                   sprintf("%lx",int(100000*rand)).'-'.
 3191:                   sprintf("%lx",int(100000*rand));
 3192:        $newkey=~s/1/g/g; # folks mix up 1 and l
 3193:        $newkey=~s/0/h/g; # and also 0 and O
 3194:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 3195:        if ($existing{$newkey}) {
 3196:            $i--;
 3197:        } else {
 3198: 	  if (&put('accesskeys',
 3199:               { $newkey => '# generated '.localtime().
 3200:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 3201:                            '; '.$logentry },
 3202: 		   $cdom,$cnum) eq 'ok') {
 3203:               $total++;
 3204: 	  }
 3205:        }
 3206:     }
 3207:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 3208:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 3209:     return $total;
 3210: }
 3211: 
 3212: # ------------------------------------------------------- Validate an accesskey
 3213: 
 3214: sub validate_access_key {
 3215:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 3216:     $cdom=
 3217:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 3218:     $cnum=
 3219:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 3220:     $udom=$env{'user.domain'} unless (defined($udom));
 3221:     $uname=$env{'user.name'} unless (defined($uname));
 3222:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 3223:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 3224: }
 3225: 
 3226: # ------------------------------------- Find the section of student in a course
 3227: sub devalidate_getsection_cache {
 3228:     my ($udom,$unam,$courseid)=@_;
 3229:     my $hashid="$udom:$unam:$courseid";
 3230:     &devalidate_cache_new('getsection',$hashid);
 3231: }
 3232: 
 3233: sub courseid_to_courseurl {
 3234:     my ($courseid) = @_;
 3235:     #already url style courseid
 3236:     return $courseid if ($courseid =~ m{^/});
 3237: 
 3238:     if (exists($env{'course.'.$courseid.'.num'})) {
 3239: 	my $cnum = $env{'course.'.$courseid.'.num'};
 3240: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 3241: 	return "/$cdom/$cnum";
 3242:     }
 3243: 
 3244:     my %courseinfo=&coursedescription($courseid);
 3245:     if (exists($courseinfo{'num'})) {
 3246: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 3247:     }
 3248: 
 3249:     return undef;
 3250: }
 3251: 
 3252: sub getsection {
 3253:     my ($udom,$unam,$courseid)=@_;
 3254:     my $cachetime=1800;
 3255: 
 3256:     my $hashid="$udom:$unam:$courseid";
 3257:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3258:     if (defined($cached)) { return $result; }
 3259: 
 3260:     my %Pending; 
 3261:     my %Expired;
 3262:     #
 3263:     # Each role can either have not started yet (pending), be active, 
 3264:     #    or have expired.
 3265:     #
 3266:     # If there is an active role, we are done.
 3267:     #
 3268:     # If there is more than one role which has not started yet, 
 3269:     #     choose the one which will start sooner
 3270:     # If there is one role which has not started yet, return it.
 3271:     #
 3272:     # If there is more than one expired role, choose the one which ended last.
 3273:     # If there is a role which has expired, return it.
 3274:     #
 3275:     $courseid = &courseid_to_courseurl($courseid);
 3276:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3277:     foreach my $key (keys(%roleshash)) {
 3278:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3279:         my $section=$1;
 3280:         if ($key eq $courseid.'_st') { $section=''; }
 3281:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3282:         my $now=time;
 3283:         if (defined($end) && $end && ($now > $end)) {
 3284:             $Expired{$end}=$section;
 3285:             next;
 3286:         }
 3287:         if (defined($start) && $start && ($now < $start)) {
 3288:             $Pending{$start}=$section;
 3289:             next;
 3290:         }
 3291:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3292:     }
 3293:     #
 3294:     # Presumedly there will be few matching roles from the above
 3295:     # loop and the sorting time will be negligible.
 3296:     if (scalar(keys(%Pending))) {
 3297:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3298:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3299:     } 
 3300:     if (scalar(keys(%Expired))) {
 3301:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3302:         my $time = pop(@sorted);
 3303:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3304:     }
 3305:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3306: }
 3307: 
 3308: sub save_cache {
 3309:     &purge_remembered();
 3310:     #&Apache::loncommon::validate_page();
 3311:     undef(%env);
 3312:     undef($env_loaded);
 3313: }
 3314: 
 3315: my $to_remember=-1;
 3316: my %remembered;
 3317: my %accessed;
 3318: my $kicks=0;
 3319: my $hits=0;
 3320: sub make_key {
 3321:     my ($name,$id) = @_;
 3322:     if (length($id) > 65 
 3323: 	&& length(&escape($id)) > 200) {
 3324: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3325:     }
 3326:     return &escape($name.':'.$id);
 3327: }
 3328: 
 3329: sub devalidate_cache_new {
 3330:     my ($name,$id,$debug) = @_;
 3331:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3332:     my $remembered_id=$name.':'.$id;
 3333:     $id=&make_key($name,$id);
 3334:     $memcache->delete($id);
 3335:     delete($remembered{$remembered_id});
 3336:     delete($accessed{$remembered_id});
 3337: }
 3338: 
 3339: sub is_cached_new {
 3340:     my ($name,$id,$debug) = @_;
 3341:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3342:     if (exists($remembered{$remembered_id})) {
 3343: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3344: 	$accessed{$remembered_id}=[&gettimeofday()];
 3345: 	$hits++;
 3346: 	return ($remembered{$remembered_id},1);
 3347:     }
 3348:     $id=&make_key($name,$id);
 3349:     my $value = $memcache->get($id);
 3350:     if (!(defined($value))) {
 3351: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3352: 	return (undef,undef);
 3353:     }
 3354:     if ($value eq '__undef__') {
 3355: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3356: 	$value=undef;
 3357:     }
 3358:     &make_room($remembered_id,$value,$debug);
 3359:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3360:     return ($value,1);
 3361: }
 3362: 
 3363: sub do_cache_new {
 3364:     my ($name,$id,$value,$time,$debug) = @_;
 3365:     my $remembered_id=$name.':'.$id;
 3366:     $id=&make_key($name,$id);
 3367:     my $setvalue=$value;
 3368:     if (!defined($setvalue)) {
 3369: 	$setvalue='__undef__';
 3370:     }
 3371:     if (!defined($time) ) {
 3372: 	$time=600;
 3373:     }
 3374:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3375:     my $result = $memcache->set($id,$setvalue,$time);
 3376:     if (! $result) {
 3377: 	&logthis("caching of id -> $id  failed");
 3378: 	$memcache->disconnect_all();
 3379:     }
 3380:     # need to make a copy of $value
 3381:     &make_room($remembered_id,$value,$debug);
 3382:     return $value;
 3383: }
 3384: 
 3385: sub make_room {
 3386:     my ($remembered_id,$value,$debug)=@_;
 3387: 
 3388:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3389:                                     : $value;
 3390:     if ($to_remember<0) { return; }
 3391:     $accessed{$remembered_id}=[&gettimeofday()];
 3392:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3393:     my $to_kick;
 3394:     my $max_time=0;
 3395:     foreach my $other (keys(%accessed)) {
 3396: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3397: 	    $to_kick=$other;
 3398: 	    $max_time=&tv_interval($accessed{$other});
 3399: 	}
 3400:     }
 3401:     delete($remembered{$to_kick});
 3402:     delete($accessed{$to_kick});
 3403:     $kicks++;
 3404:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3405:     return;
 3406: }
 3407: 
 3408: sub purge_remembered {
 3409:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3410:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3411:     undef(%remembered);
 3412:     undef(%accessed);
 3413: }
 3414: # ------------------------------------- Read an entry from a user's environment
 3415: 
 3416: sub userenvironment {
 3417:     my ($udom,$unam,@what)=@_;
 3418:     my $items;
 3419:     foreach my $item (@what) {
 3420:         $items.=&escape($item).'&';
 3421:     }
 3422:     $items=~s/\&$//;
 3423:     my %returnhash=();
 3424:     my $uhome = &homeserver($unam,$udom);
 3425:     unless ($uhome eq 'no_host') {
 3426:         my @answer=split(/\&/, 
 3427:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3428:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3429:             return %returnhash;
 3430:         }
 3431:         my $i;
 3432:         for ($i=0;$i<=$#what;$i++) {
 3433: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3434:         }
 3435:     }
 3436:     return %returnhash;
 3437: }
 3438: 
 3439: # ---------------------------------------------------------- Get a studentphoto
 3440: sub studentphoto {
 3441:     my ($udom,$unam,$ext) = @_;
 3442:     my $home=&homeserver($unam,$udom);
 3443:     if (defined($env{'request.course.id'})) {
 3444:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3445:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3446:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3447:             } else {
 3448:                 my ($result,$perm_reqd)=
 3449: 		    &auto_photo_permission($unam,$udom);
 3450:                 if ($result eq 'ok') {
 3451:                     if (!($perm_reqd eq 'yes')) {
 3452:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3453:                     }
 3454:                 }
 3455:             }
 3456:         }
 3457:     } else {
 3458:         my ($result,$perm_reqd) = 
 3459: 	    &auto_photo_permission($unam,$udom);
 3460:         if ($result eq 'ok') {
 3461:             if (!($perm_reqd eq 'yes')) {
 3462:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3463:             }
 3464:         }
 3465:     }
 3466:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3467: }
 3468: 
 3469: sub retrievestudentphoto {
 3470:     my ($udom,$unam,$ext,$type) = @_;
 3471:     my $home=&homeserver($unam,$udom);
 3472:     my $ret=&reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3473:     if ($ret eq 'ok') {
 3474:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3475:         if ($type eq 'thumbnail') {
 3476:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3477:         }
 3478:         my $tokenurl=&tokenwrapper($url);
 3479:         return $tokenurl;
 3480:     } else {
 3481:         if ($type eq 'thumbnail') {
 3482:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3483:         } else { 
 3484:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3485:         }
 3486:     }
 3487: }
 3488: 
 3489: # -------------------------------------------------------------------- New chat
 3490: 
 3491: sub chatsend {
 3492:     my ($newentry,$anon,$group)=@_;
 3493:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3494:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3495:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3496:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3497: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3498: 		   &escape($newentry)).':'.$group,$chome);
 3499: }
 3500: 
 3501: # ------------------------------------------ Find current version of a resource
 3502: 
 3503: sub getversion {
 3504:     my $fname=&clutter(shift);
 3505:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3506:     return &currentversion(&filelocation('',$fname));
 3507: }
 3508: 
 3509: sub currentversion {
 3510:     my $fname=shift;
 3511:     my $author=$fname;
 3512:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3513:     my ($udom,$uname)=split(/\//,$author);
 3514:     my $home=&homeserver($uname,$udom);
 3515:     if ($home eq 'no_host') { 
 3516:         return -1; 
 3517:     }
 3518:     my $answer=&reply("currentversion:$fname",$home);
 3519:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3520: 	return -1;
 3521:     }
 3522:     return $answer;
 3523: }
 3524: 
 3525: #
 3526: # Return special version number of resource if set by override, empty otherwise
 3527: #
 3528: sub usedversion {
 3529:     my $fname=shift;
 3530:     unless ($fname) { $fname=$env{'request.uri'}; }
 3531:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3532:     if ($urlversion) { return $urlversion; }
 3533:     return '';
 3534: }
 3535: 
 3536: # ----------------------------- Subscribe to a resource, return URL if possible
 3537: 
 3538: sub subscribe {
 3539:     my $fname=shift;
 3540:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3541:     $fname=~s/[\n\r]//g;
 3542:     my $author=$fname;
 3543:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3544:     my ($udom,$uname)=split(/\//,$author);
 3545:     my $home=homeserver($uname,$udom);
 3546:     if ($home eq 'no_host') {
 3547:         return 'not_found';
 3548:     }
 3549:     my $answer=reply("sub:$fname",$home);
 3550:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3551: 	$answer.=' by '.$home;
 3552:     }
 3553:     return $answer;
 3554: }
 3555:     
 3556: # -------------------------------------------------------------- Replicate file
 3557: 
 3558: sub repcopy {
 3559:     my $filename=shift;
 3560:     $filename=~s/\/+/\//g;
 3561:     my $londocroot = $perlvar{'lonDocRoot'};
 3562:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3563:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3564:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3565: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3566: 	return &repcopy_userfile($filename);
 3567:     }
 3568:     $filename=~s/[\n\r]//g;
 3569:     my $transname="$filename.in.transfer";
 3570: # FIXME: this should flock
 3571:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3572:     my $remoteurl=subscribe($filename);
 3573:     if ($remoteurl =~ /^con_lost by/) {
 3574: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3575:            return 'unavailable';
 3576:     } elsif ($remoteurl eq 'not_found') {
 3577: 	   #&logthis("Subscribe returned not_found: $filename");
 3578: 	   return 'not_found';
 3579:     } elsif ($remoteurl =~ /^rejected by/) {
 3580: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3581:            return 'forbidden';
 3582:     } elsif ($remoteurl eq 'directory') {
 3583:            return 'ok';
 3584:     } else {
 3585:         my $author=$filename;
 3586:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3587:         my ($udom,$uname)=split(/\//,$author);
 3588:         my $home=homeserver($uname,$udom);
 3589:         unless ($home eq $perlvar{'lonHostID'}) {
 3590:            my @parts=split(/\//,$filename);
 3591:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3592:            if ($path ne "$londocroot/res") {
 3593:                &logthis("Malconfiguration for replication: $filename");
 3594: 	       return 'bad_request';
 3595:            }
 3596:            my $count;
 3597:            for ($count=5;$count<$#parts;$count++) {
 3598:                $path.="/$parts[$count]";
 3599:                if ((-e $path)!=1) {
 3600: 		   mkdir($path,0777);
 3601:                }
 3602:            }
 3603:            my $request=new HTTP::Request('GET',"$remoteurl");
 3604:            my $response;
 3605:            if ($remoteurl =~ m{/raw/}) {
 3606:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3607:            } else {
 3608:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3609:            }
 3610:            if ($response->is_error()) {
 3611: 	       unlink($transname);
 3612:                my $message=$response->status_line;
 3613:                &logthis("<font color=\"blue\">WARNING:"
 3614:                        ." LWP get: $message: $filename</font>");
 3615:                return 'unavailable';
 3616:            } else {
 3617: 	       if ($remoteurl!~/\.meta$/) {
 3618:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3619:                   my $mresponse;
 3620:                   if ($remoteurl =~ m{/raw/}) {
 3621:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3622:                   } else {
 3623:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3624:                   }
 3625:                   if ($mresponse->is_error()) {
 3626: 		      unlink($filename.'.meta');
 3627:                       &logthis(
 3628:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3629:                   }
 3630: 	       }
 3631:                rename($transname,$filename);
 3632:                return 'ok';
 3633:            }
 3634:        }
 3635:     }
 3636: }
 3637: 
 3638: # ------------------------------------------------- Unsubscribe from a resource
 3639: 
 3640: sub unsubscribe {
 3641:     my ($fname) = @_;
 3642:     my $answer;
 3643:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3644:     $fname=~s/[\n\r]//g;
 3645:     my $author=$fname;
 3646:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3647:     my ($udom,$uname)=split(/\//,$author);
 3648:     my $home=homeserver($uname,$udom);
 3649:     if ($home eq 'no_host') {
 3650:         $answer = 'no_host';
 3651:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3652:         $answer = 'home';
 3653:     } else {
 3654:         my $defdom = $perlvar{'lonDefDomain'};
 3655:         if (&will_trust('content',$defdom,$udom)) {
 3656:             $answer = reply("unsub:$fname",$home);
 3657:         } else {
 3658:             $answer = 'untrusted';
 3659:         }
 3660:     }
 3661:     return $answer;
 3662: }
 3663: 
 3664: # ------------------------------------------------ Get server side include body
 3665: sub ssi_body {
 3666:     my ($filelink,%form)=@_;
 3667:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3668:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3669:     }
 3670:     my $output='';
 3671:     my $response;
 3672:     if ($filelink=~/^https?\:/) {
 3673:        ($output,$response)=&externalssi($filelink);
 3674:     } else {
 3675:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3676:        $filelink .= 'inhibitmenu=yes';
 3677:        ($output,$response)=&ssi($filelink,%form);
 3678:     }
 3679:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3680:     $output=~s/^.*?\<body[^\>]*\>//si;
 3681:     $output=~s/\<\/body\s*\>.*?$//si;
 3682:     if (wantarray) {
 3683:         return ($output, $response);
 3684:     } else {
 3685:         return $output;
 3686:     }
 3687: }
 3688: 
 3689: # --------------------------------------------------------- Server Side Include
 3690: 
 3691: sub absolute_url {
 3692:     my ($host_name,$unalias,$keep_proto) = @_;
 3693:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3694:     if ($host_name eq '') {
 3695: 	$host_name = $ENV{'SERVER_NAME'};
 3696:     }
 3697:     if ($unalias) {
 3698:         my $alias = &get_proxy_alias();
 3699:         if ($alias eq $host_name) {
 3700:             my $lonhost = $perlvar{'lonHostID'};
 3701:             my $hostname = &hostname($lonhost);
 3702:             my $lcproto; 
 3703:             if (($keep_proto) || ($hostname eq '')) {
 3704:                 $lcproto = $protocol;
 3705:             } else {
 3706:                 $lcproto = $protocol{$lonhost};
 3707:                 $lcproto = 'http' if ($lcproto ne 'https');
 3708:                 $lcproto .= '://';
 3709:             }
 3710:             unless ($hostname eq '') {
 3711:                 return $lcproto.$hostname;
 3712:             }
 3713:         }
 3714:     }
 3715:     return $protocol.$host_name;
 3716: }
 3717: 
 3718: #
 3719: #   Server side include.
 3720: # Parameters:
 3721: #  fn     Possibly encrypted resource name/id.
 3722: #  form   Hash that describes how the rendering should be done
 3723: #         and other things.
 3724: # Returns:
 3725: #   Scalar context: The content of the response.
 3726: #   Array context:  2 element list of the content and the full response object.
 3727: #     
 3728: sub ssi {
 3729: 
 3730:     my ($fn,%form)=@_;
 3731:     my ($host,$request,$response);
 3732:     $host = &absolute_url('',1);
 3733: 
 3734:     $form{'no_update_last_known'}=1;
 3735:     &Apache::lonenc::check_encrypt(\$fn);
 3736:     if (%form) {
 3737:       $request=new HTTP::Request('POST',$host.$fn);
 3738:       $request->content(join('&',map { 
 3739:             my $name = escape($_);
 3740:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3741:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3742:             : &escape($form{$_}) );    
 3743:         } keys(%form)));
 3744:     } else {
 3745:       $request=new HTTP::Request('GET',$host.$fn);
 3746:     }
 3747: 
 3748:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3749:     my $lonhost = $perlvar{'lonHostID'};
 3750:     my $islocal;
 3751:     if (($env{'request.course.id'}) &&
 3752:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3753:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3754:         ($form{'grade_symb'} ne '') &&
 3755:         (&allowed('mgr',$env{'request.course.id'}.
 3756:                         ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3757:         $islocal = 1;
 3758:     }
 3759:     $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3760:                                              '','','',$islocal);
 3761: 
 3762:     if (wantarray) {
 3763: 	return ($response->content, $response);
 3764:     } else {
 3765: 	return $response->content;
 3766:     }
 3767: }
 3768: 
 3769: sub externalssi {
 3770:     my ($url)=@_;
 3771:     my $request=new HTTP::Request('GET',$url);
 3772:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3773:     if (wantarray) {
 3774:         return ($response->content, $response);
 3775:     } else {
 3776:         return $response->content;
 3777:     }
 3778: }
 3779: 
 3780: 
 3781: # If the local copy of a replicated resource is outdated, trigger a  
 3782: # connection from the homeserver to flush the delayed queue. If no update 
 3783: # happens, remove local copies of outdated resource (and corresponding
 3784: # metadata file).
 3785: 
 3786: sub remove_stale_resfile {
 3787:     my ($url) = @_;
 3788:     my $removed;
 3789:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3790:         my $audom = $1;
 3791:         my $auname = $2;
 3792:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3793:             my $homeserver = &homeserver($auname,$audom);
 3794:             unless (($homeserver eq 'no_host') ||
 3795:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3796:                 my $fname = &filelocation('',$url);
 3797:                 if (-e $fname) {
 3798:                     my $hostname = &hostname($homeserver);
 3799:                     if ($hostname) {
 3800:                         my $protocol = $protocol{$homeserver};
 3801:                         $protocol = 'http' if ($protocol ne 'https');
 3802:                         my $uri = &declutter($url);
 3803:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3804:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3805:                         if ($response->is_success()) {
 3806:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3807:                             my $locmodtime = (stat($fname))[9];
 3808:                             if ($locmodtime < $remmodtime) {
 3809:                                 my $stale;
 3810:                                 my $answer = &reply('pong',$homeserver);
 3811:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3812:                                     sleep(0.2);
 3813:                                     $locmodtime = (stat($fname))[9];
 3814:                                     if ($locmodtime < $remmodtime) {
 3815:                                         my $posstransfer = $fname.'.in.transfer';
 3816:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3817:                                             $removed = 1;
 3818:                                         } else {
 3819:                                             $stale = 1;
 3820:                                         }
 3821:                                     } else {
 3822:                                         $removed = 1;
 3823:                                     }
 3824:                                 } else {
 3825:                                     $stale = 1;
 3826:                                 }
 3827:                                 if ($stale) {
 3828:                                     if (unlink($fname)) {
 3829:                                         if ($uri!~/\.meta$/) {
 3830:                                             if (-e $fname.'.meta') {
 3831:                                                 unlink($fname.'.meta');
 3832:                                             }
 3833:                                         }
 3834:                                         my $unsubresult = &unsubscribe($fname);
 3835:                                         unless ($unsubresult eq 'ok') {
 3836:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3837:                                         }
 3838:                                         $removed = 1;
 3839:                                     }
 3840:                                 }
 3841:                             }
 3842:                         }
 3843:                     }
 3844:                 }
 3845:             }
 3846:         }
 3847:     }
 3848:     return $removed;
 3849: }
 3850: 
 3851: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3852: 
 3853: sub allowuploaded {
 3854:     my ($srcurl,$url)=@_;
 3855:     $url=&clutter(&declutter($url));
 3856:     my $dir=$url;
 3857:     $dir=~s/\/[^\/]+$//;
 3858:     my %httpref=();
 3859:     my $httpurl=&hreflocation('',$url);
 3860:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3861:     &Apache::lonnet::appenv(\%httpref);
 3862: }
 3863: 
 3864: #
 3865: # Determine if the current user should be able to edit a particular resource,
 3866: # when viewing in course context.
 3867: # (a) When viewing resource used to determine if "Edit" item is included in 
 3868: #     Functions.
 3869: # (b) When displaying folder contents in course editor, used to determine if
 3870: #     "Edit" link will be displayed alongside resource.
 3871: #
 3872: #  input: six args -- filename (decluttered), course number, course domain,
 3873: #                   url, symb (if registered) and group (if this is a group
 3874: #                   item -- e.g., bulletin board, group page etc.).
 3875: #  output: array of five scalars -- 
 3876: #          $cfile -- url for file editing if editable on current server
 3877: #          $home -- homeserver of resource (i.e., for author if published,
 3878: #                                           or course if uploaded.).
 3879: #          $switchserver --  1 if server switch will be needed.
 3880: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3881: #          $forceview -- 1 if icon/link should be to go to view mode
 3882: #
 3883: 
 3884: sub can_edit_resource {
 3885:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3886:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3887: #
 3888: # For aboutme pages user can only edit his/her own.
 3889: #
 3890:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3891:         my ($sdom,$sname) = ($1,$2);
 3892:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3893:             $home = $env{'user.home'};
 3894:             $cfile = $resurl;
 3895:             if ($env{'form.forceedit'}) {
 3896:                 $forceview = 1;
 3897:             } else {
 3898:                 $forceedit = 1;
 3899:             }
 3900:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3901:         } else {
 3902:             return;
 3903:         }
 3904:     }
 3905: 
 3906:     if ($env{'request.course.id'}) {
 3907:         my $crsedit = &allowed('mdc',$env{'request.course.id'});
 3908:         if ($group ne '') {
 3909: # if this is a group homepage or group bulletin board, check group privs
 3910:             my $allowed = 0;
 3911:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3912:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3913:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3914:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3915:                     $allowed = 1;
 3916:                 }
 3917:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3918:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3919:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3920:                     $allowed = 1;
 3921:                 }
 3922:             }
 3923:             if ($allowed) {
 3924:                 $home=&homeserver($cnum,$cdom);
 3925:                 if ($env{'form.forceedit'}) {
 3926:                     $forceview = 1;
 3927:                 } else {
 3928:                     $forceedit = 1;
 3929:                 }
 3930:                 $cfile = $resurl;
 3931:             } else {
 3932:                 return;
 3933:             }
 3934:         } else {
 3935:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3936:                 unless (&allowed('opa',$env{'request.course.id'})) {
 3937:                     return;
 3938:                 }
 3939:             } elsif (!$crsedit) {
 3940:                 if ($env{'request.role'} =~ m{^st\./$cdom/$cnum}) {
 3941: #
 3942: # No edit allowed where CC has switched to student role.
 3943: #
 3944:                     return;
 3945:                 } elsif (($resurl !~ m{^/res/$match_domain/$match_username/}) ||
 3946:                          ($resurl =~ m{^/res/lib/templates/})) {
 3947:                     return;
 3948:                 }
 3949:             }
 3950:         }
 3951:     }
 3952: 
 3953:     if ($file ne '') {
 3954:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3955:             if (&is_course_upload($file,$cnum,$cdom)) {
 3956:                 $uploaded = 1;
 3957:                 $incourse = 1;
 3958:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3959:                     $cfile = &hreflocation('',$file);
 3960:                     if ($env{'form.forceedit'}) {
 3961:                         $forceview = 1;
 3962:                     } else {
 3963:                         $forceedit = 1;
 3964:                     }
 3965:                 }
 3966:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3967:                 $incourse = 1;
 3968:                 if ($env{'form.forceedit'}) {
 3969:                     $forceview = 1;
 3970:                 } else {
 3971:                     $forceedit = 1;
 3972:                 }
 3973:                 $cfile = $resurl;
 3974:             } elsif (($resurl ne '') && (&is_on_map($resurl))) {
 3975:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3976:                     $incourse = 1;
 3977:                     if ($env{'form.forceedit'}) {
 3978:                         $forceview = 1;
 3979:                     } else {
 3980:                         $forceedit = 1;
 3981:                     }
 3982:                     $cfile = $resurl;
 3983:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3984:                     $incourse = 1;
 3985:                     $cfile = $resurl.'/smpedit';
 3986:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3987:                     $incourse = 1;
 3988:                     if ($env{'form.forceedit'}) {
 3989:                         $forceview = 1;
 3990:                     } else {
 3991:                         $forceedit = 1;
 3992:                     }
 3993:                     $cfile = $resurl;
 3994:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3995:                     my ($map,$id,$res) = &decode_symb($symb);
 3996:                     if ($map =~ /\.page$/) {
 3997:                         $incourse = 1;
 3998:                         if ($env{'form.forceedit'}) {
 3999:                             $forceview = 1;
 4000:                             $cfile = $map;
 4001:                         } else {
 4002:                             $forceedit = 1;
 4003:                             $cfile =  '/adm/wrapper'.$resurl;
 4004:                         }
 4005:                     }
 4006:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4007:                     $incourse = 1;
 4008:                     if ($env{'form.forceedit'}) {
 4009:                         $forceview = 1;
 4010:                     } else {
 4011:                         $forceedit = 1;
 4012:                     }
 4013:                     $cfile = $resurl;
 4014:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4015:                     $incourse = 1;
 4016:                     if ($env{'form.forceedit'}) {
 4017:                         $forceview = 1;
 4018:                     } else {
 4019:                         $forceedit = 1;
 4020:                     }
 4021:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4022:                 }
 4023:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 4024:                 my $template = '/res/lib/templates/simpleproblem.problem';
 4025:                 if (&is_on_map($template)) { 
 4026:                     $incourse = 1;
 4027:                     $forceview = 1;
 4028:                     $cfile = $template;
 4029:                 }
 4030:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4031:                 $incourse = 1;
 4032:                 if ($env{'form.forceedit'}) {
 4033:                     $forceview = 1;
 4034:                 } else {
 4035:                     $forceedit = 1;
 4036:                 }
 4037:                 $cfile = $resurl;
 4038:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 4039:                 $incourse = 1;
 4040:                 if ($env{'form.forceedit'}) {
 4041:                     $forceview = 1;
 4042:                 } else {
 4043:                     $forceedit = 1;
 4044:                 }
 4045:                 $cfile = $resurl;
 4046:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 4047:                 $incourse = 1;
 4048:                 $forceview = 1;
 4049:                 if ($symb) {
 4050:                     my ($map,$id,$res)=&decode_symb($symb);
 4051:                     $env{'request.symb'} = $symb;
 4052:                     $cfile = &clutter($res);
 4053:                 } else {
 4054:                     $cfile = $env{'form.suppurl'};
 4055:                     my $escfile = &unescape($cfile);
 4056:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 4057:                         $cfile = '/adm/wrapper'.$escfile;
 4058:                     } else {
 4059:                         $escfile =~ s{^http://}{};
 4060:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 4061:                     }
 4062:                 }
 4063:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 4064:                 if ($env{'form.forceedit'}) {
 4065:                     $forceview = 1;
 4066:                 } else {
 4067:                     $forceedit = 1;
 4068:                 }
 4069:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 4070:             }
 4071:         }
 4072:         if ($uploaded || $incourse) {
 4073:             $home=&homeserver($cnum,$cdom);
 4074:         } elsif ($file !~ m{/$}) {
 4075:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 4076:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 4077:             # Check that the user has permission to edit this resource
 4078:             my $setpriv = 1;
 4079:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 4080:             if (defined($cfudom)) {
 4081:                 $home=&homeserver($cfuname,$cfudom);
 4082:                 $cfile=$file;
 4083:             }
 4084:         }
 4085:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 4086:             (($home ne '') && ($home ne 'no_host'))) {
 4087:             my @ids=&current_machine_ids();
 4088:             unless (grep(/^\Q$home\E$/,@ids)) {
 4089:                 $switchserver=1;
 4090:             }
 4091:         }
 4092:     }
 4093:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 4094: }
 4095: 
 4096: sub is_course_upload {
 4097:     my ($file,$cnum,$cdom) = @_;
 4098:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 4099:     $uploadpath =~ s{^\/}{};
 4100:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 4101:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 4102:         return 1;
 4103:     }
 4104:     return;
 4105: }
 4106: 
 4107: sub in_course {
 4108:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 4109:     if ($hideprivileged) {
 4110:         my $skipuser;
 4111:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 4112:         my @possdoms = ($cdom);  
 4113:         if ($coursehash{'checkforpriv'}) { 
 4114:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 4115:         }
 4116:         if (&privileged($uname,$udom,\@possdoms)) {
 4117:             $skipuser = 1;
 4118:             if ($coursehash{'nothideprivileged'}) {
 4119:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 4120:                     my $user;
 4121:                     if ($item =~ /:/) {
 4122:                         $user = $item;
 4123:                     } else {
 4124:                         $user = join(':',split(/[\@]/,$item));
 4125:                     }
 4126:                     if ($user eq $uname.':'.$udom) {
 4127:                         undef($skipuser);
 4128:                         last;
 4129:                     }
 4130:                 }
 4131:             }
 4132:             if ($skipuser) {
 4133:                 return 0;
 4134:             }
 4135:         }
 4136:     }
 4137:     $type ||= 'any';
 4138:     if (!defined($cdom) || !defined($cnum)) {
 4139:         my $cid  = $env{'request.course.id'};
 4140:         $cdom = $env{'course.'.$cid.'.domain'};
 4141:         $cnum = $env{'course.'.$cid.'.num'};
 4142:     }
 4143:     my $typesref;
 4144:     if (($type eq 'any') || ($type eq 'all')) {
 4145:         $typesref = ['active','previous','future'];
 4146:     } elsif ($type eq 'previous' || $type eq 'future') {
 4147:         $typesref = [$type];
 4148:     }
 4149:     my %roles = &get_my_roles($uname,$udom,'userroles',
 4150:                               $typesref,undef,[$cdom]);
 4151:     my ($tmp) = keys(%roles);
 4152:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 4153:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 4154:     if (@course_roles > 0) {
 4155:         return 1;
 4156:     }
 4157:     return 0;
 4158: }
 4159: 
 4160: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 4161: # input: action, courseID, current domain, intended
 4162: #        path to file, source of file, instruction to parse file for objects,
 4163: #        ref to hash for embedded objects,
 4164: #        ref to hash for codebase of java objects.
 4165: #        reference to scalar to accommodate mime type determined
 4166: #          from File::MMagic if $parser = parse.
 4167: #
 4168: # output: url to file (if action was uploaddoc), 
 4169: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 4170: #
 4171: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 4172: # course.
 4173: #
 4174: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4175: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 4176: #          course's home server.
 4177: #
 4178: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 4179: #          be copied from $source (current location) to 
 4180: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4181: #         and will then be copied to
 4182: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 4183: #         course's home server.
 4184: #
 4185: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4186: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 4187: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 4188: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 4189: #         in course's home server.
 4190: #
 4191: 
 4192: sub process_coursefile {
 4193:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 4194:         $mimetype)=@_;
 4195:     my $fetchresult;
 4196:     my $home=&homeserver($docuname,$docudom);
 4197:     if ($action eq 'propagate') {
 4198:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4199: 			     $home);
 4200:     } else {
 4201:         my $fpath = '';
 4202:         my $fname = $file;
 4203:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4204:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4205:         my $filepath = &build_filepath($fpath);
 4206:         if ($action eq 'copy') {
 4207:             if ($source eq '') {
 4208:                 $fetchresult = 'no source file';
 4209:                 return $fetchresult;
 4210:             } else {
 4211:                 my $destination = $filepath.'/'.$fname;
 4212:                 rename($source,$destination);
 4213:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4214:                                  $home);
 4215:             }
 4216:         } elsif ($action eq 'uploaddoc') {
 4217:             open(my $fh,'>',$filepath.'/'.$fname);
 4218:             print $fh $env{'form.'.$source};
 4219:             close($fh);
 4220:             if ($parser eq 'parse') {
 4221:                 my $mm = new File::MMagic;
 4222:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 4223:                 if ($type eq 'text/html') {
 4224:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 4225:                     unless ($parse_result eq 'ok') {
 4226:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 4227:                     }
 4228:                 }
 4229:                 if (ref($mimetype)) {
 4230:                     $$mimetype = $type;
 4231:                 } 
 4232:             }
 4233:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4234:                                  $home);
 4235:             if ($fetchresult eq 'ok') {
 4236:                 return '/uploaded/'.$fpath.'/'.$fname;
 4237:             } else {
 4238:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4239:                         ' to host '.$home.': '.$fetchresult);
 4240:                 return '/adm/notfound.html';
 4241:             }
 4242:         }
 4243:     }
 4244:     unless ( $fetchresult eq 'ok') {
 4245:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4246:              ' to host '.$home.': '.$fetchresult);
 4247:     }
 4248:     return $fetchresult;
 4249: }
 4250: 
 4251: sub build_filepath {
 4252:     my ($fpath) = @_;
 4253:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 4254:     unless ($fpath eq '') {
 4255:         my @parts=split('/',$fpath);
 4256:         foreach my $part (@parts) {
 4257:             $filepath.= '/'.$part;
 4258:             if ((-e $filepath)!=1) {
 4259:                 mkdir($filepath,0777);
 4260:             }
 4261:         }
 4262:     }
 4263:     return $filepath;
 4264: }
 4265: 
 4266: sub store_edited_file {
 4267:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 4268:     my $file = $primary_url;
 4269:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 4270:     my $fpath = '';
 4271:     my $fname = $file;
 4272:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 4273:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 4274:     my $filepath = &build_filepath($fpath);
 4275:     open(my $fh,'>',$filepath.'/'.$fname);
 4276:     print $fh $content;
 4277:     close($fh);
 4278:     my $home=&homeserver($docuname,$docudom);
 4279:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4280: 			  $home);
 4281:     if ($$fetchresult eq 'ok') {
 4282:         return '/uploaded/'.$fpath.'/'.$fname;
 4283:     } else {
 4284:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4285: 		 ' to host '.$home.': '.$$fetchresult);
 4286:         return '/adm/notfound.html';
 4287:     }
 4288: }
 4289: 
 4290: sub clean_filename {
 4291:     my ($fname,$args)=@_;
 4292: # Replace Windows backslashes by forward slashes
 4293:     $fname=~s/\\/\//g;
 4294:     if (!$args->{'keep_path'}) {
 4295:         # Get rid of everything but the actual filename
 4296: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4297:     }
 4298: # Replace spaces by underscores
 4299:     $fname=~s/\s+/\_/g;
 4300: # Transliterate non-ascii text to ascii
 4301:     my $lang = &Apache::lonlocal::current_language();
 4302:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4303: # Replace all other weird characters by nothing
 4304:     $fname=~s{[^/\w\.\-]}{}g;
 4305: # Replace all .\d. sequences with _\d. so they no longer look like version
 4306: # numbers
 4307:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4308: # Replace three or more adjacent underscores with one for consistency 
 4309: # with loncfile::filename_check() so complete url can be extracted by
 4310: # lonnet::decode_symb()
 4311:     $fname=~s/_{3,}/_/g;
 4312:     return $fname;
 4313: }
 4314: 
 4315: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4316: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4317: # image with the same aspect ratio as the original, but with dimensions which do 
 4318: # not exceed $resizewidth and $resizeheight.
 4319:  
 4320: sub resizeImage {
 4321:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4322:     my $ima = Image::Magick->new;
 4323:     my $resized;
 4324:     if (-e $img_path) {
 4325:         $ima->Read($img_path);
 4326:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4327:             my $width = $ima->Get('width');
 4328:             my $height = $ima->Get('height');
 4329:             if ($width > $resizewidth) {
 4330: 	        my $factor = $width/$resizewidth;
 4331:                 my $newheight = $height/$factor;
 4332:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4333:                 $resized = 1;
 4334:             }
 4335:         }
 4336:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4337:             my $width = $ima->Get('width');
 4338:             my $height = $ima->Get('height');
 4339:             if ($height > $resizeheight) {
 4340:                 my $factor = $height/$resizeheight;
 4341:                 my $newwidth = $width/$factor;
 4342:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4343:                 $resized = 1;
 4344:             }
 4345:         }
 4346:         if ($resized) {
 4347:             $ima->Write($img_path);
 4348:         }
 4349:     }
 4350:     return;
 4351: }
 4352: 
 4353: # --------------- Take an uploaded file and put it into the userfiles directory
 4354: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4355: #                    the desired filename is in $env{"form.$formname.filename"}
 4356: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4357: #                                    canceloverwrite, scantron, toollogo  or ''.
 4358: #                   if 'coursedoc': upload to the current course
 4359: #                   if 'existingfile': write file to tmp/overwrites directory 
 4360: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4361: #                   $context is passed as argument to &finishuserfileupload
 4362: #        $subdir - directory in userfile to store the file into
 4363: #        $parser - instruction to parse file for objects ($parser = parse) or
 4364: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4365: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4366: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4367: #        $allfiles - reference to hash for embedded objects
 4368: #        $codebase - reference to hash for codebase of java objects
 4369: #        $destuname - username for permanent storage of uploaded file
 4370: #        $destudom - domain for permanaent storage of uploaded file
 4371: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4372: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4373: #        $resizewidth - width (pixels) to which to resize uploaded image
 4374: #        $resizeheight - height (pixels) to which to resize uploaded image
 4375: #        $mimetype - reference to scalar to accommodate mime type determined
 4376: #                    from File::MMagic.
 4377: # 
 4378: # output: url of file in userspace, or error: <message> 
 4379: #             or /adm/notfound.html if failure to upload occurse
 4380: 
 4381: sub userfileupload {
 4382:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4383:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4384:     if (!defined($subdir)) { $subdir='unknown'; }
 4385:     my $fname=$env{'form.'.$formname.'.filename'};
 4386:     $fname=&clean_filename($fname);
 4387:     # See if there is anything left
 4388:     unless ($fname) { return 'error: no uploaded file'; }
 4389:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4390:     if ($fname =~ /^\./) {
 4391:         my ($s,$usec) = &gettimeofday();
 4392:         while (length($usec) < 6) {
 4393:             $usec = '0'.$usec;
 4394:         }
 4395:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4396:     }
 4397:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4398:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4399:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4400:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4401:         my $now = time;
 4402:         my $filepath;
 4403:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4404:              $filepath = 'tmp/helprequests/'.$now;
 4405:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4406:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4407:                          '_'.$env{'user.domain'}.'/pending';
 4408:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4409:             my ($docuname,$docudom);
 4410:             if ($destudom =~ /^$match_domain$/) {
 4411:                 $docudom = $destudom;
 4412:             } else {
 4413:                 $docudom = $env{'user.domain'};
 4414:             }
 4415:             if ($destuname =~ /^$match_username$/) {
 4416:                 $docuname = $destuname;
 4417:             } else {
 4418:                 $docuname = $env{'user.name'};
 4419:             }
 4420:             if (exists($env{'form.group'})) {
 4421:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4422:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4423:             }
 4424:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4425:             if ($context eq 'canceloverwrite') {
 4426:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4427:                 if (-e  $tempfile) {
 4428:                     my @info = stat($tempfile);
 4429:                     if ($info[9] eq $env{'form.timestamp'}) {
 4430:                         unlink($tempfile);
 4431:                     }
 4432:                 }
 4433:                 return;
 4434:             }
 4435:         }
 4436:         # Create the directory if not present
 4437:         my @parts=split(/\//,$filepath);
 4438:         my $fullpath = $perlvar{'lonDaemons'};
 4439:         for (my $i=0;$i<@parts;$i++) {
 4440:             $fullpath .= '/'.$parts[$i];
 4441:             if ((-e $fullpath)!=1) {
 4442:                 mkdir($fullpath,0777);
 4443:             }
 4444:         }
 4445:         open(my $fh,'>',$fullpath.'/'.$fname);
 4446:         print $fh $env{'form.'.$formname};
 4447:         close($fh);
 4448:         if ($context eq 'existingfile') {
 4449:             my @info = stat($fullpath.'/'.$fname);
 4450:             return ($fullpath.'/'.$fname,$info[9]);
 4451:         } else {
 4452:             return $fullpath.'/'.$fname;
 4453:         }
 4454:     }
 4455:     if ($subdir eq 'scantron') {
 4456:         $fname = 'scantron_orig_'.$fname;
 4457:     } else {
 4458:         $fname="$subdir/$fname";
 4459:     }
 4460:     if ($context eq 'coursedoc') {
 4461: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4462: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4463:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4464:             return &finishuserfileupload($docuname,$docudom,
 4465: 					 $formname,$fname,$parser,$allfiles,
 4466: 					 $codebase,$thumbwidth,$thumbheight,
 4467:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4468:         } else {
 4469:             if ($env{'form.folder'}) {
 4470:                 $fname=$env{'form.folder'}.'/'.$fname;
 4471:             }
 4472:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4473: 				       $fname,$formname,$parser,
 4474: 				       $allfiles,$codebase,$mimetype);
 4475:         }
 4476:     } elsif (defined($destuname)) {
 4477:         my $docuname=$destuname;
 4478:         my $docudom=$destudom;
 4479: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4480: 				     $parser,$allfiles,$codebase,
 4481:                                      $thumbwidth,$thumbheight,
 4482:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4483:     } else {
 4484:         my $docuname=$env{'user.name'};
 4485:         my $docudom=$env{'user.domain'};
 4486:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4487:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4488:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4489:         }
 4490: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4491: 				     $parser,$allfiles,$codebase,
 4492:                                      $thumbwidth,$thumbheight,
 4493:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4494:     }
 4495: }
 4496: 
 4497: sub finishuserfileupload {
 4498:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4499:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4500:     my $path=$docudom.'/'.$docuname.'/';
 4501:     my $filepath=$perlvar{'lonDocRoot'};
 4502:   
 4503:     my ($fnamepath,$file,$fetchthumb);
 4504:     $file=$fname;
 4505:     if ($fname=~m|/|) {
 4506:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4507: 	$path.=$fnamepath.'/';
 4508:     }
 4509:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4510:     my $count;
 4511:     for ($count=4;$count<=$#parts;$count++) {
 4512:         $filepath.="/$parts[$count]";
 4513:         if ((-e $filepath)!=1) {
 4514: 	    mkdir($filepath,0777);
 4515:         }
 4516:     }
 4517: 
 4518: # Save the file
 4519:     {
 4520: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4521: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4522: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4523: 	    return '/adm/notfound.html';
 4524: 	}
 4525:         if ($context eq 'overwrite') {
 4526:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4527:             my $target = $filepath.'/'.$file;
 4528:             if (-e $source) {
 4529:                 my @info = stat($source);
 4530:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4531:                     unless (&File::Copy::move($source,$target)) {
 4532:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4533:                         return "Moving from $source failed";
 4534:                     }
 4535:                 } else {
 4536:                     return "Temporary file: $source had unexpected date/time for last modification";
 4537:                 }
 4538:             } else {
 4539:                 return "Temporary file: $source missing";
 4540:             }
 4541:         } elsif (!print FH ($env{'form.'.$formname})) {
 4542: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4543: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4544: 	    return '/adm/notfound.html';
 4545: 	}
 4546: 	close(FH);
 4547:         if ($resizewidth && $resizeheight) {
 4548:             my $mm = new File::MMagic;
 4549:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4550:             if ($mime_type =~ m{^image/}) {
 4551: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4552:             }  
 4553: 	}
 4554:     }
 4555:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4556:         if (ref($mimetype)) {
 4557:             if ($$mimetype eq '') {
 4558:                 my $mm = new File::MMagic;
 4559:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4560:                 $$mimetype = $type;
 4561:             }
 4562:         }
 4563:     }
 4564:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4565:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4566:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4567:                                                        $allfiles,$codebase);
 4568:             unless ($parse_result eq 'ok') {
 4569:                 &logthis('Failed to parse '.$filepath.$file.
 4570: 	   	         ' for embedded media: '.$parse_result); 
 4571:             }
 4572:         }
 4573:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4574:         my $format = $env{'form.scantron_format'};
 4575:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4576:     }
 4577:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4578:         my $input = $filepath.'/'.$file;
 4579:         my $output = $filepath.'/'.'tn-'.$file;
 4580:         my $makethumb; 
 4581:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4582:         if ($context eq 'toollogo') {
 4583:             my ($fullwidth,$fullheight) = &check_dimensions($input);
 4584:             if ($fullwidth ne '' && $fullheight ne '') {
 4585:                 if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 4586:                     $makethumb = 1;
 4587:                 }
 4588:             }
 4589:         } else {
 4590:             $makethumb = 1;
 4591:         }
 4592:         if ($makethumb) {
 4593:             my @args = ('convert','-sample',$thumbsize,$input,$output);
 4594:             system({$args[0]} @args);
 4595:             if (-e $filepath.'/'.'tn-'.$file) {
 4596:                 $fetchthumb  = 1; 
 4597:             }
 4598:         }
 4599:     }
 4600:  
 4601: # Notify homeserver to grep it
 4602: #
 4603:     my $docuhome=&homeserver($docuname,$docudom);	
 4604:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4605:     if ($fetchresult eq 'ok') {
 4606:         if ($fetchthumb) {
 4607:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4608:             if ($thumbresult ne 'ok') {
 4609:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4610:                          $docuhome.': '.$thumbresult);
 4611:             }
 4612:         }
 4613: #
 4614: # Return the URL to it
 4615:         return '/uploaded/'.$path.$file;
 4616:     } else {
 4617:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4618: 		 ': '.$fetchresult);
 4619:         return '/adm/notfound.html';
 4620:     }
 4621: }
 4622: 
 4623: sub extract_embedded_items {
 4624:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4625:     my @state = ();
 4626:     my (%lastids,%related,%shockwave,%flashvars);
 4627:     my %javafiles = (
 4628:                       codebase => '',
 4629:                       code => '',
 4630:                       archive => ''
 4631:                     );
 4632:     my %mediafiles = (
 4633:                       src => '',
 4634:                       movie => '',
 4635:                      );
 4636:     my $p;
 4637:     if ($content) {
 4638:         $p = HTML::LCParser->new($content);
 4639:     } else {
 4640:         $p = HTML::LCParser->new($fullpath);
 4641:     }
 4642:     while (my $t=$p->get_token()) {
 4643: 	if ($t->[0] eq 'S') {
 4644: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4645: 	    push(@state, $tagname);
 4646:             if (lc($tagname) eq 'allow') {
 4647:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4648:             }
 4649: 	    if (lc($tagname) eq 'img') {
 4650: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4651: 	    }
 4652: 	    if (lc($tagname) eq 'a') {
 4653:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4654:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4655:                 }
 4656: 	    }
 4657:             if (lc($tagname) eq 'script') {
 4658:                 my $src;
 4659:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4660:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4661:                 } else {
 4662:                     if ($attr->{'src'} ne '') {
 4663:                         $src = $attr->{'src'};
 4664:                         &add_filetype($allfiles,$src,'src');
 4665:                     }
 4666:                 }
 4667:                 my $text = $p->get_trimmed_text();
 4668:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4669:                     my @swfargs = split(/,/,$1);
 4670:                     foreach my $item (@swfargs) {
 4671:                         $item =~ s/["']//g;
 4672:                         $item =~ s/^\s+//;
 4673:                         $item =~ s/\s+$//;
 4674:                     }
 4675:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4676:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4677:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4678:                         } else {
 4679:                             $related{$swfargs[0]} = [$swfargs[2]];
 4680:                         }
 4681:                     }
 4682:                 }
 4683:             }
 4684:             if (lc($tagname) eq 'link') {
 4685:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4686:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4687:                 }
 4688:             }
 4689: 	    if (lc($tagname) eq 'object' ||
 4690: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4691: 		foreach my $item (keys(%javafiles)) {
 4692: 		    $javafiles{$item} = '';
 4693: 		}
 4694:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4695:                     $lastids{lc($tagname)} = $attr->{'id'};
 4696:                 }
 4697: 	    }
 4698: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4699: 		my $name = lc($attr->{'name'});
 4700: 		foreach my $item (keys(%javafiles)) {
 4701: 		    if ($name eq $item) {
 4702: 			$javafiles{$item} = $attr->{'value'};
 4703: 			last;
 4704: 		    }
 4705: 		}
 4706:                 my $pathfrom;
 4707: 		foreach my $item (keys(%mediafiles)) {
 4708: 		    if ($name eq $item) {
 4709:                         $pathfrom = $attr->{'value'};
 4710:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4711: 			&add_filetype($allfiles,$pathfrom,$name);
 4712: 			last;
 4713: 		    }
 4714: 		}
 4715:                 if ($name eq 'flashvars') {
 4716:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4717:                 }
 4718:                 if ($pathfrom ne '') {
 4719:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4720:                                          $pathfrom);
 4721:                 }
 4722: 	    }
 4723: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4724: 		foreach my $item (keys(%javafiles)) {
 4725: 		    if ($attr->{$item}) {
 4726: 			$javafiles{$item} = $attr->{$item};
 4727: 			last;
 4728: 		    }
 4729: 		}
 4730: 		foreach my $item (keys(%mediafiles)) {
 4731: 		    if ($attr->{$item}) {
 4732: 			&add_filetype($allfiles,$attr->{$item},$item);
 4733: 			last;
 4734: 		    }
 4735: 		}
 4736:                 if (lc($tagname) eq 'embed') {
 4737:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4738:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4739:                                              $attr->{'src'});
 4740:                     }
 4741:                 }
 4742: 	    }
 4743:             if (lc($tagname) eq 'iframe') {
 4744:                 my $src = $attr->{'src'} ;
 4745:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4746:                     &add_filetype($allfiles,$src,'src');
 4747:                 } elsif ($src =~ m{^/}) {
 4748:                     if ($env{'request.course.id'}) {
 4749:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4750:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4751:                         my $url = &hreflocation('',$fullpath);
 4752:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4753:                             my $relpath = $1;
 4754:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4755:                                 &add_filetype($allfiles,$1,'src');
 4756:                             }
 4757:                         }
 4758:                     }
 4759:                 }
 4760:             }
 4761:             if ($t->[4] =~ m{/>$}) {
 4762:                 pop(@state);
 4763:             }
 4764: 	} elsif ($t->[0] eq 'E') {
 4765: 	    my ($tagname) = ($t->[1]);
 4766: 	    if ($javafiles{'codebase'} ne '') {
 4767: 		$javafiles{'codebase'} .= '/';
 4768: 	    }  
 4769: 	    if (lc($tagname) eq 'applet' ||
 4770: 		lc($tagname) eq 'object' ||
 4771: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4772: 		) {
 4773: 		foreach my $item (keys(%javafiles)) {
 4774: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4775: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4776: 			&add_filetype($allfiles,$file,$item);
 4777: 		    }
 4778: 		}
 4779: 	    } 
 4780: 	    pop @state;
 4781: 	}
 4782:     }
 4783:     foreach my $id (sort(keys(%flashvars))) {
 4784:         if ($shockwave{$id} ne '') {
 4785:             my @pairs = split(/\&/,$flashvars{$id});
 4786:             foreach my $pair (@pairs) {
 4787:                 my ($key,$value) = split(/\=/,$pair);
 4788:                 if ($key eq 'thumb') {
 4789:                     &add_filetype($allfiles,$value,$key);
 4790:                 } elsif ($key eq 'content') {
 4791:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4792:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4793:                     if ($ext ne '') {
 4794:                         &add_filetype($allfiles,$path.$value,$ext);
 4795:                     }
 4796:                 }
 4797:             }
 4798:         }
 4799:     }
 4800:     return 'ok';
 4801: }
 4802: 
 4803: sub add_filetype {
 4804:     my ($allfiles,$file,$type)=@_;
 4805:     if (exists($allfiles->{$file})) {
 4806: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4807: 	    push(@{$allfiles->{$file}}, &escape($type));
 4808: 	}
 4809:     } else {
 4810: 	@{$allfiles->{$file}} = (&escape($type));
 4811:     }
 4812: }
 4813: 
 4814: sub embedded_dependency {
 4815:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4816:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4817:         if (($identifier ne '') &&
 4818:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4819:             ($pathfrom ne '')) {
 4820:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4821:             foreach my $dep (@{$related->{$identifier}}) {
 4822:                 &add_filetype($allfiles,$path.$dep,'object');
 4823:             }
 4824:         }
 4825:     }
 4826:     return;
 4827: }
 4828: 
 4829: sub check_dimensions {
 4830:     my ($inputfile) = @_;
 4831:     my ($fullwidth,$fullheight);
 4832:     if (($inputfile =~ m|^[/\w.\-]+$|) && (-e $inputfile)) {
 4833:         my $mm = new File::MMagic;
 4834:         my $mime_type = $mm->checktype_filename($inputfile);
 4835:         if ($mime_type =~ m{^image/}) {
 4836:             if (open(PIPE,"identify $inputfile 2>&1 |")) {
 4837:                 my $imageinfo = <PIPE>;
 4838:                 if (!close(PIPE)) {
 4839:                     &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 4840:                 }
 4841:                 chomp($imageinfo);
 4842:                 my ($fullsize) =
 4843:                     ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 4844:                 if ($fullsize) {
 4845:                     ($fullwidth,$fullheight) = split(/x/,$fullsize);
 4846:                 }
 4847:             }
 4848:         }
 4849:     }
 4850:     return ($fullwidth,$fullheight);
 4851: }
 4852: 
 4853: sub bubblesheet_converter {
 4854:     my ($cdom,$fullpath,$config,$format) = @_;
 4855:     if ((&domain($cdom) ne '') &&
 4856:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4857:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4858:         my (%csvcols,%csvoptions);
 4859:         if (ref($config->{'fields'}) eq 'HASH') {  
 4860:             %csvcols = %{$config->{'fields'}};
 4861:         }
 4862:         if (ref($config->{'options'}) eq 'HASH') {
 4863:             %csvoptions = %{$config->{'options'}};
 4864:         }
 4865:         my %csvbynum = reverse(%csvcols);
 4866:         my %scantronconf = &get_scantron_config($format,$cdom);
 4867:         if (keys(%scantronconf)) {
 4868:             my %bynum = (
 4869:                           $scantronconf{CODEstart} => 'CODEstart',
 4870:                           $scantronconf{IDstart}   => 'IDstart',
 4871:                           $scantronconf{PaperID}   => 'PaperID',
 4872:                           $scantronconf{FirstName} => 'FirstName',
 4873:                           $scantronconf{LastName}  => 'LastName',
 4874:                           $scantronconf{Qstart}    => 'Qstart',
 4875:                         );
 4876:             my @ordered;
 4877:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4878:                 push(@ordered,$bynum{$item});
 4879:             }
 4880:             my %mapstart = (
 4881:                               CODEstart => 'CODE',
 4882:                               IDstart   => 'ID',
 4883:                               PaperID   => 'PaperID',
 4884:                               FirstName => 'FirstName',
 4885:                               LastName  => 'LastName',
 4886:                               Qstart    => 'FirstQuestion',
 4887:                            );
 4888:             my %maplength = (
 4889:                               CODEstart => 'CODElength',
 4890:                               IDstart   => 'IDlength',
 4891:                               PaperID   => 'PaperIDlength',
 4892:                               FirstName => 'FirstNamelength',
 4893:                               LastName  => 'LastNamelength',
 4894:             );
 4895:             if (open(my $fh,'<',$fullpath)) {
 4896:                 my $output;
 4897:                 my %lettdig = &letter_to_digits();
 4898:                 my %diglett = reverse(%lettdig);
 4899:                 my $numletts = scalar(keys(%lettdig));
 4900:                 my $num = 0;
 4901:                 while (my $line=<$fh>) {
 4902:                     $num ++;
 4903:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4904:                     $line =~ s{[\r\n]+$}{};
 4905:                     my %found;
 4906:                     my @values = split(/,/,$line,-1);
 4907:                     my ($qstart,$record);
 4908:                     for (my $i=0; $i<@values; $i++) {
 4909:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4910:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4911:                             if ($values[$i] eq '') {
 4912:                                 $values[$i] = $scantronconf{'Qoff'};
 4913:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4914:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4915:                                     $values[$i] = $lettdig{uc($values[$i])};
 4916:                                 }
 4917:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4918:                                 if ($values[$i] =~ /^[0-9]$/) {
 4919:                                     $values[$i] = $diglett{$values[$i]};
 4920:                                 }
 4921:                             } else {
 4922:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4923:                                     my $digit;
 4924:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4925:                                         $digit = $lettdig{uc($values[$i])}-1;
 4926:                                         if ($values[$i] eq 'J') {
 4927:                                             $digit += $numletts;
 4928:                                         }
 4929:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4930:                                         $digit = $values[$i]-1;
 4931:                                         if ($values[$i] eq '0') {
 4932:                                             $digit += $numletts;
 4933:                                         }
 4934:                                     }
 4935:                                     my $qval='';
 4936:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4937:                                         if ($j == $digit) {
 4938:                                             $qval .= $scantronconf{'Qon'};
 4939:                                         } else {
 4940:                                             $qval .= $scantronconf{'Qoff'};
 4941:                                         }
 4942:                                     }
 4943:                                     $values[$i] = $qval;
 4944:                                 }
 4945:                             }
 4946:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4947:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4948:                             }
 4949:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4950:                             if ($numblank > 0) {
 4951:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4952:                             }
 4953:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4954:                                 $qstart = $i;
 4955:                                 $found{$csvbynum{$i}} = $values[$i];
 4956:                             } else {
 4957:                                 $found{'FirstQuestion'} .= $values[$i];
 4958:                             }
 4959:                         } elsif (exists($csvbynum{$i})) {
 4960:                             if ($csvoptions{'rem'}) {
 4961:                                 $values[$i] =~ s/^\s+//;
 4962:                             }
 4963:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4964:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4965:                                     $values[$i] = '0'.$values[$i];
 4966:                                 }
 4967:                             }
 4968:                             $found{$csvbynum{$i}} = $values[$i];
 4969:                         }
 4970:                     }
 4971:                     foreach my $item (@ordered) {
 4972:                         my $currlength = 1+length($record);
 4973:                         my $numspaces = $scantronconf{$item} - $currlength;
 4974:                         if ($numspaces > 0) {
 4975:                             $record .= (' ' x $numspaces);
 4976:                         }
 4977:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4978:                             unless ($item eq 'Qstart') {
 4979:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4980:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4981:                                 }
 4982:                             }
 4983:                             $record .= $found{$mapstart{$item}};
 4984:                         }
 4985:                     }
 4986:                     $output .= "$record\n";
 4987:                 }
 4988:                 close($fh);
 4989:                 if ($output) {
 4990:                     if (open(my $fh,'>',$fullpath)) {
 4991:                         print $fh $output;
 4992:                         close($fh);
 4993:                     }
 4994:                 }
 4995:             }
 4996:         }
 4997:         return;
 4998:     }
 4999: }
 5000: 
 5001: sub letter_to_digits {
 5002:     my %lettdig = (
 5003:                     A => 1,
 5004:                     B => 2,
 5005:                     C => 3,
 5006:                     D => 4,
 5007:                     E => 5,
 5008:                     F => 6,
 5009:                     G => 7,
 5010:                     H => 8,
 5011:                     I => 9,
 5012:                     J => 0,
 5013:                   );
 5014:     return %lettdig;
 5015: }
 5016: 
 5017: sub get_scantron_config {
 5018:     my ($which,$cdom) = @_;
 5019:     my @lines = &get_scantronformat_file($cdom);
 5020:     my %config;
 5021:     #FIXME probably should move to XML it has already gotten a bit much now
 5022:     foreach my $line (@lines) {
 5023:         my ($name,$descrip)=split(/:/,$line);
 5024:         if ($name ne $which ) { next; }
 5025:         chomp($line);
 5026:         my @config=split(/:/,$line);
 5027:         $config{'name'}=$config[0];
 5028:         $config{'description'}=$config[1];
 5029:         $config{'CODElocation'}=$config[2];
 5030:         $config{'CODEstart'}=$config[3];
 5031:         $config{'CODElength'}=$config[4];
 5032:         $config{'IDstart'}=$config[5];
 5033:         $config{'IDlength'}=$config[6];
 5034:         $config{'Qstart'}=$config[7];
 5035:         $config{'Qlength'}=$config[8];
 5036:         $config{'Qoff'}=$config[9];
 5037:         $config{'Qon'}=$config[10];
 5038:         $config{'PaperID'}=$config[11];
 5039:         $config{'PaperIDlength'}=$config[12];
 5040:         $config{'FirstName'}=$config[13];
 5041:         $config{'FirstNamelength'}=$config[14];
 5042:         $config{'LastName'}=$config[15];
 5043:         $config{'LastNamelength'}=$config[16];
 5044:         $config{'BubblesPerRow'}=$config[17];
 5045:         last;
 5046:     }
 5047:     return %config;
 5048: }
 5049: 
 5050: sub get_scantronformat_file {
 5051:     my ($cdom) = @_;
 5052:     if ($cdom eq '') {
 5053:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 5054:     }
 5055:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 5056:     my $gottab = 0;
 5057:     my @lines;
 5058:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5059:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5060:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 5061:             if ($formatfile ne '-1') {
 5062:                 @lines = split("\n",$formatfile,-1);
 5063:                 $gottab = 1;
 5064:             }
 5065:         }
 5066:     }
 5067:     if (!$gottab) {
 5068:         my $confname = $cdom.'-domainconfig';
 5069:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 5070:         my $formatfile = &getfile($default);
 5071:         if ($formatfile ne '-1') {
 5072:             @lines = split("\n",$formatfile,-1);
 5073:             $gottab = 1;
 5074:         }
 5075:     }
 5076:     if (!$gottab) {
 5077:         my @domains = &current_machine_domains();
 5078:         if (grep(/^\Q$cdom\E$/,@domains)) {
 5079:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 5080:                 @lines = <$fh>;
 5081:                 close($fh);
 5082:             }
 5083:         } else {
 5084:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 5085:                 @lines = <$fh>;
 5086:                 close($fh);
 5087:             }
 5088:         }
 5089:         chomp(@lines);
 5090:     }
 5091:     return @lines;
 5092: }
 5093: 
 5094: sub removeuploadedurl {
 5095:     my ($url)=@_;	
 5096:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 5097:     return &removeuserfile($uname,$udom,$fname);
 5098: }
 5099: 
 5100: sub removeuserfile {
 5101:     my ($docuname,$docudom,$fname)=@_;
 5102:     my $home=&homeserver($docuname,$docudom);    
 5103:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 5104:     if ($result eq 'ok') {	
 5105:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 5106:             my $metafile = $fname.'.meta';
 5107:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 5108: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 5109:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 5110:             my $sqlresult = 
 5111:                 &update_portfolio_table($docuname,$docudom,$file,
 5112:                                         'portfolio_metadata',$group,
 5113:                                         'delete');
 5114:         }
 5115:     }
 5116:     return $result;
 5117: }
 5118: 
 5119: sub mkdiruserfile {
 5120:     my ($docuname,$docudom,$dir)=@_;
 5121:     my $home=&homeserver($docuname,$docudom);
 5122:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 5123: }
 5124: 
 5125: sub renameuserfile {
 5126:     my ($docuname,$docudom,$old,$new)=@_;
 5127:     my $home=&homeserver($docuname,$docudom);
 5128:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 5129:                         &escape("$old").':'.&escape("$new"),$home);
 5130:     if ($result eq 'ok') {
 5131:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 5132:             my $oldmeta = $old.'.meta';
 5133:             my $newmeta = $new.'.meta';
 5134:             my $metaresult = 
 5135:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 5136: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 5137:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 5138:             my $sqlresult = 
 5139:                 &update_portfolio_table($docuname,$docudom,$file,
 5140:                                         'portfolio_metadata',$group,
 5141:                                         'delete');
 5142:         }
 5143:     }
 5144:     return $result;
 5145: }
 5146: 
 5147: # ------------------------------------------------------------------------- Log
 5148: 
 5149: sub log {
 5150:     my ($dom,$nam,$hom,$what)=@_;
 5151:     return critical("log:$dom:$nam:$what",$hom);
 5152: }
 5153: 
 5154: # ------------------------------------------------------------------ Course Log
 5155: #
 5156: # This routine flushes several buffers of non-mission-critical nature
 5157: #
 5158: 
 5159: sub flushcourselogs {
 5160:     &logthis('Flushing log buffers');
 5161: #
 5162: # course logs
 5163: # This is a log of all transactions in a course, which can be used
 5164: # for data mining purposes
 5165: #
 5166: # It also collects the courseid database, which lists last transaction
 5167: # times and course titles for all courseids
 5168: #
 5169:     my %courseidbuffer=();
 5170:     foreach my $crsid (keys(%courselogs)) {
 5171:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 5172: 		          &escape($courselogs{$crsid}),
 5173: 		          $coursehombuf{$crsid}) eq 'ok') {
 5174: 	    delete $courselogs{$crsid};
 5175:         } else {
 5176:             &logthis('Failed to flush log buffer for '.$crsid);
 5177:             if (length($courselogs{$crsid})>40000) {
 5178:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 5179:                         " exceeded maximum size, deleting.</font>");
 5180:                delete $courselogs{$crsid};
 5181:             }
 5182:         }
 5183:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 5184:             'description' => $coursedescrbuf{$crsid},
 5185:             'inst_code'    => $courseinstcodebuf{$crsid},
 5186:             'type'        => $coursetypebuf{$crsid},
 5187:             'owner'       => $courseownerbuf{$crsid},
 5188:         };
 5189:     }
 5190: #
 5191: # Write course id database (reverse lookup) to homeserver of courses 
 5192: # Is used in pickcourse
 5193: #
 5194:     foreach my $crs_home (keys(%courseidbuffer)) {
 5195:         my $response = &courseidput(&host_domain($crs_home),
 5196:                                     $courseidbuffer{$crs_home},
 5197:                                     $crs_home,'timeonly');
 5198:     }
 5199: #
 5200: # File accesses
 5201: # Writes to the dynamic metadata of resources to get hit counts, etc.
 5202: #
 5203:     foreach my $entry (keys(%accesshash)) {
 5204:         if ($entry =~ /___count$/) {
 5205:             my ($dom,$name);
 5206:             ($dom,$name,undef)=
 5207: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 5208:             if (! defined($dom) || $dom eq '' || 
 5209:                 ! defined($name) || $name eq '') {
 5210:                 my $cid = $env{'request.course.id'};
 5211: #
 5212: # FIXME 11/29/2021
 5213: # Typo in rev. 1.458 (2003/12/09)??
 5214: # These should likely by $env{'course.'.$cid.'.domain'} and $env{'course.'.$cid.'.num'}
 5215: #
 5216: # While these remain as $env{'request.'.$cid.'.domain'} and $env{'request.'.$cid.'.num'}
 5217: # $dom and $name will always be null, so the &inc() call will default to storing this data
 5218: # in a nohist_accesscount.db file for the user rather than the course.
 5219: #
 5220: # That said there is a lot of noise in the data being stored.
 5221: # So counts for prtspool/  and adm/ etc. are recorded.
 5222: #
 5223: # A review of which items ending '___count' are written to %accesshash should likely be 
 5224: # made before deciding whether to set these to 'course.' instead of 'request.'
 5225: #
 5226: # Under the current scheme each user receives a nohist_accesscount.db file listing 
 5227: # accesses for things which are not published resources, regardless of course, and
 5228: # there is not a nohist_accesscount.db file in a course, which might log accesses from
 5229: # anyone in the course for things which are not published resources.
 5230: #
 5231: # For an author, nohist_accesscount.db ends up having records for other items
 5232: # mixed up with the legitimate access counts for the author's published resources.
 5233: #
 5234:                 $dom  = $env{'request.'.$cid.'.domain'};
 5235:                 $name = $env{'request.'.$cid.'.num'};
 5236:             }
 5237:             my $value = $accesshash{$entry};
 5238:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 5239:             my %temphash=($url => $value);
 5240:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 5241:             if ($result eq 'ok') {
 5242:                 delete $accesshash{$entry};
 5243:             }
 5244:         } else {
 5245:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 5246:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 5247:             my %temphash=($entry => $accesshash{$entry});
 5248:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 5249:                 delete $accesshash{$entry};
 5250:             }
 5251:         }
 5252:     }
 5253: #
 5254: # Roles
 5255: # Reverse lookup of user roles for course faculty/staff and co-authorship
 5256: #
 5257:     foreach my $entry (keys(%userrolehash)) {
 5258:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 5259: 	    split(/\:/,$entry);
 5260:         if (&put('nohist_userroles',
 5261:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 5262:                 $rudom,$runame) eq 'ok') {
 5263: 	    delete $userrolehash{$entry};
 5264:         }
 5265:     }
 5266: #
 5267: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 5268: #
 5269:     my %domrolebuffer = ();
 5270:     foreach my $entry (keys(%domainrolehash)) {
 5271:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 5272:         if ($domrolebuffer{$rudom}) {
 5273:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 5274:                       '='.&escape($domainrolehash{$entry});
 5275:         } else {
 5276:             $domrolebuffer{$rudom}.=&escape($entry).
 5277:                       '='.&escape($domainrolehash{$entry});
 5278:         }
 5279:         delete $domainrolehash{$entry};
 5280:     }
 5281:     foreach my $dom (keys(%domrolebuffer)) {
 5282: 	my %servers;
 5283: 	if (defined(&domain($dom,'primary'))) {
 5284: 	    my $primary=&domain($dom,'primary');
 5285: 	    my $hostname=&hostname($primary);
 5286: 	    $servers{$primary} = $hostname;
 5287: 	} else { 
 5288: 	    %servers = &get_servers($dom,'library');
 5289: 	}
 5290: 	foreach my $tryserver (keys(%servers)) {
 5291: 	    if (&reply('domroleput:'.$dom.':'.
 5292: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 5293: 		last;
 5294: 	    } else {  
 5295: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 5296: 	    }
 5297:         }
 5298:     }
 5299:     $dumpcount++;
 5300: }
 5301: 
 5302: sub courselog {
 5303:     my $what=shift;
 5304:     $what=time.':'.$what;
 5305:     unless ($env{'request.course.id'}) { return ''; }
 5306:     $coursedombuf{$env{'request.course.id'}}=
 5307:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 5308:     $coursenumbuf{$env{'request.course.id'}}=
 5309:        $env{'course.'.$env{'request.course.id'}.'.num'};
 5310:     $coursehombuf{$env{'request.course.id'}}=
 5311:        $env{'course.'.$env{'request.course.id'}.'.home'};
 5312:     $coursedescrbuf{$env{'request.course.id'}}=
 5313:        $env{'course.'.$env{'request.course.id'}.'.description'};
 5314:     $courseinstcodebuf{$env{'request.course.id'}}=
 5315:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 5316:     $courseownerbuf{$env{'request.course.id'}}=
 5317:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 5318:     $coursetypebuf{$env{'request.course.id'}}=
 5319:        $env{'course.'.$env{'request.course.id'}.'.type'};
 5320:     if (defined $courselogs{$env{'request.course.id'}}) {
 5321: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 5322:     } else {
 5323: 	$courselogs{$env{'request.course.id'}}.=$what;
 5324:     }
 5325:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 5326: 	&flushcourselogs();
 5327:     }
 5328: }
 5329: 
 5330: sub courseacclog {
 5331:     my $fnsymb=shift;
 5332:     unless ($env{'request.course.id'}) { return ''; }
 5333:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 5334:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 5335:         $what.=':POST';
 5336:         # FIXME: Probably ought to escape things....
 5337: 	foreach my $key (keys(%env)) {
 5338:             if ($key=~/^form\.(.*)/) {
 5339:                 my $formitem = $1;
 5340:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5341:                     $what.=':'.$formitem.'='.$env{$key};
 5342:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5343:                     if ($formitem eq 'proctorpassword') {
 5344:                         $what.=':'.$formitem.'=' . '*' x length($env{$key});
 5345:                     } else {
 5346:                         $what.=':'.$formitem.'='.$env{$key};
 5347:                     }
 5348:                 }
 5349:             }
 5350:         }
 5351:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5352:         # FIXME: We should not be depending on a form parameter that someone
 5353:         # editing lonsearchcat.pm might change in the future.
 5354:         if ($env{'form.phase'} eq 'course_search') {
 5355:             $what.= ':POST';
 5356:             # FIXME: Probably ought to escape things....
 5357:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5358:                                  'crsdiscuss') {
 5359:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5360:             }
 5361:         }
 5362:     }
 5363:     &courselog($what);
 5364: }
 5365: 
 5366: sub countacc {
 5367:     my $url=&declutter(shift);
 5368:     return if (! defined($url) || $url eq '');
 5369:     unless ($env{'request.course.id'}) { return ''; }
 5370: #
 5371: # Mark that this url was used in this course
 5372: #
 5373:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5374: #
 5375: # Increase the access count for this resource in this child process
 5376: #
 5377:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5378:     $accesshash{$key}++;
 5379: }
 5380: 
 5381: sub linklog {
 5382:     my ($from,$to)=@_;
 5383:     $from=&declutter($from);
 5384:     $to=&declutter($to);
 5385:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5386:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5387: }
 5388: 
 5389: sub statslog {
 5390:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5391:     if ($users<2) { return; }
 5392:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5393:             'course'       => $env{'request.course.id'},
 5394:             'sections'     => '"all"',
 5395:             'num_students' => $users,
 5396:             'part'         => $part,
 5397:             'symb'         => $symb,
 5398:             'mean_tries'   => $av_attempts,
 5399:             'deg_of_diff'  => $degdiff});
 5400:     foreach my $key (keys(%dynstore)) {
 5401:         $accesshash{$key}=$dynstore{$key};
 5402:     }
 5403: }
 5404:   
 5405: sub userrolelog {
 5406:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5407:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5408:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5409:        $userrolehash
 5410:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5411:                     =$tend.':'.$tstart;
 5412:     }
 5413:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5414:        $userrolehash
 5415:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5416:                     =$tend.':'.$tstart;
 5417:     }
 5418:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5419:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5420:        $domainrolehash
 5421:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5422:                     = $tend.':'.$tstart;
 5423:     }
 5424: }
 5425: 
 5426: sub courserolelog {
 5427:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5428:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5429:         my $cdom = $1;
 5430:         my $cnum = $2;
 5431:         my $sec = $3;
 5432:         my $namespace = 'rolelog';
 5433:         my %storehash = (
 5434:                            role    => $trole,
 5435:                            start   => $tstart,
 5436:                            end     => $tend,
 5437:                            selfenroll => $selfenroll,
 5438:                            context    => $context,
 5439:                         );
 5440:         if ($trole eq 'gr') {
 5441:             $namespace = 'groupslog';
 5442:             $storehash{'group'} = $sec;
 5443:         } else {
 5444:             $storehash{'section'} = $sec;
 5445:         }
 5446:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5447:                    $domain,$cnum,$cdom);
 5448:         if (($trole ne 'st') || ($sec ne '')) {
 5449:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5450:         }
 5451:     }
 5452:     return;
 5453: }
 5454: 
 5455: sub domainrolelog {
 5456:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5457:     if ($area =~ m{^/($match_domain)/$}) {
 5458:         my $cdom = $1;
 5459:         my $domconfiguser = &get_domainconfiguser($cdom);
 5460:         my $namespace = 'rolelog';
 5461:         my %storehash = (
 5462:                            role    => $trole,
 5463:                            start   => $tstart,
 5464:                            end     => $tend,
 5465:                            context => $context,
 5466:                         );
 5467:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5468:                    $domain,$domconfiguser,$cdom);
 5469:     }
 5470:     return;
 5471: 
 5472: }
 5473: 
 5474: sub coauthorrolelog {
 5475:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5476:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5477:         my $audom = $1;
 5478:         my $auname = $2;
 5479:         my $namespace = 'rolelog';
 5480:         my %storehash = (
 5481:                            role    => $trole,
 5482:                            start   => $tstart,
 5483:                            end     => $tend,
 5484:                            context => $context,
 5485:                         );
 5486:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5487:                    $domain,$auname,$audom);
 5488:     }
 5489:     return;
 5490: }
 5491: 
 5492: sub get_course_adv_roles {
 5493:     my ($cid,$codes) = @_;
 5494:     $cid=$env{'request.course.id'} unless (defined($cid));
 5495:     my %coursehash=&coursedescription($cid);
 5496:     my $crstype = &Apache::loncommon::course_type($cid);
 5497:     my %nothide=();
 5498:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5499:         if ($user !~ /:/) {
 5500: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5501:         } else {
 5502:             $nothide{$user}=1;
 5503:         }
 5504:     }
 5505:     my @possdoms = ($coursehash{'domain'});
 5506:     if ($coursehash{'checkforpriv'}) {
 5507:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5508:     }
 5509:     my %returnhash=();
 5510:     my %dumphash=
 5511:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5512:     my $now=time;
 5513:     my %privileged;
 5514:     foreach my $entry (keys(%dumphash)) {
 5515: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5516:         if (($tstart) && ($tstart<0)) { next; }
 5517:         if (($tend) && ($tend<$now)) { next; }
 5518:         if (($tstart) && ($now<$tstart)) { next; }
 5519:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5520: 	if ($username eq '' || $domain eq '') { next; }
 5521:         if ((&privileged($username,$domain,\@possdoms)) &&
 5522:             (!$nothide{$username.':'.$domain})) { next; }
 5523: 	if ($role eq 'cr') { next; }
 5524:         if ($codes) {
 5525:             if ($section) { $role .= ':'.$section; }
 5526:             if ($returnhash{$role}) {
 5527:                 $returnhash{$role}.=','.$username.':'.$domain;
 5528:             } else {
 5529:                 $returnhash{$role}=$username.':'.$domain;
 5530:             }
 5531:         } else {
 5532:             my $key=&plaintext($role,$crstype);
 5533:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5534:             if ($returnhash{$key}) {
 5535: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5536:             } else {
 5537:                 $returnhash{$key}=$username.':'.$domain;
 5538:             }
 5539:         }
 5540:     }
 5541:     return %returnhash;
 5542: }
 5543: 
 5544: sub get_my_roles {
 5545:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5546:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5547:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5548:     my (%dumphash,%nothide);
 5549:     if ($context eq 'userroles') {
 5550:         %dumphash = &dump('roles',$udom,$uname);
 5551:     } else {
 5552:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5553:         if ($hidepriv) {
 5554:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5555:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5556:                 if ($user !~ /:/) {
 5557:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5558:                 } else {
 5559:                     $nothide{$user} = 1;
 5560:                 }
 5561:             }
 5562:         }
 5563:     }
 5564:     my %returnhash=();
 5565:     my $now=time;
 5566:     my %privileged;
 5567:     foreach my $entry (keys(%dumphash)) {
 5568:         my ($role,$tend,$tstart);
 5569:         if ($context eq 'userroles') {
 5570:             next if ($entry =~ /^rolesdef/);
 5571: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5572:         } else {
 5573:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5574:         }
 5575:         if (($tstart) && ($tstart<0)) { next; }
 5576:         my $status = 'active';
 5577:         if (($tend) && ($tend<=$now)) {
 5578:             $status = 'previous';
 5579:         } 
 5580:         if (($tstart) && ($now<$tstart)) {
 5581:             $status = 'future';
 5582:         }
 5583:         if (ref($types) eq 'ARRAY') {
 5584:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5585:                 next;
 5586:             } 
 5587:         } else {
 5588:             if ($status ne 'active') {
 5589:                 next;
 5590:             }
 5591:         }
 5592:         my ($rolecode,$username,$domain,$section,$area);
 5593:         if ($context eq 'userroles') {
 5594:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5595:             (undef,$domain,$username,$section) = split(/\//,$area);
 5596:         } else {
 5597:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5598:         }
 5599:         if (ref($roledoms) eq 'ARRAY') {
 5600:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5601:                 next;
 5602:             }
 5603:         }
 5604:         if (ref($roles) eq 'ARRAY') {
 5605:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5606:                 if ($role =~ /^cr\//) {
 5607:                     if (!grep(/^cr$/,@{$roles})) {
 5608:                         next;
 5609:                     }
 5610:                 } elsif ($role =~ /^gr\//) {
 5611:                     if (!grep(/^gr$/,@{$roles})) {
 5612:                         next;
 5613:                     }
 5614:                 } else {
 5615:                     next;
 5616:                 }
 5617:             }
 5618:         }
 5619:         if ($hidepriv) {
 5620:             my @privroles = ('dc','su');
 5621:             if ($context eq 'userroles') {
 5622:                 next if (grep(/^\Q$role\E$/,@privroles));
 5623:             } else {
 5624:                 my $possdoms = [$domain];
 5625:                 if (ref($roledoms) eq 'ARRAY') {
 5626:                    push(@{$possdoms},@{$roledoms}); 
 5627:                 }
 5628:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5629:                     if (!$nothide{$username.':'.$domain}) {
 5630:                         next;
 5631:                     }
 5632:                 }
 5633:             }
 5634:         }
 5635:         if ($withsec) {
 5636:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5637:                 $tstart.':'.$tend;
 5638:         } else {
 5639:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5640:         }
 5641:     }
 5642:     return %returnhash;
 5643: }
 5644: 
 5645: sub get_all_adhocroles {
 5646:     my ($dom) = @_;
 5647:     my @roles_by_num = ();
 5648:     my %domdefaults = &get_domain_defaults($dom);
 5649:     my (%description,%access_in_dom,%access_info);
 5650:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5651:         my $count = 0;
 5652:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5653:         my %ordered;
 5654:         foreach my $role (sort(keys(%domcurrent))) {
 5655:             my ($order,$desc,$access_in_dom);
 5656:             if (ref($domcurrent{$role}) eq 'HASH') {
 5657:                 $order = $domcurrent{$role}{'order'};
 5658:                 $desc = $domcurrent{$role}{'desc'};
 5659:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5660:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5661:             }
 5662:             if ($order eq '') {
 5663:                 $order = $count;
 5664:             }
 5665:             $ordered{$order} = $role;
 5666:             if ($desc ne '') {
 5667:                 $description{$role} = $desc;
 5668:             } else {
 5669:                 $description{$role}= $role;
 5670:             }
 5671:             $count++;
 5672:         }
 5673:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5674:             push(@roles_by_num,$ordered{$item});
 5675:         }
 5676:     }
 5677:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5678: }
 5679: 
 5680: sub get_my_adhocroles {
 5681:     my ($cid,$checkreg) = @_;
 5682:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5683:     if ($env{'request.course.id'} eq $cid) {
 5684:         $cdom = $env{'course.'.$cid.'.domain'};
 5685:         $cnum = $env{'course.'.$cid.'.num'};
 5686:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5687:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5688:         $cdom = $1;
 5689:         $cnum = $2;
 5690:         %info = &get('environment',['internal.coursecode'],
 5691:                      $cdom,$cnum);
 5692:     }
 5693:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5694:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5695:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5696:         if ($rosterhash{$user} ne '') {
 5697:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5698:             return ([],{}) if ($type eq 'auto');
 5699:         }
 5700:     }
 5701:     if (($cdom ne '') && ($cnum ne ''))  {
 5702:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5703:             my $then=$env{'user.login.time'};
 5704:             my $update=$env{'user.update.time'};
 5705:             if (!$update) {
 5706:                 $update = $then;
 5707:             }
 5708:             my @liveroles;
 5709:             foreach my $role ('dh','da') {
 5710:                 if ($env{"user.role.$role./$cdom/"}) {
 5711:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5712:                     my $limit = $update;
 5713:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5714:                         $limit = $then;
 5715:                     }
 5716:                     my $activerole = 1;
 5717:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5718:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5719:                     if ($activerole) {
 5720:                         push(@liveroles,$role);
 5721:                     }
 5722:                 }
 5723:             }
 5724:             if (@liveroles) {
 5725:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5726:                     my ($accessref,$accessinfo,%access_in_dom);
 5727:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5728:                     if (ref($roles_by_num) eq 'ARRAY') {
 5729:                         if (@{$roles_by_num}) {
 5730:                             my %settings;
 5731:                             if ($env{'request.course.id'} eq $cid) {
 5732:                                 foreach my $envkey (keys(%env)) {
 5733:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5734:                                         $settings{$1} = $env{$envkey};
 5735:                                     }
 5736:                                 }
 5737:                             } else {
 5738:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5739:                             }
 5740:                             my %setincrs;
 5741:                             if ($settings{'internal.adhocaccess'}) {
 5742:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5743:                             }
 5744:                             my @statuses;
 5745:                             if ($env{'environment.inststatus'}) {
 5746:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5747:                             }
 5748:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5749:                             if (ref($accessref) eq 'HASH') {
 5750:                                 %access_in_dom = %{$accessref};
 5751:                             }
 5752:                             foreach my $role (@{$roles_by_num}) {
 5753:                                 my ($curraccess,@okstatus,@personnel);
 5754:                                 if ($setincrs{$role}) {
 5755:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5756:                                     if ($curraccess eq 'status') {
 5757:                                         @okstatus = split(/\&/,$rest);
 5758:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5759:                                         @personnel = split(/\&/,$rest);
 5760:                                     }
 5761:                                 } else {
 5762:                                     $curraccess = $access_in_dom{$role};
 5763:                                     if (ref($accessinfo) eq 'HASH') {
 5764:                                         if ($curraccess eq 'status') {
 5765:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5766:                                                 @okstatus = @{$accessinfo->{$role}};
 5767:                                             }
 5768:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5769:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5770:                                                 @personnel = @{$accessinfo->{$role}};
 5771:                                             }
 5772:                                         }
 5773:                                     }
 5774:                                 }
 5775:                                 if ($curraccess eq 'none') {
 5776:                                     next;
 5777:                                 } elsif ($curraccess eq 'all') {
 5778:                                     push(@possroles,$role);
 5779:                                 } elsif ($curraccess eq 'dh') {
 5780:                                     if (grep(/^dh$/,@liveroles)) {
 5781:                                         push(@possroles,$role);
 5782:                                     } else {
 5783:                                         next;
 5784:                                     }
 5785:                                 } elsif ($curraccess eq 'da') {
 5786:                                     if (grep(/^da$/,@liveroles)) {
 5787:                                         push(@possroles,$role);
 5788:                                     } else {
 5789:                                         next;
 5790:                                     }
 5791:                                 } elsif ($curraccess eq 'status') {
 5792:                                     if (@okstatus) {
 5793:                                         if (!@statuses) {
 5794:                                             if (grep(/^default$/,@okstatus)) {
 5795:                                                 push(@possroles,$role);
 5796:                                             }
 5797:                                         } else {
 5798:                                             foreach my $status (@okstatus) {
 5799:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5800:                                                     push(@possroles,$role);
 5801:                                                     last;
 5802:                                                 }
 5803:                                             }
 5804:                                         }
 5805:                                     }
 5806:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5807:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5808:                                         if ($curraccess eq 'exc') {
 5809:                                             push(@possroles,$role);
 5810:                                         }
 5811:                                     } elsif ($curraccess eq 'inc') {
 5812:                                         push(@possroles,$role);
 5813:                                     }
 5814:                                 }
 5815:                             }
 5816:                         }
 5817:                     }
 5818:                 }
 5819:             }
 5820:         }
 5821:     }
 5822:     unless (ref($description) eq 'HASH') {
 5823:         if (ref($roles_by_num) eq 'ARRAY') {
 5824:             my %desc;
 5825:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5826:             $description = \%desc;
 5827:         } else {
 5828:             $description = {};
 5829:         }
 5830:     }
 5831:     return (\@possroles,$description);
 5832: }
 5833: 
 5834: # ----------------------------------------------------- Frontpage Announcements
 5835: #
 5836: #
 5837: 
 5838: sub postannounce {
 5839:     my ($server,$text)=@_;
 5840:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5841:     unless ($text=~/\w/) { $text=''; }
 5842:     return &reply('setannounce:'.&escape($text),$server);
 5843: }
 5844: 
 5845: sub getannounce {
 5846: 
 5847:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5848: 	my $announcement='';
 5849: 	while (my $line = <$fh>) { $announcement .= $line; }
 5850: 	close($fh);
 5851: 	if ($announcement=~/\w/) { 
 5852: 	    return 
 5853:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5854:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5855: 	} else {
 5856: 	    return '';
 5857: 	}
 5858:     } else {
 5859: 	return '';
 5860:     }
 5861: }
 5862: 
 5863: # ---------------------------------------------------------- Course ID routines
 5864: # Deal with domain's nohist_courseid.db files
 5865: #
 5866: 
 5867: sub courseidput {
 5868:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5869:     return unless (ref($storehash) eq 'HASH');
 5870:     my $outcome;
 5871:     if ($caller eq 'timeonly') {
 5872:         my $cids = '';
 5873:         foreach my $item (keys(%$storehash)) {
 5874:             $cids.=&escape($item).'&';
 5875:         }
 5876:         $cids=~s/\&$//;
 5877:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5878:                           $coursehome);       
 5879:     } else {
 5880:         my $items = '';
 5881:         foreach my $item (keys(%$storehash)) {
 5882:             $items.= &escape($item).'='.
 5883:                      &freeze_escape($$storehash{$item}).'&';
 5884:         }
 5885:         $items=~s/\&$//;
 5886:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5887:                           $coursehome);
 5888:     }
 5889:     if ($outcome eq 'unknown_cmd') {
 5890:         my $what;
 5891:         foreach my $cid (keys(%$storehash)) {
 5892:             $what .= &escape($cid).'=';
 5893:             foreach my $item ('description','inst_code','owner','type') {
 5894:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5895:             }
 5896:             $what =~ s/\:$/&/;
 5897:         }
 5898:         $what =~ s/\&$//;  
 5899:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5900:     } else {
 5901:         return $outcome;
 5902:     }
 5903: }
 5904: 
 5905: sub courseiddump {
 5906:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5907:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5908:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5909:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5910:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5911:     my $as_hash = 1;
 5912:     my %returnhash;
 5913:     if (!$domfilter) { $domfilter=''; }
 5914:     my %libserv = &all_library();
 5915:     foreach my $tryserver (keys(%libserv)) {
 5916:         if ( (  $hostidflag == 1 
 5917: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5918: 	     || (!defined($hostidflag)) ) {
 5919: 
 5920: 	    if (($domfilter eq '') ||
 5921: 		(&host_domain($tryserver) eq $domfilter)) {
 5922:                 my $rep;
 5923:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5924:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5925:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5926:                                 &escape($descfilter), &escape($instcodefilter), 
 5927:                                 &escape($ownerfilter), &escape($coursefilter),
 5928:                                 &escape($typefilter), &escape($regexp_ok), 
 5929:                                 $as_hash, &escape($selfenrollonly), 
 5930:                                 &escape($catfilter), $showhidden, $caller, 
 5931:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5932:                                 &escape($createdbefore), &escape($createdafter), 
 5933:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5934:                                 $reqcrsdom,&escape($reqinstcode))));
 5935:                 } else {
 5936:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5937:                              $sincefilter.':'.&escape($descfilter).':'.
 5938:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5939:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5940:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5941:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5942:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5943:                              &escape($cc_clone).':'.$cloneonly.':'.
 5944:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5945:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5946:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5947:                 }
 5948:                      
 5949:                 my @pairs=split(/\&/,$rep);
 5950:                 foreach my $item (@pairs) {
 5951:                     my ($key,$value)=split(/\=/,$item,2);
 5952:                     $key = &unescape($key);
 5953:                     next if ($key =~ /^error: 2 /);
 5954:                     my $result = &thaw_unescape($value);
 5955:                     if (ref($result) eq 'HASH') {
 5956:                         $returnhash{$key}=$result;
 5957:                     } else {
 5958:                         my @responses = split(/:/,$value);
 5959:                         my @items = ('description','inst_code','owner','type');
 5960:                         for (my $i=0; $i<@responses; $i++) {
 5961:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5962:                         }
 5963:                     }
 5964:                 }
 5965:             }
 5966:         }
 5967:     }
 5968:     return %returnhash;
 5969: }
 5970: 
 5971: sub courselastaccess {
 5972:     my ($cdom,$cnum,$hostidref) = @_;
 5973:     my %returnhash;
 5974:     if ($cdom && $cnum) {
 5975:         my $chome = &homeserver($cnum,$cdom);
 5976:         if ($chome ne 'no_host') {
 5977:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5978:             &extract_lastaccess(\%returnhash,$rep);
 5979:         }
 5980:     } else {
 5981:         if (!$cdom) { $cdom=''; }
 5982:         my %libserv = &all_library();
 5983:         foreach my $tryserver (keys(%libserv)) {
 5984:             if (ref($hostidref) eq 'ARRAY') {
 5985:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5986:             } 
 5987:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5988:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5989:                 &extract_lastaccess(\%returnhash,$rep);
 5990:             }
 5991:         }
 5992:     }
 5993:     return %returnhash;
 5994: }
 5995: 
 5996: sub extract_lastaccess {
 5997:     my ($returnhash,$rep) = @_;
 5998:     if (ref($returnhash) eq 'HASH') {
 5999:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 6000:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 6001:                  $rep eq '') {
 6002:             my @pairs=split(/\&/,$rep);
 6003:             foreach my $item (@pairs) {
 6004:                 my ($key,$value)=split(/\=/,$item,2);
 6005:                 $key = &unescape($key);
 6006:                 next if ($key =~ /^error: 2 /);
 6007:                 $returnhash->{$key} = &thaw_unescape($value);
 6008:             }
 6009:         }
 6010:     }
 6011:     return;
 6012: }
 6013: 
 6014: # ---------------------------------------------------------- DC e-mail
 6015: 
 6016: sub dcmailput {
 6017:     my ($domain,$msgid,$message,$server)=@_;
 6018:     my $status = &critical(
 6019:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 6020:        &escape($message),$server);
 6021:     return $status;
 6022: }
 6023: 
 6024: sub dcmaildump {
 6025:     my ($dom,$startdate,$enddate,$senders) = @_;
 6026:     my %returnhash=();
 6027: 
 6028:     if (defined(&domain($dom,'primary'))) {
 6029:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 6030:                                                          &escape($enddate).':';
 6031: 	my @esc_senders=map { &escape($_)} @$senders;
 6032: 	$cmd.=&escape(join('&',@esc_senders));
 6033: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 6034:             my ($key,$value) = split(/\=/,$line,2);
 6035:             if (($key) && ($value)) {
 6036:                 $returnhash{&unescape($key)} = &unescape($value);
 6037:             }
 6038:         }
 6039:     }
 6040:     return %returnhash;
 6041: }
 6042: # ---------------------------------------------------------- Domain roles
 6043: 
 6044: sub get_domain_roles {
 6045:     my ($dom,$roles,$startdate,$enddate)=@_;
 6046:     if ((!defined($startdate)) || ($startdate eq '')) {
 6047:         $startdate = '.';
 6048:     }
 6049:     if ((!defined($enddate)) || ($enddate eq '')) {
 6050:         $enddate = '.';
 6051:     }
 6052:     my $rolelist;
 6053:     if (ref($roles) eq 'ARRAY') {
 6054:         $rolelist = join('&',@{$roles});
 6055:     }
 6056:     my %personnel = ();
 6057: 
 6058:     my %servers = &get_servers($dom,'library');
 6059:     foreach my $tryserver (keys(%servers)) {
 6060: 	%{$personnel{$tryserver}}=();
 6061: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 6062: 					    &escape($startdate).':'.
 6063: 					    &escape($enddate).':'.
 6064: 					    &escape($rolelist), $tryserver))) {
 6065: 	    my ($key,$value) = split(/\=/,$line,2);
 6066: 	    if (($key) && ($value)) {
 6067: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 6068: 	    }
 6069: 	}
 6070:     }
 6071:     return %personnel;
 6072: }
 6073: 
 6074: sub get_active_domroles {
 6075:     my ($dom,$roles) = @_;
 6076:     return () unless (ref($roles) eq 'ARRAY');
 6077:     my $now = time;
 6078:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 6079:     my %domroles;
 6080:     foreach my $server (keys(%dompersonnel)) {
 6081:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 6082:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 6083:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 6084:         }
 6085:     }
 6086:     return %domroles;
 6087: }
 6088: 
 6089: # ----------------------------------------------------------- Interval timing 
 6090: 
 6091: {
 6092: # Caches needed for speedup of navmaps
 6093: # We don't want to cache this for very long at all (5 seconds at most)
 6094: # 
 6095: # The user for whom we cache
 6096: my $cachedkey='';
 6097: # The cached times for this user
 6098: my %cachedtimes=();
 6099: # When this was last done
 6100: my $cachedtime='';
 6101: 
 6102: sub load_all_first_access {
 6103:     my ($uname,$udom,$ignorecache)=@_;
 6104:     if (($cachedkey eq $uname.':'.$udom) &&
 6105:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 6106:         (!$ignorecache)) {
 6107:         return;
 6108:     }
 6109:     $cachedtime=time;
 6110:     $cachedkey=$uname.':'.$udom;
 6111:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 6112: }
 6113: 
 6114: sub get_first_access {
 6115:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 6116:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6117:     if ($argsymb) { $symb=$argsymb; }
 6118:     my ($map,$id,$res)=&decode_symb($symb);
 6119:     if ($argmap) { $map = $argmap; }
 6120:     if ($type eq 'course') {
 6121: 	$res='course';
 6122:     } elsif ($type eq 'map') {
 6123: 	$res=&symbread($map);
 6124:     } else {
 6125: 	$res=$symb;
 6126:     }
 6127:     &load_all_first_access($uname,$udom,$ignorecache);
 6128:     return $cachedtimes{"$courseid\0$res"};
 6129: }
 6130: 
 6131: sub set_first_access {
 6132:     my ($type,$interval)=@_;
 6133:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 6134:     my ($map,$id,$res)=&decode_symb($symb);
 6135:     if ($type eq 'course') {
 6136: 	$res='course';
 6137:     } elsif ($type eq 'map') {
 6138: 	$res=&symbread($map);
 6139:     } else {
 6140: 	$res=$symb;
 6141:     }
 6142:     $cachedkey='';
 6143:     my $firstaccess=&get_first_access($type,$symb,$map);
 6144:     if ($firstaccess) {
 6145:         &logthis("First access time already set ($firstaccess) when attempting ".
 6146:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 6147:                  "in $courseid");
 6148:         return 'already_set';
 6149:     } else {
 6150:         my $start = time;
 6151: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 6152:                           $udom,$uname);
 6153:         if ($putres eq 'ok') {
 6154:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 6155:                  $udom,$uname); 
 6156:             &appenv(
 6157:                      {
 6158:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 6159:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 6160:                      }
 6161:                   );
 6162:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 6163:                 $cachedtimes{"$courseid\0$res"} = $start;
 6164:             }
 6165:         } elsif ($putres ne 'refused') {
 6166:             &logthis("Result: $putres when attempting to set first access time ".
 6167:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 6168:         }
 6169:         return $putres;
 6170:     }
 6171:     return 'already_set';
 6172: }
 6173: }
 6174: 
 6175: # --------------------------------------------- Set Expire Date for Spreadsheet
 6176: 
 6177: sub expirespread {
 6178:     my ($uname,$udom,$stype,$usymb)=@_;
 6179:     my $cid=$env{'request.course.id'}; 
 6180:     if ($cid) {
 6181:        my $now=time;
 6182:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 6183:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 6184:                             $env{'course.'.$cid.'.num'}.
 6185: 	        	    ':nohist_expirationdates:'.
 6186:                             &escape($key).'='.$now,
 6187:                             $env{'course.'.$cid.'.home'})
 6188:     }
 6189:     return 'ok';
 6190: }
 6191: 
 6192: # ----------------------------------------------------- Devalidate Spreadsheets
 6193: 
 6194: sub devalidate {
 6195:     my ($symb,$uname,$udom)=@_;
 6196:     my $cid=$env{'request.course.id'}; 
 6197:     if ($cid) {
 6198:         # delete the stored spreadsheets for
 6199:         # - the student level sheet of this user in course's homespace
 6200:         # - the assessment level sheet for this resource 
 6201:         #   for this user in user's homespace
 6202: 	# - current conditional state info
 6203: 	my $key=$uname.':'.$udom.':';
 6204:         my $status=
 6205: 	    &del('nohist_calculatedsheets',
 6206: 		 [$key.'studentcalc:'],
 6207: 		 $env{'course.'.$cid.'.domain'},
 6208: 		 $env{'course.'.$cid.'.num'})
 6209: 		.' '.
 6210: 	    &del('nohist_calculatedsheets_'.$cid,
 6211: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 6212:         unless ($status eq 'ok ok') {
 6213:            &logthis('Could not devalidate spreadsheet '.
 6214:                     $uname.' at '.$udom.' for '.
 6215: 		    $symb.': '.$status);
 6216:         }
 6217: 	&delenv('user.state.'.$cid);
 6218:     }
 6219: }
 6220: 
 6221: sub get_scalar {
 6222:     my ($string,$end) = @_;
 6223:     my $value;
 6224:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 6225: 	$value = $1;
 6226:     } elsif ($$string =~ s/^([^&]*?)&//) {
 6227: 	$value = $1;
 6228:     }
 6229:     return &unescape($value);
 6230: }
 6231: 
 6232: sub array2str {
 6233:   my (@array) = @_;
 6234:   my $result=&arrayref2str(\@array);
 6235:   $result=~s/^__ARRAY_REF__//;
 6236:   $result=~s/__END_ARRAY_REF__$//;
 6237:   return $result;
 6238: }
 6239: 
 6240: sub arrayref2str {
 6241:   my ($arrayref) = @_;
 6242:   my $result='__ARRAY_REF__';
 6243:   foreach my $elem (@$arrayref) {
 6244:     if(ref($elem) eq 'ARRAY') {
 6245:       $result.=&arrayref2str($elem).'&';
 6246:     } elsif(ref($elem) eq 'HASH') {
 6247:       $result.=&hashref2str($elem).'&';
 6248:     } elsif(ref($elem)) {
 6249:       #print("Got a ref of ".(ref($elem))." skipping.");
 6250:     } else {
 6251:       $result.=&escape($elem).'&';
 6252:     }
 6253:   }
 6254:   $result=~s/\&$//;
 6255:   $result .= '__END_ARRAY_REF__';
 6256:   return $result;
 6257: }
 6258: 
 6259: sub hash2str {
 6260:   my (%hash) = @_;
 6261:   my $result=&hashref2str(\%hash);
 6262:   $result=~s/^__HASH_REF__//;
 6263:   $result=~s/__END_HASH_REF__$//;
 6264:   return $result;
 6265: }
 6266: 
 6267: sub hashref2str {
 6268:   my ($hashref)=@_;
 6269:   my $result='__HASH_REF__';
 6270:   foreach my $key (sort(keys(%$hashref))) {
 6271:     if (ref($key) eq 'ARRAY') {
 6272:       $result.=&arrayref2str($key).'=';
 6273:     } elsif (ref($key) eq 'HASH') {
 6274:       $result.=&hashref2str($key).'=';
 6275:     } elsif (ref($key)) {
 6276:       $result.='=';
 6277:       #print("Got a ref of ".(ref($key))." skipping.");
 6278:     } else {
 6279: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 6280:     }
 6281: 
 6282:     if(ref($hashref->{$key}) eq 'ARRAY') {
 6283:       $result.=&arrayref2str($hashref->{$key}).'&';
 6284:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 6285:       $result.=&hashref2str($hashref->{$key}).'&';
 6286:     } elsif(ref($hashref->{$key})) {
 6287:        $result.='&';
 6288:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 6289:     } else {
 6290:       $result.=&escape($hashref->{$key}).'&';
 6291:     }
 6292:   }
 6293:   $result=~s/\&$//;
 6294:   $result .= '__END_HASH_REF__';
 6295:   return $result;
 6296: }
 6297: 
 6298: sub str2hash {
 6299:     my ($string)=@_;
 6300:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 6301:     return %$hash;
 6302: }
 6303: 
 6304: sub str2hashref {
 6305:   my ($string) = @_;
 6306: 
 6307:   my %hash;
 6308: 
 6309:   if($string !~ /^__HASH_REF__/) {
 6310:       if (! ($string eq '' || !defined($string))) {
 6311: 	  $hash{'error'}='Not hash reference';
 6312:       }
 6313:       return (\%hash, $string);
 6314:   }
 6315: 
 6316:   $string =~ s/^__HASH_REF__//;
 6317: 
 6318:   while($string !~ /^__END_HASH_REF__/) {
 6319:       #key
 6320:       my $key='';
 6321:       if($string =~ /^__HASH_REF__/) {
 6322:           ($key, $string)=&str2hashref($string);
 6323:           if(defined($key->{'error'})) {
 6324:               $hash{'error'}='Bad data';
 6325:               return (\%hash, $string);
 6326:           }
 6327:       } elsif($string =~ /^__ARRAY_REF__/) {
 6328:           ($key, $string)=&str2arrayref($string);
 6329:           if($key->[0] eq 'Array reference error') {
 6330:               $hash{'error'}='Bad data';
 6331:               return (\%hash, $string);
 6332:           }
 6333:       } else {
 6334:           $string =~ s/^(.*?)=//;
 6335: 	  $key=&unescape($1);
 6336:       }
 6337:       $string =~ s/^=//;
 6338: 
 6339:       #value
 6340:       my $value='';
 6341:       if($string =~ /^__HASH_REF__/) {
 6342:           ($value, $string)=&str2hashref($string);
 6343:           if(defined($value->{'error'})) {
 6344:               $hash{'error'}='Bad data';
 6345:               return (\%hash, $string);
 6346:           }
 6347:       } elsif($string =~ /^__ARRAY_REF__/) {
 6348:           ($value, $string)=&str2arrayref($string);
 6349:           if($value->[0] eq 'Array reference error') {
 6350:               $hash{'error'}='Bad data';
 6351:               return (\%hash, $string);
 6352:           }
 6353:       } else {
 6354: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6355:       }
 6356:       $string =~ s/^&//;
 6357: 
 6358:       $hash{$key}=$value;
 6359:   }
 6360: 
 6361:   $string =~ s/^__END_HASH_REF__//;
 6362: 
 6363:   return (\%hash, $string);
 6364: }
 6365: 
 6366: sub str2array {
 6367:     my ($string)=@_;
 6368:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6369:     return @$array;
 6370: }
 6371: 
 6372: sub str2arrayref {
 6373:   my ($string) = @_;
 6374:   my @array;
 6375: 
 6376:   if($string !~ /^__ARRAY_REF__/) {
 6377:       if (! ($string eq '' || !defined($string))) {
 6378: 	  $array[0]='Array reference error';
 6379:       }
 6380:       return (\@array, $string);
 6381:   }
 6382: 
 6383:   $string =~ s/^__ARRAY_REF__//;
 6384: 
 6385:   while($string !~ /^__END_ARRAY_REF__/) {
 6386:       my $value='';
 6387:       if($string =~ /^__HASH_REF__/) {
 6388:           ($value, $string)=&str2hashref($string);
 6389:           if(defined($value->{'error'})) {
 6390:               $array[0] ='Array reference error';
 6391:               return (\@array, $string);
 6392:           }
 6393:       } elsif($string =~ /^__ARRAY_REF__/) {
 6394:           ($value, $string)=&str2arrayref($string);
 6395:           if($value->[0] eq 'Array reference error') {
 6396:               $array[0] ='Array reference error';
 6397:               return (\@array, $string);
 6398:           }
 6399:       } else {
 6400: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6401:       }
 6402:       $string =~ s/^&//;
 6403: 
 6404:       push(@array, $value);
 6405:   }
 6406: 
 6407:   $string =~ s/^__END_ARRAY_REF__//;
 6408: 
 6409:   return (\@array, $string);
 6410: }
 6411: 
 6412: # -------------------------------------------------------------------Temp Store
 6413: 
 6414: sub tmpreset {
 6415:   my ($symb,$namespace,$domain,$stuname) = @_;
 6416:   if (!$symb) {
 6417:     $symb=&symbread();
 6418:     if (!$symb) { $symb= $env{'request.url'}; }
 6419:   }
 6420:   $symb=escape($symb);
 6421: 
 6422:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6423:   $namespace=~s/\//\_/g;
 6424:   $namespace=~s/\W//g;
 6425: 
 6426:   if (!$domain) { $domain=$env{'user.domain'}; }
 6427:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6428:   if ($domain eq 'public' && $stuname eq 'public') {
 6429:       $stuname=&get_requestor_ip();
 6430:   }
 6431:   my $path=LONCAPA::tempdir();
 6432:   my %hash;
 6433:   if (tie(%hash,'GDBM_File',
 6434: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6435: 	  &GDBM_WRCREAT(),0640)) {
 6436:     foreach my $key (keys(%hash)) {
 6437:       if ($key=~ /:$symb/) {
 6438: 	delete($hash{$key});
 6439:       }
 6440:     }
 6441:   }
 6442: }
 6443: 
 6444: sub tmpstore {
 6445:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6446: 
 6447:   if (!$symb) {
 6448:     $symb=&symbread();
 6449:     if (!$symb) { $symb= $env{'request.url'}; }
 6450:   }
 6451:   $symb=escape($symb);
 6452: 
 6453:   if (!$namespace) {
 6454:     # I don't think we would ever want to store this for a course.
 6455:     # it seems this will only be used if we don't have a course.
 6456:     #$namespace=$env{'request.course.id'};
 6457:     #if (!$namespace) {
 6458:       $namespace=$env{'request.state'};
 6459:     #}
 6460:   }
 6461:   $namespace=~s/\//\_/g;
 6462:   $namespace=~s/\W//g;
 6463:   if (!$domain) { $domain=$env{'user.domain'}; }
 6464:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6465:   if ($domain eq 'public' && $stuname eq 'public') {
 6466:       $stuname=&get_requestor_ip();
 6467:   }
 6468:   my $now=time;
 6469:   my %hash;
 6470:   my $path=LONCAPA::tempdir();
 6471:   if (tie(%hash,'GDBM_File',
 6472: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6473: 	  &GDBM_WRCREAT(),0640)) {
 6474:     $hash{"version:$symb"}++;
 6475:     my $version=$hash{"version:$symb"};
 6476:     my $allkeys=''; 
 6477:     foreach my $key (keys(%$storehash)) {
 6478:       $allkeys.=$key.':';
 6479:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6480:     }
 6481:     $hash{"$version:$symb:timestamp"}=$now;
 6482:     $allkeys.='timestamp';
 6483:     $hash{"$version:keys:$symb"}=$allkeys;
 6484:     if (untie(%hash)) {
 6485:       return 'ok';
 6486:     } else {
 6487:       return "error:$!";
 6488:     }
 6489:   } else {
 6490:     return "error:$!";
 6491:   }
 6492: }
 6493: 
 6494: # -----------------------------------------------------------------Temp Restore
 6495: 
 6496: sub tmprestore {
 6497:   my ($symb,$namespace,$domain,$stuname) = @_;
 6498: 
 6499:   if (!$symb) {
 6500:     $symb=&symbread();
 6501:     if (!$symb) { $symb= $env{'request.url'}; }
 6502:   }
 6503:   $symb=escape($symb);
 6504: 
 6505:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6506: 
 6507:   if (!$domain) { $domain=$env{'user.domain'}; }
 6508:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6509:   if ($domain eq 'public' && $stuname eq 'public') {
 6510:       $stuname=&get_requestor_ip();
 6511:   }
 6512:   my %returnhash;
 6513:   $namespace=~s/\//\_/g;
 6514:   $namespace=~s/\W//g;
 6515:   my %hash;
 6516:   my $path=LONCAPA::tempdir();
 6517:   if (tie(%hash,'GDBM_File',
 6518: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6519: 	  &GDBM_READER(),0640)) {
 6520:     my $version=$hash{"version:$symb"};
 6521:     $returnhash{'version'}=$version;
 6522:     my $scope;
 6523:     for ($scope=1;$scope<=$version;$scope++) {
 6524:       my $vkeys=$hash{"$scope:keys:$symb"};
 6525:       my @keys=split(/:/,$vkeys);
 6526:       my $key;
 6527:       $returnhash{"$scope:keys"}=$vkeys;
 6528:       foreach $key (@keys) {
 6529: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6530: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6531:       }
 6532:     }
 6533:     if (!(untie(%hash))) {
 6534:       return "error:$!";
 6535:     }
 6536:   } else {
 6537:     return "error:$!";
 6538:   }
 6539:   return %returnhash;
 6540: }
 6541: 
 6542: # ----------------------------------------------------------------------- Store
 6543: 
 6544: sub store {
 6545:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6546:     my $home='';
 6547: 
 6548:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6549: 
 6550:     $symb=&symbclean($symb);
 6551:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6552: 
 6553:     if (!$domain) { $domain=$env{'user.domain'}; }
 6554:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6555: 
 6556:     &devalidate($symb,$stuname,$domain);
 6557: 
 6558:     $symb=escape($symb);
 6559:     if (!$namespace) { 
 6560:        unless ($namespace=$env{'request.course.id'}) { 
 6561:           return ''; 
 6562:        } 
 6563:     }
 6564:     if (!$home) { $home=$env{'user.home'}; }
 6565: 
 6566:     $$storehash{'ip'}=&get_requestor_ip();
 6567:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6568: 
 6569:     my $namevalue='';
 6570:     foreach my $key (keys(%$storehash)) {
 6571:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6572:     }
 6573:     $namevalue=~s/\&$//;
 6574:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6575:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6576: }
 6577: 
 6578: # -------------------------------------------------------------- Critical Store
 6579: 
 6580: sub cstore {
 6581:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6582:     my $home='';
 6583: 
 6584:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6585: 
 6586:     $symb=&symbclean($symb);
 6587:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6588: 
 6589:     if (!$domain) { $domain=$env{'user.domain'}; }
 6590:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6591: 
 6592:     &devalidate($symb,$stuname,$domain);
 6593: 
 6594:     $symb=escape($symb);
 6595:     if (!$namespace) { 
 6596:        unless ($namespace=$env{'request.course.id'}) { 
 6597:           return ''; 
 6598:        } 
 6599:     }
 6600:     if (!$home) { $home=$env{'user.home'}; }
 6601: 
 6602:     $$storehash{'ip'}=&get_requestor_ip();
 6603:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6604: 
 6605:     my $namevalue='';
 6606:     foreach my $key (keys(%$storehash)) {
 6607:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6608:     }
 6609:     $namevalue=~s/\&$//;
 6610:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6611:     return critical
 6612:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6613: }
 6614: 
 6615: # --------------------------------------------------------------------- Restore
 6616: 
 6617: sub restore {
 6618:     my ($symb,$namespace,$domain,$stuname) = @_;
 6619:     my $home='';
 6620: 
 6621:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6622: 
 6623:     if (!$symb) {
 6624:         return if ($namespace eq 'courserequests');
 6625:         unless ($symb=escape(&symbread())) { return ''; }
 6626:     } else {
 6627:         unless ($namespace eq 'courserequests') {
 6628:             $symb=&escape(&symbclean($symb));
 6629:         }
 6630:     }
 6631:     if (!$namespace) { 
 6632:        unless ($namespace=$env{'request.course.id'}) { 
 6633:           return ''; 
 6634:        } 
 6635:     }
 6636:     if (!$domain) { $domain=$env{'user.domain'}; }
 6637:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6638:     if (!$home) { $home=$env{'user.home'}; }
 6639:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6640: 
 6641:     my %returnhash=();
 6642:     foreach my $line (split(/\&/,$answer)) {
 6643: 	my ($name,$value)=split(/\=/,$line);
 6644:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6645:     }
 6646:     my $version;
 6647:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6648:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6649:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6650:        }
 6651:     }
 6652:     return %returnhash;
 6653: }
 6654: 
 6655: # ---------------------------------------------------------- Course Description
 6656: #
 6657: #  
 6658: 
 6659: sub coursedescription {
 6660:     my ($courseid,$args)=@_;
 6661:     $courseid=~s/^\///;
 6662:     $courseid=~s/\_/\//g;
 6663:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6664:     my $chome=&homeserver($cnum,$cdomain);
 6665:     my $normalid=$cdomain.'_'.$cnum;
 6666:     # need to always cache even if we get errors otherwise we keep 
 6667:     # trying and trying and trying to get the course description.
 6668:     my %envhash=();
 6669:     my %returnhash=();
 6670:     
 6671:     my $expiretime=600;
 6672:     if ($env{'request.course.id'} eq $normalid) {
 6673: 	$expiretime=120;
 6674:     }
 6675: 
 6676:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6677:     if (!$args->{'freshen_cache'}
 6678: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6679: 	foreach my $key (keys(%env)) {
 6680: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6681: 	    my ($setting) = $1;
 6682: 	    $returnhash{$setting} = $env{$key};
 6683: 	}
 6684: 	return %returnhash;
 6685:     }
 6686: 
 6687:     # get the data again
 6688: 
 6689:     if (!$args->{'one_time'}) {
 6690: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6691:     }
 6692: 
 6693:     if ($chome ne 'no_host') {
 6694:        %returnhash=&dump('environment',$cdomain,$cnum);
 6695:        if (!exists($returnhash{'con_lost'})) {
 6696: 	   my $username = $env{'user.name'}; # Defult username
 6697: 	   if(defined $args->{'user'}) {
 6698: 	       $username = $args->{'user'};
 6699: 	   }
 6700:            $returnhash{'home'}= $chome;
 6701: 	   $returnhash{'domain'} = $cdomain;
 6702: 	   $returnhash{'num'} = $cnum;
 6703:            if (!defined($returnhash{'type'})) {
 6704:                $returnhash{'type'} = 'Course';
 6705:            }
 6706:            while (my ($name,$value) = each %returnhash) {
 6707:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6708:            }
 6709:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6710:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6711: 	       $username.'_'.$cdomain.'_'.$cnum;
 6712:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6713:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6714:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6715:        }
 6716:     }
 6717:     if (!$args->{'one_time'}) {
 6718: 	&appenv(\%envhash);
 6719:     }
 6720:     return %returnhash;
 6721: }
 6722: 
 6723: sub update_released_required {
 6724:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6725:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6726:         $cid = $env{'request.course.id'};
 6727:         $cdom = $env{'course.'.$cid.'.domain'};
 6728:         $cnum = $env{'course.'.$cid.'.num'};
 6729:         $chome = $env{'course.'.$cid.'.home'};
 6730:     }
 6731:     if ($needsrelease) {
 6732:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6733:         my $needsupdate;
 6734:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6735:             $needsupdate = 1;
 6736:         } else {
 6737:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6738:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6739:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6740:                 $needsupdate = 1;
 6741:             }
 6742:         }
 6743:         if ($needsupdate) {
 6744:             my %needshash = (
 6745:                              'internal.releaserequired' => $needsrelease,
 6746:                             );
 6747:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6748:             if ($putresult eq 'ok') {
 6749:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6750:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6751:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6752:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6753:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6754:                 }
 6755:             }
 6756:         }
 6757:     }
 6758:     return;
 6759: }
 6760: 
 6761: # -------------------------------------------------See if a user is privileged
 6762: 
 6763: sub privileged {
 6764:     my ($username,$domain,$possdomains,$possroles)=@_;
 6765:     my $now = time;
 6766:     my $roles;
 6767:     if (ref($possroles) eq 'ARRAY') {
 6768:         $roles = $possroles; 
 6769:     } else {
 6770:         $roles = ['dc','su'];
 6771:     }
 6772:     if (ref($possdomains) eq 'ARRAY') {
 6773:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6774:         foreach my $dom (@{$possdomains}) {
 6775:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6776:                 (ref($privileged{$dom}) eq 'HASH')) {
 6777:                 foreach my $role (@{$roles}) {
 6778:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6779:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6780:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6781:                             return 1 unless (($end && $end < $now) ||
 6782:                                              ($start && $start > $now));
 6783:                         }
 6784:                     }
 6785:                 }
 6786:             }
 6787:         }
 6788:     } else {
 6789:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6790:         my $now = time;
 6791: 
 6792:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6793:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6794:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6795:                 return 1 unless ($tend && $tend < $now) 
 6796:                         or ($tstart && $tstart > $now);
 6797:             }
 6798:         }
 6799:     }
 6800:     return 0;
 6801: }
 6802: 
 6803: sub privileged_by_domain {
 6804:     my ($domains,$roles) = @_;
 6805:     my %privileged = ();
 6806:     my $cachetime = 60*60*24;
 6807:     my $now = time;
 6808:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6809:         return %privileged;
 6810:     }
 6811:     foreach my $dom (@{$domains}) {
 6812:         next if (ref($privileged{$dom}) eq 'HASH');
 6813:         my $needroles;
 6814:         foreach my $role (@{$roles}) {
 6815:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6816:             if (defined($cached)) {
 6817:                 if (ref($result) eq 'HASH') {
 6818:                     $privileged{$dom}{$role} = $result;
 6819:                 }
 6820:             } else {
 6821:                 $needroles = 1;
 6822:             }
 6823:         }
 6824:         if ($needroles) {
 6825:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6826:             $privileged{$dom} = {};
 6827:             foreach my $server (keys(%dompersonnel)) {
 6828:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6829:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6830:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6831:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6832:                         next if ($end && $end < $now);
 6833:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6834:                             $dompersonnel{$server}{$item};
 6835:                     }
 6836:                 }
 6837:             }
 6838:             if (ref($privileged{$dom}) eq 'HASH') {
 6839:                 foreach my $role (@{$roles}) {
 6840:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6841:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6842:                     } else {
 6843:                         my %hash = ();
 6844:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6845:                     }
 6846:                 }
 6847:             }
 6848:         }
 6849:     }
 6850:     return %privileged;
 6851: }
 6852: 
 6853: # -------------------------------------------------------- Get user privileges
 6854: 
 6855: sub rolesinit {
 6856:     my ($domain, $username) = @_;
 6857:     my %userroles = ('user.login.time' => time);
 6858:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6859: 
 6860:     # firstaccess and timerinterval are related to timed maps/resources. 
 6861:     # also, blocking can be triggered by an activating timer
 6862:     # it's saved in the user's %env.
 6863:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6864:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6865:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6866:         %timerintchk, %timerintenv);
 6867: 
 6868:     foreach my $key (keys(%firstaccess)) {
 6869:         my ($cid, $rest) = split(/\0/, $key);
 6870:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6871:     }
 6872: 
 6873:     foreach my $key (keys(%timerinterval)) {
 6874:         my ($cid,$rest) = split(/\0/,$key);
 6875:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6876:     }
 6877: 
 6878:     my %allroles=();
 6879:     my %allgroups=();
 6880: 
 6881:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6882:         my $role = $rolesdump{$area};
 6883:         $area =~ s/\_\w\w$//;
 6884: 
 6885:         my ($trole, $tend, $tstart, $group_privs);
 6886: 
 6887:         if ($role =~ /^cr/) {
 6888:         # Custom role, defined by a user 
 6889:         # e.g., user.role.cr/msu/smith/mynewrole
 6890:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6891:                 $trole = $1;
 6892:                 ($tend, $tstart) = split('_', $2);
 6893:             } else {
 6894:                 $trole = $role;
 6895:             }
 6896:         } elsif ($role =~ m|^gr/|) {
 6897:         # Role of member in a group, defined within a course/community
 6898:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6899:             ($trole, $tend, $tstart) = split(/_/, $role);
 6900:             next if $tstart eq '-1';
 6901:             ($trole, $group_privs) = split(/\//, $trole);
 6902:             $group_privs = &unescape($group_privs);
 6903:         } else {
 6904:         # Just a normal role, defined in roles.tab
 6905:             ($trole, $tend, $tstart) = split(/_/,$role);
 6906:         }
 6907: 
 6908:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6909:                  $username);
 6910:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6911: 
 6912:         # role expired or not available yet?
 6913:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6914:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6915: 
 6916:         next if $area eq '' or $trole eq '';
 6917: 
 6918:         my $spec = "$trole.$area";
 6919:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6920: 
 6921:         if ($trole =~ /^cr\//) {
 6922:         # Custom role, defined by a user
 6923:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6924:         } elsif ($trole eq 'gr') {
 6925:         # Role of a member in a group, defined within a course/community
 6926:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6927:             next;
 6928:         } else {
 6929:         # Normal role, defined in roles.tab
 6930:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6931:         }
 6932: 
 6933:         my $cid = $tdomain.'_'.$trest;
 6934:         unless ($firstaccchk{$cid}) {
 6935:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6936:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6937:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6938:                         $coursetimerstarts{$cid}{$item}; 
 6939:                 }
 6940:             }
 6941:             $firstaccchk{$cid} = 1;
 6942:         }
 6943:         unless ($timerintchk{$cid}) {
 6944:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6945:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6946:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6947:                        $coursetimerintervals{$cid}{$item};
 6948:                 }
 6949:             }
 6950:             $timerintchk{$cid} = 1;
 6951:         }
 6952:     }
 6953: 
 6954:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6955:                                                           \%allroles, \%allgroups);
 6956:     $env{'user.adv'} = $userroles{'user.adv'};
 6957:     $env{'user.rar'} = $userroles{'user.rar'};
 6958: 
 6959:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6960: }
 6961: 
 6962: sub set_arearole {
 6963:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6964:     unless ($nolog) {
 6965: # log the associated role with the area
 6966:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6967:     }
 6968:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6969: }
 6970: 
 6971: sub custom_roleprivs {
 6972:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6973:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6974:     my $homsvr = &homeserver($rauthor,$rdomain);
 6975:     if (&hostname($homsvr) ne '') {
 6976:         my ($rdummy,$roledef)=
 6977:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6978:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6979:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6980:             if (defined($syspriv)) {
 6981:                 if ($trest =~ /^$match_community$/) {
 6982:                     $syspriv =~ s/bre\&S//; 
 6983:                 }
 6984:                 $$allroles{'cm./'}.=':'.$syspriv;
 6985:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6986:             }
 6987:             if ($tdomain ne '') {
 6988:                 if (defined($dompriv)) {
 6989:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6990:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6991:                 }
 6992:                 if (($trest ne '') && (defined($coursepriv))) {
 6993:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6994:                         my $rolename = $1;
 6995:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6996:                     }
 6997:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6998:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6999:                 }
 7000:             }
 7001:         }
 7002:     }
 7003: }
 7004: 
 7005: sub course_adhocrole_privs {
 7006:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 7007:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 7008:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 7009:         my (%currprivs,%storeprivs);
 7010:         foreach my $item (split(/:/,$coursepriv)) {
 7011:             my ($priv,$restrict) = split(/\&/,$item);
 7012:             $currprivs{$priv} = $restrict;
 7013:         }
 7014:         my (%possadd,%possremove,%full);
 7015:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 7016:             my ($priv,$restrict)=split(/\&/,$item);
 7017:             $full{$priv} = $restrict;
 7018:         }
 7019:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 7020:             next if ($item eq '');
 7021:             my ($rule,$rest) = split(/=/,$item);
 7022:             next unless (($rule eq 'off') || ($rule eq 'on'));
 7023:             foreach my $priv (split(/:/,$rest)) {
 7024:                 if ($priv ne '') {
 7025:                     if ($rule eq 'off') {
 7026:                         $possremove{$priv} = 1;
 7027:                     } else {
 7028:                         $possadd{$priv} = 1;
 7029:                     }
 7030:                 }
 7031:             }
 7032:         }
 7033:         foreach my $priv (sort(keys(%full))) {
 7034:             if (exists($currprivs{$priv})) {
 7035:                 unless (exists($possremove{$priv})) {
 7036:                     $storeprivs{$priv} = $currprivs{$priv};
 7037:                 }
 7038:             } elsif (exists($possadd{$priv})) {
 7039:                 $storeprivs{$priv} = $full{$priv};
 7040:             }
 7041:         }
 7042:         $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 7043:     }
 7044:     return $coursepriv;
 7045: }
 7046: 
 7047: sub group_roleprivs {
 7048:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 7049:     my $access = 1;
 7050:     my $now = time;
 7051:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 7052:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 7053:     if ($access) {
 7054:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 7055:         $$allgroups{$course}{$group} .=':'.$group_privs;
 7056:     }
 7057: }
 7058: 
 7059: sub standard_roleprivs {
 7060:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 7061:     if (defined($pr{$trole.':s'})) {
 7062:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 7063:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 7064:     }
 7065:     if ($tdomain ne '') {
 7066:         if (defined($pr{$trole.':d'})) {
 7067:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7068:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 7069:         }
 7070:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 7071:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 7072:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 7073:         }
 7074:     }
 7075: }
 7076: 
 7077: sub set_userprivs {
 7078:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 7079:     my $author=0;
 7080:     my $adv=0;
 7081:     my $rar=0;
 7082:     my %grouproles = ();
 7083:     if (keys(%{$allgroups}) > 0) {
 7084:         my @groupkeys; 
 7085:         foreach my $role (keys(%{$allroles})) {
 7086:             push(@groupkeys,$role);
 7087:         }
 7088:         if (ref($groups_roles) eq 'HASH') {
 7089:             foreach my $key (keys(%{$groups_roles})) {
 7090:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 7091:                     push(@groupkeys,$key);
 7092:                 }
 7093:             }
 7094:         }
 7095:         if (@groupkeys > 0) {
 7096:             foreach my $role (@groupkeys) {
 7097:                 my ($trole,$area,$sec,$extendedarea);
 7098:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 7099:                     $trole = $1;
 7100:                     $area = $2;
 7101:                     $sec = $3;
 7102:                     $extendedarea = $area.$sec;
 7103:                     if (exists($$allgroups{$area})) {
 7104:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 7105:                             my $spec = $trole.'.'.$extendedarea;
 7106:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 7107:                                                 $$allgroups{$area}{$group};
 7108:                         }
 7109:                     }
 7110:                 }
 7111:             }
 7112:         }
 7113:     }
 7114:     foreach my $group (keys(%grouproles)) {
 7115:         $$allroles{$group} = $grouproles{$group};
 7116:     }
 7117:     foreach my $role (keys(%{$allroles})) {
 7118:         my %thesepriv;
 7119:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 7120:         foreach my $item (split(/:/,$$allroles{$role})) {
 7121:             if ($item ne '') {
 7122:                 my ($privilege,$restrictions)=split(/&/,$item);
 7123:                 if ($restrictions eq '') {
 7124:                     $thesepriv{$privilege}='F';
 7125:                 } elsif ($thesepriv{$privilege} ne 'F') {
 7126:                     $thesepriv{$privilege}.=$restrictions;
 7127:                 }
 7128:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 7129:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 7130:             }
 7131:         }
 7132:         my $thesestr='';
 7133:         foreach my $priv (sort(keys(%thesepriv))) {
 7134: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 7135: 	}
 7136:         $userroles->{'user.priv.'.$role} = $thesestr;
 7137:     }
 7138:     return ($author,$adv,$rar);
 7139: }
 7140: 
 7141: sub role_status {
 7142:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 7143:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 7144:         my ($one,$two) = split(m{\./},$rolekey,2);
 7145:         (undef,undef,$$role) = split(/\./,$one,3);
 7146:         unless (!defined($$role) || $$role eq '') {
 7147:             $$where = '/'.$two;
 7148:             $$trolecode=$$role.'.'.$$where;
 7149:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 7150:             $$tstatus='is';
 7151:             if ($$tstart && $$tstart>$update) {
 7152:                 $$tstatus='future';
 7153:                 if ($$tstart<$now) {
 7154:                     if ($$tstart && $$tstart>$refresh) {
 7155:                         if (($$where ne '') && ($$role ne '')) {
 7156:                             my (%allroles,%allgroups,$group_privs,
 7157:                                 %groups_roles,@rolecodes);
 7158:                             my %userroles = (
 7159:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 7160:                             );
 7161:                             @rolecodes = ('cm'); 
 7162:                             my $spec=$$role.'.'.$$where;
 7163:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 7164:                             if ($$role =~ /^cr\//) {
 7165:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 7166:                                 push(@rolecodes,'cr');
 7167:                             } elsif ($$role eq 'gr') {
 7168:                                 push(@rolecodes,$$role);
 7169:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 7170:                                                     $env{'user.name'});
 7171:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 7172:                                 (undef,my $group_privs) = split(/\//,$trole);
 7173:                                 $group_privs = &unescape($group_privs);
 7174:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 7175:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 7176:                                 &get_groups_roles($tdomain,$trest,
 7177:                                                   \%course_roles,\@rolecodes,
 7178:                                                   \%groups_roles);
 7179:                             } else {
 7180:                                 push(@rolecodes,$$role);
 7181:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 7182:                             }
 7183:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 7184:                                                                    \%groups_roles);
 7185:                             &appenv(\%userroles,\@rolecodes);
 7186:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7187:                         }
 7188:                     }
 7189:                     $$tstatus = 'is';
 7190:                 }
 7191:             }
 7192:             if ($$tend) {
 7193:                 if ($$tend<$update) {
 7194:                     $$tstatus='expired';
 7195:                 } elsif ($$tend<$now) {
 7196:                     $$tstatus='will_not';
 7197:                 }
 7198:             }
 7199:         }
 7200:     }
 7201: }
 7202: 
 7203: sub get_groups_roles {
 7204:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 7205:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 7206:                   (ref($rolecodes) eq 'ARRAY') && 
 7207:                   (ref($groups_roles) eq 'HASH')); 
 7208:     if (keys(%{$cdom_courseroles}) > 0) {
 7209:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 7210:         if ($cdom ne '' && $cnum ne '') {
 7211:             foreach my $key (keys(%{$cdom_courseroles})) {
 7212:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 7213:                     my $crsrole = $1;
 7214:                     my $crssec = $2;
 7215:                     if ($crsrole =~ /^cr/) {
 7216:                         unless (grep(/^cr$/,@{$rolecodes})) {
 7217:                             push(@{$rolecodes},'cr');
 7218:                         }
 7219:                     } else {
 7220:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 7221:                             push(@{$rolecodes},$crsrole);
 7222:                         }
 7223:                     }
 7224:                     my $rolekey = "$crsrole./$cdom/$cnum";
 7225:                     if ($crssec ne '') {
 7226:                         $rolekey .= "/$crssec";
 7227:                     }
 7228:                     $rolekey .= './';
 7229:                     $groups_roles->{$rolekey} = $rolecodes;
 7230:                 }
 7231:             }
 7232:         }
 7233:     }
 7234:     return;
 7235: }
 7236: 
 7237: sub delete_env_groupprivs {
 7238:     my ($where,$courseroles,$possroles) = @_;
 7239:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 7240:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 7241:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 7242:         %{$courseroles->{$udom}} =
 7243:             &get_my_roles('','','userroles',['active'],
 7244:                           $possroles,[$udom],1);
 7245:     }
 7246:     if (ref($courseroles->{$udom}) eq 'HASH') {
 7247:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 7248:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 7249:             my $area = '/'.$cdom.'/'.$cnum;
 7250:             my $privkey = "user.priv.$crsrole.$area";
 7251:             if ($crssec ne '') {
 7252:                 $privkey .= '/'.$crssec;
 7253:             }
 7254:             $privkey .= ".$area/$group";
 7255:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 7256:         }
 7257:     }
 7258:     return;
 7259: }
 7260: 
 7261: sub check_adhoc_privs {
 7262:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 7263:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 7264:     if ($sec) {
 7265:         $cckey .= '/'.$sec;
 7266:     } 
 7267:     my $setprivs;
 7268:     if ($env{$cckey}) {
 7269:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 7270:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 7271:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 7272:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7273:             $setprivs = 1;
 7274:         }
 7275:     } else {
 7276:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 7277:         $setprivs = 1;
 7278:     }
 7279:     return $setprivs;
 7280: }
 7281: 
 7282: sub set_adhoc_privileges {
 7283: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 7284:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 7285:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 7286:     if ($sec ne '') {
 7287:         $area .= '/'.$sec;
 7288:     }
 7289:     my $spec = $role.'.'.$area;
 7290:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 7291:                                   $env{'user.name'},1);
 7292:     my %rolehash = ();
 7293:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 7294:         my $rolename = $1;
 7295:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 7296:         my %domdef = &get_domain_defaults($dcdom);
 7297:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 7298:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 7299:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 7300:             }
 7301:         }
 7302:     } else {
 7303:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 7304:     }
 7305:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 7306:     &appenv(\%userroles,[$role,'cm']);
 7307:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 7308:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 7309:             ($caller eq 'tiny')) {
 7310:         &appenv( {'request.role'        => $spec,
 7311:                   'request.role.domain' => $dcdom,
 7312:                   'request.course.sec'  => $sec,
 7313:                  }
 7314:                );
 7315:         my $tadv=0;
 7316:         if (&allowed('adv') eq 'F') { $tadv=1; }
 7317:         &appenv({'request.role.adv'    => $tadv});
 7318:     }
 7319: }
 7320: 
 7321: # --------------------------------------------------------------- get interface
 7322: 
 7323: sub get {
 7324:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7325:    my $items='';
 7326:    foreach my $item (@$storearr) {
 7327:        $items.=&escape($item).'&';
 7328:    }
 7329:    $items=~s/\&$//;
 7330:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7331:    if (!$uname) { $uname=$env{'user.name'}; }
 7332:    my $uhome=&homeserver($uname,$udomain);
 7333: 
 7334:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 7335:    my @pairs=split(/\&/,$rep);
 7336:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 7337:      return @pairs;
 7338:    }
 7339:    my %returnhash=();
 7340:    my $i=0;
 7341:    foreach my $item (@$storearr) {
 7342:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7343:       $i++;
 7344:    }
 7345:    return %returnhash;
 7346: }
 7347: 
 7348: # --------------------------------------------------------------- del interface
 7349: 
 7350: sub del {
 7351:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7352:    my $items='';
 7353:    foreach my $item (@$storearr) {
 7354:        $items.=&escape($item).'&';
 7355:    }
 7356: 
 7357:    $items=~s/\&$//;
 7358:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7359:    if (!$uname) { $uname=$env{'user.name'}; }
 7360:    my $uhome=&homeserver($uname,$udomain);
 7361:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7362: }
 7363: 
 7364: # -------------------------------------------------------------- dump interface
 7365: 
 7366: sub unserialize {
 7367:     my ($rep, $escapedkeys) = @_;
 7368: 
 7369:     return {} if $rep =~ /^error/;
 7370: 
 7371:     my %returnhash=();
 7372: 	foreach my $item (split(/\&/,$rep)) {
 7373: 	    my ($key, $value) = split(/=/, $item, 2);
 7374: 	    $key = unescape($key) unless $escapedkeys;
 7375: 	    next if $key =~ /^error: 2 /;
 7376: 	    $returnhash{$key} = &thaw_unescape($value);
 7377: 	}
 7378:     #return %returnhash;
 7379:     return \%returnhash;
 7380: }        
 7381: 
 7382: # see Lond::dump_with_regexp
 7383: # if $escapedkeys hash keys won't get unescaped.
 7384: sub dump {
 7385:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys,$encrypt)=@_;
 7386:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7387:     if (!$uname) { $uname=$env{'user.name'}; }
 7388:     my $uhome=&homeserver($uname,$udomain);
 7389: 
 7390:     if ($regexp) {
 7391:         $regexp=&escape($regexp);
 7392:     } else {
 7393:         $regexp='.';
 7394:     }
 7395:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7396:         # user is hosted on this machine
 7397:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7398:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7399:         return %{unserialize($reply, $escapedkeys)};
 7400:     }
 7401:     my $rep;
 7402:     if ($encrypt) {
 7403:         $rep=&reply("encrypt:edump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7404:     } else {
 7405:         $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7406:     }
 7407:     my @pairs=split(/\&/,$rep);
 7408:     my %returnhash=();
 7409:     if (!($rep =~ /^error/ )) {
 7410: 	foreach my $item (@pairs) {
 7411: 	    my ($key,$value)=split(/=/,$item,2);
 7412:         $key = unescape($key) unless $escapedkeys;
 7413:         #$key = &unescape($key);
 7414: 	    next if ($key =~ /^error: 2 /);
 7415: 	    $returnhash{$key}=&thaw_unescape($value);
 7416: 	}
 7417:     }
 7418:     return %returnhash;
 7419: }
 7420: 
 7421: 
 7422: # --------------------------------------------------------- dumpstore interface
 7423: 
 7424: sub dumpstore {
 7425:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7426:    # same as dump but keys must be escaped. They may contain colon separated
 7427:    # lists of values that may themself contain colons (e.g. symbs).
 7428:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7429: }
 7430: 
 7431: # -------------------------------------------------------------- keys interface
 7432: 
 7433: sub getkeys {
 7434:    my ($namespace,$udomain,$uname)=@_;
 7435:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7436:    if (!$uname) { $uname=$env{'user.name'}; }
 7437:    my $uhome=&homeserver($uname,$udomain);
 7438:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7439:    my @keyarray=();
 7440:    foreach my $key (split(/\&/,$rep)) {
 7441:       next if ($key =~ /^error: 2 /);
 7442:       push(@keyarray,&unescape($key));
 7443:    }
 7444:    return @keyarray;
 7445: }
 7446: 
 7447: # --------------------------------------------------------------- currentdump
 7448: sub currentdump {
 7449:    my ($courseid,$sdom,$sname)=@_;
 7450:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7451:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7452:    $sname    = $env{'user.name'}         if (! defined($sname));
 7453:    my $uhome = &homeserver($sname,$sdom);
 7454:    my $rep;
 7455: 
 7456:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7457:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7458:                    $courseid)));
 7459:    } else {
 7460:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7461:    }
 7462: 
 7463:    return if ($rep =~ /^(error:|no_such_host)/);
 7464:    #
 7465:    my %returnhash=();
 7466:    #
 7467:    if ($rep eq 'unknown_cmd') {
 7468:        # an old lond will not know currentdump
 7469:        # Do a dump and make it look like a currentdump
 7470:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7471:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7472:        my %hash = @tmp;
 7473:        @tmp=();
 7474:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7475:    } else {
 7476:        my @pairs=split(/\&/,$rep);
 7477:        foreach my $pair (@pairs) {
 7478:            my ($key,$value)=split(/=/,$pair,2);
 7479:            my ($symb,$param) = split(/:/,$key);
 7480:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7481:                                                         &thaw_unescape($value);
 7482:        }
 7483:    }
 7484:    return %returnhash;
 7485: }
 7486: 
 7487: sub convert_dump_to_currentdump{
 7488:     my %hash = %{shift()};
 7489:     my %returnhash;
 7490:     # Code ripped from lond, essentially.  The only difference
 7491:     # here is the unescaping done by lonnet::dump().  Conceivably
 7492:     # we might run in to problems with parameter names =~ /^v\./
 7493:     while (my ($key,$value) = each(%hash)) {
 7494:         my ($v,$symb,$param) = split(/:/,$key);
 7495: 	$symb  = &unescape($symb);
 7496: 	$param = &unescape($param);
 7497:         next if ($v eq 'version' || $symb eq 'keys');
 7498:         next if (exists($returnhash{$symb}) &&
 7499:                  exists($returnhash{$symb}->{$param}) &&
 7500:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7501:         $returnhash{$symb}->{$param}=$value;
 7502:         $returnhash{$symb}->{'v.'.$param}=$v;
 7503:     }
 7504:     #
 7505:     # Remove all of the keys in the hashes which keep track of
 7506:     # the version of the parameter.
 7507:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7508:         # use a foreach because we are going to delete from the hash.
 7509:         foreach my $key (keys(%$param_hash)) {
 7510:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7511:         }
 7512:     }
 7513:     return \%returnhash;
 7514: }
 7515: 
 7516: # ------------------------------------------------------ critical inc interface
 7517: 
 7518: sub cinc {
 7519:     return &inc(@_,'critical');
 7520: }
 7521: 
 7522: # --------------------------------------------------------------- inc interface
 7523: 
 7524: sub inc {
 7525:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7526:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7527:     if (!$uname) { $uname=$env{'user.name'}; }
 7528:     my $uhome=&homeserver($uname,$udomain);
 7529:     my $items='';
 7530:     if (! ref($store)) {
 7531:         # got a single value, so use that instead
 7532:         $items = &escape($store).'=&';
 7533:     } elsif (ref($store) eq 'SCALAR') {
 7534:         $items = &escape($$store).'=&';        
 7535:     } elsif (ref($store) eq 'ARRAY') {
 7536:         $items = join('=&',map {&escape($_);} @{$store});
 7537:     } elsif (ref($store) eq 'HASH') {
 7538:         while (my($key,$value) = each(%{$store})) {
 7539:             $items.= &escape($key).'='.&escape($value).'&';
 7540:         }
 7541:     }
 7542:     $items=~s/\&$//;
 7543:     if ($critical) {
 7544: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7545:     } else {
 7546: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7547:     }
 7548: }
 7549: 
 7550: # --------------------------------------------------------------- put interface
 7551: 
 7552: sub put {
 7553:    my ($namespace,$storehash,$udomain,$uname,$encrypt)=@_;
 7554:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7555:    if (!$uname) { $uname=$env{'user.name'}; }
 7556:    my $uhome=&homeserver($uname,$udomain);
 7557:    my $items='';
 7558:    foreach my $item (keys(%$storehash)) {
 7559:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7560:    }
 7561:    $items=~s/\&$//;
 7562:    if ($encrypt) {
 7563:        return &reply("encrypt:put:$udomain:$uname:$namespace:$items",$uhome);
 7564:    } else {
 7565:        return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7566:    }
 7567: }
 7568: 
 7569: # ------------------------------------------------------------ newput interface
 7570: 
 7571: sub newput {
 7572:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7573:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7574:    if (!$uname) { $uname=$env{'user.name'}; }
 7575:    my $uhome=&homeserver($uname,$udomain);
 7576:    my $items='';
 7577:    foreach my $key (keys(%$storehash)) {
 7578:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7579:    }
 7580:    $items=~s/\&$//;
 7581:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7582: }
 7583: 
 7584: # ---------------------------------------------------------  putstore interface
 7585: 
 7586: sub putstore {
 7587:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7588:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7589:    if (!$uname) { $uname=$env{'user.name'}; }
 7590:    my $uhome=&homeserver($uname,$udomain);
 7591:    my $items='';
 7592:    foreach my $key (keys(%$storehash)) {
 7593:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7594:    }
 7595:    $items=~s/\&$//;
 7596:    my $esc_symb=&escape($symb);
 7597:    my $esc_v=&escape($version);
 7598:    my $reply =
 7599:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7600: 	      $uhome);
 7601:    if (($tolog) && ($reply eq 'ok')) {
 7602:        my $namevalue='';
 7603:        foreach my $key (keys(%{$storehash})) {
 7604:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7605:        }
 7606:        my $ip = &get_requestor_ip();
 7607:        $namevalue .= 'ip='.&escape($ip).
 7608:                      '&host='.&escape($perlvar{'lonHostID'}).
 7609:                      '&version='.$esc_v.
 7610:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7611:        &courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7612:    }
 7613:    if ($reply eq 'unknown_cmd') {
 7614:        # gfall back to way things use to be done
 7615:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7616: 			    $uname);
 7617:    }
 7618:    return $reply;
 7619: }
 7620: 
 7621: sub old_putstore {
 7622:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7623:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7624:     if (!$uname) { $uname=$env{'user.name'}; }
 7625:     my $uhome=&homeserver($uname,$udomain);
 7626:     my %newstorehash;
 7627:     foreach my $item (keys(%$storehash)) {
 7628: 	my $key = $version.':'.&escape($symb).':'.$item;
 7629: 	$newstorehash{$key} = $storehash->{$item};
 7630:     }
 7631:     my $items='';
 7632:     my %allitems = ();
 7633:     foreach my $item (keys(%newstorehash)) {
 7634: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7635: 	    my $key = $1.':keys:'.$2;
 7636: 	    $allitems{$key} .= $3.':';
 7637: 	}
 7638: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7639:     }
 7640:     foreach my $item (keys(%allitems)) {
 7641: 	$allitems{$item} =~ s/\:$//;
 7642: 	$items.= $item.'='.$allitems{$item}.'&';
 7643:     }
 7644:     $items=~s/\&$//;
 7645:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7646: }
 7647: 
 7648: # ------------------------------------------------------ critical put interface
 7649: 
 7650: sub cput {
 7651:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7652:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7653:    if (!$uname) { $uname=$env{'user.name'}; }
 7654:    my $uhome=&homeserver($uname,$udomain);
 7655:    my $items='';
 7656:    foreach my $item (keys(%$storehash)) {
 7657:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7658:    }
 7659:    $items=~s/\&$//;
 7660:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7661: }
 7662: 
 7663: # -------------------------------------------------------------- eget interface
 7664: 
 7665: sub eget {
 7666:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7667:    my $items='';
 7668:    foreach my $item (@$storearr) {
 7669:        $items.=&escape($item).'&';
 7670:    }
 7671:    $items=~s/\&$//;
 7672:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7673:    if (!$uname) { $uname=$env{'user.name'}; }
 7674:    my $uhome=&homeserver($uname,$udomain);
 7675:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7676:    my @pairs=split(/\&/,$rep);
 7677:    my %returnhash=();
 7678:    my $i=0;
 7679:    foreach my $item (@$storearr) {
 7680:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7681:       $i++;
 7682:    }
 7683:    return %returnhash;
 7684: }
 7685: 
 7686: # ------------------------------------------------------------ tmpput interface
 7687: sub tmpput {
 7688:     my ($storehash,$server,$context)=@_;
 7689:     my $items='';
 7690:     foreach my $item (keys(%$storehash)) {
 7691: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7692:     }
 7693:     $items=~s/\&$//;
 7694:     if (defined($context)) {
 7695:         $items .= ':'.&escape($context);
 7696:     }
 7697:     return &reply("tmpput:$items",$server);
 7698: }
 7699: 
 7700: # ------------------------------------------------------------ tmpget interface
 7701: sub tmpget {
 7702:     my ($token,$server)=@_;
 7703:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7704:     my $rep=&reply("tmpget:$token",$server);
 7705:     my %returnhash;
 7706:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7707:         return %returnhash;
 7708:     }
 7709:     foreach my $item (split(/\&/,$rep)) {
 7710: 	my ($key,$value)=split(/=/,$item);
 7711: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7712:     }
 7713:     return %returnhash;
 7714: }
 7715: 
 7716: # ------------------------------------------------------------ tmpdel interface
 7717: sub tmpdel {
 7718:     my ($token,$server)=@_;
 7719:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7720:     return &reply("tmpdel:$token",$server);
 7721: }
 7722: 
 7723: # ------------------------------------------------------------ get_timebased_id 
 7724: 
 7725: sub get_timebased_id {
 7726:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7727:         $maxtries) = @_;
 7728:     my ($newid,$error,$dellock);
 7729:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7730:         return ('','ok','invalid call to get suffix');
 7731:     }
 7732: 
 7733: # set defaults for any optional args for which values were not supplied
 7734:     if ($who eq '') {
 7735:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7736:     }
 7737:     if (!$locktries) {
 7738:         $locktries = 3;
 7739:     }
 7740:     if (!$maxtries) {
 7741:         $maxtries = 10;
 7742:     }
 7743:     
 7744:     if (($cdom eq '') || ($cnum eq '')) {
 7745:         if ($env{'request.course.id'}) {
 7746:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7747:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7748:         }
 7749:         if (($cdom eq '') || ($cnum eq '')) {
 7750:             return ('','ok','call to get suffix not in course context');
 7751:         }
 7752:     }
 7753: 
 7754: # construct locking item
 7755:     my $lockhash = {
 7756:                       $prefix."\0".'locked_'.$keyid => $who,
 7757:                    };
 7758:     my $tries = 0;
 7759: 
 7760: # attempt to get lock on nohist_$namespace file
 7761:     my $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7762:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7763:         $tries ++;
 7764:         sleep 1;
 7765:         $gotlock = &newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7766:     }
 7767: 
 7768: # attempt to get unique identifier, based on current timestamp
 7769:     if ($gotlock eq 'ok') {
 7770:         my %inuse = &dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7771:         my $id = time;
 7772:         $newid = $id;
 7773:         if ($idtype eq 'addcode') {
 7774:             $newid .= &sixnum_code();
 7775:         }
 7776:         my $idtries = 0;
 7777:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7778:             if ($idtype eq 'concat') {
 7779:                 $newid = $id.$idtries;
 7780:             } elsif ($idtype eq 'addcode') {
 7781:                 $newid = $newid.&sixnum_code();
 7782:             } else {
 7783:                 $newid ++;
 7784:             }
 7785:             $idtries ++;
 7786:         }
 7787:         if (!exists($inuse{$prefix."\0".$newid})) {
 7788:             my %new_item =  (
 7789:                               $prefix."\0".$newid => $who,
 7790:                             );
 7791:             my $putresult = &put('nohist_'.$namespace,\%new_item,
 7792:                                                  $cdom,$cnum);
 7793:             if ($putresult ne 'ok') {
 7794:                 undef($newid);
 7795:                 $error = 'error saving new item: '.$putresult;
 7796:             }
 7797:         } else {
 7798:              undef($newid);
 7799:              $error = ('error: no unique suffix available for the new item ');
 7800:         }
 7801: #  remove lock
 7802:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7803:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7804:     } else {
 7805:         $error = "error: could not obtain lockfile\n";
 7806:         $dellock = 'ok';
 7807:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7808:             $dellock = 'nolock';
 7809:         }
 7810:     }
 7811:     return ($newid,$dellock,$error);
 7812: }
 7813: 
 7814: sub sixnum_code {
 7815:     my $code;
 7816:     for (0..6) {
 7817:         $code .= int( rand(9) );
 7818:     }
 7819:     return $code;
 7820: }
 7821: 
 7822: # -------------------------------------------------- portfolio access checking
 7823: 
 7824: sub portfolio_access {
 7825:     my ($requrl,$clientip) = @_;
 7826:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7827:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7828:     if ($result) {
 7829:         my %setters;
 7830:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7831:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7832:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip,$unum,$udom);
 7833:             if (($startblock && $endblock) || ($by_ip)) {
 7834:                 return 'B';
 7835:             }
 7836:         } else {
 7837:             my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) =
 7838:                 &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 7839:             if (($startblock && $endblock) || ($by_ip)) {
 7840:                 return 'B';
 7841:             }
 7842:         }
 7843:     }
 7844:     if ($result eq 'ok') {
 7845:        return 'F';
 7846:     } elsif ($result =~ /^[^:]+:guest_/) {
 7847:        return 'A';
 7848:     }
 7849:     return '';
 7850: }
 7851: 
 7852: sub get_portfolio_access {
 7853:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7854: 
 7855:     if (!ref($access_hash)) {
 7856: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7857: 	my %access_controls = &get_access_controls($current_perms,$group,
 7858: 						   $file_name);
 7859: 	$access_hash = $access_controls{$file_name};
 7860:     }
 7861: 
 7862:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7863:     my $now = time;
 7864:     if (ref($access_hash) eq 'HASH') {
 7865:         foreach my $key (keys(%{$access_hash})) {
 7866:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7867:             if ($start > $now) {
 7868:                 next;
 7869:             }
 7870:             if ($end && $end<$now) {
 7871:                 next;
 7872:             }
 7873:             if ($scope eq 'public') {
 7874:                 $public = $key;
 7875:                 last;
 7876:             } elsif ($scope eq 'guest') {
 7877:                 $guest = $key;
 7878:             } elsif ($scope eq 'domains') {
 7879:                 push(@domains,$key);
 7880:             } elsif ($scope eq 'users') {
 7881:                 push(@users,$key);
 7882:             } elsif ($scope eq 'course') {
 7883:                 push(@courses,$key);
 7884:             } elsif ($scope eq 'group') {
 7885:                 push(@groups,$key);
 7886:             } elsif ($scope eq 'ip') {
 7887:                 push(@ips,$key);
 7888:             }
 7889:         }
 7890:         if ($public) {
 7891:             return 'ok';
 7892:         } elsif (@ips > 0) {
 7893:             my $allowed;
 7894:             foreach my $ipkey (@ips) {
 7895:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7896:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7897:                         $allowed = 1;
 7898:                         last; 
 7899:                     }
 7900:                 }
 7901:             }
 7902:             if ($allowed) {
 7903:                 return 'ok';
 7904:             }
 7905:         }
 7906:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7907:             if ($guest) {
 7908:                 return $guest;
 7909:             }
 7910:         } else {
 7911:             if (@domains > 0) {
 7912:                 foreach my $domkey (@domains) {
 7913:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7914:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7915:                             return 'ok';
 7916:                         }
 7917:                     }
 7918:                 }
 7919:             }
 7920:             if (@users > 0) {
 7921:                 foreach my $userkey (@users) {
 7922:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7923:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7924:                             if (ref($item) eq 'HASH') {
 7925:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7926:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7927:                                     return 'ok';
 7928:                                 }
 7929:                             }
 7930:                         }
 7931:                     } 
 7932:                 }
 7933:             }
 7934:             my %roleshash;
 7935:             my @courses_and_groups = @courses;
 7936:             push(@courses_and_groups,@groups); 
 7937:             if (@courses_and_groups > 0) {
 7938:                 my (%allgroups,%allroles); 
 7939:                 my ($start,$end,$role,$sec,$group);
 7940:                 foreach my $envkey (%env) {
 7941:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7942:                         my $cid = $2.'_'.$3; 
 7943:                         if ($1 eq 'gr') {
 7944:                             $group = $4;
 7945:                             $allgroups{$cid}{$group} = $env{$envkey};
 7946:                         } else {
 7947:                             if ($4 eq '') {
 7948:                                 $sec = 'none';
 7949:                             } else {
 7950:                                 $sec = $4;
 7951:                             }
 7952:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7953:                         }
 7954:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7955:                         my $cid = $2.'_'.$3;
 7956:                         if ($4 eq '') {
 7957:                             $sec = 'none';
 7958:                         } else {
 7959:                             $sec = $4;
 7960:                         }
 7961:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7962:                     }
 7963:                 }
 7964:                 if (keys(%allroles) == 0) {
 7965:                     return;
 7966:                 }
 7967:                 foreach my $key (@courses_and_groups) {
 7968:                     my %content = %{$$access_hash{$key}};
 7969:                     my $cnum = $content{'number'};
 7970:                     my $cdom = $content{'domain'};
 7971:                     my $cid = $cdom.'_'.$cnum;
 7972:                     if (!exists($allroles{$cid})) {
 7973:                         next;
 7974:                     }    
 7975:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7976:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7977:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7978:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7979:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7980:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7981:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7982:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7983:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7984:                                         if (grep/^all$/,@sections) {
 7985:                                             return 'ok';
 7986:                                         } else {
 7987:                                             if (grep/^$sec$/,@sections) {
 7988:                                                 return 'ok';
 7989:                                             }
 7990:                                         }
 7991:                                     }
 7992:                                 }
 7993:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7994:                                     if (grep/^none$/,@groups) {
 7995:                                         return 'ok';
 7996:                                     }
 7997:                                 } else {
 7998:                                     if (grep/^all$/,@groups) {
 7999:                                         return 'ok';
 8000:                                     } 
 8001:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 8002:                                         if (grep/^$group$/,@groups) {
 8003:                                             return 'ok';
 8004:                                         }
 8005:                                     }
 8006:                                 } 
 8007:                             }
 8008:                         }
 8009:                     }
 8010:                 }
 8011:             }
 8012:             if ($guest) {
 8013:                 return $guest;
 8014:             }
 8015:         }
 8016:     }
 8017:     return;
 8018: }
 8019: 
 8020: sub course_group_datechecker {
 8021:     my ($dates,$now,$status) = @_;
 8022:     my ($start,$end) = split(/\./,$dates);
 8023:     if (!$start && !$end) {
 8024:         return 'ok';
 8025:     }
 8026:     if (grep/^active$/,@{$status}) {
 8027:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 8028:             return 'ok';
 8029:         }
 8030:     }
 8031:     if (grep/^previous$/,@{$status}) {
 8032:         if ($end > $now ) {
 8033:             return 'ok';
 8034:         }
 8035:     }
 8036:     if (grep/^future$/,@{$status}) {
 8037:         if ($start > $now) {
 8038:             return 'ok';
 8039:         }
 8040:     }
 8041:     return; 
 8042: }
 8043: 
 8044: sub parse_portfolio_url {
 8045:     my ($url) = @_;
 8046: 
 8047:     my ($type,$udom,$unum,$group,$file_name);
 8048:     
 8049:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 8050: 	$type = 1;
 8051:         $udom = $1;
 8052:         $unum = $2;
 8053:         $file_name = $3;
 8054:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 8055: 	$type = 2;
 8056:         $udom = $1;
 8057:         $unum = $2;
 8058:         $group = $3;
 8059:         $file_name = $3.'/'.$4;
 8060:     }
 8061:     if (wantarray) {
 8062: 	return ($type,$udom,$unum,$file_name,$group);
 8063:     }
 8064:     return $type;
 8065: }
 8066: 
 8067: sub is_portfolio_url {
 8068:     my ($url) = @_;
 8069:     return scalar(&parse_portfolio_url($url));
 8070: }
 8071: 
 8072: sub is_portfolio_file {
 8073:     my ($file) = @_;
 8074:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 8075:         return 1;
 8076:     }
 8077:     return;
 8078: }
 8079: 
 8080: sub is_coursetool_logo {
 8081:     my ($uri) = @_;
 8082:     if ($env{'request.course.id'}) {
 8083:         my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 8084:         if ($uri =~ m{^/*uploaded\Q$courseurl\E/toollogo/\d+/[^/]+$}) {
 8085:             return 1;
 8086:         }
 8087:     }
 8088:     return;
 8089: }
 8090: 
 8091: sub usertools_access {
 8092:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 8093:     my ($access,%tools);
 8094:     if ($context eq '') {
 8095:         $context = 'tools';
 8096:     }
 8097:     if ($context eq 'requestcourses') {
 8098:         %tools = (
 8099:                       official   => 1,
 8100:                       unofficial => 1,
 8101:                       community  => 1,
 8102:                       textbook   => 1,
 8103:                       placement  => 1,
 8104:                       lti        => 1,
 8105:                  );
 8106:     } elsif ($context eq 'requestauthor') {
 8107:         %tools = (
 8108:                       requestauthor => 1,
 8109:                  );
 8110:     } else {
 8111:         %tools = (
 8112:                       aboutme   => 1,
 8113:                       blog      => 1,
 8114:                       webdav    => 1,
 8115:                       portfolio => 1,
 8116:                       timezone  => 1,
 8117:                  );
 8118:     }
 8119:     return if (!defined($tools{$tool}));
 8120: 
 8121:     if (($udom eq '') || ($uname eq '')) {
 8122:         $udom = $env{'user.domain'};
 8123:         $uname = $env{'user.name'};
 8124:     }
 8125: 
 8126:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8127:         if ($action ne 'reload') {
 8128:             if ($context eq 'requestcourses') {
 8129:                 return $env{'environment.canrequest.'.$tool};
 8130:             } elsif ($context eq 'requestauthor') {
 8131:                 return $env{'environment.canrequest.author'};
 8132:             } else {
 8133:                 return $env{'environment.availabletools.'.$tool};
 8134:             }
 8135:         }
 8136:     }
 8137: 
 8138:     my ($toolstatus,$inststatus,$envkey);
 8139:     if ($context eq 'requestauthor') {
 8140:         $envkey = $context; 
 8141:     } else {
 8142:         $envkey = $context.'.'.$tool;
 8143:     }
 8144: 
 8145:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 8146:          ($action ne 'reload')) {
 8147:         $toolstatus = $env{'environment.'.$envkey};
 8148:         $inststatus = $env{'environment.inststatus'};
 8149:     } else {
 8150:         if (ref($userenvref) eq 'HASH') {
 8151:             $toolstatus = $userenvref->{$envkey};
 8152:             $inststatus = $userenvref->{'inststatus'};
 8153:         } else {
 8154:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 8155:             $toolstatus = $userenv{$envkey};
 8156:             $inststatus = $userenv{'inststatus'};
 8157:         }
 8158:     }
 8159: 
 8160:     if ($toolstatus ne '') {
 8161:         if ($toolstatus) {
 8162:             $access = 1;
 8163:         } else {
 8164:             $access = 0;
 8165:         }
 8166:         return $access;
 8167:     }
 8168: 
 8169:     my ($is_adv,%domdef);
 8170:     if (ref($is_advref) eq 'HASH') {
 8171:         $is_adv = $is_advref->{'is_adv'};
 8172:     } else {
 8173:         $is_adv = &is_advanced_user($udom,$uname);
 8174:     }
 8175:     if (ref($domdefref) eq 'HASH') {
 8176:         %domdef = %{$domdefref};
 8177:     } else {
 8178:         %domdef = &get_domain_defaults($udom);
 8179:     }
 8180:     if (ref($domdef{$tool}) eq 'HASH') {
 8181:         if ($is_adv) {
 8182:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 8183:                 if ($domdef{$tool}{'_LC_adv'}) { 
 8184:                     $access = 1;
 8185:                 } else {
 8186:                     $access = 0;
 8187:                 }
 8188:                 return $access;
 8189:             }
 8190:         }
 8191:         if ($inststatus ne '') {
 8192:             my ($hasaccess,$hasnoaccess);
 8193:             foreach my $affiliation (split(/:/,$inststatus)) {
 8194:                 if ($domdef{$tool}{$affiliation} ne '') { 
 8195:                     if ($domdef{$tool}{$affiliation}) {
 8196:                         $hasaccess = 1;
 8197:                     } else {
 8198:                         $hasnoaccess = 1;
 8199:                     }
 8200:                 }
 8201:             }
 8202:             if ($hasaccess || $hasnoaccess) {
 8203:                 if ($hasaccess) {
 8204:                     $access = 1;
 8205:                 } elsif ($hasnoaccess) {
 8206:                     $access = 0; 
 8207:                 }
 8208:                 return $access;
 8209:             }
 8210:         } else {
 8211:             if ($domdef{$tool}{'default'} ne '') {
 8212:                 if ($domdef{$tool}{'default'}) {
 8213:                     $access = 1;
 8214:                 } elsif ($domdef{$tool}{'default'} == 0) {
 8215:                     $access = 0;
 8216:                 }
 8217:                 return $access;
 8218:             }
 8219:         }
 8220:     } else {
 8221:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 8222:             $access = 1;
 8223:         } else {
 8224:             $access = 0;
 8225:         }
 8226:         return $access;
 8227:     }
 8228: }
 8229: 
 8230: sub is_course_owner {
 8231:     my ($cdom,$cnum,$udom,$uname) = @_;
 8232:     if (($udom eq '') || ($uname eq '')) {
 8233:         $udom = $env{'user.domain'};
 8234:         $uname = $env{'user.name'};
 8235:     }
 8236:     unless (($udom eq '') || ($uname eq '')) {
 8237:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 8238:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 8239:                 return 1;
 8240:             } else {
 8241:                 my %courseinfo = &coursedescription($cdom.'/'.$cnum);
 8242:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 8243:                     return 1;
 8244:                 }
 8245:             }
 8246:         }
 8247:     }
 8248:     return;
 8249: }
 8250: 
 8251: sub is_advanced_user {
 8252:     my ($udom,$uname) = @_;
 8253:     if ($udom ne '' && $uname ne '') {
 8254:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 8255:             if (wantarray) {
 8256:                 return ($env{'user.adv'},$env{'user.author'});
 8257:             } else {
 8258:                 return $env{'user.adv'};
 8259:             }
 8260:         }
 8261:     }
 8262:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 8263:     my %allroles;
 8264:     my ($is_adv,$is_author);
 8265:     foreach my $role (keys(%roleshash)) {
 8266:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 8267:         my $area = '/'.$tdomain.'/'.$trest;
 8268:         if ($sec ne '') {
 8269:             $area .= '/'.$sec;
 8270:         }
 8271:         if (($area ne '') && ($trole ne '')) {
 8272:             my $spec=$trole.'.'.$area;
 8273:             if ($trole =~ /^cr\//) {
 8274:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 8275:             } elsif ($trole ne 'gr') {
 8276:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 8277:             }
 8278:             if ($trole eq 'au') {
 8279:                 $is_author = 1;
 8280:             }
 8281:         }
 8282:     }
 8283:     foreach my $role (keys(%allroles)) {
 8284:         last if ($is_adv);
 8285:         foreach my $item (split(/:/,$allroles{$role})) {
 8286:             if ($item ne '') {
 8287:                 my ($privilege,$restrictions)=split(/&/,$item);
 8288:                 if ($privilege eq 'adv') {
 8289:                     $is_adv = 1;
 8290:                     last;
 8291:                 }
 8292:             }
 8293:         }
 8294:     }
 8295:     if (wantarray) {
 8296:         return ($is_adv,$is_author);
 8297:     }
 8298:     return $is_adv;
 8299: }
 8300: 
 8301: sub check_can_request {
 8302:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 8303:     my $canreq = 0;
 8304:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 8305:         $uname = $env{'user.name'};
 8306:         $udom = $env{'user.domain'};
 8307:     }
 8308:     my ($types,$typename) = &Apache::loncommon::course_types();
 8309:     my @options = ('approval','validate','autolimit');
 8310:     my $optregex = join('|',@options);
 8311:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 8312:         my %willtrust;
 8313:         foreach my $type (@{$types}) {
 8314:             if (&usertools_access($uname,$udom,$type,undef,
 8315:                                   'requestcourses')) {
 8316:                 $canreq ++;
 8317:                 if (ref($request_domains) eq 'HASH') {
 8318:                     push(@{$request_domains->{$type}},$udom);
 8319:                 }
 8320:                 if ($dom eq $udom) {
 8321:                     $can_request->{$type} = 1;
 8322:                 }
 8323:             }
 8324:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 8325:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 8326:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 8327:                 if (@curr > 0) {
 8328:                     foreach my $item (@curr) {
 8329:                         if (ref($request_domains) eq 'HASH') {
 8330:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 8331:                             if ($otherdom ne '') {
 8332:                                 unless (exists($willtrust{$otherdom})) {
 8333:                                     $willtrust{$otherdom} = &will_trust('reqcrs',$env{'user.domain'},$otherdom);
 8334:                                 }
 8335:                                 if ($willtrust{$otherdom}) {
 8336:                                     if (ref($request_domains->{$type}) eq 'ARRAY') {
 8337:                                         unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 8338:                                             push(@{$request_domains->{$type}},$otherdom);
 8339:                                         }
 8340:                                     } else {
 8341:                                         push(@{$request_domains->{$type}},$otherdom);
 8342:                                     }
 8343:                                 }
 8344:                             }
 8345:                         }
 8346:                     }
 8347:                     unless ($dom eq $env{'user.domain'}) {
 8348:                         $canreq ++;
 8349:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 8350:                             $can_request->{$type} = 1;
 8351:                         }
 8352:                     }
 8353:                 }
 8354:             }
 8355:         }
 8356:     }
 8357:     return $canreq;
 8358: }
 8359: 
 8360: # ---------------------------------------------- Custom access rule evaluation
 8361: 
 8362: sub customaccess {
 8363:     my ($priv,$uri)=@_;
 8364:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 8365:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 8366:     $udom = &LONCAPA::clean_domain($udom);
 8367:     $ucrs = &LONCAPA::clean_username($ucrs);
 8368:     my $access=0;
 8369:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 8370: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 8371: 	if ($type eq 'user') {
 8372: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8373: 		my ($tdom,$tuname)=split(m{/},$scope);
 8374: 		if ($tdom) {
 8375: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8376: 		}
 8377: 		if ($tuname) {
 8378: 		    if ($tuname ne $env{'user.name'}) { next; }
 8379: 		}
 8380: 		$access=($effect eq 'allow');
 8381: 		last;
 8382: 	    }
 8383: 	} else {
 8384: 	    if ($role) {
 8385: 		if ($role ne $urole) { next; }
 8386: 	    }
 8387: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8388: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8389: 		if ($tdom) {
 8390: 		    if ($tdom ne $udom) { next; }
 8391: 		}
 8392: 		if ($tcrs) {
 8393: 		    if ($tcrs ne $ucrs) { next; }
 8394: 		}
 8395: 		if ($tsec) {
 8396: 		    if ($tsec ne $usec) { next; }
 8397: 		}
 8398: 		$access=($effect eq 'allow');
 8399: 		last;
 8400: 	    }
 8401: 	    if ($realm eq '' && $role eq '') {
 8402: 		$access=($effect eq 'allow');
 8403: 	    }
 8404: 	}
 8405:     }
 8406:     return $access;
 8407: }
 8408: 
 8409: # ------------------------------------------------- Check for a user privilege
 8410: 
 8411: sub allowed {
 8412:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache,$nodeeplinkcheck,$nodeeplinkout)=@_;
 8413:     my $ver_orguri=$uri;
 8414:     $uri=&deversion($uri);
 8415:     my $orguri=$uri;
 8416:     $uri=&declutter($uri);
 8417: 
 8418:     if ($priv eq 'evb') {
 8419: # Evade communication block restrictions for specified role in a course or domain
 8420:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8421:             return $1;
 8422:         } else {
 8423:             return;
 8424:         }
 8425:     }
 8426: 
 8427:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8428: # Free bre access to adm and meta resources
 8429:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|viewclasslist|aboutme|ext\.tool)$})) 
 8430: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8431: 	&& ($priv eq 'bre')) {
 8432: 	return 'F';
 8433:     }
 8434: 
 8435: # Free bre access to user's own portfolio contents
 8436:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8437:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8438: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8439:         my %setters;
 8440:         my ($startblock,$endblock,$triggerblock,$by_ip,$blockdom) = 
 8441:             &Apache::loncommon::blockcheck(\%setters,'port',$clientip);
 8442:         if (($startblock && $endblock) || ($by_ip)) {
 8443:             return 'B';
 8444:         } else {
 8445:             return 'F';
 8446:         }
 8447:     }
 8448: 
 8449: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8450:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8451:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8452:         if (exists($env{'request.course.id'})) {
 8453:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8454:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8455:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8456:                 my $courseprivid=$env{'request.course.id'};
 8457:                 $courseprivid=~s/\_/\//;
 8458:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8459:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8460:                     return $1; 
 8461:                 } else {
 8462:                     if ($env{'request.course.sec'}) {
 8463:                         $courseprivid.='/'.$env{'request.course.sec'};
 8464:                     }
 8465:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8466:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8467:                         return $2;
 8468:                     }
 8469:                 }
 8470:             }
 8471:         }
 8472:     }
 8473: 
 8474: # Free bre to public access
 8475: 
 8476:     if ($priv eq 'bre') {
 8477:         my $copyright;
 8478:         unless ($uri =~ /ext\.tool/) {
 8479:             $copyright=&metadata($uri,'copyright');
 8480:         }
 8481: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8482:            return 'F'; 
 8483:         }
 8484:         if ($copyright eq 'priv') {
 8485:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8486: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8487: 		return '';
 8488:             }
 8489:         }
 8490:         if ($copyright eq 'domain') {
 8491:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8492: 	    unless (($env{'user.domain'} eq $1) ||
 8493:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8494: 		return '';
 8495:             }
 8496:         }
 8497:         if ($env{'request.role'}=~ /li\.\//) {
 8498:             # Library role, so allow browsing of resources in this domain.
 8499:             return 'F';
 8500:         }
 8501:         if ($copyright eq 'custom') {
 8502: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8503:         }
 8504:     }
 8505:     # Domain coordinator is trying to create a course
 8506:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8507:         # uri is the requested domain in this case.
 8508:         # comparison to 'request.role.domain' shows if the user has selected
 8509:         # a role of dc for the domain in question.
 8510:         return 'F' if ($uri eq $env{'request.role.domain'});
 8511:     }
 8512: 
 8513:     my $thisallowed='';
 8514:     my $statecond=0;
 8515:     my $courseprivid='';
 8516: 
 8517:     my $ownaccess;
 8518:     # Community Coordinator or Assistant Co-author browsing resource space.
 8519:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8520:         if ($uri eq '') {
 8521:             $ownaccess = 1;
 8522:         } else {
 8523:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8524:                 my $udom = $env{'user.domain'};
 8525:                 my $uname = $env{'user.name'};
 8526:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8527:                     $ownaccess = 1;
 8528:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8529:                     unless ($uri =~ m{\.\./}) {
 8530:                         $ownaccess = 1;
 8531:                     }
 8532:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8533:                     my $now = time;
 8534:                     if ($uri =~ m{^([^/]+)/?$}) {
 8535:                         my $adom = $1;
 8536:                         foreach my $key (keys(%env)) {
 8537:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8538:                                 my ($start,$end) = split(/\./,$env{$key});
 8539:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8540:                                     $ownaccess = 1;
 8541:                                     last;
 8542:                                 }
 8543:                             }
 8544:                         }
 8545:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8546:                         my $adom = $1;
 8547:                         my $aname = $2;
 8548:                         foreach my $role ('ca','aa') { 
 8549:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8550:                                 my ($start,$end) =
 8551:                                     split(/\./,$env{"user.role.$role./$adom/$aname"});
 8552:                                 if (($now >= $start) && (!$end || $end > $now)) {
 8553:                                     $ownaccess = 1;
 8554:                                     last;
 8555:                                 }
 8556:                             }
 8557:                         }
 8558:                     }
 8559:                 }
 8560:             }
 8561:         }
 8562:     }
 8563: 
 8564: # Course
 8565: 
 8566:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8567:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8568:             $thisallowed.=$1;
 8569:         }
 8570:     }
 8571: 
 8572: # Domain
 8573: 
 8574:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8575:        =~/\Q$priv\E\&([^\:]*)/) {
 8576:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8577:             $thisallowed.=$1;
 8578:         }
 8579:     }
 8580: 
 8581: # User who is not author or co-author might still be able to edit
 8582: # resource of an author in the domain (e.g., if Domain Coordinator).
 8583:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8584:         (&allowed('mdc',$env{'request.course.id'}))) {
 8585:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8586:             $thisallowed.=$1;
 8587:         }
 8588:     }
 8589: 
 8590: # Course: uri itself is a course
 8591:     my $courseuri=$uri;
 8592:     $courseuri=~s/\_(\d)/\/$1/;
 8593:     $courseuri=~s/^([^\/])/\/$1/;
 8594: 
 8595:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8596:        =~/\Q$priv\E\&([^\:]*)/) {
 8597:         if ($priv eq 'mip') {
 8598:             my $rem = $1;
 8599:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8600:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8601:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8602:                 if ($cdom ne '') {
 8603:                     my %passwdconf = &get_passwdconf($cdom);
 8604:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8605:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8606:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8607:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8608:                                 unless (@inststatuses) {
 8609:                                     @inststatuses = ('default');
 8610:                                 }
 8611:                                 foreach my $status (@inststatuses) {
 8612:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8613:                                         $thisallowed.=$rem;
 8614:                                     }
 8615:                                 }
 8616:                             }
 8617:                         }
 8618:                     }
 8619:                 }
 8620:             }
 8621:         } else {
 8622:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8623:                 $thisallowed.=$1;
 8624:             }
 8625:         }
 8626:     }
 8627: 
 8628: # URI is an uploaded document for this course, default permissions don't matter
 8629: # not allowing 'edit' access (editupload) to uploaded course docs
 8630:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8631: 	$thisallowed='';
 8632:         my ($match)=&is_on_map($uri);
 8633:         if ($match) {
 8634:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8635:                   =~/\Q$priv\E\&([^\:]*)/) {
 8636:                 my $value = $1;
 8637:                 my $deeplinkblock;
 8638:                 unless ($nodeeplinkcheck) {
 8639:                     $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8640:                 }
 8641:                 if ($deeplinkblock) {
 8642:                     $thisallowed='D';
 8643:                 } elsif ($noblockcheck) {
 8644:                     $thisallowed.=$value;
 8645:                 } else {
 8646:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8647:                     if (@blockers > 0) {
 8648:                         $thisallowed = 'B';
 8649:                     } else {
 8650:                         $thisallowed.=$value;
 8651:                     }
 8652:                 }
 8653:             }
 8654:         } else {
 8655:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8656:             if ($refuri) {
 8657:                 if ($refuri =~ m|^/adm/|) {
 8658:                     $thisallowed='F';
 8659:                 } else {
 8660:                     $refuri=&declutter($refuri);
 8661:                     my ($match) = &is_on_map($refuri);
 8662:                     if ($match) {
 8663:                         my $deeplinkblock;
 8664:                         unless ($nodeeplinkcheck) {
 8665:                             $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8666:                         }
 8667:                         if ($deeplinkblock) {
 8668:                             $thisallowed='D';
 8669:                         } elsif ($noblockcheck) {
 8670:                             $thisallowed='F';
 8671:                         } else {
 8672:                             my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8673:                             if (@blockers > 0) {
 8674:                                 $thisallowed = 'B';
 8675:                             } else {
 8676:                                 $thisallowed='F';
 8677:                             }
 8678:                         }
 8679:                     }
 8680:                 }
 8681:             }
 8682:         }
 8683:     }
 8684: 
 8685:     if ($priv eq 'bre'
 8686: 	&& $thisallowed ne 'F' 
 8687: 	&& $thisallowed ne '2'
 8688: 	&& &is_portfolio_url($uri)) {
 8689: 	$thisallowed = &portfolio_access($uri,$clientip);
 8690:     }
 8691: 
 8692: # Full access at system, domain or course-wide level? Exit.
 8693:     if ($thisallowed=~/F/) {
 8694: 	return 'F';
 8695:     }
 8696: 
 8697: # If this is generating or modifying users, exit with special codes
 8698: 
 8699:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8700: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8701: 	    my ($audom,$auname)=split('/',$uri);
 8702: # no author name given, so this just checks on the general right to make a co-author in this domain
 8703: 	    unless ($auname) { return $thisallowed; }
 8704: # an author name is given, so we are about to actually make a co-author for a certain account
 8705: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8706: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8707: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8708: 	}
 8709: 	return $thisallowed;
 8710:     }
 8711: #
 8712: # Gathered so far: system, domain and course wide privileges
 8713: #
 8714: # Course: See if uri or referer is an individual resource that is part of 
 8715: # the course
 8716: 
 8717:     if ($env{'request.course.id'}) {
 8718: 
 8719:         if ($priv eq 'bre') {
 8720:             if (&is_coursetool_logo($uri)) {
 8721:                 return 'F';
 8722:             }
 8723:         }
 8724: 
 8725: # If this is modifying password (internal auth) domains must match for user and user's role.
 8726: 
 8727:         if ($priv eq 'mip') {
 8728:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8729:                 return $thisallowed;
 8730:             } else {
 8731:                 return '';
 8732:             }
 8733:         }
 8734: 
 8735:        $courseprivid=$env{'request.course.id'};
 8736:        if ($env{'request.course.sec'}) {
 8737:           $courseprivid.='/'.$env{'request.course.sec'};
 8738:        }
 8739:        $courseprivid=~s/\_/\//;
 8740:        my $checkreferer=1;
 8741:        my ($match,$cond)=&is_on_map($uri);
 8742:        if ($match) {
 8743:            $statecond=$cond;
 8744:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8745:                =~/\Q$priv\E\&([^\:]*)/) {
 8746:                my $value = $1;
 8747:                if ($priv eq 'bre') {
 8748:                    my $deeplinkblock;
 8749:                    unless ($nodeeplinkcheck) {
 8750:                        $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8751:                    }
 8752:                    if ($deeplinkblock) {
 8753:                        $thisallowed = 'D';
 8754:                    } elsif ($noblockcheck) {
 8755:                        $thisallowed.=$value;
 8756:                    } else {
 8757:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8758:                        if (@blockers > 0) {
 8759:                            $thisallowed = 'B';
 8760:                        } else {
 8761:                            $thisallowed.=$value;
 8762:                        }
 8763:                    }
 8764:                } else {
 8765:                    $thisallowed.=$value;
 8766:                }
 8767:                $checkreferer=0;
 8768:            }
 8769:        }
 8770: 
 8771:        if ($checkreferer) {
 8772: 	  my $refuri=$env{'httpref.'.$orguri};
 8773:             unless ($refuri) {
 8774:                 foreach my $key (keys(%env)) {
 8775: 		    if ($key=~/^httpref\..*\*/) {
 8776: 			my $pattern=$key;
 8777:                         $pattern=~s/^httpref\.\/res\///;
 8778:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8779:                         $pattern=~s/\//\\\//g;
 8780:                         if ($orguri=~/$pattern/) {
 8781: 			    $refuri=$env{$key};
 8782:                         }
 8783:                     }
 8784:                 }
 8785:             }
 8786: 
 8787:          if ($refuri) { 
 8788: 	  $refuri=&declutter($refuri);
 8789:           my ($match,$cond)=&is_on_map($refuri);
 8790:             if ($match) {
 8791:               my $refstatecond=$cond;
 8792:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8793:                   =~/\Q$priv\E\&([^\:]*)/) {
 8794:                   my $value = $1;
 8795:                   if ($priv eq 'bre') {
 8796:                       my $deeplinkblock;
 8797:                       unless ($nodeeplinkcheck) {
 8798:                           $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8799:                       }
 8800:                       if ($deeplinkblock) {
 8801:                           $thisallowed = 'D';
 8802:                       } elsif ($noblockcheck) {
 8803:                           $thisallowed.=$value;
 8804:                       } else {
 8805:                           my @blockers = &has_comm_blocking($priv,'',$refuri,'',1);
 8806:                           if (@blockers > 0) {
 8807:                               $thisallowed = 'B';
 8808:                           } else {
 8809:                               $thisallowed.=$value;
 8810:                           }
 8811:                       }
 8812:                   } else {
 8813:                       $thisallowed.=$value;
 8814:                   }
 8815:                   $uri=$refuri;
 8816:                   $statecond=$refstatecond;
 8817:               }
 8818:           }
 8819:         }
 8820:        }
 8821:    }
 8822: 
 8823: #
 8824: # Gathered now: all privileges that could apply, and condition number
 8825: # 
 8826: #
 8827: # Full or no access?
 8828: #
 8829: 
 8830:     if ($thisallowed=~/F/) {
 8831: 	return 'F';
 8832:     }
 8833: 
 8834:     unless ($thisallowed) {
 8835:         return '';
 8836:     }
 8837: 
 8838: # Restrictions exist, deal with them
 8839: #
 8840: #   C:according to course preferences
 8841: #   R:according to resource settings
 8842: #   L:unless locked
 8843: #   X:according to user session state
 8844: #
 8845: 
 8846: # Possibly locked functionality, check all courses
 8847: # In roles.tab, L (unless locked) available for bre, pch, plc, pac and sma.
 8848: # Locks might take effect only after 10 minutes cache expiration for other
 8849: # courses, and 2 minutes for current course, in which user has st or ta role
 8850: # which is neither expired nor a future role (unless current course).
 8851: 
 8852:     my ($needlockcheck,$now,$crsonly);
 8853:     if ($thisallowed=~/L/) {
 8854:         $now = time;
 8855:         if ($priv eq 'bre') {
 8856:             if ($uri ne '') {
 8857:                 if ($orguri =~ m{^/+res/}) {
 8858:                     if ($uri =~ m{^lib/templates/}) {
 8859:                         if ($env{'request.course.id'}) {
 8860:                             $crsonly = 1;
 8861:                             $needlockcheck = 1;
 8862:                         }
 8863:                     } else {
 8864:                         $needlockcheck = 1;
 8865:                     }
 8866:                 } elsif ($env{'request.course.id'}) {
 8867:                     my ($crsdom,$crsnum) = split('_',$env{'request.course.id'});
 8868:                     if (($uri =~ m{^(adm|uploaded|public)/$crsdom/$crsnum/}) ||
 8869:                         ($uri =~ m{^adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$})) {
 8870:                         $crsonly = 1;
 8871:                     }
 8872:                     $needlockcheck = 1;
 8873:                 }
 8874:             }
 8875:         } elsif (($priv eq 'pch') || ($priv eq 'plc') || ($priv eq 'pac') || ($priv eq 'sma')) {
 8876:             $needlockcheck = 1;
 8877:         }
 8878:     }
 8879:     if ($needlockcheck) {
 8880:         foreach my $envkey (keys(%env)) {
 8881:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8882:                my $courseid=$2;
 8883:                my $roleid=$1.'.'.$2;
 8884:                $courseid=~s/^\///;
 8885:                unless ($env{'request.role'} eq $roleid) {
 8886:                    my ($start,$end) = split(/\./,$env{$envkey});
 8887:                    next unless (($now >= $start) && (!$end || $end > $now));
 8888:                }
 8889:                my $expiretime=600;
 8890:                if ($env{'request.role'} eq $roleid) {
 8891: 		  $expiretime=120;
 8892:                }
 8893: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8894:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8895:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8896: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8897:                }
 8898:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8899:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8900: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8901:                        &log($env{'user.domain'},$env{'user.name'},
 8902:                             $env{'user.home'},
 8903:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8904:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8905:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8906: 		       return '';
 8907:                    }
 8908:                }
 8909:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8910:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8911: 		   if ($env{$prefix.'priv.'.$priv.'.lock.expire'}>time) {
 8912:                        &log($env{'user.domain'},$env{'user.name'},
 8913:                             $env{'user.home'},
 8914:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8915:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8916:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8917: 		       return '';
 8918:                    }
 8919:                }
 8920: 	   }
 8921:        }
 8922:     }
 8923: 
 8924: #
 8925: # Rest of the restrictions depend on selected course
 8926: #
 8927: 
 8928:     unless ($env{'request.course.id'}) {
 8929: 	if ($thisallowed eq 'A') {
 8930: 	    return 'A';
 8931:         } elsif ($thisallowed eq 'B') {
 8932:             return 'B';
 8933: 	} else {
 8934: 	    return '1';
 8935: 	}
 8936:     }
 8937: 
 8938: #
 8939: # Now user is definitely in a course
 8940: #
 8941: 
 8942: 
 8943: # Course preferences
 8944: 
 8945:    if ($thisallowed=~/C/) {
 8946:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8947:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8948:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8949: 	   =~/\Q$rolecode\E/) {
 8950: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8951: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8952: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8953: 			$env{'request.course.id'});
 8954: 	   }
 8955:            return '';
 8956:        }
 8957: 
 8958:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8959: 	   =~/\Q$unamedom\E/) {
 8960: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8961: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8962: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8963: 			$env{'request.course.id'});
 8964: 	   }
 8965:            return '';
 8966:        }
 8967:    }
 8968: 
 8969: # Resource preferences
 8970: 
 8971:    if ($thisallowed=~/R/) {
 8972:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8973:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8974: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8975: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8976: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8977: 	   }
 8978: 	   return '';
 8979:        }
 8980:    }
 8981: 
 8982: # Restricted for deeplinked session?
 8983: 
 8984:     if ($env{'request.deeplink.login'}) {
 8985:         if ($env{'acc.deeplinkout'} && !$nodeeplinkout) {
 8986:             if (!$symb) { $symb=&symbread($uri,1); }
 8987:             if (($symb) && ($env{'acc.deeplinkout'}=~/\&\Q$symb\E\&/)) {
 8988:                 return '';
 8989:             }
 8990:         }
 8991:     }
 8992: 
 8993: # Restricted by state or randomout?
 8994: 
 8995:    if ($thisallowed=~/X/) {
 8996:       if ($env{'acc.randomout'}) {
 8997: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8998:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8999:             return ''; 
 9000:          }
 9001:       }
 9002:       if (&condval($statecond)) {
 9003: 	 return '2';
 9004:       } else {
 9005:          return '';
 9006:       }
 9007:    }
 9008: 
 9009:     if ($thisallowed eq 'A') {
 9010: 	return 'A';
 9011:     } elsif ($thisallowed eq 'B') {
 9012:         return 'B';
 9013:     } elsif ($thisallowed eq 'D') {
 9014:         return 'D';
 9015:     }
 9016:    return 'F';
 9017: }
 9018: 
 9019: # ------------------------------------------- Check construction space access
 9020: 
 9021: sub constructaccess {
 9022:     my ($url,$setpriv)=@_;
 9023: 
 9024: # We do not allow editing of previous versions of files
 9025:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 9026: 
 9027: # Get username and domain from URL
 9028:     my ($ownername,$ownerdomain,$ownerhome);
 9029: 
 9030:     ($ownerdomain,$ownername) =
 9031:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 9032: 
 9033: # The URL does not really point to any authorspace, forget it
 9034:     unless (($ownername) && ($ownerdomain)) { return ''; }
 9035: 
 9036: # Now we need to see if the user has access to the authorspace of
 9037: # $ownername at $ownerdomain
 9038: 
 9039:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 9040: # Real author for this?
 9041:        $ownerhome = $env{'user.home'};
 9042:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 9043:           return ($ownername,$ownerdomain,$ownerhome);
 9044:        }
 9045:     } elsif (&is_course($ownerdomain,$ownername)) {
 9046: # Course Authoring Space?
 9047:         if ($env{'request.course.id'}) {
 9048:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 9049:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 9050:                 if (&allowed('mdc',$env{'request.course.id'})) {
 9051:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 9052:                     return ($ownername,$ownerdomain,$ownerhome);
 9053:                 }
 9054:             }
 9055:         }
 9056:         return '';
 9057:     } else {
 9058: # Co-author for this?
 9059:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 9060:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 9061:             $ownerhome = &homeserver($ownername,$ownerdomain);
 9062:             return ($ownername,$ownerdomain,$ownerhome);
 9063:         }
 9064:     }
 9065: 
 9066: # We don't have any access right now. If we are not possibly going to do anything about this,
 9067: # we might as well leave
 9068:    unless ($setpriv) { return ''; }
 9069: 
 9070: # Backdoor access?
 9071:     my $allowed=&allowed('eco',$ownerdomain);
 9072: # Nope
 9073:     unless ($allowed) { return ''; }
 9074: # Looks like we may have access, but could be locked by the owner of the construction space
 9075:     if ($allowed eq 'U') {
 9076:         my %blocked=&get('environment',['domcoord.author'],
 9077:                          $ownerdomain,$ownername);
 9078: # Is blocked by owner
 9079:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 9080:     }
 9081:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 9082: # Grant temporary access
 9083:         my $then=$env{'user.login.time'};
 9084:         my $update=$env{'user.update.time'};
 9085:         if (!$update) { $update = $then; }
 9086:         my $refresh=$env{'user.refresh.time'};
 9087:         if (!$refresh) { $refresh = $update; }
 9088:         my $now = time;
 9089:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 9090:                            $now,'ca','constructaccess');
 9091:         $ownerhome = &homeserver($ownername,$ownerdomain);
 9092:         return($ownername,$ownerdomain,$ownerhome);
 9093:     }
 9094: # No business here
 9095:     return '';
 9096: }
 9097: 
 9098: # ----------------------------------------------------------- Content Blocking
 9099: 
 9100: {
 9101: # Caches for faster Course Contents display where content blocking
 9102: # is in operation (i.e., interval param set) for timed quiz.
 9103: #
 9104: # User for whom data are being temporarily cached.
 9105: my $cacheduser='';
 9106: # Course for which data are being temporarily cached.
 9107: my $cachedcid='';
 9108: # Cached blockers for this user (a hash of blocking items). 
 9109: my %cachedblockers=();
 9110: # When the data were last cached.
 9111: my $cachedlast='';
 9112: 
 9113: sub load_all_blockers {
 9114:     my ($uname,$udom)=@_;
 9115:     if (($uname ne '') && ($udom ne '')) { 
 9116:         if (($cacheduser eq $uname.':'.$udom) &&
 9117:             ($cachedcid eq $env{'request.course.id'}) &&
 9118:             (abs($cachedlast-time)<5)) {
 9119:             return;
 9120:         }
 9121:     }
 9122:     $cachedlast=time;
 9123:     $cacheduser=$uname.':'.$udom;
 9124:     $cachedcid=$env{'request.course.id'};
 9125:     %cachedblockers = &get_commblock_resources();
 9126:     return;
 9127: }
 9128: 
 9129: sub get_comm_blocks {
 9130:     my ($cdom,$cnum) = @_;
 9131:     if ($cdom eq '' || $cnum eq '') {
 9132:         return unless ($env{'request.course.id'});
 9133:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9134:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9135:     }
 9136:     my %commblocks;
 9137:     my $hashid=$cdom.'_'.$cnum;
 9138:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 9139:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 9140:         %commblocks = %{$blocksref};
 9141:     } else {
 9142:         %commblocks = &dump('comm_block',$cdom,$cnum);
 9143:         my $cachetime = 600;
 9144:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 9145:     }
 9146:     return %commblocks;
 9147: }
 9148: 
 9149: sub get_commblock_resources {
 9150:     my ($blocks) = @_;
 9151:     my %blockers = ();
 9152:     return %blockers unless ($env{'request.course.id'});
 9153:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9154:     if ($env{'request.course.sec'}) {
 9155:         $courseurl .= '/'.$env{'request.course.sec'};
 9156:     }
 9157:     return %blockers if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9158:     my %commblocks;
 9159:     if (ref($blocks) eq 'HASH') {
 9160:         %commblocks = %{$blocks};
 9161:     } else {
 9162:         %commblocks = &get_comm_blocks();
 9163:     }
 9164:     return %blockers unless (keys(%commblocks) > 0); 
 9165:     my $navmap = Apache::lonnavmaps::navmap->new();
 9166:     return %blockers unless (ref($navmap));
 9167:     my $now = time;
 9168:     foreach my $block (keys(%commblocks)) {
 9169:         if ($block =~ /^(\d+)____(\d+)$/) {
 9170:             my ($start,$end) = ($1,$2);
 9171:             if ($start <= $now && $end >= $now) {
 9172:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9173:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9174:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9175:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9176:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 9177:                             }
 9178:                         }
 9179:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9180:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9181:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9182:                             }
 9183:                         }
 9184:                     }
 9185:                 }
 9186:             }
 9187:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 9188:             my $item = $1;
 9189:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 9190:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 9191:                     my (@interval,$mapname);
 9192:                     my $type = 'map';
 9193:                     if ($item eq 'course') {
 9194:                         $type = 'course';
 9195:                         @interval=&EXT("resource.0.interval");
 9196:                     } else {
 9197:                         if ($item =~ /___\d+___/) {
 9198:                             $type = 'resource';
 9199:                             @interval=&EXT("resource.0.interval",$item);
 9200:                         } else {
 9201:                             $mapname = &deversion($item);
 9202:                             if (ref($navmap)) {
 9203:                                 my $timelimit = $navmap->get_mapparam(undef,$mapname,'0.interval');
 9204:                                 @interval = ($timelimit,'map');
 9205:                             }
 9206:                         }
 9207:                     }
 9208:                     if ($interval[0] =~ /^(\d+)/) {
 9209:                         my $timelimit = $1; 
 9210:                         my $first_access;
 9211:                         if ($type eq 'resource') {
 9212:                             $first_access=&get_first_access($interval[1],$item);
 9213:                         } elsif ($type eq 'map') {
 9214:                             $first_access=&get_first_access($interval[1],undef,$item);
 9215:                         } else {
 9216:                             $first_access=&get_first_access($interval[1]);
 9217:                         }
 9218:                         if ($first_access) {
 9219:                             my $timesup = $first_access+$timelimit;
 9220:                             if ($timesup > $now) {
 9221:                                 my $activeblock;
 9222:                                 if ($type eq 'resource') {
 9223:                                     if (ref($navmap)) {
 9224:                                         my $res = $navmap->getBySymb($item);
 9225:                                         if ($res->answerable()) {
 9226:                                             $activeblock = 1;
 9227:                                         }
 9228:                                     }
 9229:                                 } elsif ($type eq 'map') {
 9230:                                     my $mapsymb = &symbread($mapname,1);
 9231:                                     if (($mapsymb) && (ref($navmap))) {
 9232:                                         my $mapres = $navmap->getBySymb($mapsymb);
 9233:                                         if (ref($mapres)) {
 9234:                                             my $first = $mapres->map_start();
 9235:                                             my $finish = $mapres->map_finish();
 9236:                                             my $it = $navmap->getIterator($first,$finish,undef,0,0);
 9237:                                             if (ref($it)) {
 9238:                                                 my $res;
 9239:                                                 while ($res = $it->next(undef,1)) {
 9240:                                                     next unless (ref($res));
 9241:                                                     my $symb = $res->symb();
 9242:                                                     next if (($symb eq $mapsymb) || ($symb eq ''));
 9243:                                                     @interval=&EXT("resource.0.interval",$symb);
 9244:                                                     if ($interval[1] eq 'map') {
 9245:                                                         if ($res->answerable()) {
 9246:                                                             $activeblock = 1;
 9247:                                                             last;
 9248:                                                         }
 9249:                                                     }
 9250:                                                 }
 9251:                                             }
 9252:                                         }
 9253:                                     }
 9254:                                 }
 9255:                                 if ($activeblock) {
 9256:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 9257:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 9258:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 9259:                                          }
 9260:                                     }
 9261:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 9262:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 9263:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 9264:                                         }
 9265:                                     }
 9266:                                 }
 9267:                             }
 9268:                         }
 9269:                     }
 9270:                 }
 9271:             }
 9272:         }
 9273:     }
 9274:     return %blockers;
 9275: }
 9276: 
 9277: sub has_comm_blocking {
 9278:     my ($priv,$symb,$uri,$ignoresymbdb,$noenccheck,$blocked,$blocks) = @_;
 9279:     my @blockers;
 9280:     return unless ($env{'request.course.id'});
 9281:     return unless ($priv eq 'bre');
 9282:     return if ($env{'request.state'} eq 'construct');
 9283:     my $courseurl = &courseid_to_courseurl($env{'request.course.id'});
 9284:     if ($env{'request.course.sec'}) {
 9285:         $courseurl .= '/'.$env{'request.course.sec'};
 9286:     }
 9287:     return if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseurl} =~/evb\&([^\:]*)/);
 9288:     my %blockinfo;
 9289:     if (ref($blocks) eq 'HASH') {
 9290:         %blockinfo = &get_commblock_resources($blocks);
 9291:     } else {
 9292:         &load_all_blockers($env{'user.name'},$env{'user.domain'});
 9293:         %blockinfo = %cachedblockers;
 9294:     }
 9295:     return unless (keys(%blockinfo) > 0);
 9296:     my (%possibles,@symbs);
 9297:     if (!$symb) {
 9298:         $symb = &symbread($uri,1,1,1,\%possibles,$ignoresymbdb,$noenccheck);
 9299:     }
 9300:     if ($symb) {
 9301:         @symbs = ($symb);
 9302:     } elsif (keys(%possibles)) { 
 9303:         @symbs = keys(%possibles);
 9304:     }
 9305:     my $noblock;
 9306:     foreach my $symb (@symbs) {
 9307:         last if ($noblock);
 9308:         my ($map,$resid,$resurl)=&decode_symb($symb);
 9309:         foreach my $block (keys(%blockinfo)) {
 9310:             if ($block =~ /^firstaccess____(.+)$/) {
 9311:                 my $item = $1;
 9312:                 unless ($blocked) {
 9313:                     if (($item eq $map) || ($item eq $symb)) {
 9314:                         $noblock = 1;
 9315:                         last;
 9316:                     }
 9317:                 }
 9318:             }
 9319:             if (ref($blockinfo{$block}) eq 'HASH') {
 9320:                 if (ref($blockinfo{$block}{'resources'}) eq 'HASH') {
 9321:                     if ($blockinfo{$block}{'resources'}{$symb}) {
 9322:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9323:                             push(@blockers,$block);
 9324:                         }
 9325:                     }
 9326:                 }
 9327:                 if (ref($blockinfo{$block}{'maps'}) eq 'HASH') {
 9328:                     if ($blockinfo{$block}{'maps'}{$map}) {
 9329:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 9330:                             push(@blockers,$block);
 9331:                         }
 9332:                     }
 9333:                 }
 9334:             }
 9335:         }
 9336:     }
 9337:     unless ($noblock) { 
 9338:         return @blockers;
 9339:     }
 9340:     return;
 9341: }
 9342: }
 9343: 
 9344: sub deeplink_check {
 9345:     my ($priv,$symb,$uri) = @_;
 9346:     return unless ($env{'request.course.id'});
 9347:     return unless ($priv eq 'bre');
 9348:     return if ($env{'request.state'} eq 'construct');
 9349:     return if ($env{'request.role.adv'});
 9350:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 9351:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 9352:     my (%possibles,@symbs);
 9353:     if (!$symb) {
 9354:         $symb = &symbread($uri,1,1,1,\%possibles);
 9355:     }
 9356:     if ($symb) {
 9357:         @symbs = ($symb);
 9358:     } elsif (keys(%possibles)) {
 9359:         @symbs = keys(%possibles);
 9360:     }
 9361: 
 9362:     my ($deeplink_symb,$allow);
 9363:     if ($env{'request.deeplink.login'}) {
 9364:         $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 9365:     }
 9366:     foreach my $symb (@symbs) {
 9367:         last if ($allow);
 9368:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 9369:         if ($deeplink eq '') {
 9370:             $allow = 1;
 9371:         } else {
 9372:             my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
 9373:             if ($state ne 'only') {
 9374:                 $allow = 1;
 9375:             } else {
 9376:                 my $check_deeplink_entry;
 9377:                 if ($protect ne 'none') {
 9378:                     my ($acctype,$item) = split(/:/,$protect);
 9379:                     if (($acctype eq 'ltic') && ($env{'user.linkprotector'})) {
 9380:                         if (grep(/^\Q$item\Ec$/,split(/,/,$env{'user.linkprotector'}))) {
 9381:                             $check_deeplink_entry = 1
 9382:                         }
 9383:                     } elsif (($acctype eq 'ltid') && ($env{'user.linkprotector'})) {
 9384:                         if (grep(/^\Q$item\Ed$/,split(/,/,$env{'user.linkprotector'}))) {
 9385:                             $check_deeplink_entry = 1;
 9386:                         }
 9387:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 9388:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 9389:                             $check_deeplink_entry = 1;
 9390:                         }
 9391:                     }
 9392:                 }
 9393:                 if (($protect eq 'none') || ($check_deeplink_entry)) {
 9394:                     if ($scope eq 'res') {
 9395:                         if ($symb eq $deeplink_symb) {
 9396:                             $allow = 1;
 9397:                         }
 9398:                     } elsif (($scope eq 'map') || ($scope eq 'rec')) {
 9399:                         my ($map_from_symb,$map_from_login);
 9400:                         $map_from_symb = &deversion((&decode_symb($symb))[0]);
 9401:                         if ($deeplink_symb =~ /\.(page|sequence)$/) {
 9402:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[2]);
 9403:                         } else {
 9404:                             $map_from_login = &deversion((&decode_symb($deeplink_symb))[0]);
 9405:                         }
 9406:                         if (($map_from_symb) && ($map_from_login)) {
 9407:                             if ($map_from_symb eq $map_from_login) {
 9408:                                 $allow = 1;
 9409:                             } elsif ($scope eq 'rec') {
 9410:                                 my @recurseup = &get_map_hierarchy($map_from_symb,$env{'request.course.id'});
 9411:                                 if (grep(/^\Q$map_from_login\E$/,@recurseup)) {
 9412:                                     $allow = 1;
 9413:                                 }
 9414:                             }
 9415:                         }
 9416:                     }
 9417:                 }
 9418:             }
 9419:         }
 9420:     }
 9421:     return if ($allow);
 9422:     return 1;
 9423: }
 9424: 
 9425: # -------------------------------- Deversion and split uri into path an filename   
 9426: 
 9427: #
 9428: #   Removes the version from a URI and
 9429: #   splits it in to its filename and path to the filename.
 9430: #   Seems like File::Basename could have done this more clearly.
 9431: #   Parameters:
 9432: #      $uri   - input URI
 9433: #   Returns:
 9434: #     Two element list consisting of 
 9435: #     $pathname  - the URI up to and excluding the trailing /
 9436: #     $filename  - The part of the URI following the last /
 9437: #  NOTE:
 9438: #    Another realization of this is simply:
 9439: #    use File::Basename;
 9440: #    ...
 9441: #    $uri = shift;
 9442: #    $filename = basename($uri);
 9443: #    $path     = dirname($uri);
 9444: #    return ($filename, $path);
 9445: #
 9446: #     The implementation below is probably faster however.
 9447: #
 9448: sub split_uri_for_cond {
 9449:     my $uri=&deversion(&declutter(shift));
 9450:     my @uriparts=split(/\//,$uri);
 9451:     my $filename=pop(@uriparts);
 9452:     my $pathname=join('/',@uriparts);
 9453:     return ($pathname,$filename);
 9454: }
 9455: # --------------------------------------------------- Is a resource on the map?
 9456: 
 9457: sub is_on_map {
 9458:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9459:     #Trying to find the conditional for the file
 9460:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9461: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9462:     if ($match) {
 9463: 	return (1,$1);
 9464:     } else {
 9465: 	return (0,0);
 9466:     }
 9467: }
 9468: 
 9469: # --------------------------------------------------------- Get symb from alias
 9470: 
 9471: sub get_symb_from_alias {
 9472:     my $symb=shift;
 9473:     my ($map,$resid,$url)=&decode_symb($symb);
 9474: # Already is a symb
 9475:     if ($url) { return $symb; }
 9476: # Must be an alias
 9477:     my $aliassymb='';
 9478:     my %bighash;
 9479:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9480:                             &GDBM_READER(),0640)) {
 9481:         my $rid=$bighash{'mapalias_'.$symb};
 9482: 	if ($rid) {
 9483: 	    my ($mapid,$resid)=split(/\./,$rid);
 9484: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9485: 				    $resid,$bighash{'src_'.$rid});
 9486: 	}
 9487:         untie %bighash;
 9488:     }
 9489:     return $aliassymb;
 9490: }
 9491: 
 9492: # ----------------------------------------------------------------- Define Role
 9493: 
 9494: sub definerole {
 9495:   if (allowed('mcr','/')) {
 9496:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9497:     foreach my $role (split(':',$sysrole)) {
 9498: 	my ($crole,$cqual)=split(/\&/,$role);
 9499:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9500:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9501: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9502:                return "refused:s:$crole&$cqual"; 
 9503:             }
 9504:         }
 9505:     }
 9506:     foreach my $role (split(':',$domrole)) {
 9507: 	my ($crole,$cqual)=split(/\&/,$role);
 9508:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9509:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9510: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9511:                return "refused:d:$crole&$cqual"; 
 9512:             }
 9513:         }
 9514:     }
 9515:     foreach my $role (split(':',$courole)) {
 9516: 	my ($crole,$cqual)=split(/\&/,$role);
 9517:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9518:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9519: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9520:                return "refused:c:$crole&$cqual"; 
 9521:             }
 9522:         }
 9523:     }
 9524:     my $uhome;
 9525:     if (($uname ne '') && ($udom ne '')) {
 9526:         $uhome = &homeserver($uname,$udom);
 9527:         return $uhome if ($uhome eq 'no_host');
 9528:     } else {
 9529:         $uname = $env{'user.name'};
 9530:         $udom = $env{'user.domain'};
 9531:         $uhome = $env{'user.home'};
 9532:     }
 9533:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9534:                 "$udom:$uname:rolesdef_$rolename=".
 9535:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9536:     return reply($command,$uhome);
 9537:   } else {
 9538:     return 'refused';
 9539:   }
 9540: }
 9541: 
 9542: # ---------------- Make a metadata query against the network of library servers
 9543: 
 9544: sub metadata_query {
 9545:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9546:     my %rhash;
 9547:     my %libserv = &all_library();
 9548:     my @server_list = (defined($server_array) ? @$server_array
 9549:                                               : keys(%libserv) );
 9550:     for my $server (@server_list) {
 9551:         my $domains = ''; 
 9552:         if (ref($domains_hash) eq 'HASH') {
 9553:             $domains = $domains_hash->{$server}; 
 9554:         }
 9555: 	unless ($custom or $customshow) {
 9556: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9557: 	    $rhash{$server}=$reply;
 9558: 	}
 9559: 	else {
 9560: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9561: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9562: 			     $server);
 9563: 	    $rhash{$server}=$reply;
 9564: 	}
 9565:     }
 9566:     return \%rhash;
 9567: }
 9568: 
 9569: # ----------------------------------------- Send log queries and wait for reply
 9570: 
 9571: sub log_query {
 9572:     my ($uname,$udom,$query,%filters)=@_;
 9573:     my $uhome=&homeserver($uname,$udom);
 9574:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9575:     my $uhost=&hostname($uhome);
 9576:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9577:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9578:                        $uhome);
 9579:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9580:     return get_query_reply($queryid);
 9581: }
 9582: 
 9583: # -------------------------- Update MySQL table for portfolio file
 9584: 
 9585: sub update_portfolio_table {
 9586:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9587:     if ($group ne '') {
 9588:         $file_name =~s /^\Q$group\E//;
 9589:     }
 9590:     my $homeserver = &homeserver($uname,$udom);
 9591:     my $queryid=
 9592:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9593:                ':'.&escape($file_name).':'.$action,$homeserver);
 9594:     my $reply = &get_query_reply($queryid);
 9595:     return $reply;
 9596: }
 9597: 
 9598: # -------------------------- Update MySQL allusers table
 9599: 
 9600: sub update_allusers_table {
 9601:     my ($uname,$udom,$names) = @_;
 9602:     my $homeserver = &homeserver($uname,$udom);
 9603:     my $queryid=
 9604:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9605:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9606:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9607:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9608:                'generation='.&escape($names->{'generation'}).'%%'.
 9609:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9610:                'id='.&escape($names->{'id'}),$homeserver);
 9611:     return;
 9612: }
 9613: 
 9614: # ------- Request retrieval of institutional classlists for course(s)
 9615: 
 9616: sub fetch_enrollment_query {
 9617:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9618:     my ($homeserver,$sleep,$loopmax);
 9619:     my $maxtries = 1;
 9620:     if ($context eq 'automated') {
 9621:         $homeserver = $perlvar{'lonHostID'};
 9622:         $sleep = 2;
 9623:         $loopmax = 100;
 9624:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9625:     } else {
 9626:         $homeserver = &homeserver($cnum,$dom);
 9627:     }
 9628:     my $host=&hostname($homeserver);
 9629:     my $cmd = '';
 9630:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9631:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9632:     }
 9633:     $cmd =~ s/%%$//;
 9634:     $cmd = &escape($cmd);
 9635:     my $query = 'fetchenrollment';
 9636:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9637:     unless ($queryid=~/^\Q$host\E\_/) { 
 9638:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9639:         return 'error: '.$queryid;
 9640:     }
 9641:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9642:     my $tries = 1;
 9643:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9644:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9645:         $tries ++;
 9646:     }
 9647:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9648:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9649:     } else {
 9650:         my @responses = split(/:/,$reply);
 9651:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9652:             foreach my $line (@responses) {
 9653:                 my ($key,$value) = split(/=/,$line,2);
 9654:                 $$replyref{$key} = $value;
 9655:             }
 9656:         } else {
 9657:             my $pathname = LONCAPA::tempdir();
 9658:             foreach my $line (@responses) {
 9659:                 my ($key,$value) = split(/=/,$line);
 9660:                 $$replyref{$key} = $value;
 9661:                 if ($value > 0) {
 9662:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9663:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9664:                         my $destname = $pathname.'/'.$filename;
 9665:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9666:                         if ($xml_classlist =~ /^error/) {
 9667:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9668:                         } else {
 9669:                             if ( open(FILE,">",$destname) ) {
 9670:                                 print FILE &unescape($xml_classlist);
 9671:                                 close(FILE);
 9672:                             } else {
 9673:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9674:                             }
 9675:                         }
 9676:                     }
 9677:                 }
 9678:             }
 9679:         }
 9680:         return 'ok';
 9681:     }
 9682:     return 'error';
 9683: }
 9684: 
 9685: sub get_query_reply {
 9686:     my ($queryid,$sleep,$loopmax) = @_;
 9687:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9688:         $sleep = 0.2;
 9689:     }
 9690:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9691:         $loopmax = 100;
 9692:     }
 9693:     my $replyfile=LONCAPA::tempdir().$queryid;
 9694:     my $reply='';
 9695:     for (1..$loopmax) {
 9696: 	sleep($sleep);
 9697:         if (-e $replyfile.'.end') {
 9698: 	    if (open(my $fh,"<",$replyfile)) {
 9699: 		$reply = join('',<$fh>);
 9700: 		close($fh);
 9701: 	   } else { return 'error: reply_file_error'; }
 9702:            return &unescape($reply);
 9703: 	}
 9704:     }
 9705:     return 'timeout:'.$queryid;
 9706: }
 9707: 
 9708: sub courselog_query {
 9709: #
 9710: # possible filters:
 9711: # url: url or symb
 9712: # username
 9713: # domain
 9714: # action: view, submit, grade
 9715: # start: timestamp
 9716: # end: timestamp
 9717: #
 9718:     my (%filters)=@_;
 9719:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9720:     if ($filters{'url'}) {
 9721: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9722:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9723:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9724:     }
 9725:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9726:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9727:     return &log_query($cname,$cdom,'courselog',%filters);
 9728: }
 9729: 
 9730: sub userlog_query {
 9731: #
 9732: # possible filters:
 9733: # action: log check role
 9734: # start: timestamp
 9735: # end: timestamp
 9736: #
 9737:     my ($uname,$udom,%filters)=@_;
 9738:     return &log_query($uname,$udom,'userlog',%filters);
 9739: }
 9740: 
 9741: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9742: 
 9743: sub auto_run {
 9744:     my ($cnum,$cdom) = @_;
 9745:     my $response = 0;
 9746:     my $settings;
 9747:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9748:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9749:         $settings = $domconfig{'autoenroll'};
 9750:         if ($settings->{'run'} eq '1') {
 9751:             $response = 1;
 9752:         }
 9753:     } else {
 9754:         my $homeserver;
 9755:         if (&is_course($cdom,$cnum)) {
 9756:             $homeserver = &homeserver($cnum,$cdom);
 9757:         } else {
 9758:             $homeserver = &domain($cdom,'primary');
 9759:         }
 9760:         if ($homeserver ne 'no_host') {
 9761:             $response = &reply('autorun:'.$cdom,$homeserver);
 9762:         }
 9763:     }
 9764:     return $response;
 9765: }
 9766: 
 9767: sub auto_get_sections {
 9768:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9769:     my $homeserver;
 9770:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9771:         $homeserver = &homeserver($cnum,$cdom);
 9772:     }
 9773:     if (!defined($homeserver)) { 
 9774:         if ($cdom =~ /^$match_domain$/) {
 9775:             $homeserver = &domain($cdom,'primary');
 9776:         }
 9777:     }
 9778:     my @secs;
 9779:     if (defined($homeserver)) {
 9780:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9781:         unless ($response eq 'refused') {
 9782:             @secs = split(/:/,$response);
 9783:         }
 9784:     }
 9785:     return @secs;
 9786: }
 9787: 
 9788: sub auto_new_course {
 9789:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9790:     my $homeserver = &homeserver($cnum,$cdom);
 9791:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9792:     return $response;
 9793: }
 9794: 
 9795: sub auto_validate_courseID {
 9796:     my ($cnum,$cdom,$inst_course_id) = @_;
 9797:     my $homeserver = &homeserver($cnum,$cdom);
 9798:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9799:     return $response;
 9800: }
 9801: 
 9802: sub auto_validate_instcode {
 9803:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9804:     my ($homeserver,$response);
 9805:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9806:         $homeserver = &homeserver($cnum,$cdom);
 9807:     }
 9808:     if (!defined($homeserver)) {
 9809:         if ($cdom =~ /^$match_domain$/) {
 9810:             $homeserver = &domain($cdom,'primary');
 9811:         }
 9812:     }
 9813:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9814:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9815:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9816:     return ($outcome,$description,$defaultcredits);
 9817: }
 9818: 
 9819: sub auto_validate_inst_crosslist {
 9820:     my ($cnum,$cdom,$instcode,$inst_xlist,$coowner) = @_;
 9821:     my ($homeserver,$response);
 9822:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9823:         $homeserver = &homeserver($cnum,$cdom);
 9824:     }
 9825:     if (!defined($homeserver)) {
 9826:         if ($cdom =~ /^$match_domain$/) {
 9827:             $homeserver = &domain($cdom,'primary');
 9828:         }
 9829:     }
 9830:     unless (($homeserver eq '') || ($homeserver eq 'no_host')) {
 9831:         $response=&reply('autovalidateinstcrosslist:'.$cdom.':'.
 9832:                          &escape($instcode).':'.&escape($inst_xlist).':'.
 9833:                          &escape($coowner),$homeserver);
 9834:     }
 9835:     return $response;
 9836: }
 9837: 
 9838: sub auto_create_password {
 9839:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9840:     my ($homeserver,$response);
 9841:     my $create_passwd = 0;
 9842:     my $authchk = '';
 9843:     if ($udom =~ /^$match_domain$/) {
 9844:         $homeserver = &domain($udom,'primary');
 9845:     }
 9846:     if ($homeserver eq '') {
 9847:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9848:             $homeserver = &homeserver($cnum,$cdom);
 9849:         }
 9850:     }
 9851:     if ($homeserver eq '') {
 9852:         $authchk = 'nodomain';
 9853:     } else {
 9854:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9855:         if ($response eq 'refused') {
 9856:             $authchk = 'refused';
 9857:         } else {
 9858:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9859:         }
 9860:     }
 9861:     return ($authparam,$create_passwd,$authchk);
 9862: }
 9863: 
 9864: sub auto_photo_permission {
 9865:     my ($cnum,$cdom,$students) = @_;
 9866:     my $homeserver = &homeserver($cnum,$cdom);
 9867:     my ($outcome,$perm_reqd,$conditions) = 
 9868: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9869:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9870: 	return (undef,undef);
 9871:     }
 9872:     return ($outcome,$perm_reqd,$conditions);
 9873: }
 9874: 
 9875: sub auto_checkphotos {
 9876:     my ($uname,$udom,$pid) = @_;
 9877:     my $homeserver = &homeserver($uname,$udom);
 9878:     my ($result,$resulttype);
 9879:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9880: 				   &escape($uname).':'.&escape($pid),
 9881: 				   $homeserver));
 9882:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9883: 	return (undef,undef);
 9884:     }
 9885:     if ($outcome) {
 9886:         ($result,$resulttype) = split(/:/,$outcome);
 9887:     } 
 9888:     return ($result,$resulttype);
 9889: }
 9890: 
 9891: sub auto_photochoice {
 9892:     my ($cnum,$cdom) = @_;
 9893:     my $homeserver = &homeserver($cnum,$cdom);
 9894:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9895: 						       &escape($cdom),
 9896: 						       $homeserver)));
 9897:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9898: 	return (undef,undef);
 9899:     }
 9900:     return ($update,$comment);
 9901: }
 9902: 
 9903: sub auto_photoupdate {
 9904:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9905:     my $homeserver = &homeserver($cnum,$dom);
 9906:     my $host=&hostname($homeserver);
 9907:     my $cmd = '';
 9908:     my $maxtries = 1;
 9909:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9910:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9911:     }
 9912:     $cmd =~ s/%%$//;
 9913:     $cmd = &escape($cmd);
 9914:     my $query = 'institutionalphotos';
 9915:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9916:     unless ($queryid=~/^\Q$host\E\_/) {
 9917:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9918:         return 'error: '.$queryid;
 9919:     }
 9920:     my $reply = &get_query_reply($queryid);
 9921:     my $tries = 1;
 9922:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9923:         $reply = &get_query_reply($queryid);
 9924:         $tries ++;
 9925:     }
 9926:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9927:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9928:     } else {
 9929:         my @responses = split(/:/,$reply);
 9930:         my $outcome = shift(@responses); 
 9931:         foreach my $item (@responses) {
 9932:             my ($key,$value) = split(/=/,$item);
 9933:             $$photo{$key} = $value;
 9934:         }
 9935:         return $outcome;
 9936:     }
 9937:     return 'error';
 9938: }
 9939: 
 9940: sub auto_instcode_format {
 9941:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9942: 	$cat_order) = @_;
 9943:     my $courses = '';
 9944:     my @homeservers;
 9945:     if ($caller eq 'global') {
 9946: 	my %servers = &get_servers($codedom,'library');
 9947: 	foreach my $tryserver (keys(%servers)) {
 9948: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9949: 		push(@homeservers,$tryserver);
 9950: 	    }
 9951:         }
 9952:     } elsif ($caller eq 'requests') {
 9953:         if ($codedom =~ /^$match_domain$/) {
 9954:             my $chome = &domain($codedom,'primary');
 9955:             unless ($chome eq 'no_host') {
 9956:                 push(@homeservers,$chome);
 9957:             }
 9958:         }
 9959:     } else {
 9960:         push(@homeservers,&homeserver($caller,$codedom));
 9961:     }
 9962:     foreach my $code (keys(%{$instcodes})) {
 9963:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9964:     }
 9965:     chop($courses);
 9966:     my $ok_response = 0;
 9967:     my $response;
 9968:     while (@homeservers > 0 && $ok_response == 0) {
 9969:         my $server = shift(@homeservers); 
 9970:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9971:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9972:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9973: 		split(/:/,$response);
 9974:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9975:             push(@{$codetitles},&str2array($codetitles_str));
 9976:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9977:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9978:             $ok_response = 1;
 9979:         }
 9980:     }
 9981:     if ($ok_response) {
 9982:         return 'ok';
 9983:     } else {
 9984:         return $response;
 9985:     }
 9986: }
 9987: 
 9988: sub auto_instcode_defaults {
 9989:     my ($domain,$returnhash,$code_order) = @_;
 9990:     my @homeservers;
 9991: 
 9992:     my %servers = &get_servers($domain,'library');
 9993:     foreach my $tryserver (keys(%servers)) {
 9994: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9995: 	    push(@homeservers,$tryserver);
 9996: 	}
 9997:     }
 9998: 
 9999:     my $response;
10000:     foreach my $server (@homeservers) {
10001:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
10002:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10003: 	
10004: 	foreach my $pair (split(/\&/,$response)) {
10005: 	    my ($name,$value)=split(/\=/,$pair);
10006: 	    if ($name eq 'code_order') {
10007: 		@{$code_order} = split(/\&/,&unescape($value));
10008: 	    } else {
10009: 		$returnhash->{&unescape($name)}=&unescape($value);
10010: 	    }
10011: 	}
10012: 	return 'ok';
10013:     }
10014: 
10015:     return $response;
10016: }
10017: 
10018: sub auto_possible_instcodes {
10019:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
10020:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
10021:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10022:         return;
10023:     }
10024:     my (@homeservers,$uhome);
10025:     if (defined(&domain($domain,'primary'))) {
10026:         $uhome=&domain($domain,'primary');
10027:         push(@homeservers,&domain($domain,'primary'));
10028:     } else {
10029:         my %servers = &get_servers($domain,'library');
10030:         foreach my $tryserver (keys(%servers)) {
10031:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10032:                 push(@homeservers,$tryserver);
10033:             }
10034:         }
10035:     }
10036:     my $response;
10037:     foreach my $server (@homeservers) {
10038:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
10039:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
10040:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
10041:             split(':',$response);
10042:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
10043:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
10044:         foreach my $item (split('&',$cat_title)) {   
10045:             my ($name,$value)=split('=',$item);
10046:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
10047:         }
10048:         foreach my $item (split('&',$cat_order)) {
10049:             my ($name,$value)=split('=',$item);
10050:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
10051:         }
10052:         return 'ok';
10053:     }
10054:     return $response;
10055: }
10056: 
10057: sub auto_courserequest_checks {
10058:     my ($dom) = @_;
10059:     my ($homeserver,%validations);
10060:     if ($dom =~ /^$match_domain$/) {
10061:         $homeserver = &domain($dom,'primary');
10062:     }
10063:     unless ($homeserver eq 'no_host') {
10064:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
10065:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10066:             my @items = split(/&/,$response);
10067:             foreach my $item (@items) {
10068:                 my ($key,$value) = split('=',$item);
10069:                 $validations{&unescape($key)} = &thaw_unescape($value);
10070:             }
10071:         }
10072:     }
10073:     return %validations; 
10074: }
10075: 
10076: sub auto_courserequest_validation {
10077:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
10078:     my ($homeserver,$response);
10079:     if ($dom =~ /^$match_domain$/) {
10080:         $homeserver = &domain($dom,'primary');
10081:     }
10082:     unless ($homeserver eq 'no_host') {
10083:         my $customdata;
10084:         if (ref($custominfo) eq 'HASH') {
10085:             $customdata = &freeze_escape($custominfo);
10086:         }
10087:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
10088:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
10089:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
10090:                                     $customdata,$homeserver));
10091:     }
10092:     return $response;
10093: }
10094: 
10095: sub auto_validate_class_sec {
10096:     my ($cdom,$cnum,$owners,$inst_class) = @_;
10097:     my $homeserver = &homeserver($cnum,$cdom);
10098:     my $ownerlist;
10099:     if (ref($owners) eq 'ARRAY') {
10100:         $ownerlist = join(',',@{$owners});
10101:     } else {
10102:         $ownerlist = $owners;
10103:     }
10104:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
10105:                         &escape($ownerlist).':'.$cdom,$homeserver);
10106:     return $response;
10107: }
10108: 
10109: sub auto_instsec_reformat {
10110:     my ($cdom,$action,$instsecref) = @_;
10111:     return unless(($action eq 'clutter') || ($action eq 'declutter'));
10112:     my @homeservers;
10113:     if (defined(&domain($cdom,'primary'))) {
10114:         push(@homeservers,&domain($cdom,'primary'));
10115:     } else {
10116:         my %servers = &get_servers($cdom,'library');
10117:         foreach my $tryserver (keys(%servers)) {
10118:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
10119:                 push(@homeservers,$tryserver);
10120:             }
10121:         }
10122:     }
10123:     my $response;
10124:     my %reformatted = %{$instsecref};
10125:     foreach my $server (@homeservers) {
10126:         if (ref($instsecref) eq 'HASH') {
10127:             my $info = &freeze_escape($instsecref);
10128:             my $response=&reply('autoinstsecreformat:'.$cdom.':'.
10129:                                 $action.':'.$info,$server);
10130:             next if ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/);
10131:             my @items = split(/&/,$response);
10132:             foreach my $item (@items) {
10133:                 my ($key,$value) = split(/=/,$item);
10134:                 $reformatted{&unescape($key)} = &thaw_unescape($value);
10135:             }
10136:         }
10137:     }
10138:     return %reformatted;
10139: }
10140: 
10141: sub auto_validate_instclasses {
10142:     my ($cdom,$cnum,$owners,$classesref) = @_;
10143:     my ($homeserver,%validations);
10144:     $homeserver = &homeserver($cnum,$cdom);
10145:     unless ($homeserver eq 'no_host') {
10146:         my $ownerlist;
10147:         if (ref($owners) eq 'ARRAY') {
10148:             $ownerlist = join(',',@{$owners});
10149:         } else {
10150:             $ownerlist = $owners;
10151:         }
10152:         if (ref($classesref) eq 'HASH') {
10153:             my $classes = &freeze_escape($classesref);
10154:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
10155:                                 ':'.$cdom.':'.$classes,$homeserver);
10156:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10157:                 my @items = split(/&/,$response);
10158:                 foreach my $item (@items) {
10159:                     my ($key,$value) = split('=',$item);
10160:                     $validations{&unescape($key)} = &thaw_unescape($value);
10161:                 }
10162:             }
10163:         }
10164:     }
10165:     return %validations;
10166: }
10167: 
10168: sub auto_crsreq_update {
10169:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
10170:         $code,$accessstart,$accessend,$inbound) = @_;
10171:     my ($homeserver,%crsreqresponse);
10172:     if ($cdom =~ /^$match_domain$/) {
10173:         $homeserver = &domain($cdom,'primary');
10174:     }
10175:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10176:         my $info;
10177:         if (ref($inbound) eq 'HASH') {
10178:             $info = &freeze_escape($inbound);
10179:         }
10180:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
10181:                             ':'.&escape($action).':'.&escape($ownername).':'.
10182:                             &escape($ownerdomain).':'.&escape($fullname).':'.
10183:                             &escape($title).':'.&escape($code).':'.
10184:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
10185:                             $homeserver);
10186:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
10187:             my @items = split(/&/,$response);
10188:             foreach my $item (@items) {
10189:                 my ($key,$value) = split('=',$item);
10190:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
10191:             }
10192:         }
10193:     }
10194:     return \%crsreqresponse;
10195: }
10196: 
10197: sub auto_export_grades {
10198:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
10199:     my ($homeserver,%exportresponse);
10200:     if ($cdom =~ /^$match_domain$/) {
10201:         $homeserver = &domain($cdom,'primary');
10202:     }
10203:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
10204:         my $info;
10205:         if (ref($inforef) eq 'HASH') {
10206:             $info = &freeze_escape($inforef);
10207:         }
10208:         if (ref($gradesref) eq 'HASH') {
10209:             my $grades = &freeze_escape($gradesref);
10210:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
10211:                                 $info.':'.$grades,$homeserver);
10212:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
10213:                 my @items = split(/&/,$response);
10214:                 foreach my $item (@items) {
10215:                     my ($key,$value) = split('=',$item);
10216:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
10217:                 }
10218:             }
10219:         }
10220:     }
10221:     return \%exportresponse;
10222: }
10223: 
10224: sub check_instcode_cloning {
10225:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
10226:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
10227:         return;
10228:     }
10229:     my $canclone;
10230:     if (@{$code_order} > 0) {
10231:         my $instcoderegexp ='^';
10232:         my @clonecodes = split(/\&/,$cloner);
10233:         foreach my $item (@{$code_order}) {
10234:             if (grep(/^\Q$item\E=/,@clonecodes)) {
10235:                 foreach my $pair (@clonecodes) {
10236:                     my ($key,$val) = split(/\=/,$pair,2);
10237:                     $val = &unescape($val);
10238:                     if ($key eq $item) {
10239:                         $instcoderegexp .= '('.$val.')';
10240:                         last;
10241:                     }
10242:                 }
10243:             } else {
10244:                 $instcoderegexp .= $codedefaults->{$item};
10245:             }
10246:         }
10247:         $instcoderegexp .= '$';
10248:         my (@from,@to);
10249:         eval {
10250:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
10251:                (@to) = ($clonetocode =~ /$instcoderegexp/);
10252:         };
10253:         if ((@from > 0) && (@to > 0)) {
10254:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10255:             if (!@diffs) {
10256:                 $canclone = 1;
10257:             }
10258:         }
10259:     }
10260:     return $canclone;
10261: }
10262: 
10263: sub default_instcode_cloning {
10264:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
10265:     my (%codedefaults,@code_order,$canclone);
10266:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
10267:         %codedefaults = %{$codedefaultsref};
10268:         @code_order = @{$codeorderref};
10269:     } elsif ($clonedom) {
10270:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
10271:     }
10272:     if (($domdefclone) && (@code_order)) {
10273:         my @clonecodes = split(/\+/,$domdefclone);
10274:         my $instcoderegexp ='^';
10275:         foreach my $item (@code_order) {
10276:             if (grep(/^\Q$item\E$/,@clonecodes)) {
10277:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
10278:             } else {
10279:                 $instcoderegexp .= $codedefaults{$item};
10280:             }
10281:         }
10282:         $instcoderegexp .= '$';
10283:         my (@from,@to);
10284:         eval {
10285:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
10286:             (@to) = ($clonetocode =~ /$instcoderegexp/);
10287:         };
10288:         if ((@from > 0) && (@to > 0)) {
10289:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
10290:             if (!@diffs) {
10291:                 $canclone = 1;
10292:             }
10293:         }
10294:     }
10295:     return $canclone;
10296: }
10297: 
10298: # ------------------------------------------------------- Course Group routines
10299: 
10300: sub get_coursegroups {
10301:     my ($cdom,$cnum,$group,$namespace) = @_;
10302:     return(&dump($namespace,$cdom,$cnum,$group));
10303: }
10304: 
10305: sub modify_coursegroup {
10306:     my ($cdom,$cnum,$groupsettings) = @_;
10307:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
10308: }
10309: 
10310: sub toggle_coursegroup_status {
10311:     my ($cdom,$cnum,$group,$action) = @_;
10312:     my ($from_namespace,$to_namespace);
10313:     if ($action eq 'delete') {
10314:         $from_namespace = 'coursegroups';
10315:         $to_namespace = 'deleted_groups';
10316:     } else {
10317:         $from_namespace = 'deleted_groups';
10318:         $to_namespace = 'coursegroups';
10319:     }
10320:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
10321:     if (my $tmp = &error(%curr_group)) {
10322:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
10323:         return ('read error',$tmp);
10324:     } else {
10325:         my %savedsettings = %curr_group; 
10326:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
10327:         my $deloutcome;
10328:         if ($result eq 'ok') {
10329:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
10330:         } else {
10331:             return ('write error',$result);
10332:         }
10333:         if ($deloutcome eq 'ok') {
10334:             return 'ok';
10335:         } else {
10336:             return ('delete error',$deloutcome);
10337:         }
10338:     }
10339: }
10340: 
10341: sub modify_group_roles {
10342:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
10343:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
10344:     my $role = 'gr/'.&escape($userprivs);
10345:     my ($uname,$udom) = split(/:/,$user);
10346:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
10347:     if ($result eq 'ok') {
10348:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
10349:     }
10350:     return $result;
10351: }
10352: 
10353: sub modify_coursegroup_membership {
10354:     my ($cdom,$cnum,$membership) = @_;
10355:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
10356:     return $result;
10357: }
10358: 
10359: sub get_active_groups {
10360:     my ($udom,$uname,$cdom,$cnum) = @_;
10361:     my $now = time;
10362:     my %groups = ();
10363:     foreach my $key (keys(%env)) {
10364:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
10365:             my ($start,$end) = split(/\./,$env{$key});
10366:             if (($end!=0) && ($end<$now)) { next; }
10367:             if (($start!=0) && ($start>$now)) { next; }
10368:             if ($1 eq $cdom && $2 eq $cnum) {
10369:                 $groups{$3} = $env{$key} ;
10370:             }
10371:         }
10372:     }
10373:     return %groups;
10374: }
10375: 
10376: sub get_group_membership {
10377:     my ($cdom,$cnum,$group) = @_;
10378:     return(&dump('groupmembership',$cdom,$cnum,$group));
10379: }
10380: 
10381: sub get_users_groups {
10382:     my ($udom,$uname,$courseid) = @_;
10383:     my @usersgroups;
10384:     my $cachetime=1800;
10385: 
10386:     my $hashid="$udom:$uname:$courseid";
10387:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
10388:     if (defined($cached)) {
10389:         @usersgroups = split(/:/,$grouplist);
10390:     } else {  
10391:         $grouplist = '';
10392:         my $courseurl = &courseid_to_courseurl($courseid);
10393:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
10394:         my $access_end = $env{'course.'.$courseid.
10395:                               '.default_enrollment_end_date'};
10396:         my $now = time;
10397:         foreach my $key (keys(%roleshash)) {
10398:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
10399:                 my $group = $1;
10400:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
10401:                     my $start = $2;
10402:                     my $end = $1;
10403:                     if ($start == -1) { next; } # deleted from group
10404:                     if (($start!=0) && ($start>$now)) { next; }
10405:                     if (($end!=0) && ($end<$now)) {
10406:                         if ($access_end && $access_end < $now) {
10407:                             if ($access_end - $end < 86400) {
10408:                                 push(@usersgroups,$group);
10409:                             }
10410:                         }
10411:                         next;
10412:                     }
10413:                     push(@usersgroups,$group);
10414:                 }
10415:             }
10416:         }
10417:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
10418:         $grouplist = join(':',@usersgroups);
10419:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
10420:     }
10421:     return @usersgroups;
10422: }
10423: 
10424: sub devalidate_getgroups_cache {
10425:     my ($udom,$uname,$cdom,$cnum)=@_;
10426:     my $courseid = $cdom.'_'.$cnum;
10427: 
10428:     my $hashid="$udom:$uname:$courseid";
10429:     &devalidate_cache_new('getgroups',$hashid);
10430: }
10431: 
10432: # ------------------------------------------------------------------ Plain Text
10433: 
10434: sub plaintext {
10435:     my ($short,$type,$cid,$forcedefault) = @_;
10436:     if ($short =~ m{^cr/}) {
10437: 	return (split('/',$short))[-1];
10438:     }
10439:     if (!defined($cid)) {
10440:         $cid = $env{'request.course.id'};
10441:     }
10442:     my %rolenames = (
10443:                       Course    => 'std',
10444:                       Community => 'alt1',
10445:                       Placement => 'std',
10446:                     );
10447:     if ($cid ne '') {
10448:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
10449:             unless ($forcedefault) {
10450:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
10451:                 &Apache::lonlocal::mt_escape(\$roletext);
10452:                 return &Apache::lonlocal::mt($roletext);
10453:             }
10454:         }
10455:     }
10456:     if ((defined($type)) && (defined($rolenames{$type})) &&
10457:         (defined($rolenames{$type})) && 
10458:         (defined($prp{$short}{$rolenames{$type}}))) {
10459:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
10460:     } elsif ($cid ne '') {
10461:         my $crstype = $env{'course.'.$cid.'.type'};
10462:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
10463:             (defined($prp{$short}{$rolenames{$crstype}}))) {
10464:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
10465:         }
10466:     }
10467:     return &Apache::lonlocal::mt($prp{$short}{'std'});
10468: }
10469: 
10470: # ----------------------------------------------------------------- Assign Role
10471: 
10472: sub assignrole {
10473:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
10474:         $context)=@_;
10475:     my $mrole;
10476:     if ($role =~ /^cr\//) {
10477:         my $cwosec=$url;
10478:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10479:         if ((!&allowed('ccr',$cwosec)) && (!&allowed('ccr',$udom))) {
10480:            my $refused = 1;
10481:            if ($context eq 'requestcourses') {
10482:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
10483:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
10484:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
10485:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10486:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10487:                            if ($crsenv{'internal.courseowner'} eq
10488:                                $env{'user.name'}.':'.$env{'user.domain'}) {
10489:                                $refused = '';
10490:                            }
10491:                        }
10492:                    }
10493:                }
10494:            }
10495:            if ($refused) {
10496:                &logthis('Refused custom assignrole: '.
10497:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
10498:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
10499:                return 'refused';
10500:            }
10501:         }
10502:         $mrole='cr';
10503:     } elsif ($role =~ /^gr\//) {
10504:         my $cwogrp=$url;
10505:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10506:         unless (&allowed('mdg',$cwogrp)) {
10507:             &logthis('Refused group assignrole: '.
10508:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10509:                     $env{'user.name'}.' at '.$env{'user.domain'});
10510:             return 'refused';
10511:         }
10512:         $mrole='gr';
10513:     } else {
10514:         my $cwosec=$url;
10515:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10516:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10517:             my $refused;
10518:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10519:                 if (!(&allowed('c'.$role,$url))) {
10520:                     $refused = 1;
10521:                 }
10522:             } else {
10523:                 $refused = 1;
10524:             }
10525:             if ($refused) {
10526:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10527:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10528:                     my %crsenv;
10529:                     if ($role eq 'cc' || $role eq 'co') {
10530:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10531:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10532:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10533:                                 if ($crsenv{'internal.courseowner'} eq 
10534:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10535:                                     $refused = '';
10536:                                 }
10537:                             }
10538:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10539:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10540:                                 if ($crsenv{'internal.courseowner'} eq 
10541:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10542:                                     $refused = '';
10543:                                 }
10544:                             }
10545:                         }
10546:                     }
10547:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10548:                     if ($role eq 'st') {
10549:                         $refused = '';
10550:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10551:                         $refused = '';
10552:                     }
10553:                 } elsif ($context eq 'requestcourses') {
10554:                     my @possroles = ('st','ta','ep','in','cc','co');
10555:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10556:                         my $wrongcc;
10557:                         if ($cnum =~ /^$match_community$/) {
10558:                             $wrongcc = 1 if ($role eq 'cc');
10559:                         } else {
10560:                             $wrongcc = 1 if ($role eq 'co');
10561:                         }
10562:                         unless ($wrongcc) {
10563:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10564:                             if ($crsenv{'internal.courseowner'} eq 
10565:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10566:                                 $refused = '';
10567:                             }
10568:                         }
10569:                     }
10570:                 } elsif ($context eq 'requestauthor') {
10571:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10572:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10573:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10574:                             $refused = '';
10575:                         } else {
10576:                             my %domdefaults = &get_domain_defaults($udom);
10577:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10578:                                 my $checkbystatus;
10579:                                 if ($env{'user.adv'}) { 
10580:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10581:                                     if ($disposition eq 'automatic') {
10582:                                         $refused = '';
10583:                                     } elsif ($disposition eq '') {
10584:                                         $checkbystatus = 1;
10585:                                     } 
10586:                                 } else {
10587:                                     $checkbystatus = 1;
10588:                                 }
10589:                                 if ($checkbystatus) {
10590:                                     if ($env{'environment.inststatus'}) {
10591:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10592:                                         foreach my $type (@inststatuses) {
10593:                                             if (($type ne '') &&
10594:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10595:                                                 $refused = '';
10596:                                             }
10597:                                         }
10598:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10599:                                         $refused = '';
10600:                                     }
10601:                                 }
10602:                             }
10603:                         }
10604:                     }
10605:                 }
10606:                 if ($refused) {
10607:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10608:                              ' '.$role.' '.$end.' '.$start.' by '.
10609: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10610:                     return 'refused';
10611:                 }
10612:             }
10613:         } elsif ($role eq 'au') {
10614:             if ($url ne '/'.$udom.'/') {
10615:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10616:                          ' to assign author role for '.$uname.':'.$udom.
10617:                          ' in domain: '.$url.' refused (wrong domain).');
10618:                 return 'refused';
10619:             }
10620:         }
10621:         $mrole=$role;
10622:     }
10623:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10624:                 "$udom:$uname:$url".'_'."$mrole=$role";
10625:     if ($end) { $command.='_'.$end; }
10626:     if ($start) {
10627: 	if ($end) { 
10628:            $command.='_'.$start; 
10629:         } else {
10630:            $command.='_0_'.$start;
10631:         }
10632:     }
10633:     my $origstart = $start;
10634:     my $origend = $end;
10635:     my $delflag;
10636: # actually delete
10637:     if ($deleteflag) {
10638: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10639: # modify command to delete the role
10640:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10641:                 "$udom:$uname:$url".'_'."$mrole";
10642: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10643: # set start and finish to negative values for userrolelog
10644:            $start=-1;
10645:            $end=-1;
10646:            $delflag = 1;
10647:         }
10648:     }
10649: # send command
10650:     my $answer=&reply($command,&homeserver($uname,$udom));
10651: # log new user role if status is ok
10652:     if ($answer eq 'ok') {
10653: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10654:         if (($role eq 'cc') || ($role eq 'in') ||
10655:             ($role eq 'ep') || ($role eq 'ad') ||
10656:             ($role eq 'ta') || ($role eq 'st') ||
10657:             ($role=~/^cr/) || ($role eq 'gr') ||
10658:             ($role eq 'co')) {
10659: # for course roles, perform group memberships changes triggered by role change.
10660:             unless ($role =~ /^gr/) {
10661:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10662:                                                  $origstart,$selfenroll,$context);
10663:             }
10664:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10665:                            $selfenroll,$context);
10666:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10667:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10668:                  ($role eq 'da')) {
10669:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10670:                            $context);
10671:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10672:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10673:                              $context); 
10674:         }
10675:         if ($role eq 'cc') {
10676:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10677:         }
10678:     }
10679:     return $answer;
10680: }
10681: 
10682: sub autoupdate_coowners {
10683:     my ($url,$end,$start,$uname,$udom) = @_;
10684:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10685:     if (($cdom ne '') && ($cnum ne '')) {
10686:         my $now = time;
10687:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10688:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10689:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10690:             my $instcode = $coursehash{'internal.coursecode'};
10691:             my $xlists = $coursehash{'internal.crosslistings'};
10692:             if ($instcode ne '') {
10693:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10694:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10695:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10696:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10697:                         unless ($result eq 'valid') {
10698:                             if ($xlists ne '') {
10699:                                 foreach my $xlist (split(',',$xlists)) {
10700:                                     my ($inst_crosslist,$lcsec) = split(':',$xlist);
10701:                                     $result =
10702:                                         &auto_validate_inst_crosslist($cnum,$cdom,$instcode,
10703:                                                                       $inst_crosslist,$uname.':'.$udom);
10704:                                     last if ($result eq 'valid');
10705:                                 }
10706:                             }
10707:                         }
10708:                         if ($result eq 'valid') {
10709:                             if ($coursehash{'internal.co-owners'}) {
10710:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10711:                                     push(@newcoowners,$coowner);
10712:                                 }
10713:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10714:                                     push(@newcoowners,$uname.':'.$udom);
10715:                                 }
10716:                                 @newcoowners = sort(@newcoowners);
10717:                             } else {
10718:                                 push(@newcoowners,$uname.':'.$udom);
10719:                             }
10720:                         } elsif ($coursehash{'internal.co-owners'}) {
10721:                             foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10722:                                 unless ($coowner eq $uname.':'.$udom) {
10723:                                     push(@newcoowners,$coowner);
10724:                                 }
10725:                             }
10726:                             unless (@newcoowners > 0) {
10727:                                 $delcoowners = 1;
10728:                                 $coowners = '';
10729:                             }
10730:                         }
10731:                         if (@newcoowners || $delcoowners) {
10732:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10733:                                             $delcoowners,@newcoowners);
10734:                         }
10735:                     }
10736:                 }
10737:             }
10738:         }
10739:     }
10740: }
10741: 
10742: sub store_coowners {
10743:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10744:     my $cid = $cdom.'_'.$cnum;
10745:     my ($coowners,$delresult,$putresult);
10746:     if (@newcoowners) {
10747:         $coowners = join(',',@newcoowners);
10748:         my %coownershash = (
10749:                             'internal.co-owners' => $coowners,
10750:                            );
10751:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10752:         if ($putresult eq 'ok') {
10753:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10754:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10755:             }
10756:         }
10757:     }
10758:     if ($delcoowners) {
10759:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10760:         if ($delresult eq 'ok') {
10761:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10762:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10763:             }
10764:         }
10765:     }
10766:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10767:         my %crsinfo =
10768:             &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10769:         if (ref($crsinfo{$cid}) eq 'HASH') {
10770:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10771:             my $cidput = &courseidput($cdom,\%crsinfo,$chome,'notime');
10772:         }
10773:     }
10774: }
10775: 
10776: # -------------------------------------------------- Modify user authentication
10777: # Overrides without validation
10778: 
10779: sub modifyuserauth {
10780:     my ($udom,$uname,$umode,$upass)=@_;
10781:     my $uhome=&homeserver($uname,$udom);
10782:     my $allowed;
10783:     if (&allowed('mau',$udom)) {
10784:         $allowed = 1;
10785:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10786:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10787:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10788:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10789:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10790:         if (($cdom ne '') && ($cnum ne '')) {
10791:             my $is_owner = &is_course_owner($cdom,$cnum);
10792:             if ($is_owner) {
10793:                 $allowed = 1;
10794:             }
10795:         }
10796:     }
10797:     unless ($allowed) { return 'refused'; }
10798:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10799:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10800:              ' in domain '.$env{'request.role.domain'});  
10801:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10802: 		     &escape($upass),$uhome);
10803:     my $ip = &get_requestor_ip();
10804:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10805:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10806:          '(Remote '.$ip.'): '.$reply);
10807:     &log($udom,,$uname,$uhome,
10808:         'Authentication changed by '.$env{'user.domain'}.', '.
10809:                                      $env{'user.name'}.', '.$umode.
10810:          '(Remote '.$ip.'): '.$reply);
10811:     unless ($reply eq 'ok') {
10812:         &logthis('Authentication mode error: '.$reply);
10813: 	return 'error: '.$reply;
10814:     }   
10815:     return 'ok';
10816: }
10817: 
10818: # --------------------------------------------------------------- Modify a user
10819: 
10820: sub modifyuser {
10821:     my ($udom,    $uname, $uid,
10822:         $umode,   $upass, $first,
10823:         $middle,  $last,  $gene,
10824:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10825:     $udom= &LONCAPA::clean_domain($udom);
10826:     $uname=&LONCAPA::clean_username($uname);
10827:     my $showcandelete = 'none';
10828:     if (ref($candelete) eq 'ARRAY') {
10829:         if (@{$candelete} > 0) {
10830:             $showcandelete = join(', ',@{$candelete});
10831:         }
10832:     }
10833:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10834:              $umode.', '.$first.', '.$middle.', '.
10835: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10836:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10837:                                      ' desiredhome not specified'). 
10838:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10839:              ' in domain '.$env{'request.role.domain'});
10840:     my $uhome=&homeserver($uname,$udom,'true');
10841:     my $newuser;
10842:     if ($uhome eq 'no_host') {
10843:         $newuser = 1;
10844:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10845:                 ($umode eq 'lti')) {
10846:             return 'error: more information needed to create new user';
10847:         }
10848:     }
10849: # ----------------------------------------------------------------- Create User
10850:     if (($uhome eq 'no_host') && 
10851: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10852:         my $unhome='';
10853:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10854:             $unhome = $desiredhome;
10855: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10856: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10857:         } else { # load balancing routine for determining $unhome
10858:             my $loadm=10000000;
10859: 	    my %servers = &get_servers($udom,'library');
10860: 	    foreach my $tryserver (keys(%servers)) {
10861: 		my $answer=reply('load',$tryserver);
10862: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10863: 		    $loadm=$answer;
10864: 		    $unhome=$tryserver;
10865: 		}
10866: 	    }
10867:         }
10868:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10869: 	    return 'error: unable to find a home server for '.$uname.
10870:                    ' in domain '.$udom;
10871:         }
10872:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10873:                          &escape($upass),$unhome);
10874: 	unless ($reply eq 'ok') {
10875:             return 'error: '.$reply;
10876:         }   
10877:         $uhome=&homeserver($uname,$udom,'true');
10878:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10879: 	    return 'error: unable verify users home machine.';
10880:         }
10881:     }   # End of creation of new user
10882: # ---------------------------------------------------------------------- Add ID
10883:     if ($uid) {
10884:        $uid=~tr/A-Z/a-z/;
10885:        my %uidhash=&idrget($udom,$uname);
10886:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10887:          && (!$forceid)) {
10888: 	  unless ($uid eq $uidhash{$uname}) {
10889: 	      return 'error: user id "'.$uid.'" does not match '.
10890:                   'current user id "'.$uidhash{$uname}.'".';
10891:           }
10892:        } else {
10893: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10894:        }
10895:     }
10896: # -------------------------------------------------------------- Add names, etc
10897:     my @tmp=&get('environment',
10898: 		   ['firstname','middlename','lastname','generation','id',
10899:                     'permanentemail','inststatus'],
10900: 		   $udom,$uname);
10901:     my (%names,%oldnames);
10902:     if ($tmp[0] =~ m/^error:.*/) { 
10903:         %names=(); 
10904:     } else {
10905:         %names = @tmp;
10906:         %oldnames = %names;
10907:     }
10908: #
10909: # If name, email and/or uid are blank (e.g., because an uploaded file
10910: # of users did not contain them), do not overwrite existing values
10911: # unless field is in $candelete array ref.  
10912: #
10913: 
10914:     my @fields = ('firstname','middlename','lastname','generation',
10915:                   'permanentemail','id');
10916:     my %newvalues;
10917:     if (ref($candelete) eq 'ARRAY') {
10918:         foreach my $field (@fields) {
10919:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10920:                 if ($field eq 'firstname') {
10921:                     $names{$field} = $first;
10922:                 } elsif ($field eq 'middlename') {
10923:                     $names{$field} = $middle;
10924:                 } elsif ($field eq 'lastname') {
10925:                     $names{$field} = $last;
10926:                 } elsif ($field eq 'generation') { 
10927:                     $names{$field} = $gene;
10928:                 } elsif ($field eq 'permanentemail') {
10929:                     $names{$field} = $email;
10930:                 } elsif ($field eq 'id') {
10931:                     $names{$field}  = $uid;
10932:                 }
10933:             }
10934:         }
10935:     }
10936:     if ($first)  { $names{'firstname'}  = $first; }
10937:     if (defined($middle)) { $names{'middlename'} = $middle; }
10938:     if ($last)   { $names{'lastname'}   = $last; }
10939:     if (defined($gene))   { $names{'generation'} = $gene; }
10940:     if ($email) {
10941:        $email=~s/[^\w\@\.\-\,]//gs;
10942:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10943:     }
10944:     if ($uid) { $names{'id'}  = $uid; }
10945:     if (defined($inststatus)) {
10946:         $names{'inststatus'} = '';
10947:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10948:         if (ref($usertypes) eq 'HASH') {
10949:             my @okstatuses; 
10950:             foreach my $item (split(/:/,$inststatus)) {
10951:                 if (defined($usertypes->{$item})) {
10952:                     push(@okstatuses,$item);  
10953:                 }
10954:             }
10955:             if (@okstatuses) {
10956:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10957:             }
10958:         }
10959:     }
10960:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10961:                  $umode.', '.$first.', '.$middle.', '.
10962:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10963:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10964:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10965:     } else {
10966:         $logmsg .= ' during self creation';
10967:     }
10968:     my $changed;
10969:     if ($newuser) {
10970:         $changed = 1;
10971:     } else {
10972:         foreach my $field (@fields) {
10973:             if ($names{$field} ne $oldnames{$field}) {
10974:                 $changed = 1;
10975:                 last;
10976:             }
10977:         }
10978:     }
10979:     unless ($changed) {
10980:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10981:         &logthis($logmsg);
10982:         return 'ok';
10983:     }
10984:     my $reply = &put('environment', \%names, $udom,$uname);
10985:     if ($reply ne 'ok') { 
10986:         return 'error: '.$reply;
10987:     }
10988:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10989:         &devalidate_cache_new('emailscache',$uname.':'.$udom);
10990:     }
10991:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10992:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10993:     $logmsg = 'Success modifying user '.$logmsg;
10994:     &logthis($logmsg);
10995:     return 'ok';
10996: }
10997: 
10998: # -------------------------------------------------------------- Modify student
10999: 
11000: sub modifystudent {
11001:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
11002:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
11003:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
11004:     if (!$cid) {
11005: 	unless ($cid=$env{'request.course.id'}) {
11006: 	    return 'not_in_class';
11007: 	}
11008:     }
11009: # --------------------------------------------------------------- Make the user
11010:     my $reply=&modifyuser
11011: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
11012:          $desiredhome,$email,$inststatus);
11013:     unless ($reply eq 'ok') { return $reply; }
11014:     # This will cause &modify_student_enrollment to get the uid from the
11015:     # student's environment
11016:     $uid = undef if (!$forceid);
11017:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
11018:                                         $gene,$usec,$end,$start,$type,$locktype,
11019:                                         $cid,$selfenroll,$context,$credits,$instsec);
11020:     return $reply;
11021: }
11022: 
11023: sub modify_student_enrollment {
11024:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
11025:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
11026:     my ($cdom,$cnum,$chome);
11027:     if (!$cid) {
11028: 	unless ($cid=$env{'request.course.id'}) {
11029: 	    return 'not_in_class';
11030: 	}
11031: 	$cdom=$env{'course.'.$cid.'.domain'};
11032: 	$cnum=$env{'course.'.$cid.'.num'};
11033:     } else {
11034: 	($cdom,$cnum)=split(/_/,$cid);
11035:     }
11036:     $chome=$env{'course.'.$cid.'.home'};
11037:     if (!$chome) {
11038: 	$chome=&homeserver($cnum,$cdom);
11039:     }
11040:     if (!$chome) { return 'unknown_course'; }
11041:     # Make sure the user exists
11042:     my $uhome=&homeserver($uname,$udom);
11043:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11044: 	return 'error: no such user';
11045:     }
11046:     # Get student data if we were not given enough information
11047:     if (!defined($first)  || $first  eq '' || 
11048:         !defined($last)   || $last   eq '' || 
11049:         !defined($uid)    || $uid    eq '' || 
11050:         !defined($middle) || $middle eq '' || 
11051:         !defined($gene)   || $gene   eq '') {
11052:         # They did not supply us with enough data to enroll the student, so
11053:         # we need to pick up more information.
11054:         my %tmp = &get('environment',
11055:                        ['firstname','middlename','lastname', 'generation','id']
11056:                        ,$udom,$uname);
11057: 
11058:         #foreach my $key (keys(%tmp)) {
11059:         #    &logthis("key $key = ".$tmp{$key});
11060:         #}
11061:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
11062:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
11063:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
11064:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
11065:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
11066:     }
11067:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
11068:     my $user = "$uname:$udom";
11069:     my %old_entry = &get('classlist',[$user],$cdom,$cnum);
11070:     my $reply=cput('classlist',
11071: 		   {$user => 
11072: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
11073: 		   $cdom,$cnum);
11074:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
11075:         &devalidate_getsection_cache($udom,$uname,$cid);
11076:     } else { 
11077: 	return 'error: '.$reply;
11078:     }
11079:     # Add student role to user
11080:     my $uurl='/'.$cid;
11081:     $uurl=~s/\_/\//g;
11082:     if ($usec) {
11083: 	$uurl.='/'.$usec;
11084:     }
11085:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
11086:                              $selfenroll,$context);
11087:     if ($result ne 'ok') {
11088:         if ($old_entry{$user} ne '') {
11089:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
11090:         } else {
11091:             $reply = &del('classlist',[$user],$cdom,$cnum);
11092:         }
11093:     }
11094:     return $result; 
11095: }
11096: 
11097: sub format_name {
11098:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
11099:     my $name;
11100:     if ($first ne 'lastname') {
11101: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
11102:     } else {
11103: 	if ($lastname=~/\S/) {
11104: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
11105: 	    $name=~s/\s+,/,/;
11106: 	} else {
11107: 	    $name.= $firstname.' '.$middlename.' '.$generation;
11108: 	}
11109:     }
11110:     $name=~s/^\s+//;
11111:     $name=~s/\s+$//;
11112:     $name=~s/\s+/ /g;
11113:     return $name;
11114: }
11115: 
11116: # ------------------------------------------------- Write to course preferences
11117: 
11118: sub writecoursepref {
11119:     my ($courseid,%prefs)=@_;
11120:     $courseid=~s/^\///;
11121:     $courseid=~s/\_/\//g;
11122:     my ($cdomain,$cnum)=split(/\//,$courseid);
11123:     my $chome=homeserver($cnum,$cdomain);
11124:     if (($chome eq '') || ($chome eq 'no_host')) { 
11125: 	return 'error: no such course';
11126:     }
11127:     my $cstring='';
11128:     foreach my $pref (keys(%prefs)) {
11129: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
11130:     }
11131:     $cstring=~s/\&$//;
11132:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
11133: }
11134: 
11135: # ---------------------------------------------------------- Make/modify course
11136: 
11137: sub createcourse {
11138:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
11139:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
11140:     $url=&declutter($url);
11141:     my $cid='';
11142:     if ($context eq 'requestcourses') {
11143:         my $can_create = 0;
11144:         my ($ownername,$ownerdom) = split(':',$course_owner);
11145:         if ($udom eq $ownerdom) {
11146:             my $reload;
11147:             if (($callercontext eq 'auto') &&
11148:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
11149:                 $reload = 'reload';
11150:             }
11151:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
11152:                                   $context)) {
11153:                 $can_create = 1;
11154:             }
11155:         } else {
11156:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
11157:                                            $category);
11158:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
11159:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
11160:                 if (@curr > 0) {
11161:                     my @options = qw(approval validate autolimit);
11162:                     my $optregex = join('|',@options);
11163:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
11164:                         $can_create = 1;
11165:                     }
11166:                 }
11167:             }
11168:         }
11169:         if ($can_create) {
11170:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
11171:                 unless (&allowed('ccc',$udom)) {
11172:                     return 'refused'; 
11173:                 }
11174:             }
11175:         } else {
11176:             return 'refused';
11177:         }
11178:     } elsif (!&allowed('ccc',$udom)) {
11179:         return 'refused';
11180:     }
11181: # --------------------------------------------------------------- Get Unique ID
11182:     my $uname;
11183:     if ($cnum =~ /^$match_courseid$/) {
11184:         my $chome=&homeserver($cnum,$udom,'true');
11185:         if (($chome eq '') || ($chome eq 'no_host')) {
11186:             $uname = $cnum;
11187:         } else {
11188:             $uname = &generate_coursenum($udom,$crstype);
11189:         }
11190:     } else {
11191:         $uname = &generate_coursenum($udom,$crstype);
11192:     }
11193:     return $uname if ($uname =~ /^error/);
11194: # -------------------------------------------------- Check supplied server name
11195:     if (!defined($course_server)) {
11196:         if (defined(&domain($udom,'primary'))) {
11197:             $course_server = &domain($udom,'primary');
11198:         } else {
11199:             $course_server = $env{'user.home'}; 
11200:         }
11201:     }
11202:     my %host_servers =
11203:         &get_servers($udom,'library');
11204:     unless ($host_servers{$course_server}) {
11205:         return 'error: invalid home server for course: '.$course_server;
11206:     }
11207: # ------------------------------------------------------------- Make the course
11208:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
11209:                       $course_server);
11210:     unless ($reply eq 'ok') { return 'error: '.$reply; }
11211:     my $uhome=&homeserver($uname,$udom,'true');
11212:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
11213: 	return 'error: no such course';
11214:     }
11215: # ----------------------------------------------------------------- Course made
11216: # log existence
11217:     my $now = time;
11218:     my $newcourse = {
11219:                     $udom.'_'.$uname => {
11220:                                      description => $description,
11221:                                      inst_code   => $inst_code,
11222:                                      owner       => $course_owner,
11223:                                      type        => $crstype,
11224:                                      creator     => $env{'user.name'}.':'.
11225:                                                     $env{'user.domain'},
11226:                                      created     => $now,
11227:                                      context     => $context,
11228:                                                 },
11229:                     };
11230:     &courseidput($udom,$newcourse,$uhome,'notime');
11231: # set toplevel url
11232:     my $topurl=$url;
11233:     unless ($nonstandard) {
11234: # ------------------------------------------ For standard courses, make top url
11235:         my $mapurl=&clutter($url);
11236:         if ($mapurl eq '/res/') { $mapurl=''; }
11237:         $env{'form.initmap'}=(<<ENDINITMAP);
11238: <map>
11239: <resource id="1" type="start"></resource>
11240: <resource id="2" src="$mapurl"></resource>
11241: <resource id="3" type="finish"></resource>
11242: <link index="1" from="1" to="2"></link>
11243: <link index="2" from="2" to="3"></link>
11244: </map>
11245: ENDINITMAP
11246:         $topurl=&declutter(
11247:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
11248:                           );
11249:     }
11250: # ----------------------------------------------------------- Write preferences
11251:     &writecoursepref($udom.'_'.$uname,
11252:                      ('description'              => $description,
11253:                       'url'                      => $topurl,
11254:                       'internal.creator'         => $env{'user.name'}.':'.
11255:                                                     $env{'user.domain'},
11256:                       'internal.created'         => $now,
11257:                       'internal.creationcontext' => $context)
11258:                     );
11259:     return '/'.$udom.'/'.$uname;
11260: }
11261: 
11262: # ------------------------------------------------------------------- Create ID
11263: sub generate_coursenum {
11264:     my ($udom,$crstype) = @_;
11265:     my $domdesc = &domain($udom);
11266:     return 'error: invalid domain' if ($domdesc eq '');
11267:     my $first;
11268:     if ($crstype eq 'Community') {
11269:         $first = '0';
11270:     } else {
11271:         $first = int(1+rand(9)); 
11272:     } 
11273:     my $uname=$first.
11274:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11275:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
11276:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11277: # ----------------------------------------------- Make sure that does not exist
11278:     my $uhome=&homeserver($uname,$udom,'true');
11279:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
11280:         if ($crstype eq 'Community') {
11281:             $first = '0';
11282:         } else {
11283:             $first = int(1+rand(9));
11284:         }
11285:         $uname=$first.
11286:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
11287:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
11288:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
11289:         $uhome=&homeserver($uname,$udom,'true');
11290:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
11291:             return 'error: unable to generate unique course-ID';
11292:         }
11293:     }
11294:     return $uname;
11295: }
11296: 
11297: sub is_course {
11298:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
11299:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
11300: 
11301:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
11302:     my $uhome=&homeserver($cnum,$cdom);
11303:     my $iscourse;
11304:     if (grep { $_ eq $uhome } current_machine_ids()) {
11305:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
11306:     } else {
11307:         my $hashid = $cdom.':'.$cnum;
11308:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
11309:         unless (defined($cached)) {
11310:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
11311:                                         $cnum,undef,undef,'.');
11312:             $iscourse = 0;
11313:             if (exists($courses{$cdom.'_'.$cnum})) {
11314:                 $iscourse = 1;
11315:             }
11316:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
11317:         }
11318:     }
11319:     return unless ($iscourse);
11320:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
11321: }
11322: 
11323: sub store_userdata {
11324:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
11325:     my $result;
11326:     if ($datakey ne '') {
11327:         if (ref($storehash) eq 'HASH') {
11328:             if ($udom eq '' || $uname eq '') {
11329:                 $udom = $env{'user.domain'};
11330:                 $uname = $env{'user.name'};
11331:             }
11332:             my $uhome=&homeserver($uname,$udom);
11333:             if (($uhome eq '') || ($uhome eq 'no_host')) {
11334:                 $result = 'error: no_host';
11335:             } else {
11336:                 $storehash->{'ip'} = &get_requestor_ip();
11337:                 $storehash->{'host'} = $perlvar{'lonHostID'};
11338: 
11339:                 my $namevalue='';
11340:                 foreach my $key (keys(%{$storehash})) {
11341:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
11342:                 }
11343:                 $namevalue=~s/\&$//;
11344:                 unless ($namespace eq 'courserequests') {
11345:                     $datakey = &escape($datakey);
11346:                 }
11347:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
11348:                                   $namevalue,$uhome);
11349:             }
11350:         } else {
11351:             $result = 'error: data to store was not a hash reference'; 
11352:         }
11353:     } else {
11354:         $result= 'error: invalid requestkey'; 
11355:     }
11356:     return $result;
11357: }
11358: 
11359: # ---------------------------------------------------------- Assign Custom Role
11360: 
11361: sub assigncustomrole {
11362:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
11363:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
11364:                        $end,$start,$deleteflag,$selfenroll,$context);
11365: }
11366: 
11367: # ----------------------------------------------------------------- Revoke Role
11368: 
11369: sub revokerole {
11370:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
11371:     my $now=time;
11372:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
11373: }
11374: 
11375: # ---------------------------------------------------------- Revoke Custom Role
11376: 
11377: sub revokecustomrole {
11378:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
11379:     my $now=time;
11380:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
11381:            $deleteflag,$selfenroll,$context);
11382: }
11383: 
11384: # ------------------------------------------------------------ Disk usage
11385: sub diskusage {
11386:     my ($udom,$uname,$directorypath,$getpropath)=@_;
11387:     $directorypath =~ s/\/$//;
11388:     my $listing=&reply('du2:'.&escape($directorypath).':'
11389:                        .&escape($getpropath).':'.&escape($uname).':'
11390:                        .&escape($udom),homeserver($uname,$udom));
11391:     if ($listing eq 'unknown_cmd') {
11392:         if ($getpropath) {
11393:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
11394:         }
11395:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
11396:     }
11397:     return $listing;
11398: }
11399: 
11400: sub is_locked {
11401:     my ($file_name, $domain, $user, $which) = @_;
11402:     my @check;
11403:     my $is_locked;
11404:     push (@check,$file_name);
11405:     my %locked = &get('file_permissions',\@check,
11406: 		      $env{'user.domain'},$env{'user.name'});
11407:     my ($tmp)=keys(%locked);
11408:     if ($tmp=~/^error:/) { undef(%locked); }
11409:     
11410:     if (ref($locked{$file_name}) eq 'ARRAY') {
11411:         $is_locked = 'false';
11412:         foreach my $entry (@{$locked{$file_name}}) {
11413:            if (ref($entry) eq 'ARRAY') {
11414:                $is_locked = 'true';
11415:                if (ref($which) eq 'ARRAY') {
11416:                    push(@{$which},$entry);
11417:                } else {
11418:                    last;
11419:                }
11420:            }
11421:        }
11422:     } else {
11423:         $is_locked = 'false';
11424:     }
11425:     return $is_locked;
11426: }
11427: 
11428: sub declutter_portfile {
11429:     my ($file) = @_;
11430:     $file =~ s{^(/portfolio/|portfolio/)}{/};
11431:     return $file;
11432: }
11433: 
11434: # ------------------------------------------------------------- Mark as Read Only
11435: 
11436: sub mark_as_readonly {
11437:     my ($domain,$user,$files,$what) = @_;
11438:     my %current_permissions = &dump('file_permissions',$domain,$user);
11439:     my ($tmp)=keys(%current_permissions);
11440:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11441:     foreach my $file (@{$files}) {
11442: 	$file = &declutter_portfile($file);
11443:         push(@{$current_permissions{$file}},$what);
11444:     }
11445:     &put('file_permissions',\%current_permissions,$domain,$user);
11446:     return;
11447: }
11448: 
11449: # ------------------------------------------------------------Save Selected Files
11450: 
11451: sub save_selected_files {
11452:     my ($user, $path, @files) = @_;
11453:     my $filename = $user."savedfiles";
11454:     my @other_files = &files_not_in_path($user, $path);
11455:     open (OUT,'>',LONCAPA::tempdir().$filename);
11456:     foreach my $file (@files) {
11457:         print (OUT $env{'form.currentpath'}.$file."\n");
11458:     }
11459:     foreach my $file (@other_files) {
11460:         print (OUT $file."\n");
11461:     }
11462:     close (OUT);
11463:     return 'ok';
11464: }
11465: 
11466: sub clear_selected_files {
11467:     my ($user) = @_;
11468:     my $filename = $user."savedfiles";
11469:     open (OUT,'>',LONCAPA::tempdir().$filename);
11470:     print (OUT undef);
11471:     close (OUT);
11472:     return ("ok");    
11473: }
11474: 
11475: sub files_in_path {
11476:     my ($user, $path) = @_;
11477:     my $filename = $user."savedfiles";
11478:     my %return_files;
11479:     open (IN,'<',LONCAPA::tempdir().$filename);
11480:     while (my $line_in = <IN>) {
11481:         chomp ($line_in);
11482:         my @paths_and_file = split (m!/!, $line_in);
11483:         my $file_part = pop (@paths_and_file);
11484:         my $path_part = join ('/', @paths_and_file);
11485:         $path_part.='/';
11486:         my $path_and_file = $path_part.$file_part;
11487:         if ($path_part eq $path) {
11488:             $return_files{$file_part}= 'selected';
11489:         }
11490:     }
11491:     close (IN);
11492:     return (\%return_files);
11493: }
11494: 
11495: # called in portfolio select mode, to show files selected NOT in current directory
11496: sub files_not_in_path {
11497:     my ($user, $path) = @_;
11498:     my $filename = $user."savedfiles";
11499:     my @return_files;
11500:     my $path_part;
11501:     open(IN, '<',LONCAPA::tempdir().$filename);
11502:     while (my $line = <IN>) {
11503:         #ok, I know it's clunky, but I want it to work
11504:         my @paths_and_file = split(m|/|, $line);
11505:         my $file_part = pop(@paths_and_file);
11506:         chomp($file_part);
11507:         my $path_part = join('/', @paths_and_file);
11508:         $path_part .= '/';
11509:         my $path_and_file = $path_part.$file_part;
11510:         if ($path_part ne $path) {
11511:             push(@return_files, ($path_and_file));
11512:         }
11513:     }
11514:     close(OUT);
11515:     return (@return_files);
11516: }
11517: 
11518: #------------------------------Submitted/Handedback Portfolio Files Versioning
11519:  
11520: sub portfiles_versioning {
11521:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11522:     my $portfolio_root = '/userfiles/portfolio';
11523:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11524:     foreach my $file (@{$portfiles}) {
11525:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11526:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11527:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11528:         my $getpropath = 1;
11529:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11530:                                              $stu_name,$getpropath);
11531:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11532:         my $new_answer = 
11533:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11534:         if ($new_answer ne 'problem getting file') {
11535:             push(@{$versioned_portfiles}, $directory.$new_answer);
11536:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11537:                               [$symb,$env{'request.course.id'},'graded']);
11538:         }
11539:     }
11540: }
11541: 
11542: sub get_next_version {
11543:     my ($answer_name, $answer_ext, $dir_list) = @_;
11544:     my $version;
11545:     if (ref($dir_list) eq 'ARRAY') {
11546:         foreach my $row (@{$dir_list}) {
11547:             my ($file) = split(/\&/,$row,2);
11548:             my ($file_name,$file_version,$file_ext) =
11549:                 &file_name_version_ext($file);
11550:             if (($file_name eq $answer_name) &&
11551:                 ($file_ext eq $answer_ext)) {
11552:                      # gets here if filename and extension match,
11553:                      # regardless of version
11554:                 if ($file_version ne '') {
11555:                     # a versioned file is found  so save it for later
11556:                     if ($file_version > $version) {
11557:                         $version = $file_version;
11558:                     }
11559:                 }
11560:             }
11561:         }
11562:     }
11563:     $version ++;
11564:     return($version);
11565: }
11566: 
11567: sub version_selected_portfile {
11568:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11569:     my ($answer_name,$answer_ver,$answer_ext) =
11570:         &file_name_version_ext($file_name);
11571:     my $new_answer;
11572:     $env{'form.copy'} =
11573:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11574:     if($env{'form.copy'} eq '-1') {
11575:         $new_answer = 'problem getting file';
11576:     } else {
11577:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11578:         my $copy_result = 
11579:             &finishuserfileupload($stu_name,$domain,'copy',
11580:                                   '/portfolio'.$directory.$new_answer);
11581:     }
11582:     undef($env{'form.copy'});
11583:     return ($new_answer);
11584: }
11585: 
11586: sub file_name_version_ext {
11587:     my ($file)=@_;
11588:     my @file_parts = split(/\./, $file);
11589:     my ($name,$version,$ext);
11590:     if (@file_parts > 1) {
11591:         $ext=pop(@file_parts);
11592:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11593:             $version=pop(@file_parts);
11594:         }
11595:         $name=join('.',@file_parts);
11596:     } else {
11597:         $name=join('.',@file_parts);
11598:     }
11599:     return($name,$version,$ext);
11600: }
11601: 
11602: #----------------------------------------------Get portfolio file permissions
11603: 
11604: sub get_portfile_permissions {
11605:     my ($domain,$user) = @_;
11606:     my %current_permissions = &dump('file_permissions',$domain,$user);
11607:     my ($tmp)=keys(%current_permissions);
11608:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11609:     return \%current_permissions;
11610: }
11611: 
11612: #---------------------------------------------Get portfolio file access controls
11613: 
11614: sub get_access_controls {
11615:     my ($current_permissions,$group,$file) = @_;
11616:     my %access;
11617:     my $real_file = $file;
11618:     $file =~ s/\.meta$//;
11619:     if (defined($file)) {
11620:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11621:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11622:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11623:             }
11624:         }
11625:     } else {
11626:         foreach my $key (keys(%{$current_permissions})) {
11627:             if ($key =~ /\0accesscontrol$/) {
11628:                 if (defined($group)) {
11629:                     if ($key !~ m-^\Q$group\E/-) {
11630:                         next;
11631:                     }
11632:                 }
11633:                 my ($fullpath) = split(/\0/,$key);
11634:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11635:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11636:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11637:                     }
11638:                 }
11639:             }
11640:         }
11641:     }
11642:     return %access;
11643: }
11644: 
11645: sub modify_access_controls {
11646:     my ($file_name,$changes,$domain,$user)=@_;
11647:     my ($outcome,$deloutcome);
11648:     my %store_permissions;
11649:     my %new_values;
11650:     my %new_control;
11651:     my %translation;
11652:     my @deletions = ();
11653:     my $now = time;
11654:     if (exists($$changes{'activate'})) {
11655:         if (ref($$changes{'activate'}) eq 'HASH') {
11656:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11657:             my $numnew = scalar(@newitems);
11658:             for (my $i=0; $i<$numnew; $i++) {
11659:                 my $newkey = $newitems[$i];
11660:                 my $newid = &Apache::loncommon::get_cgi_id();
11661:                 if ($newkey =~ /^\d+:/) { 
11662:                     $newkey =~ s/^(\d+)/$newid/;
11663:                     $translation{$1} = $newid;
11664:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11665:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11666:                     $translation{$1} = $newid;
11667:                 }
11668:                 $new_values{$file_name."\0".$newkey} = 
11669:                                           $$changes{'activate'}{$newitems[$i]};
11670:                 $new_control{$newkey} = $now;
11671:             }
11672:         }
11673:     }
11674:     my %todelete;
11675:     my %changed_items;
11676:     foreach my $action ('delete','update') {
11677:         if (exists($$changes{$action})) {
11678:             if (ref($$changes{$action}) eq 'HASH') {
11679:                 foreach my $key (keys(%{$$changes{$action}})) {
11680:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11681:                     if ($action eq 'delete') { 
11682:                         $todelete{$itemnum} = 1;
11683:                     } else {
11684:                         $changed_items{$itemnum} = $key;
11685:                     }
11686:                 }
11687:             }
11688:         }
11689:     }
11690:     # get lock on access controls for file.
11691:     my $lockhash = {
11692:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11693:                                                        ':'.$env{'user.domain'},
11694:                    }; 
11695:     my $tries = 0;
11696:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11697:    
11698:     while (($gotlock ne 'ok') && $tries < 10) {
11699:         $tries ++;
11700:         sleep(0.1);
11701:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11702:     }
11703:     if ($gotlock eq 'ok') {
11704:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11705:         my ($tmp)=keys(%curr_permissions);
11706:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11707:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11708:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11709:             if (ref($curr_controls) eq 'HASH') {
11710:                 foreach my $control_item (keys(%{$curr_controls})) {
11711:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11712:                     if (defined($todelete{$itemnum})) {
11713:                         push(@deletions,$file_name."\0".$control_item);
11714:                     } else {
11715:                         if (defined($changed_items{$itemnum})) {
11716:                             $new_control{$changed_items{$itemnum}} = $now;
11717:                             push(@deletions,$file_name."\0".$control_item);
11718:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11719:                         } else {
11720:                             $new_control{$control_item} = $$curr_controls{$control_item};
11721:                         }
11722:                     }
11723:                 }
11724:             }
11725:         }
11726:         my ($group);
11727:         if (&is_course($domain,$user)) {
11728:             ($group,my $file) = split(/\//,$file_name,2);
11729:         }
11730:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11731:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11732:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11733:         #  remove lock
11734:         my @del_lock = ($file_name."\0".'locked_access_records');
11735:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11736:         my $sqlresult =
11737:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11738:                                     $group);
11739:     } else {
11740:         $outcome = "error: could not obtain lockfile\n";  
11741:     }
11742:     return ($outcome,$deloutcome,\%new_values,\%translation);
11743: }
11744: 
11745: sub make_public_indefinitely {
11746:     my (@requrl) = @_;
11747:     return &automated_portfile_access('public',\@requrl);
11748: }
11749: 
11750: sub automated_portfile_access {
11751:     my ($accesstype,$addsref,$delsref,$info) = @_;
11752:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11753:         return 'invalid';
11754:     }
11755:     my %urls;
11756:     if (ref($addsref) eq 'ARRAY') {
11757:         foreach my $requrl (@{$addsref}) {
11758:             if (&is_portfolio_url($requrl)) {
11759:                 unless (exists($urls{$requrl})) {
11760:                     $urls{$requrl} = 'add';
11761:                 }
11762:             }
11763:         }
11764:     }
11765:     if (ref($delsref) eq 'ARRAY') {
11766:         foreach my $requrl (@{$delsref}) { 
11767:             if (&is_portfolio_url($requrl)) {
11768:                 unless (exists($urls{$requrl})) {
11769:                     $urls{$requrl} = 'delete'; 
11770:                 }
11771:             }
11772:         }
11773:     }
11774:     unless (keys(%urls)) {
11775:         return 'invalid';
11776:     }
11777:     my $ip;
11778:     if ($accesstype eq 'ip') {
11779:         if (ref($info) eq 'HASH') {
11780:             if ($info->{'ip'} ne '') {
11781:                 $ip = $info->{'ip'};
11782:             }
11783:         }
11784:         if ($ip eq '') {
11785:             return 'invalid';
11786:         }
11787:     }
11788:     my $errors;
11789:     my $now = time;
11790:     my %current_perms;
11791:     foreach my $requrl (sort(keys(%urls))) {
11792:         my $action;
11793:         if ($urls{$requrl} eq 'add') {
11794:             $action = 'activate';
11795:         } else {
11796:             $action = 'none';
11797:         }
11798:         my $aclnum = 0;
11799:         my (undef,$udom,$unum,$file_name,$group) =
11800:             &parse_portfolio_url($requrl);
11801:         unless (exists($current_perms{$unum.':'.$udom})) {
11802:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11803:         }
11804:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11805:                                                    $group,$file_name);
11806:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11807:             my ($num,$scope,$end,$start) = 
11808:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11809:             if ($scope eq $accesstype) {
11810:                 if (($start <= $now) && ($end == 0)) {
11811:                     if ($accesstype eq 'ip') {
11812:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11813:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11814:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11815:                                     if ($urls{$requrl} eq 'add') {
11816:                                         $action = 'none';
11817:                                         last;
11818:                                     } else {
11819:                                         $action = 'delete';
11820:                                         $aclnum = $num;
11821:                                         last;
11822:                                     }
11823:                                 }
11824:                             }
11825:                         }
11826:                     } elsif ($accesstype eq 'public') {
11827:                         if ($urls{$requrl} eq 'add') {
11828:                             $action = 'none';
11829:                             last;
11830:                         } else {
11831:                             $action = 'delete';
11832:                             $aclnum = $num;
11833:                             last;
11834:                         }
11835:                     }
11836:                 } elsif ($accesstype eq 'public') {
11837:                     $action = 'update';
11838:                     $aclnum = $num;
11839:                     last;
11840:                 }
11841:             }
11842:         }
11843:         if ($action eq 'none') {
11844:             next;
11845:         } else {
11846:             my %changes;
11847:             my $newend = 0;
11848:             my $newstart = $now;
11849:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11850:             $changes{$action}{$newkey} = {
11851:                 type => $accesstype,
11852:                 time => {
11853:                     start => $newstart,
11854:                     end   => $newend,
11855:                 },
11856:             };
11857:             if ($accesstype eq 'ip') {
11858:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11859:             }
11860:             my ($outcome,$deloutcome,$new_values,$translation) =
11861:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11862:             unless ($outcome eq 'ok') {
11863:                 $errors .= $outcome.' ';
11864:             }
11865:         }
11866:     }
11867:     if ($errors) {
11868:         $errors =~ s/\s$//;
11869:         return $errors;
11870:     } else {
11871:         return 'ok';
11872:     }
11873: }
11874: 
11875: #------------------------------------------------------Get Marked as Read Only
11876: 
11877: sub get_marked_as_readonly {
11878:     my ($domain,$user,$what,$group) = @_;
11879:     my $current_permissions = &get_portfile_permissions($domain,$user);
11880:     my @readonly_files;
11881:     my $cmp1=$what;
11882:     if (ref($what)) { $cmp1=join('',@{$what}) };
11883:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11884:         if (defined($group)) {
11885:             if ($file_name !~ m-^\Q$group\E/-) {
11886:                 next;
11887:             }
11888:         }
11889:         if (ref($value) eq "ARRAY"){
11890:             foreach my $stored_what (@{$value}) {
11891:                 my $cmp2=$stored_what;
11892:                 if (ref($stored_what) eq 'ARRAY') {
11893:                     $cmp2=join('',@{$stored_what});
11894:                 }
11895:                 if ($cmp1 eq $cmp2) {
11896:                     push(@readonly_files, $file_name);
11897:                     last;
11898:                 } elsif (!defined($what)) {
11899:                     push(@readonly_files, $file_name);
11900:                     last;
11901:                 }
11902:             }
11903:         }
11904:     }
11905:     return @readonly_files;
11906: }
11907: #-----------------------------------------------------------Get Marked as Read Only Hash
11908: 
11909: sub get_marked_as_readonly_hash {
11910:     my ($current_permissions,$group,$what) = @_;
11911:     my %readonly_files;
11912:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11913:         if (defined($group)) {
11914:             if ($file_name !~ m-^\Q$group\E/-) {
11915:                 next;
11916:             }
11917:         }
11918:         if (ref($value) eq "ARRAY"){
11919:             foreach my $stored_what (@{$value}) {
11920:                 if (ref($stored_what) eq 'ARRAY') {
11921:                     foreach my $lock_descriptor(@{$stored_what}) {
11922:                         if ($lock_descriptor eq 'graded') {
11923:                             $readonly_files{$file_name} = 'graded';
11924:                         } elsif ($lock_descriptor eq 'handback') {
11925:                             $readonly_files{$file_name} = 'handback';
11926:                         } else {
11927:                             if (!exists($readonly_files{$file_name})) {
11928:                                 $readonly_files{$file_name} = 'locked';
11929:                             }
11930:                         }
11931:                     }
11932:                 } 
11933:             }
11934:         } 
11935:     }
11936:     return %readonly_files;
11937: }
11938: # ------------------------------------------------------------ Unmark as Read Only
11939: 
11940: sub unmark_as_readonly {
11941:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11942:     # for portfolio submissions, $what contains [$symb,$crsid] 
11943:     my ($domain,$user,$what,$file_name,$group) = @_;
11944:     $file_name = &declutter_portfile($file_name);
11945:     my $symb_crs = $what;
11946:     if (ref($what)) { $symb_crs=join('',@$what); }
11947:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11948:     my ($tmp)=keys(%current_permissions);
11949:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11950:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11951:     foreach my $file (@readonly_files) {
11952: 	my $clean_file = &declutter_portfile($file);
11953: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11954: 	my $current_locks = $current_permissions{$file};
11955:         my @new_locks;
11956:         my @del_keys;
11957:         if (ref($current_locks) eq "ARRAY"){
11958:             foreach my $locker (@{$current_locks}) {
11959:                 my $compare=$locker;
11960:                 if (ref($locker) eq 'ARRAY') {
11961:                     $compare=join('',@{$locker});
11962:                     if ($compare ne $symb_crs) {
11963:                         push(@new_locks, $locker);
11964:                     }
11965:                 }
11966:             }
11967:             if (scalar(@new_locks) > 0) {
11968:                 $current_permissions{$file} = \@new_locks;
11969:             } else {
11970:                 push(@del_keys, $file);
11971:                 &del('file_permissions',\@del_keys, $domain, $user);
11972:                 delete($current_permissions{$file});
11973:             }
11974:         }
11975:     }
11976:     &put('file_permissions',\%current_permissions,$domain,$user);
11977:     return;
11978: }
11979: 
11980: # ------------------------------------------------------------ Directory lister
11981: 
11982: sub dirlist {
11983:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11984:     $uri=~s/^\///;
11985:     $uri=~s/\/$//;
11986:     my ($udom, $uname);
11987:     if ($getuserdir) {
11988:         $udom = $userdomain;
11989:         $uname = $username;
11990:     } else {
11991:         (undef,$udom,$uname)=split(/\//,$uri);
11992:         if(defined($userdomain)) {
11993:             $udom = $userdomain;
11994:         }
11995:         if(defined($username)) {
11996:             $uname = $username;
11997:         }
11998:     }
11999:     my ($dirRoot,$listing,@listing_results);
12000: 
12001:     $dirRoot = $perlvar{'lonDocRoot'};
12002:     if (defined($getpropath)) {
12003:         $dirRoot = &propath($udom,$uname);
12004:         $dirRoot =~ s/\/$//;
12005:     } elsif (defined($getuserdir)) {
12006:         my $subdir=$uname.'__';
12007:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
12008:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
12009:                    ."/$udom/$subdir/$uname";
12010:     } elsif (defined($alternateRoot)) {
12011:         $dirRoot = $alternateRoot;
12012:     }
12013: 
12014:     if($udom) {
12015:         if($uname) {
12016:             my $uhome = &homeserver($uname,$udom);
12017:             if ($uhome eq 'no_host') {
12018:                 return ([],'no_host');
12019:             }
12020:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
12021:                               .$getuserdir.':'.&escape($dirRoot)
12022:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
12023:             if ($listing eq 'unknown_cmd') {
12024:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
12025:             } else {
12026:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12027:             }
12028:             if ($listing eq 'unknown_cmd') {
12029:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
12030:                 @listing_results = split(/:/,$listing);
12031:             } else {
12032:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
12033:             }
12034:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
12035:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
12036:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12037:                 return ([],$listing);
12038:             } else {
12039:                 return (\@listing_results);
12040:             }
12041:         } elsif(!$alternateRoot) {
12042:             my (%allusers,%listerror);
12043: 	    my %servers = &get_servers($udom,'library');
12044:  	    foreach my $tryserver (keys(%servers)) {
12045:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
12046:                                   &escape($udom),$tryserver);
12047:                 if ($listing eq 'unknown_cmd') {
12048: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
12049: 				      $udom, $tryserver);
12050:                 } else {
12051:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
12052:                 }
12053: 		if ($listing eq 'unknown_cmd') {
12054: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
12055: 				      $udom, $tryserver);
12056: 		    @listing_results = split(/:/,$listing);
12057: 		} else {
12058: 		    @listing_results =
12059: 			map { &unescape($_); } split(/:/,$listing);
12060: 		}
12061:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
12062:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
12063:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
12064:                     $listerror{$tryserver} = $listing;
12065:                 } else {
12066: 		    foreach my $line (@listing_results) {
12067: 			my ($entry) = split(/&/,$line,2);
12068: 			$allusers{$entry} = 1;
12069: 		    }
12070: 		}
12071:             }
12072:             my @alluserslist=();
12073:             foreach my $user (sort(keys(%allusers))) {
12074:                 push(@alluserslist,$user.'&user');
12075:             }
12076: 
12077:             if (!%listerror) {
12078:                 # no errors
12079:                 return (\@alluserslist);
12080:             } elsif (scalar(keys(%servers)) == 1) {
12081:                 # one library server, one error 
12082:                 my ($key) = keys(%listerror);
12083:                 return (\@alluserslist, $listerror{$key});
12084:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
12085:                 # con_lost indicates that we might miss data from at least one
12086:                 # library server
12087:                 return (\@alluserslist, 'con_lost');
12088:             } else {
12089:                 # multiple library servers and no con_lost -> data should be
12090:                 # complete. 
12091:                 return (\@alluserslist);
12092:             }
12093: 
12094:         } else {
12095:             return ([],'missing username');
12096:         }
12097:     } elsif(!defined($getpropath)) {
12098:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
12099:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
12100:         return (\@all_domains);
12101:     } else {
12102:         return ([],'missing domain');
12103:     }
12104: }
12105: 
12106: # --------------------------------------------- GetFileTimestamp
12107: # This function utilizes dirlist and returns the date stamp for
12108: # when it was last modified.  It will also return an error of -1
12109: # if an error occurs
12110: 
12111: sub GetFileTimestamp {
12112:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
12113:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
12114:     $studentName   = &LONCAPA::clean_username($studentName);
12115:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
12116:                                     undef,$getuserdir);
12117:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12118:         return -1;
12119:     }
12120:     if (ref($fileref) eq 'ARRAY') {
12121:         my @stats = split('&',$fileref->[0]);
12122:         # @stats contains first the filename, then the stat output
12123:         return $stats[10]; # so this is 10 instead of 9.
12124:     } else {
12125:         return -1;
12126:     }
12127: }
12128: 
12129: sub stat_file {
12130:     my ($uri) = @_;
12131:     $uri = &clutter_with_no_wrapper($uri);
12132: 
12133:     my ($udom,$uname,$file);
12134:     if ($uri =~ m-^/(uploaded|editupload)/-) {
12135: 	($udom,$uname,$file) =
12136: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
12137: 	$file = 'userfiles/'.$file;
12138:     }
12139:     if ($uri =~ m-^/res/-) {
12140: 	($udom,$uname) = 
12141: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
12142: 	$file = $uri;
12143:     }
12144: 
12145:     if (!$udom || !$uname || !$file) {
12146: 	# unable to handle the uri
12147: 	return ();
12148:     }
12149:     my $getpropath;
12150:     if ($file =~ /^userfiles\//) {
12151:         $getpropath = 1;
12152:     }
12153:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
12154:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
12155:         return ();
12156:     } else {
12157:         if (ref($listref) eq 'ARRAY') {
12158:             my @stats = split('&',$listref->[0]);
12159: 	    shift(@stats); #filename is first
12160: 	    return @stats;
12161:         }
12162:     }
12163:     return ();
12164: }
12165: 
12166: # --------------------------------------------------------- recursedirs
12167: # Recursive function to traverse either a specific user's Authoring Space
12168: # or corresponding Published Resource Space, and populate the hash ref:
12169: # $dirhashref with URLs of all directories, and if $filehashref hash
12170: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
12171: # or .rights files in resource space, and .meta, .save, .log, .bak and
12172: # .rights files in Authoring Space.
12173: #
12174: # Inputs:
12175: #
12176: # $is_home - true if current server is home server for user's space
12177: # $recurse - if true will also traverse subdirectories recursively
12178: # $include - reference to hash containing allowed file extensions.  If provided,
12179: #             files which do not have a matching extension will be ignored.
12180: # $exclude - reference to hash containing excluded file extensions.  If provided,
12181: #             files which have a matching extension will be ignored.
12182: # $nonemptydir - if true, will only populate $fileshashref hash entry for a particular
12183: #             directory with first file found (with acceptable extension).
12184: # $addtopdir - if true, set $dirhashref->{'/'} = 1 
12185: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
12186: # $relpath - Current path (relative to top level).
12187: # $dirhashref - reference to hash to populate with URLs of directories (Required)
12188: # $filehashref - reference to hash to populate with URLs of files (Optional)
12189: #
12190: # Returns: nothing
12191: #
12192: # Side Effects: populates $dirhashref, and $filehashref (if provided).
12193: #
12194: # Currently used by interface/londocs.pm to create linked select boxes for
12195: # directory and filename to import a Course "Author" resource into a course, and
12196: # also to create linked select boxes for Authoring Space and Directory to choose
12197: # save location for creation of a new "standard" problem from the Course Editor.
12198: #
12199: 
12200: sub recursedirs {
12201:     my ($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$relpath,$dirhashref,$filehashref) = @_;
12202:     return unless (ref($dirhashref) eq 'HASH');
12203:     my $docroot = $perlvar{'lonDocRoot'};
12204:     my $currpath = $docroot.$toppath;
12205:     if ($relpath ne '') {
12206:         $currpath .= "/$relpath";
12207:     }
12208:     my ($savefile,$checkinc,$checkexc);
12209:     if (ref($filehashref)) {
12210:         $savefile = 1;
12211:     }
12212:     if (ref($include) eq 'HASH') {
12213:         $checkinc = 1;
12214:     }
12215:     if (ref($exclude) eq 'HASH') {
12216:         $checkexc = 1;
12217:     }
12218:     if ($is_home) {
12219:         if ((-e $currpath) && (opendir(my $dirh,$currpath))) {
12220:             my $filecount = 0;
12221:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
12222:                 next if ($item eq '');
12223:                 if (-d "$currpath/$item") {
12224:                     my $newpath;
12225:                     if ($relpath ne '') {
12226:                         $newpath = "$relpath/$item";
12227:                     } else {
12228:                         $newpath = $item;
12229:                     }
12230:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12231:                     if ($recurse) {
12232:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
12233:                     }
12234:                 } elsif (($savefile) || ($relpath eq '')) {
12235:                     next if ($nonemptydir && $filecount);
12236:                     if ($checkinc || $checkexc) {
12237:                         my ($extension) = ($item =~ /\.(\w+)$/);
12238:                         if ($checkinc) {
12239:                             next unless ($extension && $include->{$extension});
12240:                         }
12241:                         if ($checkexc) {
12242:                             next if ($extension && $exclude->{$extension});
12243:                         }
12244:                     }
12245:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12246:                         $dirhashref->{'/'} = 1;
12247:                     }
12248:                     if ($savefile) {
12249:                         if ($relpath eq '') {
12250:                             $filehashref->{'/'}{$item} = 1;
12251:                         } else {
12252:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
12253:                         }
12254:                     }
12255:                     $filecount ++;
12256:                 }
12257:             }
12258:             closedir($dirh);
12259:         }
12260:     } else {
12261:         my ($dirlistref,$listerror) =
12262:             &dirlist($toppath.$relpath);
12263:         my @dir_lines;
12264:         my $dirptr=16384;
12265:         if (ref($dirlistref) eq 'ARRAY') {
12266:             my $filecount = 0;
12267:             foreach my $dir_line (sort
12268:                               {
12269:                                   my ($afile)=split('&',$a,2);
12270:                                   my ($bfile)=split('&',$b,2);
12271:                                   return (lc($afile) cmp lc($bfile));
12272:                               } (@{$dirlistref})) {
12273:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
12274:                     split(/\&/,$dir_line,16);
12275:                 $item =~ s/\s+$//;
12276:                 next if (($item =~ /^\.\.?$/) || ($obs));
12277:                 if ($dirptr&$testdir) {
12278:                     my $newpath;
12279:                     if ($relpath) {
12280:                         $newpath = "$relpath/$item";
12281:                     } else {
12282:                         $newpath = $item;
12283:                     }
12284:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
12285:                     if ($recurse) {
12286:                         &recursedirs($is_home,$recurse,$include,$exclude,$nonemptydir,$addtopdir,$toppath,$newpath,$dirhashref,$filehashref);
12287:                     }
12288:                 } elsif (($savefile) || ($relpath eq '')) {
12289:                     next if ($nonemptydir && $filecount);
12290:                     if ($checkinc || $checkexc) {
12291:                         my $extension;
12292:                         if ($checkinc) {
12293:                             next unless ($extension && $include->{$extension});
12294:                         }
12295:                         if ($checkexc) {
12296:                             next if ($extension && $exclude->{$extension});
12297:                         }
12298:                     }
12299:                     if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12300:                         $dirhashref->{'/'} = 1;
12301:                     }
12302:                     if ($savefile) {
12303:                         if ($relpath eq '') {
12304:                             $filehashref->{'/'}{$item} = 1;
12305:                         } else {
12306:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
12307:                         }
12308:                     }
12309:                     $filecount ++; 
12310:                 }
12311:             }
12312:         }
12313:     }
12314:     if ($addtopdir) {
12315:         if (($relpath eq '') && (!exists($dirhashref->{'/'}))) {
12316:             $dirhashref->{'/'} = 1;
12317:         }
12318:     }
12319:     return;
12320: }
12321: 
12322: sub priv_exclude {
12323:     return {
12324:              meta => 1,
12325:              save => 1,
12326:              log => 1,
12327:              bak => 1,
12328:              rights => 1,
12329:              DS_Store => 1,
12330:            };
12331: }
12332: 
12333: # -------------------------------------------------------- Value of a Condition
12334: 
12335: # gets the value of a specific preevaluated condition
12336: #    stored in the string  $env{user.state.<cid>}
12337: # or looks up a condition reference in the bighash and if if hasn't
12338: # already been evaluated recurses into docondval to get the value of
12339: # the condition, then memoizing it to 
12340: #   $env{user.state.<cid>.<condition>}
12341: sub directcondval {
12342:     my $number=shift;
12343:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
12344: 	&Apache::lonuserstate::evalstate();
12345:     }
12346:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
12347: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
12348:     } elsif ($number =~ /^_/) {
12349: 	my $sub_condition;
12350: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12351: 		&GDBM_READER(),0640)) {
12352: 	    $sub_condition=$bighash{'conditions'.$number};
12353: 	    untie(%bighash);
12354: 	}
12355: 	my $value = &docondval($sub_condition);
12356: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
12357: 	return $value;
12358:     }
12359:     if ($env{'user.state.'.$env{'request.course.id'}}) {
12360:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
12361:     } else {
12362:        return 2;
12363:     }
12364: }
12365: 
12366: # get the collection of conditions for this resource
12367: sub condval {
12368:     my $condidx=shift;
12369:     my $allpathcond='';
12370:     foreach my $cond (split(/\|/,$condidx)) {
12371: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
12372: 	    $allpathcond.=
12373: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
12374: 	}
12375:     }
12376:     $allpathcond=~s/\|$//;
12377:     return &docondval($allpathcond);
12378: }
12379: 
12380: #evaluates an expression of conditions
12381: sub docondval {
12382:     my ($allpathcond) = @_;
12383:     my $result=0;
12384:     if ($env{'request.course.id'}
12385: 	&& defined($allpathcond)) {
12386: 	my $operand='|';
12387: 	my @stack;
12388: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
12389: 	    if ($chunk eq '(') {
12390: 		push @stack,($operand,$result);
12391: 	    } elsif ($chunk eq ')') {
12392: 		my $before=pop @stack;
12393: 		if (pop @stack eq '&') {
12394: 		    $result=$result>$before?$before:$result;
12395: 		} else {
12396: 		    $result=$result>$before?$result:$before;
12397: 		}
12398: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
12399: 		$operand=$chunk;
12400: 	    } else {
12401: 		my $new=directcondval($chunk);
12402: 		if ($operand eq '&') {
12403: 		    $result=$result>$new?$new:$result;
12404: 		} else {
12405: 		    $result=$result>$new?$result:$new;
12406: 		}
12407: 	    }
12408: 	}
12409:     }
12410:     return $result;
12411: }
12412: 
12413: # ---------------------------------------------------- Devalidate courseresdata
12414: 
12415: sub devalidatecourseresdata {
12416:     my ($coursenum,$coursedomain)=@_;
12417:     my $hashid=$coursenum.':'.$coursedomain;
12418:     &devalidate_cache_new('courseres',$hashid);
12419: }
12420: 
12421: 
12422: # --------------------------------------------------- Course Resourcedata Query
12423: #
12424: #  Parameters:
12425: #      $coursenum    - Number of the course.
12426: #      $coursedomain - Domain at which the course was created.
12427: #  Returns:
12428: #     A hash of the course parameters along (I think) with timestamps
12429: #     and version info.
12430: 
12431: sub get_courseresdata {
12432:     my ($coursenum,$coursedomain)=@_;
12433:     my $coursehom=&homeserver($coursenum,$coursedomain);
12434:     my $hashid=$coursenum.':'.$coursedomain;
12435:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
12436:     my %dumpreply;
12437:     unless (defined($cached)) {
12438: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
12439: 	$result=\%dumpreply;
12440: 	my ($tmp) = keys(%dumpreply);
12441: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12442: 	    &do_cache_new('courseres',$hashid,$result,600);
12443: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
12444: 	    return $tmp;
12445: 	} elsif ($tmp =~ /^(error)/) {
12446: 	    $result=undef;
12447: 	    &do_cache_new('courseres',$hashid,$result,600);
12448: 	}
12449:     }
12450:     return $result;
12451: }
12452: 
12453: sub devalidateuserresdata {
12454:     my ($uname,$udom)=@_;
12455:     my $hashid="$udom:$uname";
12456:     &devalidate_cache_new('userres',$hashid);
12457: }
12458: 
12459: sub get_userresdata {
12460:     my ($uname,$udom)=@_;
12461:     #most student don\'t have any data set, check if there is some data
12462:     if (&EXT_cache_status($udom,$uname)) { return undef; }
12463: 
12464:     my $hashid="$udom:$uname";
12465:     my ($result,$cached)=&is_cached_new('userres',$hashid);
12466:     if (!defined($cached)) {
12467: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
12468: 	$result=\%resourcedata;
12469: 	&do_cache_new('userres',$hashid,$result,600);
12470:     }
12471:     my ($tmp)=keys(%$result);
12472:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
12473: 	return $result;
12474:     }
12475:     #error 2 occurs when the .db doesn't exist
12476:     if ($tmp!~/error: 2 /) {
12477:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
12478: 	    &logthis("<font color=\"blue\">WARNING:".
12479: 		     " Trying to get resource data for ".
12480: 		     $uname." at ".$udom.": ".
12481: 		     $tmp."</font>");
12482:         }
12483:     } elsif ($tmp=~/error: 2 /) {
12484: 	#&EXT_cache_set($udom,$uname);
12485: 	&do_cache_new('userres',$hashid,undef,600);
12486: 	undef($tmp); # not really an error so don't send it back
12487:     }
12488:     return $tmp;
12489: }
12490: #----------------------------------------------- resdata - return resource data
12491: #  Purpose:
12492: #    Return resource data for either users or for a course.
12493: #  Parameters:
12494: #     $name      - Course/user name.
12495: #     $domain    - Name of the domain the user/course is registered on.
12496: #     $type      - Type of thing $name is (must be 'course' or 'user')
12497: #     $mapp      - decluttered URL of enclosing map  
12498: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
12499: #     $recurseup - Ref to array of map URLs, starting with map containing
12500: #                  $mapp up through hierarchy of nested maps to top level map.  
12501: #     $courseid  - CourseID (first part of param identifier).
12502: #     $modifier  - Middle part of param identifier.
12503: #     $what      - Last part of param identifier.
12504: #     @which     - Array of names of resources desired.
12505: #  Returns:
12506: #     The value of the first reasource in @which that is found in the
12507: #     resource hash.
12508: #  Exceptional Conditions:
12509: #     If the $type passed in is not valid (not the string 'course' or 
12510: #     'user', an undefined  reference is returned.
12511: #     If none of the resources are found, an undef is returned
12512: sub resdata {
12513:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
12514:         $modifier,$what,@which)=@_;
12515:     my $result;
12516:     if ($type eq 'course') {
12517: 	$result=&get_courseresdata($name,$domain);
12518:     } elsif ($type eq 'user') {
12519: 	$result=&get_userresdata($name,$domain);
12520:     }
12521:     if (!ref($result)) { return $result; }    
12522:     foreach my $item (@which) {
12523:         if ($item->[1] eq 'course') {
12524:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
12525:                 unless ($$recursed) {
12526:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
12527:                     $$recursed = 1;
12528:                 }
12529:                 foreach my $item (@${recurseup}) {
12530:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
12531:                     last if (defined($result->{$norecursechk}));
12532:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
12533:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
12534:                 }
12535:             }
12536:         }
12537:         if (defined($result->{$item->[0]})) {
12538: 	    return [$result->{$item->[0]},$item->[1]];
12539: 	}
12540:     }
12541:     return undef;
12542: }
12543: 
12544: sub get_domain_lti {
12545:     my ($cdom,$context) = @_;
12546:     my ($name,$cachename,%lti);
12547:     if ($context eq 'consumer') {
12548:         $name = 'ltitools';
12549:     } elsif ($context eq 'provider') {
12550:         $name = 'lti';
12551:     } elsif ($context eq 'linkprot') {
12552:         $name = 'ltisec';
12553:     } else {
12554:         return %lti;
12555:     }
12556:     if ($context eq 'linkprot') {
12557:         $cachename = $context;
12558:     } else {
12559:         $cachename = $name;
12560:     }
12561:     my ($result,$cached)=&is_cached_new($cachename,$cdom);
12562:     if (defined($cached)) {
12563:         if (ref($result) eq 'HASH') {
12564:             %lti = %{$result};
12565:         }
12566:     } else {
12567:         my %domconfig = &get_dom('configuration',[$name],$cdom);
12568:         if (ref($domconfig{$name}) eq 'HASH') {
12569:             if ($context eq 'linkprot') {
12570:                 if (ref($domconfig{$name}{'linkprot'}) eq 'HASH') {
12571:                     %lti = %{$domconfig{$name}{'linkprot'}};
12572:                 }
12573:             } else {
12574:                 %lti = %{$domconfig{$name}};
12575:             }
12576:         }
12577:         my $cachetime = 24*60*60;
12578:         &do_cache_new($cachename,$cdom,\%lti,$cachetime);
12579:     }
12580:     return %lti;
12581: }
12582: 
12583: sub get_course_lti {
12584:     my ($cnum,$cdom,$context) = @_;
12585:     my ($name,$cachename,%lti);
12586:     if ($context eq 'consumer') {
12587:         $name = 'ltitools';
12588:         $cachename = 'courseltitools';
12589:     } elsif ($context eq 'provider') {
12590:         $name = 'lti';
12591:         $cachename = 'courselti';
12592:     } else {
12593:         return %lti;
12594:     }
12595:     my $hashid=$cdom.'_'.$cnum;
12596:     my ($result,$cached)=&is_cached_new($cachename,$hashid);
12597:     if (defined($cached)) {
12598:         if (ref($result) eq 'HASH') {
12599:             %lti = %{$result};
12600:         }
12601:     } else {
12602:         %lti = &dump($name,$cdom,$cnum,undef,undef,undef,1);
12603:         my $cachetime = 24*60*60;
12604:         &do_cache_new($cachename,$hashid,\%lti,$cachetime);
12605:     }
12606:     return %lti;
12607: }
12608: 
12609: sub courselti_itemid {
12610:     my ($cnum,$cdom,$url,$method,$params,$context) = @_;
12611:     my ($chome,$itemid);
12612:     $chome = &homeserver($cnum,$cdom);
12613:     return if ($chome eq 'no_host');
12614:     if (ref($params) eq 'HASH') {
12615:         my $rep;
12616:         if (grep { $_ eq $chome } current_machine_ids()) {
12617:             $rep = LONCAPA::Lond::crslti_itemid($cdom,$cnum,$url,$method,$params,$perlvar{'lonVersion'});
12618:         } else {
12619:             my $escurl = &escape($url);
12620:             my $escmethod = &escape($method);
12621:             my $items = &freeze_escape($params);
12622:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$chome);
12623:         }
12624:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12625:                 ($rep eq 'unknown_cmd')) {
12626:             $itemid = $rep;
12627:         }
12628:     }
12629:     return $itemid;
12630: }
12631: 
12632: sub domainlti_itemid {
12633:     my ($cdom,$url,$method,$params,$context) = @_;
12634:     my ($primary_id,$itemid);
12635:     $primary_id = &domain($cdom,'primary');
12636:     return if ($primary_id eq '');
12637:     if (ref($params) eq 'HASH') {
12638:         my $rep;
12639:         if (grep { $_ eq $primary_id } current_machine_ids()) {
12640:             $rep = LONCAPA::Lond::domlti_itemid($cdom,$context,$url,$method,$params,$perlvar{'lonVersion'});
12641:         } else {
12642:             my $cnum = '';
12643:             my $escurl = &escape($url);
12644:             my $escmethod = &escape($method);
12645:             my $items = &freeze_escape($params);
12646:             $rep = &reply("encrypt:lti:$cdom:$cnum:$context:$escurl:$escmethod:$items",$primary_id);
12647:         }
12648:         unless (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
12649:                 ($rep eq 'unknown_cmd')) {
12650:             $itemid = $rep;
12651:         }
12652:     }
12653:     return $itemid;
12654: }
12655: 
12656: sub count_supptools {
12657:     my ($cnum,$cdom,$ignorecache,$reload)=@_;
12658:     my $hashid=$cnum.':'.$cdom;
12659:     my ($numexttools,$cached);
12660:     unless ($ignorecache) {
12661:         ($numexttools,$cached) = &is_cached_new('supptools',$hashid);
12662:     }
12663:     unless (defined($cached)) {
12664:         my $chome=&homeserver($cnum,$cdom);
12665:         $numexttools = 0;
12666:         unless ($chome eq 'no_host') {
12667:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$reload);
12668:             if (ref($supplemental) eq 'HASH') {
12669:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12670:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
12671:                         if ($key =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
12672:                             $numexttools ++;
12673:                         }
12674:                     }
12675:                 }
12676:             }
12677:         }
12678:         &do_cache_new('supptools',$hashid,$numexttools,600);
12679:     }
12680:     return $numexttools;
12681: }
12682: 
12683: sub has_unhidden_suppfiles {
12684:     my ($cnum,$cdom,$ignorecache,$possdel)=@_;
12685:     my $hashid=$cnum.':'.$cdom;
12686:     my ($showsupp,$cached);
12687:     unless ($ignorecache) {
12688:         ($showsupp,$cached) = &is_cached_new('showsupp',$hashid);
12689:     }
12690:     unless (defined($cached)) {
12691:         my $chome=&homeserver($cnum,$cdom);
12692:         unless ($chome eq 'no_host') {
12693:             my ($supplemental) = &Apache::loncommon::get_supplemental($cnum,$cdom,$ignorecache,$possdel);
12694:             if (ref($supplemental) eq 'HASH') {
12695:                 if ((ref($supplemental->{'ids'}) eq 'HASH') && (ref($supplemental->{'hidden'}) eq 'HASH')) {
12696:                     foreach my $key (keys(%{$supplemental->{'ids'}})) {
12697:                         next if ($key =~ /\.sequence$/);
12698:                         if (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
12699:                             foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
12700:                                 unless ($supplemental->{'hidden'}->{$id}) {
12701:                                     $showsupp = 1;
12702:                                     last;
12703:                                 }
12704:                             }
12705:                         }
12706:                         last if ($showsupp);
12707:                     }
12708:                 }
12709:             }
12710:         }
12711:         &do_cache_new('showsupp',$hashid,$showsupp,600);
12712:     }
12713:     return $showsupp;
12714: }
12715: 
12716: #
12717: # EXT resource caching routines
12718: #
12719: 
12720: {
12721: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12722: #
12723: # The course for which we cache
12724: my $cachedmapkey='';
12725: # The cached recursive maps for this course
12726: my %cachedmaps=();
12727: # When this was last done
12728: my $cachedmaptime='';
12729: 
12730: sub clear_EXT_cache_status {
12731:     &delenv('cache.EXT.');
12732: }
12733: 
12734: sub EXT_cache_status {
12735:     my ($target_domain,$target_user) = @_;
12736:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12737:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12738:         # We know already the user has no data
12739:         return 1;
12740:     } else {
12741:         return 0;
12742:     }
12743: }
12744: 
12745: sub EXT_cache_set {
12746:     my ($target_domain,$target_user) = @_;
12747:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12748:     #&appenv({$cachename => time});
12749: }
12750: 
12751: # --------------------------------------------------------- Value of a Variable
12752: sub EXT {
12753: 
12754:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid,$recurseupref)=@_;
12755:     unless ($varname) { return ''; }
12756:     #get real user name/domain, courseid and symb
12757:     my $courseid;
12758:     my $publicuser;
12759:     if ($symbparm) {
12760: 	$symbparm=&get_symb_from_alias($symbparm);
12761:     }
12762:     if (!($uname && $udom)) {
12763:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12764:       if (!$symbparm) {	$symbparm=$cursymb; }
12765:     } else {
12766: 	$courseid=$env{'request.course.id'};
12767:     }
12768:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12769:     my $rest;
12770:     if (defined($therest[0])) {
12771:        $rest=join('.',@therest);
12772:     } else {
12773:        $rest='';
12774:     }
12775: 
12776:     my $qualifierrest=$qualifier;
12777:     if ($rest) { $qualifierrest.='.'.$rest; }
12778:     my $spacequalifierrest=$space;
12779:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12780:     if ($realm eq 'user') {
12781: # --------------------------------------------------------------- user.resource
12782: 	if ($space eq 'resource') {
12783: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12784: 		  || defined($Apache::lonhomework::parsing_a_task))
12785: 		 &&
12786: 		 ($symbparm eq &symbread()) ) {
12787: 		# if we are in the middle of processing the resource the
12788: 		# get the value we are planning on committing
12789:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12790:                     return $Apache::lonhomework::results{$qualifierrest};
12791:                 } else {
12792:                     return $Apache::lonhomework::history{$qualifierrest};
12793:                 }
12794: 	    } else {
12795: 		my %restored;
12796: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12797: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12798: 		} else {
12799: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12800: 		}
12801: 		return $restored{$qualifierrest};
12802: 	    }
12803: # ----------------------------------------------------------------- user.access
12804:         } elsif ($space eq 'access') {
12805: 	    # FIXME - not supporting calls for a specific user
12806:             return &allowed($qualifier,$rest);
12807: # ------------------------------------------ user.preferences, user.environment
12808:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12809: 	    if (($uname eq $env{'user.name'}) &&
12810: 		($udom eq $env{'user.domain'})) {
12811: 		return $env{join('.',('environment',$qualifierrest))};
12812: 	    } else {
12813: 		my %returnhash;
12814: 		if (!$publicuser) {
12815: 		    %returnhash=&userenvironment($udom,$uname,
12816: 						 $qualifierrest);
12817: 		}
12818: 		return $returnhash{$qualifierrest};
12819: 	    }
12820: # ----------------------------------------------------------------- user.course
12821:         } elsif ($space eq 'course') {
12822: 	    # FIXME - not supporting calls for a specific user
12823:             return $env{join('.',('request.course',$qualifier))};
12824: # ------------------------------------------------------------------- user.role
12825:         } elsif ($space eq 'role') {
12826: 	    # FIXME - not supporting calls for a specific user
12827:             my ($role,$where)=split(/\./,$env{'request.role'});
12828:             if ($qualifier eq 'value') {
12829: 		return $role;
12830:             } elsif ($qualifier eq 'extent') {
12831:                 return $where;
12832:             }
12833: # ----------------------------------------------------------------- user.domain
12834:         } elsif ($space eq 'domain') {
12835:             return $udom;
12836: # ------------------------------------------------------------------- user.name
12837:         } elsif ($space eq 'name') {
12838:             return $uname;
12839: # ---------------------------------------------------- Any other user namespace
12840:         } else {
12841: 	    my %reply;
12842: 	    if (!$publicuser) {
12843: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12844: 	    }
12845: 	    return $reply{$qualifierrest};
12846:         }
12847:     } elsif ($realm eq 'query') {
12848: # ---------------------------------------------- pull stuff out of query string
12849:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12850: 						[$spacequalifierrest]);
12851: 	return $env{'form.'.$spacequalifierrest}; 
12852:    } elsif ($realm eq 'request') {
12853: # ------------------------------------------------------------- request.browser
12854:         if ($space eq 'browser') {
12855:             return $env{'browser.'.$qualifier};
12856: # ------------------------------------------------------------ request.filename
12857:         } else {
12858:             return $env{'request.'.$spacequalifierrest};
12859:         }
12860:     } elsif ($realm eq 'course') {
12861: # ---------------------------------------------------------- course.description
12862:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12863:     } elsif ($realm eq 'resource') {
12864: 
12865: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12866: 	    if (!$symbparm) { $symbparm=&symbread(); }
12867: 	}
12868: 
12869:         if ($qualifier eq '') {
12870: 	    if ($space eq 'title') {
12871: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12872: 	        return &gettitle($symbparm);
12873: 	    }
12874: 	
12875: 	    if ($space eq 'map') {
12876: 	        my ($map) = &decode_symb($symbparm);
12877: 	        return &symbread($map);
12878: 	    }
12879:             if ($space eq 'maptitle') {
12880:                 my ($map) = &decode_symb($symbparm);
12881:                 return &gettitle($map);
12882:             }
12883: 	    if ($space eq 'filename') {
12884: 	        if ($symbparm) {
12885: 		    return &clutter((&decode_symb($symbparm))[2]);
12886: 	        }
12887: 	        return &hreflocation('',$env{'request.filename'});
12888: 	    }
12889: 
12890:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12891:                 if ($space eq 'visibleparts') {
12892:                     my $navmap = Apache::lonnavmaps::navmap->new();
12893:                     my $item;
12894:                     if (ref($navmap)) {
12895:                         my $res = $navmap->getBySymb($symbparm);
12896:                         my $parts = $res->parts();
12897:                         if (ref($parts) eq 'ARRAY') {
12898:                             $item = join(',',@{$parts});
12899:                         }
12900:                         undef($navmap);
12901:                     }
12902:                     return $item;
12903:                 }
12904:             }
12905:         }
12906: 
12907: 	my ($section, $group, @groups, @recurseup, $recursed);
12908:         if (ref($recurseupref) eq 'ARRAY') {
12909:             @recurseup = @{$recurseupref};
12910:             $recursed = 1;
12911:         }
12912: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12913:         if (($courseid eq '') && ($cid)) {
12914:             $courseid = $cid;
12915:         }
12916: 	if (($symbparm && $courseid) && 
12917: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12918: 
12919: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12920: 
12921: # ----------------------------------------------------- Cascading lookup scheme
12922: 	    my $symbp=$symbparm;
12923: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12924: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12925:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12926: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12927: 	    if (($env{'user.name'} eq $uname) &&
12928: 		($env{'user.domain'} eq $udom)) {
12929: 		$section=$env{'request.course.sec'};
12930:                 @groups = split(/:/,$env{'request.course.groups'});  
12931:                 @groups=&sort_course_groups($courseid,@groups); 
12932: 	    } else {
12933: 		if (! defined($usection)) {
12934: 		    $section=&getsection($udom,$uname,$courseid);
12935: 		} else {
12936: 		    $section = $usection;
12937: 		}
12938:                 @groups = &get_users_groups($udom,$uname,$courseid);
12939: 	    }
12940: 
12941: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12942: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12943:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12944: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12945: 
12946: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12947: 	    my $courselevelr=$courseid.'.'.$symbparm;
12948:             $courseleveli=$courseid.'.'.$recurseparm;
12949: 	    $courselevelm=$courseid.'.'.$mapparm;
12950: 
12951: # ----------------------------------------------------------- first, check user
12952: 
12953: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12954:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12955: 				       ([$courselevelr,'resource'],
12956: 					[$courselevelm,'map'     ],
12957:                                         [$courseleveli,'map'     ],
12958: 					[$courselevel, 'course'  ]));
12959: 	    if (defined($userreply)) { return &get_reply($userreply); }
12960: 
12961: # ------------------------------------------------ second, check some of course
12962:             my $coursereply;
12963:             if (@groups > 0) {
12964:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12965:                                        $recurseparm,$mapparm,$spacequalifierrest,
12966:                                        $mapp,\$recursed,\@recurseup);
12967:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12968:             }
12969: 
12970: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12971: 				  $env{'course.'.$courseid.'.domain'},
12972: 				  'course',$mapp,\$recursed,\@recurseup,
12973:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12974: 				  ([$seclevelr,   'resource'],
12975: 				   [$seclevelm,   'map'     ],
12976:                                    [$secleveli,   'map'     ],
12977: 				   [$seclevel,    'course'  ],
12978: 				   [$courselevelr,'resource']));
12979: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12980: 
12981: # ------------------------------------------------------ third, check map parms
12982: 	    my %parmhash=();
12983: 	    my $thisparm='';
12984: 	    if (tie(%parmhash,'GDBM_File',
12985: 		    $env{'request.course.fn'}.'_parms.db',
12986: 		    &GDBM_READER(),0640)) {
12987: 		$thisparm=$parmhash{$symbparm};
12988: 		untie(%parmhash);
12989: 	    }
12990: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12991: 	}
12992: # ------------------------------------------ fourth, look in resource metadata
12993:  
12994:         my $what = $spacequalifierrest;
12995: 	$what=~s/\./\_/;
12996: 	my $filename;
12997: 	if (!$symbparm) { $symbparm=&symbread(); }
12998: 	if ($symbparm) {
12999: 	    $filename=(&decode_symb($symbparm))[2];
13000: 	} else {
13001: 	    $filename=$env{'request.filename'};
13002: 	}
13003:         my $toolsymb;
13004:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
13005:             $toolsymb = $symbparm;
13006:         }
13007: 	my $metadata=&metadata($filename,$what,$toolsymb);
13008: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13009: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
13010: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
13011: 
13012: # ----------------------------------------------- fifth, look in rest of course
13013: 	if ($symbparm && defined($courseid) && 
13014: 	    $courseid eq $env{'request.course.id'}) {
13015: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
13016: 				     $env{'course.'.$courseid.'.domain'},
13017: 				     'course',$mapp,\$recursed,\@recurseup,
13018:                                      $courseid,'.',$spacequalifierrest,
13019: 				     ([$courselevelm,'map'   ],
13020:                                       [$courseleveli,'map'   ],
13021: 				      [$courselevel, 'course']));
13022: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
13023: 	}
13024: # ------------------------------------------------------------------ Cascade up
13025: 	unless ($space eq '0') {
13026: 	    my @parts=split(/_/,$space);
13027: 	    my $id=pop(@parts);
13028: 	    my $part=join('_',@parts);
13029: 	    if ($part eq '') { $part='0'; }
13030: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
13031: 				 $symbparm,$udom,$uname,$section,1);
13032: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
13033: 	}
13034: 	if ($recurse) { return undef; }
13035: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
13036: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
13037: # ---------------------------------------------------- Any other user namespace
13038:     } elsif ($realm eq 'environment') {
13039: # ----------------------------------------------------------------- environment
13040: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
13041: 	    return $env{'environment.'.$spacequalifierrest};
13042: 	} else {
13043: 	    if ($uname eq 'anonymous' && $udom eq '') {
13044: 		return '';
13045: 	    }
13046: 	    my %returnhash=&userenvironment($udom,$uname,
13047: 					    $spacequalifierrest);
13048: 	    return $returnhash{$spacequalifierrest};
13049: 	}
13050:     } elsif ($realm eq 'system') {
13051: # ----------------------------------------------------------------- system.time
13052: 	if ($space eq 'time') {
13053: 	    return time;
13054:         }
13055:     } elsif ($realm eq 'server') {
13056: # ----------------------------------------------------------------- system.time
13057: 	if ($space eq 'name') {
13058: 	    return $ENV{'SERVER_NAME'};
13059:         }
13060:     } elsif ($realm eq 'client') {
13061:         if ($space eq 'remote_addr') {
13062:             return &get_requestor_ip();
13063:         }
13064:     }
13065:     return '';
13066: }
13067: 
13068: sub get_reply {
13069:     my ($reply_value) = @_;
13070:     if (ref($reply_value) eq 'ARRAY') {
13071:         if (wantarray) {
13072: 	    return @$reply_value;
13073:         }
13074:         return $reply_value->[0];
13075:     } else {
13076:         return $reply_value;
13077:     }
13078: }
13079: 
13080: sub check_group_parms {
13081:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
13082:         $recursed,$recurseupref) = @_;
13083:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
13084:                   [$what,'course']);
13085:     my $coursereply;
13086:     foreach my $group (@{$groups}) {
13087:         my @groupitems = ();
13088:         foreach my $level (@levels) {
13089:              my $item = $courseid.'.['.$group.'].'.$level->[0];
13090:              push(@groupitems,[$item,$level->[1]]);
13091:         }
13092:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
13093:                                    $env{'course.'.$courseid.'.domain'},
13094:                                    'course',$mapp,$recursed,$recurseupref,
13095:                                    $courseid,'.['.$group.'].',$what,
13096:                                    @groupitems);
13097:         last if (defined($coursereply));
13098:     }
13099:     return $coursereply;
13100: }
13101: 
13102: sub get_map_hierarchy {
13103:     my ($mapname,$courseid) = @_;
13104:     my @recurseup = ();
13105:     if ($mapname) {
13106:         if (($cachedmapkey eq $courseid) &&
13107:             (abs($cachedmaptime-time)<5)) {
13108:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
13109:                 return @{$cachedmaps{$mapname}};
13110:             }
13111:         }
13112:         my $navmap = Apache::lonnavmaps::navmap->new();
13113:         if (ref($navmap)) {
13114:             @recurseup = $navmap->recurseup_maps($mapname);
13115:             undef($navmap);
13116:             $cachedmaps{$mapname} = \@recurseup;
13117:             $cachedmaptime=time;
13118:             $cachedmapkey=$courseid;
13119:         }
13120:     }
13121:     return @recurseup;
13122: }
13123: 
13124: }
13125: 
13126: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
13127:     my ($courseid,@groups) = @_;
13128:     @groups = sort(@groups);
13129:     return @groups;
13130: }
13131: 
13132: sub packages_tab_default {
13133:     my ($uri,$varname,$toolsymb)=@_;
13134:     my (undef,$part,$name)=split(/\./,$varname);
13135: 
13136:     my (@extension,@specifics,$do_default);
13137:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
13138: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
13139: 	if ($pack_type eq 'default') {
13140: 	    $do_default=1;
13141: 	} elsif ($pack_type eq 'extension') {
13142: 	    push(@extension,[$package,$pack_type,$pack_part]);
13143: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
13144: 	    # only look at packages defaults for packages that this id is
13145: 	    push(@specifics,[$package,$pack_type,$pack_part]);
13146: 	}
13147:     }
13148:     # first look for a package that matches the requested part id
13149:     foreach my $package (@specifics) {
13150: 	my (undef,$pack_type,$pack_part)=@{$package};
13151: 	next if ($pack_part ne $part);
13152: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13153: 	    return $packagetab{"$pack_type&$name&default"};
13154: 	}
13155:     }
13156:     # look for any possible matching non extension_ package
13157:     foreach my $package (@specifics) {
13158: 	my (undef,$pack_type,$pack_part)=@{$package};
13159: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13160: 	    return $packagetab{"$pack_type&$name&default"};
13161: 	}
13162: 	if ($pack_type eq 'part') { $pack_part='0'; }
13163: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
13164: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
13165: 	}
13166:     }
13167:     # look for any posible extension_ match
13168:     foreach my $package (@extension) {
13169: 	my ($package,$pack_type)=@{$package};
13170: 	if (defined($packagetab{"$pack_type&$name&default"})) {
13171: 	    return $packagetab{"$pack_type&$name&default"};
13172: 	}
13173: 	if (defined($packagetab{$package."&$name&default"})) {
13174: 	    return $packagetab{$package."&$name&default"};
13175: 	}
13176:     }
13177:     # look for a global default setting
13178:     if ($do_default && defined($packagetab{"default&$name&default"})) {
13179: 	return $packagetab{"default&$name&default"};
13180:     }
13181:     return undef;
13182: }
13183: 
13184: sub add_prefix_and_part {
13185:     my ($prefix,$part)=@_;
13186:     my $keyroot;
13187:     if (defined($prefix) && $prefix !~ /^__/) {
13188: 	# prefix that has a part already
13189: 	$keyroot=$prefix;
13190:     } elsif (defined($prefix)) {
13191: 	# prefix that is missing a part
13192: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
13193:     } else {
13194: 	# no prefix at all
13195: 	if (defined($part)) { $keyroot='_'.$part; }
13196:     }
13197:     return $keyroot;
13198: }
13199: 
13200: # ---------------------------------------------------------------- Get metadata
13201: 
13202: my %metaentry;
13203: my %importedpartids;
13204: my %importedrespids;
13205: sub metadata {
13206:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
13207:     $uri=&declutter($uri);
13208:     # if it is a non metadata possible uri return quickly
13209:     if (($uri eq '') || 
13210: 	(($uri =~ m|^/*adm/|) && 
13211: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
13212:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
13213: 	return undef;
13214:     }
13215:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
13216: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
13217: 	return undef;
13218:     }
13219:     my $filename=$uri;
13220:     $uri=~s/\.meta$//;
13221: #
13222: # Is the metadata already cached?
13223: # Look at timestamp of caching
13224: # Everything is cached by the main uri, libraries are never directly cached
13225: #
13226:     if (!defined($liburi)) {
13227: 	my ($result,$cached)=&is_cached_new('meta',$uri);
13228: 	if (defined($cached)) { return $result->{':'.$what}; }
13229:     }
13230: 
13231: #
13232: # If the uri is for an external tool the file from
13233: # which metadata should be retrieved depends on whether
13234: # the tool had been configured to be gradable (set in the Course
13235: # Editor or Resource Editor).
13236: #
13237: # If a valid symb has been included as the third arg in the call
13238: # to &metadata() that can be used to retrieve the value of
13239: # parameter_0_gradable set for the resource, and included in the
13240: # uploaded map containing the tool. The value is retrieved via
13241: # &EXT(), if a valid symb is available.  Otherwise the value of
13242: # gradable in the exttool_$marker.db file for the tool instance
13243: # is retrieved via &get().
13244: #
13245: # When lonuserstate::traceroute() calls lonnet::EXT() for 
13246: # hiddenresource and encrypturl (during course initialization)
13247: # the map-level parameter for resource.0.gradable included in the 
13248: # uploaded map containing the tool will not yet have been stored
13249: # in the user_course_parms.db file for the user's session, so in 
13250: # this case fall back to retrieving gradable status from the
13251: # exttool_$marker.db file.
13252: #
13253: # In order to avoid an infinite loop, &metadata() will return
13254: # before a call to &EXT(), if the uri is for an external tool
13255: # and the $what for which metadata is being requested is
13256: # parameter_0_gradable or 0_gradable.
13257: #
13258: 
13259:     if ($uri =~ /ext\.tool$/) {
13260:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
13261:             return;
13262:         } else {
13263:             my ($checked,$use_passback);
13264:             if ($toolsymb ne '') {
13265:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
13266:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
13267:                     $checked = 1;
13268:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
13269:                         $use_passback = 1;
13270:                     }
13271:                 }
13272:             }
13273:             unless ($checked) {
13274:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
13275:                 $marker=~s/\D//g;
13276:                 if ($marker) {
13277:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
13278:                     $use_passback = $toolsettings{'gradable'};
13279:                 }
13280:             }
13281:             if ($use_passback) {
13282:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
13283:             } else {
13284:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
13285:             }
13286:         }
13287:     }
13288: 
13289:     {
13290: # Imported parts would go here
13291:         my @origfiletagids=();
13292:         my $importedparts=0;
13293: 
13294: # Imported responseids would go here
13295:         my $importedresponses=0;
13296: #
13297: # Is this a recursive call for a library?
13298: #
13299: #	if (! exists($metacache{$uri})) {
13300: #	    $metacache{$uri}={};
13301: #	}
13302: 	my $cachetime = 60*60;
13303:         if ($liburi) {
13304: 	    $liburi=&declutter($liburi);
13305:             $filename=$liburi;
13306:         } else {
13307: 	    &devalidate_cache_new('meta',$uri);
13308: 	    undef(%metaentry);
13309: 	}
13310:         my %metathesekeys=();
13311:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
13312: 	my $metastring;
13313: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
13314: 	    my $which = &hreflocation('','/'.($liburi || $uri));
13315: 	    $metastring = 
13316: 		&Apache::lonnet::ssi_body($which,
13317: 					  ('grade_target' => 'meta'));
13318: 	    $cachetime = 1; # only want this cached in the child not long term
13319: 	} elsif (($uri !~ m -^(editupload)/-) && 
13320:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
13321: 	    my $file=&filelocation('',&clutter($filename));
13322: 	    #push(@{$metaentry{$uri.'.file'}},$file);
13323: 	    $metastring=&getfile($file);
13324: 	}
13325:         my $parser=HTML::LCParser->new(\$metastring);
13326:         my $token;
13327:         undef %metathesekeys;
13328:         while ($token=$parser->get_token) {
13329: 	    if ($token->[0] eq 'S') {
13330: 		if (defined($token->[2]->{'package'})) {
13331: #
13332: # This is a package - get package info
13333: #
13334: 		    my $package=$token->[2]->{'package'};
13335: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13336: 		    if (defined($token->[2]->{'id'})) { 
13337: 			$keyroot.='_'.$token->[2]->{'id'}; 
13338: 		    }
13339: 		    if ($metaentry{':packages'}) {
13340: 			$metaentry{':packages'}.=','.$package.$keyroot;
13341: 		    } else {
13342: 			$metaentry{':packages'}=$package.$keyroot;
13343: 		    }
13344: 		    foreach my $pack_entry (keys(%packagetab)) {
13345: 			my $part=$keyroot;
13346: 			$part=~s/^\_//;
13347: 			if ($pack_entry=~/^\Q$package\E\&/ || 
13348: 			    $pack_entry=~/^\Q$package\E_0\&/) {
13349: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
13350: 			    # ignore package.tab specified default values
13351:                             # here &package_tab_default() will fetch those
13352: 			    if ($subp eq 'default') { next; }
13353: 			    my $value=$packagetab{$pack_entry};
13354: 			    my $unikey;
13355: 			    if ($pack =~ /_0$/) {
13356: 				$unikey='parameter_0_'.$name;
13357: 				$part=0;
13358: 			    } else {
13359: 				$unikey='parameter'.$keyroot.'_'.$name;
13360: 			    }
13361: 			    if ($subp eq 'display') {
13362: 				$value.=' [Part: '.$part.']';
13363: 			    }
13364: 			    $metaentry{':'.$unikey.'.part'}=$part;
13365: 			    $metathesekeys{$unikey}=1;
13366: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13367: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
13368: 			    }
13369: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
13370: 				$metaentry{':'.$unikey}=
13371: 				    $metaentry{':'.$unikey.'.default'};
13372: 			    }
13373: 			}
13374: 		    }
13375: 		} else {
13376: #
13377: # This is not a package - some other kind of start tag
13378: #
13379: 		    my $entry=$token->[1];
13380: 		    my $unikey='';
13381: 
13382: 		    if ($entry eq 'import') {
13383: #
13384: # Importing a library here
13385: #
13386:                         my $location=$parser->get_text('/import');
13387:                         my $dir=$filename;
13388:                         $dir=~s|[^/]*$||;
13389:                         $location=&filelocation($dir,$location);
13390: 
13391:                         my $importid=$token->[2]->{'id'};
13392:                         my $importmode=$token->[2]->{'importmode'};
13393: #
13394: # Check metadata for imported file to
13395: # see if it contained response items
13396: #
13397:                         my ($origfile,@libfilekeys);
13398:                         my %currmetaentry = %metaentry;
13399:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
13400:                                                            $depthcount+1));
13401:                         if (grep(/^responseorder$/,@libfilekeys)) {
13402:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
13403:                                                              undef,$depthcount+1);
13404:                             if ($libresponseorder ne '') {
13405:                                 if ($#origfiletagids<0) {
13406:                                     undef(%importedrespids);
13407:                                     undef(%importedpartids);
13408:                                 }
13409:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
13410:                                 if (@respids) {
13411:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
13412:                                 }
13413:                                 if ($importedrespids{$importid} ne '') {
13414:                                     $importedresponses = 1;
13415: # We need to get the original file and the imported file to get the response order correct
13416: # Load and inspect original file
13417:                                     if ($#origfiletagids<0) {
13418:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13419:                                         $origfile=&getfile($origfilelocation);
13420:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13421:                                     }
13422:                                 }
13423:                             }
13424:                         }
13425: # Do not overwrite contents of %metaentry hash for resource itself with 
13426: # hash populated for imported library file
13427:                         %metaentry = %currmetaentry;
13428:                         undef(%currmetaentry);
13429:                         if ($importmode eq 'part') {
13430: # Import as part(s)
13431:                            $importedparts=1;
13432: # We need to get the original file and the imported file to get the part order correct
13433: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
13434: # Load and inspect original file if we didn't do that already
13435:                            if ($#origfiletagids<0) {
13436:                                undef(%importedrespids);
13437:                                undef(%importedpartids);
13438:                                if ($origfile eq '') {
13439:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
13440:                                    $origfile=&getfile($origfilelocation);
13441:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13442:                                }
13443:                            }
13444:                            my @impfilepartids;
13445: # If <partorder> tag is included in metadata for the imported file
13446: # get the parts in the imported file from that.
13447:                            if (grep(/^partorder$/,@libfilekeys)) {
13448:                                %currmetaentry = %metaentry;
13449:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13450:                                                             $depthcount+1);
13451:                                %metaentry = %currmetaentry;
13452:                                undef(%currmetaentry);
13453:                                if ($libpartorder ne '') {
13454:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
13455:                                }
13456:                            } else {
13457: # If no <partorder> tag available, load and inspect imported file
13458:                                my $impfile=&getfile($location);
13459:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
13460:                            }
13461:                            if ($#impfilepartids>=0) {
13462: # This problem had parts
13463:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
13464:                            } else {
13465: # Importing by turning a single problem into a problem part
13466: # It gets the import-tags ID as part-ID
13467:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
13468:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
13469:                            }
13470:                         } else {
13471: # Import as problem or as normal import
13472:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
13473:                             unless ($importmode eq 'problem') {
13474: # Normal import
13475:                                 if (defined($token->[2]->{'id'})) {
13476:                                     $unikey.='_'.$token->[2]->{'id'};
13477:                                 }
13478:                             }
13479: # Check metadata for imported file to
13480: # see if it contained parts
13481:                             if (grep(/^partorder$/,@libfilekeys)) {
13482:                                 %currmetaentry = %metaentry;
13483:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
13484:                                                              $depthcount+1);
13485:                                 %metaentry = %currmetaentry;
13486:                                 undef(%currmetaentry);
13487:                                 if ($libpartorder ne '') {
13488:                                     $importedparts = 1;
13489:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
13490:                                 }
13491:                             }
13492:                         }
13493: 			if ($depthcount<20) {
13494: 			    my $metadata = 
13495: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
13496: 					  $depthcount+1);
13497: 			    foreach my $meta (split(',',$metadata)) {
13498: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
13499: 				$metathesekeys{$meta}=1;
13500: 			    }
13501:                         }
13502: 		    } else {
13503: #
13504: # Not importing, some other kind of non-package, non-library start tag
13505: # 
13506:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
13507:                         if (defined($token->[2]->{'id'})) {
13508:                             $unikey.='_'.$token->[2]->{'id'};
13509:                         }
13510: 			if (defined($token->[2]->{'name'})) { 
13511: 			    $unikey.='_'.$token->[2]->{'name'}; 
13512: 			}
13513: 			$metathesekeys{$unikey}=1;
13514: 			foreach my $param (@{$token->[3]}) {
13515: 			    $metaentry{':'.$unikey.'.'.$param} =
13516: 				$token->[2]->{$param};
13517: 			}
13518: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
13519: 			my $default=$metaentry{':'.$unikey.'.default'};
13520: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
13521: 		 # only ws inside the tag, and not in default, so use default
13522: 		 # as value
13523: 			    $metaentry{':'.$unikey}=$default;
13524: 			} elsif ( $internaltext =~ /\S/ ) {
13525: 		  # something interesting inside the tag
13526: 			    $metaentry{':'.$unikey}=$internaltext;
13527: 			} else {
13528: 		  # no interesting values, don't set a default
13529: 			}
13530: # end of not-a-package not-a-library import
13531: 		    }
13532: # end of not-a-package start tag
13533: 		}
13534: # the next is the end of "start tag"
13535: 	    }
13536: 	}
13537: 	my ($extension) = ($uri =~ /\.(\w+)$/);
13538: 	$extension = lc($extension);
13539: 	if ($extension eq 'htm') { $extension='html'; }
13540: 
13541: 	foreach my $key (keys(%packagetab)) {
13542: 	    #no specific packages #how's our extension
13543: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
13544: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
13545: 					 \%metathesekeys);
13546: 	}
13547: 
13548: 	if (!exists($metaentry{':packages'})
13549: 	    || $packagetab{"import_defaults&extension_$extension"}) {
13550: 	    foreach my $key (keys(%packagetab)) {
13551: 		#no specific packages well let's get default then
13552: 		if ($key!~/^default&/) { next; }
13553: 		&metadata_create_package_def($uri,$key,'default',
13554: 					     \%metathesekeys);
13555: 	    }
13556: 	}
13557: # are there custom rights to evaluate
13558: 	if ($metaentry{':copyright'} eq 'custom') {
13559: 
13560:     #
13561:     # Importing a rights file here
13562:     #
13563: 	    unless ($depthcount) {
13564: 		my $location=$metaentry{':customdistributionfile'};
13565: 		my $dir=$filename;
13566: 		$dir=~s|[^/]*$||;
13567: 		$location=&filelocation($dir,$location);
13568: 		my $rights_metadata =
13569: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
13570: 			      $depthcount+1);
13571: 		foreach my $rights (split(',',$rights_metadata)) {
13572: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
13573: 		    $metathesekeys{$rights}=1;
13574: 		}
13575: 	    }
13576: 	}
13577: 	# uniqifiy package listing
13578: 	my %seen;
13579: 	my @uniq_packages =
13580: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
13581: 	$metaentry{':packages'} = join(',',@uniq_packages);
13582: 
13583:         if (($importedresponses) || ($importedparts)) {
13584:             if ($importedparts) {
13585: # We had imported parts and need to rebuild partorder
13586:                 $metaentry{':partorder'}='';
13587:                 $metathesekeys{'partorder'}=1;
13588:             }
13589:             if ($importedresponses) {
13590: # We had imported responses and need to rebuil responseorder
13591:                 $metaentry{':responseorder'}='';
13592:                 $metathesekeys{'responseorder'}=1;
13593:             }
13594:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
13595:                 my $origid = $origfiletagids[$index+1];
13596:                 if ($origfiletagids[$index] eq 'part') {
13597: # Original part, part of the problem
13598:                     if ($importedparts) {
13599:                         $metaentry{':partorder'}.=','.$origid;
13600:                     }
13601:                 } elsif ($origfiletagids[$index] eq 'import') {
13602:                     if ($importedparts) {
13603: # We have imported parts at this position
13604:                         if ($importedpartids{$origid} ne '') {
13605:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
13606:                         }
13607:                     }
13608:                     if ($importedresponses) {
13609: # We have imported responses at this position
13610:                         if ($importedrespids{$origid} ne '') {
13611:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
13612:                         }
13613:                     }
13614:                 } else {
13615: # Original response item, part of the problem
13616:                     if ($importedresponses) {
13617:                         $metaentry{':responseorder'}.=','.$origid;
13618:                     }
13619:                 }
13620:             }
13621:             if ($importedparts) {
13622:                 $metaentry{':partorder'}=~s/^\,//;
13623:             }
13624:             if ($importedresponses) {
13625:                 $metaentry{':responseorder'}=~s/^\,//;
13626:             }
13627:         }
13628: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
13629: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
13630: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
13631:         unless ($liburi) {
13632: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
13633:         }
13634: # this is the end of "was not already recently cached
13635:     }
13636:     return $metaentry{':'.$what};
13637: }
13638: 
13639: sub metadata_create_package_def {
13640:     my ($uri,$key,$package,$metathesekeys)=@_;
13641:     my ($pack,$name,$subp)=split(/\&/,$key);
13642:     if ($subp eq 'default') { next; }
13643:     
13644:     if (defined($metaentry{':packages'})) {
13645: 	$metaentry{':packages'}.=','.$package;
13646:     } else {
13647: 	$metaentry{':packages'}=$package;
13648:     }
13649:     my $value=$packagetab{$key};
13650:     my $unikey;
13651:     $unikey='parameter_0_'.$name;
13652:     $metaentry{':'.$unikey.'.part'}=0;
13653:     $$metathesekeys{$unikey}=1;
13654:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
13655: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
13656:     }
13657:     if (defined($metaentry{':'.$unikey.'.default'})) {
13658: 	$metaentry{':'.$unikey}=
13659: 	    $metaentry{':'.$unikey.'.default'};
13660:     }
13661: }
13662: 
13663: sub metadata_generate_part0 {
13664:     my ($metadata,$metacache,$uri) = @_;
13665:     my %allnames;
13666:     foreach my $metakey (keys(%$metadata)) {
13667: 	if ($metakey=~/^parameter\_(.*)/) {
13668: 	  my $part=$$metacache{':'.$metakey.'.part'};
13669: 	  my $name=$$metacache{':'.$metakey.'.name'};
13670: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
13671: 	    $allnames{$name}=$part;
13672: 	  }
13673: 	}
13674:     }
13675:     foreach my $name (keys(%allnames)) {
13676:       $$metadata{"parameter_0_$name"}=1;
13677:       my $key=":parameter_0_$name";
13678:       $$metacache{"$key.part"}='0';
13679:       $$metacache{"$key.name"}=$name;
13680:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
13681: 					   $allnames{$name}.'_'.$name.
13682: 					   '.type'};
13683:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
13684: 			     '.display'};
13685:       my $expr='[Part: '.$allnames{$name}.']';
13686:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
13687:       $$metacache{"$key.display"}=$olddis;
13688:     }
13689: }
13690: 
13691: # ------------------------------------------------------ Devalidate title cache
13692: 
13693: sub devalidate_title_cache {
13694:     my ($url)=@_;
13695:     if (!$env{'request.course.id'}) { return; }
13696:     my $symb=&symbread($url);
13697:     if (!$symb) { return; }
13698:     my $key=$env{'request.course.id'}."\0".$symb;
13699:     &devalidate_cache_new('title',$key);
13700: }
13701: 
13702: # ------------------------------------------------- Get the title of a course
13703: 
13704: sub current_course_title {
13705:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13706: }
13707: # ------------------------------------------------- Get the title of a resource
13708: 
13709: sub gettitle {
13710:     my $urlsymb=shift;
13711:     my $symb=&symbread($urlsymb);
13712:     if ($symb) {
13713: 	my $key=$env{'request.course.id'}."\0".$symb;
13714: 	my ($result,$cached)=&is_cached_new('title',$key);
13715: 	if (defined($cached)) { 
13716: 	    return $result;
13717: 	}
13718: 	my ($map,$resid,$url)=&decode_symb($symb);
13719: 	my $title='';
13720: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13721: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13722: 	} else {
13723: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13724: 		    &GDBM_READER(),0640)) {
13725: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13726: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13727: 		untie(%bighash);
13728: 	    }
13729: 	}
13730: 	$title=~s/\&colon\;/\:/gs;
13731: 	if ($title) {
13732: # Remember both $symb and $title for dynamic metadata
13733:             $accesshash{$symb.'___crstitle'}=$title;
13734:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13735: # Cache this title and then return it
13736: 	    return &do_cache_new('title',$key,$title,600);
13737: 	}
13738: 	$urlsymb=$url;
13739:     }
13740:     my $title=&metadata($urlsymb,'title');
13741:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13742:     return $title;
13743: }
13744: 
13745: sub get_slot {
13746:     my ($which,$cnum,$cdom)=@_;
13747:     if (!$cnum || !$cdom) {
13748: 	(undef,my $courseid)=&whichuser();
13749: 	$cdom=$env{'course.'.$courseid.'.domain'};
13750: 	$cnum=$env{'course.'.$courseid.'.num'};
13751:     }
13752:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13753:     my %slotinfo;
13754:     if (exists($remembered{$key})) {
13755: 	$slotinfo{$which} = $remembered{$key};
13756:     } else {
13757: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13758: 	&Apache::lonhomework::showhash(%slotinfo);
13759: 	my ($tmp)=keys(%slotinfo);
13760: 	if ($tmp=~/^error:/) { return (); }
13761: 	$remembered{$key} = $slotinfo{$which};
13762:     }
13763:     if (ref($slotinfo{$which}) eq 'HASH') {
13764: 	return %{$slotinfo{$which}};
13765:     }
13766:     return $slotinfo{$which};
13767: }
13768: 
13769: sub get_reservable_slots {
13770:     my ($cnum,$cdom,$uname,$udom) = @_;
13771:     my $now = time;
13772:     my $reservable_info;
13773:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13774:     if (exists($remembered{$key})) {
13775:         $reservable_info = $remembered{$key};
13776:     } else {
13777:         my %resv;
13778:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13779:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13780:         $reservable_info = \%resv;
13781:         $remembered{$key} = $reservable_info;
13782:     }
13783:     return $reservable_info;
13784: }
13785: 
13786: sub get_course_slots {
13787:     my ($cnum,$cdom) = @_;
13788:     my $hashid=$cnum.':'.$cdom;
13789:     my ($result,$cached) = &is_cached_new('allslots',$hashid);
13790:     if (defined($cached)) {
13791:         if (ref($result) eq 'HASH') {
13792:             return %{$result};
13793:         }
13794:     } else {
13795:         my %slots=&dump('slots',$cdom,$cnum);
13796:         my ($tmp) = keys(%slots);
13797:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13798:             &do_cache_new('allslots',$hashid,\%slots,600);
13799:             return %slots;
13800:         }
13801:     }
13802:     return;
13803: }
13804: 
13805: sub devalidate_slots_cache {
13806:     my ($cnum,$cdom)=@_;
13807:     my $hashid=$cnum.':'.$cdom;
13808:     &devalidate_cache_new('allslots',$hashid);
13809: }
13810: 
13811: sub get_coursechange {
13812:     my ($cdom,$cnum) = @_;
13813:     if ($cdom eq '' || $cnum eq '') {
13814:         return unless ($env{'request.course.id'});
13815:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13816:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13817:     }
13818:     my $hashid=$cdom.'_'.$cnum;
13819:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13820:     if ((defined($cached)) && ($change ne '')) {
13821:         return $change;
13822:     } else {
13823:         my %crshash;
13824:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13825:         if ($crshash{'internal.contentchange'} eq '') {
13826:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13827:             if ($change eq '') {
13828:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13829:                 $change = $crshash{'internal.created'};
13830:             }
13831:         } else {
13832:             $change = $crshash{'internal.contentchange'};
13833:         }
13834:         my $cachetime = 600;
13835:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13836:     }
13837:     return $change;
13838: }
13839: 
13840: sub devalidate_coursechange_cache {
13841:     my ($cdom,$cnum)=@_;
13842:     my $hashid=$cdom.'_'.$cnum;
13843:     &devalidate_cache_new('crschange',$hashid);
13844: }
13845: 
13846: sub get_suppchange {
13847:     my ($cdom,$cnum) = @_;
13848:     if ($cdom eq '' || $cnum eq '') {
13849:         return unless ($env{'request.course.id'});
13850:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13851:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13852:     }
13853:     my $hashid=$cdom.'_'.$cnum;
13854:     my ($change,$cached)=&is_cached_new('suppchange',$hashid);
13855:     if ((defined($cached)) && ($change ne '')) {
13856:         return $change;
13857:     } else {
13858:         my %crshash = &get('environment',['internal.supplementalchange'],$cdom,$cnum);
13859:         if ($crshash{'internal.supplementalchange'} eq '') {
13860:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13861:             if ($change eq '') {
13862:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13863:                 $change = $crshash{'internal.created'};
13864:             }
13865:         } else {
13866:             $change = $crshash{'internal.supplementalchange'};
13867:         }
13868:         my $cachetime = 600;
13869:         &do_cache_new('suppchange',$hashid,$change,$cachetime);
13870:     }
13871:     return $change;
13872: }
13873: 
13874: sub devalidate_suppchange_cache {
13875:     my ($cdom,$cnum)=@_;
13876:     my $hashid=$cdom.'_'.$cnum;
13877:     &devalidate_cache_new('suppchange',$hashid);
13878: }
13879: 
13880: sub update_supp_caches {
13881:     my ($cdom,$cnum) = @_;
13882:     my %servers = &internet_dom_servers($cdom);
13883:     my @ids=&current_machine_ids();
13884:     foreach my $server (keys(%servers)) {
13885:         next if (grep(/^\Q$server\E$/,@ids));
13886:         my $hashid=$cnum.':'.$cdom;
13887:         my $cachekey = &escape('showsupp').':'.&escape($hashid);
13888:         &remote_devalidate_cache($server,[$cachekey]);
13889:     }
13890:     &has_unhidden_suppfiles($cnum,$cdom,1,1);
13891:     &count_supptools($cnum,$cdom,1);
13892:     my $now = time;
13893:     if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
13894:         &Apache::lonnet::appenv({'request.course.suppupdated' => $now});
13895:     }
13896:     &put('environment',{'internal.supplementalchange' => $now},
13897:          $cdom,$cnum);
13898:     &Apache::lonnet::appenv(
13899:         {'course.'.$cdom.'_'.$cnum.'.internal.supplementalchange' => $now});
13900:     &do_cache_new('suppchange',$cdom.'_'.$cnum,$now,600);
13901: }
13902: 
13903: # ------------------------------------------------- Update symbolic store links
13904: 
13905: sub symblist {
13906:     my ($mapname,%newhash)=@_;
13907:     $mapname=&deversion(&declutter($mapname));
13908:     my %hash;
13909:     if (($env{'request.course.fn'}) && (%newhash)) {
13910:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13911:                       &GDBM_WRCREAT(),0640)) {
13912: 	    foreach my $url (keys(%newhash)) {
13913: 		next if ($url eq 'last_known'
13914: 			 && $env{'form.no_update_last_known'});
13915: 		$hash{declutter($url)}=&encode_symb($mapname,
13916: 						    $newhash{$url}->[1],
13917: 						    $newhash{$url}->[0]);
13918:             }
13919:             if (untie(%hash)) {
13920: 		return 'ok';
13921:             }
13922:         }
13923:     }
13924:     return 'error';
13925: }
13926: 
13927: # --------------------------------------------------------------- Verify a symb
13928: 
13929: sub symbverify {
13930:     my ($symb,$thisurl,$encstate)=@_;
13931:     my $thisfn=$thisurl;
13932:     $thisfn=&declutter($thisfn);
13933: # direct jump to resource in page or to a sequence - will construct own symbs
13934:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13935: # check URL part
13936:     my ($map,$resid,$url)=&decode_symb($symb);
13937: 
13938:     unless ($url eq $thisfn) { return 0; }
13939: 
13940:     $symb=&symbclean($symb);
13941:     $thisurl=&deversion($thisurl);
13942:     $thisfn=&deversion($thisfn);
13943: 
13944:     my %bighash;
13945:     my $okay=0;
13946: 
13947:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13948:                             &GDBM_READER(),0640)) {
13949:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13950:             $thisurl =~ s/\?.+$//;
13951:             if ($map =~ m{^uploaded/.+\.page$}) {
13952:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13953:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13954:             }
13955:         }
13956:         my $ids;
13957:         if ($map =~ m{^uploaded/.+\.page$}) {
13958:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13959:         } else {
13960:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13961:         }
13962:         unless ($ids) {
13963:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13964:             $ids=$bighash{$idkey};
13965:         }
13966:         if ($ids) {
13967: # ------------------------------------------------------------------- Has ID(s)
13968:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13969:                 $symb =~ s/\?.+$//;
13970:             }
13971: 	    foreach my $id (split(/\,/,$ids)) {
13972: 	       my ($mapid,$resid)=split(/\./,$id);
13973:                if (
13974:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13975:    eq $symb) {
13976:                    if (ref($encstate)) {
13977:                        $$encstate = $bighash{'encrypted_'.$id};
13978:                    }
13979: 		   if (($env{'request.role.adv'}) ||
13980: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13981:                        ($thisurl eq '/adm/navmaps')) {
13982: 		       $okay=1;
13983:                        last;
13984: 		   }
13985: 	       }
13986: 	   }
13987:         }
13988: 	untie(%bighash);
13989:     }
13990:     return $okay;
13991: }
13992: 
13993: # --------------------------------------------------------------- Clean-up symb
13994: 
13995: sub symbclean {
13996:     my $symb=shift;
13997:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13998: # remove version from map
13999:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
14000: 
14001: # remove version from URL
14002:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
14003: 
14004: # remove wrapper
14005: 
14006:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
14007:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
14008:     return $symb;
14009: }
14010: 
14011: # ---------------------------------------------- Split symb to find map and url
14012: 
14013: sub encode_symb {
14014:     my ($map,$resid,$url)=@_;
14015:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
14016: }
14017: 
14018: sub decode_symb {
14019:     my $symb=shift;
14020:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
14021:     my ($map,$resid,$url)=split(/___/,$symb);
14022:     return (&fixversion($map),$resid,&fixversion($url));
14023: }
14024: 
14025: sub fixversion {
14026:     my $fn=shift;
14027:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
14028:     my %bighash;
14029:     my $uri=&clutter($fn);
14030:     my $key=$env{'request.course.id'}.'_'.$uri;
14031: # is this cached?
14032:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
14033:     if (defined($cached)) { return $result; }
14034: # unfortunately not cached, or expired
14035:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14036: 	    &GDBM_READER(),0640)) {
14037:  	if ($bighash{'version_'.$uri}) {
14038:  	    my $version=$bighash{'version_'.$uri};
14039:  	    unless (($version eq 'mostrecent') || 
14040: 		    ($version==&getversion($uri))) {
14041:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
14042:  	    }
14043:  	}
14044:  	untie %bighash;
14045:     }
14046:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
14047: }
14048: 
14049: sub deversion {
14050:     my $url=shift;
14051:     $url=~s/\.\d+\.(\w+)$/\.$1/;
14052:     return $url;
14053: }
14054: 
14055: # ------------------------------------------------------ Return symb list entry
14056: 
14057: sub symbread {
14058:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
14059:         $ignoresymbdb,$noenccheck)=@_;
14060:     my $cache_str='request.symbread.cached.'.$thisfn;
14061:     if (defined($env{$cache_str})) {
14062:         unless (ref($possibles) eq 'HASH') {
14063:             if ($ignorecachednull) {
14064:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
14065:             } else {
14066:                 return $env{$cache_str};
14067:             }
14068:         }
14069:     }
14070: # no filename provided? try from environment
14071:     unless ($thisfn) {
14072:         if ($env{'request.symb'}) {
14073:             return $env{$cache_str}=&symbclean($env{'request.symb'});
14074: 	}
14075: 	$thisfn=$env{'request.filename'};
14076:     }
14077:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14078: # is that filename actually a symb? Verify, clean, and return
14079:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
14080: 	if (&symbverify($thisfn,$1)) {
14081: 	    return $env{$cache_str}=&symbclean($thisfn);
14082: 	}
14083:     }
14084:     $thisfn=declutter($thisfn);
14085:     my %hash;
14086:     my %bighash;
14087:     my $syval='';
14088:     if (($env{'request.course.fn'}) && ($thisfn)) {
14089:         unless ($ignoresymbdb) {
14090:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
14091:                           &GDBM_READER(),0640)) {
14092: 	        $syval=$hash{$thisfn};
14093:                 untie(%hash);
14094:             }
14095:             if ($syval && $checkforblock) {
14096:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$ignoresymbdb,$noenccheck);
14097:                 if (@blockers) {
14098:                     $syval='';
14099:                 }
14100:             }
14101:         }
14102: # ---------------------------------------------------------- There was an entry
14103:         if ($syval) {
14104: 	    #unless ($syval=~/\_\d+$/) {
14105: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
14106: 		    #&appenv({'request.ambiguous' => $thisfn});
14107: 		    #return $env{$cache_str}='';
14108: 		#}    
14109: 		#$syval.=$1;
14110: 	    #}
14111:         } else {
14112: # ------------------------------------------------------- Was not in symb table
14113:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
14114:                             &GDBM_READER(),0640)) {
14115: # ---------------------------------------------- Get ID(s) for current resource
14116:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
14117:               unless ($ids) { 
14118:                  $ids=$bighash{'ids_/'.$thisfn};
14119:               }
14120:               unless ($ids) {
14121: # alias?
14122: 		  $ids=$bighash{'mapalias_'.$thisfn};
14123:               }
14124:               if ($ids) {
14125: # ------------------------------------------------------------------- Has ID(s)
14126:                  my @possibilities=split(/\,/,$ids);
14127:                  if ($#possibilities==0) {
14128: # ----------------------------------------------- There is only one possibility
14129: 		     my ($mapid,$resid)=split(/\./,$ids);
14130: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
14131: 						    $resid,$thisfn);
14132:                      if (ref($possibles) eq 'HASH') {
14133:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14134:                              $possibles->{$syval} = 1;
14135:                          }
14136:                      }
14137:                      if ($checkforblock) {
14138:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
14139:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids},'',$noenccheck);
14140:                              if (@blockers) {
14141:                                  $syval = '';
14142:                                  untie(%bighash);
14143:                                  return $env{$cache_str}='';
14144:                              }
14145:                          }
14146:                      }
14147:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
14148: # ------------------------------------------ There is more than one possibility
14149:                      my $realpossible=0;
14150:                      foreach my $id (@possibilities) {
14151: 			 my $file=$bighash{'src_'.$id};
14152:                          my $canaccess;
14153:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
14154:                              $canaccess = 1;
14155:                          } else { 
14156:                              $canaccess = &allowed('bre',$file);
14157:                          }
14158:                          if ($canaccess) {
14159:          		     my ($mapid,$resid)=split(/\./,$id);
14160:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
14161:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
14162: 						             $resid,$thisfn);
14163:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
14164:                                  next unless (($noenccheck) || ($bighash{'encrypted_'.$id} eq $env{'request.enc'}));
14165:                                  if ($checkforblock) {
14166:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file,'',$noenccheck);
14167:                                      if (@blockers > 0) {
14168:                                          $syval = '';
14169:                                      } else {
14170:                                          $syval = $poss_syval;
14171:                                          $realpossible++;
14172:                                      }
14173:                                  } else {
14174:                                      $syval = $poss_syval;
14175:                                      $realpossible++;
14176:                                  }
14177:                                  if ($syval) {
14178:                                      if (ref($possibles) eq 'HASH') {
14179:                                          $possibles->{$syval} = 1;
14180:                                      }
14181:                                  }
14182:                              }
14183: 			 }
14184:                      }
14185: 		     if ($realpossible!=1) { $syval=''; }
14186:                  } else {
14187:                      $syval='';
14188:                  }
14189: 	      }
14190:               untie(%bighash);
14191:            }
14192:         }
14193:         if ($syval) {
14194: 	    return $env{$cache_str}=$syval;
14195:         }
14196:     }
14197:     &appenv({'request.ambiguous' => $thisfn});
14198:     return $env{$cache_str}='';
14199: }
14200: 
14201: # ---------------------------------------------------------- Return random seed
14202: 
14203: sub numval {
14204:     my $txt=shift;
14205:     $txt=~tr/A-J/0-9/;
14206:     $txt=~tr/a-j/0-9/;
14207:     $txt=~tr/K-T/0-9/;
14208:     $txt=~tr/k-t/0-9/;
14209:     $txt=~tr/U-Z/0-5/;
14210:     $txt=~tr/u-z/0-5/;
14211:     $txt=~s/\D//g;
14212:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
14213:     return int($txt);
14214: }
14215: 
14216: sub numval2 {
14217:     my $txt=shift;
14218:     $txt=~tr/A-J/0-9/;
14219:     $txt=~tr/a-j/0-9/;
14220:     $txt=~tr/K-T/0-9/;
14221:     $txt=~tr/k-t/0-9/;
14222:     $txt=~tr/U-Z/0-5/;
14223:     $txt=~tr/u-z/0-5/;
14224:     $txt=~s/\D//g;
14225:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14226:     my $total;
14227:     foreach my $val (@txts) { $total+=$val; }
14228:     if ($_64bit) { if ($total > 2**32) { return -1; } }
14229:     return int($total);
14230: }
14231: 
14232: sub numval3 {
14233:     use integer;
14234:     my $txt=shift;
14235:     $txt=~tr/A-J/0-9/;
14236:     $txt=~tr/a-j/0-9/;
14237:     $txt=~tr/K-T/0-9/;
14238:     $txt=~tr/k-t/0-9/;
14239:     $txt=~tr/U-Z/0-5/;
14240:     $txt=~tr/u-z/0-5/;
14241:     $txt=~s/\D//g;
14242:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
14243:     my $total;
14244:     foreach my $val (@txts) { $total+=$val; }
14245:     if ($_64bit) { $total=(($total<<32)>>32); }
14246:     return $total;
14247: }
14248: 
14249: sub digest {
14250:     my ($data)=@_;
14251:     my $digest=&Digest::MD5::md5($data);
14252:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
14253:     my ($e,$f);
14254:     {
14255:         use integer;
14256:         $e=($a+$b);
14257:         $f=($c+$d);
14258:         if ($_64bit) {
14259:             $e=(($e<<32)>>32);
14260:             $f=(($f<<32)>>32);
14261:         }
14262:     }
14263:     if (wantarray) {
14264: 	return ($e,$f);
14265:     } else {
14266: 	my $g;
14267: 	{
14268: 	    use integer;
14269: 	    $g=($e+$f);
14270: 	    if ($_64bit) {
14271: 		$g=(($g<<32)>>32);
14272: 	    }
14273: 	}
14274: 	return $g;
14275:     }
14276: }
14277: 
14278: sub latest_rnd_algorithm_id {
14279:     return '64bit5';
14280: }
14281: 
14282: sub get_rand_alg {
14283:     my ($courseid)=@_;
14284:     if (!$courseid) { $courseid=(&whichuser())[1]; }
14285:     if ($courseid) {
14286: 	return $env{"course.$courseid.rndseed"};
14287:     }
14288:     return &latest_rnd_algorithm_id();
14289: }
14290: 
14291: sub validCODE {
14292:     my ($CODE)=@_;
14293:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
14294:     return 0;
14295: }
14296: 
14297: sub getCODE {
14298:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
14299:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
14300: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
14301: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
14302: 	return $Apache::lonhomework::history{'resource.CODE'};
14303:     }
14304:     return undef;
14305: }
14306: #
14307: #  Determines the random seed for a specific context:
14308: #
14309: # parameters:
14310: #   symb      - in course context the symb for the seed.
14311: #   course_id - The course id of the form domain_coursenum.
14312: #   domain    - Domain for the user.
14313: #   course    - Course for the user.
14314: #   cenv      - environment of the course.
14315: #
14316: # NOTE:
14317: #   All parameters are picked out of the environment if missing
14318: #   or not defined.
14319: #   If a symb cannot be determined the current time is used instead.
14320: #
14321: #  For a given well defined symb, courside, domain, username,
14322: #  and course environment, the seed is reproducible.
14323: #
14324: sub rndseed {
14325:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
14326:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
14327:     if (!defined($symb)) {
14328: 	unless ($symb=$wsymb) { return time; }
14329:     }
14330:     if (!defined $courseid) { 
14331: 	$courseid=$wcourseid; 
14332:     }
14333:     if (!defined $domain) { $domain=$wdomain; }
14334:     if (!defined $username) { $username=$wusername }
14335: 
14336:     my $which;
14337:     if (defined($cenv->{'rndseed'})) {
14338: 	$which = $cenv->{'rndseed'};
14339:     } else {
14340: 	$which =&get_rand_alg($courseid);
14341:     }
14342:     if (defined(&getCODE())) {
14343: 
14344: 	if ($which eq '64bit5') {
14345: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
14346: 	} elsif ($which eq '64bit4') {
14347: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
14348: 	} else {
14349: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
14350: 	}
14351:     } elsif ($which eq '64bit5') {
14352: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
14353:     } elsif ($which eq '64bit4') {
14354: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
14355:     } elsif ($which eq '64bit3') {
14356: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
14357:     } elsif ($which eq '64bit2') {
14358: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
14359:     } elsif ($which eq '64bit') {
14360: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
14361:     }
14362:     return &rndseed_32bit($symb,$courseid,$domain,$username);
14363: }
14364: 
14365: sub rndseed_32bit {
14366:     my ($symb,$courseid,$domain,$username)=@_;
14367:     {
14368: 	use integer;
14369: 	my $symbchck=unpack("%32C*",$symb) << 27;
14370: 	my $symbseed=numval($symb) << 22;
14371: 	my $namechck=unpack("%32C*",$username) << 17;
14372: 	my $nameseed=numval($username) << 12;
14373: 	my $domainseed=unpack("%32C*",$domain) << 7;
14374: 	my $courseseed=unpack("%32C*",$courseid);
14375: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
14376: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14377: 	#&logthis("rndseed :$num:$symb");
14378: 	if ($_64bit) { $num=(($num<<32)>>32); }
14379: 	return $num;
14380:     }
14381: }
14382: 
14383: sub rndseed_64bit {
14384:     my ($symb,$courseid,$domain,$username)=@_;
14385:     {
14386: 	use integer;
14387: 	my $symbchck=unpack("%32S*",$symb) << 21;
14388: 	my $symbseed=numval($symb) << 10;
14389: 	my $namechck=unpack("%32S*",$username);
14390: 	
14391: 	my $nameseed=numval($username) << 21;
14392: 	my $domainseed=unpack("%32S*",$domain) << 10;
14393: 	my $courseseed=unpack("%32S*",$courseid);
14394: 	
14395: 	my $num1=$symbchck+$symbseed+$namechck;
14396: 	my $num2=$nameseed+$domainseed+$courseseed;
14397: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14398: 	#&logthis("rndseed :$num:$symb");
14399: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14400: 	return "$num1,$num2";
14401:     }
14402: }
14403: 
14404: sub rndseed_64bit2 {
14405:     my ($symb,$courseid,$domain,$username)=@_;
14406:     {
14407: 	use integer;
14408: 	# strings need to be an even # of cahracters long, it it is odd the
14409:         # last characters gets thrown away
14410: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14411: 	my $symbseed=numval($symb) << 10;
14412: 	my $namechck=unpack("%32S*",$username.' ');
14413: 	
14414: 	my $nameseed=numval($username) << 21;
14415: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14416: 	my $courseseed=unpack("%32S*",$courseid.' ');
14417: 	
14418: 	my $num1=$symbchck+$symbseed+$namechck;
14419: 	my $num2=$nameseed+$domainseed+$courseseed;
14420: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14421: 	#&logthis("rndseed :$num:$symb");
14422: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14423: 	return "$num1,$num2";
14424:     }
14425: }
14426: 
14427: sub rndseed_64bit3 {
14428:     my ($symb,$courseid,$domain,$username)=@_;
14429:     {
14430: 	use integer;
14431: 	# strings need to be an even # of cahracters long, it it is odd the
14432:         # last characters gets thrown away
14433: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14434: 	my $symbseed=numval2($symb) << 10;
14435: 	my $namechck=unpack("%32S*",$username.' ');
14436: 	
14437: 	my $nameseed=numval2($username) << 21;
14438: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14439: 	my $courseseed=unpack("%32S*",$courseid.' ');
14440: 	
14441: 	my $num1=$symbchck+$symbseed+$namechck;
14442: 	my $num2=$nameseed+$domainseed+$courseseed;
14443: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14444: 	#&logthis("rndseed :$num1:$num2:$_64bit");
14445: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14446: 	
14447: 	return "$num1:$num2";
14448:     }
14449: }
14450: 
14451: sub rndseed_64bit4 {
14452:     my ($symb,$courseid,$domain,$username)=@_;
14453:     {
14454: 	use integer;
14455: 	# strings need to be an even # of cahracters long, it it is odd the
14456:         # last characters gets thrown away
14457: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
14458: 	my $symbseed=numval3($symb) << 10;
14459: 	my $namechck=unpack("%32S*",$username.' ');
14460: 	
14461: 	my $nameseed=numval3($username) << 21;
14462: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
14463: 	my $courseseed=unpack("%32S*",$courseid.' ');
14464: 	
14465: 	my $num1=$symbchck+$symbseed+$namechck;
14466: 	my $num2=$nameseed+$domainseed+$courseseed;
14467: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
14468: 	#&logthis("rndseed :$num1:$num2:$_64bit");
14469: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
14470: 	
14471: 	return "$num1:$num2";
14472:     }
14473: }
14474: 
14475: sub rndseed_64bit5 {
14476:     my ($symb,$courseid,$domain,$username)=@_;
14477:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
14478:     return "$num1:$num2";
14479: }
14480: 
14481: sub rndseed_CODE_64bit {
14482:     my ($symb,$courseid,$domain,$username)=@_;
14483:     {
14484: 	use integer;
14485: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14486: 	my $symbseed=numval2($symb);
14487: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14488: 	my $CODEseed=numval(&getCODE());
14489: 	my $courseseed=unpack("%32S*",$courseid.' ');
14490: 	my $num1=$symbseed+$CODEchck;
14491: 	my $num2=$CODEseed+$courseseed+$symbchck;
14492: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14493: 	#&logthis("rndseed :$num1:$num2:$symb");
14494: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14495: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14496: 	return "$num1:$num2";
14497:     }
14498: }
14499: 
14500: sub rndseed_CODE_64bit4 {
14501:     my ($symb,$courseid,$domain,$username)=@_;
14502:     {
14503: 	use integer;
14504: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
14505: 	my $symbseed=numval3($symb);
14506: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
14507: 	my $CODEseed=numval3(&getCODE());
14508: 	my $courseseed=unpack("%32S*",$courseid.' ');
14509: 	my $num1=$symbseed+$CODEchck;
14510: 	my $num2=$CODEseed+$courseseed+$symbchck;
14511: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
14512: 	#&logthis("rndseed :$num1:$num2:$symb");
14513: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
14514: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
14515: 	return "$num1:$num2";
14516:     }
14517: }
14518: 
14519: sub rndseed_CODE_64bit5 {
14520:     my ($symb,$courseid,$domain,$username)=@_;
14521:     my $code = &getCODE();
14522:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
14523:     return "$num1:$num2";
14524: }
14525: 
14526: sub setup_random_from_rndseed {
14527:     my ($rndseed)=@_;
14528:     if ($rndseed =~/([,:])/) {
14529:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
14530:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
14531:             &Math::Random::random_set_seed_from_phrase($rndseed);
14532:         } else {
14533:             &Math::Random::random_set_seed($num1,$num2);
14534:         }
14535:     } else {
14536: 	&Math::Random::random_set_seed_from_phrase($rndseed);
14537:     }
14538: }
14539: 
14540: sub latest_receipt_algorithm_id {
14541:     return 'receipt3';
14542: }
14543: 
14544: sub recunique {
14545:     my $fucourseid=shift;
14546:     my $unique;
14547:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
14548: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14549: 	$unique=$env{"course.$fucourseid.internal.encseed"};
14550:     } else {
14551: 	$unique=$perlvar{'lonReceipt'};
14552:     }
14553:     return unpack("%32C*",$unique);
14554: }
14555: 
14556: sub recprefix {
14557:     my $fucourseid=shift;
14558:     my $prefix;
14559:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
14560: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
14561: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
14562:     } else {
14563: 	$prefix=$perlvar{'lonHostID'};
14564:     }
14565:     return unpack("%32C*",$prefix);
14566: }
14567: 
14568: sub ireceipt {
14569:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
14570: 
14571:     my $return =&recprefix($fucourseid).'-';
14572: 
14573:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
14574: 	$env{'request.state'} eq 'construct') {
14575: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
14576: 	return $return;
14577:     }
14578: 
14579:     my $cuname=unpack("%32C*",$funame);
14580:     my $cudom=unpack("%32C*",$fudom);
14581:     my $cucourseid=unpack("%32C*",$fucourseid);
14582:     my $cusymb=unpack("%32C*",$fusymb);
14583:     my $cunique=&recunique($fucourseid);
14584:     my $cpart=unpack("%32S*",$part);
14585:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
14586: 
14587: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
14588: 			       
14589: 	$return.= ($cunique%$cuname+
14590: 		   $cunique%$cudom+
14591: 		   $cusymb%$cuname+
14592: 		   $cusymb%$cudom+
14593: 		   $cucourseid%$cuname+
14594: 		   $cucourseid%$cudom+
14595: 		   $cpart%$cuname+
14596: 		   $cpart%$cudom);
14597:     } else {
14598: 	$return.= ($cunique%$cuname+
14599: 		   $cunique%$cudom+
14600: 		   $cusymb%$cuname+
14601: 		   $cusymb%$cudom+
14602: 		   $cucourseid%$cuname+
14603: 		   $cucourseid%$cudom);
14604:     }
14605:     return $return;
14606: }
14607: 
14608: sub receipt {
14609:     my ($part)=@_;
14610:     my ($symb,$courseid,$domain,$name) = &whichuser();
14611:     return &ireceipt($name,$domain,$courseid,$symb,$part);
14612: }
14613: 
14614: sub whichuser {
14615:     my ($passedsymb)=@_;
14616:     my ($symb,$courseid,$domain,$name,$publicuser);
14617:     if (defined($env{'form.grade_symb'})) {
14618: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
14619: 	my $allowed=&allowed('vgr',$tmp_courseid);
14620: 	if (!$allowed &&
14621: 	    exists($env{'request.course.sec'}) &&
14622: 	    $env{'request.course.sec'} !~ /^\s*$/) {
14623: 	    $allowed=&allowed('vgr',$tmp_courseid.
14624: 			      '/'.$env{'request.course.sec'});
14625: 	}
14626: 	if ($allowed) {
14627: 	    ($symb)=&get_env_multiple('form.grade_symb');
14628: 	    $courseid=$tmp_courseid;
14629: 	    ($domain)=&get_env_multiple('form.grade_domain');
14630: 	    ($name)=&get_env_multiple('form.grade_username');
14631: 	    return ($symb,$courseid,$domain,$name,$publicuser);
14632: 	}
14633:     }
14634:     if (!$passedsymb) {
14635: 	$symb=&symbread();
14636:     } else {
14637: 	$symb=$passedsymb;
14638:     }
14639:     $courseid=$env{'request.course.id'};
14640:     $domain=$env{'user.domain'};
14641:     $name=$env{'user.name'};
14642:     if ($name eq 'public' && $domain eq 'public') {
14643: 	if (!defined($env{'form.username'})) {
14644: 	    $env{'form.username'}.=time.rand(10000000);
14645: 	}
14646: 	$name.=$env{'form.username'};
14647:     }
14648:     return ($symb,$courseid,$domain,$name,$publicuser);
14649: 
14650: }
14651: 
14652: # ------------------------------------------------------------ Serves up a file
14653: # returns either the contents of the file or 
14654: # -1 if the file doesn't exist
14655: #
14656: # if the target is a file that was uploaded via DOCS, 
14657: # a check will be made to see if a current copy exists on the local server,
14658: # if it does this will be served, otherwise a copy will be retrieved from
14659: # the home server for the course and stored in /home/httpd/html/userfiles on
14660: # the local server.   
14661: 
14662: sub getfile {
14663:     my ($file) = @_;
14664:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
14665:     &repcopy($file);
14666:     return &readfile($file);
14667: }
14668: 
14669: sub repcopy_userfile {
14670:     my ($file)=@_;
14671:     my $londocroot = $perlvar{'lonDocRoot'};
14672:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
14673:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
14674:     my ($cdom,$cnum,$filename) = 
14675: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
14676:     my $uri="/uploaded/$cdom/$cnum/$filename";
14677:     if (-e "$file") {
14678: # we already have a local copy, check it out
14679: 	my @fileinfo = stat($file);
14680: 	my $rtncode;
14681: 	my $info;
14682: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
14683: 	if ($lwpresp ne 'ok') {
14684: # there is no such file anymore, even though we had a local copy
14685: 	    if ($rtncode eq '404') {
14686: 		unlink($file);
14687: 	    }
14688: 	    return -1;
14689: 	}
14690: 	if ($info < $fileinfo[9]) {
14691: # nice, the file we have is up-to-date, just say okay
14692: 	    return 'ok';
14693: 	} else {
14694: # the file is outdated, get rid of it
14695: 	    unlink($file);
14696: 	}
14697:     }
14698: # one way or the other, at this point, we don't have the file
14699: # construct the correct path for the file
14700:     my @parts = ($cdom,$cnum); 
14701:     if ($filename =~ m|^(.+)/[^/]+$|) {
14702: 	push @parts, split(/\//,$1);
14703:     }
14704:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
14705:     foreach my $part (@parts) {
14706: 	$path .= '/'.$part;
14707: 	if (!-e $path) {
14708: 	    mkdir($path,0770);
14709: 	}
14710:     }
14711: # now the path exists for sure
14712: # get a user agent
14713:     my $transferfile=$file.'.in.transfer';
14714: # FIXME: this should flock
14715:     if (-e $transferfile) { return 'ok'; }
14716:     my $request;
14717:     $uri=~s/^\///;
14718:     my $homeserver = &homeserver($cnum,$cdom);
14719:     my $hostname = &hostname($homeserver);
14720:     my $protocol = $protocol{$homeserver};
14721:     $protocol = 'http' if ($protocol ne 'https');
14722:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
14723:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
14724: # did it work?
14725:     if ($response->is_error()) {
14726: 	unlink($transferfile);
14727: 	&logthis("Userfile repcopy failed for $uri");
14728: 	return -1;
14729:     }
14730: # worked, rename the transfer file
14731:     rename($transferfile,$file);
14732:     return 'ok';
14733: }
14734: 
14735: sub tokenwrapper {
14736:     my $uri=shift;
14737:     $uri=~s|^https?\://([^/]+)||;
14738:     $uri=~s|^/||;
14739:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14740:     my $token=$1;
14741:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14742:     if ($udom && $uname && $file) {
14743: 	$file=~s|(\?\.*)*$||;
14744:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14745:         my $homeserver = &homeserver($uname,$udom);
14746:         my $hostname = &hostname($homeserver);
14747:         my $protocol = $protocol{$homeserver};
14748:         $protocol = 'http' if ($protocol ne 'https');
14749:         return $protocol.'://'.$hostname.'/'.$uri.
14750:                (($uri=~/\?/)?'&':'?').'token='.$token.
14751:                                '&tokenissued='.$perlvar{'lonHostID'};
14752:     } else {
14753:         return '/adm/notfound.html';
14754:     }
14755: }
14756: 
14757: # call with reqtype HEAD: get last modification time
14758: # call with reqtype GET: get the file contents
14759: # Do not call this with reqtype GET for large files! It loads everything into memory
14760: #
14761: sub getuploaded {
14762:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14763:     $uri=~s/^\///;
14764:     my $homeserver = &homeserver($cnum,$cdom);
14765:     my $hostname = &hostname($homeserver);
14766:     my $protocol = $protocol{$homeserver};
14767:     $protocol = 'http' if ($protocol ne 'https');
14768:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14769:     my $request=new HTTP::Request($reqtype,$uri);
14770:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14771:     $$rtncode = $response->code;
14772:     if (! $response->is_success()) {
14773: 	return 'failed';
14774:     }      
14775:     if ($reqtype eq 'HEAD') {
14776: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14777:     } elsif ($reqtype eq 'GET') {
14778: 	$$info = $response->content;
14779:     }
14780:     return 'ok';
14781: }
14782: 
14783: sub readfile {
14784:     my $file = shift;
14785:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14786:     my $fh;
14787:     open($fh,"<",$file);
14788:     my $a='';
14789:     while (my $line = <$fh>) { $a .= $line; }
14790:     return $a;
14791: }
14792: 
14793: sub filelocation {
14794:     my ($dir,$file) = @_;
14795:     my $location;
14796:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14797: 
14798:     if ($file =~ m-^/adm/-) {
14799: 	$file=~s-^/adm/wrapper/-/-;
14800: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14801:     }
14802: 
14803:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14804:         $location = $file;
14805:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14806:         my ($udom,$uname,$filename)=
14807:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14808:         my $home=&homeserver($uname,$udom);
14809:         my $is_me=0;
14810:         my @ids=&current_machine_ids();
14811:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14812:         if ($is_me) {
14813:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14814:         } else {
14815:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14816:   	      $udom.'/'.$uname.'/'.$filename;
14817:         }
14818:     } elsif ($file =~ m-^/adm/-) {
14819: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14820:     } else {
14821:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14822:         $file=~s:^/(res|priv)/:/:;
14823:         my $space=$1;
14824:         if ( !( $file =~ m:^/:) ) {
14825:             $location = $dir. '/'.$file;
14826:         } else {
14827:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14828:         }
14829:     }
14830:     $location=~s://+:/:g; # remove duplicate /
14831:     while ($location=~m{/\.\./}) {
14832: 	if ($location =~ m{/[^/]+/\.\./}) {
14833: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14834: 	} else {
14835: 	    $location=~ s{/\.\./}{/}g;
14836: 	}
14837:     } #remove dir/..
14838:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14839:     return $location;
14840: }
14841: 
14842: sub hreflocation {
14843:     my ($dir,$file)=@_;
14844:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14845: 	$file=filelocation($dir,$file);
14846:     } elsif ($file=~m-^/adm/-) {
14847: 	$file=~s-^/adm/wrapper/-/-;
14848: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14849:     }
14850:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14851: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14852:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14853: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14854: 	        {/uploaded/$1/$2/}x;
14855:     }
14856:     if ($file=~ m{^/userfiles/}) {
14857: 	$file =~ s{^/userfiles/}{/uploaded/};
14858:     }
14859:     return $file;
14860: }
14861: 
14862: 
14863: 
14864: 
14865: 
14866: sub current_machine_domains {
14867:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14868: }
14869: 
14870: sub machine_domains {
14871:     my ($hostname) = @_;
14872:     my @domains;
14873:     my %hostname = &all_hostnames();
14874:     while( my($id, $name) = each(%hostname)) {
14875: #	&logthis("-$id-$name-$hostname-");
14876: 	if ($hostname eq $name) {
14877: 	    push(@domains,&host_domain($id));
14878: 	}
14879:     }
14880:     return @domains;
14881: }
14882: 
14883: sub current_machine_ids {
14884:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14885: }
14886: 
14887: sub machine_ids {
14888:     my ($hostname) = @_;
14889:     $hostname ||= &hostname($perlvar{'lonHostID'});
14890:     my @ids;
14891:     my %name_to_host = &all_names();
14892:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14893: 	return @{ $name_to_host{$hostname} };
14894:     }
14895:     return;
14896: }
14897: 
14898: sub additional_machine_domains {
14899:     my @domains;
14900:     if (-e "$perlvar{'lonTabDir'}/expected_domains.tab") {
14901:         if (open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab")) {
14902:             while (my $line = <$fh>) {
14903:                 chomp($line);           
14904:                 $line =~ s/\s//g;
14905:                 push(@domains,$line);
14906:             }
14907:             close($fh);
14908:         }
14909:     }
14910:     return @domains;
14911: }
14912: 
14913: sub default_login_domain {
14914:     my $domain = $perlvar{'lonDefDomain'};
14915:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14916:     foreach my $posdom (&current_machine_domains(),
14917:                         &additional_machine_domains()) {
14918:         if (lc($posdom) eq lc($testdomain)) {
14919:             $domain=$posdom;
14920:             last;
14921:         }
14922:     }
14923:     return $domain;
14924: }
14925: 
14926: sub shared_institution {
14927:     my ($dom,$lonhost) = @_;
14928:     if ($lonhost eq '') {
14929:         $lonhost = $perlvar{'lonHostID'};
14930:     }
14931:     my $same_intdom;
14932:     my $hostintdom = &internet_dom($lonhost);
14933:     if ($hostintdom ne '') {
14934:         my %iphost = &get_iphost();
14935:         my $primary_id = &domain($dom,'primary');
14936:         my $primary_ip = &get_host_ip($primary_id);
14937:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14938:             foreach my $id (@{$iphost{$primary_ip}}) {
14939:                 my $intdom = &internet_dom($id);
14940:                 if ($intdom eq $hostintdom) {
14941:                     $same_intdom = 1;
14942:                     last;
14943:                 }
14944:             }
14945:         }
14946:     }
14947:     return $same_intdom;
14948: }
14949: 
14950: sub uses_sts {
14951:     my ($ignore_cache) = @_;
14952:     my $lonhost = $perlvar{'lonHostID'};
14953:     my $hostname = &hostname($lonhost);
14954:     my $sts_on;
14955:     if ($protocol{$lonhost} eq 'https') {
14956:         my $cachetime = 12*3600;
14957:         if (!$ignore_cache) {
14958:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14959:             if (defined($cached)) {
14960:                 return $sts_on;
14961:             }
14962:         }
14963:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14964:         my $request=new HTTP::Request('HEAD',$url);
14965:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14966:         if ($response->is_success) {
14967:             my $has_sts = $response->header('Strict-Transport-Security');
14968:             if ($has_sts eq '') {
14969:                 $sts_on = 0;
14970:             } else {
14971:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14972:                     my $maxage = $1;
14973:                     if ($maxage) {
14974:                         $sts_on = 1;
14975:                     } else {
14976:                         $sts_on = 0;
14977:                     }
14978:                 } else {
14979:                     $sts_on = 0;
14980:                 }
14981:             }
14982:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14983:         }
14984:     }
14985:     return;
14986: }
14987: 
14988: sub waf_allssl {
14989:     my ($host_name) = @_;
14990:     my $alias = &get_proxy_alias();
14991:     if ($host_name eq '') {
14992:         $host_name = $ENV{'SERVER_NAME'};
14993:     }
14994:     if (($host_name ne '') && ($alias eq $host_name)) {
14995:         my $serverhomedom = &host_domain($perlvar{'lonHostID'});
14996:         my %defdomdefaults = &get_domain_defaults($serverhomedom);
14997:         if ($defdomdefaults{'waf_sslopt'}) {
14998:             return $defdomdefaults{'waf_sslopt'};
14999:         }
15000:     }
15001:     return;
15002: }
15003: 
15004: sub get_requestor_ip {
15005:     my ($r,$nolookup,$noproxy) = @_;
15006:     my $from_ip;
15007:     if (ref($r)) {
15008:         if ($r->can('useragent_ip')) {
15009:             if ($noproxy && $r->can('client_ip')) {
15010:                 $from_ip = $r->client_ip();
15011:             } else {
15012:                 $from_ip = $r->useragent_ip();
15013:             }
15014:         } elsif ($r->connection->can('remote_ip')) {
15015:             $from_ip = $r->connection->remote_ip();
15016:         } else {
15017:             $from_ip = $r->get_remote_host($nolookup);
15018:         }
15019:     } else {
15020:         $from_ip = $ENV{'REMOTE_ADDR'};
15021:     }
15022:     return $from_ip if ($noproxy); 
15023:     # Who controls proxy settings for server
15024:     my $dom_in_use = $Apache::lonnet::perlvar{'lonDefDomain'};
15025:     my $proxyinfo = &get_proxy_settings($dom_in_use);
15026:     if ((ref($proxyinfo) eq 'HASH') && ($from_ip)) {
15027:         if ($proxyinfo->{'vpnint'}) {
15028:             if (&ip_match($from_ip,$proxyinfo->{'vpnint'})) {
15029:                 return $from_ip;
15030:             }
15031:         }
15032:         if ($proxyinfo->{'trusted'}) {
15033:             if (&ip_match($from_ip,$proxyinfo->{'trusted'})) {
15034:                 my $ipheader = $proxyinfo->{'ipheader'};
15035:                 my ($ip,$xfor);
15036:                 if (ref($r)) {
15037:                     if ($ipheader) {
15038:                         $ip = $r->headers_in->{$ipheader};
15039:                     }
15040:                     $xfor = $r->headers_in->{'X-Forwarded-For'};
15041:                 } else {
15042:                     if ($ipheader) {
15043:                         $ip = $ENV{'HTTP_'.uc($ipheader)};
15044:                     }
15045:                     $xfor = $ENV{'HTTP_X_FORWARDED_FOR'};
15046:                 }
15047:                 if (($ip eq '') && ($xfor ne '')) {
15048:                     foreach my $poss_ip (reverse(split(/\s*,\s*/,$xfor))) {
15049:                         unless (&ip_match($poss_ip,$proxyinfo->{'trusted'})) {
15050:                             $ip = $poss_ip;
15051:                             last;
15052:                         }
15053:                     }
15054:                 }
15055:                 if ($ip ne '') {
15056:                     return $ip;
15057:                 }
15058:             }
15059:         }
15060:     }
15061:     return $from_ip;
15062: }
15063: 
15064: sub get_proxy_settings {
15065:     my ($dom_in_use) = @_;
15066:     my %domdefaults = &get_domain_defaults($dom_in_use);
15067:     my $proxyinfo = {
15068:                        ipheader => $domdefaults{'waf_ipheader'},
15069:                        trusted  => $domdefaults{'waf_trusted'},
15070:                        vpnint   => $domdefaults{'waf_vpnint'},
15071:                        vpnext   => $domdefaults{'waf_vpnext'},
15072:                        sslopt   => $domdefaults{'waf_sslopt'},
15073:                     };
15074:     return $proxyinfo;
15075: }
15076: 
15077: sub ip_match {
15078:     my ($ip,$pattern_str) = @_;
15079:     $ip=Net::CIDR::cidrvalidate($ip);
15080:     if ($ip) {
15081:         return Net::CIDR::cidrlookup($ip,split(/\s*,\s*/,$pattern_str));
15082:     }
15083:     return;
15084: }
15085: 
15086: sub get_proxy_alias {
15087:     my ($lonid) = @_;
15088:     if ($lonid eq '') {
15089:         $lonid = $perlvar{'lonHostID'};
15090:     }
15091:     if (!defined(&hostname($lonid))) {
15092:         return;
15093:     }
15094:     if ($lonid ne '') {
15095:         my ($alias,$cached) = &is_cached_new('proxyalias',$lonid);
15096:         if ($cached) {
15097:             return $alias;
15098:         }
15099:         my $dom = &host_domain($lonid);
15100:         if ($dom ne '') {
15101:             my $cachetime = 60*60*24;
15102:             my %domconfig =
15103:                 &get_dom('configuration',['wafproxy'],$dom);
15104:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15105:                 if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
15106:                     $alias = $domconfig{'wafproxy'}{'alias'}{$lonid};
15107:                 }
15108:             }
15109:             return &do_cache_new('proxyalias',$lonid,$alias,$cachetime);
15110:         }
15111:     }
15112:     return;
15113: }
15114: 
15115: sub use_proxy_alias {
15116:     my ($r,$lonid) = @_;
15117:     my $alias = &get_proxy_alias($lonid);
15118:     if ($alias) {
15119:         my $dom = &host_domain($lonid);
15120:         if ($dom ne '') {
15121:             my $proxyinfo = &get_proxy_settings($dom);
15122:             my ($vpnint,$remote_ip);
15123:             if (ref($proxyinfo) eq 'HASH') {
15124:                 $vpnint = $proxyinfo->{'vpnint'};
15125:                 if ($vpnint) {
15126:                     $remote_ip = &get_requestor_ip($r,1,1);
15127:                 }
15128:             }
15129:             unless ($vpnint && &ip_match($remote_ip,$vpnint)) {
15130:                 return $alias;
15131:             }
15132:         }
15133:     }
15134:     return;
15135: }
15136: 
15137: sub alias_sso {
15138:     my ($lonid) = @_;
15139:     if ($lonid eq '') {
15140:         $lonid = $perlvar{'lonHostID'};
15141:     }
15142:     if (!defined(&hostname($lonid))) {
15143:         return;
15144:     }
15145:     if ($lonid ne '') {
15146:         my ($use_alias,$cached) = &is_cached_new('proxysaml',$lonid);
15147:         if ($cached) {
15148:             return $use_alias;
15149:         }
15150:         my $dom = &host_domain($lonid);
15151:         if ($dom ne '') {
15152:             my $cachetime = 60*60*24;
15153:             my %domconfig =
15154:                 &get_dom('configuration',['wafproxy'],$dom);
15155:             if (ref($domconfig{'wafproxy'}) eq 'HASH') {
15156:                 if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
15157:                     $use_alias = $domconfig{'wafproxy'}{'saml'}{$lonid};
15158:                 }
15159:             }
15160:             return &do_cache_new('proxysaml',$lonid,$use_alias,$cachetime);
15161:         }
15162:     }
15163:     return;
15164: }
15165: 
15166: sub get_saml_landing {
15167:     my ($lonid) = @_;
15168:     if ($lonid eq '') {
15169:         my $defdom = &default_login_domain();
15170:         my @hosts = &current_machine_ids();
15171:         if (@hosts > 1) {
15172:             foreach my $hostid (@hosts) {
15173:                 if (&host_domain($hostid) eq $defdom) {
15174:                     $lonid = $hostid;
15175:                     last;
15176:                 }
15177:             }
15178:         } else {
15179:             $lonid = $perlvar{'lonHostID'};
15180:         }
15181:         if ($lonid) {
15182:             unless (&host_domain($lonid) eq $defdom) {
15183:                 return;
15184:             }
15185:         } else {
15186:             return;
15187:         }
15188:     } elsif (!defined(&hostname($lonid))) {
15189:         return;
15190:     }
15191:     my ($landing,$cached) = &is_cached_new('samllanding',$lonid);
15192:     if ($cached) {
15193:         return $landing;
15194:     }
15195:     my $dom = &host_domain($lonid);
15196:     if ($dom ne '') {
15197:         my $cachetime = 60*60*24;
15198:         my %domconfig =
15199:             &get_dom('configuration',['login'],$dom);
15200:         if (ref($domconfig{'login'}) eq 'HASH') {
15201:             if (ref($domconfig{'login'}{'saml'}) eq 'HASH') {
15202:                 if (ref($domconfig{'login'}{'saml'}{$lonid}) eq 'HASH') {
15203:                     $landing = 1;
15204:                 }
15205:             }
15206:         }
15207:         return &do_cache_new('samllanding',$lonid,$landing,$cachetime);
15208:     }
15209:     return;
15210: }
15211: 
15212: # ------------------------------------------------------------- Declutters URLs
15213: 
15214: sub declutter {
15215:     my $thisfn=shift;
15216:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
15217:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
15218:         $thisfn=~s{^/home/httpd/html}{};
15219:     }
15220:     $thisfn=~s/^\///;
15221:     $thisfn=~s|^adm/wrapper/||;
15222:     $thisfn=~s|^adm/coursedocs/showdoc/||;
15223:     $thisfn=~s/^res\///;
15224:     $thisfn=~s/^priv\///;
15225:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
15226:         $thisfn=~s/\?.+$//;
15227:     }
15228:     return $thisfn;
15229: }
15230: 
15231: # ------------------------------------------------------------- Clutter up URLs
15232: 
15233: sub clutter {
15234:     my $thisfn='/'.&declutter(shift);
15235:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
15236: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
15237:        $thisfn='/res'.$thisfn; 
15238:     }
15239:     if ($thisfn !~m|^/adm|) {
15240: 	if ($thisfn =~ m|^/ext/|) {
15241: 	    $thisfn='/adm/wrapper'.$thisfn;
15242: 	} else {
15243: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
15244: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
15245: 	    if ($embstyle eq 'ssi'
15246: 		|| ($embstyle eq 'hdn')
15247: 		|| ($embstyle eq 'rat')
15248: 		|| ($embstyle eq 'prv')
15249: 		|| ($embstyle eq 'ign')) {
15250: 		#do nothing with these
15251: 	    } elsif (($embstyle eq 'img') 
15252: 		|| ($embstyle eq 'emb')
15253: 		|| ($embstyle eq 'wrp')) {
15254: 		$thisfn='/adm/wrapper'.$thisfn;
15255: 	    } elsif ($embstyle eq 'unk'
15256: 		     && $thisfn!~/\.(sequence|page)$/) {
15257: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
15258: 	    } else {
15259: #		&logthis("Got a blank emb style");
15260: 	    }
15261: 	}
15262:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
15263:         $thisfn='/adm/wrapper'.$thisfn;
15264:     }
15265:     return $thisfn;
15266: }
15267: 
15268: sub clutter_with_no_wrapper {
15269:     my $uri = &clutter(shift);
15270:     if ($uri =~ m-^/adm/-) {
15271: 	$uri =~ s-^/adm/wrapper/-/-;
15272: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
15273:     }
15274:     return $uri;
15275: }
15276: 
15277: sub freeze_escape {
15278:     my ($value)=@_;
15279:     if (ref($value)) {
15280: 	$value=&nfreeze($value);
15281: 	return '__FROZEN__'.&escape($value);
15282:     }
15283:     return &escape($value);
15284: }
15285: 
15286: 
15287: sub thaw_unescape {
15288:     my ($value)=@_;
15289:     if ($value =~ /^__FROZEN__/) {
15290: 	substr($value,0,10,undef);
15291: 	$value=&unescape($value);
15292: 	return &thaw($value);
15293:     }
15294:     return &unescape($value);
15295: }
15296: 
15297: sub correct_line_ends {
15298:     my ($result)=@_;
15299:     $$result =~s/\r\n/\n/mg;
15300:     $$result =~s/\r/\n/mg;
15301: }
15302: # ================================================================ Main Program
15303: 
15304: sub goodbye {
15305:    &logthis("Starting Shut down");
15306: #not converted to using infrastruture and probably shouldn't be
15307:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
15308: #converted
15309: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
15310:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
15311: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
15312: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
15313: #1.1 only
15314: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
15315: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
15316: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
15317: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
15318:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
15319:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
15320:    &logthis(sprintf("%-20s is %s",'hits',$hits));
15321:    &flushcourselogs();
15322:    &logthis("Shutting down");
15323: }
15324: 
15325: sub get_dns {
15326:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
15327:     if (!$ignore_cache) {
15328: 	my ($content,$cached)=
15329: 	    &is_cached_new('dns',$url);
15330: 	if ($cached) {
15331: 	    &$func($content,$hashref);
15332: 	    return;
15333: 	}
15334:     }
15335: 
15336:     my %alldns;
15337:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
15338:         foreach my $dns (<$config>) {
15339: 	    next if ($dns !~ /^\^(\S*)/x);
15340:             my $line = $1;
15341:             my ($host,$protocol) = split(/:/,$line);
15342:             if ($protocol ne 'https') {
15343:                 $protocol = 'http';
15344:             }
15345: 	    $alldns{$host} = $protocol;
15346:         }
15347:         close($config);
15348:     }
15349:     while (%alldns) {
15350: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
15351:         my ($contents,@content);
15352:         if ($dns eq Sys::Hostname::FQDN::fqdn()) {
15353:             my $command = (split('/',$url))[3];
15354:             my ($dir,$file) = &parse_getdns_url($command,$url);
15355:             delete($alldns{$dns});
15356:             next if (($dir eq '') || ($file eq ''));
15357:             if (open(my $config,'<',"$dir/$file")) {
15358:                 @content = <$config>;
15359:                 close($config);
15360:             }
15361:             if ($url eq '/adm/dns/loncapaCRL') {
15362:                 $contents = join('',@content);
15363:             }
15364:         } else {
15365: 	    my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
15366:             my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
15367:             delete($alldns{$dns});
15368: 	    next if ($response->is_error());
15369:             if ($url eq '/adm/dns/loncapaCRL') {
15370:                 $contents = $response->content;
15371:             } else {
15372:                 @content = split("\n",$response->content);
15373:             }
15374:         }
15375:         if ($url eq '/adm/dns/loncapaCRL') {
15376:             return &$func($contents);
15377:         } else {
15378: 	    unless ($nocache) {
15379: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
15380: 	    }
15381: 	    &$func(\@content,$hashref);
15382:             return;
15383:         }
15384:     }
15385:     my $which = (split('/',$url,4))[3];
15386:     if ($which eq 'loncapaCRL') {
15387:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15388:         if (-e $diskfile) {
15389:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
15390:         } else {
15391:             &logthis("unable to contact DNS, no on disk file $diskfile available");
15392:         }
15393:     } else {
15394:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
15395:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
15396:             my @content = <$config>;
15397:             close($config);
15398:             &$func(\@content,$hashref);
15399:         }
15400:     }
15401:     return;
15402: }
15403: 
15404: # ------------------------------------------------------Get DNS checksums file
15405: sub parse_dns_checksums_tab {
15406:     my ($lines,$hashref) = @_;
15407:     my $lonhost = $perlvar{'lonHostID'};
15408:     my $machine_dom = &host_domain($lonhost);
15409:     my $loncaparev = &get_server_loncaparev($machine_dom);
15410:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
15411:     my $webconfdir = '/etc/httpd/conf';
15412:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
15413:         $webconfdir = '/etc/apache2';
15414:     } elsif ($distro =~ /^sles(\d+)$/) {
15415:         if ($1 >= 10) {
15416:             $webconfdir = '/etc/apache2';
15417:         }
15418:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
15419:         if ($1 >= 10.0) {
15420:             $webconfdir = '/etc/apache2';
15421:         }
15422:     }
15423:     my ($release,$timestamp) = split(/\-/,$loncaparev);
15424:     my (%chksum,%revnum);
15425:     if (ref($lines) eq 'ARRAY') {
15426:         chomp(@{$lines});
15427:         my $version = shift(@{$lines});
15428:         if ($version eq $release) {  
15429:             foreach my $line (@{$lines}) {
15430:                 my ($file,$version,$shasum) = split(/,/,$line);
15431:                 if ($file =~ m{^/etc/httpd/conf}) {
15432:                     if ($webconfdir eq '/etc/apache2') {
15433:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
15434:                     }
15435:                 }
15436:                 $chksum{$file} = $shasum;
15437:                 $revnum{$file} = $version;
15438:             }
15439:             if (ref($hashref) eq 'HASH') {
15440:                 %{$hashref} = (
15441:                                 sums     => \%chksum,
15442:                                 versions => \%revnum,
15443:                               );
15444:             }
15445:         }
15446:     }
15447:     return;
15448: }
15449: 
15450: sub fetch_dns_checksums {
15451:     my %checksums;
15452:     my $machine_dom = &host_domain($perlvar{'lonHostID'});
15453:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
15454:     my ($release,$timestamp) = split(/\-/,$loncaparev);
15455:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
15456:              \%checksums);
15457:     return \%checksums;
15458: }
15459: 
15460: sub fetch_crl_pemfile {
15461:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
15462: }
15463: 
15464: sub save_crl_pem {
15465:     my ($content) = @_;
15466:     my ($msg,$hadchanges);
15467:     if ($content ne '') {
15468:         my $now = time;
15469:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
15470:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
15471:         if (open(my $fh,'>',"$tmpcrl")) {
15472:             print $fh $content;
15473:             close($fh);
15474:             if (-e $lonca) {
15475:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
15476:                     my $check = <PIPE>;
15477:                     close(PIPE);
15478:                     chomp($check);
15479:                     if ($check eq 'verify OK') {
15480:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
15481:                         my $backup;
15482:                         if (-e $dest) {
15483:                             if (&File::Copy::move($dest,"$dest.bak")) {
15484:                                 $backup = 'ok';
15485:                             }
15486:                         }
15487:                         if (&File::Copy::move($tmpcrl,$dest)) {
15488:                             $msg = 'ok';
15489:                             if ($backup) {
15490:                                 my (%oldnums,%newnums);
15491:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
15492:                                     while (<PIPE>) {
15493:                                         $oldnums{(split(/:/))[1]} = 1;
15494:                                     }
15495:                                     close(PIPE);
15496:                                 }
15497:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
15498:                                     while(<PIPE>) {
15499:                                         $newnums{(split(/:/))[1]} = 1;
15500:                                     }
15501:                                     close(PIPE);
15502:                                 }
15503:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
15504:                                     unless (exists($oldnums{$key})) {
15505:                                         $hadchanges = 1;
15506:                                         last;
15507:                                     }
15508:                                 }
15509:                                 unless ($hadchanges) {
15510:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
15511:                                         unless (exists($newnums{$key})) {
15512:                                             $hadchanges = 1;
15513:                                             last;
15514:                                         }
15515:                                     }
15516:                                 }
15517:                             }
15518:                         }
15519:                     } else {
15520:                         unlink($tmpcrl);
15521:                     }
15522:                 } else {
15523:                     unlink($tmpcrl);
15524:                 }
15525:             } else {
15526:                 unlink($tmpcrl);
15527:             }
15528:         }
15529:     }
15530:     return ($msg,$hadchanges);
15531: }
15532: 
15533: sub parse_getdns_url {
15534:     my ($command,$url) = @_;
15535:     my $dir = $perlvar{'lonTabDir'};
15536:     my $file;
15537:     if ($command eq 'hosts') {
15538:         $file = 'dns_hosts.tab';
15539:     } elsif ($command eq 'domain') {
15540:         $file = 'dns_domain.tab';
15541:     } elsif ($command eq 'checksums') {
15542:         my $version = (split('/',$url))[4];
15543:         $file = "dns_checksums/$version.tab",
15544:     } elsif ($command eq 'loncapaCRL') {
15545:         $dir = $perlvar{'lonCertificateDirectory'};
15546:         $file = $perlvar{'lonnetCertRevocationList'};
15547:     }
15548:     return ($dir,$file);
15549: }
15550: 
15551: # ------------------------------------------------------------ Read domain file
15552: {
15553:     my $loaded;
15554:     my %domain;
15555: 
15556:     sub parse_domain_tab {
15557: 	my ($lines) = @_;
15558: 	foreach my $line (@$lines) {
15559: 	    next if ($line =~ /^(\#|\s*$ )/x);
15560: 
15561: 	    chomp($line);
15562: 	    my ($name,@elements) = split(/:/,$line,9);
15563: 	    my %this_domain;
15564: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
15565: 			       'lang_def', 'city', 'longi', 'lati',
15566: 			       'primary') {
15567: 		$this_domain{$field} = shift(@elements);
15568: 	    }
15569: 	    $domain{$name} = \%this_domain;
15570: 	}
15571:     }
15572: 
15573:     sub reset_domain_info {
15574: 	undef($loaded);
15575: 	undef(%domain);
15576:     }
15577: 
15578:     sub load_domain_tab {
15579: 	my ($ignore_cache,$nocache) = @_;
15580: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
15581: 	my $fh;
15582: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
15583: 	    my @lines = <$fh>;
15584: 	    &parse_domain_tab(\@lines);
15585: 	}
15586: 	close($fh);
15587: 	$loaded = 1;
15588:     }
15589: 
15590:     sub domain {
15591: 	&load_domain_tab() if (!$loaded);
15592: 
15593: 	my ($name,$what) = @_;
15594: 	return if ( !exists($domain{$name}) );
15595: 
15596: 	if (!$what) {
15597: 	    return $domain{$name}{'description'};
15598: 	}
15599: 	return $domain{$name}{$what};
15600:     }
15601: 
15602:     sub domain_info {
15603:         &load_domain_tab() if (!$loaded);
15604:         return %domain;
15605:     }
15606: 
15607: }
15608: 
15609: 
15610: # ------------------------------------------------------------- Read hosts file
15611: {
15612:     my %hostname;
15613:     my %hostdom;
15614:     my %libserv;
15615:     my $loaded;
15616:     my %name_to_host;
15617:     my %internetdom;
15618:     my %LC_dns_serv;
15619: 
15620:     sub parse_hosts_tab {
15621: 	my ($file) = @_;
15622: 	foreach my $configline (@$file) {
15623: 	    next if ($configline =~ /^(\#|\s*$ )/x);
15624:             chomp($configline);
15625: 	    if ($configline =~ /^\^/) {
15626:                 if ($configline =~ /^\^([\w.\-]+)/) {
15627:                     $LC_dns_serv{$1} = 1;
15628:                 }
15629:                 next;
15630:             }
15631: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
15632: 	    $name=~s/\s//g;
15633: 	    if ($id && $domain && $role && $name) {
15634:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
15635:                     my $curr = $hostname{$id};
15636:                     my $skip;
15637:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
15638:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
15639:                             $skip = 1;
15640:                         } else {
15641:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
15642:                         }
15643:                     }
15644:                     unless ($skip) {
15645:                         push(@{$name_to_host{$name}},$id);
15646:                     }
15647:                 } else {
15648:                     push(@{$name_to_host{$name}},$id);
15649:                 }
15650: 		$hostname{$id}=$name;
15651: 		$hostdom{$id}=$domain;
15652: 		if ($role eq 'library') { $libserv{$id}=$name; }
15653:                 if (defined($protocol)) {
15654:                     if ($protocol eq 'https') {
15655:                         $protocol{$id} = $protocol;
15656:                     } else {
15657:                         $protocol{$id} = 'http'; 
15658:                     }
15659:                 } else {
15660:                     $protocol{$id} = 'http';
15661:                 }
15662:                 if (defined($intdom)) {
15663:                     $internetdom{$id} = $intdom;
15664:                 }
15665: 	    }
15666: 	}
15667:     }
15668:     
15669:     sub reset_hosts_info {
15670: 	&purge_remembered();
15671: 	&reset_domain_info();
15672: 	&reset_hosts_ip_info();
15673:         undef(%internetdom);
15674: 	undef(%name_to_host);
15675: 	undef(%hostname);
15676: 	undef(%hostdom);
15677: 	undef(%libserv);
15678: 	undef($loaded);
15679:     }
15680: 
15681:     sub load_hosts_tab {
15682: 	my ($ignore_cache,$nocache) = @_;
15683: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
15684: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
15685: 	my @config = <$config>;
15686: 	&parse_hosts_tab(\@config);
15687: 	close($config);
15688: 	$loaded=1;
15689:     }
15690: 
15691:     sub hostname {
15692: 	&load_hosts_tab() if (!$loaded);
15693: 
15694: 	my ($lonid) = @_;
15695: 	return $hostname{$lonid};
15696:     }
15697: 
15698:     sub all_hostnames {
15699: 	&load_hosts_tab() if (!$loaded);
15700: 
15701: 	return %hostname;
15702:     }
15703: 
15704:     sub all_names {
15705:         my ($ignore_cache,$nocache) = @_;
15706: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
15707: 
15708: 	return %name_to_host;
15709:     }
15710: 
15711:     sub all_host_domain {
15712:         &load_hosts_tab() if (!$loaded);
15713:         return %hostdom;
15714:     }
15715: 
15716:     sub all_host_intdom {
15717:         &load_hosts_tab() if (!$loaded);
15718:         return %internetdom;
15719:     }
15720: 
15721:     sub is_library {
15722: 	&load_hosts_tab() if (!$loaded);
15723: 
15724: 	return exists($libserv{$_[0]});
15725:     }
15726: 
15727:     sub all_library {
15728: 	&load_hosts_tab() if (!$loaded);
15729: 
15730: 	return %libserv;
15731:     }
15732: 
15733:     sub unique_library {
15734: 	#2x reverse removes all hostnames that appear more than once
15735:         my %unique = reverse &all_library();
15736:         return reverse %unique;
15737:     }
15738: 
15739:     sub get_servers {
15740: 	&load_hosts_tab() if (!$loaded);
15741: 
15742: 	my ($domain,$type) = @_;
15743: 	my %possible_hosts = ($type eq 'library') ? %libserv
15744: 	                                          : %hostname;
15745: 	my %result;
15746: 	if (ref($domain) eq 'ARRAY') {
15747: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15748: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
15749: 		    $result{$host} = $hostname;
15750: 		}
15751: 	    }
15752: 	} else {
15753: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
15754: 		if ($hostdom{$host} eq $domain) {
15755: 		    $result{$host} = $hostname;
15756: 		}
15757: 	    }
15758: 	}
15759: 	return %result;
15760:     }
15761: 
15762:     sub get_unique_servers {
15763:         my %unique = reverse &get_servers(@_);
15764: 	return reverse %unique;
15765:     }
15766: 
15767:     sub host_domain {
15768: 	&load_hosts_tab() if (!$loaded);
15769: 
15770: 	my ($lonid) = @_;
15771: 	return $hostdom{$lonid};
15772:     }
15773: 
15774:     sub all_domains {
15775: 	&load_hosts_tab() if (!$loaded);
15776: 
15777: 	my %seen;
15778: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
15779: 	return @uniq;
15780:     }
15781: 
15782:     sub internet_dom {
15783:         &load_hosts_tab() if (!$loaded);
15784: 
15785:         my ($lonid) = @_;
15786:         return $internetdom{$lonid};
15787:     }
15788: 
15789:     sub is_LC_dns {
15790:         &load_hosts_tab() if (!$loaded);
15791: 
15792:         my ($hostname) = @_;
15793:         return exists($LC_dns_serv{$hostname});
15794:     }
15795: 
15796: }
15797: 
15798: { 
15799:     my %iphost;
15800:     my %name_to_ip;
15801:     my %lonid_to_ip;
15802: 
15803:     sub get_hosts_from_ip {
15804: 	my ($ip) = @_;
15805: 	my %iphosts = &get_iphost();
15806: 	if (ref($iphosts{$ip})) {
15807: 	    return @{$iphosts{$ip}};
15808: 	}
15809: 	return;
15810:     }
15811:     
15812:     sub reset_hosts_ip_info {
15813: 	undef(%iphost);
15814: 	undef(%name_to_ip);
15815: 	undef(%lonid_to_ip);
15816:     }
15817: 
15818:     sub get_host_ip {
15819: 	my ($lonid) = @_;
15820: 	if (exists($lonid_to_ip{$lonid})) {
15821: 	    return $lonid_to_ip{$lonid};
15822: 	}
15823: 	my $name=&hostname($lonid);
15824:    	my $ip = gethostbyname($name);
15825: 	return if (!$ip || length($ip) ne 4);
15826: 	$ip=inet_ntoa($ip);
15827: 	$name_to_ip{$name}   = $ip;
15828: 	$lonid_to_ip{$lonid} = $ip;
15829: 	return $ip;
15830:     }
15831:     
15832:     sub get_iphost {
15833: 	my ($ignore_cache,$nocache) = @_;
15834: 
15835: 	if (!$ignore_cache) {
15836: 	    if (%iphost) {
15837: 		return %iphost;
15838: 	    }
15839: 	    my ($ip_info,$cached)=
15840: 		&is_cached_new('iphost','iphost');
15841: 	    if ($cached) {
15842: 		%iphost      = %{$ip_info->[0]};
15843: 		%name_to_ip  = %{$ip_info->[1]};
15844: 		%lonid_to_ip = %{$ip_info->[2]};
15845: 		return %iphost;
15846: 	    }
15847: 	}
15848: 
15849: 	# get yesterday's info for fallback
15850: 	my %old_name_to_ip;
15851: 	my ($ip_info,$cached)=
15852: 	    &is_cached_new('iphost','iphost');
15853: 	if ($cached) {
15854: 	    %old_name_to_ip = %{$ip_info->[1]};
15855: 	}
15856: 
15857: 	my %name_to_host = &all_names($ignore_cache,$nocache);
15858: 	foreach my $name (keys(%name_to_host)) {
15859: 	    my $ip;
15860: 	    if (!exists($name_to_ip{$name})) {
15861: 		$ip = gethostbyname($name);
15862: 		if (!$ip || length($ip) ne 4) {
15863: 		    if (defined($old_name_to_ip{$name})) {
15864: 			$ip = $old_name_to_ip{$name};
15865: 			&logthis("Can't find $name defaulting to old $ip");
15866: 		    } else {
15867: 			&logthis("Name $name no IP found");
15868: 			next;
15869: 		    }
15870: 		} else {
15871: 		    $ip=inet_ntoa($ip);
15872: 		}
15873: 		$name_to_ip{$name} = $ip;
15874: 	    } else {
15875: 		$ip = $name_to_ip{$name};
15876: 	    }
15877: 	    foreach my $id (@{ $name_to_host{$name} }) {
15878: 		$lonid_to_ip{$id} = $ip;
15879: 	    }
15880: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
15881: 	}
15882:         unless ($nocache) {
15883: 	    &do_cache_new('iphost','iphost',
15884: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
15885: 		          48*60*60);
15886:         }
15887: 
15888: 	return %iphost;
15889:     }
15890: 
15891:     #
15892:     #  Given a DNS returns the loncapa host name for that DNS 
15893:     # 
15894:     sub host_from_dns {
15895:         my ($dns) = @_;
15896:         my @hosts;
15897:         my $ip;
15898: 
15899:         if (exists($name_to_ip{$dns})) {
15900:             $ip = $name_to_ip{$dns};
15901:         }
15902:         if (!$ip) {
15903:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
15904:             if (length($ip) == 4) { 
15905: 	        $ip   = &IO::Socket::inet_ntoa($ip);
15906:             }
15907:         }
15908:         if ($ip) {
15909: 	    @hosts = get_hosts_from_ip($ip);
15910: 	    return $hosts[0];
15911:         }
15912:         return undef;
15913:     }
15914: 
15915:     sub get_internet_names {
15916:         my ($lonid) = @_;
15917:         return if ($lonid eq '');
15918:         my ($idnref,$cached)=
15919:             &is_cached_new('internetnames',$lonid);
15920:         if ($cached) {
15921:             return $idnref;
15922:         }
15923:         my $ip = &get_host_ip($lonid);
15924:         my @hosts = &get_hosts_from_ip($ip);
15925:         my %iphost = &get_iphost();
15926:         my (@idns,%seen);
15927:         foreach my $id (@hosts) {
15928:             my $dom = &host_domain($id);
15929:             my $prim_id = &domain($dom,'primary');
15930:             my $prim_ip = &get_host_ip($prim_id);
15931:             next if ($seen{$prim_ip});
15932:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
15933:                 foreach my $id (@{$iphost{$prim_ip}}) {
15934:                     my $intdom = &internet_dom($id);
15935:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
15936:                         push(@idns,$intdom);
15937:                     }
15938:                 }
15939:             }
15940:             $seen{$prim_ip} = 1;
15941:         }
15942:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
15943:     }
15944: 
15945: }
15946: 
15947: sub all_loncaparevs {
15948:     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);
15949: }
15950: 
15951: # ---------------------------------------------------------- Read loncaparev table
15952: {
15953:     sub load_loncaparevs { 
15954:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
15955:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
15956:                 while (my $configline=<$config>) {
15957:                     chomp($configline);
15958:                     my ($hostid,$loncaparev)=split(/:/,$configline);
15959:                     $loncaparevs{$hostid}=$loncaparev;
15960:                 }
15961:                 close($config);
15962:             }
15963:         }
15964:     }
15965: }
15966: 
15967: # ---------------------------------------------------------- Read serverhostID table
15968: {
15969:     sub load_serverhomeIDs {
15970:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
15971:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
15972:                 while (my $configline=<$config>) {
15973:                     chomp($configline);
15974:                     my ($name,$id)=split(/:/,$configline);
15975:                     $serverhomeIDs{$name}=$id;
15976:                 }
15977:                 close($config);
15978:             }
15979:         }
15980:     }
15981: }
15982: 
15983: 
15984: BEGIN {
15985: 
15986: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
15987:     unless ($readit) {
15988: {
15989:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
15990:     %perlvar = (%perlvar,%{$configvars});
15991: }
15992: 
15993: 
15994: # ------------------------------------------------------ Read spare server file
15995: {
15996:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15997: 
15998:     while (my $configline=<$config>) {
15999:        chomp($configline);
16000:        if ($configline) {
16001: 	   my ($host,$type) = split(':',$configline,2);
16002: 	   if (!defined($type) || $type eq '') { $type = 'default' };
16003: 	   push(@{ $spareid{$type} }, $host);
16004:        }
16005:     }
16006:     close($config);
16007: }
16008: # ------------------------------------------------------------ Read permissions
16009: {
16010:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
16011: 
16012:     while (my $configline=<$config>) {
16013: 	chomp($configline);
16014: 	if ($configline) {
16015: 	    my ($role,$perm)=split(/ /,$configline);
16016: 	    if ($perm ne '') { $pr{$role}=$perm; }
16017: 	}
16018:     }
16019:     close($config);
16020: }
16021: 
16022: # -------------------------------------------- Read plain texts for permissions
16023: {
16024:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
16025: 
16026:     while (my $configline=<$config>) {
16027: 	chomp($configline);
16028: 	if ($configline) {
16029: 	    my ($short,@plain)=split(/:/,$configline);
16030:             %{$prp{$short}} = ();
16031: 	    if (@plain > 0) {
16032:                 $prp{$short}{'std'} = $plain[0];
16033:                 for (my $i=1; $i<@plain; $i++) {
16034:                     $prp{$short}{'alt'.$i} = $plain[$i];  
16035:                 }
16036:             }
16037: 	}
16038:     }
16039:     close($config);
16040: }
16041: 
16042: # ---------------------------------------------------------- Read package table
16043: {
16044:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
16045: 
16046:     while (my $configline=<$config>) {
16047: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
16048: 	chomp($configline);
16049: 	my ($short,$plain)=split(/:/,$configline);
16050: 	my ($pack,$name)=split(/\&/,$short);
16051: 	if ($plain ne '') {
16052: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
16053: 	    $packagetab{$short}=$plain; 
16054: 	}
16055:     }
16056:     close($config);
16057: }
16058: 
16059: # ---------------------------------------------------------- Read loncaparev table
16060: 
16061: &load_loncaparevs();
16062: 
16063: # ---------------------------------------------------------- Read serverhostID table
16064: 
16065: &load_serverhomeIDs();
16066: 
16067: # ---------------------------------------------------------- Read releaseslist XML
16068: {
16069:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
16070:     if (-e $file) {
16071:         my $parser = HTML::LCParser->new($file);
16072:         while (my $token = $parser->get_token()) {
16073:             if ($token->[0] eq 'S') {
16074:                 my $item = $token->[1];
16075:                 my $name = $token->[2]{'name'};
16076:                 my $value = $token->[2]{'value'};
16077:                 my $valuematch = $token->[2]{'valuematch'};
16078:                 my $namematch = $token->[2]{'namematch'};
16079:                 if ($item eq 'parameter') {
16080:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
16081:                         my $release = $parser->get_text();
16082:                         $release =~ s/(^\s*|\s*$ )//gx;
16083:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
16084:                     }
16085:                 } elsif ($item ne '' && $name ne '') {
16086:                     my $release = $parser->get_text();
16087:                     $release =~ s/(^\s*|\s*$ )//gx;
16088:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
16089:                 }
16090:             }
16091:         }
16092:     }
16093: }
16094: 
16095: # ---------------------------------------------------------- Read managers table
16096: {
16097:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
16098:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
16099:             while (my $configline=<$config>) {
16100:                 chomp($configline);
16101:                 next if ($configline =~ /^\#/);
16102:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
16103:                     $managerstab{$configline} = 1;
16104:                 }
16105:             }
16106:             close($config);
16107:         }
16108:     }
16109: }
16110: 
16111: # ------------- set up temporary directory
16112: {
16113:     $tmpdir = LONCAPA::tempdir();
16114: 
16115: }
16116: 
16117: # ------------- set default texengine (domain default overrides this)
16118: {
16119:     $deftex = LONCAPA::texengine();
16120: }
16121: 
16122: # ------------- set default minimum length for passwords for internal auth users
16123: {
16124:     $passwdmin = LONCAPA::passwd_min();
16125: }
16126: 
16127: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
16128: 				'compress_threshold'=> 20_000,
16129:  			        });
16130: 
16131: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
16132: $dumpcount=0;
16133: $locknum=0;
16134: 
16135: &logtouch();
16136: &logthis('<font color="yellow">INFO: Read configuration</font>');
16137: $readit=1;
16138:     {
16139: 	use integer;
16140: 	my $test=(2**32)+1;
16141: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
16142: 	&logthis(" Detected 64bit platform ($_64bit)");
16143:     }
16144: }
16145: }
16146: 
16147: 1;
16148: __END__
16149: 
16150: =pod
16151: 
16152: =head1 NAME
16153: 
16154: Apache::lonnet - Subroutines to ask questions about things in the network.
16155: 
16156: =head1 SYNOPSIS
16157: 
16158: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
16159: 
16160:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
16161: 
16162: Common parameters:
16163: 
16164: =over 4
16165: 
16166: =item *
16167: 
16168: $uname : an internal username (if $cname expecting a course Id specifically)
16169: 
16170: =item *
16171: 
16172: $udom : a domain (if $cdom expecting a course's domain specifically)
16173: 
16174: =item *
16175: 
16176: $symb : a resource instance identifier
16177: 
16178: =item *
16179: 
16180: $namespace : the name of a .db file that contains the data needed or
16181: being set.
16182: 
16183: =back
16184: 
16185: =head1 OVERVIEW
16186: 
16187: lonnet provides subroutines which interact with the
16188: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
16189: about classes, users, and resources.
16190: 
16191: For many of these objects you can also use this to store data about
16192: them or modify them in various ways.
16193: 
16194: =head2 Symbs
16195: 
16196: To identify a specific instance of a resource, LON-CAPA uses symbols
16197: or "symbs"X<symb>. These identifiers are built from the URL of the
16198: map, the resource number of the resource in the map, and the URL of
16199: the resource itself. The latter is somewhat redundant, but might help
16200: if maps change.
16201: 
16202: An example is
16203: 
16204:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
16205: 
16206: The respective map entry is
16207: 
16208:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
16209:   title="Problem 2">
16210:  </resource>
16211: 
16212: Symbs are used by the random number generator, as well as to store and
16213: restore data specific to a certain instance of for example a problem.
16214: 
16215: =head2 Storing And Retrieving Data
16216: 
16217: X<store()>X<cstore()>X<restore()>Three of the most important functions
16218: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
16219: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
16220: is is the non-critical message twin of cstore. These functions are for
16221: handlers to store a perl hash to a user's permanent data space in an
16222: easy manner, and to retrieve it again on another call. It is expected
16223: that a handler would use this once at the beginning to retrieve data,
16224: and then again once at the end to send only the new data back.
16225: 
16226: The data is stored in the user's data directory on the user's
16227: homeserver under the ID of the course.
16228: 
16229: The hash that is returned by restore will have all of the previous
16230: value for all of the elements of the hash.
16231: 
16232: Example:
16233: 
16234:  #creating a hash
16235:  my %hash;
16236:  $hash{'foo'}='bar';
16237: 
16238:  #storing it
16239:  &Apache::lonnet::cstore(\%hash);
16240: 
16241:  #changing a value
16242:  $hash{'foo'}='notbar';
16243: 
16244:  #adding a new value
16245:  $hash{'bar'}='foo';
16246:  &Apache::lonnet::cstore(\%hash);
16247: 
16248:  #retrieving the hash
16249:  my %history=&Apache::lonnet::restore();
16250: 
16251:  #print the hash
16252:  foreach my $key (sort(keys(%history))) {
16253:    print("\%history{$key} = $history{$key}");
16254:  }
16255: 
16256: Will print out:
16257: 
16258:  %history{1:foo} = bar
16259:  %history{1:keys} = foo:timestamp
16260:  %history{1:timestamp} = 990455579
16261:  %history{2:bar} = foo
16262:  %history{2:foo} = notbar
16263:  %history{2:keys} = foo:bar:timestamp
16264:  %history{2:timestamp} = 990455580
16265:  %history{bar} = foo
16266:  %history{foo} = notbar
16267:  %history{timestamp} = 990455580
16268:  %history{version} = 2
16269: 
16270: Note that the special hash entries C<keys>, C<version> and
16271: C<timestamp> were added to the hash. C<version> will be equal to the
16272: total number of versions of the data that have been stored. The
16273: C<timestamp> attribute will be the UNIX time the hash was
16274: stored. C<keys> is available in every historical section to list which
16275: keys were added or changed at a specific historical revision of a
16276: hash.
16277: 
16278: B<Warning>: do not store the hash that restore returns directly. This
16279: will cause a mess since it will restore the historical keys as if the
16280: were new keys. I.E. 1:foo will become 1:1:foo etc.
16281: 
16282: Calling convention:
16283: 
16284:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
16285:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
16286: 
16287: For more detailed information, see lonnet specific documentation.
16288: 
16289: =head1 RETURN MESSAGES
16290: 
16291: =over 4
16292: 
16293: =item * B<con_lost>: unable to contact remote host
16294: 
16295: =item * B<con_delayed>: unable to contact remote host, message will be delivered
16296: when the connection is brought back up
16297: 
16298: =item * B<con_failed>: unable to contact remote host and unable to save message
16299: for later delivery
16300: 
16301: =item * B<error:>: an error a occurred, a description of the error follows the :
16302: 
16303: =item * B<no_such_host>: unable to fund a host associated with the user/domain
16304: that was requested
16305: 
16306: =back
16307: 
16308: =head1 PUBLIC SUBROUTINES
16309: 
16310: =head2 Session Environment Functions
16311: 
16312: =over 4
16313: 
16314: =item * 
16315: X<appenv()>
16316: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
16317: the user envirnoment file, and will be restored for each access this
16318: user makes during this session, also modifies the %env for the current
16319: process. Optional rolesarrayref - if defined contains a reference to an array
16320: of roles which are exempt from the restriction on modifying user.role entries 
16321: in the user's environment.db and in %env.    
16322: 
16323: =item *
16324: X<delenv()>
16325: B<delenv($delthis,$regexp)>: removes all items from the session
16326: environment file that begin with $delthis. If the 
16327: optional second arg - $regexp - is true, $delthis is treated as a 
16328: regular expression, otherwise \Q$delthis\E is used. 
16329: The values are also deleted from the current processes %env.
16330: 
16331: =item * get_env_multiple($name) 
16332: 
16333: gets $name from the %env hash, it seemlessly handles the cases where multiple
16334: values may be defined and end up as an array ref.
16335: 
16336: returns an array of values
16337: 
16338: =back
16339: 
16340: =head2 User Information
16341: 
16342: =over 4
16343: 
16344: =item *
16345: X<queryauthenticate()>
16346: B<queryauthenticate($uname,$udom)>: try to determine user's current 
16347: authentication scheme
16348: 
16349: =item *
16350: X<authenticate()>
16351: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
16352: authenticate user from domain's lib servers (first use the current
16353: one). C<$upass> should be the users password.
16354: $checkdefauth is optional (value is 1 if a check should be made to
16355:    authenticate user using default authentication method, and allow
16356:    account creation if username does not have account in the domain).
16357: $clientcancheckhost is optional (value is 1 if checking whether the
16358:    server can host will occur on the client side in lonauth.pm).   
16359: 
16360: =item *
16361: X<homeserver()>
16362: B<homeserver($uname,$udom)>: find the server which has
16363: the user's directory and files (there must be only one), this caches
16364: the answer, and also caches if there is a borken connection.
16365: 
16366: =item *
16367: X<idget()>
16368: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
16369: a list of student/employee IDs or clicker IDs
16370: (student/employee IDs are a unique resource in a domain, there must be 
16371: only 1 ID per username, and only 1 username per ID in a specific domain).
16372: clickerIDs are not necessarily unique, as students might share clickers.
16373: (returns hash: id=>name,id=>name)
16374: 
16375: =item *
16376: X<idrget()>
16377: B<idrget($udom,@unames)>: find the IDs behind a list of
16378: usernames (returns hash: name=>id,name=>id)
16379: 
16380: =item *
16381: X<idput()>
16382: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
16383: names and associated student/employee IDs or clicker IDs.
16384: 
16385: =item *
16386: X<iddel()>
16387: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
16388: student/employee ID or clicker ID username look-ups from domain.
16389: The homeserver ($uhome) and namespace ($namespace) are optional.
16390: If no $uhome is provided, it will be determined usig &homeserver()
16391: for each user.  If no $namespace is provided, the default is ids.
16392: 
16393: =item *
16394: X<updateclickers()>
16395: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
16396: clicker ID-to-username look-ups in clickers.db on library server.
16397: Permitted actions are add or del (i.e., add or delete). The 
16398: clickers.db contains clickerID as keys (escaped), and each corresponding
16399: value is an escaped comma-separated list of usernames (for whom the
16400: library server is the homeserver), who registered that particular ID.
16401: If $critical is true, the update will be sent via &critical, otherwise
16402: &reply() will be used.
16403: 
16404: =item *
16405: X<rolesinit()>
16406: B<rolesinit($udom,$username)>: get user privileges.
16407: returns user role, first access and timer interval hashes
16408: 
16409: =item *
16410: X<privileged()>
16411: B<privileged($username,$domain)>: returns a true if user has a
16412: privileged and active role (i.e. su or dc), false otherwise.
16413: 
16414: =item *
16415: X<getsection()>
16416: B<getsection($udom,$uname,$cname)>: finds the section of student in the
16417: course $cname, return section name/number or '' for "not in course"
16418: and '-1' for "no section"
16419: 
16420: =item *
16421: X<userenvironment()>
16422: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
16423: passed in @what from the requested user's environment, returns a hash
16424: 
16425: =item * 
16426: X<userlog_query()>
16427: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
16428: activity.log file. %filters defines filters applied when parsing the
16429: log file. These can be start or end timestamps, or the type of action
16430: - log to look for Login or Logout events, check for Checkin or
16431: Checkout, role for role selection. The response is in the form
16432: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
16433: escaped strings of the action recorded in the activity.log file.
16434: 
16435: =back
16436: 
16437: =head2 User Roles
16438: 
16439: =over 4
16440: 
16441: =item *
16442: 
16443: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
16444: returns codes for allowed actions.
16445: 
16446: The first argument is required, all others are optional.
16447: 
16448: $priv is the privilege being checked.
16449: $uri contains additional information about what is being checked for access (e.g.,
16450: URL, course ID etc.). 
16451: $symb is the unique resource instance identifier in a course; if needed,
16452: but not provided, it will be retrieved via a call to &symbread(). 
16453: $role is the role for which a priv is being checked (only used if priv is evb). 
16454: $clientip is the user's IP address (only used when checking for access to portfolio 
16455: files).
16456: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
16457: prevents recursive calls to &allowed.
16458: 
16459:  F: full access
16460:  U,I,K: authentication modes (cxx only)
16461:  '': forbidden
16462:  1: user needs to choose course
16463:  2: browse allowed
16464:  A: passphrase authentication needed
16465:  B: access temporarily blocked because of a blocking event in a course.
16466:  D: access blocked because access is required via session initiated via deep-link 
16467: 
16468: =item *
16469: 
16470: constructaccess($url,$setpriv) : check for access to construction space URL
16471: 
16472: See if the owner domain and name in the URL match those in the
16473: expected environment.  If so, return three element list
16474: ($ownername,$ownerdomain,$ownerhome).
16475: 
16476: Otherwise return the null string.
16477: 
16478: If second argument 'setpriv' is true, it assigns the privileges,
16479: and returns the same three element list, unless the owner has
16480: blocked "ad hoc" Domain Coordinator access to the Author Space,
16481: in which case the null string is returned.
16482: 
16483: =item *
16484: 
16485: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
16486: define a custom role rolename set privileges in format of lonTabs/roles.tab
16487: for system, domain, and course level. $uname and $udom are optional (current
16488: user's username and domain will be used when either of $uname or $udom are absent.
16489: 
16490: =item *
16491: 
16492: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
16493: (rolesplain.tab); plain text explanation of a user role term.
16494: $type is Course (default) or Community.
16495: If $forcedefault evaluates to true, text returned will be default 
16496: text for $type. Otherwise, if this is a course, the text returned 
16497: will be a custom name for the role (if defined in the course's 
16498: environment).  If no custom name is defined the default is returned.
16499:    
16500: =item *
16501: 
16502: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
16503: All arguments are optional. Returns a hash of a roles, either for
16504: co-author/assistant author roles for a user's Construction Space
16505: (default), or if $context is 'userroles', roles for the user himself,
16506: In the hash, keys are set to colon-separated $uname,$udom,$role, and
16507: (optionally) if $withsec is true, a fourth colon-separated item - $section.
16508: For each key, value is set to colon-separated start and end times for
16509: the role.  If no username and domain are specified, will default to
16510: current user/domain. Types, roles, and roledoms are references to arrays
16511: of role statuses (active, future or previous), roles 
16512: (e.g., cc,in, st etc.) and domains of the roles which can be used
16513: to restrict the list of roles reported. If no array ref is 
16514: provided for types, will default to return only active roles.
16515: 
16516: =item *
16517: 
16518: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
16519: user: $uname:$udom has a role in the course: $cdom_$cnum. 
16520: 
16521: Additional optional arguments are: $type (if role checking is to be restricted 
16522: to certain user status types -- previous (expired roles), active (currently
16523: available roles) or future (roles available in the future), and
16524: $hideprivileged -- if true will not report course roles for users who
16525: have active Domain Coordinator role in course's domain or in additional
16526: domains (specified in 'Domains to check for privileged users' in course
16527: environment -- set via:  Course Settings -> Classlists and staff listing).
16528: 
16529: =item *
16530: 
16531: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
16532: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
16533: $possdomains and $possroles are optional array refs -- to domains to check and
16534: roles to check.  If $possdomains is not specified, a dump will be done of the
16535: users' roles.db to check for a dc or su role in any domain. This can be
16536: time consuming if &privileged is called repeatedly (e.g., when displaying a
16537: classlist), so in such cases, supplying a $possdomains array is preferred, as
16538: this then allows &privileged_by_domain() to be used, which caches the identity
16539: of privileged users, eliminating the need for repeated calls to &dump().
16540: 
16541: =item *
16542: 
16543: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
16544: where the outer hash keys are domains specified in the $possdomains array ref,
16545: next inner hash keys are privileged roles specified in the $roles array ref,
16546: and the innermost hash contains key = value pairs for username:domain = end:start
16547: for active or future "privileged" users with that role in that domain. To avoid
16548: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
16549: innerhash are cached using priv_$role and $dom as the identifiers.
16550: 
16551: =back
16552: 
16553: =head2 User Modification
16554: 
16555: =over 4
16556: 
16557: =item *
16558: 
16559: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
16560: user for the level given by URL.  Optional start and end dates (leave empty
16561: string or zero for "no date")
16562: 
16563: =item *
16564: 
16565: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
16566: change a users, password, possible return values are: ok,
16567: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
16568: refused
16569: 
16570: =item *
16571: 
16572: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
16573: 
16574: =item *
16575: 
16576: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
16577:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
16578: 
16579: will update user information (firstname,middlename,lastname,generation,
16580: permanentemail), and if forceid is true, student/employee ID also.
16581: A user's institutional affiliation(s) can also be updated.
16582: User information fields will not be overwritten with empty entries 
16583: unless the field is included in the $candelete array reference.
16584: This array is included when a single user is modified via "Manage Users",
16585: or when Autoupdate.pl is run by cron in a domain.
16586: 
16587: =item *
16588: 
16589: modifystudent
16590: 
16591: modify a student's enrollment and identification information.
16592: The course id is resolved based on the current user's environment.  
16593: This means the invoking user must be a course coordinator or otherwise
16594: associated with a course.
16595: 
16596: This call is essentially a wrapper for lonnet::modifyuser and
16597: lonnet::modify_student_enrollment
16598: 
16599: Inputs: 
16600: 
16601: =over 4
16602: 
16603: =item B<$udom> Student's loncapa domain
16604: 
16605: =item B<$uname> Student's loncapa login name
16606: 
16607: =item B<$uid> Student/Employee ID
16608: 
16609: =item B<$umode> Student's authentication mode
16610: 
16611: =item B<$upass> Student's password
16612: 
16613: =item B<$first> Student's first name
16614: 
16615: =item B<$middle> Student's middle name
16616: 
16617: =item B<$last> Student's last name
16618: 
16619: =item B<$gene> Student's generation
16620: 
16621: =item B<$usec> Student's section in course
16622: 
16623: =item B<$end> Unix time of the roles expiration
16624: 
16625: =item B<$start> Unix time of the roles start date
16626: 
16627: =item B<$forceid> If defined, allow $uid to be changed
16628: 
16629: =item B<$desiredhome> server to use as home server for student
16630: 
16631: =item B<$email> Student's permanent e-mail address
16632: 
16633: =item B<$type> Type of enrollment (auto or manual)
16634: 
16635: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
16636: 
16637: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
16638: 
16639: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
16640: 
16641: =item B<$context> role change context (shown in User Management Logs display in a course)
16642: 
16643: =item B<$inststatus> institutional status of user - : separated string of escaped status types
16644: 
16645: =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.
16646: 
16647: =back
16648: 
16649: =item *
16650: 
16651: modify_student_enrollment
16652: 
16653: Change a student's enrollment status in a class.  The environment variable
16654: 'role.request.course' must be defined for this function to proceed.
16655: 
16656: Inputs:
16657: 
16658: =over 4
16659: 
16660: =item $udom, student's domain
16661: 
16662: =item $uname, student's name
16663: 
16664: =item $uid, student's user id
16665: 
16666: =item $first, student's first name
16667: 
16668: =item $middle
16669: 
16670: =item $last
16671: 
16672: =item $gene
16673: 
16674: =item $usec
16675: 
16676: =item $end
16677: 
16678: =item $start
16679: 
16680: =item $type
16681: 
16682: =item $locktype
16683: 
16684: =item $cid
16685: 
16686: =item $selfenroll
16687: 
16688: =item $context
16689: 
16690: =item $credits, number of credits student will earn from this class
16691: 
16692: =item $instsec, institutional course section code for student
16693: 
16694: =back
16695: 
16696: 
16697: =item *
16698: 
16699: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
16700: custom role; give a custom role to a user for the level given by URL.  Specify
16701: name and domain of role author, and role name
16702: 
16703: =item *
16704: 
16705: revokerole($udom,$uname,$url,$role) : revoke a role for url
16706: 
16707: =item *
16708: 
16709: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
16710: 
16711: =back
16712: 
16713: =head2 Course Infomation
16714: 
16715: =over 4
16716: 
16717: =item *
16718: 
16719: coursedescription($courseid,$options) : returns a hash of information about the
16720: specified course id, including all environment settings for the
16721: course, the description of the course will be in the hash under the
16722: key 'description'
16723: 
16724: $options is an optional parameter that if supplied is a hash reference that controls
16725: what how this function works.  It has the following key/values:
16726: 
16727: =over 4
16728: 
16729: =item freshen_cache
16730: 
16731: If defined, and the environment cache for the course is valid, it is 
16732: returned in the returned hash.
16733: 
16734: =item one_time
16735: 
16736: If defined, the last cache time is set to _now_
16737: 
16738: =item user
16739: 
16740: If defined, the supplied username is used instead of the current user.
16741: 
16742: 
16743: =back
16744: 
16745: =item *
16746: 
16747: resdata($name,$domain,$type,@which) : request for current parameter
16748: setting for a specific $type, where $type is either 'course' or 'user',
16749: @what should be a list of parameters to ask about. This routine caches
16750: answers for 10 minutes.
16751: 
16752: =item *
16753: 
16754: get_courseresdata($courseid, $domain) : dump the entire course resource
16755: data base, returning a hash that is keyed by the resource name and has
16756: values that are the resource value.  I believe that the timestamps and
16757: versions are also returned.
16758: 
16759: =back
16760: 
16761: =head2 Course Modification
16762: 
16763: =over 4
16764: 
16765: =item *
16766: 
16767: writecoursepref($courseid,%prefs) : write preferences (environment
16768: database) for a course
16769: 
16770: =item *
16771: 
16772: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
16773: 
16774: =item *
16775: 
16776: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
16777: 
16778: =item *
16779: 
16780: is_course($courseid), is_course($cdom, $cnum)
16781: 
16782: Accepts either a combined $courseid (in the form of domain_courseid) or the
16783: two component version $cdom, $cnum. It checks if the specified course exists.
16784: 
16785: Returns:
16786:     undef if the course doesn't exist, otherwise
16787:     in scalar context the combined courseid.
16788:     in list context the two components of the course identifier, domain and 
16789:     courseid.    
16790: 
16791: =back
16792: 
16793: =head2 Bubblesheet Configuration
16794: 
16795: =over 4
16796: 
16797: =item *
16798: 
16799: get_scantron_config($which)
16800: 
16801: $which - the name of the configuration to parse from the file.
16802: 
16803: Parses and returns the bubblesheet configuration line selected as a
16804: hash of configuration file fields.
16805: 
16806: 
16807: Returns:
16808:     If the named configuration is not in the file, an empty
16809:     hash is returned.
16810: 
16811:     a hash with the fields
16812:       name         - internal name for the this configuration setup
16813:       description  - text to display to operator that describes this config
16814:       CODElocation - if 0 or the string 'none'
16815:                           - no CODE exists for this config
16816:                      if -1 || the string 'letter'
16817:                           - a CODE exists for this config and is
16818:                             a string of letters
16819:                      Unsupported value (but planned for future support)
16820:                           if a positive integer
16821:                                - The CODE exists as the first n items from
16822:                                  the question section of the form
16823:                           if the string 'number'
16824:                                - The CODE exists for this config and is
16825:                                  a string of numbers
16826:       CODEstart   - (only matter if a CODE exists) column in the line where
16827:                      the CODE starts
16828:       CODElength  - length of the CODE
16829:       IDstart     - column where the student/employee ID starts
16830:       IDlength    - length of the student/employee ID info
16831:       Qstart      - column where the information from the bubbled
16832:                     'questions' start
16833:       Qlength     - number of columns comprising a single bubble line from
16834:                     the sheet. (usually either 1 or 10)
16835:       Qon         - either a single character representing the character used
16836:                     to signal a bubble was chosen in the positional setup, or
16837:                     the string 'letter' if the letter of the chosen bubble is
16838:                     in the final, or 'number' if a number representing the
16839:                     chosen bubble is in the file (1->A 0->J)
16840:       Qoff        - the character used to represent that a bubble was
16841:                     left blank
16842:       PaperID     - if the scanning process generates a unique number for each
16843:                     sheet scanned the column that this ID number starts in
16844:       PaperIDlength - number of columns that comprise the unique ID number
16845:                       for the sheet of paper
16846:       FirstName   - column that the first name starts in
16847:       FirstNameLength - number of columns that the first name spans
16848:       LastName    - column that the last name starts in
16849:       LastNameLength - number of columns that the last name spans
16850:       BubblesPerRow - number of bubbles available in each row used to
16851:                       bubble an answer. (If not specified, 10 assumed).
16852: 
16853: 
16854: =item *
16855: 
16856: get_scantronformat_file($cdom)
16857: 
16858: $cdom - the course's domain (optional); if not supplied, uses
16859: domain for current $env{'request.course.id'}.
16860: 
16861: Returns an array containing lines from the scantron format file for
16862: the domain of the course.
16863: 
16864: If a url for a custom.tab file is listed in domain's configuration.db,
16865: lines are from this file.
16866: 
16867: Otherwise, if a default.tab has been published in RES space by the
16868: domainconfig user, lines are from this file.
16869: 
16870: Otherwise, fall back to getting lines from the legacy file on the
16871: local server:  /home/httpd/lonTabs/default_scantronformat.tab
16872: 
16873: =back
16874: 
16875: =head2 Resource Subroutines
16876: 
16877: =over 4
16878: 
16879: =item *
16880: 
16881: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
16882: 
16883: =item *
16884: 
16885: repcopy($filename) : subscribes to the requested file, and attempts to
16886: replicate from the owning library server, Might return
16887: 'unavailable', 'not_found', 'forbidden', 'ok', or
16888: 'bad_request', also attempts to grab the metadata for the
16889: resource. Expects the local filesystem pathname
16890: (/home/httpd/html/res/....)
16891: 
16892: =back
16893: 
16894: =head2 Resource Information
16895: 
16896: =over 4
16897: 
16898: =item *
16899: 
16900: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
16901: and returns the value of a variety of different possible values,
16902: $varname should be a request string, and the other parameters can be
16903: used to specify who and what one is asking about. Ordinarily, $cid 
16904: does not need to be specified, as it is retrived from 
16905: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
16906: within lonuserstate::loadmap() when initializing a course, before
16907: $env{'request.course.id'} has been set, so it needs to be provided
16908: in that one case.
16909: 
16910: Possible values for $varname are environment.lastname (or other item
16911: from the envirnment hash), user.name (or someother aspect about the
16912: user), resource.0.maxtries (or some other part and parameter of a
16913: resource)
16914: 
16915: =item *
16916: 
16917: directcondval($number) : get current value of a condition; reads from a state
16918: string
16919: 
16920: =item *
16921: 
16922: condval($condidx) : value of condition index based on state
16923: 
16924: =item *
16925: 
16926: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
16927: resource's metadata, $what should be either a specific key, or either
16928: 'keys' (to get a list of possible keys) or 'packages' to get a list of
16929: packages that this resource currently uses, the last 3 arguments are 
16930: only used internally for recursive metadata.
16931: 
16932: the toolsymb is only used where the uri is for an external tool (for which
16933: the uri as well as the symb are guaranteed to be unique).
16934: 
16935: this function automatically caches all requests except any made recursively
16936: to retrieve a list of metadata keys for an imported library file ($liburi is 
16937: defined).
16938: 
16939: =item *
16940: 
16941: metadata_query($query,$custom,$customshow) : make a metadata query against the
16942: network of library servers; returns file handle of where SQL and regex results
16943: will be stored for query
16944: 
16945: =item *
16946: 
16947: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
16948: return symbolic list entry (all arguments optional). 
16949: 
16950: Args: filename is the filename (including path) for the file for which a symb 
16951: is required; donotrecurse, if true will prevent calls to allowed() being made 
16952: to check access status if more than one resource was found in the bighash 
16953: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
16954: a randompick); ignorecachednull, if true will prevent a symb of '' being 
16955: returned if $env{$cache_str} is defined as ''; checkforblock if true will
16956: cause possible symbs to be checked to determine if they are subject to content
16957: blocking, if so they will not be included as possible symbs; possibles is a
16958: ref to a hash, which, as a side effect, will be populated with all possible 
16959: symbs (content blocking not tested).
16960:  
16961: returns the data handle
16962: 
16963: =item *
16964: 
16965: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
16966: and is a possible symb for the URL in $thisfn, and if is an encrypted
16967: resource that the user accessed using /enc/ returns a 1 on success, 0
16968: on failure, user must be in a course, as it assumes the existence of
16969: the course initial hash, and uses $env('request.course.id'}.  The third
16970: arg is an optional reference to a scalar.  If this arg is passed in the 
16971: call to symbverify, it will be set to 1 if the symb has been set to be 
16972: encrypted; otherwise it will be null.  
16973: 
16974: =item *
16975: 
16976: symbclean($symb) : removes versions numbers from a symb, returns the
16977: cleaned symb
16978: 
16979: =item *
16980: 
16981: is_on_map($uri) : checks if the $uri is somewhere on the current
16982: course map, user must be in a course for it to work.
16983: 
16984: =item *
16985: 
16986: numval($salt) : return random seed value (addend for rndseed)
16987: 
16988: =item *
16989: 
16990: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
16991: a random seed, all arguments are optional, if they aren't sent it uses the
16992: environment to derive them. Note: if symb isn't sent and it can't get one
16993: from &symbread it will use the current time as its return value
16994: 
16995: =item *
16996: 
16997: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16998: unfakeable, receipt
16999: 
17000: =item *
17001: 
17002: receipt() : API to ireceipt working off of env values; given out to users
17003: 
17004: =item *
17005: 
17006: countacc($url) : count the number of accesses to a given URL
17007: 
17008: =item *
17009: 
17010: 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
17011: 
17012: =item *
17013: 
17014: 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)
17015: 
17016: =item *
17017: 
17018: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
17019: 
17020: =item *
17021: 
17022: devalidate($symb) : devalidate temporary spreadsheet calculations,
17023: forcing spreadsheet to reevaluate the resource scores next time.
17024: 
17025: =item * 
17026: 
17027: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
17028: when viewing in course context.
17029: 
17030:  input: six args -- filename (decluttered), course number, course domain,
17031:                     url, symb (if registered) and group (if this is a 
17032:                     group item -- e.g., bulletin board, group page etc.).
17033: 
17034:  output: array of five scalars --
17035:          $cfile -- url for file editing if editable on current server
17036:          $home -- homeserver of resource (i.e., for author if published,
17037:                                           or course if uploaded.).
17038:          $switchserver --  1 if server switch will be needed.
17039:          $forceedit -- 1 if icon/link should be to go to edit mode 
17040:          $forceview -- 1 if icon/link should be to go to view mode
17041: 
17042: =item *
17043: 
17044: is_course_upload($file,$cnum,$cdom)
17045: 
17046: Used in course context to determine if current file was uploaded to 
17047: the course (i.e., would be found in /userfiles/docs on the course's 
17048: homeserver.
17049: 
17050:   input: 3 args -- filename (decluttered), course number and course domain.
17051:   output: boolean -- 1 if file was uploaded.
17052: 
17053: =back
17054: 
17055: =head2 Storing/Retreiving Data
17056: 
17057: =over 4
17058: 
17059: =item *
17060: 
17061: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
17062: permanently for this url; hashref needs to be given and should be a \%hashname;
17063: the remaining args aren't required and if they aren't passed or are '' they will
17064: be derived from the env (with the exception of $laststore, which is an 
17065: optional arg used when a user's submission is stored in grading).
17066: $laststore is $version=$timestamp, where $version is the most recent version
17067: number retrieved for the corresponding $symb in the $namespace db file, and
17068: $timestamp is the timestamp for that transaction (UNIX time).
17069: $laststore is currently only passed when cstore() is called by 
17070: structuretags::finalize_storage().
17071: 
17072: =item *
17073: 
17074: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
17075: but uses critical subroutine
17076: 
17077: =item *
17078: 
17079: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
17080: all args are optional
17081: 
17082: =item *
17083: 
17084: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
17085: dumps the complete (or key matching regexp) namespace into a hash
17086: ($udom, $uname, $regexp, $range are optional) for a namespace that is
17087: normally &store()ed into
17088: 
17089: $range should be either an integer '100' (give me the first 100
17090:                                            matching records)
17091:               or be  two integers sperated by a - with no spaces
17092:                  '30-50' (give me the 30th through the 50th matching
17093:                           records)
17094: 
17095: 
17096: =item *
17097: 
17098: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
17099: replaces a &store() version of data with a replacement set of data
17100: for a particular resource in a namespace passed in the $storehash hash 
17101: reference. If $tolog is true, the transaction is logged in the courselog
17102: with an action=PUTSTORE.
17103: 
17104: =item *
17105: 
17106: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
17107: works very similar to store/cstore, but all data is stored in a
17108: temporary location and can be reset using tmpreset, $storehash should
17109: be a hash reference, returns nothing on success
17110: 
17111: =item *
17112: 
17113: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
17114: similar to restore, but all data is stored in a temporary location and
17115: can be reset using tmpreset. Returns a hash of values on success,
17116: error string otherwise.
17117: 
17118: =item *
17119: 
17120: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
17121: deltes all keys for $symb form the temporary storage hash.
17122: 
17123: =item *
17124: 
17125: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17126: reference filled in from namesp ($udom and $uname are optional)
17127: 
17128: =item *
17129: 
17130: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
17131: namesp ($udom and $uname are optional)
17132: 
17133: =item *
17134: 
17135: dump($namespace,$udom,$uname,$regexp,$range) : 
17136: dumps the complete (or key matching regexp) namespace into a hash
17137: ($udom, $uname, $regexp, $range are optional)
17138: 
17139: $range should be either an integer '100' (give me the first 100
17140:                                            matching records)
17141:               or be  two integers sperated by a - with no spaces
17142:                  '30-50' (give me the 30th through the 50th matching
17143:                           records)
17144: =item *
17145: 
17146: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
17147: $store can be a scalar, an array reference, or if the amount to be 
17148: incremented is > 1, a hash reference.
17149: 
17150: ($udom and $uname are optional)
17151: 
17152: =item *
17153: 
17154: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
17155: ($udom and $uname are optional)
17156: 
17157: =item *
17158: 
17159: cput($namespace,$storehash,$udom,$uname) : critical put
17160: ($udom and $uname are optional)
17161: 
17162: =item *
17163: 
17164: newput($namespace,$storehash,$udom,$uname) :
17165: 
17166: Attempts to store the items in the $storehash, but only if they don't
17167: currently exist, if this succeeds you can be certain that you have 
17168: successfully created a new key value pair in the $namespace db.
17169: 
17170: 
17171: Args:
17172:  $namespace: name of database to store values to
17173:  $storehash: hashref to store to the db
17174:  $udom: (optional) domain of user containing the db
17175:  $uname: (optional) name of user caontaining the db
17176: 
17177: Returns:
17178:  'ok' -> succeeded in storing all keys of $storehash
17179:  'key_exists: <key>' -> failed to anything out of $storehash, as at
17180:                         least <key> already existed in the db (other
17181:                         requested keys may also already exist)
17182:  'error: <msg>' -> unable to tie the DB or other error occurred
17183:  'con_lost' -> unable to contact request server
17184:  'refused' -> action was not allowed by remote machine
17185: 
17186: 
17187: =item *
17188: 
17189: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
17190: reference filled in from namesp (encrypts the return communication)
17191: ($udom and $uname are optional)
17192: 
17193: =item *
17194: 
17195: log($udom,$name,$home,$message) : write to permanent log for user; use
17196: critical subroutine
17197: 
17198: =item *
17199: 
17200: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
17201: array reference filled in from namespace found in domain level on either
17202: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
17203: 
17204: =item *
17205: 
17206: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
17207: domain level either on specified domain server ($uhome) or primary domain 
17208: server ($udom and $uhome are optional)
17209: 
17210: =item * 
17211: 
17212: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
17213: for: authentication, language, quotas, timezone, date locale, and portal URL in
17214: the target domain.
17215: 
17216: May also include additional key => value pairs for the following groups:
17217: 
17218: =over
17219: 
17220: =item
17221: disk quotas (MB allocated by default to portfolios and authoring spaces).
17222: 
17223: =over
17224: 
17225: =item defaultquota, authorquota
17226: 
17227: =back
17228: 
17229: =item
17230: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
17231: portfolio for users).
17232: 
17233: =over
17234: 
17235: =item
17236: aboutme, blog, webdav, portfolio
17237: 
17238: =back
17239: 
17240: =item
17241: requestcourses: ability to request courses, and how requests are processed.
17242: 
17243: =over
17244: 
17245: =item
17246: official, unofficial, community, textbook, placement
17247: 
17248: =back
17249: 
17250: =item
17251: inststatus: types of institutional affiliation, and order in which they are displayed.
17252: 
17253: =over
17254: 
17255: =item
17256: inststatustypes, inststatusorder, inststatusguest
17257: 
17258: =back
17259: 
17260: =item
17261: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
17262: for course's uploaded content.
17263: 
17264: =over
17265: 
17266: =item
17267: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
17268: communityquota, textbookquota, placementquota
17269: 
17270: =back
17271: 
17272: =item
17273: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
17274: on your servers.
17275: 
17276: =over
17277: 
17278: =item 
17279: remotesessions, hostedsessions
17280: 
17281: =back
17282: 
17283: =back
17284: 
17285: In cases where a domain coordinator has never used the "Set Domain Configuration"
17286: utility to create a configuration.db file on a domain's primary library server 
17287: only the following domain defaults: auth_def, auth_arg_def, lang_def
17288: -- corresponding values are authentication type (internal, krb4, krb5,
17289: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
17290: will be available. Values are retrieved from cache (if current), unless the
17291: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
17292: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
17293: 
17294: Typical usage:
17295: 
17296: %domdefaults = &get_domain_defaults($target_domain);
17297: 
17298: =back
17299: 
17300: =head2 Network Status Functions
17301: 
17302: =over 4
17303: 
17304: =item *
17305: 
17306: dirlist() : return directory list based on URI (first arg).
17307: 
17308: Inputs: 1 required, 5 optional.
17309: 
17310: =over
17311: 
17312: =item 
17313: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
17314: 
17315: =item
17316: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
17317: 
17318: =item
17319: $username -  username of user/course to be listed. Extracted from $uri if absent. 
17320: 
17321: =item
17322: $getpropath - boolean: 1 if prepend path using &propath(). 
17323: 
17324: =item
17325: $getuserdir - boolean: 1 if prepend path for "userfiles".
17326: 
17327: =item 
17328: $alternateRoot - path to prepend in place of path from $uri.
17329: 
17330: =back
17331: 
17332: Returns: Array of up to two items.
17333: 
17334: =over
17335: 
17336: a reference to an array of files/subdirectories
17337: 
17338: =over
17339: 
17340: Each element in the array of files/subdirectories is a & separated list of
17341: item name and the result of running stat on the item.  If dirlist was requested
17342: for a file instead of a directory, the item name will be ''. For a directory 
17343: listing, if the item is a metadata file, the element will end &N&M 
17344: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
17345: default copyright set (1).  
17346: 
17347: =back
17348: 
17349: a scalar containing error condition (if encountered).
17350: 
17351: =over
17352: 
17353: =item 
17354: no_host (no homeserver identified for $username:$domain).
17355: 
17356: =item 
17357: no_such_host (server contacted for listing not identified as valid host).
17358: 
17359: =item 
17360: con_lost (connection to remote server failed).
17361: 
17362: =item 
17363: refused (invalid $username:$domain received on lond side).
17364: 
17365: =item 
17366: no_such_dir (directory at specified path on lond side does not exist). 
17367: 
17368: =item 
17369: empty (directory at specified path on lond side is empty).
17370: 
17371: =over
17372: 
17373: This is currently not encountered because the &ls3, &ls2, 
17374: &ls (_handler) routines on the lond side do not filter out
17375: . and .. from a directory listing. 
17376: 
17377: =back
17378: 
17379: =back
17380: 
17381: =back
17382: 
17383: =item *
17384: 
17385: spareserver() : find server with least workload from spare.tab
17386: 
17387: 
17388: =item *
17389: 
17390: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
17391: if there is no corresponding loncapa host.
17392: 
17393: =back
17394: 
17395: 
17396: =head2 Apache Request
17397: 
17398: =over 4
17399: 
17400: =item *
17401: 
17402: ssi($url,%hash) : server side include, does a complete request cycle on url to
17403: localhost, posts hash
17404: 
17405: =back
17406: 
17407: =head2 Data to String to Data
17408: 
17409: =over 4
17410: 
17411: =item *
17412: 
17413: hash2str(%hash) : convert a hash into a string complete with escaping and '='
17414: and '&' separators, supports elements that are arrayrefs and hashrefs
17415: 
17416: =item *
17417: 
17418: hashref2str($hashref) : convert a hashref into a string complete with
17419: escaping and '=' and '&' separators, supports elements that are
17420: arrayrefs and hashrefs
17421: 
17422: =item *
17423: 
17424: arrayref2str($arrayref) : convert an arrayref into a string complete
17425: with escaping and '&' separators, supports elements that are arrayrefs
17426: and hashrefs
17427: 
17428: =item *
17429: 
17430: str2hash($string) : convert string to hash using unescaping and
17431: splitting on '=' and '&', supports elements that are arrayrefs and
17432: hashrefs
17433: 
17434: =item *
17435: 
17436: str2array($string) : convert string to hash using unescaping and
17437: splitting on '&', supports elements that are arrayrefs and hashrefs
17438: 
17439: =back
17440: 
17441: =head2 Logging Routines
17442: 
17443: 
17444: These routines allow one to make log messages in the lonnet.log and
17445: lonnet.perm logfiles.
17446: 
17447: =over 4
17448: 
17449: =item *
17450: 
17451: logtouch() : make sure the logfile, lonnet.log, exists
17452: 
17453: =item *
17454: 
17455: logthis() : append message to the normal lonnet.log file, it gets
17456: preiodically rolled over and deleted.
17457: 
17458: =item *
17459: 
17460: logperm() : append a permanent message to lonnet.perm.log, this log
17461: file never gets deleted by any automated portion of the system, only
17462: messages of critical importance should go in here.
17463: 
17464: 
17465: =back
17466: 
17467: =head2 General File Helper Routines
17468: 
17469: =over 4
17470: 
17471: =item *
17472: 
17473: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
17474: (a) files in /uploaded
17475:   (i) If a local copy of the file exists - 
17476:       compares modification date of local copy with last-modified date for 
17477:       definitive version stored on home server for course. If local copy is 
17478:       stale, requests a new version from the home server and stores it. 
17479:       If the original has been removed from the home server, then local copy 
17480:       is unlinked.
17481:   (ii) If local copy does not exist -
17482:       requests the file from the home server and stores it. 
17483:   
17484:   If $caller is 'uploadrep':  
17485:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
17486:     for request for files originally uploaded via DOCS. 
17487:      - returns 'ok' if fresh local copy now available, -1 otherwise.
17488:   
17489:   Otherwise:
17490:      This indicates a call from the content generation phase of the request.
17491:      -  returns the entire contents of the file or -1.
17492:      
17493: (b) files in /res
17494:    - returns the entire contents of a file or -1; 
17495:    it properly subscribes to and replicates the file if neccessary.
17496: 
17497: 
17498: =item *
17499: 
17500: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
17501:                   reference
17502: 
17503: returns either a stat() list of data about the file or an empty list
17504: if the file doesn't exist or couldn't find out about it (connection
17505: problems or user unknown)
17506: 
17507: =item *
17508: 
17509: filelocation($dir,$file) : returns file system location of a file
17510: based on URI; meant to be "fairly clean" absolute reference, $dir is a
17511: directory that relative $file lookups are to looked in ($dir of /a/dir
17512: and a file of ../bob will become /a/bob)
17513: 
17514: =item *
17515: 
17516: hreflocation($dir,$file) : returns file system location or a URL; same as
17517: filelocation except for hrefs
17518: 
17519: =item *
17520: 
17521: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
17522: also removes beginning /home/httpd/html unless /priv/ follows it.
17523: 
17524: =back
17525: 
17526: =head2 Usererfile file routines (/uploaded*)
17527: 
17528: =over 4
17529: 
17530: =item *
17531: 
17532: userfileupload(): main rotine for putting a file in a user or course's
17533:                   filespace, arguments are,
17534: 
17535:  formname - required - this is the name of the element in $env where the
17536:            filename, and the contents of the file to create/modifed exist
17537:            the filename is in $env{'form.'.$formname.'.filename'} and the
17538:            contents of the file is located in $env{'form.'.$formname}
17539:  context - if coursedoc, store the file in the course of the active role
17540:              of the current user; 
17541:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
17542:            if 'canceloverwrite': delete file in tmp/overwrites directory
17543:  subdir - required - subdirectory to put the file in under ../userfiles/
17544:          if undefined, it will be placed in "unknown"
17545: 
17546:  (This routine calls clean_filename() to remove any dangerous
17547:  characters from the filename, and then calls finuserfileupload() to
17548:  complete the transaction)
17549: 
17550:  returns either the url of the uploaded file (/uploaded/....) if successful
17551:  and /adm/notfound.html if unsuccessful
17552: 
17553: =item *
17554: 
17555: clean_filename(): routine for cleaing a filename up for storage in
17556:                  userfile space, argument is:
17557: 
17558:  filename - proposed filename
17559: 
17560: returns: the new clean filename
17561: 
17562: =item *
17563: 
17564: finishuserfileupload(): routine that creates and sends the file to
17565: userspace, probably shouldn't be called directly
17566: 
17567:   docuname: username or courseid of destination for the file
17568:   docudom: domain of user/course of destination for the file
17569:   formname: same as for userfileupload()
17570:   fname: filename (including subdirectories) for the file
17571:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
17572:           if hashref, and context is scantron, will convert csv format to standard format
17573:   allfiles: reference to hash used to store objects found by parser
17574:   codebase: reference to hash used for codebases of java objects found by parser
17575:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
17576:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
17577:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
17578:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
17579:   context: if 'overwrite', will move the uploaded file from its temporary location to
17580:             userfiles to facilitate overwriting a previously uploaded file with same name.
17581:   mimetype: reference to scalar to accommodate mime type determined
17582:             from File::MMagic if $parser = parse.
17583: 
17584:  returns either the url of the uploaded file (/uploaded/....) if successful
17585:  and /adm/notfound.html if unsuccessful (or an error message if context 
17586:  was 'overwrite').
17587:  
17588: 
17589: =item *
17590: 
17591: renameuserfile(): renames an existing userfile to a new name
17592: 
17593:   Args:
17594:    docuname: username or courseid of destination for the file
17595:    docudom: domain of user/course of destination for the file
17596:    old: current file name (including any subdirs under userfiles)
17597:    new: desired file name (including any subdirs under userfiles)
17598: 
17599: =item *
17600: 
17601: mkdiruserfile(): creates a directory is a userfiles dir
17602: 
17603:   Args:
17604:    docuname: username or courseid of destination for the file
17605:    docudom: domain of user/course of destination for the file
17606:    dir: dir to create (including any subdirs under userfiles)
17607: 
17608: =item *
17609: 
17610: removeuserfile(): removes a file that exists in userfiles
17611: 
17612:   Args:
17613:    docuname: username or courseid of destination for the file
17614:    docudom: domain of user/course of destination for the file
17615:    fname: filname to delete (including any subdirs under userfiles)
17616: 
17617: =item *
17618: 
17619: removeuploadedurl(): convience function for removeuserfile()
17620: 
17621:   Args:
17622:    url:  a full /uploaded/... url to delete
17623: 
17624: =item * 
17625: 
17626: get_portfile_permissions():
17627:   Args:
17628:     domain: domain of user or course contain the portfolio files
17629:     user: name of user or num of course contain the portfolio files
17630:   Returns:
17631:     hashref of a dump of the proper file_permissions.db
17632:    
17633: 
17634: =item * 
17635: 
17636: get_access_controls():
17637: 
17638: Args:
17639:   current_permissions: the hash ref returned from get_portfile_permissions()
17640:   group: (optional) the group you want the files associated with
17641:   file: (optional) the file you want access info on
17642: 
17643: Returns:
17644:     a hash (keys are file names) of hashes containing
17645:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
17646:         values are XML containing access control settings (see below) 
17647: 
17648: Internal notes:
17649: 
17650:  access controls are stored in file_permissions.db as key=value pairs.
17651:     key -> path to file/file_name\0uniqueID:scope_end_start
17652:         where scope -> public,guest,course,group,domains or users.
17653:               end -> UNIX time for end of access (0 -> no end date)
17654:               start -> UNIX time for start of access
17655: 
17656:     value -> XML description of access control
17657:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
17658:             <start></start>
17659:             <end></end>
17660: 
17661:             <password></password>  for scope type = guest
17662: 
17663:             <domain></domain>     for scope type = course or group
17664:             <number></number>
17665:             <roles id="">
17666:              <role></role>
17667:              <access></access>
17668:              <section></section>
17669:              <group></group>
17670:             </roles>
17671: 
17672:             <dom></dom>         for scope type = domains
17673: 
17674:             <users>             for scope type = users
17675:              <user>
17676:               <uname></uname>
17677:               <udom></udom>
17678:              </user>
17679:             </users>
17680:            </scope> 
17681:               
17682:  Access data is also aggregated for each file in an additional key=value pair:
17683:  key -> path to file/file_name\0accesscontrol 
17684:  value -> reference to hash
17685:           hash contains key = value pairs
17686:           where key = uniqueID:scope_end_start
17687:                 value = UNIX time record was last updated
17688: 
17689:           Used to improve speed of look-ups of access controls for each file.  
17690:  
17691:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
17692: 
17693: =item *
17694: 
17695: modify_access_controls():
17696: 
17697: Modifies access controls for a portfolio file
17698: Args
17699: 1. file name
17700: 2. reference to hash of required changes,
17701: 3. domain
17702: 4. username
17703:   where domain,username are the domain of the portfolio owner 
17704:   (either a user or a course) 
17705: 
17706: Returns:
17707: 1. result of additions or updates ('ok' or 'error', with error message). 
17708: 2. result of deletions ('ok' or 'error', with error message).
17709: 3. reference to hash of any new or updated access controls.
17710: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
17711:    key = integer (inbound ID)
17712:    value = uniqueID
17713: 
17714: =item *
17715: 
17716: get_timebased_id():
17717: 
17718: Attempts to get a unique timestamp-based suffix for use with items added to a 
17719: course via the Course Editor (e.g., folders, composite pages, 
17720: group bulletin boards).
17721: 
17722: Args: (first three required; six others optional)
17723: 
17724: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
17725:    docssequence, or name of group
17726: 
17727: 2. keyid (alphanumeric): name of temporary locking key in hash,
17728:    e.g., num, boardids
17729: 
17730: 3. namespace: name of gdbm file used to store suffixes already assigned;  
17731:    file will be named nohist_namespace.db
17732: 
17733: 4. cdom: domain of course; default is current course domain from %env
17734: 
17735: 5. cnum: course number; default is current course number from %env
17736: 
17737: 6. idtype: set to concat if an additional digit is to be appended to the 
17738:    unix timestamp to form the suffix, if the plain timestamp is already
17739:    in use.  Default is to not do this, but simply increment the unix 
17740:    timestamp by 1 until a unique key is obtained.
17741: 
17742: 7. who: holder of locking key; defaults to user:domain for user.
17743: 
17744: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
17745:    retrying); default is 3.
17746: 
17747: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
17748: 
17749: Returns:
17750: 
17751: 1. suffix obtained (numeric)
17752: 
17753: 2. result of deleting locking key (ok if deleted, or lock never obtained)
17754: 
17755: 3. error: contains (localized) error message if an error occurred.
17756: 
17757: 
17758: =back
17759: 
17760: =head2 HTTP Helper Routines
17761: 
17762: =over 4
17763: 
17764: =item *
17765: 
17766: escape() : unpack non-word characters into CGI-compatible hex codes
17767: 
17768: =item *
17769: 
17770: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
17771: 
17772: =back
17773: 
17774: =head1 PRIVATE SUBROUTINES
17775: 
17776: =head2 Underlying communication routines (Shouldn't call)
17777: 
17778: =over 4
17779: 
17780: =item *
17781: 
17782: subreply() : tries to pass a message to lonc, returns con_lost if incapable
17783: 
17784: =item *
17785: 
17786: reply() : uses subreply to send a message to remote machine, logs all failures
17787: 
17788: =item *
17789: 
17790: critical() : passes a critical message to another server; if cannot
17791: get through then place message in connection buffer directory and
17792: returns con_delayed, if incapable of saving message, returns
17793: con_failed
17794: 
17795: =item *
17796: 
17797: reconlonc() : tries to reconnect lonc client processes.
17798: 
17799: =back
17800: 
17801: =head2 Resource Access Logging
17802: 
17803: =over 4
17804: 
17805: =item *
17806: 
17807: flushcourselogs() : flush (save) buffer logs and access logs
17808: 
17809: =item *
17810: 
17811: courselog($what) : save message for course in hash
17812: 
17813: =item *
17814: 
17815: courseacclog($what) : save message for course using &courselog().  Perform
17816: special processing for specific resource types (problems, exams, quizzes, etc).
17817: 
17818: =item *
17819: 
17820: goodbye() : flush course logs and log shutting down; it is called in srm.conf
17821: as a PerlChildExitHandler
17822: 
17823: =back
17824: 
17825: =head2 Other
17826: 
17827: =over 4
17828: 
17829: =item *
17830: 
17831: symblist($mapname,%newhash) : update symbolic storage links
17832: 
17833: =back
17834: 
17835: =cut
17836: 

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