File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1407: download - view: text, annotated - select for diffs
Wed Apr 24 01:44:46 2019 UTC (5 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Forgot Password utility
  - Support configurable: link lifetime, case sensitivity (for username and
    e-mail address), information required, e-mail types, and custom text.
  - Bug 6648: captcha can be enabled for public-facing web form.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1407 2019/04/24 01:44:46 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);
   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 LONCAPA qw(:DEFAULT :match);
  100: use LONCAPA::Configuration;
  101: use LONCAPA::lonmetadata;
  102: use LONCAPA::Lond;
  103: use LONCAPA::LWPReq;
  104: use LONCAPA::transliterate;
  105: 
  106: use File::Copy;
  107: 
  108: my $readit;
  109: my $max_connection_retries = 20;     # Or some such value.
  110: 
  111: require Exporter;
  112: 
  113: our @ISA = qw (Exporter);
  114: our @EXPORT = qw(%env);
  115: 
  116: 
  117: # ------------------------------------ Logging (parameters, docs, slots, roles)
  118: {
  119:     my $logid;
  120:     sub write_log {
  121: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
  122:         if ($context eq 'course') {
  123:             if (($cnum eq '') || ($cdom eq '')) {
  124:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  125:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  126:             }
  127:         }
  128: 	$logid ++;
  129:         my $now = time();
  130: 	my $id=$now.'00000'.$$.'00000'.$logid;
  131:         my $logentry = { 
  132:                           $id => {
  133:                                    'exe_uname' => $env{'user.name'},
  134:                                    'exe_udom'  => $env{'user.domain'},
  135:                                    'exe_time'  => $now,
  136:                                    'exe_ip'    => $ENV{'REMOTE_ADDR'},
  137:                                    'delflag'   => $delflag,
  138:                                    'logentry'  => $storehash,
  139:                                    'uname'     => $uname,
  140:                                    'udom'      => $udom,
  141:                                   }
  142:                        };
  143: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
  144:     }
  145: }
  146: 
  147: sub logtouch {
  148:     my $execdir=$perlvar{'lonDaemons'};
  149:     unless (-e "$execdir/logs/lonnet.log") {	
  150: 	open(my $fh,">>","$execdir/logs/lonnet.log");
  151: 	close $fh;
  152:     }
  153:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
  154:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
  155: }
  156: 
  157: sub logthis {
  158:     my $message=shift;
  159:     my $execdir=$perlvar{'lonDaemons'};
  160:     my $now=time;
  161:     my $local=localtime($now);
  162:     if (open(my $fh,">>","$execdir/logs/lonnet.log")) {
  163: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
  164: 	print $fh $logstring;
  165: 	close($fh);
  166:     }
  167:     return 1;
  168: }
  169: 
  170: sub logperm {
  171:     my $message=shift;
  172:     my $execdir=$perlvar{'lonDaemons'};
  173:     my $now=time;
  174:     my $local=localtime($now);
  175:     if (open(my $fh,">>","$execdir/logs/lonnet.perm.log")) {
  176: 	print $fh "$now:$message:$local\n";
  177: 	close($fh);
  178:     }
  179:     return 1;
  180: }
  181: 
  182: sub create_connection {
  183:     my ($hostname,$lonid) = @_;
  184:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
  185: 				     Type    => SOCK_STREAM,
  186: 				     Timeout => 10);
  187:     return 0 if (!$client);
  188:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname),$loncaparevs{$lonid})."\n");
  189:     my $result = <$client>;
  190:     chomp($result);
  191:     return 1 if ($result eq 'done');
  192:     return 0;
  193: }
  194: 
  195: sub get_server_timezone {
  196:     my ($cnum,$cdom) = @_;
  197:     my $home=&homeserver($cnum,$cdom);
  198:     if ($home ne 'no_host') {
  199:         my $cachetime = 24*3600;
  200:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
  201:         if (defined($cached)) {
  202:             return $timezone;
  203:         } else {
  204:             my $timezone = &reply('servertimezone',$home);
  205:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
  206:         }
  207:     }
  208: }
  209: 
  210: sub get_server_distarch {
  211:     my ($lonhost,$ignore_cache) = @_;
  212:     if (defined($lonhost)) {
  213:         if (!defined(&hostname($lonhost))) {
  214:             return;
  215:         }
  216:         my $cachetime = 12*3600;
  217:         if (!$ignore_cache) {
  218:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
  219:             if (defined($cached)) {
  220:                 return $distarch;
  221:             }
  222:         }
  223:         my $rep = &reply('serverdistarch',$lonhost);
  224:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
  225:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
  226:                 $rep eq '') {
  227:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
  228:         }
  229:     }
  230:     return;
  231: }
  232: 
  233: sub get_servercerts_info {
  234:     my ($lonhost,$hostname,$context) = @_;
  235:     return if ($lonhost eq '');
  236:     if ($hostname eq '') {
  237:         $hostname = &hostname($lonhost);
  238:     }
  239:     return if ($hostname eq '');
  240:     my ($rep,$uselocal);
  241:     if ($context eq 'install') {
  242:         $uselocal = 1;
  243:     } elsif (grep { $_ eq $lonhost } &current_machine_ids()) {
  244:         $uselocal = 1;
  245:     }
  246:     if (($context ne 'cgi') && ($context ne 'install') && ($uselocal)) {
  247:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
  248:         if ($distro eq '') {
  249:             $uselocal = 0;
  250:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
  251:             if ($1 < 6) {
  252:                 $uselocal = 0;
  253:             }
  254:         }  elsif ($distro =~ /^(?:sles)(\d+)$/) {
  255:             if ($1 < 12) {
  256:                 $uselocal = 0;
  257:             }
  258:         }
  259:     }
  260:     if ($uselocal) {
  261:         $rep = LONCAPA::Lond::server_certs(\%perlvar,$lonhost,$hostname);
  262:     } else {
  263:         $rep=&reply('servercerts',$lonhost);
  264:     }
  265:     my ($result,%returnhash);
  266:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  267:         ($rep eq 'unknown_cmd')) {
  268:         $result = $rep;
  269:     } else {
  270:         $result = 'ok';
  271:         my @pairs=split(/\&/,$rep);
  272:         foreach my $item (@pairs) {
  273:             my ($key,$value)=split(/=/,$item,2);
  274:             my $what = &unescape($key);
  275:             $returnhash{$what}=&thaw_unescape($value);
  276:         }
  277:     }
  278:     return ($result,\%returnhash);
  279: }
  280: 
  281: sub get_server_loncaparev {
  282:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
  283:     if (defined($lonhost)) {
  284:         if (!defined(&hostname($lonhost))) {
  285:             undef($lonhost);
  286:         }
  287:     }
  288:     if (!defined($lonhost)) {
  289:         if (defined(&domain($dom,'primary'))) {
  290:             $lonhost=&domain($dom,'primary');
  291:             if ($lonhost eq 'no_host') {
  292:                 undef($lonhost);
  293:             }
  294:         }
  295:     }
  296:     if (defined($lonhost)) {
  297:         my $cachetime = 12*3600;
  298:         if (!$ignore_cache) {
  299:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
  300:             if (defined($cached)) {
  301:                 return $loncaparev;
  302:             }
  303:         }
  304:         my ($answer,$loncaparev);
  305:         my @ids=&current_machine_ids();
  306:         if (grep(/^\Q$lonhost\E$/,@ids)) {
  307:             $answer = $perlvar{'lonVersion'};
  308:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  309:                 $loncaparev = $1;
  310:             }
  311:         } else {
  312:             $answer = &reply('serverloncaparev',$lonhost);
  313:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
  314:                 if ($caller eq 'loncron') {
  315:                     my $hostname = &hostname($lonhost);
  316:                     my $protocol = $protocol{$lonhost};
  317:                     $protocol = 'http' if ($protocol ne 'https');
  318:                     my $url = $protocol.'://'.$hostname.'/adm/about.html';
  319:                     my $request=new HTTP::Request('GET',$url);
  320:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
  321:                     unless ($response->is_error()) {
  322:                         my $content = $response->content;
  323:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
  324:                             $loncaparev = $1;
  325:                         }
  326:                     }
  327:                 } else {
  328:                     $loncaparev = $loncaparevs{$lonhost};
  329:                 }
  330:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
  331:                 $loncaparev = $1;
  332:             }
  333:         }
  334:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
  335:     }
  336: }
  337: 
  338: sub get_server_homeID {
  339:     my ($hostname,$ignore_cache,$caller) = @_;
  340:     unless ($ignore_cache) {
  341:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
  342:         if (defined($cached)) {
  343:             return $serverhomeID;
  344:         }
  345:     }
  346:     my $cachetime = 12*3600;
  347:     my $serverhomeID;
  348:     if ($caller eq 'loncron') { 
  349:         my @machine_ids = &machine_ids($hostname);
  350:         foreach my $id (@machine_ids) {
  351:             my $response = &reply('serverhomeID',$id);
  352:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
  353:                 $serverhomeID = $response;
  354:                 last;
  355:             }
  356:         }
  357:         if ($serverhomeID eq '') {
  358:             $serverhomeID = $machine_ids[-1];
  359:         }
  360:     } else {
  361:         $serverhomeID = $serverhomeIDs{$hostname};
  362:     }
  363:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
  364: }
  365: 
  366: sub get_remote_globals {
  367:     my ($lonhost,$whathash,$ignore_cache) = @_;
  368:     my ($result,%returnhash,%whatneeded);
  369:     if (ref($whathash) eq 'HASH') {
  370:         foreach my $what (sort(keys(%{$whathash}))) {
  371:             my $hashid = $lonhost.'-'.$what;
  372:             my ($response,$cached);
  373:             unless ($ignore_cache) {
  374:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
  375:             }
  376:             if (defined($cached)) {
  377:                 $returnhash{$what} = $response;
  378:             } else {
  379:                 $whatneeded{$what} = 1;
  380:             }
  381:         }
  382:         if (keys(%whatneeded) == 0) {
  383:             $result = 'ok';
  384:         } else {
  385:             my $requested = &freeze_escape(\%whatneeded);
  386:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
  387:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
  388:                 ($rep eq 'unknown_cmd')) {
  389:                 $result = $rep;
  390:             } else {
  391:                 $result = 'ok';
  392:                 my @pairs=split(/\&/,$rep);
  393:                 foreach my $item (@pairs) {
  394:                     my ($key,$value)=split(/=/,$item,2);
  395:                     my $what = &unescape($key);
  396:                     my $hashid = $lonhost.'-'.$what;
  397:                     $returnhash{$what}=&thaw_unescape($value);
  398:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
  399:                 }
  400:             }
  401:         }
  402:     }
  403:     return ($result,\%returnhash);
  404: }
  405: 
  406: sub remote_devalidate_cache {
  407:     my ($lonhost,$cachekeys) = @_;
  408:     my $items;
  409:     return unless (ref($cachekeys) eq 'ARRAY');
  410:     my $cachestr = join('&',@{$cachekeys});
  411:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
  412:     return $response;
  413: }
  414: 
  415: # -------------------------------------------------- Non-critical communication
  416: sub subreply {
  417:     my ($cmd,$server)=@_;
  418:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
  419:     #
  420:     #  With loncnew process trimming, there's a timing hole between lonc server
  421:     #  process exit and the master server picking up the listen on the AF_UNIX
  422:     #  socket.  In that time interval, a lock file will exist:
  423: 
  424:     my $lockfile=$peerfile.".lock";
  425:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
  426: 	sleep(0.1);
  427:     }
  428:     # At this point, either a loncnew parent is listening or an old lonc
  429:     # or loncnew child is listening so we can connect or everything's dead.
  430:     #
  431:     #   We'll give the connection a few tries before abandoning it.  If
  432:     #   connection is not possible, we'll con_lost back to the client.
  433:     #   
  434:     my $client;
  435:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
  436: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  437: 				      Type    => SOCK_STREAM,
  438: 				      Timeout => 10);
  439: 	if ($client) {
  440: 	    last;		# Connected!
  441: 	} else {
  442: 	    &create_connection(&hostname($server),$server);
  443: 	}
  444:         sleep(0.1);	# Try again later if failed connection.
  445:     }
  446:     my $answer;
  447:     if ($client) {
  448: 	print $client "sethost:$server:$cmd\n";
  449: 	$answer=<$client>;
  450: 	if (!$answer) { $answer="con_lost"; }
  451: 	chomp($answer);
  452:     } else {
  453: 	$answer = 'con_lost';	# Failed connection.
  454:     }
  455:     return $answer;
  456: }
  457: 
  458: sub reply {
  459:     my ($cmd,$server)=@_;
  460:     unless (defined(&hostname($server))) { return 'no_such_host'; }
  461:     my $answer=subreply($cmd,$server);
  462:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
  463:         my $logged = $cmd;
  464:         if ($cmd =~ /^encrypt:([^:]+):/) {
  465:             my $subcmd = $1;
  466:             if (($subcmd eq 'auth') || ($subcmd eq 'passwd') ||
  467:                 ($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  468:                 ($subcmd eq 'putdom') || ($subcmd eq 'autoexportgrades')) {
  469:                 (undef,undef,my @rest) = split(/:/,$cmd);
  470:                 if (($subcmd eq 'auth') || ($subcmd eq 'putdom')) {
  471:                     splice(@rest,2,1,'Hidden');
  472:                 } elsif ($subcmd eq 'passwd') {
  473:                     splice(@rest,2,2,('Hidden','Hidden'));
  474:                 } elsif (($subcmd eq 'changeuserauth') || ($subcmd eq 'makeuser') ||
  475:                          ($subcmd eq 'autoexportgrades')) {
  476:                     splice(@rest,3,1,'Hidden');
  477:                 }
  478:                 $logged = join(':',('encrypt:'.$subcmd,@rest));
  479:             }
  480:         }
  481:         &logthis("<font color=\"blue\">WARNING:".
  482:                  " $logged to $server returned $answer</font>");
  483:     }
  484:     return $answer;
  485: }
  486: 
  487: # ----------------------------------------------------------- Send USR1 to lonc
  488: 
  489: sub reconlonc {
  490:     my ($lonid) = @_;
  491:     if ($lonid) {
  492:         my $hostname = &hostname($lonid);
  493: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
  494: 	if ($hostname && -e $peerfile) {
  495: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
  496: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
  497: 					     Type    => SOCK_STREAM,
  498: 					     Timeout => 10);
  499: 	    if ($client) {
  500: 		print $client ("reset_retries\n");
  501: 		my $answer=<$client>;
  502: 		#reset just this one.
  503: 	    }
  504: 	}
  505: 	return;
  506:     }
  507: 
  508:     &logthis("Trying to reconnect lonc");
  509:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  510:     if (open(my $fh,"<",$loncfile)) {
  511: 	my $loncpid=<$fh>;
  512:         chomp($loncpid);
  513:         if (kill 0 => $loncpid) {
  514: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
  515:             kill USR1 => $loncpid;
  516:             sleep 1;
  517:         } else {
  518: 	    &logthis(
  519:                "<font color=\"blue\">WARNING:".
  520:                " lonc at pid $loncpid not responding, giving up</font>");
  521:         }
  522:     } else {
  523: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
  524:     }
  525: }
  526: 
  527: # ------------------------------------------------------ Critical communication
  528: 
  529: sub critical {
  530:     my ($cmd,$server)=@_;
  531:     unless (&hostname($server)) {
  532:         &logthis("<font color=\"blue\">WARNING:".
  533:                " Critical message to unknown server ($server)</font>");
  534:         return 'no_such_host';
  535:     }
  536:     my $answer=reply($cmd,$server);
  537:     if ($answer eq 'con_lost') {
  538: 	&reconlonc($server);
  539: 	my $answer=reply($cmd,$server);
  540:         if ($answer eq 'con_lost') {
  541:             my $now=time;
  542:             my $middlename=$cmd;
  543:             $middlename=substr($middlename,0,16);
  544:             $middlename=~s/\W//g;
  545:             my $dfilename=
  546:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
  547:             $dumpcount++;
  548:             {
  549: 		my $dfh;
  550: 		if (open($dfh,">",$dfilename)) {
  551: 		    print $dfh "$cmd\n"; 
  552: 		    close($dfh);
  553: 		}
  554:             }
  555:             sleep 1;
  556:             my $wcmd='';
  557:             {
  558: 		my $dfh;
  559: 		if (open($dfh,"<",$dfilename)) {
  560: 		    $wcmd=<$dfh>; 
  561: 		    close($dfh);
  562: 		}
  563:             }
  564:             chomp($wcmd);
  565:             if ($wcmd eq $cmd) {
  566: 		&logthis("<font color=\"blue\">WARNING: ".
  567:                          "Connection buffer $dfilename: $cmd</font>");
  568:                 &logperm("D:$server:$cmd");
  569: 	        return 'con_delayed';
  570:             } else {
  571:                 &logthis("<font color=\"red\">CRITICAL:"
  572:                         ." Critical connection failed: $server $cmd</font>");
  573:                 &logperm("F:$server:$cmd");
  574:                 return 'con_failed';
  575:             }
  576:         }
  577:     }
  578:     return $answer;
  579: }
  580: 
  581: # ------------------------------------------- check if return value is an error
  582: 
  583: sub error {
  584:     my ($result) = @_;
  585:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
  586: 	if ($2 == 2) { return undef; }
  587: 	return $1;
  588:     }
  589:     return undef;
  590: }
  591: 
  592: sub convert_and_load_session_env {
  593:     my ($lonidsdir,$handle)=@_;
  594:     my @profile;
  595:     {
  596: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  597: 	if (!$opened) {
  598: 	    return 0;
  599: 	}
  600: 	flock($idf,LOCK_SH);
  601: 	@profile=<$idf>;
  602: 	close($idf);
  603:     }
  604:     my %temp_env;
  605:     foreach my $line (@profile) {
  606: 	if ($line !~ m/=/) {
  607: 	    return 0;
  608: 	}
  609: 	chomp($line);
  610: 	my ($envname,$envvalue)=split(/=/,$line,2);
  611: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
  612:     }
  613:     unlink("$lonidsdir/$handle.id");
  614:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
  615: 	    0640)) {
  616: 	%disk_env = %temp_env;
  617: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
  618: 	untie(%disk_env);
  619:     }
  620:     return 1;
  621: }
  622: 
  623: # ------------------------------------------- Transfer profile into environment
  624: my $env_loaded;
  625: sub transfer_profile_to_env {
  626:     my ($lonidsdir,$handle,$force_transfer) = @_;
  627:     if (!$force_transfer && $env_loaded) { return; } 
  628: 
  629:     if (!defined($lonidsdir)) {
  630: 	$lonidsdir = $perlvar{'lonIDsDir'};
  631:     }
  632:     if (!defined($handle)) {
  633:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
  634:     }
  635: 
  636:     my $convert;
  637:     {
  638:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  639: 	if (!$opened) {
  640: 	    return;
  641: 	}
  642: 	flock($idf,LOCK_SH);
  643: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  644: 		&GDBM_READER(),0640)) {
  645: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
  646: 	    untie(%disk_env);
  647: 	} else {
  648: 	    $convert = 1;
  649: 	}
  650:     }
  651:     if ($convert) {
  652: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
  653: 	    &logthis("Failed to load session, or convert session.");
  654: 	}
  655:     }
  656: 
  657:     my %remove;
  658:     while ( my $envname = each(%env) ) {
  659:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
  660:             if ($time < time-300) {
  661:                 $remove{$key}++;
  662:             }
  663:         }
  664:     }
  665: 
  666:     $env{'user.environment'} = "$lonidsdir/$handle.id";
  667:     $env_loaded=1;
  668:     foreach my $expired_key (keys(%remove)) {
  669:         &delenv($expired_key);
  670:     }
  671: }
  672: 
  673: # ---------------------------------------------------- Check for valid session 
  674: sub check_for_valid_session {
  675:     my ($r,$name,$userhashref,$domref) = @_;
  676:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  677:     my ($lonidsdir,$linkname,$pubname,$secure,$lonid);
  678:     if ($name eq 'lonDAV') {
  679:         $lonidsdir=$r->dir_config('lonDAVsessDir');
  680:     } else {
  681:         $lonidsdir=$r->dir_config('lonIDsDir');
  682:         if ($name eq '') {
  683:             $name = 'lonID';
  684:         }
  685:     }
  686:     if ($name eq 'lonID') {
  687:         $secure = 'lonSID';
  688:         $linkname = 'lonLinkID';
  689:         $pubname = 'lonPubID';
  690:         if (exists($cookies{$secure})) {
  691:             $lonid=$cookies{$secure};
  692:         } elsif (exists($cookies{$name})) {
  693:             $lonid=$cookies{$name};
  694:         } elsif ((exists($cookies{$linkname})) && ($ENV{'SERVER_PORT'} != 443)) {
  695:             $lonid=$cookies{$linkname};
  696:         } elsif (exists($cookies{$pubname})) {
  697:             $lonid=$cookies{$pubname};
  698:         }
  699:     } else {
  700:         $lonid=$cookies{$name};
  701:     }
  702:     return undef if (!$lonid);
  703: 
  704:     my $handle=&LONCAPA::clean_handle($lonid->value);
  705:     if (-l "$lonidsdir/$handle.id") {
  706:         my $link = readlink("$lonidsdir/$handle.id");
  707:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  708:             $handle = $1;
  709:         }
  710:     }
  711:     if (!-e "$lonidsdir/$handle.id") {
  712:         if ((ref($domref)) && ($name eq 'lonID') && 
  713:             ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  714:             my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  715:             if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  716:                 $$domref = $possudom;
  717:             }
  718:         }
  719:         return undef;
  720:     }
  721: 
  722:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
  723:     return undef if (!$opened);
  724: 
  725:     flock($idf,LOCK_SH);
  726:     my %disk_env;
  727:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  728: 	    &GDBM_READER(),0640)) {
  729: 	return undef;	
  730:     }
  731: 
  732:     if (!defined($disk_env{'user.name'})
  733: 	|| !defined($disk_env{'user.domain'})) {
  734:         untie(%disk_env);
  735: 	return undef;
  736:     }
  737: 
  738:     if (ref($userhashref) eq 'HASH') {
  739:         $userhashref->{'name'} = $disk_env{'user.name'};
  740:         $userhashref->{'domain'} = $disk_env{'user.domain'};
  741:         $userhashref->{'lti'} = $disk_env{'request.lti.login'};
  742:         if ($userhashref->{'lti'}) {
  743:             $userhashref->{'ltitarget'} = $disk_env{'request.lti.target'};
  744:             $userhashref->{'ltiuri'} = $disk_env{'request.lti.uri'};
  745:         }
  746:     }
  747:     untie(%disk_env);
  748: 
  749:     return $handle;
  750: }
  751: 
  752: sub timed_flock {
  753:     my ($file,$lock_type) = @_;
  754:     my $failed=0;
  755:     eval {
  756: 	local $SIG{__DIE__}='DEFAULT';
  757: 	local $SIG{ALRM}=sub {
  758: 	    $failed=1;
  759: 	    die("failed lock");
  760: 	};
  761: 	alarm(13);
  762: 	flock($file,$lock_type);
  763: 	alarm(0);
  764:     };
  765:     if ($failed) {
  766: 	return undef;
  767:     } else {
  768: 	return 1;
  769:     }
  770: }
  771: 
  772: sub get_sessionfile_vars {
  773:     my ($handle,$lonidsdir,$storearr) = @_;
  774:     my %returnhash;
  775:     unless (ref($storearr) eq 'ARRAY') {
  776:         return %returnhash;
  777:     }
  778:     if (-l "$lonidsdir/$handle.id") {
  779:         my $link = readlink("$lonidsdir/$handle.id");
  780:         if ((-e $link) && ($link =~ m{^\Q$lonidsdir\E/(.+)\.id$})) {
  781:             $handle = $1;
  782:         }
  783:     }
  784:     if ((-e "$lonidsdir/$handle.id") &&
  785:         ($handle =~ /^($match_username)\_\d+\_($match_domain)\_(.+)$/)) {
  786:         my ($possuname,$possudom,$possuhome) = ($1,$2,$3);
  787:         if ((&domain($possudom) ne '') && (&homeserver($possuname,$possudom) eq $possuhome)) {
  788:             if (open(my $idf,'+<',"$lonidsdir/$handle.id")) {
  789:                 flock($idf,LOCK_SH);
  790:                 if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
  791:                         &GDBM_READER(),0640)) {
  792:                     foreach my $item (@{$storearr}) {
  793:                         $returnhash{$item} = $disk_env{$item};
  794:                     }
  795:                     untie(%disk_env);
  796:                 }
  797:             }
  798:         }
  799:     }
  800:     return %returnhash;
  801: }
  802: 
  803: # ---------------------------------------------------------- Append Environment
  804: 
  805: sub appenv {
  806:     my ($newenv,$roles) = @_;
  807:     if (ref($newenv) eq 'HASH') {
  808:         foreach my $key (keys(%{$newenv})) {
  809:             my $refused = 0;
  810: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
  811:                 $refused = 1;
  812:                 if (ref($roles) eq 'ARRAY') {
  813:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
  814:                     if (grep(/^\Q$role\E$/,@{$roles})) {
  815:                         $refused = 0;
  816:                     }
  817:                 }
  818:             }
  819:             if ($refused) {
  820:                 &logthis("<font color=\"blue\">WARNING: ".
  821:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
  822:                          .'</font>');
  823: 	        delete($newenv->{$key});
  824:             } else {
  825:                 $env{$key}=$newenv->{$key};
  826:             }
  827:         }
  828:         my $lonids = $perlvar{'lonIDsDir'};
  829:         if ($env{'user.environment'} =~ m{^\Q$lonids/\E$match_username\_\d+\_$match_domain\_[\w\-.]+\.id$}) {
  830:             my $opened = open(my $env_file,'+<',$env{'user.environment'});
  831:             if ($opened
  832: 	        && &timed_flock($env_file,LOCK_EX)
  833: 	        &&
  834: 	        tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  835: 	            (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  836: 	        while (my ($key,$value) = each(%{$newenv})) {
  837: 	            $disk_env{$key} = $value;
  838: 	        }
  839: 	        untie(%disk_env);
  840:             }
  841:         }
  842:     }
  843:     return 'ok';
  844: }
  845: # ----------------------------------------------------- Delete from Environment
  846: 
  847: sub delenv {
  848:     my ($delthis,$regexp,$roles) = @_;
  849:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
  850:         my $refused = 1;
  851:         if (ref($roles) eq 'ARRAY') {
  852:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
  853:             if (grep(/^\Q$role\E$/,@{$roles})) {
  854:                 $refused = 0;
  855:             }
  856:         }
  857:         if ($refused) {
  858:             &logthis("<font color=\"blue\">WARNING: ".
  859:                      "Attempt to delete from environment ".$delthis);
  860:             return 'error';
  861:         }
  862:     }
  863:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
  864:     if ($opened
  865: 	&& &timed_flock($env_file,LOCK_EX)
  866: 	&&
  867: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
  868: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
  869: 	foreach my $key (keys(%disk_env)) {
  870: 	    if ($regexp) {
  871:                 if ($key=~/^$delthis/) {
  872:                     delete($env{$key});
  873:                     delete($disk_env{$key});
  874:                 } 
  875:             } else {
  876:                 if ($key=~/^\Q$delthis\E/) {
  877: 		    delete($env{$key});
  878: 		    delete($disk_env{$key});
  879: 	        }
  880:             }
  881: 	}
  882: 	untie(%disk_env);
  883:     }
  884:     return 'ok';
  885: }
  886: 
  887: sub get_env_multiple {
  888:     my ($name) = @_;
  889:     my @values;
  890:     if (defined($env{$name})) {
  891:         # exists is it an array
  892:         if (ref($env{$name})) {
  893:             @values=@{ $env{$name} };
  894:         } else {
  895:             $values[0]=$env{$name};
  896:         }
  897:     }
  898:     return(@values);
  899: }
  900: 
  901: # ------------------------------------------------------------------- Locking
  902: 
  903: sub set_lock {
  904:     my ($text)=@_;
  905:     $locknum++;
  906:     my $id=$$.'-'.$locknum;
  907:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
  908:              'session.lock.'.$id => $text});
  909:     return $id;
  910: }
  911: 
  912: sub get_locks {
  913:     my $num=0;
  914:     my %texts=();
  915:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  916:        if ($lock=~/\w/) {
  917:           $num++;
  918:           $texts{$lock}=$env{'session.lock.'.$lock};
  919:        }
  920:    }
  921:    return ($num,%texts);
  922: }
  923: 
  924: sub remove_lock {
  925:     my ($id)=@_;
  926:     my $newlocks='';
  927:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  928:        if (($lock=~/\w/) && ($lock ne $id)) {
  929:           $newlocks.=','.$lock;
  930:        }
  931:     }
  932:     &appenv({'session.locks' => $newlocks});
  933:     &delenv('session.lock.'.$id);
  934: }
  935: 
  936: sub remove_all_locks {
  937:     my $activelocks=$env{'session.locks'};
  938:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
  939:        if ($lock=~/\w/) {
  940:           &remove_lock($lock);
  941:        }
  942:     }
  943: }
  944: 
  945: 
  946: # ------------------------------------------ Find out current server userload
  947: sub userload {
  948:     my $numusers=0;
  949:     {
  950: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
  951: 	my $filename;
  952: 	my $curtime=time;
  953: 	while ($filename=readdir(LONIDS)) {
  954: 	    next if ($filename eq '.' || $filename eq '..');
  955: 	    next if ($filename =~ /publicuser_\d+\.id/);
  956:             next if ($filename =~ /^[a-f0-9]+_linked\.id$/);
  957: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
  958: 	    if ($curtime-$mtime < 1800) { $numusers++; }
  959: 	}
  960: 	closedir(LONIDS);
  961:     }
  962:     my $userloadpercent=0;
  963:     my $maxuserload=$perlvar{'lonUserLoadLim'};
  964:     if ($maxuserload) {
  965: 	$userloadpercent=100*$numusers/$maxuserload;
  966:     }
  967:     $userloadpercent=sprintf("%.2f",$userloadpercent);
  968:     return $userloadpercent;
  969: }
  970: 
  971: # ------------------------------ Find server with least workload from spare.tab
  972: 
  973: sub spareserver {
  974:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
  975:     my $spare_server;
  976:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
  977:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
  978:                                                      :  $userloadpercent;
  979:     my ($uint_dom,$remotesessions);
  980:     if (($udom ne '') && (&domain($udom) ne '')) {
  981:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  982:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  983:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  984:         $remotesessions = $udomdefaults{'remotesessions'};
  985:     }
  986:     my $spareshash = &this_host_spares($udom);
  987:     if (ref($spareshash) eq 'HASH') {
  988:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
  989:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
  990:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
  991:                                              $try_server));
  992: 	        ($spare_server, $lowest_load) =
  993: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
  994:             }
  995:         }
  996: 
  997:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
  998: 
  999:         if (!$found_server) {
 1000:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
 1001: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
 1002:                     next unless (&spare_can_host($udom,$uint_dom,
 1003:                                                  $remotesessions,$try_server));
 1004: 	            ($spare_server, $lowest_load) =
 1005: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
 1006:                 }
 1007: 	    }
 1008:         }
 1009:     }
 1010: 
 1011:     if (!$want_server_name) {
 1012:         if (defined($spare_server)) {
 1013:             my $hostname = &hostname($spare_server);
 1014:             if (defined($hostname)) {
 1015:                 my $protocol = 'http';
 1016:                 if ($protocol{$spare_server} eq 'https') {
 1017:                     $protocol = $protocol{$spare_server};
 1018:                 }
 1019: 	        $spare_server = $protocol.'://'.$hostname;
 1020:             }
 1021:         }
 1022:     }
 1023:     return $spare_server;
 1024: }
 1025: 
 1026: sub compare_server_load {
 1027:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
 1028: 
 1029:     if ($required) {
 1030:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
 1031:         my $remoterev = &get_server_loncaparev(undef,$try_server);
 1032:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 1033:         if (($major eq '' && $minor eq '') ||
 1034:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
 1035:             return ($spare_server,$lowest_load);
 1036:         }
 1037:     }
 1038: 
 1039:     my $loadans     = &reply('load',    $try_server);
 1040:     my $userloadans = &reply('userload',$try_server);
 1041: 
 1042:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
 1043: 	return ($spare_server, $lowest_load); #didn't get a number from the server
 1044:     }
 1045: 
 1046:     my $load;
 1047:     if ($loadans =~ /\d/) {
 1048: 	if ($userloadans =~ /\d/) {
 1049: 	    #both are numbers, pick the bigger one
 1050: 	    $load = ($loadans > $userloadans) ? $loadans 
 1051: 		                              : $userloadans;
 1052: 	} else {
 1053: 	    $load = $loadans;
 1054: 	}
 1055:     } else {
 1056: 	$load = $userloadans;
 1057:     }
 1058: 
 1059:     if (($load =~ /\d/) && ($load < $lowest_load)) {
 1060: 	$spare_server = $try_server;
 1061: 	$lowest_load  = $load;
 1062:     }
 1063:     return ($spare_server,$lowest_load);
 1064: }
 1065: 
 1066: # --------------------------- ask offload servers if user already has a session
 1067: sub find_existing_session {
 1068:     my ($udom,$uname) = @_;
 1069:     my $spareshash = &this_host_spares($udom);
 1070:     if (ref($spareshash) eq 'HASH') {
 1071:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
 1072:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
 1073:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1074:             }
 1075:         }
 1076:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
 1077:             foreach my $try_server (@{ $spareshash->{'default'} }) {
 1078:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
 1079:             }
 1080:         }
 1081:     }
 1082:     return;
 1083: }
 1084: 
 1085: # check if user's browser sent load balancer cookie and server still has session
 1086: # and is not overloaded.
 1087: sub check_for_balancer_cookie {
 1088:     my ($r,$update_mtime) = @_;
 1089:     my ($otherserver,$cookie);
 1090:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1091:     if (exists($cookies{'balanceID'})) {
 1092:         my $balid = $cookies{'balanceID'};
 1093:         $cookie=&LONCAPA::clean_handle($balid->value);
 1094:         my $balancedir=$r->dir_config('lonBalanceDir');
 1095:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1096:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1097:                 my ($possudom,$possuname) = ($1,$2);
 1098:                 my $has_session = 0;
 1099:                 if ((&domain($possudom) ne '') &&
 1100:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1101:                     my $try_server;
 1102:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1103:                     if ($opened) {
 1104:                         flock($idf,LOCK_SH);
 1105:                         while (my $line = <$idf>) {
 1106:                             chomp($line);
 1107:                             if (&hostname($line) ne '') {
 1108:                                 $try_server = $line;
 1109:                                 last;
 1110:                             }
 1111:                         }
 1112:                         close($idf);
 1113:                         if (($try_server) &&
 1114:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1115:                             my $lowest_load = 30000;
 1116:                             ($otherserver,$lowest_load) =
 1117:                                 &compare_server_load($try_server,undef,$lowest_load);
 1118:                             if ($otherserver ne '' && $lowest_load < 100) {
 1119:                                 $has_session = 1;
 1120:                             } else {
 1121:                                 undef($otherserver);
 1122:                             }
 1123:                         }
 1124:                     }
 1125:                 }
 1126:                 if ($has_session) {
 1127:                     if ($update_mtime) {
 1128:                         my $atime = my $mtime = time;
 1129:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1130:                     }
 1131:                 } else {
 1132:                     unlink("$balancedir/$cookie.id");
 1133:                 }
 1134:             }
 1135:         }
 1136:     }
 1137:     return ($otherserver,$cookie);
 1138: }
 1139: 
 1140: sub delbalcookie {
 1141:     my ($cookie,$balancer) =@_;
 1142:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1143:         my ($udom,$uname) = ($1,$2);
 1144:         my $uprimary_id = &domain($udom,'primary');
 1145:         my $uintdom = &internet_dom($uprimary_id);
 1146:         my $intdom = &internet_dom($balancer);
 1147:         my $serverhomedom = &host_domain($balancer);
 1148:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1149:             return &reply("delbalcookie:$cookie",$balancer);
 1150:         }
 1151:     }
 1152: }
 1153: 
 1154: # -------------------------------- ask if server already has a session for user
 1155: sub has_user_session {
 1156:     my ($lonid,$udom,$uname) = @_;
 1157:     my $result = &reply(join(':','userhassession',
 1158: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1159:     return 1 if ($result eq 'ok');
 1160: 
 1161:     return 0;
 1162: }
 1163: 
 1164: # --------- determine least loaded server in a user's domain which allows login
 1165: 
 1166: sub choose_server {
 1167:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1168:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1169:     my %servers = &get_servers($udom);
 1170:     my $lowest_load = 30000;
 1171:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1172:     if ($skiploadbal) {
 1173:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1174:         unless (defined($cached)) {
 1175:             my $cachetime = 60*60*24;
 1176:             my %domconfig =
 1177:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1178:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1179:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1180:                                            $cachetime);
 1181:             }
 1182:         }
 1183:     }
 1184:     foreach my $lonhost (keys(%servers)) {
 1185:         if ($skiploadbal) {
 1186:             if (ref($balancers) eq 'HASH') {
 1187:                 next if (exists($balancers->{$lonhost}));
 1188:             }
 1189:         }
 1190:         my $loginvia;
 1191:         if ($checkloginvia) {
 1192:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1193:             if ($loginvia) {
 1194:                 my ($server,$path) = split(/:/,$loginvia);
 1195:                 ($login_host, $lowest_load) =
 1196:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1197:                 if ($login_host eq $server) {
 1198:                     $portal_path = $path;
 1199:                     $isredirect = 1;
 1200:                 }
 1201:             } else {
 1202:                 ($login_host, $lowest_load) =
 1203:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1204:                 if ($login_host eq $lonhost) {
 1205:                     $portal_path = '';
 1206:                     $isredirect = ''; 
 1207:                 }
 1208:             }
 1209:         } else {
 1210:             ($login_host, $lowest_load) =
 1211:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1212:         }
 1213:     }
 1214:     if ($login_host ne '') {
 1215:         $hostname = &hostname($login_host);
 1216:     }
 1217:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1218: }
 1219: 
 1220: # --------------------------------------------- Try to change a user's password
 1221: 
 1222: sub changepass {
 1223:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1224:     $currentpass = &escape($currentpass);
 1225:     $newpass     = &escape($newpass);
 1226:     my $lonhost = $perlvar{'lonHostID'};
 1227:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1228: 		       $server);
 1229:     if (! $answer) {
 1230: 	&logthis("No reply on password change request to $server ".
 1231: 		 "by $uname in domain $udom.");
 1232:     } elsif ($answer =~ "^ok") {
 1233:         &logthis("$uname in $udom successfully changed their password ".
 1234: 		 "on $server.");
 1235:     } elsif ($answer =~ "^pwchange_failure") {
 1236: 	&logthis("$uname in $udom was unable to change their password ".
 1237: 		 "on $server.  The action was blocked by either lcpasswd ".
 1238: 		 "or pwchange");
 1239:     } elsif ($answer =~ "^non_authorized") {
 1240:         &logthis("$uname in $udom did not get their password correct when ".
 1241: 		 "attempting to change it on $server.");
 1242:     } elsif ($answer =~ "^auth_mode_error") {
 1243:         &logthis("$uname in $udom attempted to change their password despite ".
 1244: 		 "not being locally or internally authenticated on $server.");
 1245:     } elsif ($answer =~ "^unknown_user") {
 1246:         &logthis("$uname in $udom attempted to change their password ".
 1247: 		 "on $server but were unable to because $server is not ".
 1248: 		 "their home server.");
 1249:     } elsif ($answer =~ "^refused") {
 1250: 	&logthis("$server refused to change $uname in $udom password because ".
 1251: 		 "it was sent an unencrypted request to change the password.");
 1252:     } elsif ($answer =~ "invalid_client") {
 1253:         &logthis("$server refused to change $uname in $udom password because ".
 1254:                  "it was a reset by e-mail originating from an invalid server.");
 1255:     }
 1256:     return $answer;
 1257: }
 1258: 
 1259: # ----------------------- Try to determine user's current authentication scheme
 1260: 
 1261: sub queryauthenticate {
 1262:     my ($uname,$udom)=@_;
 1263:     my $uhome=&homeserver($uname,$udom);
 1264:     if (!$uhome) {
 1265: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1266: 	return 'no_host';
 1267:     }
 1268:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1269:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1270: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1271:     }
 1272:     return $answer;
 1273: }
 1274: 
 1275: # --------- Try to authenticate user from domain's lib servers (first this one)
 1276: 
 1277: sub authenticate {
 1278:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1279:     $upass=&escape($upass);
 1280:     $uname= &LONCAPA::clean_username($uname);
 1281:     my $uhome=&homeserver($uname,$udom,1);
 1282:     my $newhome;
 1283:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1284: # Maybe the machine was offline and only re-appeared again recently?
 1285:         &reconlonc();
 1286: # One more
 1287: 	$uhome=&homeserver($uname,$udom,1);
 1288:         if (($uhome eq 'no_host') && $checkdefauth) {
 1289:             if (defined(&domain($udom,'primary'))) {
 1290:                 $newhome=&domain($udom,'primary');
 1291:             }
 1292:             if ($newhome ne '') {
 1293:                 $uhome = $newhome;
 1294:             }
 1295:         }
 1296: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1297: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1298: 	    return 'no_host';
 1299:         }
 1300:     }
 1301:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1302:     if ($answer eq 'authorized') {
 1303:         if ($newhome) {
 1304:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1305:             return 'no_account_on_host'; 
 1306:         } else {
 1307:             &logthis("User $uname at $udom authorized by $uhome");
 1308:             return $uhome;
 1309:         }
 1310:     }
 1311:     if ($answer eq 'non_authorized') {
 1312: 	&logthis("User $uname at $udom rejected by $uhome");
 1313: 	return 'no_host'; 
 1314:     }
 1315:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1316:     return 'no_host';
 1317: }
 1318: 
 1319: sub can_host_session {
 1320:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1321:     my $canhost = 1;
 1322:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1323:     if (ref($remotesessions) eq 'HASH') {
 1324:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1325:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1326:                 $canhost = 0;
 1327:             } else {
 1328:                 $canhost = 1;
 1329:             }
 1330:         }
 1331:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1332:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1333:                 $canhost = 1;
 1334:             } else {
 1335:                 $canhost = 0;
 1336:             }
 1337:         }
 1338:         if ($canhost) {
 1339:             if ($remotesessions->{'version'} ne '') {
 1340:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1341:                 if ($reqmajor ne '' && $reqminor ne '') {
 1342:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1343:                         my $major = $1;
 1344:                         my $minor = $2;
 1345:                         if (($major < $reqmajor ) ||
 1346:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1347:                             $canhost = 0;
 1348:                         }
 1349:                     } else {
 1350:                         $canhost = 0;
 1351:                     }
 1352:                 }
 1353:             }
 1354:         }
 1355:     }
 1356:     if ($canhost) {
 1357:         if (ref($hostedsessions) eq 'HASH') {
 1358:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1359:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1360:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1361:                 if (($uint_dom ne '') && 
 1362:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1363:                     $canhost = 0;
 1364:                 } else {
 1365:                     $canhost = 1;
 1366:                 }
 1367:             }
 1368:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1369:                 if (($uint_dom ne '') && 
 1370:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1371:                     $canhost = 1;
 1372:                 } else {
 1373:                     $canhost = 0;
 1374:                 }
 1375:             }
 1376:         }
 1377:     }
 1378:     return $canhost;
 1379: }
 1380: 
 1381: sub spare_can_host {
 1382:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1383:     my $canhost=1;
 1384:     my $try_server_hostname = &hostname($try_server);
 1385:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1386:     my $serverhomedom = &host_domain($serverhomeID);
 1387:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1388:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1389:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1390:             $canhost = 0;
 1391:         }
 1392:     }
 1393:     if (($canhost) && ($uint_dom)) {
 1394:         my @intdoms;
 1395:         my $internet_names = &get_internet_names($try_server);
 1396:         if (ref($internet_names) eq 'ARRAY') {
 1397:             @intdoms = @{$internet_names};
 1398:         }
 1399:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1400:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1401:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1402:                                          $remotesessions,
 1403:                                          $defdomdefaults{'hostedsessions'});
 1404:         }
 1405:     }
 1406:     return $canhost;
 1407: }
 1408: 
 1409: sub this_host_spares {
 1410:     my ($dom) = @_;
 1411:     my ($dom_in_use,$lonhost_in_use,$result);
 1412:     my @hosts = &current_machine_ids();
 1413:     foreach my $lonhost (@hosts) {
 1414:         if (&host_domain($lonhost) eq $dom) {
 1415:             $dom_in_use = $dom;
 1416:             $lonhost_in_use = $lonhost;
 1417:             last;
 1418:         }
 1419:     }
 1420:     if ($dom_in_use ne '') {
 1421:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1422:     }
 1423:     if (ref($result) ne 'HASH') {
 1424:         $lonhost_in_use = $perlvar{'lonHostID'};
 1425:         $dom_in_use = &host_domain($lonhost_in_use);
 1426:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1427:         if (ref($result) ne 'HASH') {
 1428:             $result = \%spareid;
 1429:         }
 1430:     }
 1431:     return $result;
 1432: }
 1433: 
 1434: sub spares_for_offload  {
 1435:     my ($dom_in_use,$lonhost_in_use) = @_;
 1436:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1437:     if (defined($cached)) {
 1438:         return $result;
 1439:     } else {
 1440:         my $cachetime = 60*60*24;
 1441:         my %domconfig =
 1442:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1443:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1444:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1445:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1446:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1447:                 }
 1448:             }
 1449:         }
 1450:     }
 1451:     return;
 1452: }
 1453: 
 1454: sub get_lonbalancer_config {
 1455:     my ($servers) = @_;
 1456:     my ($currbalancer,$currtargets);
 1457:     if (ref($servers) eq 'HASH') {
 1458:         foreach my $server (keys(%{$servers})) {
 1459:             my %what = (
 1460:                          spareid => 1,
 1461:                          perlvar => 1,
 1462:                        );
 1463:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1464:             if ($result eq 'ok') {
 1465:                 if (ref($returnhash) eq 'HASH') {
 1466:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1467:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1468:                             $currbalancer = $server;
 1469:                             $currtargets = {};
 1470:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1471:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1472:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1473:                                 }
 1474:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1475:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1476:                                 }
 1477:                             }
 1478:                             last;
 1479:                         }
 1480:                     }
 1481:                 }
 1482:             }
 1483:         }
 1484:     }
 1485:     return ($currbalancer,$currtargets);
 1486: }
 1487: 
 1488: sub check_loadbalancing {
 1489:     my ($uname,$udom,$caller) = @_;
 1490:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1491:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1492:     my $lonhost = $perlvar{'lonHostID'};
 1493:     my @hosts = &current_machine_ids();
 1494:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1495:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1496:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1497:     my $serverhomedom = &host_domain($lonhost);
 1498:     my $domneedscache;
 1499:     my $cachetime = 60*60*24;
 1500: 
 1501:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1502:         $dom_in_use = $udom;
 1503:         $homeintdom = 1;
 1504:     } else {
 1505:         $dom_in_use = $serverhomedom;
 1506:     }
 1507:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1508:     unless (defined($cached)) {
 1509:         my %domconfig =
 1510:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1511:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1512:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1513:         } else {
 1514:             $domneedscache = $dom_in_use;
 1515:         }
 1516:     }
 1517:     if (ref($result) eq 'HASH') {
 1518:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1519:             &check_balancer_result($result,@hosts);
 1520:         if ($is_balancer) {
 1521:             if (ref($currrules) eq 'HASH') {
 1522:                 if ($homeintdom) {
 1523:                     if ($uname ne '') {
 1524:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1525:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1526:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1527:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1528:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1529:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1530:                             }
 1531:                         }
 1532:                         if ($rule_in_effect eq '') {
 1533:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1534:                             if ($userenv{'inststatus'} ne '') {
 1535:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1536:                                 my ($othertitle,$usertypes,$types) =
 1537:                                     &Apache::loncommon::sorted_inst_types($udom);
 1538:                                 if (ref($types) eq 'ARRAY') {
 1539:                                     foreach my $type (@{$types}) {
 1540:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1541:                                             if (exists($currrules->{$type})) {
 1542:                                                 $rule_in_effect = $currrules->{$type};
 1543:                                             }
 1544:                                         }
 1545:                                     }
 1546:                                 }
 1547:                             } else {
 1548:                                 if (exists($currrules->{'default'})) {
 1549:                                     $rule_in_effect = $currrules->{'default'};
 1550:                                 }
 1551:                             }
 1552:                         }
 1553:                     } else {
 1554:                         if (exists($currrules->{'default'})) {
 1555:                             $rule_in_effect = $currrules->{'default'};
 1556:                         }
 1557:                     }
 1558:                 } else {
 1559:                     if ($currrules->{'_LC_external'} ne '') {
 1560:                         $rule_in_effect = $currrules->{'_LC_external'};
 1561:                     }
 1562:                 }
 1563:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1564:                                                        $uname,$udom);
 1565:             }
 1566:         }
 1567:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1568:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1569:         unless (defined($cached)) {
 1570:             my %domconfig =
 1571:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1572:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1573:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1574:             } else {
 1575:                 $domneedscache = $serverhomedom;
 1576:             }
 1577:         }
 1578:         if (ref($result) eq 'HASH') {
 1579:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1580:                 &check_balancer_result($result,@hosts);
 1581:             if ($is_balancer) {
 1582:                 if (ref($currrules) eq 'HASH') {
 1583:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1584:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1585:                     }
 1586:                 }
 1587:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1588:                                                        $uname,$udom);
 1589:             }
 1590:         } else {
 1591:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1592:                 $is_balancer = 1;
 1593:                 $offloadto = &this_host_spares($dom_in_use);
 1594:             }
 1595:             unless (defined($cached)) {
 1596:                 $domneedscache = $serverhomedom;
 1597:             }
 1598:         }
 1599:     } else {
 1600:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1601:             $is_balancer = 1;
 1602:             $offloadto = &this_host_spares($dom_in_use);
 1603:         }
 1604:         unless (defined($cached)) {
 1605:             $domneedscache = $serverhomedom;
 1606:         }
 1607:     }
 1608:     if ($domneedscache) {
 1609:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1610:     }
 1611:     if ($is_balancer) {
 1612:         my $lowest_load = 30000;
 1613:         if (ref($offloadto) eq 'HASH') {
 1614:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1615:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1616:                     ($otherserver,$lowest_load) =
 1617:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1618:                 }
 1619:             }
 1620:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1621: 
 1622:             if (!$found_server) {
 1623:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1624:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1625:                         ($otherserver,$lowest_load) =
 1626:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1627:                     }
 1628:                 }
 1629:             }
 1630:         } elsif (ref($offloadto) eq 'ARRAY') {
 1631:             if (@{$offloadto} == 1) {
 1632:                 $otherserver = $offloadto->[0];
 1633:             } elsif (@{$offloadto} > 1) {
 1634:                 foreach my $try_server (@{$offloadto}) {
 1635:                     ($otherserver,$lowest_load) =
 1636:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1637:                 }
 1638:             }
 1639:         }
 1640:         unless ($caller eq 'login') {
 1641:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1642:                 $is_balancer = 0;
 1643:                 if ($uname ne '' && $udom ne '') {
 1644:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1645:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1646:                                  'user.loadbalcheck.time' => time});
 1647:                     }
 1648:                 }
 1649:             }
 1650:         }
 1651:         unless ($homeintdom) {
 1652:             undef($setcookie);
 1653:         }
 1654:     }
 1655:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1656: }
 1657: 
 1658: sub check_balancer_result {
 1659:     my ($result,@hosts) = @_;
 1660:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1661:     if (ref($result) eq 'HASH') {
 1662:         if ($result->{'lonhost'} ne '') {
 1663:             my $currbalancer = $result->{'lonhost'};
 1664:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1665:                 $is_balancer = 1;
 1666:                 $currtargets = $result->{'targets'};
 1667:                 $currrules = $result->{'rules'};
 1668:             }
 1669:             $dom_balancers = $currbalancer;
 1670:         } else {
 1671:             if (keys(%{$result})) {
 1672:                 foreach my $key (keys(%{$result})) {
 1673:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1674:                         (ref($result->{$key}) eq 'HASH')) {
 1675:                         $is_balancer = 1;
 1676:                         $currrules = $result->{$key}{'rules'};
 1677:                         $currtargets = $result->{$key}{'targets'};
 1678:                         $setcookie = $result->{$key}{'cookie'};
 1679:                         last;
 1680:                     }
 1681:                 }
 1682:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1683:             }
 1684:         }
 1685:     }
 1686:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1687: }
 1688: 
 1689: sub get_loadbalancer_targets {
 1690:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1691:     my $offloadto;
 1692:     if ($rule_in_effect eq 'none') {
 1693:         return [$perlvar{'lonHostID'}];
 1694:     } elsif ($rule_in_effect eq '') {
 1695:         $offloadto = $currtargets;
 1696:     } else {
 1697:         if ($rule_in_effect eq 'homeserver') {
 1698:             my $homeserver = &homeserver($uname,$udom);
 1699:             if ($homeserver ne 'no_host') {
 1700:                 $offloadto = [$homeserver];
 1701:             }
 1702:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1703:             my %domconfig =
 1704:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1705:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1706:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1707:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1708:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1709:                     }
 1710:                 }
 1711:             } else {
 1712:                 my %servers = &internet_dom_servers($udom);
 1713:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1714:                 if (&hostname($remotebalancer) ne '') {
 1715:                     $offloadto = [$remotebalancer];
 1716:                 }
 1717:             }
 1718:         } elsif (&hostname($rule_in_effect) ne '') {
 1719:             $offloadto = [$rule_in_effect];
 1720:         }
 1721:     }
 1722:     return $offloadto;
 1723: }
 1724: 
 1725: sub internet_dom_servers {
 1726:     my ($dom) = @_;
 1727:     my (%uniqservers,%servers);
 1728:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1729:     my @machinedoms = &machine_domains($primaryserver);
 1730:     foreach my $mdom (@machinedoms) {
 1731:         my %currservers = %servers;
 1732:         my %server = &get_servers($mdom);
 1733:         %servers = (%currservers,%server);
 1734:     }
 1735:     my %by_hostname;
 1736:     foreach my $id (keys(%servers)) {
 1737:         push(@{$by_hostname{$servers{$id}}},$id);
 1738:     }
 1739:     foreach my $hostname (sort(keys(%by_hostname))) {
 1740:         if (@{$by_hostname{$hostname}} > 1) {
 1741:             my $match = 0;
 1742:             foreach my $id (@{$by_hostname{$hostname}}) {
 1743:                 if (&host_domain($id) eq $dom) {
 1744:                     $uniqservers{$id} = $hostname;
 1745:                     $match = 1;
 1746:                 }
 1747:             }
 1748:             unless ($match) {
 1749:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1750:             }
 1751:         } else {
 1752:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1753:         }
 1754:     }
 1755:     return %uniqservers;
 1756: }
 1757: 
 1758: sub trusted_domains {
 1759:     my ($cmdtype,$calldom) = @_;
 1760:     my ($trusted,$untrusted);
 1761:     if (&domain($calldom) eq '') {
 1762:         return ($trusted,$untrusted);
 1763:     }
 1764:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1765:         return ($trusted,$untrusted);
 1766:     }
 1767:     my $callprimary = &domain($calldom,'primary');
 1768:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1769:     if ($intcalldom eq '') {
 1770:         return ($trusted,$untrusted);
 1771:     }
 1772: 
 1773:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1774:     unless (defined($cached)) {
 1775:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1776:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1777:         $trustconfig = $domconfig{'trust'};
 1778:     }
 1779:     if (ref($trustconfig)) {
 1780:         my (%possexc,%possinc,@allexc,@allinc); 
 1781:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1782:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1783:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1784:             }
 1785:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1786:                 $possinc{$intcalldom} = 1;
 1787:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1788:             }
 1789:         }
 1790:         if (keys(%possexc)) {
 1791:             if (keys(%possinc)) {
 1792:                 foreach my $key (sort(keys(%possexc))) {
 1793:                     next if ($key eq $intcalldom);
 1794:                     unless ($possinc{$key}) {
 1795:                         push(@allexc,$key);
 1796:                     }
 1797:                 }
 1798:             } else {
 1799:                 @allexc = sort(keys(%possexc));
 1800:             }
 1801:         }
 1802:         if (keys(%possinc)) {
 1803:             $possinc{$intcalldom} = 1;
 1804:             @allinc = sort(keys(%possinc));
 1805:         }
 1806:         if ((@allexc > 0) || (@allinc > 0)) {
 1807:             my %doms_by_intdom;
 1808:             my %allintdoms = &all_host_intdom();
 1809:             my %alldoms = &all_host_domain();
 1810:             foreach my $key (%allintdoms) {
 1811:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1812:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1813:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1814:                     }
 1815:                 } else {
 1816:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1817:                 }
 1818:             }
 1819:             foreach my $exc (@allexc) {
 1820:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1821:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1822:                 }
 1823:             }
 1824:             foreach my $inc (@allinc) {
 1825:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1826:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1827:                 }
 1828:             }
 1829:         }
 1830:     }
 1831:     return ($trusted,$untrusted);
 1832: }
 1833: 
 1834: sub will_trust {
 1835:     my ($cmdtype,$domain,$possdom) = @_;
 1836:     return 1 if ($domain eq $possdom);
 1837:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1838:     my $willtrust; 
 1839:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1840:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1841:             $willtrust = 1;
 1842:         }
 1843:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1844:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1845:             $willtrust = 1;
 1846:         }
 1847:     } else {
 1848:         $willtrust = 1;
 1849:     }
 1850:     return $willtrust;
 1851: }
 1852: 
 1853: # ---------------------- Find the homebase for a user from domain's lib servers
 1854: 
 1855: my %homecache;
 1856: sub homeserver {
 1857:     my ($uname,$udom,$ignoreBadCache)=@_;
 1858:     my $index="$uname:$udom";
 1859: 
 1860:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1861: 
 1862:     my %servers = &get_servers($udom,'library');
 1863:     foreach my $tryserver (keys(%servers)) {
 1864:         next if ($ignoreBadCache ne 'true' && 
 1865: 		 exists($badServerCache{$tryserver}));
 1866: 
 1867: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1868: 	if ($answer eq 'found') {
 1869: 	    delete($badServerCache{$tryserver}); 
 1870: 	    return $homecache{$index}=$tryserver;
 1871: 	} elsif ($answer eq 'no_host') {
 1872: 	    $badServerCache{$tryserver}=1;
 1873: 	}
 1874:     }    
 1875:     return 'no_host';
 1876: }
 1877: 
 1878: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1879: 
 1880: sub idget {
 1881:     my ($udom,$idsref,$namespace)=@_;
 1882:     my %returnhash=();
 1883:     my @ids=(); 
 1884:     if (ref($idsref) eq 'ARRAY') {
 1885:         @ids = @{$idsref};
 1886:     } else {
 1887:         return %returnhash; 
 1888:     }
 1889:     if ($namespace eq '') {
 1890:         $namespace = 'ids';
 1891:     }
 1892:     
 1893:     my %servers = &get_servers($udom,'library');
 1894:     foreach my $tryserver (keys(%servers)) {
 1895: 	my $idlist=join('&', map { &escape($_); } @ids);
 1896: 	if ($namespace eq 'ids') {
 1897: 	    $idlist=~tr/A-Z/a-z/;
 1898: 	}
 1899: 	my $reply;
 1900: 	if ($namespace eq 'ids') {
 1901: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1902: 	} else {
 1903: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1904: 	}
 1905: 	my @answer=();
 1906: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1907: 	    @answer=split(/\&/,$reply);
 1908: 	}                    ;
 1909: 	my $i;
 1910: 	for ($i=0;$i<=$#ids;$i++) {
 1911: 	    if ($answer[$i]) {
 1912: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1913: 	    }
 1914: 	}
 1915:     }
 1916:     return %returnhash;
 1917: }
 1918: 
 1919: # ------------------------------------- Find the IDs behind a list of usernames
 1920: 
 1921: sub idrget {
 1922:     my ($udom,@unames)=@_;
 1923:     my %returnhash=();
 1924:     foreach my $uname (@unames) {
 1925:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1926:     }
 1927:     return %returnhash;
 1928: }
 1929: 
 1930: # Store away a list of names and associated student/employee IDs or clicker IDs
 1931: 
 1932: sub idput {
 1933:     my ($udom,$idsref,$uhom,$namespace)=@_;
 1934:     my %servers=();
 1935:     my %ids=();
 1936:     my %byid = ();
 1937:     if (ref($idsref) eq 'HASH') {
 1938:         %ids=%{$idsref};
 1939:     }
 1940:     if ($namespace eq '') {
 1941:         $namespace = 'ids'; 
 1942:     }
 1943:     foreach my $uname (keys(%ids)) {
 1944: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 1945:         if ($uhom eq '') {
 1946:             $uhom=&homeserver($uname,$udom);
 1947:         }
 1948:         if ($uhom ne 'no_host') {
 1949:             my $esc_unam=&escape($uname);
 1950:             if ($namespace eq 'ids') {
 1951:                 my $id=&escape($ids{$uname});
 1952:                 $id=~tr/A-Z/a-z/;
 1953:                 my $esc_unam=&escape($uname);
 1954:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 1955:             } else {
 1956:                 my @currids = split(/,/,$ids{$uname});
 1957:                 foreach my $id (@currids) {
 1958:                     $byid{$uhom}{$id} .= $uname.',';
 1959:                 }
 1960:             }
 1961:         }
 1962:     }
 1963:     if ($namespace eq 'clickers') {
 1964:         foreach my $server (keys(%byid)) {
 1965:             if (ref($byid{$server}) eq 'HASH') {
 1966:                 foreach my $id (keys(%{$byid{$server}})) {
 1967:                     $byid{$server} =~ s/,$//;
 1968:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 1969:                 }
 1970:             }
 1971:         }
 1972:     }
 1973:     foreach my $server (keys(%servers)) {
 1974:         $servers{$server} =~ s/\&$//;
 1975:         if ($namespace eq 'ids') {     
 1976:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 1977:         } else {
 1978:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 1979:         }
 1980:     }
 1981: }
 1982: 
 1983: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 1984: 
 1985: sub iddel {
 1986:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 1987:     my %result=();
 1988:     my %ids=();
 1989:     my %byid = ();
 1990:     if (ref($idshashref) eq 'HASH') {
 1991:         %ids=%{$idshashref};
 1992:     } else {
 1993:         return %result;
 1994:     }
 1995:     if ($namespace eq '') {
 1996:         $namespace = 'ids';
 1997:     }
 1998:     my %servers=();
 1999:     while (my ($id,$unamestr) = each(%ids)) {
 2000:         if ($namespace eq 'ids') {
 2001:             my $uhom = $uhome;
 2002:             if ($uhom eq '') { 
 2003:                 $uhom=&homeserver($unamestr,$udom);
 2004:             }
 2005:             if ($uhom ne 'no_host') {
 2006:                 $servers{$uhom}.='&'.&escape($id);
 2007:             }
 2008:          } else {
 2009:             my @curritems = split(/,/,$ids{$id});
 2010:             foreach my $uname (@curritems) {
 2011:                 my $uhom = $uhome;
 2012:                 if ($uhom eq '') {
 2013:                     $uhom=&homeserver($uname,$udom);
 2014:                 }
 2015:                 if ($uhom ne 'no_host') { 
 2016:                     $byid{$uhom}{$id} .= $uname.',';
 2017:                 }
 2018:             }
 2019:         }
 2020:     }
 2021:     if ($namespace eq 'clickers') {
 2022:         foreach my $server (keys(%byid)) {
 2023:             if (ref($byid{$server}) eq 'HASH') {
 2024:                 foreach my $id (keys(%{$byid{$server}})) {
 2025:                     $byid{$server}{$id} =~ s/,$//;
 2026:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2027:                 }
 2028:             }
 2029:         }
 2030:     }
 2031:     foreach my $server (keys(%servers)) {
 2032:         $servers{$server} =~ s/\&$//;
 2033:         if ($namespace eq 'ids') {
 2034:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2035:         } elsif ($namespace eq 'clickers') {
 2036:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2037:         }
 2038:     }
 2039:     return %result;
 2040: }
 2041: 
 2042: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2043: 
 2044: sub updateclickers {
 2045:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2046:     my %clickers;
 2047:     if (ref($idshashref) eq 'HASH') {
 2048:         %clickers=%{$idshashref};
 2049:     } else {
 2050:         return;
 2051:     }
 2052:     my $items='';
 2053:     foreach my $item (keys(%clickers)) {
 2054:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2055:     }
 2056:     $items=~s/\&$//;
 2057:     my $request = "updateclickers:$udom:$action:$items";
 2058:     if ($critical) {
 2059:         return &critical($request,$uhome);
 2060:     } else {
 2061:         return &reply($request,$uhome);
 2062:     }
 2063: }
 2064: 
 2065: # ------------------------------dump from db file owned by domainconfig user
 2066: sub dump_dom {
 2067:     my ($namespace, $udom, $regexp) = @_;
 2068: 
 2069:     $udom ||= $env{'user.domain'};
 2070: 
 2071:     return () unless $udom;
 2072: 
 2073:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2074: }
 2075: 
 2076: # ------------------------------------------ get items from domain db files   
 2077: 
 2078: sub get_dom {
 2079:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2080:     return if ($udom eq 'public');
 2081:     my $items='';
 2082:     foreach my $item (@$storearr) {
 2083:         $items.=&escape($item).'&';
 2084:     }
 2085:     $items=~s/\&$//;
 2086:     if (!$udom) {
 2087:         $udom=$env{'user.domain'};
 2088:         return if ($udom eq 'public');
 2089:         if (defined(&domain($udom,'primary'))) {
 2090:             $uhome=&domain($udom,'primary');
 2091:         } else {
 2092:             undef($uhome);
 2093:         }
 2094:     } else {
 2095:         if (!$uhome) {
 2096:             if (defined(&domain($udom,'primary'))) {
 2097:                 $uhome=&domain($udom,'primary');
 2098:             }
 2099:         }
 2100:     }
 2101:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2102:         my $rep;
 2103:         if ($namespace =~ /^enc/) {
 2104:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2105:         } else {
 2106:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2107:         }
 2108:         my %returnhash;
 2109:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2110:             return %returnhash;
 2111:         }
 2112:         my @pairs=split(/\&/,$rep);
 2113:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2114:             return @pairs;
 2115:         }
 2116:         my $i=0;
 2117:         foreach my $item (@$storearr) {
 2118:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2119:             $i++;
 2120:         }
 2121:         return %returnhash;
 2122:     } else {
 2123:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2124:     }
 2125: }
 2126: 
 2127: # -------------------------------------------- put items in domain db files 
 2128: 
 2129: sub put_dom {
 2130:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2131:     if (!$udom) {
 2132:         $udom=$env{'user.domain'};
 2133:         if (defined(&domain($udom,'primary'))) {
 2134:             $uhome=&domain($udom,'primary');
 2135:         } else {
 2136:             undef($uhome);
 2137:         }
 2138:     } else {
 2139:         if (!$uhome) {
 2140:             if (defined(&domain($udom,'primary'))) {
 2141:                 $uhome=&domain($udom,'primary');
 2142:             }
 2143:         }
 2144:     } 
 2145:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2146:         my $items='';
 2147:         foreach my $item (keys(%$storehash)) {
 2148:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2149:         }
 2150:         $items=~s/\&$//;
 2151:         if ($namespace =~ /^enc/) {
 2152:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2153:         } else {
 2154:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2155:         }
 2156:     } else {
 2157:         &logthis("put_dom failed - no homeserver and/or domain");
 2158:     }
 2159: }
 2160: 
 2161: # --------------------- newput for items in db file owned by domainconfig user
 2162: sub newput_dom {
 2163:     my ($namespace,$storehash,$udom) = @_;
 2164:     my $result;
 2165:     if (!$udom) {
 2166:         $udom=$env{'user.domain'};
 2167:     }
 2168:     if ($udom) {
 2169:         my $uname = &get_domainconfiguser($udom);
 2170:         $result = &newput($namespace,$storehash,$udom,$uname);
 2171:     }
 2172:     return $result;
 2173: }
 2174: 
 2175: # --------------------- delete for items in db file owned by domainconfig user
 2176: sub del_dom {
 2177:     my ($namespace,$storearr,$udom)=@_;
 2178:     if (ref($storearr) eq 'ARRAY') {
 2179:         if (!$udom) {
 2180:             $udom=$env{'user.domain'};
 2181:         }
 2182:         if ($udom) {
 2183:             my $uname = &get_domainconfiguser($udom); 
 2184:             return &del($namespace,$storearr,$udom,$uname);
 2185:         }
 2186:     }
 2187: }
 2188: 
 2189: # ----------------------------------construct domainconfig user for a domain 
 2190: sub get_domainconfiguser {
 2191:     my ($udom) = @_;
 2192:     return $udom.'-domainconfig';
 2193: }
 2194: 
 2195: sub retrieve_inst_usertypes {
 2196:     my ($udom) = @_;
 2197:     my (%returnhash,@order);
 2198:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2199:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2200:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2201:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2202:     } else {
 2203:         if (defined(&domain($udom,'primary'))) {
 2204:             my $uhome=&domain($udom,'primary');
 2205:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2206:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2207:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2208:                 return (\%returnhash,\@order);
 2209:             }
 2210:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2211:             my @pairs=split(/\&/,$hashitems);
 2212:             foreach my $item (@pairs) {
 2213:                 my ($key,$value)=split(/=/,$item,2);
 2214:                 $key = &unescape($key);
 2215:                 next if ($key =~ /^error: 2 /);
 2216:                 $returnhash{$key}=&thaw_unescape($value);
 2217:             }
 2218:             my @esc_order = split(/\&/,$orderitems);
 2219:             foreach my $item (@esc_order) {
 2220:                 push(@order,&unescape($item));
 2221:             }
 2222:         } else {
 2223:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2224:         }
 2225:         return (\%returnhash,\@order);
 2226:     }
 2227: }
 2228: 
 2229: sub is_domainimage {
 2230:     my ($url) = @_;
 2231:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2232:         if (&domain($1) ne '') {
 2233:             return '1';
 2234:         }
 2235:     }
 2236:     return;
 2237: }
 2238: 
 2239: sub inst_directory_query {
 2240:     my ($srch) = @_;
 2241:     my $udom = $srch->{'srchdomain'};
 2242:     my %results;
 2243:     my $homeserver = &domain($udom,'primary');
 2244:     my $outcome;
 2245:     if ($homeserver ne '') {
 2246:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2247:             if ($srch->{'srchby'} eq 'email') {
 2248:                 my $lcrev = &get_server_loncaparev(undef,$homeserver);
 2249:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2250:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2251:                     (($major == 2) && ($minor < 12))) {
 2252:                     return;
 2253:                 }
 2254:             }
 2255:         }
 2256: 	my $queryid=&reply("querysend:instdirsearch:".
 2257: 			   &escape($srch->{'srchby'}).':'.
 2258: 			   &escape($srch->{'srchterm'}).':'.
 2259: 			   &escape($srch->{'srchtype'}),$homeserver);
 2260: 	my $host=&hostname($homeserver);
 2261: 	if ($queryid !~/^\Q$host\E\_/) {
 2262: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2263: 	    return;
 2264: 	}
 2265: 	my $response = &get_query_reply($queryid);
 2266: 	my $maxtries = 5;
 2267: 	my $tries = 1;
 2268: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2269: 	    $response = &get_query_reply($queryid);
 2270: 	    $tries ++;
 2271: 	}
 2272: 
 2273:         if (!&error($response) && $response ne 'refused') {
 2274:             if ($response eq 'unavailable') {
 2275:                 $outcome = $response;
 2276:             } else {
 2277:                 $outcome = 'ok';
 2278:                 my @matches = split(/\n/,$response);
 2279:                 foreach my $match (@matches) {
 2280:                     my ($key,$value) = split(/=/,$match);
 2281:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2282:                 }
 2283:             }
 2284:         }
 2285:     }
 2286:     return ($outcome,%results);
 2287: }
 2288: 
 2289: sub usersearch {
 2290:     my ($srch) = @_;
 2291:     my $dom = $srch->{'srchdomain'};
 2292:     my %results;
 2293:     my %libserv = &all_library();
 2294:     my $query = 'usersearch';
 2295:     foreach my $tryserver (keys(%libserv)) {
 2296:         if (&host_domain($tryserver) eq $dom) {
 2297:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2298:                 if ($srch->{'srchby'} eq 'email') {
 2299:                     my $lcrev = &get_server_loncaparev(undef,$tryserver);
 2300:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2301:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2302:                              (($major == 2) && ($minor < 12)));
 2303:                 }
 2304:             }
 2305:             my $host=&hostname($tryserver);
 2306:             my $queryid=
 2307:                 &reply("querysend:".&escape($query).':'.
 2308:                        &escape($srch->{'srchby'}).':'.
 2309:                        &escape($srch->{'srchtype'}).':'.
 2310:                        &escape($srch->{'srchterm'}),$tryserver);
 2311:             if ($queryid !~/^\Q$host\E\_/) {
 2312:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2313:                 next;
 2314:             }
 2315:             my $reply = &get_query_reply($queryid);
 2316:             my $maxtries = 1;
 2317:             my $tries = 1;
 2318:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2319:                 $reply = &get_query_reply($queryid);
 2320:                 $tries ++;
 2321:             }
 2322:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2323:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2324:             } else {
 2325:                 my @matches;
 2326:                 if ($reply =~ /\n/) {
 2327:                     @matches = split(/\n/,$reply);
 2328:                 } else {
 2329:                     @matches = split(/\&/,$reply);
 2330:                 }
 2331:                 foreach my $match (@matches) {
 2332:                     my ($uname,$udom,%userhash);
 2333:                     foreach my $entry (split(/:/,$match)) {
 2334:                         my ($key,$value) =
 2335:                             map {&unescape($_);} split(/=/,$entry);
 2336:                         $userhash{$key} = $value;
 2337:                         if ($key eq 'username') {
 2338:                             $uname = $value;
 2339:                         } elsif ($key eq 'domain') {
 2340:                             $udom = $value;
 2341:                         }
 2342:                     }
 2343:                     $results{$uname.':'.$udom} = \%userhash;
 2344:                 }
 2345:             }
 2346:         }
 2347:     }
 2348:     return %results;
 2349: }
 2350: 
 2351: sub get_instuser {
 2352:     my ($udom,$uname,$id) = @_;
 2353:     my $homeserver = &domain($udom,'primary');
 2354:     my ($outcome,%results);
 2355:     if ($homeserver ne '') {
 2356:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2357:                            &escape($id).':'.&escape($udom),$homeserver);
 2358:         my $host=&hostname($homeserver);
 2359:         if ($queryid !~/^\Q$host\E\_/) {
 2360:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2361:             return;
 2362:         }
 2363:         my $response = &get_query_reply($queryid);
 2364:         my $maxtries = 5;
 2365:         my $tries = 1;
 2366:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2367:             $response = &get_query_reply($queryid);
 2368:             $tries ++;
 2369:         }
 2370:         if (!&error($response) && $response ne 'refused') {
 2371:             if ($response eq 'unavailable') {
 2372:                 $outcome = $response;
 2373:             } else {
 2374:                 $outcome = 'ok';
 2375:                 my @matches = split(/\n/,$response);
 2376:                 foreach my $match (@matches) {
 2377:                     my ($key,$value) = split(/=/,$match);
 2378:                     $results{&unescape($key)} = &thaw_unescape($value);
 2379:                 }
 2380:             }
 2381:         }
 2382:     }
 2383:     my %userinfo;
 2384:     if (ref($results{$uname}) eq 'HASH') {
 2385:         %userinfo = %{$results{$uname}};
 2386:     } 
 2387:     return ($outcome,%userinfo);
 2388: }
 2389: 
 2390: sub get_multiple_instusers {
 2391:     my ($udom,$users,$caller) = @_;
 2392:     my ($outcome,$results);
 2393:     if (ref($users) eq 'HASH') {
 2394:         my $count = keys(%{$users}); 
 2395:         my $requested = &freeze_escape($users);
 2396:         my $homeserver = &domain($udom,'primary');
 2397:         if ($homeserver ne '') {
 2398:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2399:             my $host=&hostname($homeserver);
 2400:             if ($queryid !~/^\Q$host\E\_/) {
 2401:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2402:                          ' for host: '.$homeserver.'in domain '.$udom);
 2403:                 return ($outcome,$results);
 2404:             }
 2405:             my $response = &get_query_reply($queryid);
 2406:             my $maxtries = 5;
 2407:             if ($count > 100) {
 2408:                 $maxtries = 1+int($count/20);
 2409:             }
 2410:             my $tries = 1;
 2411:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2412:                 $response = &get_query_reply($queryid);
 2413:                 $tries ++;
 2414:             }
 2415:             if ($response eq '') {
 2416:                 $results = {};
 2417:                 foreach my $key (keys(%{$users})) {
 2418:                     my ($uname,$id);
 2419:                     if ($caller eq 'id') {
 2420:                         $id = $key;
 2421:                     } else {
 2422:                         $uname = $key;
 2423:                     }
 2424:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2425:                     $outcome = $resp;
 2426:                     if ($resp eq 'ok') {
 2427:                         %{$results} = (%{$results}, %info);
 2428:                     } else {
 2429:                         last;
 2430:                     }
 2431:                 }
 2432:             } elsif(!&error($response) && ($response ne 'refused')) {
 2433:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2434:                     $outcome = $response;
 2435:                 } else {
 2436:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2437:                     if ($outcome eq 'ok') {
 2438:                         $results = &thaw_unescape($userdata); 
 2439:                     }
 2440:                 }
 2441:             }
 2442:         }
 2443:     }
 2444:     return ($outcome,$results);
 2445: }
 2446: 
 2447: sub inst_rulecheck {
 2448:     my ($udom,$uname,$id,$item,$rules) = @_;
 2449:     my %returnhash;
 2450:     if ($udom ne '') {
 2451:         if (ref($rules) eq 'ARRAY') {
 2452:             @{$rules} = map {&escape($_);} (@{$rules});
 2453:             my $rulestr = join(':',@{$rules});
 2454:             my $homeserver=&domain($udom,'primary');
 2455:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2456:                 my $response;
 2457:                 if ($item eq 'username') {                
 2458:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2459:                                               ':'.&escape($uname).':'.$rulestr,
 2460:                                               $homeserver));
 2461:                 } elsif ($item eq 'id') {
 2462:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2463:                                               ':'.&escape($id).':'.$rulestr,
 2464:                                               $homeserver));
 2465:                 } elsif ($item eq 'selfcreate') {
 2466:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2467:                                                &escape($udom).':'.&escape($uname).
 2468:                                               ':'.$rulestr,$homeserver));
 2469:                 }
 2470:                 if ($response ne 'refused') {
 2471:                     my @pairs=split(/\&/,$response);
 2472:                     foreach my $item (@pairs) {
 2473:                         my ($key,$value)=split(/=/,$item,2);
 2474:                         $key = &unescape($key);
 2475:                         next if ($key =~ /^error: 2 /);
 2476:                         $returnhash{$key}=&thaw_unescape($value);
 2477:                     }
 2478:                 }
 2479:             }
 2480:         }
 2481:     }
 2482:     return %returnhash;
 2483: }
 2484: 
 2485: sub inst_userrules {
 2486:     my ($udom,$check) = @_;
 2487:     my (%ruleshash,@ruleorder);
 2488:     if ($udom ne '') {
 2489:         my $homeserver=&domain($udom,'primary');
 2490:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2491:             my $response;
 2492:             if ($check eq 'id') {
 2493:                 $response=&reply('instidrules:'.&escape($udom),
 2494:                                  $homeserver);
 2495:             } elsif ($check eq 'email') {
 2496:                 $response=&reply('instemailrules:'.&escape($udom),
 2497:                                  $homeserver);
 2498:             } else {
 2499:                 $response=&reply('instuserrules:'.&escape($udom),
 2500:                                  $homeserver);
 2501:             }
 2502:             if (($response ne 'refused') && ($response ne 'error') && 
 2503:                 ($response ne 'unknown_cmd') && 
 2504:                 ($response ne 'no_such_host')) {
 2505:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2506:                 my @pairs=split(/\&/,$hashitems);
 2507:                 foreach my $item (@pairs) {
 2508:                     my ($key,$value)=split(/=/,$item,2);
 2509:                     $key = &unescape($key);
 2510:                     next if ($key =~ /^error: 2 /);
 2511:                     $ruleshash{$key}=&thaw_unescape($value);
 2512:                 }
 2513:                 my @esc_order = split(/\&/,$orderitems);
 2514:                 foreach my $item (@esc_order) {
 2515:                     push(@ruleorder,&unescape($item));
 2516:                 }
 2517:             }
 2518:         }
 2519:     }
 2520:     return (\%ruleshash,\@ruleorder);
 2521: }
 2522: 
 2523: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2524: 
 2525: sub get_domain_defaults {
 2526:     my ($domain,$ignore_cache) = @_;
 2527:     return if (($domain eq '') || ($domain eq 'public'));
 2528:     my $cachetime = 60*60*24;
 2529:     unless ($ignore_cache) {
 2530:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2531:         if (defined($cached)) {
 2532:             if (ref($result) eq 'HASH') {
 2533:                 return %{$result};
 2534:             }
 2535:         }
 2536:     }
 2537:     my %domdefaults;
 2538:     my %domconfig =
 2539:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2540:                                   'requestcourses','inststatus',
 2541:                                   'coursedefaults','usersessions',
 2542:                                   'requestauthor','selfenrollment',
 2543:                                   'coursecategories','ssl','autoenroll',
 2544:                                   'trust','helpsettings'],$domain);
 2545:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2546:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2547:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2548:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2549:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2550:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2551:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2552:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2553:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2554:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2555:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2556:     } else {
 2557:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2558:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2559:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2560:     }
 2561:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2562:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2563:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2564:         } else {
 2565:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2566:         }
 2567:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2568:         foreach my $item (@usertools) {
 2569:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2570:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2571:             }
 2572:         }
 2573:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2574:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2575:         }
 2576:     }
 2577:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2578:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2579:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2580:         }
 2581:     }
 2582:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2583:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2584:     }
 2585:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2586:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2587:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2588:         }
 2589:     }
 2590:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2591:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2592:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2593:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2594:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2595:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2596:         }
 2597:         foreach my $type (@coursetypes) {
 2598:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2599:                 unless ($type eq 'community') {
 2600:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2601:                 }
 2602:             }
 2603:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2604:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2605:             }
 2606:             if ($domdefaults{'postsubmit'} eq 'on') {
 2607:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2608:                     $domdefaults{$type.'postsubtimeout'} = 
 2609:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2610:                 }
 2611:             }
 2612:         }
 2613:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2614:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2615:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2616:                 if (@clonecodes) {
 2617:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2618:                 }
 2619:             }
 2620:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2621:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2622:         }
 2623:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2624:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2625:         } 
 2626:     }
 2627:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2628:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2629:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2630:         }
 2631:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2632:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2633:         }
 2634:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2635:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2636:         }
 2637:     }
 2638:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2639:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2640:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2641:                             'approval','limit');
 2642:             foreach my $type (@coursetypes) {
 2643:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2644:                     my @mgrdc = ();
 2645:                     foreach my $item (@settings) {
 2646:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2647:                             push(@mgrdc,$item);
 2648:                         }
 2649:                     }
 2650:                     if (@mgrdc) {
 2651:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2652:                     }
 2653:                 }
 2654:             }
 2655:         }
 2656:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2657:             foreach my $type (@coursetypes) {
 2658:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2659:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2660:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2661:                     }
 2662:                 }
 2663:             }
 2664:         }
 2665:     }
 2666:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2667:         $domdefaults{'catauth'} = 'std';
 2668:         $domdefaults{'catunauth'} = 'std';
 2669:         if ($domconfig{'coursecategories'}{'auth'}) { 
 2670:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2671:         }
 2672:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2673:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2674:         }
 2675:     }
 2676:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2677:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2678:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2679:         }
 2680:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2681:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2682:         }
 2683:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2684:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2685:         }
 2686:     }
 2687:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2688:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2689:         foreach my $prefix (@prefixes) {
 2690:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2691:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2692:             }
 2693:         }
 2694:     }
 2695:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2696:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2697:     }
 2698:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2699:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2700:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2701:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2702:         }
 2703:     }
 2704:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2705:     return %domdefaults;
 2706: }
 2707: 
 2708: sub course_portal_url {
 2709:     my ($cnum,$cdom) = @_;
 2710:     my $chome = &homeserver($cnum,$cdom);
 2711:     my $hostname = &hostname($chome);
 2712:     my $protocol = $protocol{$chome};
 2713:     $protocol = 'http' if ($protocol ne 'https');
 2714:     my %domdefaults = &get_domain_defaults($cdom);
 2715:     my $firsturl;
 2716:     if ($domdefaults{'portal_def'}) {
 2717:         $firsturl = $domdefaults{'portal_def'};
 2718:     } else {
 2719:         $firsturl = $protocol.'://'.$hostname;
 2720:     }
 2721:     return $firsturl;
 2722: }
 2723: 
 2724: # --------------------------------------------- Get domain config for passwords
 2725: 
 2726: sub get_passwdconf {
 2727:     my ($dom) = @_;
 2728:     my (%passwdconf,$gotconf,$lookup);
 2729:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2730:     if (defined($cached)) {
 2731:         if (ref($result) eq 'HASH') {
 2732:             %passwdconf = %{$result};
 2733:             $gotconf = 1;
 2734:         }
 2735:     }
 2736:     unless ($gotconf) {
 2737:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2738:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2739:             %passwdconf = %{$domconfig{'passwords'}};
 2740:         }
 2741:         my $cachetime = 24*60*60;
 2742:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2743:     }
 2744:     return %passwdconf;
 2745: }
 2746: 
 2747: # --------------------------------------------------- Assign a key to a student
 2748: 
 2749: sub assign_access_key {
 2750: #
 2751: # a valid key looks like uname:udom#comments
 2752: # comments are being appended
 2753: #
 2754:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2755:     $kdom=
 2756:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2757:     $knum=
 2758:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2759:     $cdom=
 2760:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2761:     $cnum=
 2762:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2763:     $udom=$env{'user.name'} unless (defined($udom));
 2764:     $uname=$env{'user.domain'} unless (defined($uname));
 2765:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2766:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2767:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2768:                                                   # assigned to this person
 2769:                                                   # - this should not happen,
 2770:                                                   # unless something went wrong
 2771:                                                   # the first time around
 2772: # ready to assign
 2773:         $logentry=$1.'; '.$logentry;
 2774:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2775:                                                  $kdom,$knum) eq 'ok') {
 2776: # key now belongs to user
 2777: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2778:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2779:                 &appenv({'environment.'.$envkey => $ckey});
 2780:                 return 'ok';
 2781:             } else {
 2782:                 return 
 2783:   'error: Count not permanently assign key, will need to be re-entered later.';
 2784: 	    }
 2785:         } else {
 2786:             return 'error: Could not assign key, try again later.';
 2787:         }
 2788:     } elsif (!$existing{$ckey}) {
 2789: # the key does not exist
 2790: 	return 'error: The key does not exist';
 2791:     } else {
 2792: # the key is somebody else's
 2793: 	return 'error: The key is already in use';
 2794:     }
 2795: }
 2796: 
 2797: # ------------------------------------------ put an additional comment on a key
 2798: 
 2799: sub comment_access_key {
 2800: #
 2801: # a valid key looks like uname:udom#comments
 2802: # comments are being appended
 2803: #
 2804:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2805:     $cdom=
 2806:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2807:     $cnum=
 2808:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2809:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2810:     if ($existing{$ckey}) {
 2811:         $existing{$ckey}.='; '.$logentry;
 2812: # ready to assign
 2813:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2814:                                                  $cdom,$cnum) eq 'ok') {
 2815: 	    return 'ok';
 2816:         } else {
 2817: 	    return 'error: Count not store comment.';
 2818:         }
 2819:     } else {
 2820: # the key does not exist
 2821: 	return 'error: The key does not exist';
 2822:     }
 2823: }
 2824: 
 2825: # ------------------------------------------------------ Generate a set of keys
 2826: 
 2827: sub generate_access_keys {
 2828:     my ($number,$cdom,$cnum,$logentry)=@_;
 2829:     $cdom=
 2830:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2831:     $cnum=
 2832:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2833:     unless (&allowed('mky',$cdom)) { return 0; }
 2834:     unless (($cdom) && ($cnum)) { return 0; }
 2835:     if ($number>10000) { return 0; }
 2836:     sleep(2); # make sure don't get same seed twice
 2837:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2838:     my $total=0;
 2839:     for (my $i=1;$i<=$number;$i++) {
 2840:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2841:                   sprintf("%lx",int(100000*rand)).'-'.
 2842:                   sprintf("%lx",int(100000*rand));
 2843:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2844:        $newkey=~s/0/h/g; # and also 0 and O
 2845:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2846:        if ($existing{$newkey}) {
 2847:            $i--;
 2848:        } else {
 2849: 	  if (&put('accesskeys',
 2850:               { $newkey => '# generated '.localtime().
 2851:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2852:                            '; '.$logentry },
 2853: 		   $cdom,$cnum) eq 'ok') {
 2854:               $total++;
 2855: 	  }
 2856:        }
 2857:     }
 2858:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 2859:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 2860:     return $total;
 2861: }
 2862: 
 2863: # ------------------------------------------------------- Validate an accesskey
 2864: 
 2865: sub validate_access_key {
 2866:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 2867:     $cdom=
 2868:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2869:     $cnum=
 2870:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2871:     $udom=$env{'user.domain'} unless (defined($udom));
 2872:     $uname=$env{'user.name'} unless (defined($uname));
 2873:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2874:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 2875: }
 2876: 
 2877: # ------------------------------------- Find the section of student in a course
 2878: sub devalidate_getsection_cache {
 2879:     my ($udom,$unam,$courseid)=@_;
 2880:     my $hashid="$udom:$unam:$courseid";
 2881:     &devalidate_cache_new('getsection',$hashid);
 2882: }
 2883: 
 2884: sub courseid_to_courseurl {
 2885:     my ($courseid) = @_;
 2886:     #already url style courseid
 2887:     return $courseid if ($courseid =~ m{^/});
 2888: 
 2889:     if (exists($env{'course.'.$courseid.'.num'})) {
 2890: 	my $cnum = $env{'course.'.$courseid.'.num'};
 2891: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 2892: 	return "/$cdom/$cnum";
 2893:     }
 2894: 
 2895:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 2896:     if (exists($courseinfo{'num'})) {
 2897: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 2898:     }
 2899: 
 2900:     return undef;
 2901: }
 2902: 
 2903: sub getsection {
 2904:     my ($udom,$unam,$courseid)=@_;
 2905:     my $cachetime=1800;
 2906: 
 2907:     my $hashid="$udom:$unam:$courseid";
 2908:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 2909:     if (defined($cached)) { return $result; }
 2910: 
 2911:     my %Pending; 
 2912:     my %Expired;
 2913:     #
 2914:     # Each role can either have not started yet (pending), be active, 
 2915:     #    or have expired.
 2916:     #
 2917:     # If there is an active role, we are done.
 2918:     #
 2919:     # If there is more than one role which has not started yet, 
 2920:     #     choose the one which will start sooner
 2921:     # If there is one role which has not started yet, return it.
 2922:     #
 2923:     # If there is more than one expired role, choose the one which ended last.
 2924:     # If there is a role which has expired, return it.
 2925:     #
 2926:     $courseid = &courseid_to_courseurl($courseid);
 2927:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 2928:     foreach my $key (keys(%roleshash)) {
 2929:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 2930:         my $section=$1;
 2931:         if ($key eq $courseid.'_st') { $section=''; }
 2932:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 2933:         my $now=time;
 2934:         if (defined($end) && $end && ($now > $end)) {
 2935:             $Expired{$end}=$section;
 2936:             next;
 2937:         }
 2938:         if (defined($start) && $start && ($now < $start)) {
 2939:             $Pending{$start}=$section;
 2940:             next;
 2941:         }
 2942:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 2943:     }
 2944:     #
 2945:     # Presumedly there will be few matching roles from the above
 2946:     # loop and the sorting time will be negligible.
 2947:     if (scalar(keys(%Pending))) {
 2948:         my ($time) = sort {$a <=> $b} keys(%Pending);
 2949:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 2950:     } 
 2951:     if (scalar(keys(%Expired))) {
 2952:         my @sorted = sort {$a <=> $b} keys(%Expired);
 2953:         my $time = pop(@sorted);
 2954:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 2955:     }
 2956:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 2957: }
 2958: 
 2959: sub save_cache {
 2960:     &purge_remembered();
 2961:     #&Apache::loncommon::validate_page();
 2962:     undef(%env);
 2963:     undef($env_loaded);
 2964: }
 2965: 
 2966: my $to_remember=-1;
 2967: my %remembered;
 2968: my %accessed;
 2969: my $kicks=0;
 2970: my $hits=0;
 2971: sub make_key {
 2972:     my ($name,$id) = @_;
 2973:     if (length($id) > 65 
 2974: 	&& length(&escape($id)) > 200) {
 2975: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 2976:     }
 2977:     return &escape($name.':'.$id);
 2978: }
 2979: 
 2980: sub devalidate_cache_new {
 2981:     my ($name,$id,$debug) = @_;
 2982:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 2983:     my $remembered_id=$name.':'.$id;
 2984:     $id=&make_key($name,$id);
 2985:     $memcache->delete($id);
 2986:     delete($remembered{$remembered_id});
 2987:     delete($accessed{$remembered_id});
 2988: }
 2989: 
 2990: sub is_cached_new {
 2991:     my ($name,$id,$debug) = @_;
 2992:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 2993:     if (exists($remembered{$remembered_id})) {
 2994: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 2995: 	$accessed{$remembered_id}=[&gettimeofday()];
 2996: 	$hits++;
 2997: 	return ($remembered{$remembered_id},1);
 2998:     }
 2999:     $id=&make_key($name,$id);
 3000:     my $value = $memcache->get($id);
 3001:     if (!(defined($value))) {
 3002: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3003: 	return (undef,undef);
 3004:     }
 3005:     if ($value eq '__undef__') {
 3006: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3007: 	$value=undef;
 3008:     }
 3009:     &make_room($remembered_id,$value,$debug);
 3010:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3011:     return ($value,1);
 3012: }
 3013: 
 3014: sub do_cache_new {
 3015:     my ($name,$id,$value,$time,$debug) = @_;
 3016:     my $remembered_id=$name.':'.$id;
 3017:     $id=&make_key($name,$id);
 3018:     my $setvalue=$value;
 3019:     if (!defined($setvalue)) {
 3020: 	$setvalue='__undef__';
 3021:     }
 3022:     if (!defined($time) ) {
 3023: 	$time=600;
 3024:     }
 3025:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3026:     my $result = $memcache->set($id,$setvalue,$time);
 3027:     if (! $result) {
 3028: 	&logthis("caching of id -> $id  failed");
 3029: 	$memcache->disconnect_all();
 3030:     }
 3031:     # need to make a copy of $value
 3032:     &make_room($remembered_id,$value,$debug);
 3033:     return $value;
 3034: }
 3035: 
 3036: sub make_room {
 3037:     my ($remembered_id,$value,$debug)=@_;
 3038: 
 3039:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3040:                                     : $value;
 3041:     if ($to_remember<0) { return; }
 3042:     $accessed{$remembered_id}=[&gettimeofday()];
 3043:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3044:     my $to_kick;
 3045:     my $max_time=0;
 3046:     foreach my $other (keys(%accessed)) {
 3047: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3048: 	    $to_kick=$other;
 3049: 	    $max_time=&tv_interval($accessed{$other});
 3050: 	}
 3051:     }
 3052:     delete($remembered{$to_kick});
 3053:     delete($accessed{$to_kick});
 3054:     $kicks++;
 3055:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3056:     return;
 3057: }
 3058: 
 3059: sub purge_remembered {
 3060:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3061:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3062:     undef(%remembered);
 3063:     undef(%accessed);
 3064: }
 3065: # ------------------------------------- Read an entry from a user's environment
 3066: 
 3067: sub userenvironment {
 3068:     my ($udom,$unam,@what)=@_;
 3069:     my $items;
 3070:     foreach my $item (@what) {
 3071:         $items.=&escape($item).'&';
 3072:     }
 3073:     $items=~s/\&$//;
 3074:     my %returnhash=();
 3075:     my $uhome = &homeserver($unam,$udom);
 3076:     unless ($uhome eq 'no_host') {
 3077:         my @answer=split(/\&/, 
 3078:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3079:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3080:             return %returnhash;
 3081:         }
 3082:         my $i;
 3083:         for ($i=0;$i<=$#what;$i++) {
 3084: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3085:         }
 3086:     }
 3087:     return %returnhash;
 3088: }
 3089: 
 3090: # ---------------------------------------------------------- Get a studentphoto
 3091: sub studentphoto {
 3092:     my ($udom,$unam,$ext) = @_;
 3093:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3094:     if (defined($env{'request.course.id'})) {
 3095:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3096:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3097:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3098:             } else {
 3099:                 my ($result,$perm_reqd)=
 3100: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3101:                 if ($result eq 'ok') {
 3102:                     if (!($perm_reqd eq 'yes')) {
 3103:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3104:                     }
 3105:                 }
 3106:             }
 3107:         }
 3108:     } else {
 3109:         my ($result,$perm_reqd) = 
 3110: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3111:         if ($result eq 'ok') {
 3112:             if (!($perm_reqd eq 'yes')) {
 3113:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3114:             }
 3115:         }
 3116:     }
 3117:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3118: }
 3119: 
 3120: sub retrievestudentphoto {
 3121:     my ($udom,$unam,$ext,$type) = @_;
 3122:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3123:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3124:     if ($ret eq 'ok') {
 3125:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3126:         if ($type eq 'thumbnail') {
 3127:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3128:         }
 3129:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3130:         return $tokenurl;
 3131:     } else {
 3132:         if ($type eq 'thumbnail') {
 3133:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3134:         } else { 
 3135:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3136:         }
 3137:     }
 3138: }
 3139: 
 3140: # -------------------------------------------------------------------- New chat
 3141: 
 3142: sub chatsend {
 3143:     my ($newentry,$anon,$group)=@_;
 3144:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3145:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3146:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3147:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3148: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3149: 		   &escape($newentry)).':'.$group,$chome);
 3150: }
 3151: 
 3152: # ------------------------------------------ Find current version of a resource
 3153: 
 3154: sub getversion {
 3155:     my $fname=&clutter(shift);
 3156:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3157:     return &currentversion(&filelocation('',$fname));
 3158: }
 3159: 
 3160: sub currentversion {
 3161:     my $fname=shift;
 3162:     my $author=$fname;
 3163:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3164:     my ($udom,$uname)=split(/\//,$author);
 3165:     my $home=&homeserver($uname,$udom);
 3166:     if ($home eq 'no_host') { 
 3167:         return -1; 
 3168:     }
 3169:     my $answer=&reply("currentversion:$fname",$home);
 3170:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3171: 	return -1;
 3172:     }
 3173:     return $answer;
 3174: }
 3175: 
 3176: #
 3177: # Return special version number of resource if set by override, empty otherwise
 3178: #
 3179: sub usedversion {
 3180:     my $fname=shift;
 3181:     unless ($fname) { $fname=$env{'request.uri'}; }
 3182:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3183:     if ($urlversion) { return $urlversion; }
 3184:     return '';
 3185: }
 3186: 
 3187: # ----------------------------- Subscribe to a resource, return URL if possible
 3188: 
 3189: sub subscribe {
 3190:     my $fname=shift;
 3191:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3192:     $fname=~s/[\n\r]//g;
 3193:     my $author=$fname;
 3194:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3195:     my ($udom,$uname)=split(/\//,$author);
 3196:     my $home=homeserver($uname,$udom);
 3197:     if ($home eq 'no_host') {
 3198:         return 'not_found';
 3199:     }
 3200:     my $answer=reply("sub:$fname",$home);
 3201:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3202: 	$answer.=' by '.$home;
 3203:     }
 3204:     return $answer;
 3205: }
 3206:     
 3207: # -------------------------------------------------------------- Replicate file
 3208: 
 3209: sub repcopy {
 3210:     my $filename=shift;
 3211:     $filename=~s/\/+/\//g;
 3212:     my $londocroot = $perlvar{'lonDocRoot'};
 3213:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3214:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3215:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3216: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3217: 	return &repcopy_userfile($filename);
 3218:     }
 3219:     $filename=~s/[\n\r]//g;
 3220:     my $transname="$filename.in.transfer";
 3221: # FIXME: this should flock
 3222:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3223:     my $remoteurl=subscribe($filename);
 3224:     if ($remoteurl =~ /^con_lost by/) {
 3225: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3226:            return 'unavailable';
 3227:     } elsif ($remoteurl eq 'not_found') {
 3228: 	   #&logthis("Subscribe returned not_found: $filename");
 3229: 	   return 'not_found';
 3230:     } elsif ($remoteurl =~ /^rejected by/) {
 3231: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3232:            return 'forbidden';
 3233:     } elsif ($remoteurl eq 'directory') {
 3234:            return 'ok';
 3235:     } else {
 3236:         my $author=$filename;
 3237:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3238:         my ($udom,$uname)=split(/\//,$author);
 3239:         my $home=homeserver($uname,$udom);
 3240:         unless ($home eq $perlvar{'lonHostID'}) {
 3241:            my @parts=split(/\//,$filename);
 3242:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3243:            if ($path ne "$londocroot/res") {
 3244:                &logthis("Malconfiguration for replication: $filename");
 3245: 	       return 'bad_request';
 3246:            }
 3247:            my $count;
 3248:            for ($count=5;$count<$#parts;$count++) {
 3249:                $path.="/$parts[$count]";
 3250:                if ((-e $path)!=1) {
 3251: 		   mkdir($path,0777);
 3252:                }
 3253:            }
 3254:            my $request=new HTTP::Request('GET',"$remoteurl");
 3255:            my $response;
 3256:            if ($remoteurl =~ m{/raw/}) {
 3257:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3258:            } else {
 3259:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3260:            }
 3261:            if ($response->is_error()) {
 3262: 	       unlink($transname);
 3263:                my $message=$response->status_line;
 3264:                &logthis("<font color=\"blue\">WARNING:"
 3265:                        ." LWP get: $message: $filename</font>");
 3266:                return 'unavailable';
 3267:            } else {
 3268: 	       if ($remoteurl!~/\.meta$/) {
 3269:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3270:                   my $mresponse;
 3271:                   if ($remoteurl =~ m{/raw/}) {
 3272:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3273:                   } else {
 3274:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3275:                   }
 3276:                   if ($mresponse->is_error()) {
 3277: 		      unlink($filename.'.meta');
 3278:                       &logthis(
 3279:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3280:                   }
 3281: 	       }
 3282:                rename($transname,$filename);
 3283:                return 'ok';
 3284:            }
 3285:        }
 3286:     }
 3287: }
 3288: 
 3289: # ------------------------------------------------ Get server side include body
 3290: sub ssi_body {
 3291:     my ($filelink,%form)=@_;
 3292:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3293:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3294:     }
 3295:     my $output='';
 3296:     my $response;
 3297:     if ($filelink=~/^https?\:/) {
 3298:        ($output,$response)=&externalssi($filelink);
 3299:     } else {
 3300:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3301:        $filelink .= 'inhibitmenu=yes';
 3302:        ($output,$response)=&ssi($filelink,%form);
 3303:     }
 3304:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3305:     $output=~s/^.*?\<body[^\>]*\>//si;
 3306:     $output=~s/\<\/body\s*\>.*?$//si;
 3307:     if (wantarray) {
 3308:         return ($output, $response);
 3309:     } else {
 3310:         return $output;
 3311:     }
 3312: }
 3313: 
 3314: # --------------------------------------------------------- Server Side Include
 3315: 
 3316: sub absolute_url {
 3317:     my ($host_name) = @_;
 3318:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3319:     if ($host_name eq '') {
 3320: 	$host_name = $ENV{'SERVER_NAME'};
 3321:     }
 3322:     return $protocol.$host_name;
 3323: }
 3324: 
 3325: #
 3326: #   Server side include.
 3327: # Parameters:
 3328: #  fn     Possibly encrypted resource name/id.
 3329: #  form   Hash that describes how the rendering should be done
 3330: #         and other things.
 3331: # Returns:
 3332: #   Scalar context: The content of the response.
 3333: #   Array context:  2 element list of the content and the full response object.
 3334: #     
 3335: sub ssi {
 3336: 
 3337:     my ($fn,%form)=@_;
 3338:     my $request;
 3339: 
 3340:     $form{'no_update_last_known'}=1;
 3341:     &Apache::lonenc::check_encrypt(\$fn);
 3342:     if (%form) {
 3343:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 3344:       $request->content(join('&',map { 
 3345:             my $name = escape($_);
 3346:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3347:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3348:             : &escape($form{$_}) );    
 3349:         } keys(%form)));
 3350:     } else {
 3351:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 3352:     }
 3353: 
 3354:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3355:     my $lonhost = $perlvar{'lonHostID'};
 3356:     my $islocal;
 3357:     if (($env{'request.course.id'}) &&
 3358:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3359:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3360:         ($form{'grade_symb'} ne '') &&
 3361:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3362:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3363:         $islocal = 1;
 3364:     }
 3365:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3366:                                                 '','','',$islocal);
 3367: 
 3368:     if (wantarray) {
 3369: 	return ($response->content, $response);
 3370:     } else {
 3371: 	return $response->content;
 3372:     }
 3373: }
 3374: 
 3375: sub externalssi {
 3376:     my ($url)=@_;
 3377:     my $request=new HTTP::Request('GET',$url);
 3378:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3379:     if (wantarray) {
 3380:         return ($response->content, $response);
 3381:     } else {
 3382:         return $response->content;
 3383:     }
 3384: }
 3385: 
 3386: 
 3387: # If the local copy of a replicated resource is outdated, trigger a  
 3388: # connection from the homeserver to flush the delayed queue. If no update 
 3389: # happens, remove local copies of outdated resource (and corresponding
 3390: # metadata file).
 3391: 
 3392: sub remove_stale_resfile {
 3393:     my ($url) = @_;
 3394:     my $removed;
 3395:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3396:         my $audom = $1;
 3397:         my $auname = $2;
 3398:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3399:             my $homeserver = &homeserver($auname,$audom);
 3400:             unless (($homeserver eq 'no_host') ||
 3401:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3402:                 my $fname = &filelocation('',$url);
 3403:                 if (-e $fname) {
 3404:                     my $hostname = &hostname($homeserver);
 3405:                     if ($hostname) {
 3406:                         my $protocol = $protocol{$homeserver};
 3407:                         $protocol = 'http' if ($protocol ne 'https');
 3408:                         my $uri = &declutter($url);
 3409:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3410:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3411:                         if ($response->is_success()) {
 3412:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3413:                             my $locmodtime = (stat($fname))[9];
 3414:                             if ($locmodtime < $remmodtime) {
 3415:                                 my $stale;
 3416:                                 my $answer = &reply('pong',$homeserver);
 3417:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3418:                                     sleep(0.2);
 3419:                                     $locmodtime = (stat($fname))[9];
 3420:                                     if ($locmodtime < $remmodtime) {
 3421:                                         my $posstransfer = $fname.'.in.transfer';
 3422:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3423:                                             $removed = 1;
 3424:                                         } else {
 3425:                                             $stale = 1;
 3426:                                         }
 3427:                                     } else {
 3428:                                         $removed = 1;
 3429:                                     }
 3430:                                 } else {
 3431:                                     $stale = 1;
 3432:                                 }
 3433:                                 if ($stale) {
 3434:                                     unlink($fname);
 3435:                                     if ($uri!~/\.meta$/) {
 3436:                                         unlink($fname.'.meta');
 3437:                                     }
 3438:                                     &reply("unsub:$fname",$homeserver);
 3439:                                     $removed = 1;
 3440:                                 }
 3441:                             }
 3442:                         }
 3443:                     }
 3444:                 }
 3445:             }
 3446:         }
 3447:     }
 3448:     return $removed;
 3449: }
 3450: 
 3451: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3452: 
 3453: sub allowuploaded {
 3454:     my ($srcurl,$url)=@_;
 3455:     $url=&clutter(&declutter($url));
 3456:     my $dir=$url;
 3457:     $dir=~s/\/[^\/]+$//;
 3458:     my %httpref=();
 3459:     my $httpurl=&hreflocation('',$url);
 3460:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3461:     &Apache::lonnet::appenv(\%httpref);
 3462: }
 3463: 
 3464: #
 3465: # Determine if the current user should be able to edit a particular resource,
 3466: # when viewing in course context.
 3467: # (a) When viewing resource used to determine if "Edit" item is included in 
 3468: #     Functions.
 3469: # (b) When displaying folder contents in course editor, used to determine if
 3470: #     "Edit" link will be displayed alongside resource.
 3471: #
 3472: #  input: six args -- filename (decluttered), course number, course domain,
 3473: #                   url, symb (if registered) and group (if this is a group
 3474: #                   item -- e.g., bulletin board, group page etc.).
 3475: #  output: array of five scalars -- 
 3476: #          $cfile -- url for file editing if editable on current server
 3477: #          $home -- homeserver of resource (i.e., for author if published,
 3478: #                                           or course if uploaded.).
 3479: #          $switchserver --  1 if server switch will be needed.
 3480: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3481: #          $forceview -- 1 if icon/link should be to go to view mode
 3482: #
 3483: 
 3484: sub can_edit_resource {
 3485:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3486:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3487: #
 3488: # For aboutme pages user can only edit his/her own.
 3489: #
 3490:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3491:         my ($sdom,$sname) = ($1,$2);
 3492:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3493:             $home = $env{'user.home'};
 3494:             $cfile = $resurl;
 3495:             if ($env{'form.forceedit'}) {
 3496:                 $forceview = 1;
 3497:             } else {
 3498:                 $forceedit = 1;
 3499:             }
 3500:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3501:         } else {
 3502:             return;
 3503:         }
 3504:     }
 3505: 
 3506:     if ($env{'request.course.id'}) {
 3507:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3508:         if ($group ne '') {
 3509: # if this is a group homepage or group bulletin board, check group privs
 3510:             my $allowed = 0;
 3511:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3512:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3513:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3514:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3515:                     $allowed = 1;
 3516:                 }
 3517:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3518:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3519:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3520:                     $allowed = 1;
 3521:                 }
 3522:             }
 3523:             if ($allowed) {
 3524:                 $home=&homeserver($cnum,$cdom);
 3525:                 if ($env{'form.forceedit'}) {
 3526:                     $forceview = 1;
 3527:                 } else {
 3528:                     $forceedit = 1;
 3529:                 }
 3530:                 $cfile = $resurl;
 3531:             } else {
 3532:                 return;
 3533:             }
 3534:         } else {
 3535:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3536:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3537:                     return;
 3538:                 }
 3539:             } elsif (!$crsedit) {
 3540: #
 3541: # No edit allowed where CC has switched to student role.
 3542: #
 3543:                 return;
 3544:             }
 3545:         }
 3546:     }
 3547: 
 3548:     if ($file ne '') {
 3549:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3550:             if (&is_course_upload($file,$cnum,$cdom)) {
 3551:                 $uploaded = 1;
 3552:                 $incourse = 1;
 3553:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3554:                     $cfile = &hreflocation('',$file);
 3555:                     if ($env{'form.forceedit'}) {
 3556:                         $forceview = 1;
 3557:                     } else {
 3558:                         $forceedit = 1;
 3559:                     }
 3560:                 }
 3561:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3562:                 $incourse = 1;
 3563:                 if ($env{'form.forceedit'}) {
 3564:                     $forceview = 1;
 3565:                 } else {
 3566:                     $forceedit = 1;
 3567:                 }
 3568:                 $cfile = $resurl;
 3569:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3570:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3571:                     $incourse = 1;
 3572:                     if ($env{'form.forceedit'}) {
 3573:                         $forceview = 1;
 3574:                     } else {
 3575:                         $forceedit = 1;
 3576:                     }
 3577:                     $cfile = $resurl;
 3578:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3579:                     $incourse = 1;
 3580:                     $cfile = $resurl.'/smpedit';
 3581:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3582:                     $incourse = 1;
 3583:                     if ($env{'form.forceedit'}) {
 3584:                         $forceview = 1;
 3585:                     } else {
 3586:                         $forceedit = 1;
 3587:                     }
 3588:                     $cfile = $resurl;
 3589:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3590:                     $incourse = 1;
 3591:                     if ($env{'form.forceedit'}) {
 3592:                         $forceview = 1;
 3593:                     } else {
 3594:                         $forceedit = 1;
 3595:                     }
 3596:                     $cfile = $resurl;
 3597:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3598:                     $incourse = 1;
 3599:                     if ($env{'form.forceedit'}) {
 3600:                         $forceview = 1;
 3601:                     } else {
 3602:                         $forceedit = 1;
 3603:                     }
 3604:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3605:                 }
 3606:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3607:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3608:                 if (&is_on_map($template)) { 
 3609:                     $incourse = 1;
 3610:                     $forceview = 1;
 3611:                     $cfile = $template;
 3612:                 }
 3613:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3614:                     $incourse = 1;
 3615:                     if ($env{'form.forceedit'}) {
 3616:                         $forceview = 1;
 3617:                     } else {
 3618:                         $forceedit = 1;
 3619:                     }
 3620:                     $cfile = $resurl;
 3621:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3622:                 $incourse = 1;
 3623:                 if ($env{'form.forceedit'}) {
 3624:                     $forceview = 1;
 3625:                 } else {
 3626:                     $forceedit = 1;
 3627:                 }
 3628:                 $cfile = $resurl;
 3629:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3630:                 $incourse = 1;
 3631:                 $forceview = 1;
 3632:                 if ($symb) {
 3633:                     my ($map,$id,$res)=&decode_symb($symb);
 3634:                     $env{'request.symb'} = $symb;
 3635:                     $cfile = &clutter($res);
 3636:                 } else {
 3637:                     $cfile = $env{'form.suppurl'};
 3638:                     my $escfile = &unescape($cfile);
 3639:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3640:                         $cfile = '/adm/wrapper'.$escfile;
 3641:                     } else {
 3642:                         $escfile =~ s{^http://}{};
 3643:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3644:                     }
 3645:                 }
 3646:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3647:                 if ($env{'form.forceedit'}) {
 3648:                     $forceview = 1;
 3649:                 } else {
 3650:                     $forceedit = 1;
 3651:                 }
 3652:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3653:             }
 3654:         }
 3655:         if ($uploaded || $incourse) {
 3656:             $home=&homeserver($cnum,$cdom);
 3657:         } elsif ($file !~ m{/$}) {
 3658:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3659:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3660:             # Check that the user has permission to edit this resource
 3661:             my $setpriv = 1;
 3662:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3663:             if (defined($cfudom)) {
 3664:                 $home=&homeserver($cfuname,$cfudom);
 3665:                 $cfile=$file;
 3666:             }
 3667:         }
 3668:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3669:             (($home ne '') && ($home ne 'no_host'))) {
 3670:             my @ids=&current_machine_ids();
 3671:             unless (grep(/^\Q$home\E$/,@ids)) {
 3672:                 $switchserver=1;
 3673:             }
 3674:         }
 3675:     }
 3676:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3677: }
 3678: 
 3679: sub is_course_upload {
 3680:     my ($file,$cnum,$cdom) = @_;
 3681:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3682:     $uploadpath =~ s{^\/}{};
 3683:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3684:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3685:         return 1;
 3686:     }
 3687:     return;
 3688: }
 3689: 
 3690: sub in_course {
 3691:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3692:     if ($hideprivileged) {
 3693:         my $skipuser;
 3694:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3695:         my @possdoms = ($cdom);  
 3696:         if ($coursehash{'checkforpriv'}) { 
 3697:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3698:         }
 3699:         if (&privileged($uname,$udom,\@possdoms)) {
 3700:             $skipuser = 1;
 3701:             if ($coursehash{'nothideprivileged'}) {
 3702:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3703:                     my $user;
 3704:                     if ($item =~ /:/) {
 3705:                         $user = $item;
 3706:                     } else {
 3707:                         $user = join(':',split(/[\@]/,$item));
 3708:                     }
 3709:                     if ($user eq $uname.':'.$udom) {
 3710:                         undef($skipuser);
 3711:                         last;
 3712:                     }
 3713:                 }
 3714:             }
 3715:             if ($skipuser) {
 3716:                 return 0;
 3717:             }
 3718:         }
 3719:     }
 3720:     $type ||= 'any';
 3721:     if (!defined($cdom) || !defined($cnum)) {
 3722:         my $cid  = $env{'request.course.id'};
 3723:         $cdom = $env{'course.'.$cid.'.domain'};
 3724:         $cnum = $env{'course.'.$cid.'.num'};
 3725:     }
 3726:     my $typesref;
 3727:     if (($type eq 'any') || ($type eq 'all')) {
 3728:         $typesref = ['active','previous','future'];
 3729:     } elsif ($type eq 'previous' || $type eq 'future') {
 3730:         $typesref = [$type];
 3731:     }
 3732:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3733:                               $typesref,undef,[$cdom]);
 3734:     my ($tmp) = keys(%roles);
 3735:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3736:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3737:     if (@course_roles > 0) {
 3738:         return 1;
 3739:     }
 3740:     return 0;
 3741: }
 3742: 
 3743: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3744: # input: action, courseID, current domain, intended
 3745: #        path to file, source of file, instruction to parse file for objects,
 3746: #        ref to hash for embedded objects,
 3747: #        ref to hash for codebase of java objects.
 3748: #        reference to scalar to accommodate mime type determined
 3749: #          from File::MMagic if $parser = parse.
 3750: #
 3751: # output: url to file (if action was uploaddoc), 
 3752: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3753: #
 3754: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3755: # course.
 3756: #
 3757: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3758: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3759: #          course's home server.
 3760: #
 3761: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3762: #          be copied from $source (current location) to 
 3763: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3764: #         and will then be copied to
 3765: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3766: #         course's home server.
 3767: #
 3768: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3769: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3770: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3771: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3772: #         in course's home server.
 3773: #
 3774: 
 3775: sub process_coursefile {
 3776:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3777:         $mimetype)=@_;
 3778:     my $fetchresult;
 3779:     my $home=&homeserver($docuname,$docudom);
 3780:     if ($action eq 'propagate') {
 3781:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3782: 			     $home);
 3783:     } else {
 3784:         my $fpath = '';
 3785:         my $fname = $file;
 3786:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3787:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3788:         my $filepath = &build_filepath($fpath);
 3789:         if ($action eq 'copy') {
 3790:             if ($source eq '') {
 3791:                 $fetchresult = 'no source file';
 3792:                 return $fetchresult;
 3793:             } else {
 3794:                 my $destination = $filepath.'/'.$fname;
 3795:                 rename($source,$destination);
 3796:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3797:                                  $home);
 3798:             }
 3799:         } elsif ($action eq 'uploaddoc') {
 3800:             open(my $fh,'>',$filepath.'/'.$fname);
 3801:             print $fh $env{'form.'.$source};
 3802:             close($fh);
 3803:             if ($parser eq 'parse') {
 3804:                 my $mm = new File::MMagic;
 3805:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 3806:                 if ($type eq 'text/html') {
 3807:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 3808:                     unless ($parse_result eq 'ok') {
 3809:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 3810:                     }
 3811:                 }
 3812:                 if (ref($mimetype)) {
 3813:                     $$mimetype = $type;
 3814:                 } 
 3815:             }
 3816:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3817:                                  $home);
 3818:             if ($fetchresult eq 'ok') {
 3819:                 return '/uploaded/'.$fpath.'/'.$fname;
 3820:             } else {
 3821:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3822:                         ' to host '.$home.': '.$fetchresult);
 3823:                 return '/adm/notfound.html';
 3824:             }
 3825:         }
 3826:     }
 3827:     unless ( $fetchresult eq 'ok') {
 3828:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3829:              ' to host '.$home.': '.$fetchresult);
 3830:     }
 3831:     return $fetchresult;
 3832: }
 3833: 
 3834: sub build_filepath {
 3835:     my ($fpath) = @_;
 3836:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 3837:     unless ($fpath eq '') {
 3838:         my @parts=split('/',$fpath);
 3839:         foreach my $part (@parts) {
 3840:             $filepath.= '/'.$part;
 3841:             if ((-e $filepath)!=1) {
 3842:                 mkdir($filepath,0777);
 3843:             }
 3844:         }
 3845:     }
 3846:     return $filepath;
 3847: }
 3848: 
 3849: sub store_edited_file {
 3850:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 3851:     my $file = $primary_url;
 3852:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 3853:     my $fpath = '';
 3854:     my $fname = $file;
 3855:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3856:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3857:     my $filepath = &build_filepath($fpath);
 3858:     open(my $fh,'>',$filepath.'/'.$fname);
 3859:     print $fh $content;
 3860:     close($fh);
 3861:     my $home=&homeserver($docuname,$docudom);
 3862:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3863: 			  $home);
 3864:     if ($$fetchresult eq 'ok') {
 3865:         return '/uploaded/'.$fpath.'/'.$fname;
 3866:     } else {
 3867:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3868: 		 ' to host '.$home.': '.$$fetchresult);
 3869:         return '/adm/notfound.html';
 3870:     }
 3871: }
 3872: 
 3873: sub clean_filename {
 3874:     my ($fname,$args)=@_;
 3875: # Replace Windows backslashes by forward slashes
 3876:     $fname=~s/\\/\//g;
 3877:     if (!$args->{'keep_path'}) {
 3878:         # Get rid of everything but the actual filename
 3879: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 3880:     }
 3881: # Replace spaces by underscores
 3882:     $fname=~s/\s+/\_/g;
 3883: # Transliterate non-ascii text to ascii
 3884:     my $lang = &Apache::lonlocal::current_language();
 3885:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 3886: # Replace all other weird characters by nothing
 3887:     $fname=~s{[^/\w\.\-]}{}g;
 3888: # Replace all .\d. sequences with _\d. so they no longer look like version
 3889: # numbers
 3890:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 3891:     return $fname;
 3892: }
 3893: 
 3894: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 3895: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 3896: # image with the same aspect ratio as the original, but with dimensions which do 
 3897: # not exceed $resizewidth and $resizeheight.
 3898:  
 3899: sub resizeImage {
 3900:     my ($img_path,$resizewidth,$resizeheight) = @_;
 3901:     my $ima = Image::Magick->new;
 3902:     my $resized;
 3903:     if (-e $img_path) {
 3904:         $ima->Read($img_path);
 3905:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 3906:             my $width = $ima->Get('width');
 3907:             my $height = $ima->Get('height');
 3908:             if ($width > $resizewidth) {
 3909: 	        my $factor = $width/$resizewidth;
 3910:                 my $newheight = $height/$factor;
 3911:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 3912:                 $resized = 1;
 3913:             }
 3914:         }
 3915:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 3916:             my $width = $ima->Get('width');
 3917:             my $height = $ima->Get('height');
 3918:             if ($height > $resizeheight) {
 3919:                 my $factor = $height/$resizeheight;
 3920:                 my $newwidth = $width/$factor;
 3921:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 3922:                 $resized = 1;
 3923:             }
 3924:         }
 3925:         if ($resized) {
 3926:             $ima->Write($img_path);
 3927:         }
 3928:     }
 3929:     return;
 3930: }
 3931: 
 3932: # --------------- Take an uploaded file and put it into the userfiles directory
 3933: # input: $formname - the contents of the file are in $env{"form.$formname"}
 3934: #                    the desired filename is in $env{"form.$formname.filename"}
 3935: #        $context - possible values: coursedoc, existingfile, overwrite, 
 3936: #                                    canceloverwrite, scantron or ''.
 3937: #                   if 'coursedoc': upload to the current course
 3938: #                   if 'existingfile': write file to tmp/overwrites directory 
 3939: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 3940: #                   $context is passed as argument to &finishuserfileupload
 3941: #        $subdir - directory in userfile to store the file into
 3942: #        $parser - instruction to parse file for objects ($parser = parse) or
 3943: #                  if context is 'scantron', $parser is hashref of csv column mapping
 3944: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 3945: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 3946: #        $allfiles - reference to hash for embedded objects
 3947: #        $codebase - reference to hash for codebase of java objects
 3948: #        $desuname - username for permanent storage of uploaded file
 3949: #        $dsetudom - domain for permanaent storage of uploaded file
 3950: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 3951: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 3952: #        $resizewidth - width (pixels) to which to resize uploaded image
 3953: #        $resizeheight - height (pixels) to which to resize uploaded image
 3954: #        $mimetype - reference to scalar to accommodate mime type determined
 3955: #                    from File::MMagic.
 3956: # 
 3957: # output: url of file in userspace, or error: <message> 
 3958: #             or /adm/notfound.html if failure to upload occurse
 3959: 
 3960: sub userfileupload {
 3961:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 3962:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 3963:     if (!defined($subdir)) { $subdir='unknown'; }
 3964:     my $fname=$env{'form.'.$formname.'.filename'};
 3965:     $fname=&clean_filename($fname);
 3966:     # See if there is anything left
 3967:     unless ($fname) { return 'error: no uploaded file'; }
 3968:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 3969:     if ($fname =~ /^\./) {
 3970:         my ($s,$usec) = &gettimeofday();
 3971:         while (length($usec) < 6) {
 3972:             $usec = '0'.$usec;
 3973:         }
 3974:         $fname = $s.'_'.substr($usec,0,3).$fname;
 3975:     }
 3976:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 3977:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 3978:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 3979:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 3980:         my $now = time;
 3981:         my $filepath;
 3982:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 3983:              $filepath = 'tmp/helprequests/'.$now;
 3984:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 3985:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 3986:                          '_'.$env{'user.domain'}.'/pending';
 3987:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 3988:             my ($docuname,$docudom);
 3989:             if ($destudom =~ /^$match_domain$/) {
 3990:                 $docudom = $destudom;
 3991:             } else {
 3992:                 $docudom = $env{'user.domain'};
 3993:             }
 3994:             if ($destuname =~ /^$match_username$/) {
 3995:                 $docuname = $destuname;
 3996:             } else {
 3997:                 $docuname = $env{'user.name'};
 3998:             }
 3999:             if (exists($env{'form.group'})) {
 4000:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4001:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4002:             }
 4003:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4004:             if ($context eq 'canceloverwrite') {
 4005:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4006:                 if (-e  $tempfile) {
 4007:                     my @info = stat($tempfile);
 4008:                     if ($info[9] eq $env{'form.timestamp'}) {
 4009:                         unlink($tempfile);
 4010:                     }
 4011:                 }
 4012:                 return;
 4013:             }
 4014:         }
 4015:         # Create the directory if not present
 4016:         my @parts=split(/\//,$filepath);
 4017:         my $fullpath = $perlvar{'lonDaemons'};
 4018:         for (my $i=0;$i<@parts;$i++) {
 4019:             $fullpath .= '/'.$parts[$i];
 4020:             if ((-e $fullpath)!=1) {
 4021:                 mkdir($fullpath,0777);
 4022:             }
 4023:         }
 4024:         open(my $fh,'>',$fullpath.'/'.$fname);
 4025:         print $fh $env{'form.'.$formname};
 4026:         close($fh);
 4027:         if ($context eq 'existingfile') {
 4028:             my @info = stat($fullpath.'/'.$fname);
 4029:             return ($fullpath.'/'.$fname,$info[9]);
 4030:         } else {
 4031:             return $fullpath.'/'.$fname;
 4032:         }
 4033:     }
 4034:     if ($subdir eq 'scantron') {
 4035:         $fname = 'scantron_orig_'.$fname;
 4036:     } else {
 4037:         $fname="$subdir/$fname";
 4038:     }
 4039:     if ($context eq 'coursedoc') {
 4040: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4041: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4042:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4043:             return &finishuserfileupload($docuname,$docudom,
 4044: 					 $formname,$fname,$parser,$allfiles,
 4045: 					 $codebase,$thumbwidth,$thumbheight,
 4046:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4047:         } else {
 4048:             if ($env{'form.folder'}) {
 4049:                 $fname=$env{'form.folder'}.'/'.$fname;
 4050:             }
 4051:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4052: 				       $fname,$formname,$parser,
 4053: 				       $allfiles,$codebase,$mimetype);
 4054:         }
 4055:     } elsif (defined($destuname)) {
 4056:         my $docuname=$destuname;
 4057:         my $docudom=$destudom;
 4058: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4059: 				     $parser,$allfiles,$codebase,
 4060:                                      $thumbwidth,$thumbheight,
 4061:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4062:     } else {
 4063:         my $docuname=$env{'user.name'};
 4064:         my $docudom=$env{'user.domain'};
 4065:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4066:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4067:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4068:         }
 4069: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4070: 				     $parser,$allfiles,$codebase,
 4071:                                      $thumbwidth,$thumbheight,
 4072:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4073:     }
 4074: }
 4075: 
 4076: sub finishuserfileupload {
 4077:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4078:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4079:     my $path=$docudom.'/'.$docuname.'/';
 4080:     my $filepath=$perlvar{'lonDocRoot'};
 4081:   
 4082:     my ($fnamepath,$file,$fetchthumb);
 4083:     $file=$fname;
 4084:     if ($fname=~m|/|) {
 4085:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4086: 	$path.=$fnamepath.'/';
 4087:     }
 4088:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4089:     my $count;
 4090:     for ($count=4;$count<=$#parts;$count++) {
 4091:         $filepath.="/$parts[$count]";
 4092:         if ((-e $filepath)!=1) {
 4093: 	    mkdir($filepath,0777);
 4094:         }
 4095:     }
 4096: 
 4097: # Save the file
 4098:     {
 4099: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4100: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4101: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4102: 	    return '/adm/notfound.html';
 4103: 	}
 4104:         if ($context eq 'overwrite') {
 4105:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4106:             my $target = $filepath.'/'.$file;
 4107:             if (-e $source) {
 4108:                 my @info = stat($source);
 4109:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4110:                     unless (&File::Copy::move($source,$target)) {
 4111:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4112:                         return "Moving from $source failed";
 4113:                     }
 4114:                 } else {
 4115:                     return "Temporary file: $source had unexpected date/time for last modification";
 4116:                 }
 4117:             } else {
 4118:                 return "Temporary file: $source missing";
 4119:             }
 4120:         } elsif (!print FH ($env{'form.'.$formname})) {
 4121: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4122: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4123: 	    return '/adm/notfound.html';
 4124: 	}
 4125: 	close(FH);
 4126:         if ($resizewidth && $resizeheight) {
 4127:             my $mm = new File::MMagic;
 4128:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4129:             if ($mime_type =~ m{^image/}) {
 4130: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4131:             }  
 4132: 	}
 4133:     }
 4134:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4135:         if (ref($mimetype)) {
 4136:             if ($$mimetype eq '') {
 4137:                 my $mm = new File::MMagic;
 4138:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4139:                 $$mimetype = $type;
 4140:             }
 4141:         }
 4142:     }
 4143:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4144:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4145:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4146:                                                        $allfiles,$codebase);
 4147:             unless ($parse_result eq 'ok') {
 4148:                 &logthis('Failed to parse '.$filepath.$file.
 4149: 	   	         ' for embedded media: '.$parse_result); 
 4150:             }
 4151:         }
 4152:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4153:         my $format = $env{'form.scantron_format'};
 4154:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4155:     }
 4156:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4157:         my $input = $filepath.'/'.$file;
 4158:         my $output = $filepath.'/'.'tn-'.$file;
 4159:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4160:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4161:         system({$args[0]} @args);
 4162:         if (-e $filepath.'/'.'tn-'.$file) {
 4163:             $fetchthumb  = 1; 
 4164:         }
 4165:     }
 4166:  
 4167: # Notify homeserver to grep it
 4168: #
 4169:     my $docuhome=&homeserver($docuname,$docudom);	
 4170:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4171:     if ($fetchresult eq 'ok') {
 4172:         if ($fetchthumb) {
 4173:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4174:             if ($thumbresult ne 'ok') {
 4175:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4176:                          $docuhome.': '.$thumbresult);
 4177:             }
 4178:         }
 4179: #
 4180: # Return the URL to it
 4181:         return '/uploaded/'.$path.$file;
 4182:     } else {
 4183:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4184: 		 ': '.$fetchresult);
 4185:         return '/adm/notfound.html';
 4186:     }
 4187: }
 4188: 
 4189: sub extract_embedded_items {
 4190:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4191:     my @state = ();
 4192:     my (%lastids,%related,%shockwave,%flashvars);
 4193:     my %javafiles = (
 4194:                       codebase => '',
 4195:                       code => '',
 4196:                       archive => ''
 4197:                     );
 4198:     my %mediafiles = (
 4199:                       src => '',
 4200:                       movie => '',
 4201:                      );
 4202:     my $p;
 4203:     if ($content) {
 4204:         $p = HTML::LCParser->new($content);
 4205:     } else {
 4206:         $p = HTML::LCParser->new($fullpath);
 4207:     }
 4208:     while (my $t=$p->get_token()) {
 4209: 	if ($t->[0] eq 'S') {
 4210: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4211: 	    push(@state, $tagname);
 4212:             if (lc($tagname) eq 'allow') {
 4213:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4214:             }
 4215: 	    if (lc($tagname) eq 'img') {
 4216: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4217: 	    }
 4218: 	    if (lc($tagname) eq 'a') {
 4219:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4220:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4221:                 }
 4222: 	    }
 4223:             if (lc($tagname) eq 'script') {
 4224:                 my $src;
 4225:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4226:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4227:                 } else {
 4228:                     if ($attr->{'src'} ne '') {
 4229:                         $src = $attr->{'src'};
 4230:                         &add_filetype($allfiles,$src,'src');
 4231:                     }
 4232:                 }
 4233:                 my $text = $p->get_trimmed_text();
 4234:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4235:                     my @swfargs = split(/,/,$1);
 4236:                     foreach my $item (@swfargs) {
 4237:                         $item =~ s/["']//g;
 4238:                         $item =~ s/^\s+//;
 4239:                         $item =~ s/\s+$//;
 4240:                     }
 4241:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4242:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4243:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4244:                         } else {
 4245:                             $related{$swfargs[0]} = [$swfargs[2]];
 4246:                         }
 4247:                     }
 4248:                 }
 4249:             }
 4250:             if (lc($tagname) eq 'link') {
 4251:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4252:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4253:                 }
 4254:             }
 4255: 	    if (lc($tagname) eq 'object' ||
 4256: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4257: 		foreach my $item (keys(%javafiles)) {
 4258: 		    $javafiles{$item} = '';
 4259: 		}
 4260:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4261:                     $lastids{lc($tagname)} = $attr->{'id'};
 4262:                 }
 4263: 	    }
 4264: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4265: 		my $name = lc($attr->{'name'});
 4266: 		foreach my $item (keys(%javafiles)) {
 4267: 		    if ($name eq $item) {
 4268: 			$javafiles{$item} = $attr->{'value'};
 4269: 			last;
 4270: 		    }
 4271: 		}
 4272:                 my $pathfrom;
 4273: 		foreach my $item (keys(%mediafiles)) {
 4274: 		    if ($name eq $item) {
 4275:                         $pathfrom = $attr->{'value'};
 4276:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4277: 			&add_filetype($allfiles,$pathfrom,$name);
 4278: 			last;
 4279: 		    }
 4280: 		}
 4281:                 if ($name eq 'flashvars') {
 4282:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4283:                 }
 4284:                 if ($pathfrom ne '') {
 4285:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4286:                                          $pathfrom);
 4287:                 }
 4288: 	    }
 4289: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4290: 		foreach my $item (keys(%javafiles)) {
 4291: 		    if ($attr->{$item}) {
 4292: 			$javafiles{$item} = $attr->{$item};
 4293: 			last;
 4294: 		    }
 4295: 		}
 4296: 		foreach my $item (keys(%mediafiles)) {
 4297: 		    if ($attr->{$item}) {
 4298: 			&add_filetype($allfiles,$attr->{$item},$item);
 4299: 			last;
 4300: 		    }
 4301: 		}
 4302:                 if (lc($tagname) eq 'embed') {
 4303:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4304:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4305:                                              $attr->{'src'});
 4306:                     }
 4307:                 }
 4308: 	    }
 4309:             if (lc($tagname) eq 'iframe') {
 4310:                 my $src = $attr->{'src'} ;
 4311:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4312:                     &add_filetype($allfiles,$src,'src');
 4313:                 } elsif ($src =~ m{^/}) {
 4314:                     if ($env{'request.course.id'}) {
 4315:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4316:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4317:                         my $url = &hreflocation('',$fullpath);
 4318:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4319:                             my $relpath = $1;
 4320:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4321:                                 &add_filetype($allfiles,$1,'src');
 4322:                             }
 4323:                         }
 4324:                     }
 4325:                 }
 4326:             }
 4327:             if ($t->[4] =~ m{/>$}) {
 4328:                 pop(@state);
 4329:             }
 4330: 	} elsif ($t->[0] eq 'E') {
 4331: 	    my ($tagname) = ($t->[1]);
 4332: 	    if ($javafiles{'codebase'} ne '') {
 4333: 		$javafiles{'codebase'} .= '/';
 4334: 	    }  
 4335: 	    if (lc($tagname) eq 'applet' ||
 4336: 		lc($tagname) eq 'object' ||
 4337: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4338: 		) {
 4339: 		foreach my $item (keys(%javafiles)) {
 4340: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4341: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4342: 			&add_filetype($allfiles,$file,$item);
 4343: 		    }
 4344: 		}
 4345: 	    } 
 4346: 	    pop @state;
 4347: 	}
 4348:     }
 4349:     foreach my $id (sort(keys(%flashvars))) {
 4350:         if ($shockwave{$id} ne '') {
 4351:             my @pairs = split(/\&/,$flashvars{$id});
 4352:             foreach my $pair (@pairs) {
 4353:                 my ($key,$value) = split(/\=/,$pair);
 4354:                 if ($key eq 'thumb') {
 4355:                     &add_filetype($allfiles,$value,$key);
 4356:                 } elsif ($key eq 'content') {
 4357:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4358:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4359:                     if ($ext ne '') {
 4360:                         &add_filetype($allfiles,$path.$value,$ext);
 4361:                     }
 4362:                 }
 4363:             }
 4364:         }
 4365:     }
 4366:     return 'ok';
 4367: }
 4368: 
 4369: sub add_filetype {
 4370:     my ($allfiles,$file,$type)=@_;
 4371:     if (exists($allfiles->{$file})) {
 4372: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4373: 	    push(@{$allfiles->{$file}}, &escape($type));
 4374: 	}
 4375:     } else {
 4376: 	@{$allfiles->{$file}} = (&escape($type));
 4377:     }
 4378: }
 4379: 
 4380: sub embedded_dependency {
 4381:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4382:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4383:         if (($identifier ne '') &&
 4384:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4385:             ($pathfrom ne '')) {
 4386:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4387:             foreach my $dep (@{$related->{$identifier}}) {
 4388:                 &add_filetype($allfiles,$path.$dep,'object');
 4389:             }
 4390:         }
 4391:     }
 4392:     return;
 4393: }
 4394: 
 4395: sub bubblesheet_converter {
 4396:     my ($cdom,$fullpath,$config,$format) = @_;
 4397:     if ((&domain($cdom) ne '') &&
 4398:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4399:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4400:         my (%csvcols,%csvoptions);
 4401:         if (ref($config->{'fields'}) eq 'HASH') {  
 4402:             %csvcols = %{$config->{'fields'}};
 4403:         }
 4404:         if (ref($config->{'options'}) eq 'HASH') {
 4405:             %csvoptions = %{$config->{'options'}};
 4406:         }
 4407:         my %csvbynum = reverse(%csvcols);
 4408:         my %scantronconf = &get_scantron_config($format,$cdom);
 4409:         if (keys(%scantronconf)) {
 4410:             my %bynum = (
 4411:                           $scantronconf{CODEstart} => 'CODEstart',
 4412:                           $scantronconf{IDstart}   => 'IDstart',
 4413:                           $scantronconf{PaperID}   => 'PaperID',
 4414:                           $scantronconf{FirstName} => 'FirstName',
 4415:                           $scantronconf{LastName}  => 'LastName',
 4416:                           $scantronconf{Qstart}    => 'Qstart',
 4417:                         );
 4418:             my @ordered;
 4419:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4420:                 push(@ordered,$bynum{$item});
 4421:             }
 4422:             my %mapstart = (
 4423:                               CODEstart => 'CODE',
 4424:                               IDstart   => 'ID',
 4425:                               PaperID   => 'PaperID',
 4426:                               FirstName => 'FirstName',
 4427:                               LastName  => 'LastName',
 4428:                               Qstart    => 'FirstQuestion',
 4429:                            );
 4430:             my %maplength = (
 4431:                               CODEstart => 'CODElength',
 4432:                               IDstart   => 'IDlength',
 4433:                               PaperID   => 'PaperIDlength',
 4434:                               FirstName => 'FirstNamelength',
 4435:                               LastName  => 'LastNamelength',
 4436:             );
 4437:             if (open(my $fh,'<',$fullpath)) {
 4438:                 my $output;
 4439:                 my %lettdig = &letter_to_digits();
 4440:                 my %diglett = reverse(%lettdig);
 4441:                 my $numletts = scalar(keys(%lettdig));
 4442:                 my $num = 0;
 4443:                 while (my $line=<$fh>) {
 4444:                     $num ++;
 4445:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4446:                     $line =~ s{[\r\n]+$}{};
 4447:                     my %found;
 4448:                     my @values = split(/,/,$line);
 4449:                     my ($qstart,$record);
 4450:                     for (my $i=0; $i<@values; $i++) {
 4451:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4452:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4453:                             if ($values[$i] eq '') {
 4454:                                 $values[$i] = $scantronconf{'Qoff'};
 4455:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4456:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4457:                                     $values[$i] = $lettdig{uc($values[$i])};
 4458:                                 }
 4459:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4460:                                 if ($values[$i] =~ /^[0-9]$/) {
 4461:                                     $values[$i] = $diglett{$values[$i]};
 4462:                                 }
 4463:                             } else {
 4464:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4465:                                     my $digit;
 4466:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4467:                                         $digit = $lettdig{uc($values[$i])}-1;
 4468:                                         if ($values[$i] eq 'J') {
 4469:                                             $digit += $numletts;
 4470:                                         }
 4471:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4472:                                         $digit = $values[$i]-1;
 4473:                                         if ($values[$i] eq '0') {
 4474:                                             $digit += $numletts;
 4475:                                         }
 4476:                                     }
 4477:                                     my $qval='';
 4478:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4479:                                         if ($j == $digit) {
 4480:                                             $qval .= $scantronconf{'Qon'};
 4481:                                         } else {
 4482:                                             $qval .= $scantronconf{'Qoff'};
 4483:                                         }
 4484:                                     }
 4485:                                     $values[$i] = $qval;
 4486:                                 }
 4487:                             }
 4488:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4489:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4490:                             }
 4491:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4492:                             if ($numblank > 0) {
 4493:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4494:                             }
 4495:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4496:                                 $qstart = $i;
 4497:                                 $found{$csvbynum{$i}} = $values[$i];
 4498:                             } else {
 4499:                                 $found{'FirstQuestion'} .= $values[$i];
 4500:                             }
 4501:                         } elsif (exists($csvbynum{$i})) {
 4502:                             if ($csvoptions{'rem'}) {
 4503:                                 $values[$i] =~ s/^\s+//;
 4504:                             }
 4505:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4506:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4507:                                     $values[$i] = '0'.$values[$i];
 4508:                                 }
 4509:                             }
 4510:                             $found{$csvbynum{$i}} = $values[$i];
 4511:                         }
 4512:                     }
 4513:                     foreach my $item (@ordered) {
 4514:                         my $currlength = 1+length($record);
 4515:                         my $numspaces = $scantronconf{$item} - $currlength;
 4516:                         if ($numspaces > 0) {
 4517:                             $record .= (' ' x $numspaces);
 4518:                         }
 4519:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4520:                             unless ($item eq 'Qstart') {
 4521:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4522:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4523:                                 }
 4524:                             }
 4525:                             $record .= $found{$mapstart{$item}};
 4526:                         }
 4527:                     }
 4528:                     $output .= "$record\n";
 4529:                 }
 4530:                 close($fh);
 4531:                 if ($output) {
 4532:                     if (open(my $fh,'>',$fullpath)) {
 4533:                         print $fh $output;
 4534:                         close($fh);
 4535:                     }
 4536:                 }
 4537:             }
 4538:         }
 4539:         return;
 4540:     }
 4541: }
 4542: 
 4543: sub letter_to_digits {
 4544:     my %lettdig = (
 4545:                     A => 1,
 4546:                     B => 2,
 4547:                     C => 3,
 4548:                     D => 4,
 4549:                     E => 5,
 4550:                     F => 6,
 4551:                     G => 7,
 4552:                     H => 8,
 4553:                     I => 9,
 4554:                     J => 0,
 4555:                   );
 4556:     return %lettdig;
 4557: }
 4558: 
 4559: sub get_scantron_config {
 4560:     my ($which,$cdom) = @_;
 4561:     my @lines = &get_scantronformat_file($cdom);
 4562:     my %config;
 4563:     #FIXME probably should move to XML it has already gotten a bit much now
 4564:     foreach my $line (@lines) {
 4565:         my ($name,$descrip)=split(/:/,$line);
 4566:         if ($name ne $which ) { next; }
 4567:         chomp($line);
 4568:         my @config=split(/:/,$line);
 4569:         $config{'name'}=$config[0];
 4570:         $config{'description'}=$config[1];
 4571:         $config{'CODElocation'}=$config[2];
 4572:         $config{'CODEstart'}=$config[3];
 4573:         $config{'CODElength'}=$config[4];
 4574:         $config{'IDstart'}=$config[5];
 4575:         $config{'IDlength'}=$config[6];
 4576:         $config{'Qstart'}=$config[7];
 4577:         $config{'Qlength'}=$config[8];
 4578:         $config{'Qoff'}=$config[9];
 4579:         $config{'Qon'}=$config[10];
 4580:         $config{'PaperID'}=$config[11];
 4581:         $config{'PaperIDlength'}=$config[12];
 4582:         $config{'FirstName'}=$config[13];
 4583:         $config{'FirstNamelength'}=$config[14];
 4584:         $config{'LastName'}=$config[15];
 4585:         $config{'LastNamelength'}=$config[16];
 4586:         $config{'BubblesPerRow'}=$config[17];
 4587:         last;
 4588:     }
 4589:     return %config;
 4590: }
 4591: 
 4592: sub get_scantronformat_file {
 4593:     my ($cdom) = @_;
 4594:     if ($cdom eq '') {
 4595:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4596:     }
 4597:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4598:     my $gottab = 0;
 4599:     my @lines;
 4600:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4601:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4602:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4603:             if ($formatfile ne '-1') {
 4604:                 @lines = split("\n",$formatfile,-1);
 4605:                 $gottab = 1;
 4606:             }
 4607:         }
 4608:     }
 4609:     if (!$gottab) {
 4610:         my $confname = $cdom.'-domainconfig';
 4611:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4612:         my $formatfile = &getfile($default);
 4613:         if ($formatfile ne '-1') {
 4614:             @lines = split("\n",$formatfile,-1);
 4615:             $gottab = 1;
 4616:         }
 4617:     }
 4618:     if (!$gottab) {
 4619:         my @domains = &current_machine_domains();
 4620:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4621:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4622:                 @lines = <$fh>;
 4623:                 close($fh);
 4624:             }
 4625:         } else {
 4626:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4627:                 @lines = <$fh>;
 4628:                 close($fh);
 4629:             }
 4630:         }
 4631:     }
 4632:     return @lines;
 4633: }
 4634: 
 4635: sub removeuploadedurl {
 4636:     my ($url)=@_;	
 4637:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4638:     return &removeuserfile($uname,$udom,$fname);
 4639: }
 4640: 
 4641: sub removeuserfile {
 4642:     my ($docuname,$docudom,$fname)=@_;
 4643:     my $home=&homeserver($docuname,$docudom);    
 4644:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4645:     if ($result eq 'ok') {	
 4646:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4647:             my $metafile = $fname.'.meta';
 4648:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4649: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4650:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4651:             my $sqlresult = 
 4652:                 &update_portfolio_table($docuname,$docudom,$file,
 4653:                                         'portfolio_metadata',$group,
 4654:                                         'delete');
 4655:         }
 4656:     }
 4657:     return $result;
 4658: }
 4659: 
 4660: sub mkdiruserfile {
 4661:     my ($docuname,$docudom,$dir)=@_;
 4662:     my $home=&homeserver($docuname,$docudom);
 4663:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4664: }
 4665: 
 4666: sub renameuserfile {
 4667:     my ($docuname,$docudom,$old,$new)=@_;
 4668:     my $home=&homeserver($docuname,$docudom);
 4669:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4670:                         &escape("$old").':'.&escape("$new"),$home);
 4671:     if ($result eq 'ok') {
 4672:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4673:             my $oldmeta = $old.'.meta';
 4674:             my $newmeta = $new.'.meta';
 4675:             my $metaresult = 
 4676:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4677: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4678:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4679:             my $sqlresult = 
 4680:                 &update_portfolio_table($docuname,$docudom,$file,
 4681:                                         'portfolio_metadata',$group,
 4682:                                         'delete');
 4683:         }
 4684:     }
 4685:     return $result;
 4686: }
 4687: 
 4688: # ------------------------------------------------------------------------- Log
 4689: 
 4690: sub log {
 4691:     my ($dom,$nam,$hom,$what)=@_;
 4692:     return critical("log:$dom:$nam:$what",$hom);
 4693: }
 4694: 
 4695: # ------------------------------------------------------------------ Course Log
 4696: #
 4697: # This routine flushes several buffers of non-mission-critical nature
 4698: #
 4699: 
 4700: sub flushcourselogs {
 4701:     &logthis('Flushing log buffers');
 4702: #
 4703: # course logs
 4704: # This is a log of all transactions in a course, which can be used
 4705: # for data mining purposes
 4706: #
 4707: # It also collects the courseid database, which lists last transaction
 4708: # times and course titles for all courseids
 4709: #
 4710:     my %courseidbuffer=();
 4711:     foreach my $crsid (keys(%courselogs)) {
 4712:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4713: 		          &escape($courselogs{$crsid}),
 4714: 		          $coursehombuf{$crsid}) eq 'ok') {
 4715: 	    delete $courselogs{$crsid};
 4716:         } else {
 4717:             &logthis('Failed to flush log buffer for '.$crsid);
 4718:             if (length($courselogs{$crsid})>40000) {
 4719:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4720:                         " exceeded maximum size, deleting.</font>");
 4721:                delete $courselogs{$crsid};
 4722:             }
 4723:         }
 4724:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4725:             'description' => $coursedescrbuf{$crsid},
 4726:             'inst_code'    => $courseinstcodebuf{$crsid},
 4727:             'type'        => $coursetypebuf{$crsid},
 4728:             'owner'       => $courseownerbuf{$crsid},
 4729:         };
 4730:     }
 4731: #
 4732: # Write course id database (reverse lookup) to homeserver of courses 
 4733: # Is used in pickcourse
 4734: #
 4735:     foreach my $crs_home (keys(%courseidbuffer)) {
 4736:         my $response = &courseidput(&host_domain($crs_home),
 4737:                                     $courseidbuffer{$crs_home},
 4738:                                     $crs_home,'timeonly');
 4739:     }
 4740: #
 4741: # File accesses
 4742: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4743: #
 4744:     foreach my $entry (keys(%accesshash)) {
 4745:         if ($entry =~ /___count$/) {
 4746:             my ($dom,$name);
 4747:             ($dom,$name,undef)=
 4748: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4749:             if (! defined($dom) || $dom eq '' || 
 4750:                 ! defined($name) || $name eq '') {
 4751:                 my $cid = $env{'request.course.id'};
 4752:                 $dom  = $env{'request.'.$cid.'.domain'};
 4753:                 $name = $env{'request.'.$cid.'.num'};
 4754:             }
 4755:             my $value = $accesshash{$entry};
 4756:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4757:             my %temphash=($url => $value);
 4758:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4759:             if ($result eq 'ok') {
 4760:                 delete $accesshash{$entry};
 4761:             }
 4762:         } else {
 4763:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4764:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4765:             my %temphash=($entry => $accesshash{$entry});
 4766:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4767:                 delete $accesshash{$entry};
 4768:             }
 4769:         }
 4770:     }
 4771: #
 4772: # Roles
 4773: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4774: #
 4775:     foreach my $entry (keys(%userrolehash)) {
 4776:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4777: 	    split(/\:/,$entry);
 4778:         if (&Apache::lonnet::put('nohist_userroles',
 4779:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4780:                 $rudom,$runame) eq 'ok') {
 4781: 	    delete $userrolehash{$entry};
 4782:         }
 4783:     }
 4784: #
 4785: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4786: #
 4787:     my %domrolebuffer = ();
 4788:     foreach my $entry (keys(%domainrolehash)) {
 4789:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 4790:         if ($domrolebuffer{$rudom}) {
 4791:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 4792:                       '='.&escape($domainrolehash{$entry});
 4793:         } else {
 4794:             $domrolebuffer{$rudom}.=&escape($entry).
 4795:                       '='.&escape($domainrolehash{$entry});
 4796:         }
 4797:         delete $domainrolehash{$entry};
 4798:     }
 4799:     foreach my $dom (keys(%domrolebuffer)) {
 4800: 	my %servers;
 4801: 	if (defined(&domain($dom,'primary'))) {
 4802: 	    my $primary=&domain($dom,'primary');
 4803: 	    my $hostname=&hostname($primary);
 4804: 	    $servers{$primary} = $hostname;
 4805: 	} else { 
 4806: 	    %servers = &get_servers($dom,'library');
 4807: 	}
 4808: 	foreach my $tryserver (keys(%servers)) {
 4809: 	    if (&reply('domroleput:'.$dom.':'.
 4810: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 4811: 		last;
 4812: 	    } else {  
 4813: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 4814: 	    }
 4815:         }
 4816:     }
 4817:     $dumpcount++;
 4818: }
 4819: 
 4820: sub courselog {
 4821:     my $what=shift;
 4822:     $what=time.':'.$what;
 4823:     unless ($env{'request.course.id'}) { return ''; }
 4824:     $coursedombuf{$env{'request.course.id'}}=
 4825:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 4826:     $coursenumbuf{$env{'request.course.id'}}=
 4827:        $env{'course.'.$env{'request.course.id'}.'.num'};
 4828:     $coursehombuf{$env{'request.course.id'}}=
 4829:        $env{'course.'.$env{'request.course.id'}.'.home'};
 4830:     $coursedescrbuf{$env{'request.course.id'}}=
 4831:        $env{'course.'.$env{'request.course.id'}.'.description'};
 4832:     $courseinstcodebuf{$env{'request.course.id'}}=
 4833:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 4834:     $courseownerbuf{$env{'request.course.id'}}=
 4835:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 4836:     $coursetypebuf{$env{'request.course.id'}}=
 4837:        $env{'course.'.$env{'request.course.id'}.'.type'};
 4838:     if (defined $courselogs{$env{'request.course.id'}}) {
 4839: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 4840:     } else {
 4841: 	$courselogs{$env{'request.course.id'}}.=$what;
 4842:     }
 4843:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 4844: 	&flushcourselogs();
 4845:     }
 4846: }
 4847: 
 4848: sub courseacclog {
 4849:     my $fnsymb=shift;
 4850:     unless ($env{'request.course.id'}) { return ''; }
 4851:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 4852:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 4853:         $what.=':POST';
 4854:         # FIXME: Probably ought to escape things....
 4855: 	foreach my $key (keys(%env)) {
 4856:             if ($key=~/^form\.(.*)/) {
 4857:                 my $formitem = $1;
 4858:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 4859:                     $what.=':'.$formitem.'='.$env{$key};
 4860:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 4861:                     $what.=':'.$formitem.'='.$env{$key};
 4862:                 }
 4863:             }
 4864:         }
 4865:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 4866:         # FIXME: We should not be depending on a form parameter that someone
 4867:         # editing lonsearchcat.pm might change in the future.
 4868:         if ($env{'form.phase'} eq 'course_search') {
 4869:             $what.= ':POST';
 4870:             # FIXME: Probably ought to escape things....
 4871:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 4872:                                  'crsdiscuss') {
 4873:                 $what.=':'.$element.'='.$env{'form.'.$element};
 4874:             }
 4875:         }
 4876:     }
 4877:     &courselog($what);
 4878: }
 4879: 
 4880: sub countacc {
 4881:     my $url=&declutter(shift);
 4882:     return if (! defined($url) || $url eq '');
 4883:     unless ($env{'request.course.id'}) { return ''; }
 4884: #
 4885: # Mark that this url was used in this course
 4886: #
 4887:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 4888: #
 4889: # Increase the access count for this resource in this child process
 4890: #
 4891:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 4892:     $accesshash{$key}++;
 4893: }
 4894: 
 4895: sub linklog {
 4896:     my ($from,$to)=@_;
 4897:     $from=&declutter($from);
 4898:     $to=&declutter($to);
 4899:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 4900:     $accesshash{$to.'___'.$from.'___goto'}=1;
 4901: }
 4902: 
 4903: sub statslog {
 4904:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 4905:     if ($users<2) { return; }
 4906:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 4907:             'course'       => $env{'request.course.id'},
 4908:             'sections'     => '"all"',
 4909:             'num_students' => $users,
 4910:             'part'         => $part,
 4911:             'symb'         => $symb,
 4912:             'mean_tries'   => $av_attempts,
 4913:             'deg_of_diff'  => $degdiff});
 4914:     foreach my $key (keys(%dynstore)) {
 4915:         $accesshash{$key}=$dynstore{$key};
 4916:     }
 4917: }
 4918:   
 4919: sub userrolelog {
 4920:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 4921:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 4922:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 4923:        $userrolehash
 4924:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 4925:                     =$tend.':'.$tstart;
 4926:     }
 4927:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 4928:        $userrolehash
 4929:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 4930:                     =$tend.':'.$tstart;
 4931:     }
 4932:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 4933:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 4934:        $domainrolehash
 4935:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 4936:                     = $tend.':'.$tstart;
 4937:     }
 4938: }
 4939: 
 4940: sub courserolelog {
 4941:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 4942:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 4943:         my $cdom = $1;
 4944:         my $cnum = $2;
 4945:         my $sec = $3;
 4946:         my $namespace = 'rolelog';
 4947:         my %storehash = (
 4948:                            role    => $trole,
 4949:                            start   => $tstart,
 4950:                            end     => $tend,
 4951:                            selfenroll => $selfenroll,
 4952:                            context    => $context,
 4953:                         );
 4954:         if ($trole eq 'gr') {
 4955:             $namespace = 'groupslog';
 4956:             $storehash{'group'} = $sec;
 4957:         } else {
 4958:             $storehash{'section'} = $sec;
 4959:         }
 4960:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 4961:                    $domain,$cnum,$cdom);
 4962:         if (($trole ne 'st') || ($sec ne '')) {
 4963:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 4964:         }
 4965:     }
 4966:     return;
 4967: }
 4968: 
 4969: sub domainrolelog {
 4970:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 4971:     if ($area =~ m{^/($match_domain)/$}) {
 4972:         my $cdom = $1;
 4973:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 4974:         my $namespace = 'rolelog';
 4975:         my %storehash = (
 4976:                            role    => $trole,
 4977:                            start   => $tstart,
 4978:                            end     => $tend,
 4979:                            context => $context,
 4980:                         );
 4981:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 4982:                    $domain,$domconfiguser,$cdom);
 4983:     }
 4984:     return;
 4985: 
 4986: }
 4987: 
 4988: sub coauthorrolelog {
 4989:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 4990:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 4991:         my $audom = $1;
 4992:         my $auname = $2;
 4993:         my $namespace = 'rolelog';
 4994:         my %storehash = (
 4995:                            role    => $trole,
 4996:                            start   => $tstart,
 4997:                            end     => $tend,
 4998:                            context => $context,
 4999:                         );
 5000:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5001:                    $domain,$auname,$audom);
 5002:     }
 5003:     return;
 5004: }
 5005: 
 5006: sub get_course_adv_roles {
 5007:     my ($cid,$codes) = @_;
 5008:     $cid=$env{'request.course.id'} unless (defined($cid));
 5009:     my %coursehash=&coursedescription($cid);
 5010:     my $crstype = &Apache::loncommon::course_type($cid);
 5011:     my %nothide=();
 5012:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5013:         if ($user !~ /:/) {
 5014: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5015:         } else {
 5016:             $nothide{$user}=1;
 5017:         }
 5018:     }
 5019:     my @possdoms = ($coursehash{'domain'});
 5020:     if ($coursehash{'checkforpriv'}) {
 5021:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5022:     }
 5023:     my %returnhash=();
 5024:     my %dumphash=
 5025:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5026:     my $now=time;
 5027:     my %privileged;
 5028:     foreach my $entry (keys(%dumphash)) {
 5029: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5030:         if (($tstart) && ($tstart<0)) { next; }
 5031:         if (($tend) && ($tend<$now)) { next; }
 5032:         if (($tstart) && ($now<$tstart)) { next; }
 5033:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5034: 	if ($username eq '' || $domain eq '') { next; }
 5035:         if ((&privileged($username,$domain,\@possdoms)) &&
 5036:             (!$nothide{$username.':'.$domain})) { next; }
 5037: 	if ($role eq 'cr') { next; }
 5038:         if ($codes) {
 5039:             if ($section) { $role .= ':'.$section; }
 5040:             if ($returnhash{$role}) {
 5041:                 $returnhash{$role}.=','.$username.':'.$domain;
 5042:             } else {
 5043:                 $returnhash{$role}=$username.':'.$domain;
 5044:             }
 5045:         } else {
 5046:             my $key=&plaintext($role,$crstype);
 5047:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5048:             if ($returnhash{$key}) {
 5049: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5050:             } else {
 5051:                 $returnhash{$key}=$username.':'.$domain;
 5052:             }
 5053:         }
 5054:     }
 5055:     return %returnhash;
 5056: }
 5057: 
 5058: sub get_my_roles {
 5059:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5060:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5061:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5062:     my (%dumphash,%nothide);
 5063:     if ($context eq 'userroles') {
 5064:         %dumphash = &dump('roles',$udom,$uname);
 5065:     } else {
 5066:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5067:         if ($hidepriv) {
 5068:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5069:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5070:                 if ($user !~ /:/) {
 5071:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5072:                 } else {
 5073:                     $nothide{$user} = 1;
 5074:                 }
 5075:             }
 5076:         }
 5077:     }
 5078:     my %returnhash=();
 5079:     my $now=time;
 5080:     my %privileged;
 5081:     foreach my $entry (keys(%dumphash)) {
 5082:         my ($role,$tend,$tstart);
 5083:         if ($context eq 'userroles') {
 5084:             next if ($entry =~ /^rolesdef/);
 5085: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5086:         } else {
 5087:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5088:         }
 5089:         if (($tstart) && ($tstart<0)) { next; }
 5090:         my $status = 'active';
 5091:         if (($tend) && ($tend<=$now)) {
 5092:             $status = 'previous';
 5093:         } 
 5094:         if (($tstart) && ($now<$tstart)) {
 5095:             $status = 'future';
 5096:         }
 5097:         if (ref($types) eq 'ARRAY') {
 5098:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5099:                 next;
 5100:             } 
 5101:         } else {
 5102:             if ($status ne 'active') {
 5103:                 next;
 5104:             }
 5105:         }
 5106:         my ($rolecode,$username,$domain,$section,$area);
 5107:         if ($context eq 'userroles') {
 5108:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5109:             (undef,$domain,$username,$section) = split(/\//,$area);
 5110:         } else {
 5111:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5112:         }
 5113:         if (ref($roledoms) eq 'ARRAY') {
 5114:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5115:                 next;
 5116:             }
 5117:         }
 5118:         if (ref($roles) eq 'ARRAY') {
 5119:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5120:                 if ($role =~ /^cr\//) {
 5121:                     if (!grep(/^cr$/,@{$roles})) {
 5122:                         next;
 5123:                     }
 5124:                 } elsif ($role =~ /^gr\//) {
 5125:                     if (!grep(/^gr$/,@{$roles})) {
 5126:                         next;
 5127:                     }
 5128:                 } else {
 5129:                     next;
 5130:                 }
 5131:             }
 5132:         }
 5133:         if ($hidepriv) {
 5134:             my @privroles = ('dc','su');
 5135:             if ($context eq 'userroles') {
 5136:                 next if (grep(/^\Q$role\E$/,@privroles));
 5137:             } else {
 5138:                 my $possdoms = [$domain];
 5139:                 if (ref($roledoms) eq 'ARRAY') {
 5140:                    push(@{$possdoms},@{$roledoms}); 
 5141:                 }
 5142:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5143:                     if (!$nothide{$username.':'.$domain}) {
 5144:                         next;
 5145:                     }
 5146:                 }
 5147:             }
 5148:         }
 5149:         if ($withsec) {
 5150:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5151:                 $tstart.':'.$tend;
 5152:         } else {
 5153:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5154:         }
 5155:     }
 5156:     return %returnhash;
 5157: }
 5158: 
 5159: sub get_all_adhocroles {
 5160:     my ($dom) = @_;
 5161:     my @roles_by_num = ();
 5162:     my %domdefaults = &get_domain_defaults($dom);
 5163:     my (%description,%access_in_dom,%access_info);
 5164:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5165:         my $count = 0;
 5166:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5167:         my %ordered;
 5168:         foreach my $role (sort(keys(%domcurrent))) {
 5169:             my ($order,$desc,$access_in_dom);
 5170:             if (ref($domcurrent{$role}) eq 'HASH') {
 5171:                 $order = $domcurrent{$role}{'order'};
 5172:                 $desc = $domcurrent{$role}{'desc'};
 5173:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5174:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5175:             }
 5176:             if ($order eq '') {
 5177:                 $order = $count;
 5178:             }
 5179:             $ordered{$order} = $role;
 5180:             if ($desc ne '') {
 5181:                 $description{$role} = $desc;
 5182:             } else {
 5183:                 $description{$role}= $role;
 5184:             }
 5185:             $count++;
 5186:         }
 5187:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5188:             push(@roles_by_num,$ordered{$item});
 5189:         }
 5190:     }
 5191:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5192: }
 5193: 
 5194: sub get_my_adhocroles {
 5195:     my ($cid,$checkreg) = @_;
 5196:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5197:     if ($env{'request.course.id'} eq $cid) {
 5198:         $cdom = $env{'course.'.$cid.'.domain'};
 5199:         $cnum = $env{'course.'.$cid.'.num'};
 5200:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5201:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5202:         $cdom = $1;
 5203:         $cnum = $2;
 5204:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5205:                                      $cdom,$cnum);
 5206:     }
 5207:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5208:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5209:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5210:         if ($rosterhash{$user} ne '') {
 5211:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5212:             return ([],{}) if ($type eq 'auto');
 5213:         }
 5214:     }
 5215:     if (($cdom ne '') && ($cnum ne ''))  {
 5216:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5217:             my $then=$env{'user.login.time'};
 5218:             my $update=$env{'user.update.time'};
 5219:             if (!$update) {
 5220:                 $update = $then;
 5221:             }
 5222:             my @liveroles;
 5223:             foreach my $role ('dh','da') {
 5224:                 if ($env{"user.role.$role./$cdom/"}) {
 5225:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5226:                     my $limit = $update;
 5227:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5228:                         $limit = $then;
 5229:                     }
 5230:                     my $activerole = 1;
 5231:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5232:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5233:                     if ($activerole) {
 5234:                         push(@liveroles,$role);
 5235:                     }
 5236:                 }
 5237:             }
 5238:             if (@liveroles) {
 5239:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5240:                     my ($accessref,$accessinfo,%access_in_dom);
 5241:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5242:                     if (ref($roles_by_num) eq 'ARRAY') {
 5243:                         if (@{$roles_by_num}) {
 5244:                             my %settings;
 5245:                             if ($env{'request.course.id'} eq $cid) {
 5246:                                 foreach my $envkey (keys(%env)) {
 5247:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5248:                                         $settings{$1} = $env{$envkey};
 5249:                                     }
 5250:                                 }
 5251:                             } else {
 5252:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5253:                             }
 5254:                             my %setincrs;
 5255:                             if ($settings{'internal.adhocaccess'}) {
 5256:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5257:                             }
 5258:                             my @statuses;
 5259:                             if ($env{'environment.inststatus'}) {
 5260:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5261:                             }
 5262:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5263:                             if (ref($accessref) eq 'HASH') {
 5264:                                 %access_in_dom = %{$accessref};
 5265:                             }
 5266:                             foreach my $role (@{$roles_by_num}) {
 5267:                                 my ($curraccess,@okstatus,@personnel);
 5268:                                 if ($setincrs{$role}) {
 5269:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5270:                                     if ($curraccess eq 'status') {
 5271:                                         @okstatus = split(/\&/,$rest);
 5272:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5273:                                         @personnel = split(/\&/,$rest);
 5274:                                     }
 5275:                                 } else {
 5276:                                     $curraccess = $access_in_dom{$role};
 5277:                                     if (ref($accessinfo) eq 'HASH') {
 5278:                                         if ($curraccess eq 'status') {
 5279:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5280:                                                 @okstatus = @{$accessinfo->{$role}};
 5281:                                             }
 5282:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5283:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5284:                                                 @personnel = @{$accessinfo->{$role}};
 5285:                                             }
 5286:                                         }
 5287:                                     }
 5288:                                 }
 5289:                                 if ($curraccess eq 'none') {
 5290:                                     next;
 5291:                                 } elsif ($curraccess eq 'all') {
 5292:                                     push(@possroles,$role);
 5293:                                 } elsif ($curraccess eq 'dh') {
 5294:                                     if (grep(/^dh$/,@liveroles)) {
 5295:                                         push(@possroles,$role);
 5296:                                     } else {
 5297:                                         next;
 5298:                                     }
 5299:                                 } elsif ($curraccess eq 'da') {
 5300:                                     if (grep(/^da$/,@liveroles)) {
 5301:                                         push(@possroles,$role);
 5302:                                     } else {
 5303:                                         next;
 5304:                                     }
 5305:                                 } elsif ($curraccess eq 'status') {
 5306:                                     if (@okstatus) {
 5307:                                         if (!@statuses) {
 5308:                                             if (grep(/^default$/,@okstatus)) {
 5309:                                                 push(@possroles,$role);
 5310:                                             }
 5311:                                         } else {
 5312:                                             foreach my $status (@okstatus) {
 5313:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5314:                                                     push(@possroles,$role);
 5315:                                                     last;
 5316:                                                 }
 5317:                                             }
 5318:                                         }
 5319:                                     }
 5320:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5321:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5322:                                         if ($curraccess eq 'exc') {
 5323:                                             push(@possroles,$role);
 5324:                                         }
 5325:                                     } elsif ($curraccess eq 'inc') {
 5326:                                         push(@possroles,$role);
 5327:                                     }
 5328:                                 }
 5329:                             }
 5330:                         }
 5331:                     }
 5332:                 }
 5333:             }
 5334:         }
 5335:     }
 5336:     unless (ref($description) eq 'HASH') {
 5337:         if (ref($roles_by_num) eq 'ARRAY') {
 5338:             my %desc;
 5339:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5340:             $description = \%desc;
 5341:         } else {
 5342:             $description = {};
 5343:         }
 5344:     }
 5345:     return (\@possroles,$description);
 5346: }
 5347: 
 5348: # ----------------------------------------------------- Frontpage Announcements
 5349: #
 5350: #
 5351: 
 5352: sub postannounce {
 5353:     my ($server,$text)=@_;
 5354:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5355:     unless ($text=~/\w/) { $text=''; }
 5356:     return &reply('setannounce:'.&escape($text),$server);
 5357: }
 5358: 
 5359: sub getannounce {
 5360: 
 5361:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5362: 	my $announcement='';
 5363: 	while (my $line = <$fh>) { $announcement .= $line; }
 5364: 	close($fh);
 5365: 	if ($announcement=~/\w/) { 
 5366: 	    return 
 5367:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5368:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5369: 	} else {
 5370: 	    return '';
 5371: 	}
 5372:     } else {
 5373: 	return '';
 5374:     }
 5375: }
 5376: 
 5377: # ---------------------------------------------------------- Course ID routines
 5378: # Deal with domain's nohist_courseid.db files
 5379: #
 5380: 
 5381: sub courseidput {
 5382:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5383:     return unless (ref($storehash) eq 'HASH');
 5384:     my $outcome;
 5385:     if ($caller eq 'timeonly') {
 5386:         my $cids = '';
 5387:         foreach my $item (keys(%$storehash)) {
 5388:             $cids.=&escape($item).'&';
 5389:         }
 5390:         $cids=~s/\&$//;
 5391:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5392:                           $coursehome);       
 5393:     } else {
 5394:         my $items = '';
 5395:         foreach my $item (keys(%$storehash)) {
 5396:             $items.= &escape($item).'='.
 5397:                      &freeze_escape($$storehash{$item}).'&';
 5398:         }
 5399:         $items=~s/\&$//;
 5400:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5401:                           $coursehome);
 5402:     }
 5403:     if ($outcome eq 'unknown_cmd') {
 5404:         my $what;
 5405:         foreach my $cid (keys(%$storehash)) {
 5406:             $what .= &escape($cid).'=';
 5407:             foreach my $item ('description','inst_code','owner','type') {
 5408:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5409:             }
 5410:             $what =~ s/\:$/&/;
 5411:         }
 5412:         $what =~ s/\&$//;  
 5413:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5414:     } else {
 5415:         return $outcome;
 5416:     }
 5417: }
 5418: 
 5419: sub courseiddump {
 5420:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5421:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5422:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5423:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5424:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5425:     my $as_hash = 1;
 5426:     my %returnhash;
 5427:     if (!$domfilter) { $domfilter=''; }
 5428:     my %libserv = &all_library();
 5429:     foreach my $tryserver (keys(%libserv)) {
 5430:         if ( (  $hostidflag == 1 
 5431: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5432: 	     || (!defined($hostidflag)) ) {
 5433: 
 5434: 	    if (($domfilter eq '') ||
 5435: 		(&host_domain($tryserver) eq $domfilter)) {
 5436:                 my $rep;
 5437:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5438:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5439:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5440:                                 &escape($descfilter), &escape($instcodefilter), 
 5441:                                 &escape($ownerfilter), &escape($coursefilter),
 5442:                                 &escape($typefilter), &escape($regexp_ok), 
 5443:                                 $as_hash, &escape($selfenrollonly), 
 5444:                                 &escape($catfilter), $showhidden, $caller, 
 5445:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5446:                                 &escape($createdbefore), &escape($createdafter), 
 5447:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5448:                                 $reqcrsdom,&escape($reqinstcode))));
 5449:                 } else {
 5450:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5451:                              $sincefilter.':'.&escape($descfilter).':'.
 5452:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5453:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5454:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5455:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5456:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5457:                              &escape($cc_clone).':'.$cloneonly.':'.
 5458:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5459:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5460:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5461:                 }
 5462:                      
 5463:                 my @pairs=split(/\&/,$rep);
 5464:                 foreach my $item (@pairs) {
 5465:                     my ($key,$value)=split(/\=/,$item,2);
 5466:                     $key = &unescape($key);
 5467:                     next if ($key =~ /^error: 2 /);
 5468:                     my $result = &thaw_unescape($value);
 5469:                     if (ref($result) eq 'HASH') {
 5470:                         $returnhash{$key}=$result;
 5471:                     } else {
 5472:                         my @responses = split(/:/,$value);
 5473:                         my @items = ('description','inst_code','owner','type');
 5474:                         for (my $i=0; $i<@responses; $i++) {
 5475:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5476:                         }
 5477:                     }
 5478:                 }
 5479:             }
 5480:         }
 5481:     }
 5482:     return %returnhash;
 5483: }
 5484: 
 5485: sub courselastaccess {
 5486:     my ($cdom,$cnum,$hostidref) = @_;
 5487:     my %returnhash;
 5488:     if ($cdom && $cnum) {
 5489:         my $chome = &homeserver($cnum,$cdom);
 5490:         if ($chome ne 'no_host') {
 5491:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5492:             &extract_lastaccess(\%returnhash,$rep);
 5493:         }
 5494:     } else {
 5495:         if (!$cdom) { $cdom=''; }
 5496:         my %libserv = &all_library();
 5497:         foreach my $tryserver (keys(%libserv)) {
 5498:             if (ref($hostidref) eq 'ARRAY') {
 5499:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5500:             } 
 5501:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5502:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5503:                 &extract_lastaccess(\%returnhash,$rep);
 5504:             }
 5505:         }
 5506:     }
 5507:     return %returnhash;
 5508: }
 5509: 
 5510: sub extract_lastaccess {
 5511:     my ($returnhash,$rep) = @_;
 5512:     if (ref($returnhash) eq 'HASH') {
 5513:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5514:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5515:                  $rep eq '') {
 5516:             my @pairs=split(/\&/,$rep);
 5517:             foreach my $item (@pairs) {
 5518:                 my ($key,$value)=split(/\=/,$item,2);
 5519:                 $key = &unescape($key);
 5520:                 next if ($key =~ /^error: 2 /);
 5521:                 $returnhash->{$key} = &thaw_unescape($value);
 5522:             }
 5523:         }
 5524:     }
 5525:     return;
 5526: }
 5527: 
 5528: # ---------------------------------------------------------- DC e-mail
 5529: 
 5530: sub dcmailput {
 5531:     my ($domain,$msgid,$message,$server)=@_;
 5532:     my $status = &Apache::lonnet::critical(
 5533:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5534:        &escape($message),$server);
 5535:     return $status;
 5536: }
 5537: 
 5538: sub dcmaildump {
 5539:     my ($dom,$startdate,$enddate,$senders) = @_;
 5540:     my %returnhash=();
 5541: 
 5542:     if (defined(&domain($dom,'primary'))) {
 5543:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5544:                                                          &escape($enddate).':';
 5545: 	my @esc_senders=map { &escape($_)} @$senders;
 5546: 	$cmd.=&escape(join('&',@esc_senders));
 5547: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5548:             my ($key,$value) = split(/\=/,$line,2);
 5549:             if (($key) && ($value)) {
 5550:                 $returnhash{&unescape($key)} = &unescape($value);
 5551:             }
 5552:         }
 5553:     }
 5554:     return %returnhash;
 5555: }
 5556: # ---------------------------------------------------------- Domain roles
 5557: 
 5558: sub get_domain_roles {
 5559:     my ($dom,$roles,$startdate,$enddate)=@_;
 5560:     if ((!defined($startdate)) || ($startdate eq '')) {
 5561:         $startdate = '.';
 5562:     }
 5563:     if ((!defined($enddate)) || ($enddate eq '')) {
 5564:         $enddate = '.';
 5565:     }
 5566:     my $rolelist;
 5567:     if (ref($roles) eq 'ARRAY') {
 5568:         $rolelist = join('&',@{$roles});
 5569:     }
 5570:     my %personnel = ();
 5571: 
 5572:     my %servers = &get_servers($dom,'library');
 5573:     foreach my $tryserver (keys(%servers)) {
 5574: 	%{$personnel{$tryserver}}=();
 5575: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5576: 					    &escape($startdate).':'.
 5577: 					    &escape($enddate).':'.
 5578: 					    &escape($rolelist), $tryserver))) {
 5579: 	    my ($key,$value) = split(/\=/,$line,2);
 5580: 	    if (($key) && ($value)) {
 5581: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5582: 	    }
 5583: 	}
 5584:     }
 5585:     return %personnel;
 5586: }
 5587: 
 5588: sub get_active_domroles {
 5589:     my ($dom,$roles) = @_;
 5590:     return () unless (ref($roles) eq 'ARRAY');
 5591:     my $now = time;
 5592:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5593:     my %domroles;
 5594:     foreach my $server (keys(%dompersonnel)) {
 5595:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5596:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5597:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5598:         }
 5599:     }
 5600:     return %domroles;
 5601: }
 5602: 
 5603: # ----------------------------------------------------------- Interval timing 
 5604: 
 5605: {
 5606: # Caches needed for speedup of navmaps
 5607: # We don't want to cache this for very long at all (5 seconds at most)
 5608: # 
 5609: # The user for whom we cache
 5610: my $cachedkey='';
 5611: # The cached times for this user
 5612: my %cachedtimes=();
 5613: # When this was last done
 5614: my $cachedtime='';
 5615: 
 5616: sub load_all_first_access {
 5617:     my ($uname,$udom,$ignorecache)=@_;
 5618:     if (($cachedkey eq $uname.':'.$udom) &&
 5619:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5620:         (!$ignorecache)) {
 5621:         return;
 5622:     }
 5623:     $cachedtime=time;
 5624:     $cachedkey=$uname.':'.$udom;
 5625:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5626: }
 5627: 
 5628: sub get_first_access {
 5629:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5630:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5631:     if ($argsymb) { $symb=$argsymb; }
 5632:     my ($map,$id,$res)=&decode_symb($symb);
 5633:     if ($argmap) { $map = $argmap; }
 5634:     if ($type eq 'course') {
 5635: 	$res='course';
 5636:     } elsif ($type eq 'map') {
 5637: 	$res=&symbread($map);
 5638:     } else {
 5639: 	$res=$symb;
 5640:     }
 5641:     &load_all_first_access($uname,$udom,$ignorecache);
 5642:     return $cachedtimes{"$courseid\0$res"};
 5643: }
 5644: 
 5645: sub set_first_access {
 5646:     my ($type,$interval)=@_;
 5647:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5648:     my ($map,$id,$res)=&decode_symb($symb);
 5649:     if ($type eq 'course') {
 5650: 	$res='course';
 5651:     } elsif ($type eq 'map') {
 5652: 	$res=&symbread($map);
 5653:     } else {
 5654: 	$res=$symb;
 5655:     }
 5656:     $cachedkey='';
 5657:     my $firstaccess=&get_first_access($type,$symb,$map);
 5658:     if ($firstaccess) {
 5659:         &logthis("First access time already set ($firstaccess) when attempting ".
 5660:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5661:                  "in $courseid");
 5662:         return 'already_set';
 5663:     } else {
 5664:         my $start = time;
 5665: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5666:                           $udom,$uname);
 5667:         if ($putres eq 'ok') {
 5668:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5669:                  $udom,$uname); 
 5670:             &appenv(
 5671:                      {
 5672:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5673:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5674:                      }
 5675:                   );
 5676:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5677:                 $cachedtimes{"$courseid\0$res"} = $start;
 5678:             }
 5679:         } elsif ($putres ne 'refused') {
 5680:             &logthis("Result: $putres when attempting to set first access time ".
 5681:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5682:         }
 5683:         return $putres;
 5684:     }
 5685:     return 'already_set';
 5686: }
 5687: }
 5688: 
 5689: # --------------------------------------------- Set Expire Date for Spreadsheet
 5690: 
 5691: sub expirespread {
 5692:     my ($uname,$udom,$stype,$usymb)=@_;
 5693:     my $cid=$env{'request.course.id'}; 
 5694:     if ($cid) {
 5695:        my $now=time;
 5696:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5697:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5698:                             $env{'course.'.$cid.'.num'}.
 5699: 	        	    ':nohist_expirationdates:'.
 5700:                             &escape($key).'='.$now,
 5701:                             $env{'course.'.$cid.'.home'})
 5702:     }
 5703:     return 'ok';
 5704: }
 5705: 
 5706: # ----------------------------------------------------- Devalidate Spreadsheets
 5707: 
 5708: sub devalidate {
 5709:     my ($symb,$uname,$udom)=@_;
 5710:     my $cid=$env{'request.course.id'}; 
 5711:     if ($cid) {
 5712:         # delete the stored spreadsheets for
 5713:         # - the student level sheet of this user in course's homespace
 5714:         # - the assessment level sheet for this resource 
 5715:         #   for this user in user's homespace
 5716: 	# - current conditional state info
 5717: 	my $key=$uname.':'.$udom.':';
 5718:         my $status=
 5719: 	    &del('nohist_calculatedsheets',
 5720: 		 [$key.'studentcalc:'],
 5721: 		 $env{'course.'.$cid.'.domain'},
 5722: 		 $env{'course.'.$cid.'.num'})
 5723: 		.' '.
 5724: 	    &del('nohist_calculatedsheets_'.$cid,
 5725: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5726:         unless ($status eq 'ok ok') {
 5727:            &logthis('Could not devalidate spreadsheet '.
 5728:                     $uname.' at '.$udom.' for '.
 5729: 		    $symb.': '.$status);
 5730:         }
 5731: 	&delenv('user.state.'.$cid);
 5732:     }
 5733: }
 5734: 
 5735: sub get_scalar {
 5736:     my ($string,$end) = @_;
 5737:     my $value;
 5738:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5739: 	$value = $1;
 5740:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5741: 	$value = $1;
 5742:     }
 5743:     return &unescape($value);
 5744: }
 5745: 
 5746: sub array2str {
 5747:   my (@array) = @_;
 5748:   my $result=&arrayref2str(\@array);
 5749:   $result=~s/^__ARRAY_REF__//;
 5750:   $result=~s/__END_ARRAY_REF__$//;
 5751:   return $result;
 5752: }
 5753: 
 5754: sub arrayref2str {
 5755:   my ($arrayref) = @_;
 5756:   my $result='__ARRAY_REF__';
 5757:   foreach my $elem (@$arrayref) {
 5758:     if(ref($elem) eq 'ARRAY') {
 5759:       $result.=&arrayref2str($elem).'&';
 5760:     } elsif(ref($elem) eq 'HASH') {
 5761:       $result.=&hashref2str($elem).'&';
 5762:     } elsif(ref($elem)) {
 5763:       #print("Got a ref of ".(ref($elem))." skipping.");
 5764:     } else {
 5765:       $result.=&escape($elem).'&';
 5766:     }
 5767:   }
 5768:   $result=~s/\&$//;
 5769:   $result .= '__END_ARRAY_REF__';
 5770:   return $result;
 5771: }
 5772: 
 5773: sub hash2str {
 5774:   my (%hash) = @_;
 5775:   my $result=&hashref2str(\%hash);
 5776:   $result=~s/^__HASH_REF__//;
 5777:   $result=~s/__END_HASH_REF__$//;
 5778:   return $result;
 5779: }
 5780: 
 5781: sub hashref2str {
 5782:   my ($hashref)=@_;
 5783:   my $result='__HASH_REF__';
 5784:   foreach my $key (sort(keys(%$hashref))) {
 5785:     if (ref($key) eq 'ARRAY') {
 5786:       $result.=&arrayref2str($key).'=';
 5787:     } elsif (ref($key) eq 'HASH') {
 5788:       $result.=&hashref2str($key).'=';
 5789:     } elsif (ref($key)) {
 5790:       $result.='=';
 5791:       #print("Got a ref of ".(ref($key))." skipping.");
 5792:     } else {
 5793: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 5794:     }
 5795: 
 5796:     if(ref($hashref->{$key}) eq 'ARRAY') {
 5797:       $result.=&arrayref2str($hashref->{$key}).'&';
 5798:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 5799:       $result.=&hashref2str($hashref->{$key}).'&';
 5800:     } elsif(ref($hashref->{$key})) {
 5801:        $result.='&';
 5802:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 5803:     } else {
 5804:       $result.=&escape($hashref->{$key}).'&';
 5805:     }
 5806:   }
 5807:   $result=~s/\&$//;
 5808:   $result .= '__END_HASH_REF__';
 5809:   return $result;
 5810: }
 5811: 
 5812: sub str2hash {
 5813:     my ($string)=@_;
 5814:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 5815:     return %$hash;
 5816: }
 5817: 
 5818: sub str2hashref {
 5819:   my ($string) = @_;
 5820: 
 5821:   my %hash;
 5822: 
 5823:   if($string !~ /^__HASH_REF__/) {
 5824:       if (! ($string eq '' || !defined($string))) {
 5825: 	  $hash{'error'}='Not hash reference';
 5826:       }
 5827:       return (\%hash, $string);
 5828:   }
 5829: 
 5830:   $string =~ s/^__HASH_REF__//;
 5831: 
 5832:   while($string !~ /^__END_HASH_REF__/) {
 5833:       #key
 5834:       my $key='';
 5835:       if($string =~ /^__HASH_REF__/) {
 5836:           ($key, $string)=&str2hashref($string);
 5837:           if(defined($key->{'error'})) {
 5838:               $hash{'error'}='Bad data';
 5839:               return (\%hash, $string);
 5840:           }
 5841:       } elsif($string =~ /^__ARRAY_REF__/) {
 5842:           ($key, $string)=&str2arrayref($string);
 5843:           if($key->[0] eq 'Array reference error') {
 5844:               $hash{'error'}='Bad data';
 5845:               return (\%hash, $string);
 5846:           }
 5847:       } else {
 5848:           $string =~ s/^(.*?)=//;
 5849: 	  $key=&unescape($1);
 5850:       }
 5851:       $string =~ s/^=//;
 5852: 
 5853:       #value
 5854:       my $value='';
 5855:       if($string =~ /^__HASH_REF__/) {
 5856:           ($value, $string)=&str2hashref($string);
 5857:           if(defined($value->{'error'})) {
 5858:               $hash{'error'}='Bad data';
 5859:               return (\%hash, $string);
 5860:           }
 5861:       } elsif($string =~ /^__ARRAY_REF__/) {
 5862:           ($value, $string)=&str2arrayref($string);
 5863:           if($value->[0] eq 'Array reference error') {
 5864:               $hash{'error'}='Bad data';
 5865:               return (\%hash, $string);
 5866:           }
 5867:       } else {
 5868: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 5869:       }
 5870:       $string =~ s/^&//;
 5871: 
 5872:       $hash{$key}=$value;
 5873:   }
 5874: 
 5875:   $string =~ s/^__END_HASH_REF__//;
 5876: 
 5877:   return (\%hash, $string);
 5878: }
 5879: 
 5880: sub str2array {
 5881:     my ($string)=@_;
 5882:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 5883:     return @$array;
 5884: }
 5885: 
 5886: sub str2arrayref {
 5887:   my ($string) = @_;
 5888:   my @array;
 5889: 
 5890:   if($string !~ /^__ARRAY_REF__/) {
 5891:       if (! ($string eq '' || !defined($string))) {
 5892: 	  $array[0]='Array reference error';
 5893:       }
 5894:       return (\@array, $string);
 5895:   }
 5896: 
 5897:   $string =~ s/^__ARRAY_REF__//;
 5898: 
 5899:   while($string !~ /^__END_ARRAY_REF__/) {
 5900:       my $value='';
 5901:       if($string =~ /^__HASH_REF__/) {
 5902:           ($value, $string)=&str2hashref($string);
 5903:           if(defined($value->{'error'})) {
 5904:               $array[0] ='Array reference error';
 5905:               return (\@array, $string);
 5906:           }
 5907:       } elsif($string =~ /^__ARRAY_REF__/) {
 5908:           ($value, $string)=&str2arrayref($string);
 5909:           if($value->[0] eq 'Array reference error') {
 5910:               $array[0] ='Array reference error';
 5911:               return (\@array, $string);
 5912:           }
 5913:       } else {
 5914: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 5915:       }
 5916:       $string =~ s/^&//;
 5917: 
 5918:       push(@array, $value);
 5919:   }
 5920: 
 5921:   $string =~ s/^__END_ARRAY_REF__//;
 5922: 
 5923:   return (\@array, $string);
 5924: }
 5925: 
 5926: # -------------------------------------------------------------------Temp Store
 5927: 
 5928: sub tmpreset {
 5929:   my ($symb,$namespace,$domain,$stuname) = @_;
 5930:   if (!$symb) {
 5931:     $symb=&symbread();
 5932:     if (!$symb) { $symb= $env{'request.url'}; }
 5933:   }
 5934:   $symb=escape($symb);
 5935: 
 5936:   if (!$namespace) { $namespace=$env{'request.state'}; }
 5937:   $namespace=~s/\//\_/g;
 5938:   $namespace=~s/\W//g;
 5939: 
 5940:   if (!$domain) { $domain=$env{'user.domain'}; }
 5941:   if (!$stuname) { $stuname=$env{'user.name'}; }
 5942:   if ($domain eq 'public' && $stuname eq 'public') {
 5943:       $stuname=$ENV{'REMOTE_ADDR'};
 5944:   }
 5945:   my $path=LONCAPA::tempdir();
 5946:   my %hash;
 5947:   if (tie(%hash,'GDBM_File',
 5948: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 5949: 	  &GDBM_WRCREAT(),0640)) {
 5950:     foreach my $key (keys(%hash)) {
 5951:       if ($key=~ /:$symb/) {
 5952: 	delete($hash{$key});
 5953:       }
 5954:     }
 5955:   }
 5956: }
 5957: 
 5958: sub tmpstore {
 5959:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 5960: 
 5961:   if (!$symb) {
 5962:     $symb=&symbread();
 5963:     if (!$symb) { $symb= $env{'request.url'}; }
 5964:   }
 5965:   $symb=escape($symb);
 5966: 
 5967:   if (!$namespace) {
 5968:     # I don't think we would ever want to store this for a course.
 5969:     # it seems this will only be used if we don't have a course.
 5970:     #$namespace=$env{'request.course.id'};
 5971:     #if (!$namespace) {
 5972:       $namespace=$env{'request.state'};
 5973:     #}
 5974:   }
 5975:   $namespace=~s/\//\_/g;
 5976:   $namespace=~s/\W//g;
 5977:   if (!$domain) { $domain=$env{'user.domain'}; }
 5978:   if (!$stuname) { $stuname=$env{'user.name'}; }
 5979:   if ($domain eq 'public' && $stuname eq 'public') {
 5980:       $stuname=$ENV{'REMOTE_ADDR'};
 5981:   }
 5982:   my $now=time;
 5983:   my %hash;
 5984:   my $path=LONCAPA::tempdir();
 5985:   if (tie(%hash,'GDBM_File',
 5986: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 5987: 	  &GDBM_WRCREAT(),0640)) {
 5988:     $hash{"version:$symb"}++;
 5989:     my $version=$hash{"version:$symb"};
 5990:     my $allkeys=''; 
 5991:     foreach my $key (keys(%$storehash)) {
 5992:       $allkeys.=$key.':';
 5993:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 5994:     }
 5995:     $hash{"$version:$symb:timestamp"}=$now;
 5996:     $allkeys.='timestamp';
 5997:     $hash{"$version:keys:$symb"}=$allkeys;
 5998:     if (untie(%hash)) {
 5999:       return 'ok';
 6000:     } else {
 6001:       return "error:$!";
 6002:     }
 6003:   } else {
 6004:     return "error:$!";
 6005:   }
 6006: }
 6007: 
 6008: # -----------------------------------------------------------------Temp Restore
 6009: 
 6010: sub tmprestore {
 6011:   my ($symb,$namespace,$domain,$stuname) = @_;
 6012: 
 6013:   if (!$symb) {
 6014:     $symb=&symbread();
 6015:     if (!$symb) { $symb= $env{'request.url'}; }
 6016:   }
 6017:   $symb=escape($symb);
 6018: 
 6019:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6020: 
 6021:   if (!$domain) { $domain=$env{'user.domain'}; }
 6022:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6023:   if ($domain eq 'public' && $stuname eq 'public') {
 6024:       $stuname=$ENV{'REMOTE_ADDR'};
 6025:   }
 6026:   my %returnhash;
 6027:   $namespace=~s/\//\_/g;
 6028:   $namespace=~s/\W//g;
 6029:   my %hash;
 6030:   my $path=LONCAPA::tempdir();
 6031:   if (tie(%hash,'GDBM_File',
 6032: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6033: 	  &GDBM_READER(),0640)) {
 6034:     my $version=$hash{"version:$symb"};
 6035:     $returnhash{'version'}=$version;
 6036:     my $scope;
 6037:     for ($scope=1;$scope<=$version;$scope++) {
 6038:       my $vkeys=$hash{"$scope:keys:$symb"};
 6039:       my @keys=split(/:/,$vkeys);
 6040:       my $key;
 6041:       $returnhash{"$scope:keys"}=$vkeys;
 6042:       foreach $key (@keys) {
 6043: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6044: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6045:       }
 6046:     }
 6047:     if (!(untie(%hash))) {
 6048:       return "error:$!";
 6049:     }
 6050:   } else {
 6051:     return "error:$!";
 6052:   }
 6053:   return %returnhash;
 6054: }
 6055: 
 6056: # ----------------------------------------------------------------------- Store
 6057: 
 6058: sub store {
 6059:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6060:     my $home='';
 6061: 
 6062:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6063: 
 6064:     $symb=&symbclean($symb);
 6065:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6066: 
 6067:     if (!$domain) { $domain=$env{'user.domain'}; }
 6068:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6069: 
 6070:     &devalidate($symb,$stuname,$domain);
 6071: 
 6072:     $symb=escape($symb);
 6073:     if (!$namespace) { 
 6074:        unless ($namespace=$env{'request.course.id'}) { 
 6075:           return ''; 
 6076:        } 
 6077:     }
 6078:     if (!$home) { $home=$env{'user.home'}; }
 6079: 
 6080:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6081:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6082: 
 6083:     my $namevalue='';
 6084:     foreach my $key (keys(%$storehash)) {
 6085:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6086:     }
 6087:     $namevalue=~s/\&$//;
 6088:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6089:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6090: }
 6091: 
 6092: # -------------------------------------------------------------- Critical Store
 6093: 
 6094: sub cstore {
 6095:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6096:     my $home='';
 6097: 
 6098:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6099: 
 6100:     $symb=&symbclean($symb);
 6101:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6102: 
 6103:     if (!$domain) { $domain=$env{'user.domain'}; }
 6104:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6105: 
 6106:     &devalidate($symb,$stuname,$domain);
 6107: 
 6108:     $symb=escape($symb);
 6109:     if (!$namespace) { 
 6110:        unless ($namespace=$env{'request.course.id'}) { 
 6111:           return ''; 
 6112:        } 
 6113:     }
 6114:     if (!$home) { $home=$env{'user.home'}; }
 6115: 
 6116:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6117:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6118: 
 6119:     my $namevalue='';
 6120:     foreach my $key (keys(%$storehash)) {
 6121:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6122:     }
 6123:     $namevalue=~s/\&$//;
 6124:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6125:     return critical
 6126:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6127: }
 6128: 
 6129: # --------------------------------------------------------------------- Restore
 6130: 
 6131: sub restore {
 6132:     my ($symb,$namespace,$domain,$stuname) = @_;
 6133:     my $home='';
 6134: 
 6135:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6136: 
 6137:     if (!$symb) {
 6138:         return if ($namespace eq 'courserequests');
 6139:         unless ($symb=escape(&symbread())) { return ''; }
 6140:     } else {
 6141:         unless ($namespace eq 'courserequests') {
 6142:             $symb=&escape(&symbclean($symb));
 6143:         }
 6144:     }
 6145:     if (!$namespace) { 
 6146:        unless ($namespace=$env{'request.course.id'}) { 
 6147:           return ''; 
 6148:        } 
 6149:     }
 6150:     if (!$domain) { $domain=$env{'user.domain'}; }
 6151:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6152:     if (!$home) { $home=$env{'user.home'}; }
 6153:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6154: 
 6155:     my %returnhash=();
 6156:     foreach my $line (split(/\&/,$answer)) {
 6157: 	my ($name,$value)=split(/\=/,$line);
 6158:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6159:     }
 6160:     my $version;
 6161:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6162:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6163:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6164:        }
 6165:     }
 6166:     return %returnhash;
 6167: }
 6168: 
 6169: # ---------------------------------------------------------- Course Description
 6170: #
 6171: #  
 6172: 
 6173: sub coursedescription {
 6174:     my ($courseid,$args)=@_;
 6175:     $courseid=~s/^\///;
 6176:     $courseid=~s/\_/\//g;
 6177:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6178:     my $chome=&homeserver($cnum,$cdomain);
 6179:     my $normalid=$cdomain.'_'.$cnum;
 6180:     # need to always cache even if we get errors otherwise we keep 
 6181:     # trying and trying and trying to get the course description.
 6182:     my %envhash=();
 6183:     my %returnhash=();
 6184:     
 6185:     my $expiretime=600;
 6186:     if ($env{'request.course.id'} eq $normalid) {
 6187: 	$expiretime=120;
 6188:     }
 6189: 
 6190:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6191:     if (!$args->{'freshen_cache'}
 6192: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6193: 	foreach my $key (keys(%env)) {
 6194: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6195: 	    my ($setting) = $1;
 6196: 	    $returnhash{$setting} = $env{$key};
 6197: 	}
 6198: 	return %returnhash;
 6199:     }
 6200: 
 6201:     # get the data again
 6202: 
 6203:     if (!$args->{'one_time'}) {
 6204: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6205:     }
 6206: 
 6207:     if ($chome ne 'no_host') {
 6208:        %returnhash=&dump('environment',$cdomain,$cnum);
 6209:        if (!exists($returnhash{'con_lost'})) {
 6210: 	   my $username = $env{'user.name'}; # Defult username
 6211: 	   if(defined $args->{'user'}) {
 6212: 	       $username = $args->{'user'};
 6213: 	   }
 6214:            $returnhash{'home'}= $chome;
 6215: 	   $returnhash{'domain'} = $cdomain;
 6216: 	   $returnhash{'num'} = $cnum;
 6217:            if (!defined($returnhash{'type'})) {
 6218:                $returnhash{'type'} = 'Course';
 6219:            }
 6220:            while (my ($name,$value) = each %returnhash) {
 6221:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6222:            }
 6223:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6224:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6225: 	       $username.'_'.$cdomain.'_'.$cnum;
 6226:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6227:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6228:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6229:        }
 6230:     }
 6231:     if (!$args->{'one_time'}) {
 6232: 	&appenv(\%envhash);
 6233:     }
 6234:     return %returnhash;
 6235: }
 6236: 
 6237: sub update_released_required {
 6238:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6239:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6240:         $cid = $env{'request.course.id'};
 6241:         $cdom = $env{'course.'.$cid.'.domain'};
 6242:         $cnum = $env{'course.'.$cid.'.num'};
 6243:         $chome = $env{'course.'.$cid.'.home'};
 6244:     }
 6245:     if ($needsrelease) {
 6246:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6247:         my $needsupdate;
 6248:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6249:             $needsupdate = 1;
 6250:         } else {
 6251:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6252:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6253:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6254:                 $needsupdate = 1;
 6255:             }
 6256:         }
 6257:         if ($needsupdate) {
 6258:             my %needshash = (
 6259:                              'internal.releaserequired' => $needsrelease,
 6260:                             );
 6261:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6262:             if ($putresult eq 'ok') {
 6263:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6264:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6265:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6266:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6267:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6268:                 }
 6269:             }
 6270:         }
 6271:     }
 6272:     return;
 6273: }
 6274: 
 6275: # -------------------------------------------------See if a user is privileged
 6276: 
 6277: sub privileged {
 6278:     my ($username,$domain,$possdomains,$possroles)=@_;
 6279:     my $now = time;
 6280:     my $roles;
 6281:     if (ref($possroles) eq 'ARRAY') {
 6282:         $roles = $possroles; 
 6283:     } else {
 6284:         $roles = ['dc','su'];
 6285:     }
 6286:     if (ref($possdomains) eq 'ARRAY') {
 6287:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6288:         foreach my $dom (@{$possdomains}) {
 6289:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6290:                 (ref($privileged{$dom}) eq 'HASH')) {
 6291:                 foreach my $role (@{$roles}) {
 6292:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6293:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6294:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6295:                             return 1 unless (($end && $end < $now) ||
 6296:                                              ($start && $start > $now));
 6297:                         }
 6298:                     }
 6299:                 }
 6300:             }
 6301:         }
 6302:     } else {
 6303:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6304:         my $now = time;
 6305: 
 6306:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6307:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6308:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6309:                 return 1 unless ($tend && $tend < $now) 
 6310:                         or ($tstart && $tstart > $now);
 6311:             }
 6312:         }
 6313:     }
 6314:     return 0;
 6315: }
 6316: 
 6317: sub privileged_by_domain {
 6318:     my ($domains,$roles) = @_;
 6319:     my %privileged = ();
 6320:     my $cachetime = 60*60*24;
 6321:     my $now = time;
 6322:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6323:         return %privileged;
 6324:     }
 6325:     foreach my $dom (@{$domains}) {
 6326:         next if (ref($privileged{$dom}) eq 'HASH');
 6327:         my $needroles;
 6328:         foreach my $role (@{$roles}) {
 6329:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6330:             if (defined($cached)) {
 6331:                 if (ref($result) eq 'HASH') {
 6332:                     $privileged{$dom}{$role} = $result;
 6333:                 }
 6334:             } else {
 6335:                 $needroles = 1;
 6336:             }
 6337:         }
 6338:         if ($needroles) {
 6339:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6340:             $privileged{$dom} = {};
 6341:             foreach my $server (keys(%dompersonnel)) {
 6342:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6343:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6344:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6345:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6346:                         next if ($end && $end < $now);
 6347:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6348:                             $dompersonnel{$server}{$item};
 6349:                     }
 6350:                 }
 6351:             }
 6352:             if (ref($privileged{$dom}) eq 'HASH') {
 6353:                 foreach my $role (@{$roles}) {
 6354:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6355:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6356:                     } else {
 6357:                         my %hash = ();
 6358:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6359:                     }
 6360:                 }
 6361:             }
 6362:         }
 6363:     }
 6364:     return %privileged;
 6365: }
 6366: 
 6367: # -------------------------------------------------------- Get user privileges
 6368: 
 6369: sub rolesinit {
 6370:     my ($domain, $username) = @_;
 6371:     my %userroles = ('user.login.time' => time);
 6372:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6373: 
 6374:     # firstaccess and timerinterval are related to timed maps/resources. 
 6375:     # also, blocking can be triggered by an activating timer
 6376:     # it's saved in the user's %env.
 6377:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6378:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6379:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6380:         %timerintchk, %timerintenv);
 6381: 
 6382:     foreach my $key (keys(%firstaccess)) {
 6383:         my ($cid, $rest) = split(/\0/, $key);
 6384:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6385:     }
 6386: 
 6387:     foreach my $key (keys(%timerinterval)) {
 6388:         my ($cid,$rest) = split(/\0/,$key);
 6389:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6390:     }
 6391: 
 6392:     my %allroles=();
 6393:     my %allgroups=();
 6394: 
 6395:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6396:         my $role = $rolesdump{$area};
 6397:         $area =~ s/\_\w\w$//;
 6398: 
 6399:         my ($trole, $tend, $tstart, $group_privs);
 6400: 
 6401:         if ($role =~ /^cr/) {
 6402:         # Custom role, defined by a user 
 6403:         # e.g., user.role.cr/msu/smith/mynewrole
 6404:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6405:                 $trole = $1;
 6406:                 ($tend, $tstart) = split('_', $2);
 6407:             } else {
 6408:                 $trole = $role;
 6409:             }
 6410:         } elsif ($role =~ m|^gr/|) {
 6411:         # Role of member in a group, defined within a course/community
 6412:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6413:             ($trole, $tend, $tstart) = split(/_/, $role);
 6414:             next if $tstart eq '-1';
 6415:             ($trole, $group_privs) = split(/\//, $trole);
 6416:             $group_privs = &unescape($group_privs);
 6417:         } else {
 6418:         # Just a normal role, defined in roles.tab
 6419:             ($trole, $tend, $tstart) = split(/_/,$role);
 6420:         }
 6421: 
 6422:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6423:                  $username);
 6424:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6425: 
 6426:         # role expired or not available yet?
 6427:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6428:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6429: 
 6430:         next if $area eq '' or $trole eq '';
 6431: 
 6432:         my $spec = "$trole.$area";
 6433:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6434: 
 6435:         if ($trole =~ /^cr\//) {
 6436:         # Custom role, defined by a user
 6437:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6438:         } elsif ($trole eq 'gr') {
 6439:         # Role of a member in a group, defined within a course/community
 6440:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6441:             next;
 6442:         } else {
 6443:         # Normal role, defined in roles.tab
 6444:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6445:         }
 6446: 
 6447:         my $cid = $tdomain.'_'.$trest;
 6448:         unless ($firstaccchk{$cid}) {
 6449:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6450:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6451:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6452:                         $coursetimerstarts{$cid}{$item}; 
 6453:                 }
 6454:             }
 6455:             $firstaccchk{$cid} = 1;
 6456:         }
 6457:         unless ($timerintchk{$cid}) {
 6458:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6459:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6460:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6461:                        $coursetimerintervals{$cid}{$item};
 6462:                 }
 6463:             }
 6464:             $timerintchk{$cid} = 1;
 6465:         }
 6466:     }
 6467: 
 6468:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6469:                                                           \%allroles, \%allgroups);
 6470:     $env{'user.adv'} = $userroles{'user.adv'};
 6471:     $env{'user.rar'} = $userroles{'user.rar'};
 6472: 
 6473:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6474: }
 6475: 
 6476: sub set_arearole {
 6477:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6478:     unless ($nolog) {
 6479: # log the associated role with the area
 6480:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6481:     }
 6482:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6483: }
 6484: 
 6485: sub custom_roleprivs {
 6486:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6487:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6488:     my $homsvr = &homeserver($rauthor,$rdomain);
 6489:     if (&hostname($homsvr) ne '') {
 6490:         my ($rdummy,$roledef)=
 6491:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6492:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6493:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6494:             if (defined($syspriv)) {
 6495:                 if ($trest =~ /^$match_community$/) {
 6496:                     $syspriv =~ s/bre\&S//; 
 6497:                 }
 6498:                 $$allroles{'cm./'}.=':'.$syspriv;
 6499:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6500:             }
 6501:             if ($tdomain ne '') {
 6502:                 if (defined($dompriv)) {
 6503:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6504:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6505:                 }
 6506:                 if (($trest ne '') && (defined($coursepriv))) {
 6507:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6508:                         my $rolename = $1;
 6509:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6510:                     }
 6511:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6512:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6513:                 }
 6514:             }
 6515:         }
 6516:     }
 6517: }
 6518: 
 6519: sub course_adhocrole_privs {
 6520:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6521:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6522:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6523:         my (%currprivs,%storeprivs);
 6524:         foreach my $item (split(/:/,$coursepriv)) {
 6525:             my ($priv,$restrict) = split(/\&/,$item);
 6526:             $currprivs{$priv} = $restrict;
 6527:         }
 6528:         my (%possadd,%possremove,%full);
 6529:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6530:             my ($priv,$restrict)=split(/\&/,$item);
 6531:             $full{$priv} = $restrict;
 6532:         }
 6533:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6534:              next if ($item eq '');
 6535:              my ($rule,$rest) = split(/=/,$item);
 6536:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6537:              foreach my $priv (split(/:/,$rest)) {
 6538:                  if ($priv ne '') {
 6539:                      if ($rule eq 'off') {
 6540:                          $possremove{$priv} = 1;
 6541:                      } else {
 6542:                          $possadd{$priv} = 1;
 6543:                      }
 6544:                  }
 6545:              }
 6546:          }
 6547:          foreach my $priv (sort(keys(%full))) {
 6548:              if (exists($currprivs{$priv})) {
 6549:                  unless (exists($possremove{$priv})) {
 6550:                      $storeprivs{$priv} = $currprivs{$priv};
 6551:                  }
 6552:              } elsif (exists($possadd{$priv})) {
 6553:                  $storeprivs{$priv} = $full{$priv};
 6554:              }
 6555:          }
 6556:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6557:      }
 6558:      return $coursepriv;
 6559: }
 6560: 
 6561: sub group_roleprivs {
 6562:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6563:     my $access = 1;
 6564:     my $now = time;
 6565:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6566:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6567:     if ($access) {
 6568:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6569:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6570:     }
 6571: }
 6572: 
 6573: sub standard_roleprivs {
 6574:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6575:     if (defined($pr{$trole.':s'})) {
 6576:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6577:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6578:     }
 6579:     if ($tdomain ne '') {
 6580:         if (defined($pr{$trole.':d'})) {
 6581:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6582:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6583:         }
 6584:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6585:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6586:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6587:         }
 6588:     }
 6589: }
 6590: 
 6591: sub set_userprivs {
 6592:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6593:     my $author=0;
 6594:     my $adv=0;
 6595:     my $rar=0;
 6596:     my %grouproles = ();
 6597:     if (keys(%{$allgroups}) > 0) {
 6598:         my @groupkeys; 
 6599:         foreach my $role (keys(%{$allroles})) {
 6600:             push(@groupkeys,$role);
 6601:         }
 6602:         if (ref($groups_roles) eq 'HASH') {
 6603:             foreach my $key (keys(%{$groups_roles})) {
 6604:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6605:                     push(@groupkeys,$key);
 6606:                 }
 6607:             }
 6608:         }
 6609:         if (@groupkeys > 0) {
 6610:             foreach my $role (@groupkeys) {
 6611:                 my ($trole,$area,$sec,$extendedarea);
 6612:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6613:                     $trole = $1;
 6614:                     $area = $2;
 6615:                     $sec = $3;
 6616:                     $extendedarea = $area.$sec;
 6617:                     if (exists($$allgroups{$area})) {
 6618:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6619:                             my $spec = $trole.'.'.$extendedarea;
 6620:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6621:                                                 $$allgroups{$area}{$group};
 6622:                         }
 6623:                     }
 6624:                 }
 6625:             }
 6626:         }
 6627:     }
 6628:     foreach my $group (keys(%grouproles)) {
 6629:         $$allroles{$group} = $grouproles{$group};
 6630:     }
 6631:     foreach my $role (keys(%{$allroles})) {
 6632:         my %thesepriv;
 6633:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6634:         foreach my $item (split(/:/,$$allroles{$role})) {
 6635:             if ($item ne '') {
 6636:                 my ($privilege,$restrictions)=split(/&/,$item);
 6637:                 if ($restrictions eq '') {
 6638:                     $thesepriv{$privilege}='F';
 6639:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6640:                     $thesepriv{$privilege}.=$restrictions;
 6641:                 }
 6642:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6643:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6644:             }
 6645:         }
 6646:         my $thesestr='';
 6647:         foreach my $priv (sort(keys(%thesepriv))) {
 6648: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6649: 	}
 6650:         $userroles->{'user.priv.'.$role} = $thesestr;
 6651:     }
 6652:     return ($author,$adv,$rar);
 6653: }
 6654: 
 6655: sub role_status {
 6656:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6657:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6658:         my ($one,$two) = split(m{\./},$rolekey,2);
 6659:         (undef,undef,$$role) = split(/\./,$one,3);
 6660:         unless (!defined($$role) || $$role eq '') {
 6661:             $$where = '/'.$two;
 6662:             $$trolecode=$$role.'.'.$$where;
 6663:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6664:             $$tstatus='is';
 6665:             if ($$tstart && $$tstart>$update) {
 6666:                 $$tstatus='future';
 6667:                 if ($$tstart<$now) {
 6668:                     if ($$tstart && $$tstart>$refresh) {
 6669:                         if (($$where ne '') && ($$role ne '')) {
 6670:                             my (%allroles,%allgroups,$group_privs,
 6671:                                 %groups_roles,@rolecodes);
 6672:                             my %userroles = (
 6673:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6674:                             );
 6675:                             @rolecodes = ('cm'); 
 6676:                             my $spec=$$role.'.'.$$where;
 6677:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6678:                             if ($$role =~ /^cr\//) {
 6679:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6680:                                 push(@rolecodes,'cr');
 6681:                             } elsif ($$role eq 'gr') {
 6682:                                 push(@rolecodes,$$role);
 6683:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6684:                                                     $env{'user.name'});
 6685:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6686:                                 (undef,my $group_privs) = split(/\//,$trole);
 6687:                                 $group_privs = &unescape($group_privs);
 6688:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6689:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6690:                                 &get_groups_roles($tdomain,$trest,
 6691:                                                   \%course_roles,\@rolecodes,
 6692:                                                   \%groups_roles);
 6693:                             } else {
 6694:                                 push(@rolecodes,$$role);
 6695:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6696:                             }
 6697:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6698:                                                                    \%groups_roles);
 6699:                             &appenv(\%userroles,\@rolecodes);
 6700:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6701:                         }
 6702:                     }
 6703:                     $$tstatus = 'is';
 6704:                 }
 6705:             }
 6706:             if ($$tend) {
 6707:                 if ($$tend<$update) {
 6708:                     $$tstatus='expired';
 6709:                 } elsif ($$tend<$now) {
 6710:                     $$tstatus='will_not';
 6711:                 }
 6712:             }
 6713:         }
 6714:     }
 6715: }
 6716: 
 6717: sub get_groups_roles {
 6718:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6719:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6720:                   (ref($rolecodes) eq 'ARRAY') && 
 6721:                   (ref($groups_roles) eq 'HASH')); 
 6722:     if (keys(%{$cdom_courseroles}) > 0) {
 6723:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6724:         if ($cdom ne '' && $cnum ne '') {
 6725:             foreach my $key (keys(%{$cdom_courseroles})) {
 6726:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6727:                     my $crsrole = $1;
 6728:                     my $crssec = $2;
 6729:                     if ($crsrole =~ /^cr/) {
 6730:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6731:                             push(@{$rolecodes},'cr');
 6732:                         }
 6733:                     } else {
 6734:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6735:                             push(@{$rolecodes},$crsrole);
 6736:                         }
 6737:                     }
 6738:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6739:                     if ($crssec ne '') {
 6740:                         $rolekey .= "/$crssec";
 6741:                     }
 6742:                     $rolekey .= './';
 6743:                     $groups_roles->{$rolekey} = $rolecodes;
 6744:                 }
 6745:             }
 6746:         }
 6747:     }
 6748:     return;
 6749: }
 6750: 
 6751: sub delete_env_groupprivs {
 6752:     my ($where,$courseroles,$possroles) = @_;
 6753:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6754:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6755:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6756:         %{$courseroles->{$udom}} =
 6757:             &get_my_roles('','','userroles',['active'],
 6758:                           $possroles,[$udom],1);
 6759:     }
 6760:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6761:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6762:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6763:             my $area = '/'.$cdom.'/'.$cnum;
 6764:             my $privkey = "user.priv.$crsrole.$area";
 6765:             if ($crssec ne '') {
 6766:                 $privkey .= '/'.$crssec;
 6767:             }
 6768:             $privkey .= ".$area/$group";
 6769:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6770:         }
 6771:     }
 6772:     return;
 6773: }
 6774: 
 6775: sub check_adhoc_privs {
 6776:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6777:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6778:     if ($sec) {
 6779:         $cckey .= '/'.$sec;
 6780:     } 
 6781:     my $setprivs;
 6782:     if ($env{$cckey}) {
 6783:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 6784:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 6785:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 6786:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6787:             $setprivs = 1;
 6788:         }
 6789:     } else {
 6790:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6791:         $setprivs = 1;
 6792:     }
 6793:     return $setprivs;
 6794: }
 6795: 
 6796: sub set_adhoc_privileges {
 6797: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 6798:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 6799:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 6800:     if ($sec ne '') {
 6801:         $area .= '/'.$sec;
 6802:     }
 6803:     my $spec = $role.'.'.$area;
 6804:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 6805:                                   $env{'user.name'},1);
 6806:     my %rolehash = ();
 6807:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 6808:         my $rolename = $1;
 6809:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 6810:         my %domdef = &get_domain_defaults($dcdom);
 6811:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 6812:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 6813:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 6814:             }
 6815:         }
 6816:     } else {
 6817:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 6818:     }
 6819:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 6820:     &appenv(\%userroles,[$role,'cm']);
 6821:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6822:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 6823:             ($caller eq 'tiny')) {
 6824:         &appenv( {'request.role'        => $spec,
 6825:                   'request.role.domain' => $dcdom,
 6826:                   'request.course.sec'  => $sec,
 6827:                  }
 6828:                );
 6829:         my $tadv=0;
 6830:         if (&allowed('adv') eq 'F') { $tadv=1; }
 6831:         &appenv({'request.role.adv'    => $tadv});
 6832:     }
 6833: }
 6834: 
 6835: # --------------------------------------------------------------- get interface
 6836: 
 6837: sub get {
 6838:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6839:    my $items='';
 6840:    foreach my $item (@$storearr) {
 6841:        $items.=&escape($item).'&';
 6842:    }
 6843:    $items=~s/\&$//;
 6844:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6845:    if (!$uname) { $uname=$env{'user.name'}; }
 6846:    my $uhome=&homeserver($uname,$udomain);
 6847: 
 6848:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 6849:    my @pairs=split(/\&/,$rep);
 6850:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 6851:      return @pairs;
 6852:    }
 6853:    my %returnhash=();
 6854:    my $i=0;
 6855:    foreach my $item (@$storearr) {
 6856:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 6857:       $i++;
 6858:    }
 6859:    return %returnhash;
 6860: }
 6861: 
 6862: # --------------------------------------------------------------- del interface
 6863: 
 6864: sub del {
 6865:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6866:    my $items='';
 6867:    foreach my $item (@$storearr) {
 6868:        $items.=&escape($item).'&';
 6869:    }
 6870: 
 6871:    $items=~s/\&$//;
 6872:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6873:    if (!$uname) { $uname=$env{'user.name'}; }
 6874:    my $uhome=&homeserver($uname,$udomain);
 6875:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 6876: }
 6877: 
 6878: # -------------------------------------------------------------- dump interface
 6879: 
 6880: sub unserialize {
 6881:     my ($rep, $escapedkeys) = @_;
 6882: 
 6883:     return {} if $rep =~ /^error/;
 6884: 
 6885:     my %returnhash=();
 6886: 	foreach my $item (split(/\&/,$rep)) {
 6887: 	    my ($key, $value) = split(/=/, $item, 2);
 6888: 	    $key = unescape($key) unless $escapedkeys;
 6889: 	    next if $key =~ /^error: 2 /;
 6890: 	    $returnhash{$key} = &thaw_unescape($value);
 6891: 	}
 6892:     #return %returnhash;
 6893:     return \%returnhash;
 6894: }        
 6895: 
 6896: # see Lond::dump_with_regexp
 6897: # if $escapedkeys hash keys won't get unescaped.
 6898: sub dump {
 6899:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 6900:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 6901:     if (!$uname) { $uname=$env{'user.name'}; }
 6902:     my $uhome=&homeserver($uname,$udomain);
 6903: 
 6904:     if ($regexp) {
 6905:         $regexp=&escape($regexp);
 6906:     } else {
 6907:         $regexp='.';
 6908:     }
 6909:     if (grep { $_ eq $uhome } current_machine_ids()) {
 6910:         # user is hosted on this machine
 6911:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 6912:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 6913:         return %{unserialize($reply, $escapedkeys)};
 6914:     }
 6915:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 6916:     my @pairs=split(/\&/,$rep);
 6917:     my %returnhash=();
 6918:     if (!($rep =~ /^error/ )) {
 6919: 	foreach my $item (@pairs) {
 6920: 	    my ($key,$value)=split(/=/,$item,2);
 6921:         $key = unescape($key) unless $escapedkeys;
 6922:         #$key = &unescape($key);
 6923: 	    next if ($key =~ /^error: 2 /);
 6924: 	    $returnhash{$key}=&thaw_unescape($value);
 6925: 	}
 6926:     }
 6927:     return %returnhash;
 6928: }
 6929: 
 6930: 
 6931: # --------------------------------------------------------- dumpstore interface
 6932: 
 6933: sub dumpstore {
 6934:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 6935:    # same as dump but keys must be escaped. They may contain colon separated
 6936:    # lists of values that may themself contain colons (e.g. symbs).
 6937:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 6938: }
 6939: 
 6940: # -------------------------------------------------------------- keys interface
 6941: 
 6942: sub getkeys {
 6943:    my ($namespace,$udomain,$uname)=@_;
 6944:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6945:    if (!$uname) { $uname=$env{'user.name'}; }
 6946:    my $uhome=&homeserver($uname,$udomain);
 6947:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 6948:    my @keyarray=();
 6949:    foreach my $key (split(/\&/,$rep)) {
 6950:       next if ($key =~ /^error: 2 /);
 6951:       push(@keyarray,&unescape($key));
 6952:    }
 6953:    return @keyarray;
 6954: }
 6955: 
 6956: # --------------------------------------------------------------- currentdump
 6957: sub currentdump {
 6958:    my ($courseid,$sdom,$sname)=@_;
 6959:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 6960:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 6961:    $sname    = $env{'user.name'}         if (! defined($sname));
 6962:    my $uhome = &homeserver($sname,$sdom);
 6963:    my $rep;
 6964: 
 6965:    if (grep { $_ eq $uhome } current_machine_ids()) {
 6966:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 6967:                    $courseid)));
 6968:    } else {
 6969:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 6970:    }
 6971: 
 6972:    return if ($rep =~ /^(error:|no_such_host)/);
 6973:    #
 6974:    my %returnhash=();
 6975:    #
 6976:    if ($rep eq 'unknown_cmd') {
 6977:        # an old lond will not know currentdump
 6978:        # Do a dump and make it look like a currentdump
 6979:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 6980:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 6981:        my %hash = @tmp;
 6982:        @tmp=();
 6983:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 6984:    } else {
 6985:        my @pairs=split(/\&/,$rep);
 6986:        foreach my $pair (@pairs) {
 6987:            my ($key,$value)=split(/=/,$pair,2);
 6988:            my ($symb,$param) = split(/:/,$key);
 6989:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 6990:                                                         &thaw_unescape($value);
 6991:        }
 6992:    }
 6993:    return %returnhash;
 6994: }
 6995: 
 6996: sub convert_dump_to_currentdump{
 6997:     my %hash = %{shift()};
 6998:     my %returnhash;
 6999:     # Code ripped from lond, essentially.  The only difference
 7000:     # here is the unescaping done by lonnet::dump().  Conceivably
 7001:     # we might run in to problems with parameter names =~ /^v\./
 7002:     while (my ($key,$value) = each(%hash)) {
 7003:         my ($v,$symb,$param) = split(/:/,$key);
 7004: 	$symb  = &unescape($symb);
 7005: 	$param = &unescape($param);
 7006:         next if ($v eq 'version' || $symb eq 'keys');
 7007:         next if (exists($returnhash{$symb}) &&
 7008:                  exists($returnhash{$symb}->{$param}) &&
 7009:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7010:         $returnhash{$symb}->{$param}=$value;
 7011:         $returnhash{$symb}->{'v.'.$param}=$v;
 7012:     }
 7013:     #
 7014:     # Remove all of the keys in the hashes which keep track of
 7015:     # the version of the parameter.
 7016:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7017:         # use a foreach because we are going to delete from the hash.
 7018:         foreach my $key (keys(%$param_hash)) {
 7019:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7020:         }
 7021:     }
 7022:     return \%returnhash;
 7023: }
 7024: 
 7025: # ------------------------------------------------------ critical inc interface
 7026: 
 7027: sub cinc {
 7028:     return &inc(@_,'critical');
 7029: }
 7030: 
 7031: # --------------------------------------------------------------- inc interface
 7032: 
 7033: sub inc {
 7034:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7035:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7036:     if (!$uname) { $uname=$env{'user.name'}; }
 7037:     my $uhome=&homeserver($uname,$udomain);
 7038:     my $items='';
 7039:     if (! ref($store)) {
 7040:         # got a single value, so use that instead
 7041:         $items = &escape($store).'=&';
 7042:     } elsif (ref($store) eq 'SCALAR') {
 7043:         $items = &escape($$store).'=&';        
 7044:     } elsif (ref($store) eq 'ARRAY') {
 7045:         $items = join('=&',map {&escape($_);} @{$store});
 7046:     } elsif (ref($store) eq 'HASH') {
 7047:         while (my($key,$value) = each(%{$store})) {
 7048:             $items.= &escape($key).'='.&escape($value).'&';
 7049:         }
 7050:     }
 7051:     $items=~s/\&$//;
 7052:     if ($critical) {
 7053: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7054:     } else {
 7055: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7056:     }
 7057: }
 7058: 
 7059: # --------------------------------------------------------------- put interface
 7060: 
 7061: sub put {
 7062:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7063:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7064:    if (!$uname) { $uname=$env{'user.name'}; }
 7065:    my $uhome=&homeserver($uname,$udomain);
 7066:    my $items='';
 7067:    foreach my $item (keys(%$storehash)) {
 7068:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7069:    }
 7070:    $items=~s/\&$//;
 7071:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7072: }
 7073: 
 7074: # ------------------------------------------------------------ newput interface
 7075: 
 7076: sub newput {
 7077:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7078:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7079:    if (!$uname) { $uname=$env{'user.name'}; }
 7080:    my $uhome=&homeserver($uname,$udomain);
 7081:    my $items='';
 7082:    foreach my $key (keys(%$storehash)) {
 7083:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7084:    }
 7085:    $items=~s/\&$//;
 7086:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7087: }
 7088: 
 7089: # ---------------------------------------------------------  putstore interface
 7090: 
 7091: sub putstore {
 7092:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7093:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7094:    if (!$uname) { $uname=$env{'user.name'}; }
 7095:    my $uhome=&homeserver($uname,$udomain);
 7096:    my $items='';
 7097:    foreach my $key (keys(%$storehash)) {
 7098:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7099:    }
 7100:    $items=~s/\&$//;
 7101:    my $esc_symb=&escape($symb);
 7102:    my $esc_v=&escape($version);
 7103:    my $reply =
 7104:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7105: 	      $uhome);
 7106:    if (($tolog) && ($reply eq 'ok')) {
 7107:        my $namevalue='';
 7108:        foreach my $key (keys(%{$storehash})) {
 7109:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7110:        }
 7111:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
 7112:                      '&host='.&escape($perlvar{'lonHostID'}).
 7113:                      '&version='.$esc_v.
 7114:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7115:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7116:    }
 7117:    if ($reply eq 'unknown_cmd') {
 7118:        # gfall back to way things use to be done
 7119:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7120: 			    $uname);
 7121:    }
 7122:    return $reply;
 7123: }
 7124: 
 7125: sub old_putstore {
 7126:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7127:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7128:     if (!$uname) { $uname=$env{'user.name'}; }
 7129:     my $uhome=&homeserver($uname,$udomain);
 7130:     my %newstorehash;
 7131:     foreach my $item (keys(%$storehash)) {
 7132: 	my $key = $version.':'.&escape($symb).':'.$item;
 7133: 	$newstorehash{$key} = $storehash->{$item};
 7134:     }
 7135:     my $items='';
 7136:     my %allitems = ();
 7137:     foreach my $item (keys(%newstorehash)) {
 7138: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7139: 	    my $key = $1.':keys:'.$2;
 7140: 	    $allitems{$key} .= $3.':';
 7141: 	}
 7142: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7143:     }
 7144:     foreach my $item (keys(%allitems)) {
 7145: 	$allitems{$item} =~ s/\:$//;
 7146: 	$items.= $item.'='.$allitems{$item}.'&';
 7147:     }
 7148:     $items=~s/\&$//;
 7149:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7150: }
 7151: 
 7152: # ------------------------------------------------------ critical put interface
 7153: 
 7154: sub cput {
 7155:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7156:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7157:    if (!$uname) { $uname=$env{'user.name'}; }
 7158:    my $uhome=&homeserver($uname,$udomain);
 7159:    my $items='';
 7160:    foreach my $item (keys(%$storehash)) {
 7161:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7162:    }
 7163:    $items=~s/\&$//;
 7164:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7165: }
 7166: 
 7167: # -------------------------------------------------------------- eget interface
 7168: 
 7169: sub eget {
 7170:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7171:    my $items='';
 7172:    foreach my $item (@$storearr) {
 7173:        $items.=&escape($item).'&';
 7174:    }
 7175:    $items=~s/\&$//;
 7176:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7177:    if (!$uname) { $uname=$env{'user.name'}; }
 7178:    my $uhome=&homeserver($uname,$udomain);
 7179:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7180:    my @pairs=split(/\&/,$rep);
 7181:    my %returnhash=();
 7182:    my $i=0;
 7183:    foreach my $item (@$storearr) {
 7184:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7185:       $i++;
 7186:    }
 7187:    return %returnhash;
 7188: }
 7189: 
 7190: # ------------------------------------------------------------ tmpput interface
 7191: sub tmpput {
 7192:     my ($storehash,$server,$context)=@_;
 7193:     my $items='';
 7194:     foreach my $item (keys(%$storehash)) {
 7195: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7196:     }
 7197:     $items=~s/\&$//;
 7198:     if (defined($context)) {
 7199:         $items .= ':'.&escape($context);
 7200:     }
 7201:     return &reply("tmpput:$items",$server);
 7202: }
 7203: 
 7204: # ------------------------------------------------------------ tmpget interface
 7205: sub tmpget {
 7206:     my ($token,$server)=@_;
 7207:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7208:     my $rep=&reply("tmpget:$token",$server);
 7209:     my %returnhash;
 7210:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7211:         return %returnhash;
 7212:     }
 7213:     foreach my $item (split(/\&/,$rep)) {
 7214: 	my ($key,$value)=split(/=/,$item);
 7215: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7216:     }
 7217:     return %returnhash;
 7218: }
 7219: 
 7220: # ------------------------------------------------------------ tmpdel interface
 7221: sub tmpdel {
 7222:     my ($token,$server)=@_;
 7223:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7224:     return &reply("tmpdel:$token",$server);
 7225: }
 7226: 
 7227: # ------------------------------------------------------------ get_timebased_id 
 7228: 
 7229: sub get_timebased_id {
 7230:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7231:         $maxtries) = @_;
 7232:     my ($newid,$error,$dellock);
 7233:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7234:         return ('','ok','invalid call to get suffix');
 7235:     }
 7236: 
 7237: # set defaults for any optional args for which values were not supplied
 7238:     if ($who eq '') {
 7239:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7240:     }
 7241:     if (!$locktries) {
 7242:         $locktries = 3;
 7243:     }
 7244:     if (!$maxtries) {
 7245:         $maxtries = 10;
 7246:     }
 7247:     
 7248:     if (($cdom eq '') || ($cnum eq '')) {
 7249:         if ($env{'request.course.id'}) {
 7250:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7251:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7252:         }
 7253:         if (($cdom eq '') || ($cnum eq '')) {
 7254:             return ('','ok','call to get suffix not in course context');
 7255:         }
 7256:     }
 7257: 
 7258: # construct locking item
 7259:     my $lockhash = {
 7260:                       $prefix."\0".'locked_'.$keyid => $who,
 7261:                    };
 7262:     my $tries = 0;
 7263: 
 7264: # attempt to get lock on nohist_$namespace file
 7265:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7266:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7267:         $tries ++;
 7268:         sleep 1;
 7269:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7270:     }
 7271: 
 7272: # attempt to get unique identifier, based on current timestamp
 7273:     if ($gotlock eq 'ok') {
 7274:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7275:         my $id = time;
 7276:         $newid = $id;
 7277:         if ($idtype eq 'addcode') {
 7278:             $newid .= &sixnum_code();
 7279:         }
 7280:         my $idtries = 0;
 7281:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7282:             if ($idtype eq 'concat') {
 7283:                 $newid = $id.$idtries;
 7284:             } elsif ($idtype eq 'addcode') {
 7285:                 $newid = $newid.&sixnum_code();
 7286:             } else {
 7287:                 $newid ++;
 7288:             }
 7289:             $idtries ++;
 7290:         }
 7291:         if (!exists($inuse{$prefix."\0".$newid})) {
 7292:             my %new_item =  (
 7293:                               $prefix."\0".$newid => $who,
 7294:                             );
 7295:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7296:                                                  $cdom,$cnum);
 7297:             if ($putresult ne 'ok') {
 7298:                 undef($newid);
 7299:                 $error = 'error saving new item: '.$putresult;
 7300:             }
 7301:         } else {
 7302:              undef($newid);
 7303:              $error = ('error: no unique suffix available for the new item ');
 7304:         }
 7305: #  remove lock
 7306:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7307:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7308:     } else {
 7309:         $error = "error: could not obtain lockfile\n";
 7310:         $dellock = 'ok';
 7311:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7312:             $dellock = 'nolock';
 7313:         }
 7314:     }
 7315:     return ($newid,$dellock,$error);
 7316: }
 7317: 
 7318: sub sixnum_code {
 7319:     my $code;
 7320:     for (0..6) {
 7321:         $code .= int( rand(9) );
 7322:     }
 7323:     return $code;
 7324: }
 7325: 
 7326: # -------------------------------------------------- portfolio access checking
 7327: 
 7328: sub portfolio_access {
 7329:     my ($requrl,$clientip) = @_;
 7330:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7331:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7332:     if ($result) {
 7333:         my %setters;
 7334:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7335:             my ($startblock,$endblock) =
 7336:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7337:             if ($startblock && $endblock) {
 7338:                 return 'B';
 7339:             }
 7340:         } else {
 7341:             my ($startblock,$endblock) =
 7342:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7343:             if ($startblock && $endblock) {
 7344:                 return 'B';
 7345:             }
 7346:         }
 7347:     }
 7348:     if ($result eq 'ok') {
 7349:        return 'F';
 7350:     } elsif ($result =~ /^[^:]+:guest_/) {
 7351:        return 'A';
 7352:     }
 7353:     return '';
 7354: }
 7355: 
 7356: sub get_portfolio_access {
 7357:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7358: 
 7359:     if (!ref($access_hash)) {
 7360: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7361: 	my %access_controls = &get_access_controls($current_perms,$group,
 7362: 						   $file_name);
 7363: 	$access_hash = $access_controls{$file_name};
 7364:     }
 7365: 
 7366:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7367:     my $now = time;
 7368:     if (ref($access_hash) eq 'HASH') {
 7369:         foreach my $key (keys(%{$access_hash})) {
 7370:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7371:             if ($start > $now) {
 7372:                 next;
 7373:             }
 7374:             if ($end && $end<$now) {
 7375:                 next;
 7376:             }
 7377:             if ($scope eq 'public') {
 7378:                 $public = $key;
 7379:                 last;
 7380:             } elsif ($scope eq 'guest') {
 7381:                 $guest = $key;
 7382:             } elsif ($scope eq 'domains') {
 7383:                 push(@domains,$key);
 7384:             } elsif ($scope eq 'users') {
 7385:                 push(@users,$key);
 7386:             } elsif ($scope eq 'course') {
 7387:                 push(@courses,$key);
 7388:             } elsif ($scope eq 'group') {
 7389:                 push(@groups,$key);
 7390:             } elsif ($scope eq 'ip') {
 7391:                 push(@ips,$key);
 7392:             }
 7393:         }
 7394:         if ($public) {
 7395:             return 'ok';
 7396:         } elsif (@ips > 0) {
 7397:             my $allowed;
 7398:             foreach my $ipkey (@ips) {
 7399:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7400:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7401:                         $allowed = 1;
 7402:                         last; 
 7403:                     }
 7404:                 }
 7405:             }
 7406:             if ($allowed) {
 7407:                 return 'ok';
 7408:             }
 7409:         }
 7410:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7411:             if ($guest) {
 7412:                 return $guest;
 7413:             }
 7414:         } else {
 7415:             if (@domains > 0) {
 7416:                 foreach my $domkey (@domains) {
 7417:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7418:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7419:                             return 'ok';
 7420:                         }
 7421:                     }
 7422:                 }
 7423:             }
 7424:             if (@users > 0) {
 7425:                 foreach my $userkey (@users) {
 7426:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7427:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7428:                             if (ref($item) eq 'HASH') {
 7429:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7430:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7431:                                     return 'ok';
 7432:                                 }
 7433:                             }
 7434:                         }
 7435:                     } 
 7436:                 }
 7437:             }
 7438:             my %roleshash;
 7439:             my @courses_and_groups = @courses;
 7440:             push(@courses_and_groups,@groups); 
 7441:             if (@courses_and_groups > 0) {
 7442:                 my (%allgroups,%allroles); 
 7443:                 my ($start,$end,$role,$sec,$group);
 7444:                 foreach my $envkey (%env) {
 7445:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7446:                         my $cid = $2.'_'.$3; 
 7447:                         if ($1 eq 'gr') {
 7448:                             $group = $4;
 7449:                             $allgroups{$cid}{$group} = $env{$envkey};
 7450:                         } else {
 7451:                             if ($4 eq '') {
 7452:                                 $sec = 'none';
 7453:                             } else {
 7454:                                 $sec = $4;
 7455:                             }
 7456:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7457:                         }
 7458:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7459:                         my $cid = $2.'_'.$3;
 7460:                         if ($4 eq '') {
 7461:                             $sec = 'none';
 7462:                         } else {
 7463:                             $sec = $4;
 7464:                         }
 7465:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7466:                     }
 7467:                 }
 7468:                 if (keys(%allroles) == 0) {
 7469:                     return;
 7470:                 }
 7471:                 foreach my $key (@courses_and_groups) {
 7472:                     my %content = %{$$access_hash{$key}};
 7473:                     my $cnum = $content{'number'};
 7474:                     my $cdom = $content{'domain'};
 7475:                     my $cid = $cdom.'_'.$cnum;
 7476:                     if (!exists($allroles{$cid})) {
 7477:                         next;
 7478:                     }    
 7479:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7480:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7481:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7482:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7483:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7484:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7485:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7486:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7487:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7488:                                         if (grep/^all$/,@sections) {
 7489:                                             return 'ok';
 7490:                                         } else {
 7491:                                             if (grep/^$sec$/,@sections) {
 7492:                                                 return 'ok';
 7493:                                             }
 7494:                                         }
 7495:                                     }
 7496:                                 }
 7497:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7498:                                     if (grep/^none$/,@groups) {
 7499:                                         return 'ok';
 7500:                                     }
 7501:                                 } else {
 7502:                                     if (grep/^all$/,@groups) {
 7503:                                         return 'ok';
 7504:                                     } 
 7505:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7506:                                         if (grep/^$group$/,@groups) {
 7507:                                             return 'ok';
 7508:                                         }
 7509:                                     }
 7510:                                 } 
 7511:                             }
 7512:                         }
 7513:                     }
 7514:                 }
 7515:             }
 7516:             if ($guest) {
 7517:                 return $guest;
 7518:             }
 7519:         }
 7520:     }
 7521:     return;
 7522: }
 7523: 
 7524: sub course_group_datechecker {
 7525:     my ($dates,$now,$status) = @_;
 7526:     my ($start,$end) = split(/\./,$dates);
 7527:     if (!$start && !$end) {
 7528:         return 'ok';
 7529:     }
 7530:     if (grep/^active$/,@{$status}) {
 7531:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7532:             return 'ok';
 7533:         }
 7534:     }
 7535:     if (grep/^previous$/,@{$status}) {
 7536:         if ($end > $now ) {
 7537:             return 'ok';
 7538:         }
 7539:     }
 7540:     if (grep/^future$/,@{$status}) {
 7541:         if ($start > $now) {
 7542:             return 'ok';
 7543:         }
 7544:     }
 7545:     return; 
 7546: }
 7547: 
 7548: sub parse_portfolio_url {
 7549:     my ($url) = @_;
 7550: 
 7551:     my ($type,$udom,$unum,$group,$file_name);
 7552:     
 7553:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7554: 	$type = 1;
 7555:         $udom = $1;
 7556:         $unum = $2;
 7557:         $file_name = $3;
 7558:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7559: 	$type = 2;
 7560:         $udom = $1;
 7561:         $unum = $2;
 7562:         $group = $3;
 7563:         $file_name = $3.'/'.$4;
 7564:     }
 7565:     if (wantarray) {
 7566: 	return ($type,$udom,$unum,$file_name,$group);
 7567:     }
 7568:     return $type;
 7569: }
 7570: 
 7571: sub is_portfolio_url {
 7572:     my ($url) = @_;
 7573:     return scalar(&parse_portfolio_url($url));
 7574: }
 7575: 
 7576: sub is_portfolio_file {
 7577:     my ($file) = @_;
 7578:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7579:         return 1;
 7580:     }
 7581:     return;
 7582: }
 7583: 
 7584: sub usertools_access {
 7585:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7586:     my ($access,%tools);
 7587:     if ($context eq '') {
 7588:         $context = 'tools';
 7589:     }
 7590:     if ($context eq 'requestcourses') {
 7591:         %tools = (
 7592:                       official   => 1,
 7593:                       unofficial => 1,
 7594:                       community  => 1,
 7595:                       textbook   => 1,
 7596:                       placement  => 1,
 7597:                       lti        => 1,
 7598:                  );
 7599:     } elsif ($context eq 'requestauthor') {
 7600:         %tools = (
 7601:                       requestauthor => 1,
 7602:                  );
 7603:     } else {
 7604:         %tools = (
 7605:                       aboutme   => 1,
 7606:                       blog      => 1,
 7607:                       webdav    => 1,
 7608:                       portfolio => 1,
 7609:                  );
 7610:     }
 7611:     return if (!defined($tools{$tool}));
 7612: 
 7613:     if (($udom eq '') || ($uname eq '')) {
 7614:         $udom = $env{'user.domain'};
 7615:         $uname = $env{'user.name'};
 7616:     }
 7617: 
 7618:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7619:         if ($action ne 'reload') {
 7620:             if ($context eq 'requestcourses') {
 7621:                 return $env{'environment.canrequest.'.$tool};
 7622:             } elsif ($context eq 'requestauthor') {
 7623:                 return $env{'environment.canrequest.author'};
 7624:             } else {
 7625:                 return $env{'environment.availabletools.'.$tool};
 7626:             }
 7627:         }
 7628:     }
 7629: 
 7630:     my ($toolstatus,$inststatus,$envkey);
 7631:     if ($context eq 'requestauthor') {
 7632:         $envkey = $context; 
 7633:     } else {
 7634:         $envkey = $context.'.'.$tool;
 7635:     }
 7636: 
 7637:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7638:          ($action ne 'reload')) {
 7639:         $toolstatus = $env{'environment.'.$envkey};
 7640:         $inststatus = $env{'environment.inststatus'};
 7641:     } else {
 7642:         if (ref($userenvref) eq 'HASH') {
 7643:             $toolstatus = $userenvref->{$envkey};
 7644:             $inststatus = $userenvref->{'inststatus'};
 7645:         } else {
 7646:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7647:             $toolstatus = $userenv{$envkey};
 7648:             $inststatus = $userenv{'inststatus'};
 7649:         }
 7650:     }
 7651: 
 7652:     if ($toolstatus ne '') {
 7653:         if ($toolstatus) {
 7654:             $access = 1;
 7655:         } else {
 7656:             $access = 0;
 7657:         }
 7658:         return $access;
 7659:     }
 7660: 
 7661:     my ($is_adv,%domdef);
 7662:     if (ref($is_advref) eq 'HASH') {
 7663:         $is_adv = $is_advref->{'is_adv'};
 7664:     } else {
 7665:         $is_adv = &is_advanced_user($udom,$uname);
 7666:     }
 7667:     if (ref($domdefref) eq 'HASH') {
 7668:         %domdef = %{$domdefref};
 7669:     } else {
 7670:         %domdef = &get_domain_defaults($udom);
 7671:     }
 7672:     if (ref($domdef{$tool}) eq 'HASH') {
 7673:         if ($is_adv) {
 7674:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7675:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7676:                     $access = 1;
 7677:                 } else {
 7678:                     $access = 0;
 7679:                 }
 7680:                 return $access;
 7681:             }
 7682:         }
 7683:         if ($inststatus ne '') {
 7684:             my ($hasaccess,$hasnoaccess);
 7685:             foreach my $affiliation (split(/:/,$inststatus)) {
 7686:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7687:                     if ($domdef{$tool}{$affiliation}) {
 7688:                         $hasaccess = 1;
 7689:                     } else {
 7690:                         $hasnoaccess = 1;
 7691:                     }
 7692:                 }
 7693:             }
 7694:             if ($hasaccess || $hasnoaccess) {
 7695:                 if ($hasaccess) {
 7696:                     $access = 1;
 7697:                 } elsif ($hasnoaccess) {
 7698:                     $access = 0; 
 7699:                 }
 7700:                 return $access;
 7701:             }
 7702:         } else {
 7703:             if ($domdef{$tool}{'default'} ne '') {
 7704:                 if ($domdef{$tool}{'default'}) {
 7705:                     $access = 1;
 7706:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7707:                     $access = 0;
 7708:                 }
 7709:                 return $access;
 7710:             }
 7711:         }
 7712:     } else {
 7713:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7714:             $access = 1;
 7715:         } else {
 7716:             $access = 0;
 7717:         }
 7718:         return $access;
 7719:     }
 7720: }
 7721: 
 7722: sub is_course_owner {
 7723:     my ($cdom,$cnum,$udom,$uname) = @_;
 7724:     if (($udom eq '') || ($uname eq '')) {
 7725:         $udom = $env{'user.domain'};
 7726:         $uname = $env{'user.name'};
 7727:     }
 7728:     unless (($udom eq '') || ($uname eq '')) {
 7729:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7730:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7731:                 return 1;
 7732:             } else {
 7733:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7734:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7735:                     return 1;
 7736:                 }
 7737:             }
 7738:         }
 7739:     }
 7740:     return;
 7741: }
 7742: 
 7743: sub is_advanced_user {
 7744:     my ($udom,$uname) = @_;
 7745:     if ($udom ne '' && $uname ne '') {
 7746:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7747:             if (wantarray) {
 7748:                 return ($env{'user.adv'},$env{'user.author'});
 7749:             } else {
 7750:                 return $env{'user.adv'};
 7751:             }
 7752:         }
 7753:     }
 7754:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7755:     my %allroles;
 7756:     my ($is_adv,$is_author);
 7757:     foreach my $role (keys(%roleshash)) {
 7758:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7759:         my $area = '/'.$tdomain.'/'.$trest;
 7760:         if ($sec ne '') {
 7761:             $area .= '/'.$sec;
 7762:         }
 7763:         if (($area ne '') && ($trole ne '')) {
 7764:             my $spec=$trole.'.'.$area;
 7765:             if ($trole =~ /^cr\//) {
 7766:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7767:             } elsif ($trole ne 'gr') {
 7768:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7769:             }
 7770:             if ($trole eq 'au') {
 7771:                 $is_author = 1;
 7772:             }
 7773:         }
 7774:     }
 7775:     foreach my $role (keys(%allroles)) {
 7776:         last if ($is_adv);
 7777:         foreach my $item (split(/:/,$allroles{$role})) {
 7778:             if ($item ne '') {
 7779:                 my ($privilege,$restrictions)=split(/&/,$item);
 7780:                 if ($privilege eq 'adv') {
 7781:                     $is_adv = 1;
 7782:                     last;
 7783:                 }
 7784:             }
 7785:         }
 7786:     }
 7787:     if (wantarray) {
 7788:         return ($is_adv,$is_author);
 7789:     }
 7790:     return $is_adv;
 7791: }
 7792: 
 7793: sub check_can_request {
 7794:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 7795:     my $canreq = 0;
 7796:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 7797:         $uname = $env{'user.name'};
 7798:         $udom = $env{'user.domain'};
 7799:     }
 7800:     my ($types,$typename) = &Apache::loncommon::course_types();
 7801:     my @options = ('approval','validate','autolimit');
 7802:     my $optregex = join('|',@options);
 7803:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 7804:         foreach my $type (@{$types}) {
 7805:             if (&usertools_access($uname,$udom,$type,undef,
 7806:                                   'requestcourses')) {
 7807:                 $canreq ++;
 7808:                 if (ref($request_domains) eq 'HASH') {
 7809:                     push(@{$request_domains->{$type}},$udom);
 7810:                 }
 7811:                 if ($dom eq $udom) {
 7812:                     $can_request->{$type} = 1;
 7813:                 }
 7814:             }
 7815:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 7816:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 7817:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 7818:                 if (@curr > 0) {
 7819:                     foreach my $item (@curr) {
 7820:                         if (ref($request_domains) eq 'HASH') {
 7821:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 7822:                             if ($otherdom ne '') {
 7823:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 7824:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 7825:                                         push(@{$request_domains->{$type}},$otherdom);
 7826:                                     }
 7827:                                 } else {
 7828:                                     push(@{$request_domains->{$type}},$otherdom);
 7829:                                 }
 7830:                             }
 7831:                         }
 7832:                     }
 7833:                     unless ($dom eq $env{'user.domain'}) {
 7834:                         $canreq ++;
 7835:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 7836:                             $can_request->{$type} = 1;
 7837:                         }
 7838:                     }
 7839:                 }
 7840:             }
 7841:         }
 7842:     }
 7843:     return $canreq;
 7844: }
 7845: 
 7846: # ---------------------------------------------- Custom access rule evaluation
 7847: 
 7848: sub customaccess {
 7849:     my ($priv,$uri)=@_;
 7850:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 7851:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 7852:     $udom = &LONCAPA::clean_domain($udom);
 7853:     $ucrs = &LONCAPA::clean_username($ucrs);
 7854:     my $access=0;
 7855:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 7856: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 7857: 	if ($type eq 'user') {
 7858: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 7859: 		my ($tdom,$tuname)=split(m{/},$scope);
 7860: 		if ($tdom) {
 7861: 		    if ($tdom ne $env{'user.domain'}) { next; }
 7862: 		}
 7863: 		if ($tuname) {
 7864: 		    if ($tuname ne $env{'user.name'}) { next; }
 7865: 		}
 7866: 		$access=($effect eq 'allow');
 7867: 		last;
 7868: 	    }
 7869: 	} else {
 7870: 	    if ($role) {
 7871: 		if ($role ne $urole) { next; }
 7872: 	    }
 7873: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 7874: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 7875: 		if ($tdom) {
 7876: 		    if ($tdom ne $udom) { next; }
 7877: 		}
 7878: 		if ($tcrs) {
 7879: 		    if ($tcrs ne $ucrs) { next; }
 7880: 		}
 7881: 		if ($tsec) {
 7882: 		    if ($tsec ne $usec) { next; }
 7883: 		}
 7884: 		$access=($effect eq 'allow');
 7885: 		last;
 7886: 	    }
 7887: 	    if ($realm eq '' && $role eq '') {
 7888: 		$access=($effect eq 'allow');
 7889: 	    }
 7890: 	}
 7891:     }
 7892:     return $access;
 7893: }
 7894: 
 7895: # ------------------------------------------------- Check for a user privilege
 7896: 
 7897: sub allowed {
 7898:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
 7899:     my $ver_orguri=$uri;
 7900:     $uri=&deversion($uri);
 7901:     my $orguri=$uri;
 7902:     $uri=&declutter($uri);
 7903: 
 7904:     if ($priv eq 'evb') {
 7905: # Evade communication block restrictions for specified role in a course
 7906:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 7907:             return $1;
 7908:         } else {
 7909:             return;
 7910:         }
 7911:     }
 7912: 
 7913:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 7914: # Free bre access to adm and meta resources
 7915:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
 7916: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 7917: 	&& ($priv eq 'bre')) {
 7918: 	return 'F';
 7919:     }
 7920: 
 7921: # Free bre access to user's own portfolio contents
 7922:     my ($space,$domain,$name,@dir)=split('/',$uri);
 7923:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 7924: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 7925:         my %setters;
 7926:         my ($startblock,$endblock) = 
 7927:             &Apache::loncommon::blockcheck(\%setters,'port');
 7928:         if ($startblock && $endblock) {
 7929:             return 'B';
 7930:         } else {
 7931:             return 'F';
 7932:         }
 7933:     }
 7934: 
 7935: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 7936:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 7937:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 7938:         if (exists($env{'request.course.id'})) {
 7939:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7940:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7941:             if (($domain eq $cdom) && ($name eq $cnum)) {
 7942:                 my $courseprivid=$env{'request.course.id'};
 7943:                 $courseprivid=~s/\_/\//;
 7944:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 7945:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 7946:                     return $1; 
 7947:                 } else {
 7948:                     if ($env{'request.course.sec'}) {
 7949:                         $courseprivid.='/'.$env{'request.course.sec'};
 7950:                     }
 7951:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 7952:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 7953:                         return $2;
 7954:                     }
 7955:                 }
 7956:             }
 7957:         }
 7958:     }
 7959: 
 7960: # Free bre to public access
 7961: 
 7962:     if ($priv eq 'bre') {
 7963:         my $copyright;
 7964:         unless ($uri =~ /ext\.tool/) {
 7965:             $copyright=&metadata($uri,'copyright');
 7966:         }
 7967: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 7968:            return 'F'; 
 7969:         }
 7970:         if ($copyright eq 'priv') {
 7971:             $uri=~/([^\/]+)\/([^\/]+)\//;
 7972: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 7973: 		return '';
 7974:             }
 7975:         }
 7976:         if ($copyright eq 'domain') {
 7977:             $uri=~/([^\/]+)\/([^\/]+)\//;
 7978: 	    unless (($env{'user.domain'} eq $1) ||
 7979:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 7980: 		return '';
 7981:             }
 7982:         }
 7983:         if ($env{'request.role'}=~ /li\.\//) {
 7984:             # Library role, so allow browsing of resources in this domain.
 7985:             return 'F';
 7986:         }
 7987:         if ($copyright eq 'custom') {
 7988: 	    unless (&customaccess($priv,$uri)) { return ''; }
 7989:         }
 7990:     }
 7991:     # Domain coordinator is trying to create a course
 7992:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 7993:         # uri is the requested domain in this case.
 7994:         # comparison to 'request.role.domain' shows if the user has selected
 7995:         # a role of dc for the domain in question.
 7996:         return 'F' if ($uri eq $env{'request.role.domain'});
 7997:     }
 7998: 
 7999:     my $thisallowed='';
 8000:     my $statecond=0;
 8001:     my $courseprivid='';
 8002: 
 8003:     my $ownaccess;
 8004:     # Community Coordinator or Assistant Co-author browsing resource space.
 8005:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8006:         if ($uri eq '') {
 8007:             $ownaccess = 1;
 8008:         } else {
 8009:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8010:                 my $udom = $env{'user.domain'};
 8011:                 my $uname = $env{'user.name'};
 8012:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8013:                     $ownaccess = 1;
 8014:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8015:                     unless ($uri =~ m{\.\./}) {
 8016:                         $ownaccess = 1;
 8017:                     }
 8018:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8019:                     my $now = time;
 8020:                     if ($uri =~ m{^([^/]+)/?$}) {
 8021:                         my $adom = $1;
 8022:                         foreach my $key (keys(%env)) {
 8023:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8024:                                 my ($start,$end) = split('.',$env{$key});
 8025:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8026:                                     $ownaccess = 1;
 8027:                                     last;
 8028:                                 }
 8029:                             }
 8030:                         }
 8031:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8032:                         my $adom = $1;
 8033:                         my $aname = $2;
 8034:                         foreach my $role ('ca','aa') { 
 8035:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8036:                                 my ($start,$end) =
 8037:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 8038:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8039:                                     $ownaccess = 1;
 8040:                                     last;
 8041:                                 }
 8042:                             }
 8043:                         }
 8044:                     }
 8045:                 }
 8046:             }
 8047:         }
 8048:     }
 8049: 
 8050: # Course
 8051: 
 8052:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8053:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8054:             $thisallowed.=$1;
 8055:         }
 8056:     }
 8057: 
 8058: # Domain
 8059: 
 8060:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8061:        =~/\Q$priv\E\&([^\:]*)/) {
 8062:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8063:             $thisallowed.=$1;
 8064:         }
 8065:     }
 8066: 
 8067: # User who is not author or co-author might still be able to edit
 8068: # resource of an author in the domain (e.g., if Domain Coordinator).
 8069:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8070:         (&allowed('mdc',$env{'request.course.id'}))) {
 8071:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8072:             $thisallowed.=$1;
 8073:         }
 8074:     }
 8075: 
 8076: # Course: uri itself is a course
 8077:     my $courseuri=$uri;
 8078:     $courseuri=~s/\_(\d)/\/$1/;
 8079:     $courseuri=~s/^([^\/])/\/$1/;
 8080: 
 8081:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8082:        =~/\Q$priv\E\&([^\:]*)/) {
 8083:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8084:             $thisallowed.=$1;
 8085:         }
 8086:     }
 8087: 
 8088: # URI is an uploaded document for this course, default permissions don't matter
 8089: # not allowing 'edit' access (editupload) to uploaded course docs
 8090:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8091: 	$thisallowed='';
 8092:         my ($match)=&is_on_map($uri);
 8093:         if ($match) {
 8094:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8095:                   =~/\Q$priv\E\&([^\:]*)/) {
 8096:                 my $value = $1;
 8097:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8098:                 if ($deeplinkblock) {
 8099:                     $thisallowed='D';
 8100:                 } elsif ($noblockcheck) {
 8101:                     $thisallowed.=$value;
 8102:                 } else {
 8103:                     my @blockers = &has_comm_blocking($priv,$symb,$uri);
 8104:                     if (@blockers > 0) {
 8105:                         $thisallowed = 'B';
 8106:                     } else {
 8107:                         $thisallowed.=$value;
 8108:                     }
 8109:                 }
 8110:             }
 8111:         } else {
 8112:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8113:             if ($refuri) {
 8114:                 if ($refuri =~ m|^/adm/|) {
 8115:                     $thisallowed='F';
 8116:                 } else {
 8117:                     $refuri=&declutter($refuri);
 8118:                     my ($match) = &is_on_map($refuri);
 8119:                     if ($match) {
 8120:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8121:                         if ($deeplinkblock) {
 8122:                             $thisallowed='D';
 8123:                         } elsif ($noblockcheck) {
 8124:                             $thisallowed='F';
 8125:                         } else {
 8126:                             my @blockers = &has_comm_blocking($priv,$symb,$refuri);
 8127:                             if (@blockers > 0) {
 8128:                                 $thisallowed = 'B';
 8129:                             } else {
 8130:                                 $thisallowed='F';
 8131:                             }
 8132:                         }
 8133:                     }
 8134:                 }
 8135:             }
 8136:         }
 8137:     }
 8138: 
 8139:     if ($priv eq 'bre'
 8140: 	&& $thisallowed ne 'F' 
 8141: 	&& $thisallowed ne '2'
 8142: 	&& &is_portfolio_url($uri)) {
 8143: 	$thisallowed = &portfolio_access($uri,$clientip);
 8144:     }
 8145: 
 8146: # Full access at system, domain or course-wide level? Exit.
 8147:     if ($thisallowed=~/F/) {
 8148: 	return 'F';
 8149:     }
 8150: 
 8151: # If this is generating or modifying users, exit with special codes
 8152: 
 8153:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8154: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8155: 	    my ($audom,$auname)=split('/',$uri);
 8156: # no author name given, so this just checks on the general right to make a co-author in this domain
 8157: 	    unless ($auname) { return $thisallowed; }
 8158: # an author name is given, so we are about to actually make a co-author for a certain account
 8159: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8160: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8161: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8162: 	}
 8163: 	return $thisallowed;
 8164:     }
 8165: #
 8166: # Gathered so far: system, domain and course wide privileges
 8167: #
 8168: # Course: See if uri or referer is an individual resource that is part of 
 8169: # the course
 8170: 
 8171:     if ($env{'request.course.id'}) {
 8172: 
 8173:        $courseprivid=$env{'request.course.id'};
 8174:        if ($env{'request.course.sec'}) {
 8175:           $courseprivid.='/'.$env{'request.course.sec'};
 8176:        }
 8177:        $courseprivid=~s/\_/\//;
 8178:        my $checkreferer=1;
 8179:        my ($match,$cond)=&is_on_map($uri);
 8180:        if ($match) {
 8181:            $statecond=$cond;
 8182:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8183:                =~/\Q$priv\E\&([^\:]*)/) {
 8184:                my $value = $1;
 8185:                if ($priv eq 'bre') {
 8186:                    if ($noblockcheck) {
 8187:                        $thisallowed.=$value;
 8188:                    } else {
 8189:                        my @blockers = &has_comm_blocking($priv,$symb,$uri);
 8190:                        if (@blockers > 0) {
 8191:                            $thisallowed = 'B';
 8192:                        } else {
 8193:                            $thisallowed.=$value;
 8194:                        }
 8195:                    }
 8196:                } else {
 8197:                    $thisallowed.=$value;
 8198:                }
 8199:                $checkreferer=0;
 8200:            }
 8201:        }
 8202:        
 8203:        if ($checkreferer) {
 8204: 	  my $refuri=$env{'httpref.'.$orguri};
 8205:             unless ($refuri) {
 8206:                 foreach my $key (keys(%env)) {
 8207: 		    if ($key=~/^httpref\..*\*/) {
 8208: 			my $pattern=$key;
 8209:                         $pattern=~s/^httpref\.\/res\///;
 8210:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8211:                         $pattern=~s/\//\\\//g;
 8212:                         if ($orguri=~/$pattern/) {
 8213: 			    $refuri=$env{$key};
 8214:                         }
 8215:                     }
 8216:                 }
 8217:             }
 8218: 
 8219:          if ($refuri) { 
 8220: 	  $refuri=&declutter($refuri);
 8221:           my ($match,$cond)=&is_on_map($refuri);
 8222:             if ($match) {
 8223:               my $refstatecond=$cond;
 8224:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8225:                   =~/\Q$priv\E\&([^\:]*)/) {
 8226:                   my $value = $1;
 8227:                   if ($priv eq 'bre') {
 8228:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8229:                       if ($deeplinkblock) {
 8230:                           $thisallowed = 'D';
 8231:                       } elsif ($noblockcheck) {
 8232:                           $thisallowed.=$value;
 8233:                       } else {
 8234:                           my @blockers = &has_comm_blocking($priv,$symb,$refuri);
 8235:                           if (@blockers > 0) {
 8236:                               $thisallowed = 'B';
 8237:                           } else {
 8238:                               $thisallowed.=$value;
 8239:                           }
 8240:                       }
 8241:                   } else {
 8242:                       $thisallowed.=$value;
 8243:                   }
 8244:                   $uri=$refuri;
 8245:                   $statecond=$refstatecond;
 8246:               }
 8247:           }
 8248:         }
 8249:        }
 8250:    }
 8251: 
 8252: #
 8253: # Gathered now: all privileges that could apply, and condition number
 8254: # 
 8255: #
 8256: # Full or no access?
 8257: #
 8258: 
 8259:     if ($thisallowed=~/F/) {
 8260: 	return 'F';
 8261:     }
 8262: 
 8263:     unless ($thisallowed) {
 8264:         return '';
 8265:     }
 8266: 
 8267: # Restrictions exist, deal with them
 8268: #
 8269: #   C:according to course preferences
 8270: #   R:according to resource settings
 8271: #   L:unless locked
 8272: #   X:according to user session state
 8273: #
 8274: 
 8275: # Possibly locked functionality, check all courses
 8276: # Locks might take effect only after 10 minutes cache expiration for other
 8277: # courses, and 2 minutes for current course
 8278: 
 8279:     my $envkey;
 8280:     if ($thisallowed=~/L/) {
 8281:         foreach $envkey (keys(%env)) {
 8282:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8283:                my $courseid=$2;
 8284:                my $roleid=$1.'.'.$2;
 8285:                $courseid=~s/^\///;
 8286:                my $expiretime=600;
 8287:                if ($env{'request.role'} eq $roleid) {
 8288: 		  $expiretime=120;
 8289:                }
 8290: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8291:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8292:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8293: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8294:                }
 8295:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8296:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8297: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8298:                        &log($env{'user.domain'},$env{'user.name'},
 8299:                             $env{'user.home'},
 8300:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8301:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8302:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8303: 		       return '';
 8304:                    }
 8305:                }
 8306:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8307:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8308: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 8309:                        &log($env{'user.domain'},$env{'user.name'},
 8310:                             $env{'user.home'},
 8311:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8312:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8313:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8314: 		       return '';
 8315:                    }
 8316:                }
 8317: 	   }
 8318:        }
 8319:     }
 8320:    
 8321: #
 8322: # Rest of the restrictions depend on selected course
 8323: #
 8324: 
 8325:     unless ($env{'request.course.id'}) {
 8326: 	if ($thisallowed eq 'A') {
 8327: 	    return 'A';
 8328:         } elsif ($thisallowed eq 'B') {
 8329:             return 'B';
 8330: 	} else {
 8331: 	    return '1';
 8332: 	}
 8333:     }
 8334: 
 8335: #
 8336: # Now user is definitely in a course
 8337: #
 8338: 
 8339: 
 8340: # Course preferences
 8341: 
 8342:    if ($thisallowed=~/C/) {
 8343:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8344:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8345:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8346: 	   =~/\Q$rolecode\E/) {
 8347: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8348: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8349: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8350: 			$env{'request.course.id'});
 8351: 	   }
 8352:            return '';
 8353:        }
 8354: 
 8355:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8356: 	   =~/\Q$unamedom\E/) {
 8357: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8358: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8359: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8360: 			$env{'request.course.id'});
 8361: 	   }
 8362:            return '';
 8363:        }
 8364:    }
 8365: 
 8366: # Resource preferences
 8367: 
 8368:    if ($thisallowed=~/R/) {
 8369:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8370:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8371: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8372: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8373: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8374: 	   }
 8375: 	   return '';
 8376:        }
 8377:    }
 8378: 
 8379: # Restricted by state or randomout?
 8380: 
 8381:    if ($thisallowed=~/X/) {
 8382:       if ($env{'acc.randomout'}) {
 8383: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8384:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8385:             return ''; 
 8386:          }
 8387:       }
 8388:       if (&condval($statecond)) {
 8389: 	 return '2';
 8390:       } else {
 8391:          return '';
 8392:       }
 8393:    }
 8394: 
 8395:     if ($thisallowed eq 'A') {
 8396: 	return 'A';
 8397:     } elsif ($thisallowed eq 'B') {
 8398:         return 'B';
 8399:     } elsif ($thisallowed eq 'D') {
 8400:         return 'D';
 8401:     }
 8402:    return 'F';
 8403: }
 8404: 
 8405: # ------------------------------------------- Check construction space access
 8406: 
 8407: sub constructaccess {
 8408:     my ($url,$setpriv)=@_;
 8409: 
 8410: # We do not allow editing of previous versions of files
 8411:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8412: 
 8413: # Get username and domain from URL
 8414:     my ($ownername,$ownerdomain,$ownerhome);
 8415: 
 8416:     ($ownerdomain,$ownername) =
 8417:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8418: 
 8419: # The URL does not really point to any authorspace, forget it
 8420:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8421: 
 8422: # Now we need to see if the user has access to the authorspace of
 8423: # $ownername at $ownerdomain
 8424: 
 8425:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8426: # Real author for this?
 8427:        $ownerhome = $env{'user.home'};
 8428:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8429:           return ($ownername,$ownerdomain,$ownerhome);
 8430:        }
 8431:     } else {
 8432: # Co-author for this?
 8433:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8434:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8435:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8436:             return ($ownername,$ownerdomain,$ownerhome);
 8437:         }
 8438:         if ($env{'request.course.id'}) {
 8439:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8440:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8441:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8442:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8443:                     return ($ownername,$ownerdomain,$ownerhome);
 8444:                 }
 8445:             }
 8446:         }
 8447:     }
 8448: 
 8449: # We don't have any access right now. If we are not possibly going to do anything about this,
 8450: # we might as well leave
 8451:    unless ($setpriv) { return ''; }
 8452: 
 8453: # Backdoor access?
 8454:     my $allowed=&allowed('eco',$ownerdomain);
 8455: # Nope
 8456:     unless ($allowed) { return ''; }
 8457: # Looks like we may have access, but could be locked by the owner of the construction space
 8458:     if ($allowed eq 'U') {
 8459:         my %blocked=&get('environment',['domcoord.author'],
 8460:                          $ownerdomain,$ownername);
 8461: # Is blocked by owner
 8462:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8463:     }
 8464:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8465: # Grant temporary access
 8466:         my $then=$env{'user.login.time'};
 8467:         my $update=$env{'user.update.time'};
 8468:         if (!$update) { $update = $then; }
 8469:         my $refresh=$env{'user.refresh.time'};
 8470:         if (!$refresh) { $refresh = $update; }
 8471:         my $now = time;
 8472:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8473:                            $now,'ca','constructaccess');
 8474:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8475:         return($ownername,$ownerdomain,$ownerhome);
 8476:     }
 8477: # No business here
 8478:     return '';
 8479: }
 8480: 
 8481: # ----------------------------------------------------------- Content Blocking
 8482: 
 8483: {
 8484: # Caches for faster Course Contents display where content blocking
 8485: # is in operation (i.e., interval param set) for timed quiz.
 8486: #
 8487: # User for whom data are being temporarily cached.
 8488: my $cacheduser='';
 8489: # Cached blockers for this user (a hash of blocking items). 
 8490: my %cachedblockers=();
 8491: # When the data were last cached.
 8492: my $cachedlast='';
 8493: 
 8494: sub load_all_blockers {
 8495:     my ($uname,$udom,$blocks)=@_;
 8496:     if (($uname ne '') && ($udom ne '')) { 
 8497:         if (($cacheduser eq $uname.':'.$udom) &&
 8498:             (abs($cachedlast-time)<5)) {
 8499:             return;
 8500:         }
 8501:     }
 8502:     $cachedlast=time;
 8503:     $cacheduser=$uname.':'.$udom;
 8504:     %cachedblockers = &get_commblock_resources($blocks);
 8505: }
 8506: 
 8507: sub get_comm_blocks {
 8508:     my ($cdom,$cnum) = @_;
 8509:     if ($cdom eq '' || $cnum eq '') {
 8510:         return unless ($env{'request.course.id'});
 8511:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8512:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8513:     }
 8514:     my %commblocks;
 8515:     my $hashid=$cdom.'_'.$cnum;
 8516:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8517:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8518:         %commblocks = %{$blocksref};
 8519:     } else {
 8520:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8521:         my $cachetime = 600;
 8522:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8523:     }
 8524:     return %commblocks;
 8525: }
 8526: 
 8527: sub get_commblock_resources {
 8528:     my ($blocks) = @_;
 8529:     my %blockers = ();
 8530:     return %blockers unless ($env{'request.course.id'});
 8531:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8532:     my %commblocks;
 8533:     if (ref($blocks) eq 'HASH') {
 8534:         %commblocks = %{$blocks};
 8535:     } else {
 8536:         %commblocks = &get_comm_blocks();
 8537:     }
 8538:     return %blockers unless (keys(%commblocks) > 0); 
 8539:     my $navmap = Apache::lonnavmaps::navmap->new();
 8540:     return %blockers unless (ref($navmap));
 8541:     my $now = time;
 8542:     foreach my $block (keys(%commblocks)) {
 8543:         if ($block =~ /^(\d+)____(\d+)$/) {
 8544:             my ($start,$end) = ($1,$2);
 8545:             if ($start <= $now && $end >= $now) {
 8546:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8547:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8548:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8549:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8550:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8551:                             }
 8552:                         }
 8553:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8554:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8555:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8556:                             }
 8557:                         }
 8558:                     }
 8559:                 }
 8560:             }
 8561:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8562:             my $item = $1;
 8563:             my @to_test;
 8564:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8565:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8566:                     my @interval;
 8567:                     my $type = 'map';
 8568:                     if ($item eq 'course') {
 8569:                         $type = 'course';
 8570:                         @interval=&EXT("resource.0.interval");
 8571:                     } else {
 8572:                         if ($item =~ /___\d+___/) {
 8573:                             $type = 'resource';
 8574:                             @interval=&EXT("resource.0.interval",$item);
 8575:                             if (ref($navmap)) {                        
 8576:                                 my $res = $navmap->getBySymb($item); 
 8577:                                 push(@to_test,$res);
 8578:                             }
 8579:                         } else {
 8580:                             my $mapsymb = &symbread($item,1);
 8581:                             if ($mapsymb) {
 8582:                                 if (ref($navmap)) {
 8583:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8584:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
 8585:                                     foreach my $res (@to_test) {
 8586:                                         my $symb = $res->symb();
 8587:                                         next if ($symb eq $mapsymb);
 8588:                                         if ($symb ne '') {
 8589:                                             @interval=&EXT("resource.0.interval",$symb);
 8590:                                             if ($interval[1] eq 'map') {
 8591:                                                 last;
 8592:                                             }
 8593:                                         }
 8594:                                     }
 8595:                                 }
 8596:                             }
 8597:                         }
 8598:                     }
 8599:                     if ($interval[0] =~ /^(\d+)/) {
 8600:                         my $timelimit = $1; 
 8601:                         my $first_access;
 8602:                         if ($type eq 'resource') {
 8603:                             $first_access=&get_first_access($interval[1],$item);
 8604:                         } elsif ($type eq 'map') {
 8605:                             $first_access=&get_first_access($interval[1],undef,$item);
 8606:                         } else {
 8607:                             $first_access=&get_first_access($interval[1]);
 8608:                         }
 8609:                         if ($first_access) {
 8610:                             my $timesup = $first_access+$timelimit;
 8611:                             if ($timesup > $now) {
 8612:                                 my $activeblock;
 8613:                                 foreach my $res (@to_test) {
 8614:                                     if ($res->answerable()) {
 8615:                                         $activeblock = 1;
 8616:                                         last;
 8617:                                     }
 8618:                                 }
 8619:                                 if ($activeblock) {
 8620:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8621:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8622:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8623:                                          }
 8624:                                     }
 8625:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8626:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8627:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8628:                                         }
 8629:                                     }
 8630:                                 }
 8631:                             }
 8632:                         }
 8633:                     }
 8634:                 }
 8635:             }
 8636:         }
 8637:     }
 8638:     return %blockers;
 8639: }
 8640: 
 8641: sub has_comm_blocking {
 8642:     my ($priv,$symb,$uri,$blocks) = @_;
 8643:     my @blockers;
 8644:     return unless ($env{'request.course.id'});
 8645:     return unless ($priv eq 'bre');
 8646:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8647:     return if ($env{'request.state'} eq 'construct');
 8648:     &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
 8649:     return unless (keys(%cachedblockers) > 0);
 8650:     my (%possibles,@symbs);
 8651:     if (!$symb) {
 8652:         $symb = &symbread($uri,1,1,1,\%possibles);
 8653:     }
 8654:     if ($symb) {
 8655:         @symbs = ($symb);
 8656:     } elsif (keys(%possibles)) { 
 8657:         @symbs = keys(%possibles);
 8658:     }
 8659:     my $noblock;
 8660:     foreach my $symb (@symbs) {
 8661:         last if ($noblock);
 8662:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8663:         foreach my $block (keys(%cachedblockers)) {
 8664:             if ($block =~ /^firstaccess____(.+)$/) {
 8665:                 my $item = $1;
 8666:                 if (($item eq $map) || ($item eq $symb)) {
 8667:                     $noblock = 1;
 8668:                     last;
 8669:                 }
 8670:             }
 8671:             if (ref($cachedblockers{$block}) eq 'HASH') {
 8672:                 if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
 8673:                     if ($cachedblockers{$block}{'resources'}{$symb}) {
 8674:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8675:                             push(@blockers,$block);
 8676:                         }
 8677:                     }
 8678:                 }
 8679:             }
 8680:             if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
 8681:                 if ($cachedblockers{$block}{'maps'}{$map}) {
 8682:                     unless (grep(/^\Q$block\E$/,@blockers)) {
 8683:                         push(@blockers,$block);
 8684:                     }
 8685:                 }
 8686:             }
 8687:         }
 8688:     }
 8689:     return if ($noblock);
 8690:     return @blockers;
 8691: }
 8692: }
 8693: 
 8694: sub deeplink_check {
 8695:     my ($priv,$symb,$uri) = @_;
 8696:     return unless ($env{'request.course.id'});
 8697:     return unless ($priv eq 'bre');
 8698:     return if ($env{'request.state'} eq 'construct');
 8699:     return if ($env{'request.role.adv'});
 8700:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8701:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8702:     my (%possibles,@symbs);
 8703:     if (!$symb) {
 8704:         $symb = &symbread($uri,1,1,1,\%possibles);
 8705:     }
 8706:     if ($symb) {
 8707:         @symbs = ($symb);
 8708:     } elsif (keys(%possibles)) {
 8709:         @symbs = keys(%possibles);
 8710:     }
 8711: 
 8712:     my ($login,$switchrole,$allow);
 8713:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
 8714:         my $key = $1;
 8715:         my $tinyurl;
 8716:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
 8717:         if (defined($cached)) {
 8718:              $tinyurl = $result;
 8719:         } else {
 8720:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
 8721:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
 8722:              if ($currtiny{$key} ne '') {
 8723:                  $tinyurl = $currtiny{$key};
 8724:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
 8725:              }
 8726:         }
 8727:         if ($tinyurl ne '') {
 8728:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
 8729:             if ($cnumreq eq $cnum) {
 8730:                 $login = $posslogin;
 8731:             } else {
 8732:                 $switchrole = 1;
 8733:             }
 8734:         }
 8735:     }
 8736:     foreach my $symb (@symbs) {
 8737:         last if ($allow);
 8738:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 8739:         if ($deeplink eq '') {
 8740:             $allow = 1;
 8741:         } else {
 8742:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 8743:             if ($access eq 'any') {
 8744:                 $allow = 1;
 8745:             } elsif ($login) {
 8746:                 if ($access eq 'only') {
 8747:                     if ($scope eq 'res') {
 8748:                         if ($symb eq $login) {
 8749:                             $allow = 1;
 8750:                         }
 8751:                     } elsif ($scope eq 'map') {
 8752: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
 8753:                     } elsif ($scope eq 'rec') {
 8754: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
 8755:                     }
 8756:                 } else {
 8757:                     my ($acctype,$item) = split(/:/,$access);
 8758:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 8759:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 8760:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8761:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 8762:                                 $allow = 1;
 8763:                             }
 8764:                         }
 8765:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 8766:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 8767:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8768:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 8769:                                 $allow = 1;
 8770:                             }
 8771:                         }
 8772:                     }
 8773:                 }
 8774:             }
 8775:         }
 8776:     }
 8777:     return if ($allow);
 8778:     return 1;
 8779: }
 8780: 
 8781: # -------------------------------- Deversion and split uri into path an filename   
 8782: 
 8783: #
 8784: #   Removes the version from a URI and
 8785: #   splits it in to its filename and path to the filename.
 8786: #   Seems like File::Basename could have done this more clearly.
 8787: #   Parameters:
 8788: #      $uri   - input URI
 8789: #   Returns:
 8790: #     Two element list consisting of 
 8791: #     $pathname  - the URI up to and excluding the trailing /
 8792: #     $filename  - The part of the URI following the last /
 8793: #  NOTE:
 8794: #    Another realization of this is simply:
 8795: #    use File::Basename;
 8796: #    ...
 8797: #    $uri = shift;
 8798: #    $filename = basename($uri);
 8799: #    $path     = dirname($uri);
 8800: #    return ($filename, $path);
 8801: #
 8802: #     The implementation below is probably faster however.
 8803: #
 8804: sub split_uri_for_cond {
 8805:     my $uri=&deversion(&declutter(shift));
 8806:     my @uriparts=split(/\//,$uri);
 8807:     my $filename=pop(@uriparts);
 8808:     my $pathname=join('/',@uriparts);
 8809:     return ($pathname,$filename);
 8810: }
 8811: # --------------------------------------------------- Is a resource on the map?
 8812: 
 8813: sub is_on_map {
 8814:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 8815:     #Trying to find the conditional for the file
 8816:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 8817: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 8818:     if ($match) {
 8819: 	return (1,$1);
 8820:     } else {
 8821: 	return (0,0);
 8822:     }
 8823: }
 8824: 
 8825: # --------------------------------------------------------- Get symb from alias
 8826: 
 8827: sub get_symb_from_alias {
 8828:     my $symb=shift;
 8829:     my ($map,$resid,$url)=&decode_symb($symb);
 8830: # Already is a symb
 8831:     if ($url) { return $symb; }
 8832: # Must be an alias
 8833:     my $aliassymb='';
 8834:     my %bighash;
 8835:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 8836:                             &GDBM_READER(),0640)) {
 8837:         my $rid=$bighash{'mapalias_'.$symb};
 8838: 	if ($rid) {
 8839: 	    my ($mapid,$resid)=split(/\./,$rid);
 8840: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 8841: 				    $resid,$bighash{'src_'.$rid});
 8842: 	}
 8843:         untie %bighash;
 8844:     }
 8845:     return $aliassymb;
 8846: }
 8847: 
 8848: # ----------------------------------------------------------------- Define Role
 8849: 
 8850: sub definerole {
 8851:   if (allowed('mcr','/')) {
 8852:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 8853:     foreach my $role (split(':',$sysrole)) {
 8854: 	my ($crole,$cqual)=split(/\&/,$role);
 8855:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 8856:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 8857: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 8858:                return "refused:s:$crole&$cqual"; 
 8859:             }
 8860:         }
 8861:     }
 8862:     foreach my $role (split(':',$domrole)) {
 8863: 	my ($crole,$cqual)=split(/\&/,$role);
 8864:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 8865:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 8866: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 8867:                return "refused:d:$crole&$cqual"; 
 8868:             }
 8869:         }
 8870:     }
 8871:     foreach my $role (split(':',$courole)) {
 8872: 	my ($crole,$cqual)=split(/\&/,$role);
 8873:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 8874:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 8875: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 8876:                return "refused:c:$crole&$cqual"; 
 8877:             }
 8878:         }
 8879:     }
 8880:     my $uhome;
 8881:     if (($uname ne '') && ($udom ne '')) {
 8882:         $uhome = &homeserver($uname,$udom);
 8883:         return $uhome if ($uhome eq 'no_host');
 8884:     } else {
 8885:         $uname = $env{'user.name'};
 8886:         $udom = $env{'user.domain'};
 8887:         $uhome = $env{'user.home'};
 8888:     }
 8889:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 8890:                 "$udom:$uname:rolesdef_$rolename=".
 8891:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 8892:     return reply($command,$uhome);
 8893:   } else {
 8894:     return 'refused';
 8895:   }
 8896: }
 8897: 
 8898: # ---------------- Make a metadata query against the network of library servers
 8899: 
 8900: sub metadata_query {
 8901:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 8902:     my %rhash;
 8903:     my %libserv = &all_library();
 8904:     my @server_list = (defined($server_array) ? @$server_array
 8905:                                               : keys(%libserv) );
 8906:     for my $server (@server_list) {
 8907:         my $domains = ''; 
 8908:         if (ref($domains_hash) eq 'HASH') {
 8909:             $domains = $domains_hash->{$server}; 
 8910:         }
 8911: 	unless ($custom or $customshow) {
 8912: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 8913: 	    $rhash{$server}=$reply;
 8914: 	}
 8915: 	else {
 8916: 	    my $reply=&reply("querysend:".&escape($query).':'.
 8917: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 8918: 			     $server);
 8919: 	    $rhash{$server}=$reply;
 8920: 	}
 8921:     }
 8922:     return \%rhash;
 8923: }
 8924: 
 8925: # ----------------------------------------- Send log queries and wait for reply
 8926: 
 8927: sub log_query {
 8928:     my ($uname,$udom,$query,%filters)=@_;
 8929:     my $uhome=&homeserver($uname,$udom);
 8930:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 8931:     my $uhost=&hostname($uhome);
 8932:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 8933:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 8934:                        $uhome);
 8935:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 8936:     return get_query_reply($queryid);
 8937: }
 8938: 
 8939: # -------------------------- Update MySQL table for portfolio file
 8940: 
 8941: sub update_portfolio_table {
 8942:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 8943:     if ($group ne '') {
 8944:         $file_name =~s /^\Q$group\E//;
 8945:     }
 8946:     my $homeserver = &homeserver($uname,$udom);
 8947:     my $queryid=
 8948:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 8949:                ':'.&escape($file_name).':'.$action,$homeserver);
 8950:     my $reply = &get_query_reply($queryid);
 8951:     return $reply;
 8952: }
 8953: 
 8954: # -------------------------- Update MySQL allusers table
 8955: 
 8956: sub update_allusers_table {
 8957:     my ($uname,$udom,$names) = @_;
 8958:     my $homeserver = &homeserver($uname,$udom);
 8959:     my $queryid=
 8960:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 8961:                'lastname='.&escape($names->{'lastname'}).'%%'.
 8962:                'firstname='.&escape($names->{'firstname'}).'%%'.
 8963:                'middlename='.&escape($names->{'middlename'}).'%%'.
 8964:                'generation='.&escape($names->{'generation'}).'%%'.
 8965:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 8966:                'id='.&escape($names->{'id'}),$homeserver);
 8967:     return;
 8968: }
 8969: 
 8970: # ------- Request retrieval of institutional classlists for course(s)
 8971: 
 8972: sub fetch_enrollment_query {
 8973:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 8974:     my ($homeserver,$sleep,$loopmax);
 8975:     my $maxtries = 1;
 8976:     if ($context eq 'automated') {
 8977:         $homeserver = $perlvar{'lonHostID'};
 8978:         $sleep = 2;
 8979:         $loopmax = 100;
 8980:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 8981:     } else {
 8982:         $homeserver = &homeserver($cnum,$dom);
 8983:     }
 8984:     my $host=&hostname($homeserver);
 8985:     my $cmd = '';
 8986:     foreach my $affiliate (keys(%{$affiliatesref})) {
 8987:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 8988:     }
 8989:     $cmd =~ s/%%$//;
 8990:     $cmd = &escape($cmd);
 8991:     my $query = 'fetchenrollment';
 8992:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 8993:     unless ($queryid=~/^\Q$host\E\_/) { 
 8994:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 8995:         return 'error: '.$queryid;
 8996:     }
 8997:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 8998:     my $tries = 1;
 8999:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9000:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9001:         $tries ++;
 9002:     }
 9003:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9004:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9005:     } else {
 9006:         my @responses = split(/:/,$reply);
 9007:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9008:             foreach my $line (@responses) {
 9009:                 my ($key,$value) = split(/=/,$line,2);
 9010:                 $$replyref{$key} = $value;
 9011:             }
 9012:         } else {
 9013:             my $pathname = LONCAPA::tempdir();
 9014:             foreach my $line (@responses) {
 9015:                 my ($key,$value) = split(/=/,$line);
 9016:                 $$replyref{$key} = $value;
 9017:                 if ($value > 0) {
 9018:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9019:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9020:                         my $destname = $pathname.'/'.$filename;
 9021:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9022:                         if ($xml_classlist =~ /^error/) {
 9023:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9024:                         } else {
 9025:                             if ( open(FILE,">",$destname) ) {
 9026:                                 print FILE &unescape($xml_classlist);
 9027:                                 close(FILE);
 9028:                             } else {
 9029:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9030:                             }
 9031:                         }
 9032:                     }
 9033:                 }
 9034:             }
 9035:         }
 9036:         return 'ok';
 9037:     }
 9038:     return 'error';
 9039: }
 9040: 
 9041: sub get_query_reply {
 9042:     my ($queryid,$sleep,$loopmax) = @_;;
 9043:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9044:         $sleep = 0.2;
 9045:     }
 9046:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9047:         $loopmax = 100;
 9048:     }
 9049:     my $replyfile=LONCAPA::tempdir().$queryid;
 9050:     my $reply='';
 9051:     for (1..$loopmax) {
 9052: 	sleep($sleep);
 9053:         if (-e $replyfile.'.end') {
 9054: 	    if (open(my $fh,"<",$replyfile)) {
 9055: 		$reply = join('',<$fh>);
 9056: 		close($fh);
 9057: 	   } else { return 'error: reply_file_error'; }
 9058:            return &unescape($reply);
 9059: 	}
 9060:     }
 9061:     return 'timeout:'.$queryid;
 9062: }
 9063: 
 9064: sub courselog_query {
 9065: #
 9066: # possible filters:
 9067: # url: url or symb
 9068: # username
 9069: # domain
 9070: # action: view, submit, grade
 9071: # start: timestamp
 9072: # end: timestamp
 9073: #
 9074:     my (%filters)=@_;
 9075:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9076:     if ($filters{'url'}) {
 9077: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9078:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9079:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9080:     }
 9081:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9082:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9083:     return &log_query($cname,$cdom,'courselog',%filters);
 9084: }
 9085: 
 9086: sub userlog_query {
 9087: #
 9088: # possible filters:
 9089: # action: log check role
 9090: # start: timestamp
 9091: # end: timestamp
 9092: #
 9093:     my ($uname,$udom,%filters)=@_;
 9094:     return &log_query($uname,$udom,'userlog',%filters);
 9095: }
 9096: 
 9097: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9098: 
 9099: sub auto_run {
 9100:     my ($cnum,$cdom) = @_;
 9101:     my $response = 0;
 9102:     my $settings;
 9103:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9104:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9105:         $settings = $domconfig{'autoenroll'};
 9106:         if ($settings->{'run'} eq '1') {
 9107:             $response = 1;
 9108:         }
 9109:     } else {
 9110:         my $homeserver;
 9111:         if (&is_course($cdom,$cnum)) {
 9112:             $homeserver = &homeserver($cnum,$cdom);
 9113:         } else {
 9114:             $homeserver = &domain($cdom,'primary');
 9115:         }
 9116:         if ($homeserver ne 'no_host') {
 9117:             $response = &reply('autorun:'.$cdom,$homeserver);
 9118:         }
 9119:     }
 9120:     return $response;
 9121: }
 9122: 
 9123: sub auto_get_sections {
 9124:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9125:     my $homeserver;
 9126:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9127:         $homeserver = &homeserver($cnum,$cdom);
 9128:     }
 9129:     if (!defined($homeserver)) { 
 9130:         if ($cdom =~ /^$match_domain$/) {
 9131:             $homeserver = &domain($cdom,'primary');
 9132:         }
 9133:     }
 9134:     my @secs;
 9135:     if (defined($homeserver)) {
 9136:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9137:         unless ($response eq 'refused') {
 9138:             @secs = split(/:/,$response);
 9139:         }
 9140:     }
 9141:     return @secs;
 9142: }
 9143: 
 9144: sub auto_new_course {
 9145:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9146:     my $homeserver = &homeserver($cnum,$cdom);
 9147:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9148:     return $response;
 9149: }
 9150: 
 9151: sub auto_validate_courseID {
 9152:     my ($cnum,$cdom,$inst_course_id) = @_;
 9153:     my $homeserver = &homeserver($cnum,$cdom);
 9154:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9155:     return $response;
 9156: }
 9157: 
 9158: sub auto_validate_instcode {
 9159:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9160:     my ($homeserver,$response);
 9161:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9162:         $homeserver = &homeserver($cnum,$cdom);
 9163:     }
 9164:     if (!defined($homeserver)) {
 9165:         if ($cdom =~ /^$match_domain$/) {
 9166:             $homeserver = &domain($cdom,'primary');
 9167:         }
 9168:     }
 9169:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9170:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9171:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9172:     return ($outcome,$description,$defaultcredits);
 9173: }
 9174: 
 9175: sub auto_create_password {
 9176:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9177:     my ($homeserver,$response);
 9178:     my $create_passwd = 0;
 9179:     my $authchk = '';
 9180:     if ($udom =~ /^$match_domain$/) {
 9181:         $homeserver = &domain($udom,'primary');
 9182:     }
 9183:     if ($homeserver eq '') {
 9184:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9185:             $homeserver = &homeserver($cnum,$cdom);
 9186:         }
 9187:     }
 9188:     if ($homeserver eq '') {
 9189:         $authchk = 'nodomain';
 9190:     } else {
 9191:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9192:         if ($response eq 'refused') {
 9193:             $authchk = 'refused';
 9194:         } else {
 9195:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9196:         }
 9197:     }
 9198:     return ($authparam,$create_passwd,$authchk);
 9199: }
 9200: 
 9201: sub auto_photo_permission {
 9202:     my ($cnum,$cdom,$students) = @_;
 9203:     my $homeserver = &homeserver($cnum,$cdom);
 9204:     my ($outcome,$perm_reqd,$conditions) = 
 9205: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9206:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9207: 	return (undef,undef);
 9208:     }
 9209:     return ($outcome,$perm_reqd,$conditions);
 9210: }
 9211: 
 9212: sub auto_checkphotos {
 9213:     my ($uname,$udom,$pid) = @_;
 9214:     my $homeserver = &homeserver($uname,$udom);
 9215:     my ($result,$resulttype);
 9216:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9217: 				   &escape($uname).':'.&escape($pid),
 9218: 				   $homeserver));
 9219:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9220: 	return (undef,undef);
 9221:     }
 9222:     if ($outcome) {
 9223:         ($result,$resulttype) = split(/:/,$outcome);
 9224:     } 
 9225:     return ($result,$resulttype);
 9226: }
 9227: 
 9228: sub auto_photochoice {
 9229:     my ($cnum,$cdom) = @_;
 9230:     my $homeserver = &homeserver($cnum,$cdom);
 9231:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9232: 						       &escape($cdom),
 9233: 						       $homeserver)));
 9234:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9235: 	return (undef,undef);
 9236:     }
 9237:     return ($update,$comment);
 9238: }
 9239: 
 9240: sub auto_photoupdate {
 9241:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9242:     my $homeserver = &homeserver($cnum,$dom);
 9243:     my $host=&hostname($homeserver);
 9244:     my $cmd = '';
 9245:     my $maxtries = 1;
 9246:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9247:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9248:     }
 9249:     $cmd =~ s/%%$//;
 9250:     $cmd = &escape($cmd);
 9251:     my $query = 'institutionalphotos';
 9252:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9253:     unless ($queryid=~/^\Q$host\E\_/) {
 9254:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9255:         return 'error: '.$queryid;
 9256:     }
 9257:     my $reply = &get_query_reply($queryid);
 9258:     my $tries = 1;
 9259:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9260:         $reply = &get_query_reply($queryid);
 9261:         $tries ++;
 9262:     }
 9263:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9264:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9265:     } else {
 9266:         my @responses = split(/:/,$reply);
 9267:         my $outcome = shift(@responses); 
 9268:         foreach my $item (@responses) {
 9269:             my ($key,$value) = split(/=/,$item);
 9270:             $$photo{$key} = $value;
 9271:         }
 9272:         return $outcome;
 9273:     }
 9274:     return 'error';
 9275: }
 9276: 
 9277: sub auto_instcode_format {
 9278:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9279: 	$cat_order) = @_;
 9280:     my $courses = '';
 9281:     my @homeservers;
 9282:     if ($caller eq 'global') {
 9283: 	my %servers = &get_servers($codedom,'library');
 9284: 	foreach my $tryserver (keys(%servers)) {
 9285: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9286: 		push(@homeservers,$tryserver);
 9287: 	    }
 9288:         }
 9289:     } elsif ($caller eq 'requests') {
 9290:         if ($codedom =~ /^$match_domain$/) {
 9291:             my $chome = &domain($codedom,'primary');
 9292:             unless ($chome eq 'no_host') {
 9293:                 push(@homeservers,$chome);
 9294:             }
 9295:         }
 9296:     } else {
 9297:         push(@homeservers,&homeserver($caller,$codedom));
 9298:     }
 9299:     foreach my $code (keys(%{$instcodes})) {
 9300:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9301:     }
 9302:     chop($courses);
 9303:     my $ok_response = 0;
 9304:     my $response;
 9305:     while (@homeservers > 0 && $ok_response == 0) {
 9306:         my $server = shift(@homeservers); 
 9307:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9308:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9309:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9310: 		split(/:/,$response);
 9311:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9312:             push(@{$codetitles},&str2array($codetitles_str));
 9313:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9314:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9315:             $ok_response = 1;
 9316:         }
 9317:     }
 9318:     if ($ok_response) {
 9319:         return 'ok';
 9320:     } else {
 9321:         return $response;
 9322:     }
 9323: }
 9324: 
 9325: sub auto_instcode_defaults {
 9326:     my ($domain,$returnhash,$code_order) = @_;
 9327:     my @homeservers;
 9328: 
 9329:     my %servers = &get_servers($domain,'library');
 9330:     foreach my $tryserver (keys(%servers)) {
 9331: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9332: 	    push(@homeservers,$tryserver);
 9333: 	}
 9334:     }
 9335: 
 9336:     my $response;
 9337:     foreach my $server (@homeservers) {
 9338:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9339:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9340: 	
 9341: 	foreach my $pair (split(/\&/,$response)) {
 9342: 	    my ($name,$value)=split(/\=/,$pair);
 9343: 	    if ($name eq 'code_order') {
 9344: 		@{$code_order} = split(/\&/,&unescape($value));
 9345: 	    } else {
 9346: 		$returnhash->{&unescape($name)}=&unescape($value);
 9347: 	    }
 9348: 	}
 9349: 	return 'ok';
 9350:     }
 9351: 
 9352:     return $response;
 9353: }
 9354: 
 9355: sub auto_possible_instcodes {
 9356:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9357:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9358:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9359:         return;
 9360:     }
 9361:     my (@homeservers,$uhome);
 9362:     if (defined(&domain($domain,'primary'))) {
 9363:         $uhome=&domain($domain,'primary');
 9364:         push(@homeservers,&domain($domain,'primary'));
 9365:     } else {
 9366:         my %servers = &get_servers($domain,'library');
 9367:         foreach my $tryserver (keys(%servers)) {
 9368:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9369:                 push(@homeservers,$tryserver);
 9370:             }
 9371:         }
 9372:     }
 9373:     my $response;
 9374:     foreach my $server (@homeservers) {
 9375:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9376:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9377:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9378:             split(':',$response);
 9379:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9380:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9381:         foreach my $item (split('&',$cat_title)) {   
 9382:             my ($name,$value)=split('=',$item);
 9383:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9384:         }
 9385:         foreach my $item (split('&',$cat_order)) {
 9386:             my ($name,$value)=split('=',$item);
 9387:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9388:         }
 9389:         return 'ok';
 9390:     }
 9391:     return $response;
 9392: }
 9393: 
 9394: sub auto_courserequest_checks {
 9395:     my ($dom) = @_;
 9396:     my ($homeserver,%validations);
 9397:     if ($dom =~ /^$match_domain$/) {
 9398:         $homeserver = &domain($dom,'primary');
 9399:     }
 9400:     unless ($homeserver eq 'no_host') {
 9401:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9402:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9403:             my @items = split(/&/,$response);
 9404:             foreach my $item (@items) {
 9405:                 my ($key,$value) = split('=',$item);
 9406:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9407:             }
 9408:         }
 9409:     }
 9410:     return %validations; 
 9411: }
 9412: 
 9413: sub auto_courserequest_validation {
 9414:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9415:     my ($homeserver,$response);
 9416:     if ($dom =~ /^$match_domain$/) {
 9417:         $homeserver = &domain($dom,'primary');
 9418:     }
 9419:     unless ($homeserver eq 'no_host') {
 9420:         my $customdata;
 9421:         if (ref($custominfo) eq 'HASH') {
 9422:             $customdata = &freeze_escape($custominfo);
 9423:         }
 9424:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9425:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9426:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9427:                                     $customdata,$homeserver));
 9428:     }
 9429:     return $response;
 9430: }
 9431: 
 9432: sub auto_validate_class_sec {
 9433:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9434:     my $homeserver = &homeserver($cnum,$cdom);
 9435:     my $ownerlist;
 9436:     if (ref($owners) eq 'ARRAY') {
 9437:         $ownerlist = join(',',@{$owners});
 9438:     } else {
 9439:         $ownerlist = $owners;
 9440:     }
 9441:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9442:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9443:     return $response;
 9444: }
 9445: 
 9446: sub auto_validate_instclasses {
 9447:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9448:     my ($homeserver,%validations);
 9449:     $homeserver = &homeserver($cnum,$cdom);
 9450:     unless ($homeserver eq 'no_host') {
 9451:         my $ownerlist;
 9452:         if (ref($owners) eq 'ARRAY') {
 9453:             $ownerlist = join(',',@{$owners});
 9454:         } else {
 9455:             $ownerlist = $owners;
 9456:         }
 9457:         if (ref($classesref) eq 'HASH') {
 9458:             my $classes = &freeze_escape($classesref);
 9459:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9460:                                 ':'.$cdom.':'.$classes,$homeserver);
 9461:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9462:                 my @items = split(/&/,$response);
 9463:                 foreach my $item (@items) {
 9464:                     my ($key,$value) = split('=',$item);
 9465:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9466:                 }
 9467:             }
 9468:         }
 9469:     }
 9470:     return %validations;
 9471: }
 9472: 
 9473: sub auto_crsreq_update {
 9474:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9475:         $code,$accessstart,$accessend,$inbound) = @_;
 9476:     my ($homeserver,%crsreqresponse);
 9477:     if ($cdom =~ /^$match_domain$/) {
 9478:         $homeserver = &domain($cdom,'primary');
 9479:     }
 9480:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9481:         my $info;
 9482:         if (ref($inbound) eq 'HASH') {
 9483:             $info = &freeze_escape($inbound);
 9484:         }
 9485:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9486:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9487:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9488:                             &escape($title).':'.&escape($code).':'.
 9489:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9490:                             $homeserver);
 9491:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9492:             my @items = split(/&/,$response);
 9493:             foreach my $item (@items) {
 9494:                 my ($key,$value) = split('=',$item);
 9495:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9496:             }
 9497:         }
 9498:     }
 9499:     return \%crsreqresponse;
 9500: }
 9501: 
 9502: sub auto_export_grades {
 9503:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9504:     my ($homeserver,%exportresponse);
 9505:     if ($cdom =~ /^$match_domain$/) {
 9506:         $homeserver = &domain($cdom,'primary');
 9507:     }
 9508:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9509:         my $info;
 9510:         if (ref($inforef) eq 'HASH') {
 9511:             $info = &freeze_escape($inforef);
 9512:         }
 9513:         if (ref($gradesref) eq 'HASH') {
 9514:             my $grades = &freeze_escape($gradesref);
 9515:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9516:                                 $info.':'.$grades,$homeserver);
 9517:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9518:                 my @items = split(/&/,$response);
 9519:                 foreach my $item (@items) {
 9520:                     my ($key,$value) = split('=',$item);
 9521:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9522:                 }
 9523:             }
 9524:         }
 9525:     }
 9526:     return \%exportresponse;
 9527: }
 9528: 
 9529: sub check_instcode_cloning {
 9530:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9531:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9532:         return;
 9533:     }
 9534:     my $canclone;
 9535:     if (@{$code_order} > 0) {
 9536:         my $instcoderegexp ='^';
 9537:         my @clonecodes = split(/\&/,$cloner);
 9538:         foreach my $item (@{$code_order}) {
 9539:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9540:                 foreach my $pair (@clonecodes) {
 9541:                     my ($key,$val) = split(/\=/,$pair,2);
 9542:                     $val = &unescape($val);
 9543:                     if ($key eq $item) {
 9544:                         $instcoderegexp .= '('.$val.')';
 9545:                         last;
 9546:                     }
 9547:                 }
 9548:             } else {
 9549:                 $instcoderegexp .= $codedefaults->{$item};
 9550:             }
 9551:         }
 9552:         $instcoderegexp .= '$';
 9553:         my (@from,@to);
 9554:         eval {
 9555:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9556:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9557:         };
 9558:         if ((@from > 0) && (@to > 0)) {
 9559:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9560:             if (!@diffs) {
 9561:                 $canclone = 1;
 9562:             }
 9563:         }
 9564:     }
 9565:     return $canclone;
 9566: }
 9567: 
 9568: sub default_instcode_cloning {
 9569:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9570:     my (%codedefaults,@code_order,$canclone);
 9571:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9572:         %codedefaults = %{$codedefaultsref};
 9573:         @code_order = @{$codeorderref};
 9574:     } elsif ($clonedom) {
 9575:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9576:     }
 9577:     if (($domdefclone) && (@code_order)) {
 9578:         my @clonecodes = split(/\+/,$domdefclone);
 9579:         my $instcoderegexp ='^';
 9580:         foreach my $item (@code_order) {
 9581:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9582:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9583:             } else {
 9584:                 $instcoderegexp .= $codedefaults{$item};
 9585:             }
 9586:         }
 9587:         $instcoderegexp .= '$';
 9588:         my (@from,@to);
 9589:         eval {
 9590:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9591:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9592:         };
 9593:         if ((@from > 0) && (@to > 0)) {
 9594:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9595:             if (!@diffs) {
 9596:                 $canclone = 1;
 9597:             }
 9598:         }
 9599:     }
 9600:     return $canclone;
 9601: }
 9602: 
 9603: # ------------------------------------------------------- Course Group routines
 9604: 
 9605: sub get_coursegroups {
 9606:     my ($cdom,$cnum,$group,$namespace) = @_;
 9607:     return(&dump($namespace,$cdom,$cnum,$group));
 9608: }
 9609: 
 9610: sub modify_coursegroup {
 9611:     my ($cdom,$cnum,$groupsettings) = @_;
 9612:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9613: }
 9614: 
 9615: sub toggle_coursegroup_status {
 9616:     my ($cdom,$cnum,$group,$action) = @_;
 9617:     my ($from_namespace,$to_namespace);
 9618:     if ($action eq 'delete') {
 9619:         $from_namespace = 'coursegroups';
 9620:         $to_namespace = 'deleted_groups';
 9621:     } else {
 9622:         $from_namespace = 'deleted_groups';
 9623:         $to_namespace = 'coursegroups';
 9624:     }
 9625:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9626:     if (my $tmp = &error(%curr_group)) {
 9627:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9628:         return ('read error',$tmp);
 9629:     } else {
 9630:         my %savedsettings = %curr_group; 
 9631:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9632:         my $deloutcome;
 9633:         if ($result eq 'ok') {
 9634:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9635:         } else {
 9636:             return ('write error',$result);
 9637:         }
 9638:         if ($deloutcome eq 'ok') {
 9639:             return 'ok';
 9640:         } else {
 9641:             return ('delete error',$deloutcome);
 9642:         }
 9643:     }
 9644: }
 9645: 
 9646: sub modify_group_roles {
 9647:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9648:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9649:     my $role = 'gr/'.&escape($userprivs);
 9650:     my ($uname,$udom) = split(/:/,$user);
 9651:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9652:     if ($result eq 'ok') {
 9653:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9654:     }
 9655:     return $result;
 9656: }
 9657: 
 9658: sub modify_coursegroup_membership {
 9659:     my ($cdom,$cnum,$membership) = @_;
 9660:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9661:     return $result;
 9662: }
 9663: 
 9664: sub get_active_groups {
 9665:     my ($udom,$uname,$cdom,$cnum) = @_;
 9666:     my $now = time;
 9667:     my %groups = ();
 9668:     foreach my $key (keys(%env)) {
 9669:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9670:             my ($start,$end) = split(/\./,$env{$key});
 9671:             if (($end!=0) && ($end<$now)) { next; }
 9672:             if (($start!=0) && ($start>$now)) { next; }
 9673:             if ($1 eq $cdom && $2 eq $cnum) {
 9674:                 $groups{$3} = $env{$key} ;
 9675:             }
 9676:         }
 9677:     }
 9678:     return %groups;
 9679: }
 9680: 
 9681: sub get_group_membership {
 9682:     my ($cdom,$cnum,$group) = @_;
 9683:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9684: }
 9685: 
 9686: sub get_users_groups {
 9687:     my ($udom,$uname,$courseid) = @_;
 9688:     my @usersgroups;
 9689:     my $cachetime=1800;
 9690: 
 9691:     my $hashid="$udom:$uname:$courseid";
 9692:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9693:     if (defined($cached)) {
 9694:         @usersgroups = split(/:/,$grouplist);
 9695:     } else {  
 9696:         $grouplist = '';
 9697:         my $courseurl = &courseid_to_courseurl($courseid);
 9698:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9699:         my $access_end = $env{'course.'.$courseid.
 9700:                               '.default_enrollment_end_date'};
 9701:         my $now = time;
 9702:         foreach my $key (keys(%roleshash)) {
 9703:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 9704:                 my $group = $1;
 9705:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 9706:                     my $start = $2;
 9707:                     my $end = $1;
 9708:                     if ($start == -1) { next; } # deleted from group
 9709:                     if (($start!=0) && ($start>$now)) { next; }
 9710:                     if (($end!=0) && ($end<$now)) {
 9711:                         if ($access_end && $access_end < $now) {
 9712:                             if ($access_end - $end < 86400) {
 9713:                                 push(@usersgroups,$group);
 9714:                             }
 9715:                         }
 9716:                         next;
 9717:                     }
 9718:                     push(@usersgroups,$group);
 9719:                 }
 9720:             }
 9721:         }
 9722:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 9723:         $grouplist = join(':',@usersgroups);
 9724:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 9725:     }
 9726:     return @usersgroups;
 9727: }
 9728: 
 9729: sub devalidate_getgroups_cache {
 9730:     my ($udom,$uname,$cdom,$cnum)=@_;
 9731:     my $courseid = $cdom.'_'.$cnum;
 9732: 
 9733:     my $hashid="$udom:$uname:$courseid";
 9734:     &devalidate_cache_new('getgroups',$hashid);
 9735: }
 9736: 
 9737: # ------------------------------------------------------------------ Plain Text
 9738: 
 9739: sub plaintext {
 9740:     my ($short,$type,$cid,$forcedefault) = @_;
 9741:     if ($short =~ m{^cr/}) {
 9742: 	return (split('/',$short))[-1];
 9743:     }
 9744:     if (!defined($cid)) {
 9745:         $cid = $env{'request.course.id'};
 9746:     }
 9747:     my %rolenames = (
 9748:                       Course    => 'std',
 9749:                       Community => 'alt1',
 9750:                       Placement => 'std',
 9751:                     );
 9752:     if ($cid ne '') {
 9753:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
 9754:             unless ($forcedefault) {
 9755:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
 9756:                 &Apache::lonlocal::mt_escape(\$roletext);
 9757:                 return &Apache::lonlocal::mt($roletext);
 9758:             }
 9759:         }
 9760:     }
 9761:     if ((defined($type)) && (defined($rolenames{$type})) &&
 9762:         (defined($rolenames{$type})) && 
 9763:         (defined($prp{$short}{$rolenames{$type}}))) {
 9764:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 9765:     } elsif ($cid ne '') {
 9766:         my $crstype = $env{'course.'.$cid.'.type'};
 9767:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
 9768:             (defined($prp{$short}{$rolenames{$crstype}}))) {
 9769:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
 9770:         }
 9771:     }
 9772:     return &Apache::lonlocal::mt($prp{$short}{'std'});
 9773: }
 9774: 
 9775: # ----------------------------------------------------------------- Assign Role
 9776: 
 9777: sub assignrole {
 9778:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
 9779:         $context)=@_;
 9780:     my $mrole;
 9781:     if ($role =~ /^cr\//) {
 9782:         my $cwosec=$url;
 9783:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9784: 	unless (&allowed('ccr',$cwosec)) {
 9785:            my $refused = 1;
 9786:            if ($context eq 'requestcourses') {
 9787:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 9788:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
 9789:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
 9790:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9791:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9792:                            if ($crsenv{'internal.courseowner'} eq
 9793:                                $env{'user.name'}.':'.$env{'user.domain'}) {
 9794:                                $refused = '';
 9795:                            }
 9796:                        }
 9797:                    }
 9798:                }
 9799:            }
 9800:            if ($refused) {
 9801:                &logthis('Refused custom assignrole: '.
 9802:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
 9803:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
 9804:                return 'refused';
 9805:            }
 9806:         }
 9807:         $mrole='cr';
 9808:     } elsif ($role =~ /^gr\//) {
 9809:         my $cwogrp=$url;
 9810:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
 9811:         unless (&allowed('mdg',$cwogrp)) {
 9812:             &logthis('Refused group assignrole: '.
 9813:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
 9814:                     $env{'user.name'}.' at '.$env{'user.domain'});
 9815:             return 'refused';
 9816:         }
 9817:         $mrole='gr';
 9818:     } else {
 9819:         my $cwosec=$url;
 9820:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9821:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
 9822:             my $refused;
 9823:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
 9824:                 if (!(&allowed('c'.$role,$url))) {
 9825:                     $refused = 1;
 9826:                 }
 9827:             } else {
 9828:                 $refused = 1;
 9829:             }
 9830:             if ($refused) {
 9831:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9832:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
 9833:                     my %crsenv;
 9834:                     if ($role eq 'cc' || $role eq 'co') {
 9835:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9836:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
 9837:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
 9838:                                 if ($crsenv{'internal.courseowner'} eq 
 9839:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
 9840:                                     $refused = '';
 9841:                                 }
 9842:                             }
 9843:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
 9844:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
 9845:                                 if ($crsenv{'internal.courseowner'} eq 
 9846:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
 9847:                                     $refused = '';
 9848:                                 }
 9849:                             }
 9850:                         }
 9851:                     }
 9852:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 9853:                     if ($role eq 'st') {
 9854:                         $refused = '';
 9855:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
 9856:                         $refused = '';
 9857:                     }
 9858:                 } elsif ($context eq 'requestcourses') {
 9859:                     my @possroles = ('st','ta','ep','in','cc','co');
 9860:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
 9861:                         my $wrongcc;
 9862:                         if ($cnum =~ /^$match_community$/) {
 9863:                             $wrongcc = 1 if ($role eq 'cc');
 9864:                         } else {
 9865:                             $wrongcc = 1 if ($role eq 'co');
 9866:                         }
 9867:                         unless ($wrongcc) {
 9868:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9869:                             if ($crsenv{'internal.courseowner'} eq 
 9870:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
 9871:                                 $refused = '';
 9872:                             }
 9873:                         }
 9874:                     }
 9875:                 } elsif ($context eq 'requestauthor') {
 9876:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
 9877:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
 9878:                         if ($env{'environment.requestauthor'} eq 'automatic') {
 9879:                             $refused = '';
 9880:                         } else {
 9881:                             my %domdefaults = &get_domain_defaults($udom);
 9882:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
 9883:                                 my $checkbystatus;
 9884:                                 if ($env{'user.adv'}) { 
 9885:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
 9886:                                     if ($disposition eq 'automatic') {
 9887:                                         $refused = '';
 9888:                                     } elsif ($disposition eq '') {
 9889:                                         $checkbystatus = 1;
 9890:                                     } 
 9891:                                 } else {
 9892:                                     $checkbystatus = 1;
 9893:                                 }
 9894:                                 if ($checkbystatus) {
 9895:                                     if ($env{'environment.inststatus'}) {
 9896:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
 9897:                                         foreach my $type (@inststatuses) {
 9898:                                             if (($type ne '') &&
 9899:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
 9900:                                                 $refused = '';
 9901:                                             }
 9902:                                         }
 9903:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
 9904:                                         $refused = '';
 9905:                                     }
 9906:                                 }
 9907:                             }
 9908:                         }
 9909:                     }
 9910:                 }
 9911:                 if ($refused) {
 9912:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
 9913:                              ' '.$role.' '.$end.' '.$start.' by '.
 9914: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
 9915:                     return 'refused';
 9916:                 }
 9917:             }
 9918:         } elsif ($role eq 'au') {
 9919:             if ($url ne '/'.$udom.'/') {
 9920:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
 9921:                          ' to assign author role for '.$uname.':'.$udom.
 9922:                          ' in domain: '.$url.' refused (wrong domain).');
 9923:                 return 'refused';
 9924:             }
 9925:         }
 9926:         $mrole=$role;
 9927:     }
 9928:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9929:                 "$udom:$uname:$url".'_'."$mrole=$role";
 9930:     if ($end) { $command.='_'.$end; }
 9931:     if ($start) {
 9932: 	if ($end) { 
 9933:            $command.='_'.$start; 
 9934:         } else {
 9935:            $command.='_0_'.$start;
 9936:         }
 9937:     }
 9938:     my $origstart = $start;
 9939:     my $origend = $end;
 9940:     my $delflag;
 9941: # actually delete
 9942:     if ($deleteflag) {
 9943: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
 9944: # modify command to delete the role
 9945:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
 9946:                 "$udom:$uname:$url".'_'."$mrole";
 9947: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
 9948: # set start and finish to negative values for userrolelog
 9949:            $start=-1;
 9950:            $end=-1;
 9951:            $delflag = 1;
 9952:         }
 9953:     }
 9954: # send command
 9955:     my $answer=&reply($command,&homeserver($uname,$udom));
 9956: # log new user role if status is ok
 9957:     if ($answer eq 'ok') {
 9958: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
 9959:         if (($role eq 'cc') || ($role eq 'in') ||
 9960:             ($role eq 'ep') || ($role eq 'ad') ||
 9961:             ($role eq 'ta') || ($role eq 'st') ||
 9962:             ($role=~/^cr/) || ($role eq 'gr') ||
 9963:             ($role eq 'co')) {
 9964: # for course roles, perform group memberships changes triggered by role change.
 9965:             unless ($role =~ /^gr/) {
 9966:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
 9967:                                                  $origstart,$selfenroll,$context);
 9968:             }
 9969:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9970:                            $selfenroll,$context);
 9971:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
 9972:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
 9973:                  ($role eq 'da')) {
 9974:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9975:                            $context);
 9976:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
 9977:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
 9978:                              $context); 
 9979:         }
 9980:         if ($role eq 'cc') {
 9981:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
 9982:         }
 9983:     }
 9984:     return $answer;
 9985: }
 9986: 
 9987: sub autoupdate_coowners {
 9988:     my ($url,$end,$start,$uname,$udom) = @_;
 9989:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
 9990:     if (($cdom ne '') && ($cnum ne '')) {
 9991:         my $now = time;
 9992:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
 9993:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
 9994:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
 9995:             my $instcode = $coursehash{'internal.coursecode'};
 9996:             if ($instcode ne '') {
 9997:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
 9998:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
 9999:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10000:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10001:                         if ($result eq 'valid') {
10002:                             if ($coursehash{'internal.co-owners'}) {
10003:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10004:                                     push(@newcoowners,$coowner);
10005:                                 }
10006:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10007:                                     push(@newcoowners,$uname.':'.$udom);
10008:                                 }
10009:                                 @newcoowners = sort(@newcoowners);
10010:                             } else {
10011:                                 push(@newcoowners,$uname.':'.$udom);
10012:                             }
10013:                         } else {
10014:                             if ($coursehash{'internal.co-owners'}) {
10015:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10016:                                     unless ($coowner eq $uname.':'.$udom) {
10017:                                         push(@newcoowners,$coowner);
10018:                                     }
10019:                                 }
10020:                                 unless (@newcoowners > 0) {
10021:                                     $delcoowners = 1;
10022:                                     $coowners = '';
10023:                                 }
10024:                             }
10025:                         }
10026:                         if (@newcoowners || $delcoowners) {
10027:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10028:                                             $delcoowners,@newcoowners);
10029:                         }
10030:                     }
10031:                 }
10032:             }
10033:         }
10034:     }
10035: }
10036: 
10037: sub store_coowners {
10038:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10039:     my $cid = $cdom.'_'.$cnum;
10040:     my ($coowners,$delresult,$putresult);
10041:     if (@newcoowners) {
10042:         $coowners = join(',',@newcoowners);
10043:         my %coownershash = (
10044:                             'internal.co-owners' => $coowners,
10045:                            );
10046:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10047:         if ($putresult eq 'ok') {
10048:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10049:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10050:             }
10051:         }
10052:     }
10053:     if ($delcoowners) {
10054:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10055:         if ($delresult eq 'ok') {
10056:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10057:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10058:             }
10059:         }
10060:     }
10061:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10062:         my %crsinfo =
10063:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10064:         if (ref($crsinfo{$cid}) eq 'HASH') {
10065:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10066:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10067:         }
10068:     }
10069: }
10070: 
10071: # -------------------------------------------------- Modify user authentication
10072: # Overrides without validation
10073: 
10074: sub modifyuserauth {
10075:     my ($udom,$uname,$umode,$upass)=@_;
10076:     my $uhome=&homeserver($uname,$udom);
10077:     unless (&allowed('mau',$udom)) { return 'refused'; }
10078:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10079:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10080:              ' in domain '.$env{'request.role.domain'});  
10081:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10082: 		     &escape($upass),$uhome);
10083:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10084:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10085:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10086:     &log($udom,,$uname,$uhome,
10087:         'Authentication changed by '.$env{'user.domain'}.', '.
10088:                                      $env{'user.name'}.', '.$umode.
10089:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10090:     unless ($reply eq 'ok') {
10091:         &logthis('Authentication mode error: '.$reply);
10092: 	return 'error: '.$reply;
10093:     }   
10094:     return 'ok';
10095: }
10096: 
10097: # --------------------------------------------------------------- Modify a user
10098: 
10099: sub modifyuser {
10100:     my ($udom,    $uname, $uid,
10101:         $umode,   $upass, $first,
10102:         $middle,  $last,  $gene,
10103:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10104:     $udom= &LONCAPA::clean_domain($udom);
10105:     $uname=&LONCAPA::clean_username($uname);
10106:     my $showcandelete = 'none';
10107:     if (ref($candelete) eq 'ARRAY') {
10108:         if (@{$candelete} > 0) {
10109:             $showcandelete = join(', ',@{$candelete});
10110:         }
10111:     }
10112:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10113:              $umode.', '.$first.', '.$middle.', '.
10114: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10115:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10116:                                      ' desiredhome not specified'). 
10117:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10118:              ' in domain '.$env{'request.role.domain'});
10119:     my $uhome=&homeserver($uname,$udom,'true');
10120:     my $newuser;
10121:     if ($uhome eq 'no_host') {
10122:         $newuser = 1;
10123:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10124:                 ($umode eq 'lti')) {
10125:             return 'error: more information needed to create new user';
10126:         }
10127:     }
10128: # ----------------------------------------------------------------- Create User
10129:     if (($uhome eq 'no_host') && 
10130: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10131:         my $unhome='';
10132:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10133:             $unhome = $desiredhome;
10134: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10135: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10136:         } else { # load balancing routine for determining $unhome
10137:             my $loadm=10000000;
10138: 	    my %servers = &get_servers($udom,'library');
10139: 	    foreach my $tryserver (keys(%servers)) {
10140: 		my $answer=reply('load',$tryserver);
10141: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10142: 		    $loadm=$answer;
10143: 		    $unhome=$tryserver;
10144: 		}
10145: 	    }
10146:         }
10147:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10148: 	    return 'error: unable to find a home server for '.$uname.
10149:                    ' in domain '.$udom;
10150:         }
10151:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10152:                          &escape($upass),$unhome);
10153: 	unless ($reply eq 'ok') {
10154:             return 'error: '.$reply;
10155:         }   
10156:         $uhome=&homeserver($uname,$udom,'true');
10157:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10158: 	    return 'error: unable verify users home machine.';
10159:         }
10160:     }   # End of creation of new user
10161: # ---------------------------------------------------------------------- Add ID
10162:     if ($uid) {
10163:        $uid=~tr/A-Z/a-z/;
10164:        my %uidhash=&idrget($udom,$uname);
10165:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10166:          && (!$forceid)) {
10167: 	  unless ($uid eq $uidhash{$uname}) {
10168: 	      return 'error: user id "'.$uid.'" does not match '.
10169:                   'current user id "'.$uidhash{$uname}.'".';
10170:           }
10171:        } else {
10172: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10173:        }
10174:     }
10175: # -------------------------------------------------------------- Add names, etc
10176:     my @tmp=&get('environment',
10177: 		   ['firstname','middlename','lastname','generation','id',
10178:                     'permanentemail','inststatus'],
10179: 		   $udom,$uname);
10180:     my (%names,%oldnames);
10181:     if ($tmp[0] =~ m/^error:.*/) { 
10182:         %names=(); 
10183:     } else {
10184:         %names = @tmp;
10185:         %oldnames = %names;
10186:     }
10187: #
10188: # If name, email and/or uid are blank (e.g., because an uploaded file
10189: # of users did not contain them), do not overwrite existing values
10190: # unless field is in $candelete array ref.  
10191: #
10192: 
10193:     my @fields = ('firstname','middlename','lastname','generation',
10194:                   'permanentemail','id');
10195:     my %newvalues;
10196:     if (ref($candelete) eq 'ARRAY') {
10197:         foreach my $field (@fields) {
10198:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10199:                 if ($field eq 'firstname') {
10200:                     $names{$field} = $first;
10201:                 } elsif ($field eq 'middlename') {
10202:                     $names{$field} = $middle;
10203:                 } elsif ($field eq 'lastname') {
10204:                     $names{$field} = $last;
10205:                 } elsif ($field eq 'generation') { 
10206:                     $names{$field} = $gene;
10207:                 } elsif ($field eq 'permanentemail') {
10208:                     $names{$field} = $email;
10209:                 } elsif ($field eq 'id') {
10210:                     $names{$field}  = $uid;
10211:                 }
10212:             }
10213:         }
10214:     }
10215:     if ($first)  { $names{'firstname'}  = $first; }
10216:     if (defined($middle)) { $names{'middlename'} = $middle; }
10217:     if ($last)   { $names{'lastname'}   = $last; }
10218:     if (defined($gene))   { $names{'generation'} = $gene; }
10219:     if ($email) {
10220:        $email=~s/[^\w\@\.\-\,]//gs;
10221:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10222:     }
10223:     if ($uid) { $names{'id'}  = $uid; }
10224:     if (defined($inststatus)) {
10225:         $names{'inststatus'} = '';
10226:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10227:         if (ref($usertypes) eq 'HASH') {
10228:             my @okstatuses; 
10229:             foreach my $item (split(/:/,$inststatus)) {
10230:                 if (defined($usertypes->{$item})) {
10231:                     push(@okstatuses,$item);  
10232:                 }
10233:             }
10234:             if (@okstatuses) {
10235:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10236:             }
10237:         }
10238:     }
10239:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10240:                  $umode.', '.$first.', '.$middle.', '.
10241:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10242:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10243:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10244:     } else {
10245:         $logmsg .= ' during self creation';
10246:     }
10247:     my $changed;
10248:     if ($newuser) {
10249:         $changed = 1;
10250:     } else {
10251:         foreach my $field (@fields) {
10252:             if ($names{$field} ne $oldnames{$field}) {
10253:                 $changed = 1;
10254:                 last;
10255:             }
10256:         }
10257:     }
10258:     unless ($changed) {
10259:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10260:         &logthis($logmsg);
10261:         return 'ok';
10262:     }
10263:     my $reply = &put('environment', \%names, $udom,$uname);
10264:     if ($reply ne 'ok') { 
10265:         return 'error: '.$reply;
10266:     }
10267:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10268:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10269:     }
10270:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10271:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10272:     $logmsg = 'Success modifying user '.$logmsg;
10273:     &logthis($logmsg);
10274:     return 'ok';
10275: }
10276: 
10277: # -------------------------------------------------------------- Modify student
10278: 
10279: sub modifystudent {
10280:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10281:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10282:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10283:     if (!$cid) {
10284: 	unless ($cid=$env{'request.course.id'}) {
10285: 	    return 'not_in_class';
10286: 	}
10287:     }
10288: # --------------------------------------------------------------- Make the user
10289:     my $reply=&modifyuser
10290: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10291:          $desiredhome,$email,$inststatus);
10292:     unless ($reply eq 'ok') { return $reply; }
10293:     # This will cause &modify_student_enrollment to get the uid from the
10294:     # student's environment
10295:     $uid = undef if (!$forceid);
10296:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10297:                                         $gene,$usec,$end,$start,$type,$locktype,
10298:                                         $cid,$selfenroll,$context,$credits,$instsec);
10299:     return $reply;
10300: }
10301: 
10302: sub modify_student_enrollment {
10303:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10304:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10305:     my ($cdom,$cnum,$chome);
10306:     if (!$cid) {
10307: 	unless ($cid=$env{'request.course.id'}) {
10308: 	    return 'not_in_class';
10309: 	}
10310: 	$cdom=$env{'course.'.$cid.'.domain'};
10311: 	$cnum=$env{'course.'.$cid.'.num'};
10312:     } else {
10313: 	($cdom,$cnum)=split(/_/,$cid);
10314:     }
10315:     $chome=$env{'course.'.$cid.'.home'};
10316:     if (!$chome) {
10317: 	$chome=&homeserver($cnum,$cdom);
10318:     }
10319:     if (!$chome) { return 'unknown_course'; }
10320:     # Make sure the user exists
10321:     my $uhome=&homeserver($uname,$udom);
10322:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10323: 	return 'error: no such user';
10324:     }
10325:     # Get student data if we were not given enough information
10326:     if (!defined($first)  || $first  eq '' || 
10327:         !defined($last)   || $last   eq '' || 
10328:         !defined($uid)    || $uid    eq '' || 
10329:         !defined($middle) || $middle eq '' || 
10330:         !defined($gene)   || $gene   eq '') {
10331:         # They did not supply us with enough data to enroll the student, so
10332:         # we need to pick up more information.
10333:         my %tmp = &get('environment',
10334:                        ['firstname','middlename','lastname', 'generation','id']
10335:                        ,$udom,$uname);
10336: 
10337:         #foreach my $key (keys(%tmp)) {
10338:         #    &logthis("key $key = ".$tmp{$key});
10339:         #}
10340:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10341:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10342:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10343:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10344:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10345:     }
10346:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10347:     my $user = "$uname:$udom";
10348:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10349:     my $reply=cput('classlist',
10350: 		   {$user => 
10351: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10352: 		   $cdom,$cnum);
10353:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10354:         &devalidate_getsection_cache($udom,$uname,$cid);
10355:     } else { 
10356: 	return 'error: '.$reply;
10357:     }
10358:     # Add student role to user
10359:     my $uurl='/'.$cid;
10360:     $uurl=~s/\_/\//g;
10361:     if ($usec) {
10362: 	$uurl.='/'.$usec;
10363:     }
10364:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10365:                              $selfenroll,$context);
10366:     if ($result ne 'ok') {
10367:         if ($old_entry{$user} ne '') {
10368:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10369:         } else {
10370:             $reply = &del('classlist',[$user],$cdom,$cnum);
10371:         }
10372:     }
10373:     return $result; 
10374: }
10375: 
10376: sub format_name {
10377:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10378:     my $name;
10379:     if ($first ne 'lastname') {
10380: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10381:     } else {
10382: 	if ($lastname=~/\S/) {
10383: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10384: 	    $name=~s/\s+,/,/;
10385: 	} else {
10386: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10387: 	}
10388:     }
10389:     $name=~s/^\s+//;
10390:     $name=~s/\s+$//;
10391:     $name=~s/\s+/ /g;
10392:     return $name;
10393: }
10394: 
10395: # ------------------------------------------------- Write to course preferences
10396: 
10397: sub writecoursepref {
10398:     my ($courseid,%prefs)=@_;
10399:     $courseid=~s/^\///;
10400:     $courseid=~s/\_/\//g;
10401:     my ($cdomain,$cnum)=split(/\//,$courseid);
10402:     my $chome=homeserver($cnum,$cdomain);
10403:     if (($chome eq '') || ($chome eq 'no_host')) { 
10404: 	return 'error: no such course';
10405:     }
10406:     my $cstring='';
10407:     foreach my $pref (keys(%prefs)) {
10408: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10409:     }
10410:     $cstring=~s/\&$//;
10411:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10412: }
10413: 
10414: # ---------------------------------------------------------- Make/modify course
10415: 
10416: sub createcourse {
10417:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10418:         $course_owner,$crstype,$cnum,$context,$category)=@_;
10419:     $url=&declutter($url);
10420:     my $cid='';
10421:     if ($context eq 'requestcourses') {
10422:         my $can_create = 0;
10423:         my ($ownername,$ownerdom) = split(':',$course_owner);
10424:         if ($udom eq $ownerdom) {
10425:             if (&usertools_access($ownername,$ownerdom,$category,undef,
10426:                                   $context)) {
10427:                 $can_create = 1;
10428:             }
10429:         } else {
10430:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10431:                                            $category);
10432:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10433:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10434:                 if (@curr > 0) {
10435:                     my @options = qw(approval validate autolimit);
10436:                     my $optregex = join('|',@options);
10437:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10438:                         $can_create = 1;
10439:                     }
10440:                 }
10441:             }
10442:         }
10443:         if ($can_create) {
10444:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10445:                 unless (&allowed('ccc',$udom)) {
10446:                     return 'refused'; 
10447:                 }
10448:             }
10449:         } else {
10450:             return 'refused';
10451:         }
10452:     } elsif (!&allowed('ccc',$udom)) {
10453:         return 'refused';
10454:     }
10455: # --------------------------------------------------------------- Get Unique ID
10456:     my $uname;
10457:     if ($cnum =~ /^$match_courseid$/) {
10458:         my $chome=&homeserver($cnum,$udom,'true');
10459:         if (($chome eq '') || ($chome eq 'no_host')) {
10460:             $uname = $cnum;
10461:         } else {
10462:             $uname = &generate_coursenum($udom,$crstype);
10463:         }
10464:     } else {
10465:         $uname = &generate_coursenum($udom,$crstype);
10466:     }
10467:     return $uname if ($uname =~ /^error/);
10468: # -------------------------------------------------- Check supplied server name
10469:     if (!defined($course_server)) {
10470:         if (defined(&domain($udom,'primary'))) {
10471:             $course_server = &domain($udom,'primary');
10472:         } else {
10473:             $course_server = $env{'user.home'}; 
10474:         }
10475:     }
10476:     my %host_servers =
10477:         &Apache::lonnet::get_servers($udom,'library');
10478:     unless ($host_servers{$course_server}) {
10479:         return 'error: invalid home server for course: '.$course_server;
10480:     }
10481: # ------------------------------------------------------------- Make the course
10482:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10483:                       $course_server);
10484:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10485:     my $uhome=&homeserver($uname,$udom,'true');
10486:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10487: 	return 'error: no such course';
10488:     }
10489: # ----------------------------------------------------------------- Course made
10490: # log existence
10491:     my $now = time;
10492:     my $newcourse = {
10493:                     $udom.'_'.$uname => {
10494:                                      description => $description,
10495:                                      inst_code   => $inst_code,
10496:                                      owner       => $course_owner,
10497:                                      type        => $crstype,
10498:                                      creator     => $env{'user.name'}.':'.
10499:                                                     $env{'user.domain'},
10500:                                      created     => $now,
10501:                                      context     => $context,
10502:                                                 },
10503:                     };
10504:     &courseidput($udom,$newcourse,$uhome,'notime');
10505: # set toplevel url
10506:     my $topurl=$url;
10507:     unless ($nonstandard) {
10508: # ------------------------------------------ For standard courses, make top url
10509:         my $mapurl=&clutter($url);
10510:         if ($mapurl eq '/res/') { $mapurl=''; }
10511:         $env{'form.initmap'}=(<<ENDINITMAP);
10512: <map>
10513: <resource id="1" type="start"></resource>
10514: <resource id="2" src="$mapurl"></resource>
10515: <resource id="3" type="finish"></resource>
10516: <link index="1" from="1" to="2"></link>
10517: <link index="2" from="2" to="3"></link>
10518: </map>
10519: ENDINITMAP
10520:         $topurl=&declutter(
10521:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10522:                           );
10523:     }
10524: # ----------------------------------------------------------- Write preferences
10525:     &writecoursepref($udom.'_'.$uname,
10526:                      ('description'              => $description,
10527:                       'url'                      => $topurl,
10528:                       'internal.creator'         => $env{'user.name'}.':'.
10529:                                                     $env{'user.domain'},
10530:                       'internal.created'         => $now,
10531:                       'internal.creationcontext' => $context)
10532:                     );
10533:     return '/'.$udom.'/'.$uname;
10534: }
10535: 
10536: # ------------------------------------------------------------------- Create ID
10537: sub generate_coursenum {
10538:     my ($udom,$crstype) = @_;
10539:     my $domdesc = &domain($udom);
10540:     return 'error: invalid domain' if ($domdesc eq '');
10541:     my $first;
10542:     if ($crstype eq 'Community') {
10543:         $first = '0';
10544:     } else {
10545:         $first = int(1+rand(9)); 
10546:     } 
10547:     my $uname=$first.
10548:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10549:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10550:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10551: # ----------------------------------------------- Make sure that does not exist
10552:     my $uhome=&homeserver($uname,$udom,'true');
10553:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10554:         if ($crstype eq 'Community') {
10555:             $first = '0';
10556:         } else {
10557:             $first = int(1+rand(9));
10558:         }
10559:         $uname=$first.
10560:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10561:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10562:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10563:         $uhome=&homeserver($uname,$udom,'true');
10564:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10565:             return 'error: unable to generate unique course-ID';
10566:         }
10567:     }
10568:     return $uname;
10569: }
10570: 
10571: sub is_course {
10572:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10573:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10574: 
10575:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10576:     my $uhome=&homeserver($cnum,$cdom);
10577:     my $iscourse;
10578:     if (grep { $_ eq $uhome } current_machine_ids()) {
10579:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10580:     } else {
10581:         my $hashid = $cdom.':'.$cnum;
10582:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10583:         unless (defined($cached)) {
10584:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
10585:                                         $cnum,undef,undef,'.');
10586:             $iscourse = 0;
10587:             if (exists($courses{$cdom.'_'.$cnum})) {
10588:                 $iscourse = 1;
10589:             }
10590:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
10591:         }
10592:     }
10593:     return unless ($iscourse);
10594:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10595: }
10596: 
10597: sub store_userdata {
10598:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10599:     my $result;
10600:     if ($datakey ne '') {
10601:         if (ref($storehash) eq 'HASH') {
10602:             if ($udom eq '' || $uname eq '') {
10603:                 $udom = $env{'user.domain'};
10604:                 $uname = $env{'user.name'};
10605:             }
10606:             my $uhome=&homeserver($uname,$udom);
10607:             if (($uhome eq '') || ($uhome eq 'no_host')) {
10608:                 $result = 'error: no_host';
10609:             } else {
10610:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
10611:                 $storehash->{'host'} = $perlvar{'lonHostID'};
10612: 
10613:                 my $namevalue='';
10614:                 foreach my $key (keys(%{$storehash})) {
10615:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10616:                 }
10617:                 $namevalue=~s/\&$//;
10618:                 unless ($namespace eq 'courserequests') {
10619:                     $datakey = &escape($datakey);
10620:                 }
10621:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
10622:                                   $namevalue,$uhome);
10623:             }
10624:         } else {
10625:             $result = 'error: data to store was not a hash reference'; 
10626:         }
10627:     } else {
10628:         $result= 'error: invalid requestkey'; 
10629:     }
10630:     return $result;
10631: }
10632: 
10633: # ---------------------------------------------------------- Assign Custom Role
10634: 
10635: sub assigncustomrole {
10636:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10637:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10638:                        $end,$start,$deleteflag,$selfenroll,$context);
10639: }
10640: 
10641: # ----------------------------------------------------------------- Revoke Role
10642: 
10643: sub revokerole {
10644:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10645:     my $now=time;
10646:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10647: }
10648: 
10649: # ---------------------------------------------------------- Revoke Custom Role
10650: 
10651: sub revokecustomrole {
10652:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10653:     my $now=time;
10654:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10655:            $deleteflag,$selfenroll,$context);
10656: }
10657: 
10658: # ------------------------------------------------------------ Disk usage
10659: sub diskusage {
10660:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10661:     $directorypath =~ s/\/$//;
10662:     my $listing=&reply('du2:'.&escape($directorypath).':'
10663:                        .&escape($getpropath).':'.&escape($uname).':'
10664:                        .&escape($udom),homeserver($uname,$udom));
10665:     if ($listing eq 'unknown_cmd') {
10666:         if ($getpropath) {
10667:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10668:         }
10669:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10670:     }
10671:     return $listing;
10672: }
10673: 
10674: sub is_locked {
10675:     my ($file_name, $domain, $user, $which) = @_;
10676:     my @check;
10677:     my $is_locked;
10678:     push (@check,$file_name);
10679:     my %locked = &get('file_permissions',\@check,
10680: 		      $env{'user.domain'},$env{'user.name'});
10681:     my ($tmp)=keys(%locked);
10682:     if ($tmp=~/^error:/) { undef(%locked); }
10683:     
10684:     if (ref($locked{$file_name}) eq 'ARRAY') {
10685:         $is_locked = 'false';
10686:         foreach my $entry (@{$locked{$file_name}}) {
10687:            if (ref($entry) eq 'ARRAY') {
10688:                $is_locked = 'true';
10689:                if (ref($which) eq 'ARRAY') {
10690:                    push(@{$which},$entry);
10691:                } else {
10692:                    last;
10693:                }
10694:            }
10695:        }
10696:     } else {
10697:         $is_locked = 'false';
10698:     }
10699:     return $is_locked;
10700: }
10701: 
10702: sub declutter_portfile {
10703:     my ($file) = @_;
10704:     $file =~ s{^(/portfolio/|portfolio/)}{/};
10705:     return $file;
10706: }
10707: 
10708: # ------------------------------------------------------------- Mark as Read Only
10709: 
10710: sub mark_as_readonly {
10711:     my ($domain,$user,$files,$what) = @_;
10712:     my %current_permissions = &dump('file_permissions',$domain,$user);
10713:     my ($tmp)=keys(%current_permissions);
10714:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10715:     foreach my $file (@{$files}) {
10716: 	$file = &declutter_portfile($file);
10717:         push(@{$current_permissions{$file}},$what);
10718:     }
10719:     &put('file_permissions',\%current_permissions,$domain,$user);
10720:     return;
10721: }
10722: 
10723: # ------------------------------------------------------------Save Selected Files
10724: 
10725: sub save_selected_files {
10726:     my ($user, $path, @files) = @_;
10727:     my $filename = $user."savedfiles";
10728:     my @other_files = &files_not_in_path($user, $path);
10729:     open (OUT,'>',LONCAPA::tempdir().$filename);
10730:     foreach my $file (@files) {
10731:         print (OUT $env{'form.currentpath'}.$file."\n");
10732:     }
10733:     foreach my $file (@other_files) {
10734:         print (OUT $file."\n");
10735:     }
10736:     close (OUT);
10737:     return 'ok';
10738: }
10739: 
10740: sub clear_selected_files {
10741:     my ($user) = @_;
10742:     my $filename = $user."savedfiles";
10743:     open (OUT,'>',LONCAPA::tempdir().$filename);
10744:     print (OUT undef);
10745:     close (OUT);
10746:     return ("ok");    
10747: }
10748: 
10749: sub files_in_path {
10750:     my ($user, $path) = @_;
10751:     my $filename = $user."savedfiles";
10752:     my %return_files;
10753:     open (IN,'<',LONCAPA::tempdir().$filename);
10754:     while (my $line_in = <IN>) {
10755:         chomp ($line_in);
10756:         my @paths_and_file = split (m!/!, $line_in);
10757:         my $file_part = pop (@paths_and_file);
10758:         my $path_part = join ('/', @paths_and_file);
10759:         $path_part.='/';
10760:         my $path_and_file = $path_part.$file_part;
10761:         if ($path_part eq $path) {
10762:             $return_files{$file_part}= 'selected';
10763:         }
10764:     }
10765:     close (IN);
10766:     return (\%return_files);
10767: }
10768: 
10769: # called in portfolio select mode, to show files selected NOT in current directory
10770: sub files_not_in_path {
10771:     my ($user, $path) = @_;
10772:     my $filename = $user."savedfiles";
10773:     my @return_files;
10774:     my $path_part;
10775:     open(IN, '<',LONCAPA::tempdir().$filename);
10776:     while (my $line = <IN>) {
10777:         #ok, I know it's clunky, but I want it to work
10778:         my @paths_and_file = split(m|/|, $line);
10779:         my $file_part = pop(@paths_and_file);
10780:         chomp($file_part);
10781:         my $path_part = join('/', @paths_and_file);
10782:         $path_part .= '/';
10783:         my $path_and_file = $path_part.$file_part;
10784:         if ($path_part ne $path) {
10785:             push(@return_files, ($path_and_file));
10786:         }
10787:     }
10788:     close(OUT);
10789:     return (@return_files);
10790: }
10791: 
10792: #------------------------------Submitted/Handedback Portfolio Files Versioning
10793:  
10794: sub portfiles_versioning {
10795:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
10796:     my $portfolio_root = '/userfiles/portfolio';
10797:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
10798:     foreach my $file (@{$portfiles}) {
10799:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
10800:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
10801:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
10802:         my $getpropath = 1;
10803:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
10804:                                              $stu_name,$getpropath);
10805:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
10806:         my $new_answer = 
10807:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
10808:         if ($new_answer ne 'problem getting file') {
10809:             push(@{$versioned_portfiles}, $directory.$new_answer);
10810:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
10811:                               [$symb,$env{'request.course.id'},'graded']);
10812:         }
10813:     }
10814: }
10815: 
10816: sub get_next_version {
10817:     my ($answer_name, $answer_ext, $dir_list) = @_;
10818:     my $version;
10819:     if (ref($dir_list) eq 'ARRAY') {
10820:         foreach my $row (@{$dir_list}) {
10821:             my ($file) = split(/\&/,$row,2);
10822:             my ($file_name,$file_version,$file_ext) =
10823:                 &file_name_version_ext($file);
10824:             if (($file_name eq $answer_name) &&
10825:                 ($file_ext eq $answer_ext)) {
10826:                      # gets here if filename and extension match,
10827:                      # regardless of version
10828:                 if ($file_version ne '') {
10829:                     # a versioned file is found  so save it for later
10830:                     if ($file_version > $version) {
10831:                         $version = $file_version;
10832:                     }
10833:                 }
10834:             }
10835:         }
10836:     }
10837:     $version ++;
10838:     return($version);
10839: }
10840: 
10841: sub version_selected_portfile {
10842:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
10843:     my ($answer_name,$answer_ver,$answer_ext) =
10844:         &file_name_version_ext($file_name);
10845:     my $new_answer;
10846:     $env{'form.copy'} =
10847:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
10848:     if($env{'form.copy'} eq '-1') {
10849:         $new_answer = 'problem getting file';
10850:     } else {
10851:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
10852:         my $copy_result = 
10853:             &finishuserfileupload($stu_name,$domain,'copy',
10854:                                   '/portfolio'.$directory.$new_answer);
10855:     }
10856:     undef($env{'form.copy'});
10857:     return ($new_answer);
10858: }
10859: 
10860: sub file_name_version_ext {
10861:     my ($file)=@_;
10862:     my @file_parts = split(/\./, $file);
10863:     my ($name,$version,$ext);
10864:     if (@file_parts > 1) {
10865:         $ext=pop(@file_parts);
10866:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
10867:             $version=pop(@file_parts);
10868:         }
10869:         $name=join('.',@file_parts);
10870:     } else {
10871:         $name=join('.',@file_parts);
10872:     }
10873:     return($name,$version,$ext);
10874: }
10875: 
10876: #----------------------------------------------Get portfolio file permissions
10877: 
10878: sub get_portfile_permissions {
10879:     my ($domain,$user) = @_;
10880:     my %current_permissions = &dump('file_permissions',$domain,$user);
10881:     my ($tmp)=keys(%current_permissions);
10882:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10883:     return \%current_permissions;
10884: }
10885: 
10886: #---------------------------------------------Get portfolio file access controls
10887: 
10888: sub get_access_controls {
10889:     my ($current_permissions,$group,$file) = @_;
10890:     my %access;
10891:     my $real_file = $file;
10892:     $file =~ s/\.meta$//;
10893:     if (defined($file)) {
10894:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
10895:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
10896:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
10897:             }
10898:         }
10899:     } else {
10900:         foreach my $key (keys(%{$current_permissions})) {
10901:             if ($key =~ /\0accesscontrol$/) {
10902:                 if (defined($group)) {
10903:                     if ($key !~ m-^\Q$group\E/-) {
10904:                         next;
10905:                     }
10906:                 }
10907:                 my ($fullpath) = split(/\0/,$key);
10908:                 if (ref($$current_permissions{$key}) eq 'HASH') {
10909:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
10910:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
10911:                     }
10912:                 }
10913:             }
10914:         }
10915:     }
10916:     return %access;
10917: }
10918: 
10919: sub modify_access_controls {
10920:     my ($file_name,$changes,$domain,$user)=@_;
10921:     my ($outcome,$deloutcome);
10922:     my %store_permissions;
10923:     my %new_values;
10924:     my %new_control;
10925:     my %translation;
10926:     my @deletions = ();
10927:     my $now = time;
10928:     if (exists($$changes{'activate'})) {
10929:         if (ref($$changes{'activate'}) eq 'HASH') {
10930:             my @newitems = sort(keys(%{$$changes{'activate'}}));
10931:             my $numnew = scalar(@newitems);
10932:             for (my $i=0; $i<$numnew; $i++) {
10933:                 my $newkey = $newitems[$i];
10934:                 my $newid = &Apache::loncommon::get_cgi_id();
10935:                 if ($newkey =~ /^\d+:/) { 
10936:                     $newkey =~ s/^(\d+)/$newid/;
10937:                     $translation{$1} = $newid;
10938:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
10939:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
10940:                     $translation{$1} = $newid;
10941:                 }
10942:                 $new_values{$file_name."\0".$newkey} = 
10943:                                           $$changes{'activate'}{$newitems[$i]};
10944:                 $new_control{$newkey} = $now;
10945:             }
10946:         }
10947:     }
10948:     my %todelete;
10949:     my %changed_items;
10950:     foreach my $action ('delete','update') {
10951:         if (exists($$changes{$action})) {
10952:             if (ref($$changes{$action}) eq 'HASH') {
10953:                 foreach my $key (keys(%{$$changes{$action}})) {
10954:                     my ($itemnum) = ($key =~ /^([^:]+):/);
10955:                     if ($action eq 'delete') { 
10956:                         $todelete{$itemnum} = 1;
10957:                     } else {
10958:                         $changed_items{$itemnum} = $key;
10959:                     }
10960:                 }
10961:             }
10962:         }
10963:     }
10964:     # get lock on access controls for file.
10965:     my $lockhash = {
10966:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
10967:                                                        ':'.$env{'user.domain'},
10968:                    }; 
10969:     my $tries = 0;
10970:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10971:    
10972:     while (($gotlock ne 'ok') && $tries < 10) {
10973:         $tries ++;
10974:         sleep(0.1);
10975:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
10976:     }
10977:     if ($gotlock eq 'ok') {
10978:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
10979:         my ($tmp)=keys(%curr_permissions);
10980:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
10981:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
10982:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
10983:             if (ref($curr_controls) eq 'HASH') {
10984:                 foreach my $control_item (keys(%{$curr_controls})) {
10985:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
10986:                     if (defined($todelete{$itemnum})) {
10987:                         push(@deletions,$file_name."\0".$control_item);
10988:                     } else {
10989:                         if (defined($changed_items{$itemnum})) {
10990:                             $new_control{$changed_items{$itemnum}} = $now;
10991:                             push(@deletions,$file_name."\0".$control_item);
10992:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
10993:                         } else {
10994:                             $new_control{$control_item} = $$curr_controls{$control_item};
10995:                         }
10996:                     }
10997:                 }
10998:             }
10999:         }
11000:         my ($group);
11001:         if (&is_course($domain,$user)) {
11002:             ($group,my $file) = split(/\//,$file_name,2);
11003:         }
11004:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11005:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11006:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11007:         #  remove lock
11008:         my @del_lock = ($file_name."\0".'locked_access_records');
11009:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11010:         my $sqlresult =
11011:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11012:                                     $group);
11013:     } else {
11014:         $outcome = "error: could not obtain lockfile\n";  
11015:     }
11016:     return ($outcome,$deloutcome,\%new_values,\%translation);
11017: }
11018: 
11019: sub make_public_indefinitely {
11020:     my (@requrl) = @_;
11021:     return &automated_portfile_access('public',\@requrl);
11022: }
11023: 
11024: sub automated_portfile_access {
11025:     my ($accesstype,$addsref,$delsref,$info) = @_;
11026:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11027:         return 'invalid';
11028:     }
11029:     my %urls;
11030:     if (ref($addsref) eq 'ARRAY') {
11031:         foreach my $requrl (@{$addsref}) {
11032:             if (&is_portfolio_url($requrl)) {
11033:                 unless (exists($urls{$requrl})) {
11034:                     $urls{$requrl} = 'add';
11035:                 }
11036:             }
11037:         }
11038:     }
11039:     if (ref($delsref) eq 'ARRAY') {
11040:         foreach my $requrl (@{$delsref}) { 
11041:             if (&is_portfolio_url($requrl)) {
11042:                 unless (exists($urls{$requrl})) {
11043:                     $urls{$requrl} = 'delete'; 
11044:                 }
11045:             }
11046:         }
11047:     }
11048:     unless (keys(%urls)) {
11049:         return 'invalid';
11050:     }
11051:     my $ip;
11052:     if ($accesstype eq 'ip') {
11053:         if (ref($info) eq 'HASH') {
11054:             if ($info->{'ip'} ne '') {
11055:                 $ip = $info->{'ip'};
11056:             }
11057:         }
11058:         if ($ip eq '') {
11059:             return 'invalid';
11060:         }
11061:     }
11062:     my $errors;
11063:     my $now = time;
11064:     my %current_perms;
11065:     foreach my $requrl (sort(keys(%urls))) {
11066:         my $action;
11067:         if ($urls{$requrl} eq 'add') {
11068:             $action = 'activate';
11069:         } else {
11070:             $action = 'none';
11071:         }
11072:         my $aclnum = 0;
11073:         my (undef,$udom,$unum,$file_name,$group) =
11074:             &parse_portfolio_url($requrl);
11075:         unless (exists($current_perms{$unum.':'.$udom})) {
11076:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11077:         }
11078:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11079:                                                    $group,$file_name);
11080:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11081:             my ($num,$scope,$end,$start) = 
11082:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11083:             if ($scope eq $accesstype) {
11084:                 if (($start <= $now) && ($end == 0)) {
11085:                     if ($accesstype eq 'ip') {
11086:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11087:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11088:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11089:                                     if ($urls{$requrl} eq 'add') {
11090:                                         $action = 'none';
11091:                                         last;
11092:                                     } else {
11093:                                         $action = 'delete';
11094:                                         $aclnum = $num;
11095:                                         last;
11096:                                     }
11097:                                 }
11098:                             }
11099:                         }
11100:                     } elsif ($accesstype eq 'public') {
11101:                         if ($urls{$requrl} eq 'add') {
11102:                             $action = 'none';
11103:                             last;
11104:                         } else {
11105:                             $action = 'delete';
11106:                             $aclnum = $num;
11107:                             last;
11108:                         }
11109:                     }
11110:                 } elsif ($accesstype eq 'public') {
11111:                     $action = 'update';
11112:                     $aclnum = $num;
11113:                     last;
11114:                 }
11115:             }
11116:         }
11117:         if ($action eq 'none') {
11118:             next;
11119:         } else {
11120:             my %changes;
11121:             my $newend = 0;
11122:             my $newstart = $now;
11123:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11124:             $changes{$action}{$newkey} = {
11125:                 type => $accesstype,
11126:                 time => {
11127:                     start => $newstart,
11128:                     end   => $newend,
11129:                 },
11130:             };
11131:             if ($accesstype eq 'ip') {
11132:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11133:             }
11134:             my ($outcome,$deloutcome,$new_values,$translation) =
11135:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11136:             unless ($outcome eq 'ok') {
11137:                 $errors .= $outcome.' ';
11138:             }
11139:         }
11140:     }
11141:     if ($errors) {
11142:         $errors =~ s/\s$//;
11143:         return $errors;
11144:     } else {
11145:         return 'ok';
11146:     }
11147: }
11148: 
11149: #------------------------------------------------------Get Marked as Read Only
11150: 
11151: sub get_marked_as_readonly {
11152:     my ($domain,$user,$what,$group) = @_;
11153:     my $current_permissions = &get_portfile_permissions($domain,$user);
11154:     my @readonly_files;
11155:     my $cmp1=$what;
11156:     if (ref($what)) { $cmp1=join('',@{$what}) };
11157:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11158:         if (defined($group)) {
11159:             if ($file_name !~ m-^\Q$group\E/-) {
11160:                 next;
11161:             }
11162:         }
11163:         if (ref($value) eq "ARRAY"){
11164:             foreach my $stored_what (@{$value}) {
11165:                 my $cmp2=$stored_what;
11166:                 if (ref($stored_what) eq 'ARRAY') {
11167:                     $cmp2=join('',@{$stored_what});
11168:                 }
11169:                 if ($cmp1 eq $cmp2) {
11170:                     push(@readonly_files, $file_name);
11171:                     last;
11172:                 } elsif (!defined($what)) {
11173:                     push(@readonly_files, $file_name);
11174:                     last;
11175:                 }
11176:             }
11177:         }
11178:     }
11179:     return @readonly_files;
11180: }
11181: #-----------------------------------------------------------Get Marked as Read Only Hash
11182: 
11183: sub get_marked_as_readonly_hash {
11184:     my ($current_permissions,$group,$what) = @_;
11185:     my %readonly_files;
11186:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11187:         if (defined($group)) {
11188:             if ($file_name !~ m-^\Q$group\E/-) {
11189:                 next;
11190:             }
11191:         }
11192:         if (ref($value) eq "ARRAY"){
11193:             foreach my $stored_what (@{$value}) {
11194:                 if (ref($stored_what) eq 'ARRAY') {
11195:                     foreach my $lock_descriptor(@{$stored_what}) {
11196:                         if ($lock_descriptor eq 'graded') {
11197:                             $readonly_files{$file_name} = 'graded';
11198:                         } elsif ($lock_descriptor eq 'handback') {
11199:                             $readonly_files{$file_name} = 'handback';
11200:                         } else {
11201:                             if (!exists($readonly_files{$file_name})) {
11202:                                 $readonly_files{$file_name} = 'locked';
11203:                             }
11204:                         }
11205:                     }
11206:                 } 
11207:             }
11208:         } 
11209:     }
11210:     return %readonly_files;
11211: }
11212: # ------------------------------------------------------------ Unmark as Read Only
11213: 
11214: sub unmark_as_readonly {
11215:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11216:     # for portfolio submissions, $what contains [$symb,$crsid] 
11217:     my ($domain,$user,$what,$file_name,$group) = @_;
11218:     $file_name = &declutter_portfile($file_name);
11219:     my $symb_crs = $what;
11220:     if (ref($what)) { $symb_crs=join('',@$what); }
11221:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11222:     my ($tmp)=keys(%current_permissions);
11223:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11224:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11225:     foreach my $file (@readonly_files) {
11226: 	my $clean_file = &declutter_portfile($file);
11227: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11228: 	my $current_locks = $current_permissions{$file};
11229:         my @new_locks;
11230:         my @del_keys;
11231:         if (ref($current_locks) eq "ARRAY"){
11232:             foreach my $locker (@{$current_locks}) {
11233:                 my $compare=$locker;
11234:                 if (ref($locker) eq 'ARRAY') {
11235:                     $compare=join('',@{$locker});
11236:                     if ($compare ne $symb_crs) {
11237:                         push(@new_locks, $locker);
11238:                     }
11239:                 }
11240:             }
11241:             if (scalar(@new_locks) > 0) {
11242:                 $current_permissions{$file} = \@new_locks;
11243:             } else {
11244:                 push(@del_keys, $file);
11245:                 &del('file_permissions',\@del_keys, $domain, $user);
11246:                 delete($current_permissions{$file});
11247:             }
11248:         }
11249:     }
11250:     &put('file_permissions',\%current_permissions,$domain,$user);
11251:     return;
11252: }
11253: 
11254: # ------------------------------------------------------------ Directory lister
11255: 
11256: sub dirlist {
11257:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11258:     $uri=~s/^\///;
11259:     $uri=~s/\/$//;
11260:     my ($udom, $uname);
11261:     if ($getuserdir) {
11262:         $udom = $userdomain;
11263:         $uname = $username;
11264:     } else {
11265:         (undef,$udom,$uname)=split(/\//,$uri);
11266:         if(defined($userdomain)) {
11267:             $udom = $userdomain;
11268:         }
11269:         if(defined($username)) {
11270:             $uname = $username;
11271:         }
11272:     }
11273:     my ($dirRoot,$listing,@listing_results);
11274: 
11275:     $dirRoot = $perlvar{'lonDocRoot'};
11276:     if (defined($getpropath)) {
11277:         $dirRoot = &propath($udom,$uname);
11278:         $dirRoot =~ s/\/$//;
11279:     } elsif (defined($getuserdir)) {
11280:         my $subdir=$uname.'__';
11281:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11282:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11283:                    ."/$udom/$subdir/$uname";
11284:     } elsif (defined($alternateRoot)) {
11285:         $dirRoot = $alternateRoot;
11286:     }
11287: 
11288:     if($udom) {
11289:         if($uname) {
11290:             my $uhome = &homeserver($uname,$udom);
11291:             if ($uhome eq 'no_host') {
11292:                 return ([],'no_host');
11293:             }
11294:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11295:                               .$getuserdir.':'.&escape($dirRoot)
11296:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11297:             if ($listing eq 'unknown_cmd') {
11298:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11299:             } else {
11300:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11301:             }
11302:             if ($listing eq 'unknown_cmd') {
11303:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11304:                 @listing_results = split(/:/,$listing);
11305:             } else {
11306:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11307:             }
11308:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11309:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11310:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11311:                 return ([],$listing);
11312:             } else {
11313:                 return (\@listing_results);
11314:             }
11315:         } elsif(!$alternateRoot) {
11316:             my (%allusers,%listerror);
11317: 	    my %servers = &get_servers($udom,'library');
11318:  	    foreach my $tryserver (keys(%servers)) {
11319:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11320:                                   &escape($udom),$tryserver);
11321:                 if ($listing eq 'unknown_cmd') {
11322: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11323: 				      $udom, $tryserver);
11324:                 } else {
11325:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11326:                 }
11327: 		if ($listing eq 'unknown_cmd') {
11328: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11329: 				      $udom, $tryserver);
11330: 		    @listing_results = split(/:/,$listing);
11331: 		} else {
11332: 		    @listing_results =
11333: 			map { &unescape($_); } split(/:/,$listing);
11334: 		}
11335:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11336:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11337:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11338:                     $listerror{$tryserver} = $listing;
11339:                 } else {
11340: 		    foreach my $line (@listing_results) {
11341: 			my ($entry) = split(/&/,$line,2);
11342: 			$allusers{$entry} = 1;
11343: 		    }
11344: 		}
11345:             }
11346:             my @alluserslist=();
11347:             foreach my $user (sort(keys(%allusers))) {
11348:                 push(@alluserslist,$user.'&user');
11349:             }
11350: 
11351:             if (!%listerror) {
11352:                 # no errors
11353:                 return (\@alluserslist);
11354:             } elsif (scalar(keys(%servers)) == 1) {
11355:                 # one library server, one error 
11356:                 my ($key) = keys(%listerror);
11357:                 return (\@alluserslist, $listerror{$key});
11358:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11359:                 # con_lost indicates that we might miss data from at least one
11360:                 # library server
11361:                 return (\@alluserslist, 'con_lost');
11362:             } else {
11363:                 # multiple library servers and no con_lost -> data should be
11364:                 # complete. 
11365:                 return (\@alluserslist);
11366:             }
11367: 
11368:         } else {
11369:             return ([],'missing username');
11370:         }
11371:     } elsif(!defined($getpropath)) {
11372:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11373:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11374:         return (\@all_domains);
11375:     } else {
11376:         return ([],'missing domain');
11377:     }
11378: }
11379: 
11380: # --------------------------------------------- GetFileTimestamp
11381: # This function utilizes dirlist and returns the date stamp for
11382: # when it was last modified.  It will also return an error of -1
11383: # if an error occurs
11384: 
11385: sub GetFileTimestamp {
11386:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11387:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11388:     $studentName   = &LONCAPA::clean_username($studentName);
11389:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11390:                                     undef,$getuserdir);
11391:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11392:         return -1;
11393:     }
11394:     if (ref($fileref) eq 'ARRAY') {
11395:         my @stats = split('&',$fileref->[0]);
11396:         # @stats contains first the filename, then the stat output
11397:         return $stats[10]; # so this is 10 instead of 9.
11398:     } else {
11399:         return -1;
11400:     }
11401: }
11402: 
11403: sub stat_file {
11404:     my ($uri) = @_;
11405:     $uri = &clutter_with_no_wrapper($uri);
11406: 
11407:     my ($udom,$uname,$file);
11408:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11409: 	($udom,$uname,$file) =
11410: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11411: 	$file = 'userfiles/'.$file;
11412:     }
11413:     if ($uri =~ m-^/res/-) {
11414: 	($udom,$uname) = 
11415: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11416: 	$file = $uri;
11417:     }
11418: 
11419:     if (!$udom || !$uname || !$file) {
11420: 	# unable to handle the uri
11421: 	return ();
11422:     }
11423:     my $getpropath;
11424:     if ($file =~ /^userfiles\//) {
11425:         $getpropath = 1;
11426:     }
11427:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11428:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11429:         return ();
11430:     } else {
11431:         if (ref($listref) eq 'ARRAY') {
11432:             my @stats = split('&',$listref->[0]);
11433: 	    shift(@stats); #filename is first
11434: 	    return @stats;
11435:         }
11436:     }
11437:     return ();
11438: }
11439: 
11440: # --------------------------------------------------------- recursedirs
11441: # Recursive function to traverse either a specific user's Authoring Space
11442: # or corresponding Published Resource Space, and populate the hash ref:
11443: # $dirhashref with URLs of all directories, and if $filehashref hash
11444: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11445: # or .rights files in resource space, and .meta, .save, .log, and .bak
11446: # files in Authoring Space.
11447: #
11448: # Inputs:
11449: #
11450: # $is_home - true if current server is home server for user's space
11451: # $context - either: priv, or res respectively for Authoring or Resource Space.
11452: # $docroot - Document root (i.e., /home/httpd/html
11453: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11454: # $relpath - Current path (relative to top level).
11455: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11456: # $filehashref - reference to hash to populate with URLs of files (Optional)
11457: #
11458: # Returns: nothing
11459: #
11460: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11461: #
11462: # Currently used by interface/londocs.pm to create linked select boxes for
11463: # directory and filename to import a Course "Author" resource into a course, and
11464: # also to create linked select boxes for Authoring Space and Directory to choose
11465: # save location for creation of a new "standard" problem from the Course Editor.
11466: #
11467: 
11468: sub recursedirs {
11469:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11470:     return unless (ref($dirhashref) eq 'HASH');
11471:     my $currpath = $docroot.$toppath;
11472:     if ($relpath) {
11473:         $currpath .= "/$relpath";
11474:     }
11475:     my $savefile;
11476:     if (ref($filehashref)) {
11477:         $savefile = 1;
11478:     }
11479:     if ($is_home) {
11480:         if (opendir(my $dirh,$currpath)) {
11481:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11482:                 next if ($item eq '');
11483:                 if (-d "$currpath/$item") {
11484:                     my $newpath;
11485:                     if ($relpath) {
11486:                         $newpath = "$relpath/$item";
11487:                     } else {
11488:                         $newpath = $item;
11489:                     }
11490:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11491:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11492:                 } elsif ($savefile) {
11493:                     if ($context eq 'priv') {
11494:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11495:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11496:                         }
11497:                     } else {
11498:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11499:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11500:                         }
11501:                     }
11502:                 }
11503:             }
11504:             closedir($dirh);
11505:         }
11506:     } else {
11507:         my ($dirlistref,$listerror) =
11508:             &dirlist($toppath.$relpath);
11509:         my @dir_lines;
11510:         my $dirptr=16384;
11511:         if (ref($dirlistref) eq 'ARRAY') {
11512:             foreach my $dir_line (sort
11513:                               {
11514:                                   my ($afile)=split('&',$a,2);
11515:                                   my ($bfile)=split('&',$b,2);
11516:                                   return (lc($afile) cmp lc($bfile));
11517:                               } (@{$dirlistref})) {
11518:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11519:                     split(/\&/,$dir_line,16);
11520:                 $item =~ s/\s+$//;
11521:                 next if (($item =~ /^\.\.?$/) || ($obs));
11522:                 if ($dirptr&$testdir) {
11523:                     my $newpath;
11524:                     if ($relpath) {
11525:                         $newpath = "$relpath/$item";
11526:                     } else {
11527:                         $relpath = '/';
11528:                         $newpath = $item;
11529:                     }
11530:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11531:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11532:                 } elsif ($savefile) {
11533:                     if ($context eq 'priv') {
11534:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11535:                             $filehashref->{$relpath}{$item} = 1;
11536:                         }
11537:                     } else {
11538:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11539:                             $filehashref->{$relpath}{$item} = 1;
11540:                         }
11541:                     }
11542:                 }
11543:             }
11544:         }
11545:     }
11546:     return;
11547: }
11548: 
11549: # -------------------------------------------------------- Value of a Condition
11550: 
11551: # gets the value of a specific preevaluated condition
11552: #    stored in the string  $env{user.state.<cid>}
11553: # or looks up a condition reference in the bighash and if if hasn't
11554: # already been evaluated recurses into docondval to get the value of
11555: # the condition, then memoizing it to 
11556: #   $env{user.state.<cid>.<condition>}
11557: sub directcondval {
11558:     my $number=shift;
11559:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11560: 	&Apache::lonuserstate::evalstate();
11561:     }
11562:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11563: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11564:     } elsif ($number =~ /^_/) {
11565: 	my $sub_condition;
11566: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11567: 		&GDBM_READER(),0640)) {
11568: 	    $sub_condition=$bighash{'conditions'.$number};
11569: 	    untie(%bighash);
11570: 	}
11571: 	my $value = &docondval($sub_condition);
11572: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11573: 	return $value;
11574:     }
11575:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11576:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11577:     } else {
11578:        return 2;
11579:     }
11580: }
11581: 
11582: # get the collection of conditions for this resource
11583: sub condval {
11584:     my $condidx=shift;
11585:     my $allpathcond='';
11586:     foreach my $cond (split(/\|/,$condidx)) {
11587: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11588: 	    $allpathcond.=
11589: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11590: 	}
11591:     }
11592:     $allpathcond=~s/\|$//;
11593:     return &docondval($allpathcond);
11594: }
11595: 
11596: #evaluates an expression of conditions
11597: sub docondval {
11598:     my ($allpathcond) = @_;
11599:     my $result=0;
11600:     if ($env{'request.course.id'}
11601: 	&& defined($allpathcond)) {
11602: 	my $operand='|';
11603: 	my @stack;
11604: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11605: 	    if ($chunk eq '(') {
11606: 		push @stack,($operand,$result);
11607: 	    } elsif ($chunk eq ')') {
11608: 		my $before=pop @stack;
11609: 		if (pop @stack eq '&') {
11610: 		    $result=$result>$before?$before:$result;
11611: 		} else {
11612: 		    $result=$result>$before?$result:$before;
11613: 		}
11614: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
11615: 		$operand=$chunk;
11616: 	    } else {
11617: 		my $new=directcondval($chunk);
11618: 		if ($operand eq '&') {
11619: 		    $result=$result>$new?$new:$result;
11620: 		} else {
11621: 		    $result=$result>$new?$result:$new;
11622: 		}
11623: 	    }
11624: 	}
11625:     }
11626:     return $result;
11627: }
11628: 
11629: # ---------------------------------------------------- Devalidate courseresdata
11630: 
11631: sub devalidatecourseresdata {
11632:     my ($coursenum,$coursedomain)=@_;
11633:     my $hashid=$coursenum.':'.$coursedomain;
11634:     &devalidate_cache_new('courseres',$hashid);
11635: }
11636: 
11637: 
11638: # --------------------------------------------------- Course Resourcedata Query
11639: #
11640: #  Parameters:
11641: #      $coursenum    - Number of the course.
11642: #      $coursedomain - Domain at which the course was created.
11643: #  Returns:
11644: #     A hash of the course parameters along (I think) with timestamps
11645: #     and version info.
11646: 
11647: sub get_courseresdata {
11648:     my ($coursenum,$coursedomain)=@_;
11649:     my $coursehom=&homeserver($coursenum,$coursedomain);
11650:     my $hashid=$coursenum.':'.$coursedomain;
11651:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11652:     my %dumpreply;
11653:     unless (defined($cached)) {
11654: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11655: 	$result=\%dumpreply;
11656: 	my ($tmp) = keys(%dumpreply);
11657: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11658: 	    &do_cache_new('courseres',$hashid,$result,600);
11659: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11660: 	    return $tmp;
11661: 	} elsif ($tmp =~ /^(error)/) {
11662: 	    $result=undef;
11663: 	    &do_cache_new('courseres',$hashid,$result,600);
11664: 	}
11665:     }
11666:     return $result;
11667: }
11668: 
11669: sub devalidateuserresdata {
11670:     my ($uname,$udom)=@_;
11671:     my $hashid="$udom:$uname";
11672:     &devalidate_cache_new('userres',$hashid);
11673: }
11674: 
11675: sub get_userresdata {
11676:     my ($uname,$udom)=@_;
11677:     #most student don\'t have any data set, check if there is some data
11678:     if (&EXT_cache_status($udom,$uname)) { return undef; }
11679: 
11680:     my $hashid="$udom:$uname";
11681:     my ($result,$cached)=&is_cached_new('userres',$hashid);
11682:     if (!defined($cached)) {
11683: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
11684: 	$result=\%resourcedata;
11685: 	&do_cache_new('userres',$hashid,$result,600);
11686:     }
11687:     my ($tmp)=keys(%$result);
11688:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
11689: 	return $result;
11690:     }
11691:     #error 2 occurs when the .db doesn't exist
11692:     if ($tmp!~/error: 2 /) {
11693:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
11694: 	    &logthis("<font color=\"blue\">WARNING:".
11695: 		     " Trying to get resource data for ".
11696: 		     $uname." at ".$udom.": ".
11697: 		     $tmp."</font>");
11698:         }
11699:     } elsif ($tmp=~/error: 2 /) {
11700: 	#&EXT_cache_set($udom,$uname);
11701: 	&do_cache_new('userres',$hashid,undef,600);
11702: 	undef($tmp); # not really an error so don't send it back
11703:     }
11704:     return $tmp;
11705: }
11706: #----------------------------------------------- resdata - return resource data
11707: #  Purpose:
11708: #    Return resource data for either users or for a course.
11709: #  Parameters:
11710: #     $name      - Course/user name.
11711: #     $domain    - Name of the domain the user/course is registered on.
11712: #     $type      - Type of thing $name is (must be 'course' or 'user')
11713: #     $mapp      - decluttered URL of enclosing map  
11714: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
11715: #     $recurseup - Ref to array of map URLs, starting with map containing
11716: #                  $mapp up through hierarchy of nested maps to top level map.  
11717: #     $courseid  - CourseID (first part of param identifier).
11718: #     $modifier  - Middle part of param identifier.
11719: #     $what      - Last part of param identifier.
11720: #     @which     - Array of names of resources desired.
11721: #  Returns:
11722: #     The value of the first reasource in @which that is found in the
11723: #     resource hash.
11724: #  Exceptional Conditions:
11725: #     If the $type passed in is not valid (not the string 'course' or 
11726: #     'user', an undefined  reference is returned.
11727: #     If none of the resources are found, an undef is returned
11728: sub resdata {
11729:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
11730:         $modifier,$what,@which)=@_;
11731:     my $result;
11732:     if ($type eq 'course') {
11733: 	$result=&get_courseresdata($name,$domain);
11734:     } elsif ($type eq 'user') {
11735: 	$result=&get_userresdata($name,$domain);
11736:     }
11737:     if (!ref($result)) { return $result; }    
11738:     foreach my $item (@which) {
11739:         if ($item->[1] eq 'course') {
11740:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
11741:                 unless ($$recursed) {
11742:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
11743:                     $$recursed = 1;
11744:                 }
11745:                 foreach my $item (@${recurseup}) {
11746:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
11747:                     last if (defined($result->{$norecursechk}));
11748:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
11749:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
11750:                 }
11751:             }
11752:         }
11753:         if (defined($result->{$item->[0]})) {
11754: 	    return [$result->{$item->[0]},$item->[1]];
11755: 	}
11756:     }
11757:     return undef;
11758: }
11759: 
11760: sub get_domain_lti {
11761:     my ($cdom,$context) = @_;
11762:     my ($name,%lti);
11763:     if ($context eq 'consumer') {
11764:         $name = 'ltitools';
11765:     } elsif ($context eq 'provider') {
11766:         $name = 'lti';
11767:     } else {
11768:         return %lti;
11769:     }
11770:     my ($result,$cached)=&is_cached_new($name,$cdom);
11771:     if (defined($cached)) {
11772:         if (ref($result) eq 'HASH') {
11773:             %lti = %{$result};
11774:         }
11775:     } else {
11776:         my %domconfig = &get_dom('configuration',[$name],$cdom);
11777:         if (ref($domconfig{$name}) eq 'HASH') {
11778:             %lti = %{$domconfig{$name}};
11779:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
11780:             if (ref($encdomconfig{$name}) eq 'HASH') {
11781:                 foreach my $id (keys(%lti)) {
11782:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
11783:                         foreach my $item ('key','secret') {
11784:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
11785:                         }
11786:                     }
11787:                 }
11788:             }
11789:         }
11790:         my $cachetime = 24*60*60;
11791:         &do_cache_new($name,$cdom,\%lti,$cachetime);
11792:     }
11793:     return %lti;
11794: }
11795: 
11796: sub get_numsuppfiles {
11797:     my ($cnum,$cdom,$ignorecache)=@_;
11798:     my $hashid=$cnum.':'.$cdom;
11799:     my ($suppcount,$cached);
11800:     unless ($ignorecache) {
11801:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
11802:     }
11803:     unless (defined($cached)) {
11804:         my $chome=&homeserver($cnum,$cdom);
11805:         unless ($chome eq 'no_host') {
11806:             ($suppcount,my $supptools,my $errors) = (0,0,0);
11807:             my $suppmap = 'supplemental.sequence';
11808:             ($suppcount,$supptools,$errors) =
11809:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
11810:                                                          $supptools,$errors);
11811:         }
11812:         &do_cache_new('suppcount',$hashid,$suppcount,600);
11813:     }
11814:     return $suppcount;
11815: }
11816: 
11817: #
11818: # EXT resource caching routines
11819: #
11820: 
11821: {
11822: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
11823: #
11824: # The course for which we cache
11825: my $cachedmapkey='';
11826: # The cached recursive maps for this course
11827: my %cachedmaps=();
11828: # When this was last done
11829: my $cachedmaptime='';
11830: 
11831: sub clear_EXT_cache_status {
11832:     &delenv('cache.EXT.');
11833: }
11834: 
11835: sub EXT_cache_status {
11836:     my ($target_domain,$target_user) = @_;
11837:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11838:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
11839:         # We know already the user has no data
11840:         return 1;
11841:     } else {
11842:         return 0;
11843:     }
11844: }
11845: 
11846: sub EXT_cache_set {
11847:     my ($target_domain,$target_user) = @_;
11848:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
11849:     #&appenv({$cachename => time});
11850: }
11851: 
11852: # --------------------------------------------------------- Value of a Variable
11853: sub EXT {
11854: 
11855:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
11856:     unless ($varname) { return ''; }
11857:     #get real user name/domain, courseid and symb
11858:     my $courseid;
11859:     my $publicuser;
11860:     if ($symbparm) {
11861: 	$symbparm=&get_symb_from_alias($symbparm);
11862:     }
11863:     if (!($uname && $udom)) {
11864:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
11865:       if (!$symbparm) {	$symbparm=$cursymb; }
11866:     } else {
11867: 	$courseid=$env{'request.course.id'};
11868:     }
11869:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
11870:     my $rest;
11871:     if (defined($therest[0])) {
11872:        $rest=join('.',@therest);
11873:     } else {
11874:        $rest='';
11875:     }
11876: 
11877:     my $qualifierrest=$qualifier;
11878:     if ($rest) { $qualifierrest.='.'.$rest; }
11879:     my $spacequalifierrest=$space;
11880:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
11881:     if ($realm eq 'user') {
11882: # --------------------------------------------------------------- user.resource
11883: 	if ($space eq 'resource') {
11884: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
11885: 		  || defined($Apache::lonhomework::parsing_a_task))
11886: 		 &&
11887: 		 ($symbparm eq &symbread()) ) {	
11888: 		# if we are in the middle of processing the resource the
11889: 		# get the value we are planning on committing
11890:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
11891:                     return $Apache::lonhomework::results{$qualifierrest};
11892:                 } else {
11893:                     return $Apache::lonhomework::history{$qualifierrest};
11894:                 }
11895: 	    } else {
11896: 		my %restored;
11897: 		if ($publicuser || $env{'request.state'} eq 'construct') {
11898: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
11899: 		} else {
11900: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
11901: 		}
11902: 		return $restored{$qualifierrest};
11903: 	    }
11904: # ----------------------------------------------------------------- user.access
11905:         } elsif ($space eq 'access') {
11906: 	    # FIXME - not supporting calls for a specific user
11907:             return &allowed($qualifier,$rest);
11908: # ------------------------------------------ user.preferences, user.environment
11909:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
11910: 	    if (($uname eq $env{'user.name'}) &&
11911: 		($udom eq $env{'user.domain'})) {
11912: 		return $env{join('.',('environment',$qualifierrest))};
11913: 	    } else {
11914: 		my %returnhash;
11915: 		if (!$publicuser) {
11916: 		    %returnhash=&userenvironment($udom,$uname,
11917: 						 $qualifierrest);
11918: 		}
11919: 		return $returnhash{$qualifierrest};
11920: 	    }
11921: # ----------------------------------------------------------------- user.course
11922:         } elsif ($space eq 'course') {
11923: 	    # FIXME - not supporting calls for a specific user
11924:             return $env{join('.',('request.course',$qualifier))};
11925: # ------------------------------------------------------------------- user.role
11926:         } elsif ($space eq 'role') {
11927: 	    # FIXME - not supporting calls for a specific user
11928:             my ($role,$where)=split(/\./,$env{'request.role'});
11929:             if ($qualifier eq 'value') {
11930: 		return $role;
11931:             } elsif ($qualifier eq 'extent') {
11932:                 return $where;
11933:             }
11934: # ----------------------------------------------------------------- user.domain
11935:         } elsif ($space eq 'domain') {
11936:             return $udom;
11937: # ------------------------------------------------------------------- user.name
11938:         } elsif ($space eq 'name') {
11939:             return $uname;
11940: # ---------------------------------------------------- Any other user namespace
11941:         } else {
11942: 	    my %reply;
11943: 	    if (!$publicuser) {
11944: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
11945: 	    }
11946: 	    return $reply{$qualifierrest};
11947:         }
11948:     } elsif ($realm eq 'query') {
11949: # ---------------------------------------------- pull stuff out of query string
11950:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
11951: 						[$spacequalifierrest]);
11952: 	return $env{'form.'.$spacequalifierrest}; 
11953:    } elsif ($realm eq 'request') {
11954: # ------------------------------------------------------------- request.browser
11955:         if ($space eq 'browser') {
11956:             return $env{'browser.'.$qualifier};
11957: # ------------------------------------------------------------ request.filename
11958:         } else {
11959:             return $env{'request.'.$spacequalifierrest};
11960:         }
11961:     } elsif ($realm eq 'course') {
11962: # ---------------------------------------------------------- course.description
11963:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
11964:     } elsif ($realm eq 'resource') {
11965: 
11966: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
11967: 	    if (!$symbparm) { $symbparm=&symbread(); }
11968: 	}
11969: 
11970:         if ($qualifier eq '') {
11971: 	    if ($space eq 'title') {
11972: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
11973: 	        return &gettitle($symbparm);
11974: 	    }
11975: 	
11976: 	    if ($space eq 'map') {
11977: 	        my ($map) = &decode_symb($symbparm);
11978: 	        return &symbread($map);
11979: 	    }
11980:             if ($space eq 'maptitle') {
11981:                 my ($map) = &decode_symb($symbparm);
11982:                 return &gettitle($map);
11983:             }
11984: 	    if ($space eq 'filename') {
11985: 	        if ($symbparm) {
11986: 		    return &clutter((&decode_symb($symbparm))[2]);
11987: 	        }
11988: 	        return &hreflocation('',$env{'request.filename'});
11989: 	    }
11990: 
11991:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
11992:                 if ($space eq 'visibleparts') {
11993:                     my $navmap = Apache::lonnavmaps::navmap->new();
11994:                     my $item;
11995:                     if (ref($navmap)) {
11996:                         my $res = $navmap->getBySymb($symbparm);
11997:                         my $parts = $res->parts();
11998:                         if (ref($parts) eq 'ARRAY') {
11999:                             $item = join(',',@{$parts});
12000:                         }
12001:                         undef($navmap);
12002:                     }
12003:                     return $item;
12004:                 }
12005:             }
12006:         }
12007: 
12008: 	my ($section, $group, @groups, @recurseup, $recursed);
12009: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12010:         if (($courseid eq '') && ($cid)) {
12011:             $courseid = $cid;
12012:         }
12013: 	if (($symbparm && $courseid) && 
12014: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12015: 
12016: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12017: 
12018: # ----------------------------------------------------- Cascading lookup scheme
12019: 	    my $symbp=$symbparm;
12020: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12021: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12022:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12023: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12024: 	    if (($env{'user.name'} eq $uname) &&
12025: 		($env{'user.domain'} eq $udom)) {
12026: 		$section=$env{'request.course.sec'};
12027:                 @groups = split(/:/,$env{'request.course.groups'});  
12028:                 @groups=&sort_course_groups($courseid,@groups); 
12029: 	    } else {
12030: 		if (! defined($usection)) {
12031: 		    $section=&getsection($udom,$uname,$courseid);
12032: 		} else {
12033: 		    $section = $usection;
12034: 		}
12035:                 @groups = &get_users_groups($udom,$uname,$courseid);
12036: 	    }
12037: 
12038: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12039: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12040:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12041: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12042: 
12043: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12044: 	    my $courselevelr=$courseid.'.'.$symbparm;
12045:             $courseleveli=$courseid.'.'.$recurseparm;
12046: 	    $courselevelm=$courseid.'.'.$mapparm;
12047: 
12048: # ----------------------------------------------------------- first, check user
12049: 
12050: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12051:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12052: 				       ([$courselevelr,'resource'],
12053: 					[$courselevelm,'map'     ],
12054:                                         [$courseleveli,'map'     ],
12055: 					[$courselevel, 'course'  ]));
12056: 	    if (defined($userreply)) { return &get_reply($userreply); }
12057: 
12058: # ------------------------------------------------ second, check some of course
12059:             my $coursereply;
12060:             if (@groups > 0) {
12061:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12062:                                        $recurseparm,$mapparm,$spacequalifierrest,
12063:                                        $mapp,\$recursed,\@recurseup);
12064:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12065:             }
12066: 
12067: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12068: 				  $env{'course.'.$courseid.'.domain'},
12069: 				  'course',$mapp,\$recursed,\@recurseup,
12070:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12071: 				  ([$seclevelr,   'resource'],
12072: 				   [$seclevelm,   'map'     ],
12073:                                    [$secleveli,   'map'     ],
12074: 				   [$seclevel,    'course'  ],
12075: 				   [$courselevelr,'resource']));
12076: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12077: 
12078: # ------------------------------------------------------ third, check map parms
12079: 	    my %parmhash=();
12080: 	    my $thisparm='';
12081: 	    if (tie(%parmhash,'GDBM_File',
12082: 		    $env{'request.course.fn'}.'_parms.db',
12083: 		    &GDBM_READER(),0640)) {
12084: 		$thisparm=$parmhash{$symbparm};
12085: 		untie(%parmhash);
12086: 	    }
12087: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12088: 	}
12089: # ------------------------------------------ fourth, look in resource metadata
12090:  
12091:         my $what = $spacequalifierrest;
12092: 	$what=~s/\./\_/;
12093: 	my $filename;
12094: 	if (!$symbparm) { $symbparm=&symbread(); }
12095: 	if ($symbparm) {
12096: 	    $filename=(&decode_symb($symbparm))[2];
12097: 	} else {
12098: 	    $filename=$env{'request.filename'};
12099: 	}
12100:         my $toolsymb;
12101:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12102:             $toolsymb = $symbparm;
12103:         }
12104: 	my $metadata=&metadata($filename,$what,$toolsymb);
12105: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12106: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12107: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12108: 
12109: # ----------------------------------------------- fifth, look in rest of course
12110: 	if ($symbparm && defined($courseid) && 
12111: 	    $courseid eq $env{'request.course.id'}) {
12112: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12113: 				     $env{'course.'.$courseid.'.domain'},
12114: 				     'course',$mapp,\$recursed,\@recurseup,
12115:                                      $courseid,'.',$spacequalifierrest,
12116: 				     ([$courselevelm,'map'   ],
12117:                                       [$courseleveli,'map'   ],
12118: 				      [$courselevel, 'course']));
12119: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12120: 	}
12121: # ------------------------------------------------------------------ Cascade up
12122: 	unless ($space eq '0') {
12123: 	    my @parts=split(/_/,$space);
12124: 	    my $id=pop(@parts);
12125: 	    my $part=join('_',@parts);
12126: 	    if ($part eq '') { $part='0'; }
12127: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12128: 				 $symbparm,$udom,$uname,$section,1);
12129: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12130: 	}
12131: 	if ($recurse) { return undef; }
12132: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12133: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12134: # ---------------------------------------------------- Any other user namespace
12135:     } elsif ($realm eq 'environment') {
12136: # ----------------------------------------------------------------- environment
12137: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12138: 	    return $env{'environment.'.$spacequalifierrest};
12139: 	} else {
12140: 	    if ($uname eq 'anonymous' && $udom eq '') {
12141: 		return '';
12142: 	    }
12143: 	    my %returnhash=&userenvironment($udom,$uname,
12144: 					    $spacequalifierrest);
12145: 	    return $returnhash{$spacequalifierrest};
12146: 	}
12147:     } elsif ($realm eq 'system') {
12148: # ----------------------------------------------------------------- system.time
12149: 	if ($space eq 'time') {
12150: 	    return time;
12151:         }
12152:     } elsif ($realm eq 'server') {
12153: # ----------------------------------------------------------------- system.time
12154: 	if ($space eq 'name') {
12155: 	    return $ENV{'SERVER_NAME'};
12156:         }
12157:     }
12158:     return '';
12159: }
12160: 
12161: sub get_reply {
12162:     my ($reply_value) = @_;
12163:     if (ref($reply_value) eq 'ARRAY') {
12164:         if (wantarray) {
12165: 	    return @$reply_value;
12166:         }
12167:         return $reply_value->[0];
12168:     } else {
12169:         return $reply_value;
12170:     }
12171: }
12172: 
12173: sub check_group_parms {
12174:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12175:         $recursed,$recurseupref) = @_;
12176:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12177:                   [$what,'course']);
12178:     my $coursereply;
12179:     foreach my $group (@{$groups}) {
12180:         my @groupitems = ();
12181:         foreach my $level (@levels) {
12182:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12183:              push(@groupitems,[$item,$level->[1]]);
12184:         }
12185:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12186:                                    $env{'course.'.$courseid.'.domain'},
12187:                                    'course',$mapp,$recursed,$recurseupref,
12188:                                    $courseid,'.['.$group.'].',$what,
12189:                                    @groupitems);
12190:         last if (defined($coursereply));
12191:     }
12192:     return $coursereply;
12193: }
12194: 
12195: sub get_map_hierarchy {
12196:     my ($mapname,$courseid) = @_;
12197:     my @recurseup = ();
12198:     if ($mapname) {
12199:         if (($cachedmapkey eq $courseid) &&
12200:             (abs($cachedmaptime-time)<5)) {
12201:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12202:                 return @{$cachedmaps{$mapname}};
12203:             }
12204:         }
12205:         my $navmap = Apache::lonnavmaps::navmap->new();
12206:         if (ref($navmap)) {
12207:             @recurseup = $navmap->recurseup_maps($mapname);
12208:             undef($navmap);
12209:             $cachedmaps{$mapname} = \@recurseup;
12210:             $cachedmaptime=time;
12211:             $cachedmapkey=$courseid;
12212:         }
12213:     }
12214:     return @recurseup;
12215: }
12216: 
12217: }
12218: 
12219: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12220:     my ($courseid,@groups) = @_;
12221:     @groups = sort(@groups);
12222:     return @groups;
12223: }
12224: 
12225: sub packages_tab_default {
12226:     my ($uri,$varname,$toolsymb)=@_;
12227:     my (undef,$part,$name)=split(/\./,$varname);
12228: 
12229:     my (@extension,@specifics,$do_default);
12230:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12231: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12232: 	if ($pack_type eq 'default') {
12233: 	    $do_default=1;
12234: 	} elsif ($pack_type eq 'extension') {
12235: 	    push(@extension,[$package,$pack_type,$pack_part]);
12236: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12237: 	    # only look at packages defaults for packages that this id is
12238: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12239: 	}
12240:     }
12241:     # first look for a package that matches the requested part id
12242:     foreach my $package (@specifics) {
12243: 	my (undef,$pack_type,$pack_part)=@{$package};
12244: 	next if ($pack_part ne $part);
12245: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12246: 	    return $packagetab{"$pack_type&$name&default"};
12247: 	}
12248:     }
12249:     # look for any possible matching non extension_ package
12250:     foreach my $package (@specifics) {
12251: 	my (undef,$pack_type,$pack_part)=@{$package};
12252: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12253: 	    return $packagetab{"$pack_type&$name&default"};
12254: 	}
12255: 	if ($pack_type eq 'part') { $pack_part='0'; }
12256: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12257: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12258: 	}
12259:     }
12260:     # look for any posible extension_ match
12261:     foreach my $package (@extension) {
12262: 	my ($package,$pack_type)=@{$package};
12263: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12264: 	    return $packagetab{"$pack_type&$name&default"};
12265: 	}
12266: 	if (defined($packagetab{$package."&$name&default"})) {
12267: 	    return $packagetab{$package."&$name&default"};
12268: 	}
12269:     }
12270:     # look for a global default setting
12271:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12272: 	return $packagetab{"default&$name&default"};
12273:     }
12274:     return undef;
12275: }
12276: 
12277: sub add_prefix_and_part {
12278:     my ($prefix,$part)=@_;
12279:     my $keyroot;
12280:     if (defined($prefix) && $prefix !~ /^__/) {
12281: 	# prefix that has a part already
12282: 	$keyroot=$prefix;
12283:     } elsif (defined($prefix)) {
12284: 	# prefix that is missing a part
12285: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12286:     } else {
12287: 	# no prefix at all
12288: 	if (defined($part)) { $keyroot='_'.$part; }
12289:     }
12290:     return $keyroot;
12291: }
12292: 
12293: # ---------------------------------------------------------------- Get metadata
12294: 
12295: my %metaentry;
12296: my %importedpartids;
12297: my %importedrespids;
12298: sub metadata {
12299:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12300:     $uri=&declutter($uri);
12301:     # if it is a non metadata possible uri return quickly
12302:     if (($uri eq '') || 
12303: 	(($uri =~ m|^/*adm/|) && 
12304: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12305:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12306: 	return undef;
12307:     }
12308:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12309: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12310: 	return undef;
12311:     }
12312:     my $filename=$uri;
12313:     $uri=~s/\.meta$//;
12314: #
12315: # Is the metadata already cached?
12316: # Look at timestamp of caching
12317: # Everything is cached by the main uri, libraries are never directly cached
12318: #
12319:     if (!defined($liburi)) {
12320: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12321: 	if (defined($cached)) { return $result->{':'.$what}; }
12322:     }
12323: 
12324: #
12325: # If the uri is for an external tool the file from
12326: # which metadata should be retrieved depends on whether
12327: # the tool had been configured to be gradable (set in the Course
12328: # Editor or Resource Editor).
12329: #
12330: # If a valid symb has been included as the third arg in the call
12331: # to &metadata() that can be used to retrieve the value of
12332: # parameter_0_gradable set for the resource, and included in the
12333: # uploaded map containing the tool. The value is retrieved via
12334: # &EXT(), if a valid symb is available.  Otherwise the value of
12335: # gradable in the exttool_$marker.db file for the tool instance
12336: # is retrieved via &get().
12337: #
12338: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12339: # hiddenresource and encrypturl (during course initialization)
12340: # the map-level parameter for resource.0.gradable included in the 
12341: # uploaded map containing the tool will not yet have been stored
12342: # in the user_course_parms.db file for the user's session, so in 
12343: # this case fall back to retrieving gradable status from the
12344: # exttool_$marker.db file.
12345: #
12346: # In order to avoid an infinite loop, &metadata() will return
12347: # before a call to &EXT(), if the uri is for an external tool
12348: # and the $what for which metadata is being requested is
12349: # parameter_0_gradable or 0_gradable.
12350: #
12351: 
12352:     if ($uri =~ /ext\.tool$/) {
12353:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12354:             return;
12355:         } else {
12356:             my ($checked,$use_passback);
12357:             if ($toolsymb ne '') {
12358:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12359:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12360:                     $checked = 1;
12361:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12362:                         $use_passback = 1;
12363:                     }
12364:                 }
12365:             }
12366:             unless ($checked) {
12367:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12368:                 $marker=~s/\D//g;
12369:                 if ($marker) {
12370:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12371:                     $use_passback = $toolsettings{'gradable'};
12372:                 }
12373:             }
12374:             if ($use_passback) {
12375:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12376:             } else {
12377:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12378:             }
12379:         }
12380:     }
12381: 
12382:     {
12383: # Imported parts would go here
12384:         my @origfiletagids=();
12385:         my $importedparts=0;
12386: 
12387: # Imported responseids would go here
12388:         my $importedresponses=0;
12389: #
12390: # Is this a recursive call for a library?
12391: #
12392: #	if (! exists($metacache{$uri})) {
12393: #	    $metacache{$uri}={};
12394: #	}
12395: 	my $cachetime = 60*60;
12396:         if ($liburi) {
12397: 	    $liburi=&declutter($liburi);
12398:             $filename=$liburi;
12399:         } else {
12400: 	    &devalidate_cache_new('meta',$uri);
12401: 	    undef(%metaentry);
12402: 	}
12403:         my %metathesekeys=();
12404:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12405: 	my $metastring;
12406: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12407: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12408: 	    $metastring = 
12409: 		&Apache::lonnet::ssi_body($which,
12410: 					  ('grade_target' => 'meta'));
12411: 	    $cachetime = 1; # only want this cached in the child not long term
12412: 	} elsif (($uri !~ m -^(editupload)/-) && 
12413:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12414: 	    my $file=&filelocation('',&clutter($filename));
12415: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12416: 	    $metastring=&getfile($file);
12417: 	}
12418:         my $parser=HTML::LCParser->new(\$metastring);
12419:         my $token;
12420:         undef %metathesekeys;
12421:         while ($token=$parser->get_token) {
12422: 	    if ($token->[0] eq 'S') {
12423: 		if (defined($token->[2]->{'package'})) {
12424: #
12425: # This is a package - get package info
12426: #
12427: 		    my $package=$token->[2]->{'package'};
12428: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12429: 		    if (defined($token->[2]->{'id'})) { 
12430: 			$keyroot.='_'.$token->[2]->{'id'}; 
12431: 		    }
12432: 		    if ($metaentry{':packages'}) {
12433: 			$metaentry{':packages'}.=','.$package.$keyroot;
12434: 		    } else {
12435: 			$metaentry{':packages'}=$package.$keyroot;
12436: 		    }
12437: 		    foreach my $pack_entry (keys(%packagetab)) {
12438: 			my $part=$keyroot;
12439: 			$part=~s/^\_//;
12440: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12441: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12442: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12443: 			    # ignore package.tab specified default values
12444:                             # here &package_tab_default() will fetch those
12445: 			    if ($subp eq 'default') { next; }
12446: 			    my $value=$packagetab{$pack_entry};
12447: 			    my $unikey;
12448: 			    if ($pack =~ /_0$/) {
12449: 				$unikey='parameter_0_'.$name;
12450: 				$part=0;
12451: 			    } else {
12452: 				$unikey='parameter'.$keyroot.'_'.$name;
12453: 			    }
12454: 			    if ($subp eq 'display') {
12455: 				$value.=' [Part: '.$part.']';
12456: 			    }
12457: 			    $metaentry{':'.$unikey.'.part'}=$part;
12458: 			    $metathesekeys{$unikey}=1;
12459: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12460: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12461: 			    }
12462: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12463: 				$metaentry{':'.$unikey}=
12464: 				    $metaentry{':'.$unikey.'.default'};
12465: 			    }
12466: 			}
12467: 		    }
12468: 		} else {
12469: #
12470: # This is not a package - some other kind of start tag
12471: #
12472: 		    my $entry=$token->[1];
12473: 		    my $unikey='';
12474: 
12475: 		    if ($entry eq 'import') {
12476: #
12477: # Importing a library here
12478: #
12479:                         my $location=$parser->get_text('/import');
12480:                         my $dir=$filename;
12481:                         $dir=~s|[^/]*$||;
12482:                         $location=&filelocation($dir,$location);
12483: 
12484:                         my $importid=$token->[2]->{'id'};
12485:                         my $importmode=$token->[2]->{'importmode'};
12486: #
12487: # Check metadata for imported file to
12488: # see if it contained response items
12489: #
12490:                         my ($origfile,@libfilekeys);
12491:                         my %currmetaentry = %metaentry;
12492:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12493:                                                            $depthcount+1));
12494:                         if (grep(/^responseorder$/,@libfilekeys)) {
12495:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12496:                                                              undef,$depthcount+1);
12497:                             if ($libresponseorder ne '') {
12498:                                 if ($#origfiletagids<0) {
12499:                                     undef(%importedrespids);
12500:                                     undef(%importedpartids);
12501:                                 }
12502:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12503:                                 if (@respids) {
12504:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12505:                                 }
12506:                                 if ($importedrespids{$importid} ne '') {
12507:                                     $importedresponses = 1;
12508: # We need to get the original file and the imported file to get the response order correct
12509: # Load and inspect original file
12510:                                     if ($#origfiletagids<0) {
12511:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12512:                                         $origfile=&getfile($origfilelocation);
12513:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12514:                                     }
12515:                                 }
12516:                             }
12517:                         }
12518: # Do not overwrite contents of %metaentry hash for resource itself with 
12519: # hash populated for imported library file
12520:                         %metaentry = %currmetaentry;
12521:                         undef(%currmetaentry);
12522:                         if ($importmode eq 'part') {
12523: # Import as part(s)
12524:                            $importedparts=1;
12525: # We need to get the original file and the imported file to get the part order correct
12526: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12527: # Load and inspect original file if we didn't do that already
12528:                            if ($#origfiletagids<0) {
12529:                                undef(%importedrespids);
12530:                                undef(%importedpartids);
12531:                                if ($origfile eq '') {
12532:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12533:                                    $origfile=&getfile($origfilelocation);
12534:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12535:                                }
12536:                            }
12537:                            my @impfilepartids;
12538: # If <partorder> tag is included in metadata for the imported file
12539: # get the parts in the imported file from that.
12540:                            if (grep(/^partorder$/,@libfilekeys)) {
12541:                                %currmetaentry = %metaentry;
12542:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12543:                                                             $depthcount+1);
12544:                                %metaentry = %currmetaentry;
12545:                                undef(%currmetaentry);
12546:                                if ($libpartorder ne '') {
12547:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12548:                                }
12549:                            } else {
12550: # If no <partorder> tag available, load and inspect imported file
12551:                                my $impfile=&getfile($location);
12552:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12553:                            }
12554:                            if ($#impfilepartids>=0) {
12555: # This problem had parts
12556:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12557:                            } else {
12558: # Importing by turning a single problem into a problem part
12559: # It gets the import-tags ID as part-ID
12560:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12561:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12562:                            }
12563:                         } else {
12564: # Import as problem or as normal import
12565:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12566:                             unless ($importmode eq 'problem') {
12567: # Normal import
12568:                                 if (defined($token->[2]->{'id'})) {
12569:                                     $unikey.='_'.$token->[2]->{'id'};
12570:                                 }
12571:                             }
12572: # Check metadata for imported file to
12573: # see if it contained parts
12574:                             if (grep(/^partorder$/,@libfilekeys)) {
12575:                                 %currmetaentry = %metaentry;
12576:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12577:                                                              $depthcount+1);
12578:                                 %metaentry = %currmetaentry;
12579:                                 undef(%currmetaentry);
12580:                                 if ($libpartorder ne '') {
12581:                                     $importedparts = 1;
12582:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
12583:                                 }
12584:                             }
12585:                         }
12586: 			if ($depthcount<20) {
12587: 			    my $metadata = 
12588: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
12589: 					  $depthcount+1);
12590: 			    foreach my $meta (split(',',$metadata)) {
12591: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
12592: 				$metathesekeys{$meta}=1;
12593: 			    }
12594:                         }
12595: 		    } else {
12596: #
12597: # Not importing, some other kind of non-package, non-library start tag
12598: # 
12599:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
12600:                         if (defined($token->[2]->{'id'})) {
12601:                             $unikey.='_'.$token->[2]->{'id'};
12602:                         }
12603: 			if (defined($token->[2]->{'name'})) { 
12604: 			    $unikey.='_'.$token->[2]->{'name'}; 
12605: 			}
12606: 			$metathesekeys{$unikey}=1;
12607: 			foreach my $param (@{$token->[3]}) {
12608: 			    $metaentry{':'.$unikey.'.'.$param} =
12609: 				$token->[2]->{$param};
12610: 			}
12611: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
12612: 			my $default=$metaentry{':'.$unikey.'.default'};
12613: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
12614: 		 # only ws inside the tag, and not in default, so use default
12615: 		 # as value
12616: 			    $metaentry{':'.$unikey}=$default;
12617: 			} elsif ( $internaltext =~ /\S/ ) {
12618: 		  # something interesting inside the tag
12619: 			    $metaentry{':'.$unikey}=$internaltext;
12620: 			} else {
12621: 		  # no interesting values, don't set a default
12622: 			}
12623: # end of not-a-package not-a-library import
12624: 		    }
12625: # end of not-a-package start tag
12626: 		}
12627: # the next is the end of "start tag"
12628: 	    }
12629: 	}
12630: 	my ($extension) = ($uri =~ /\.(\w+)$/);
12631: 	$extension = lc($extension);
12632: 	if ($extension eq 'htm') { $extension='html'; }
12633: 
12634: 	foreach my $key (keys(%packagetab)) {
12635: 	    #no specific packages #how's our extension
12636: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
12637: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
12638: 					 \%metathesekeys);
12639: 	}
12640: 
12641: 	if (!exists($metaentry{':packages'})
12642: 	    || $packagetab{"import_defaults&extension_$extension"}) {
12643: 	    foreach my $key (keys(%packagetab)) {
12644: 		#no specific packages well let's get default then
12645: 		if ($key!~/^default&/) { next; }
12646: 		&metadata_create_package_def($uri,$key,'default',
12647: 					     \%metathesekeys);
12648: 	    }
12649: 	}
12650: # are there custom rights to evaluate
12651: 	if ($metaentry{':copyright'} eq 'custom') {
12652: 
12653:     #
12654:     # Importing a rights file here
12655:     #
12656: 	    unless ($depthcount) {
12657: 		my $location=$metaentry{':customdistributionfile'};
12658: 		my $dir=$filename;
12659: 		$dir=~s|[^/]*$||;
12660: 		$location=&filelocation($dir,$location);
12661: 		my $rights_metadata =
12662: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
12663: 			      $depthcount+1);
12664: 		foreach my $rights (split(',',$rights_metadata)) {
12665: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12666: 		    $metathesekeys{$rights}=1;
12667: 		}
12668: 	    }
12669: 	}
12670: 	# uniqifiy package listing
12671: 	my %seen;
12672: 	my @uniq_packages =
12673: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
12674: 	$metaentry{':packages'} = join(',',@uniq_packages);
12675: 
12676:         if (($importedresponses) || ($importedparts)) {
12677:             if ($importedparts) {
12678: # We had imported parts and need to rebuild partorder
12679:                 $metaentry{':partorder'}='';
12680:                 $metathesekeys{'partorder'}=1;
12681:             }
12682:             if ($importedresponses) {
12683: # We had imported responses and need to rebuil responseorder
12684:                 $metaentry{':responseorder'}='';
12685:                 $metathesekeys{'responseorder'}=1;
12686:             }
12687:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
12688:                 my $origid = $origfiletagids[$index+1];
12689:                 if ($origfiletagids[$index] eq 'part') {
12690: # Original part, part of the problem
12691:                     if ($importedparts) {
12692:                         $metaentry{':partorder'}.=','.$origid;
12693:                     }
12694:                 } elsif ($origfiletagids[$index] eq 'import') {
12695:                     if ($importedparts) {
12696: # We have imported parts at this position
12697:                         if ($importedpartids{$origid} ne '') {
12698:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
12699:                         }
12700:                     }
12701:                     if ($importedresponses) {
12702: # We have imported responses at this position
12703:                         if ($importedrespids{$origid} ne '') {
12704:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
12705:                         }
12706:                     }
12707:                 } else {
12708: # Original response item, part of the problem
12709:                     if ($importedresponses) {
12710:                         $metaentry{':responseorder'}.=','.$origid;
12711:                     }
12712:                 }
12713:             }
12714:             if ($importedparts) {
12715:                 $metaentry{':partorder'}=~s/^\,//;
12716:             }
12717:             if ($importedresponses) {
12718:                 $metaentry{':responseorder'}=~s/^\,//;
12719:             }
12720:         }
12721: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
12722: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
12723: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
12724:         unless ($liburi) {
12725: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
12726:         }
12727: # this is the end of "was not already recently cached
12728:     }
12729:     return $metaentry{':'.$what};
12730: }
12731: 
12732: sub metadata_create_package_def {
12733:     my ($uri,$key,$package,$metathesekeys)=@_;
12734:     my ($pack,$name,$subp)=split(/\&/,$key);
12735:     if ($subp eq 'default') { next; }
12736:     
12737:     if (defined($metaentry{':packages'})) {
12738: 	$metaentry{':packages'}.=','.$package;
12739:     } else {
12740: 	$metaentry{':packages'}=$package;
12741:     }
12742:     my $value=$packagetab{$key};
12743:     my $unikey;
12744:     $unikey='parameter_0_'.$name;
12745:     $metaentry{':'.$unikey.'.part'}=0;
12746:     $$metathesekeys{$unikey}=1;
12747:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12748: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
12749:     }
12750:     if (defined($metaentry{':'.$unikey.'.default'})) {
12751: 	$metaentry{':'.$unikey}=
12752: 	    $metaentry{':'.$unikey.'.default'};
12753:     }
12754: }
12755: 
12756: sub metadata_generate_part0 {
12757:     my ($metadata,$metacache,$uri) = @_;
12758:     my %allnames;
12759:     foreach my $metakey (keys(%$metadata)) {
12760: 	if ($metakey=~/^parameter\_(.*)/) {
12761: 	  my $part=$$metacache{':'.$metakey.'.part'};
12762: 	  my $name=$$metacache{':'.$metakey.'.name'};
12763: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
12764: 	    $allnames{$name}=$part;
12765: 	  }
12766: 	}
12767:     }
12768:     foreach my $name (keys(%allnames)) {
12769:       $$metadata{"parameter_0_$name"}=1;
12770:       my $key=":parameter_0_$name";
12771:       $$metacache{"$key.part"}='0';
12772:       $$metacache{"$key.name"}=$name;
12773:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
12774: 					   $allnames{$name}.'_'.$name.
12775: 					   '.type'};
12776:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
12777: 			     '.display'};
12778:       my $expr='[Part: '.$allnames{$name}.']';
12779:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
12780:       $$metacache{"$key.display"}=$olddis;
12781:     }
12782: }
12783: 
12784: # ------------------------------------------------------ Devalidate title cache
12785: 
12786: sub devalidate_title_cache {
12787:     my ($url)=@_;
12788:     if (!$env{'request.course.id'}) { return; }
12789:     my $symb=&symbread($url);
12790:     if (!$symb) { return; }
12791:     my $key=$env{'request.course.id'}."\0".$symb;
12792:     &devalidate_cache_new('title',$key);
12793: }
12794: 
12795: # ------------------------------------------------- Get the title of a course
12796: 
12797: sub current_course_title {
12798:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
12799: }
12800: # ------------------------------------------------- Get the title of a resource
12801: 
12802: sub gettitle {
12803:     my $urlsymb=shift;
12804:     my $symb=&symbread($urlsymb);
12805:     if ($symb) {
12806: 	my $key=$env{'request.course.id'}."\0".$symb;
12807: 	my ($result,$cached)=&is_cached_new('title',$key);
12808: 	if (defined($cached)) { 
12809: 	    return $result;
12810: 	}
12811: 	my ($map,$resid,$url)=&decode_symb($symb);
12812: 	my $title='';
12813: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
12814: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
12815: 	} else {
12816: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12817: 		    &GDBM_READER(),0640)) {
12818: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
12819: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
12820: 		untie(%bighash);
12821: 	    }
12822: 	}
12823: 	$title=~s/\&colon\;/\:/gs;
12824: 	if ($title) {
12825: # Remember both $symb and $title for dynamic metadata
12826:             $accesshash{$symb.'___crstitle'}=$title;
12827:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
12828: # Cache this title and then return it
12829: 	    return &do_cache_new('title',$key,$title,600);
12830: 	}
12831: 	$urlsymb=$url;
12832:     }
12833:     my $title=&metadata($urlsymb,'title');
12834:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
12835:     return $title;
12836: }
12837: 
12838: sub get_slot {
12839:     my ($which,$cnum,$cdom)=@_;
12840:     if (!$cnum || !$cdom) {
12841: 	(undef,my $courseid)=&whichuser();
12842: 	$cdom=$env{'course.'.$courseid.'.domain'};
12843: 	$cnum=$env{'course.'.$courseid.'.num'};
12844:     }
12845:     my $key=join("\0",'slots',$cdom,$cnum,$which);
12846:     my %slotinfo;
12847:     if (exists($remembered{$key})) {
12848: 	$slotinfo{$which} = $remembered{$key};
12849:     } else {
12850: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
12851: 	&Apache::lonhomework::showhash(%slotinfo);
12852: 	my ($tmp)=keys(%slotinfo);
12853: 	if ($tmp=~/^error:/) { return (); }
12854: 	$remembered{$key} = $slotinfo{$which};
12855:     }
12856:     if (ref($slotinfo{$which}) eq 'HASH') {
12857: 	return %{$slotinfo{$which}};
12858:     }
12859:     return $slotinfo{$which};
12860: }
12861: 
12862: sub get_reservable_slots {
12863:     my ($cnum,$cdom,$uname,$udom) = @_;
12864:     my $now = time;
12865:     my $reservable_info;
12866:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
12867:     if (exists($remembered{$key})) {
12868:         $reservable_info = $remembered{$key};
12869:     } else {
12870:         my %resv;
12871:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
12872:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
12873:         $reservable_info = \%resv;
12874:         $remembered{$key} = $reservable_info;
12875:     }
12876:     return $reservable_info;
12877: }
12878: 
12879: sub get_course_slots {
12880:     my ($cnum,$cdom) = @_;
12881:     my $hashid=$cnum.':'.$cdom;
12882:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
12883:     if (defined($cached)) {
12884:         if (ref($result) eq 'HASH') {
12885:             return %{$result};
12886:         }
12887:     } else {
12888:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
12889:         my ($tmp) = keys(%slots);
12890:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
12891:             &do_cache_new('allslots',$hashid,\%slots,600);
12892:             return %slots;
12893:         }
12894:     }
12895:     return;
12896: }
12897: 
12898: sub devalidate_slots_cache {
12899:     my ($cnum,$cdom)=@_;
12900:     my $hashid=$cnum.':'.$cdom;
12901:     &devalidate_cache_new('allslots',$hashid);
12902: }
12903: 
12904: sub get_coursechange {
12905:     my ($cdom,$cnum) = @_;
12906:     if ($cdom eq '' || $cnum eq '') {
12907:         return unless ($env{'request.course.id'});
12908:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
12909:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
12910:     }
12911:     my $hashid=$cdom.'_'.$cnum;
12912:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
12913:     if ((defined($cached)) && ($change ne '')) {
12914:         return $change;
12915:     } else {
12916:         my %crshash;
12917:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
12918:         if ($crshash{'internal.contentchange'} eq '') {
12919:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
12920:             if ($change eq '') {
12921:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
12922:                 $change = $crshash{'internal.created'};
12923:             }
12924:         } else {
12925:             $change = $crshash{'internal.contentchange'};
12926:         }
12927:         my $cachetime = 600;
12928:         &do_cache_new('crschange',$hashid,$change,$cachetime);
12929:     }
12930:     return $change;
12931: }
12932: 
12933: sub devalidate_coursechange_cache {
12934:     my ($cnum,$cdom)=@_;
12935:     my $hashid=$cnum.':'.$cdom;
12936:     &devalidate_cache_new('crschange',$hashid);
12937: }
12938: 
12939: # ------------------------------------------------- Update symbolic store links
12940: 
12941: sub symblist {
12942:     my ($mapname,%newhash)=@_;
12943:     $mapname=&deversion(&declutter($mapname));
12944:     my %hash;
12945:     if (($env{'request.course.fn'}) && (%newhash)) {
12946:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
12947:                       &GDBM_WRCREAT(),0640)) {
12948: 	    foreach my $url (keys(%newhash)) {
12949: 		next if ($url eq 'last_known'
12950: 			 && $env{'form.no_update_last_known'});
12951: 		$hash{declutter($url)}=&encode_symb($mapname,
12952: 						    $newhash{$url}->[1],
12953: 						    $newhash{$url}->[0]);
12954:             }
12955:             if (untie(%hash)) {
12956: 		return 'ok';
12957:             }
12958:         }
12959:     }
12960:     return 'error';
12961: }
12962: 
12963: # --------------------------------------------------------------- Verify a symb
12964: 
12965: sub symbverify {
12966:     my ($symb,$thisurl,$encstate)=@_;
12967:     my $thisfn=$thisurl;
12968:     $thisfn=&declutter($thisfn);
12969: # direct jump to resource in page or to a sequence - will construct own symbs
12970:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
12971: # check URL part
12972:     my ($map,$resid,$url)=&decode_symb($symb);
12973: 
12974:     unless ($url eq $thisfn) { return 0; }
12975: 
12976:     $symb=&symbclean($symb);
12977:     $thisurl=&deversion($thisurl);
12978:     $thisfn=&deversion($thisfn);
12979: 
12980:     my %bighash;
12981:     my $okay=0;
12982: 
12983:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
12984:                             &GDBM_READER(),0640)) {
12985:         my $noclutter;
12986:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
12987:             $thisurl =~ s/\?.+$//;
12988:             if ($map =~ m{^uploaded/.+\.page$}) {
12989:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
12990:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
12991:                 $noclutter = 1;
12992:             }
12993:         }
12994:         my $ids;
12995:         if ($noclutter) {
12996:             $ids=$bighash{'ids_'.$thisurl};
12997:         } else {
12998:             $ids=$bighash{'ids_'.&clutter($thisurl)};
12999:         }
13000:         unless ($ids) {
13001:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13002:             $ids=$bighash{$idkey};
13003:         }
13004:         if ($ids) {
13005: # ------------------------------------------------------------------- Has ID(s)
13006:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13007:                 $symb =~ s/\?.+$//;
13008:             }
13009: 	    foreach my $id (split(/\,/,$ids)) {
13010: 	       my ($mapid,$resid)=split(/\./,$id);
13011:                if (
13012:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13013:    eq $symb) {
13014:                    if (ref($encstate)) {
13015:                        $$encstate = $bighash{'encrypted_'.$id};
13016:                    }
13017: 		   if (($env{'request.role.adv'}) ||
13018: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13019:                        ($thisurl eq '/adm/navmaps')) {
13020: 		       $okay=1;
13021:                        last;
13022: 		   }
13023: 	       }
13024: 	   }
13025:         }
13026: 	untie(%bighash);
13027:     }
13028:     return $okay;
13029: }
13030: 
13031: # --------------------------------------------------------------- Clean-up symb
13032: 
13033: sub symbclean {
13034:     my $symb=shift;
13035:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13036: # remove version from map
13037:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13038: 
13039: # remove version from URL
13040:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13041: 
13042: # remove wrapper
13043: 
13044:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13045:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13046:     return $symb;
13047: }
13048: 
13049: # ---------------------------------------------- Split symb to find map and url
13050: 
13051: sub encode_symb {
13052:     my ($map,$resid,$url)=@_;
13053:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13054: }
13055: 
13056: sub decode_symb {
13057:     my $symb=shift;
13058:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13059:     my ($map,$resid,$url)=split(/___/,$symb);
13060:     return (&fixversion($map),$resid,&fixversion($url));
13061: }
13062: 
13063: sub fixversion {
13064:     my $fn=shift;
13065:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13066:     my %bighash;
13067:     my $uri=&clutter($fn);
13068:     my $key=$env{'request.course.id'}.'_'.$uri;
13069: # is this cached?
13070:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13071:     if (defined($cached)) { return $result; }
13072: # unfortunately not cached, or expired
13073:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13074: 	    &GDBM_READER(),0640)) {
13075:  	if ($bighash{'version_'.$uri}) {
13076:  	    my $version=$bighash{'version_'.$uri};
13077:  	    unless (($version eq 'mostrecent') || 
13078: 		    ($version==&getversion($uri))) {
13079:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13080:  	    }
13081:  	}
13082:  	untie %bighash;
13083:     }
13084:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13085: }
13086: 
13087: sub deversion {
13088:     my $url=shift;
13089:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13090:     return $url;
13091: }
13092: 
13093: # ------------------------------------------------------ Return symb list entry
13094: 
13095: sub symbread {
13096:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
13097:     my $cache_str='request.symbread.cached.'.$thisfn;
13098:     if (defined($env{$cache_str})) {
13099:         if ($ignorecachednull) {
13100:             return $env{$cache_str} unless ($env{$cache_str} eq '');
13101:         } else {
13102:             return $env{$cache_str};
13103:         }
13104:     }
13105: # no filename provided? try from environment
13106:     unless ($thisfn) {
13107:         if ($env{'request.symb'}) {
13108: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
13109: 	}
13110: 	$thisfn=$env{'request.filename'};
13111:     }
13112:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13113: # is that filename actually a symb? Verify, clean, and return
13114:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13115: 	if (&symbverify($thisfn,$1)) {
13116: 	    return $env{$cache_str}=&symbclean($thisfn);
13117: 	}
13118:     }
13119:     $thisfn=declutter($thisfn);
13120:     my %hash;
13121:     my %bighash;
13122:     my $syval='';
13123:     if (($env{'request.course.fn'}) && ($thisfn)) {
13124:         my $targetfn = $thisfn;
13125:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13126:             $targetfn = 'adm/wrapper/'.$thisfn;
13127:         }
13128: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13129: 	    $targetfn=$1;
13130: 	}
13131:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13132:                       &GDBM_READER(),0640)) {
13133: 	    $syval=$hash{$targetfn};
13134:             untie(%hash);
13135:         }
13136: # ---------------------------------------------------------- There was an entry
13137:         if ($syval) {
13138: 	    #unless ($syval=~/\_\d+$/) {
13139: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13140: 		    #&appenv({'request.ambiguous' => $thisfn});
13141: 		    #return $env{$cache_str}='';
13142: 		#}    
13143: 		#$syval.=$1;
13144: 	    #}
13145:         } else {
13146: # ------------------------------------------------------- Was not in symb table
13147:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13148:                             &GDBM_READER(),0640)) {
13149: # ---------------------------------------------- Get ID(s) for current resource
13150:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13151:               unless ($ids) { 
13152:                  $ids=$bighash{'ids_/'.$thisfn};
13153:               }
13154:               unless ($ids) {
13155: # alias?
13156: 		  $ids=$bighash{'mapalias_'.$thisfn};
13157:               }
13158:               if ($ids) {
13159: # ------------------------------------------------------------------- Has ID(s)
13160:                  my @possibilities=split(/\,/,$ids);
13161:                  if ($#possibilities==0) {
13162: # ----------------------------------------------- There is only one possibility
13163: 		     my ($mapid,$resid)=split(/\./,$ids);
13164: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13165: 						    $resid,$thisfn);
13166:                      if (ref($possibles) eq 'HASH') {
13167:                          $possibles->{$syval} = 1;    
13168:                      }
13169:                      if ($checkforblock) {
13170:                          my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
13171:                          if (@blockers) {
13172:                              $syval = '';
13173:                              return;
13174:                          }
13175:                      }
13176:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13177: # ------------------------------------------ There is more than one possibility
13178:                      my $realpossible=0;
13179:                      foreach my $id (@possibilities) {
13180: 			 my $file=$bighash{'src_'.$id};
13181:                          my $canaccess;
13182:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13183:                              $canaccess = 1;
13184:                          } else { 
13185:                              $canaccess = &allowed('bre',$file);
13186:                          }
13187:                          if ($canaccess) {
13188:          		     my ($mapid,$resid)=split(/\./,$id);
13189:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13190:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13191: 						             $resid,$thisfn);
13192:                                  if (ref($possibles) eq 'HASH') {
13193:                                      $possibles->{$syval} = 1;
13194:                                  }
13195:                                  if ($checkforblock) {
13196:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
13197:                                      unless (@blockers > 0) {
13198:                                          $syval = $poss_syval;
13199:                                          $realpossible++;
13200:                                      }
13201:                                  } else {
13202:                                      $syval = $poss_syval;
13203:                                      $realpossible++;
13204:                                  }
13205:                              }
13206: 			 }
13207:                      }
13208: 		     if ($realpossible!=1) { $syval=''; }
13209:                  } else {
13210:                      $syval='';
13211:                  }
13212: 	      }
13213:               untie(%bighash);
13214:            }
13215:         }
13216:         if ($syval) {
13217: 	    return $env{$cache_str}=$syval;
13218:         }
13219:     }
13220:     &appenv({'request.ambiguous' => $thisfn});
13221:     return $env{$cache_str}='';
13222: }
13223: 
13224: # ---------------------------------------------------------- Return random seed
13225: 
13226: sub numval {
13227:     my $txt=shift;
13228:     $txt=~tr/A-J/0-9/;
13229:     $txt=~tr/a-j/0-9/;
13230:     $txt=~tr/K-T/0-9/;
13231:     $txt=~tr/k-t/0-9/;
13232:     $txt=~tr/U-Z/0-5/;
13233:     $txt=~tr/u-z/0-5/;
13234:     $txt=~s/\D//g;
13235:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13236:     return int($txt);
13237: }
13238: 
13239: sub numval2 {
13240:     my $txt=shift;
13241:     $txt=~tr/A-J/0-9/;
13242:     $txt=~tr/a-j/0-9/;
13243:     $txt=~tr/K-T/0-9/;
13244:     $txt=~tr/k-t/0-9/;
13245:     $txt=~tr/U-Z/0-5/;
13246:     $txt=~tr/u-z/0-5/;
13247:     $txt=~s/\D//g;
13248:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13249:     my $total;
13250:     foreach my $val (@txts) { $total+=$val; }
13251:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13252:     return int($total);
13253: }
13254: 
13255: sub numval3 {
13256:     use integer;
13257:     my $txt=shift;
13258:     $txt=~tr/A-J/0-9/;
13259:     $txt=~tr/a-j/0-9/;
13260:     $txt=~tr/K-T/0-9/;
13261:     $txt=~tr/k-t/0-9/;
13262:     $txt=~tr/U-Z/0-5/;
13263:     $txt=~tr/u-z/0-5/;
13264:     $txt=~s/\D//g;
13265:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13266:     my $total;
13267:     foreach my $val (@txts) { $total+=$val; }
13268:     if ($_64bit) { $total=(($total<<32)>>32); }
13269:     return $total;
13270: }
13271: 
13272: sub digest {
13273:     my ($data)=@_;
13274:     my $digest=&Digest::MD5::md5($data);
13275:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13276:     my ($e,$f);
13277:     {
13278:         use integer;
13279:         $e=($a+$b);
13280:         $f=($c+$d);
13281:         if ($_64bit) {
13282:             $e=(($e<<32)>>32);
13283:             $f=(($f<<32)>>32);
13284:         }
13285:     }
13286:     if (wantarray) {
13287: 	return ($e,$f);
13288:     } else {
13289: 	my $g;
13290: 	{
13291: 	    use integer;
13292: 	    $g=($e+$f);
13293: 	    if ($_64bit) {
13294: 		$g=(($g<<32)>>32);
13295: 	    }
13296: 	}
13297: 	return $g;
13298:     }
13299: }
13300: 
13301: sub latest_rnd_algorithm_id {
13302:     return '64bit5';
13303: }
13304: 
13305: sub get_rand_alg {
13306:     my ($courseid)=@_;
13307:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13308:     if ($courseid) {
13309: 	return $env{"course.$courseid.rndseed"};
13310:     }
13311:     return &latest_rnd_algorithm_id();
13312: }
13313: 
13314: sub validCODE {
13315:     my ($CODE)=@_;
13316:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13317:     return 0;
13318: }
13319: 
13320: sub getCODE {
13321:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13322:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13323: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13324: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13325: 	return $Apache::lonhomework::history{'resource.CODE'};
13326:     }
13327:     return undef;
13328: }
13329: #
13330: #  Determines the random seed for a specific context:
13331: #
13332: # parameters:
13333: #   symb      - in course context the symb for the seed.
13334: #   course_id - The course id of the form domain_coursenum.
13335: #   domain    - Domain for the user.
13336: #   course    - Course for the user.
13337: #   cenv      - environment of the course.
13338: #
13339: # NOTE:
13340: #   All parameters are picked out of the environment if missing
13341: #   or not defined.
13342: #   If a symb cannot be determined the current time is used instead.
13343: #
13344: #  For a given well defined symb, courside, domain, username,
13345: #  and course environment, the seed is reproducible.
13346: #
13347: sub rndseed {
13348:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13349:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13350:     if (!defined($symb)) {
13351: 	unless ($symb=$wsymb) { return time; }
13352:     }
13353:     if (!defined $courseid) { 
13354: 	$courseid=$wcourseid; 
13355:     }
13356:     if (!defined $domain) { $domain=$wdomain; }
13357:     if (!defined $username) { $username=$wusername }
13358: 
13359:     my $which;
13360:     if (defined($cenv->{'rndseed'})) {
13361: 	$which = $cenv->{'rndseed'};
13362:     } else {
13363: 	$which =&get_rand_alg($courseid);
13364:     }
13365:     if (defined(&getCODE())) {
13366: 
13367: 	if ($which eq '64bit5') {
13368: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13369: 	} elsif ($which eq '64bit4') {
13370: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13371: 	} else {
13372: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13373: 	}
13374:     } elsif ($which eq '64bit5') {
13375: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13376:     } elsif ($which eq '64bit4') {
13377: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13378:     } elsif ($which eq '64bit3') {
13379: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13380:     } elsif ($which eq '64bit2') {
13381: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13382:     } elsif ($which eq '64bit') {
13383: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13384:     }
13385:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13386: }
13387: 
13388: sub rndseed_32bit {
13389:     my ($symb,$courseid,$domain,$username)=@_;
13390:     {
13391: 	use integer;
13392: 	my $symbchck=unpack("%32C*",$symb) << 27;
13393: 	my $symbseed=numval($symb) << 22;
13394: 	my $namechck=unpack("%32C*",$username) << 17;
13395: 	my $nameseed=numval($username) << 12;
13396: 	my $domainseed=unpack("%32C*",$domain) << 7;
13397: 	my $courseseed=unpack("%32C*",$courseid);
13398: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13399: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13400: 	#&logthis("rndseed :$num:$symb");
13401: 	if ($_64bit) { $num=(($num<<32)>>32); }
13402: 	return $num;
13403:     }
13404: }
13405: 
13406: sub rndseed_64bit {
13407:     my ($symb,$courseid,$domain,$username)=@_;
13408:     {
13409: 	use integer;
13410: 	my $symbchck=unpack("%32S*",$symb) << 21;
13411: 	my $symbseed=numval($symb) << 10;
13412: 	my $namechck=unpack("%32S*",$username);
13413: 	
13414: 	my $nameseed=numval($username) << 21;
13415: 	my $domainseed=unpack("%32S*",$domain) << 10;
13416: 	my $courseseed=unpack("%32S*",$courseid);
13417: 	
13418: 	my $num1=$symbchck+$symbseed+$namechck;
13419: 	my $num2=$nameseed+$domainseed+$courseseed;
13420: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13421: 	#&logthis("rndseed :$num:$symb");
13422: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13423: 	return "$num1,$num2";
13424:     }
13425: }
13426: 
13427: sub rndseed_64bit2 {
13428:     my ($symb,$courseid,$domain,$username)=@_;
13429:     {
13430: 	use integer;
13431: 	# strings need to be an even # of cahracters long, it it is odd the
13432:         # last characters gets thrown away
13433: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13434: 	my $symbseed=numval($symb) << 10;
13435: 	my $namechck=unpack("%32S*",$username.' ');
13436: 	
13437: 	my $nameseed=numval($username) << 21;
13438: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13439: 	my $courseseed=unpack("%32S*",$courseid.' ');
13440: 	
13441: 	my $num1=$symbchck+$symbseed+$namechck;
13442: 	my $num2=$nameseed+$domainseed+$courseseed;
13443: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13444: 	#&logthis("rndseed :$num:$symb");
13445: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13446: 	return "$num1,$num2";
13447:     }
13448: }
13449: 
13450: sub rndseed_64bit3 {
13451:     my ($symb,$courseid,$domain,$username)=@_;
13452:     {
13453: 	use integer;
13454: 	# strings need to be an even # of cahracters long, it it is odd the
13455:         # last characters gets thrown away
13456: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13457: 	my $symbseed=numval2($symb) << 10;
13458: 	my $namechck=unpack("%32S*",$username.' ');
13459: 	
13460: 	my $nameseed=numval2($username) << 21;
13461: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13462: 	my $courseseed=unpack("%32S*",$courseid.' ');
13463: 	
13464: 	my $num1=$symbchck+$symbseed+$namechck;
13465: 	my $num2=$nameseed+$domainseed+$courseseed;
13466: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13467: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13468: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13469: 	
13470: 	return "$num1:$num2";
13471:     }
13472: }
13473: 
13474: sub rndseed_64bit4 {
13475:     my ($symb,$courseid,$domain,$username)=@_;
13476:     {
13477: 	use integer;
13478: 	# strings need to be an even # of cahracters long, it it is odd the
13479:         # last characters gets thrown away
13480: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13481: 	my $symbseed=numval3($symb) << 10;
13482: 	my $namechck=unpack("%32S*",$username.' ');
13483: 	
13484: 	my $nameseed=numval3($username) << 21;
13485: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13486: 	my $courseseed=unpack("%32S*",$courseid.' ');
13487: 	
13488: 	my $num1=$symbchck+$symbseed+$namechck;
13489: 	my $num2=$nameseed+$domainseed+$courseseed;
13490: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13491: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13492: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13493: 	
13494: 	return "$num1:$num2";
13495:     }
13496: }
13497: 
13498: sub rndseed_64bit5 {
13499:     my ($symb,$courseid,$domain,$username)=@_;
13500:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13501:     return "$num1:$num2";
13502: }
13503: 
13504: sub rndseed_CODE_64bit {
13505:     my ($symb,$courseid,$domain,$username)=@_;
13506:     {
13507: 	use integer;
13508: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13509: 	my $symbseed=numval2($symb);
13510: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13511: 	my $CODEseed=numval(&getCODE());
13512: 	my $courseseed=unpack("%32S*",$courseid.' ');
13513: 	my $num1=$symbseed+$CODEchck;
13514: 	my $num2=$CODEseed+$courseseed+$symbchck;
13515: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13516: 	#&logthis("rndseed :$num1:$num2:$symb");
13517: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13518: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13519: 	return "$num1:$num2";
13520:     }
13521: }
13522: 
13523: sub rndseed_CODE_64bit4 {
13524:     my ($symb,$courseid,$domain,$username)=@_;
13525:     {
13526: 	use integer;
13527: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13528: 	my $symbseed=numval3($symb);
13529: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13530: 	my $CODEseed=numval3(&getCODE());
13531: 	my $courseseed=unpack("%32S*",$courseid.' ');
13532: 	my $num1=$symbseed+$CODEchck;
13533: 	my $num2=$CODEseed+$courseseed+$symbchck;
13534: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13535: 	#&logthis("rndseed :$num1:$num2:$symb");
13536: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13537: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13538: 	return "$num1:$num2";
13539:     }
13540: }
13541: 
13542: sub rndseed_CODE_64bit5 {
13543:     my ($symb,$courseid,$domain,$username)=@_;
13544:     my $code = &getCODE();
13545:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13546:     return "$num1:$num2";
13547: }
13548: 
13549: sub setup_random_from_rndseed {
13550:     my ($rndseed)=@_;
13551:     if ($rndseed =~/([,:])/) {
13552:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13553:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13554:             &Math::Random::random_set_seed_from_phrase($rndseed);
13555:         } else {
13556:             &Math::Random::random_set_seed($num1,$num2);
13557:         }
13558:     } else {
13559: 	&Math::Random::random_set_seed_from_phrase($rndseed);
13560:     }
13561: }
13562: 
13563: sub latest_receipt_algorithm_id {
13564:     return 'receipt3';
13565: }
13566: 
13567: sub recunique {
13568:     my $fucourseid=shift;
13569:     my $unique;
13570:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
13571: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13572: 	$unique=$env{"course.$fucourseid.internal.encseed"};
13573:     } else {
13574: 	$unique=$perlvar{'lonReceipt'};
13575:     }
13576:     return unpack("%32C*",$unique);
13577: }
13578: 
13579: sub recprefix {
13580:     my $fucourseid=shift;
13581:     my $prefix;
13582:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
13583: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13584: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
13585:     } else {
13586: 	$prefix=$perlvar{'lonHostID'};
13587:     }
13588:     return unpack("%32C*",$prefix);
13589: }
13590: 
13591: sub ireceipt {
13592:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
13593: 
13594:     my $return =&recprefix($fucourseid).'-';
13595: 
13596:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
13597: 	$env{'request.state'} eq 'construct') {
13598: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
13599: 	return $return;
13600:     }
13601: 
13602:     my $cuname=unpack("%32C*",$funame);
13603:     my $cudom=unpack("%32C*",$fudom);
13604:     my $cucourseid=unpack("%32C*",$fucourseid);
13605:     my $cusymb=unpack("%32C*",$fusymb);
13606:     my $cunique=&recunique($fucourseid);
13607:     my $cpart=unpack("%32S*",$part);
13608:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
13609: 
13610: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
13611: 			       
13612: 	$return.= ($cunique%$cuname+
13613: 		   $cunique%$cudom+
13614: 		   $cusymb%$cuname+
13615: 		   $cusymb%$cudom+
13616: 		   $cucourseid%$cuname+
13617: 		   $cucourseid%$cudom+
13618: 		   $cpart%$cuname+
13619: 		   $cpart%$cudom);
13620:     } else {
13621: 	$return.= ($cunique%$cuname+
13622: 		   $cunique%$cudom+
13623: 		   $cusymb%$cuname+
13624: 		   $cusymb%$cudom+
13625: 		   $cucourseid%$cuname+
13626: 		   $cucourseid%$cudom);
13627:     }
13628:     return $return;
13629: }
13630: 
13631: sub receipt {
13632:     my ($part)=@_;
13633:     my ($symb,$courseid,$domain,$name) = &whichuser();
13634:     return &ireceipt($name,$domain,$courseid,$symb,$part);
13635: }
13636: 
13637: sub whichuser {
13638:     my ($passedsymb)=@_;
13639:     my ($symb,$courseid,$domain,$name,$publicuser);
13640:     if (defined($env{'form.grade_symb'})) {
13641: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
13642: 	my $allowed=&allowed('vgr',$tmp_courseid);
13643: 	if (!$allowed &&
13644: 	    exists($env{'request.course.sec'}) &&
13645: 	    $env{'request.course.sec'} !~ /^\s*$/) {
13646: 	    $allowed=&allowed('vgr',$tmp_courseid.
13647: 			      '/'.$env{'request.course.sec'});
13648: 	}
13649: 	if ($allowed) {
13650: 	    ($symb)=&get_env_multiple('form.grade_symb');
13651: 	    $courseid=$tmp_courseid;
13652: 	    ($domain)=&get_env_multiple('form.grade_domain');
13653: 	    ($name)=&get_env_multiple('form.grade_username');
13654: 	    return ($symb,$courseid,$domain,$name,$publicuser);
13655: 	}
13656:     }
13657:     if (!$passedsymb) {
13658: 	$symb=&symbread();
13659:     } else {
13660: 	$symb=$passedsymb;
13661:     }
13662:     $courseid=$env{'request.course.id'};
13663:     $domain=$env{'user.domain'};
13664:     $name=$env{'user.name'};
13665:     if ($name eq 'public' && $domain eq 'public') {
13666: 	if (!defined($env{'form.username'})) {
13667: 	    $env{'form.username'}.=time.rand(10000000);
13668: 	}
13669: 	$name.=$env{'form.username'};
13670:     }
13671:     return ($symb,$courseid,$domain,$name,$publicuser);
13672: 
13673: }
13674: 
13675: # ------------------------------------------------------------ Serves up a file
13676: # returns either the contents of the file or 
13677: # -1 if the file doesn't exist
13678: #
13679: # if the target is a file that was uploaded via DOCS, 
13680: # a check will be made to see if a current copy exists on the local server,
13681: # if it does this will be served, otherwise a copy will be retrieved from
13682: # the home server for the course and stored in /home/httpd/html/userfiles on
13683: # the local server.   
13684: 
13685: sub getfile {
13686:     my ($file) = @_;
13687:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
13688:     &repcopy($file);
13689:     return &readfile($file);
13690: }
13691: 
13692: sub repcopy_userfile {
13693:     my ($file)=@_;
13694:     my $londocroot = $perlvar{'lonDocRoot'};
13695:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
13696:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
13697:     my ($cdom,$cnum,$filename) = 
13698: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
13699:     my $uri="/uploaded/$cdom/$cnum/$filename";
13700:     if (-e "$file") {
13701: # we already have a local copy, check it out
13702: 	my @fileinfo = stat($file);
13703: 	my $rtncode;
13704: 	my $info;
13705: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
13706: 	if ($lwpresp ne 'ok') {
13707: # there is no such file anymore, even though we had a local copy
13708: 	    if ($rtncode eq '404') {
13709: 		unlink($file);
13710: 	    }
13711: 	    return -1;
13712: 	}
13713: 	if ($info < $fileinfo[9]) {
13714: # nice, the file we have is up-to-date, just say okay
13715: 	    return 'ok';
13716: 	} else {
13717: # the file is outdated, get rid of it
13718: 	    unlink($file);
13719: 	}
13720:     }
13721: # one way or the other, at this point, we don't have the file
13722: # construct the correct path for the file
13723:     my @parts = ($cdom,$cnum); 
13724:     if ($filename =~ m|^(.+)/[^/]+$|) {
13725: 	push @parts, split(/\//,$1);
13726:     }
13727:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
13728:     foreach my $part (@parts) {
13729: 	$path .= '/'.$part;
13730: 	if (!-e $path) {
13731: 	    mkdir($path,0770);
13732: 	}
13733:     }
13734: # now the path exists for sure
13735: # get a user agent
13736:     my $transferfile=$file.'.in.transfer';
13737: # FIXME: this should flock
13738:     if (-e $transferfile) { return 'ok'; }
13739:     my $request;
13740:     $uri=~s/^\///;
13741:     my $homeserver = &homeserver($cnum,$cdom);
13742:     my $hostname = &hostname($homeserver);
13743:     my $protocol = $protocol{$homeserver};
13744:     $protocol = 'http' if ($protocol ne 'https');
13745:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
13746:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
13747: # did it work?
13748:     if ($response->is_error()) {
13749: 	unlink($transferfile);
13750: 	&logthis("Userfile repcopy failed for $uri");
13751: 	return -1;
13752:     }
13753: # worked, rename the transfer file
13754:     rename($transferfile,$file);
13755:     return 'ok';
13756: }
13757: 
13758: sub tokenwrapper {
13759:     my $uri=shift;
13760:     $uri=~s|^https?\://([^/]+)||;
13761:     $uri=~s|^/||;
13762:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
13763:     my $token=$1;
13764:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
13765:     if ($udom && $uname && $file) {
13766: 	$file=~s|(\?\.*)*$||;
13767:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
13768:         my $homeserver = &homeserver($uname,$udom);
13769:         my $hostname = &hostname($homeserver);
13770:         my $protocol = $protocol{$homeserver};
13771:         $protocol = 'http' if ($protocol ne 'https');
13772:         return $protocol.'://'.$hostname.'/'.$uri.
13773:                (($uri=~/\?/)?'&':'?').'token='.$token.
13774:                                '&tokenissued='.$perlvar{'lonHostID'};
13775:     } else {
13776:         return '/adm/notfound.html';
13777:     }
13778: }
13779: 
13780: # call with reqtype HEAD: get last modification time
13781: # call with reqtype GET: get the file contents
13782: # Do not call this with reqtype GET for large files! It loads everything into memory
13783: #
13784: sub getuploaded {
13785:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
13786:     $uri=~s/^\///;
13787:     my $homeserver = &homeserver($cnum,$cdom);
13788:     my $hostname = &hostname($homeserver);
13789:     my $protocol = $protocol{$homeserver};
13790:     $protocol = 'http' if ($protocol ne 'https');
13791:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
13792:     my $request=new HTTP::Request($reqtype,$uri);
13793:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
13794:     $$rtncode = $response->code;
13795:     if (! $response->is_success()) {
13796: 	return 'failed';
13797:     }      
13798:     if ($reqtype eq 'HEAD') {
13799: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
13800:     } elsif ($reqtype eq 'GET') {
13801: 	$$info = $response->content;
13802:     }
13803:     return 'ok';
13804: }
13805: 
13806: sub readfile {
13807:     my $file = shift;
13808:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
13809:     my $fh;
13810:     open($fh,"<",$file);
13811:     my $a='';
13812:     while (my $line = <$fh>) { $a .= $line; }
13813:     return $a;
13814: }
13815: 
13816: sub filelocation {
13817:     my ($dir,$file) = @_;
13818:     my $location;
13819:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
13820: 
13821:     if ($file =~ m-^/adm/-) {
13822: 	$file=~s-^/adm/wrapper/-/-;
13823: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
13824:     }
13825: 
13826:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
13827:         $location = $file;
13828:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
13829:         my ($udom,$uname,$filename)=
13830:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
13831:         my $home=&homeserver($uname,$udom);
13832:         my $is_me=0;
13833:         my @ids=&current_machine_ids();
13834:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
13835:         if ($is_me) {
13836:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
13837:         } else {
13838:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
13839:   	      $udom.'/'.$uname.'/'.$filename;
13840:         }
13841:     } elsif ($file =~ m-^/adm/-) {
13842: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
13843:     } else {
13844:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
13845:         $file=~s:^/(res|priv)/:/:;
13846:         my $space=$1;
13847:         if ( !( $file =~ m:^/:) ) {
13848:             $location = $dir. '/'.$file;
13849:         } else {
13850:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
13851:         }
13852:     }
13853:     $location=~s://+:/:g; # remove duplicate /
13854:     while ($location=~m{/\.\./}) {
13855: 	if ($location =~ m{/[^/]+/\.\./}) {
13856: 	    $location=~ s{/[^/]+/\.\./}{/}g;
13857: 	} else {
13858: 	    $location=~ s{/\.\./}{/}g;
13859: 	}
13860:     } #remove dir/..
13861:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
13862:     return $location;
13863: }
13864: 
13865: sub hreflocation {
13866:     my ($dir,$file)=@_;
13867:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
13868: 	$file=filelocation($dir,$file);
13869:     } elsif ($file=~m-^/adm/-) {
13870: 	$file=~s-^/adm/wrapper/-/-;
13871: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
13872:     }
13873:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
13874: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
13875:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
13876: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
13877: 	        {/uploaded/$1/$2/}x;
13878:     }
13879:     if ($file=~ m{^/userfiles/}) {
13880: 	$file =~ s{^/userfiles/}{/uploaded/};
13881:     }
13882:     return $file;
13883: }
13884: 
13885: 
13886: 
13887: 
13888: 
13889: sub current_machine_domains {
13890:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
13891: }
13892: 
13893: sub machine_domains {
13894:     my ($hostname) = @_;
13895:     my @domains;
13896:     my %hostname = &all_hostnames();
13897:     while( my($id, $name) = each(%hostname)) {
13898: #	&logthis("-$id-$name-$hostname-");
13899: 	if ($hostname eq $name) {
13900: 	    push(@domains,&host_domain($id));
13901: 	}
13902:     }
13903:     return @domains;
13904: }
13905: 
13906: sub current_machine_ids {
13907:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
13908: }
13909: 
13910: sub machine_ids {
13911:     my ($hostname) = @_;
13912:     $hostname ||= &hostname($perlvar{'lonHostID'});
13913:     my @ids;
13914:     my %name_to_host = &all_names();
13915:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
13916: 	return @{ $name_to_host{$hostname} };
13917:     }
13918:     return;
13919: }
13920: 
13921: sub additional_machine_domains {
13922:     my @domains;
13923:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
13924:     while( my $line = <$fh>) {
13925:         $line =~ s/\s//g;
13926:         push(@domains,$line);
13927:     }
13928:     return @domains;
13929: }
13930: 
13931: sub default_login_domain {
13932:     my $domain = $perlvar{'lonDefDomain'};
13933:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
13934:     foreach my $posdom (&current_machine_domains(),
13935:                         &additional_machine_domains()) {
13936:         if (lc($posdom) eq lc($testdomain)) {
13937:             $domain=$posdom;
13938:             last;
13939:         }
13940:     }
13941:     return $domain;
13942: }
13943: 
13944: sub uses_sts {
13945:     my ($ignore_cache) = @_;
13946:     my $lonhost = $perlvar{'lonHostID'};
13947:     my $hostname = &hostname($lonhost);
13948:     my $sts_on;
13949:     if ($protocol{$lonhost} eq 'https') {
13950:         my $cachetime = 12*3600;
13951:         if (!$ignore_cache) {
13952:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
13953:             if (defined($cached)) {
13954:                 return $sts_on;
13955:             }
13956:         }
13957:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
13958:         my $request=new HTTP::Request('HEAD',$url);
13959:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
13960:         if ($response->is_success) {
13961:             my $has_sts = $response->header('Strict-Transport-Security');
13962:             if ($has_sts eq '') {
13963:                 $sts_on = 0;
13964:             } else {
13965:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
13966:                     my $maxage = $1;
13967:                     if ($maxage) {
13968:                         $sts_on = 1;
13969:                     } else {
13970:                         $sts_on = 0;
13971:                     }
13972:                 } else {
13973:                     $sts_on = 0;
13974:                 }
13975:             }
13976:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
13977:         }
13978:     }
13979:     return;
13980: }
13981: 
13982: # ------------------------------------------------------------- Declutters URLs
13983: 
13984: sub declutter {
13985:     my $thisfn=shift;
13986:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13987:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
13988:         $thisfn=~s{^/home/httpd/html}{};
13989:     }
13990:     $thisfn=~s/^\///;
13991:     $thisfn=~s|^adm/wrapper/||;
13992:     $thisfn=~s|^adm/coursedocs/showdoc/||;
13993:     $thisfn=~s/^res\///;
13994:     $thisfn=~s/^priv\///;
13995:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
13996:         $thisfn=~s/\?.+$//;
13997:     }
13998:     return $thisfn;
13999: }
14000: 
14001: # ------------------------------------------------------------- Clutter up URLs
14002: 
14003: sub clutter {
14004:     my $thisfn='/'.&declutter(shift);
14005:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14006: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14007:        $thisfn='/res'.$thisfn; 
14008:     }
14009:     if ($thisfn !~m|^/adm|) {
14010: 	if ($thisfn =~ m|^/ext/|) {
14011: 	    $thisfn='/adm/wrapper'.$thisfn;
14012: 	} else {
14013: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14014: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14015: 	    if ($embstyle eq 'ssi'
14016: 		|| ($embstyle eq 'hdn')
14017: 		|| ($embstyle eq 'rat')
14018: 		|| ($embstyle eq 'prv')
14019: 		|| ($embstyle eq 'ign')) {
14020: 		#do nothing with these
14021: 	    } elsif (($embstyle eq 'img') 
14022: 		|| ($embstyle eq 'emb')
14023: 		|| ($embstyle eq 'wrp')) {
14024: 		$thisfn='/adm/wrapper'.$thisfn;
14025: 	    } elsif ($embstyle eq 'unk'
14026: 		     && $thisfn!~/\.(sequence|page)$/) {
14027: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14028: 	    } else {
14029: #		&logthis("Got a blank emb style");
14030: 	    }
14031: 	}
14032:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14033:         $thisfn='/adm/wrapper'.$thisfn;
14034:     }
14035:     return $thisfn;
14036: }
14037: 
14038: sub clutter_with_no_wrapper {
14039:     my $uri = &clutter(shift);
14040:     if ($uri =~ m-^/adm/-) {
14041: 	$uri =~ s-^/adm/wrapper/-/-;
14042: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14043:     }
14044:     return $uri;
14045: }
14046: 
14047: sub freeze_escape {
14048:     my ($value)=@_;
14049:     if (ref($value)) {
14050: 	$value=&nfreeze($value);
14051: 	return '__FROZEN__'.&escape($value);
14052:     }
14053:     return &escape($value);
14054: }
14055: 
14056: 
14057: sub thaw_unescape {
14058:     my ($value)=@_;
14059:     if ($value =~ /^__FROZEN__/) {
14060: 	substr($value,0,10,undef);
14061: 	$value=&unescape($value);
14062: 	return &thaw($value);
14063:     }
14064:     return &unescape($value);
14065: }
14066: 
14067: sub correct_line_ends {
14068:     my ($result)=@_;
14069:     $$result =~s/\r\n/\n/mg;
14070:     $$result =~s/\r/\n/mg;
14071: }
14072: # ================================================================ Main Program
14073: 
14074: sub goodbye {
14075:    &logthis("Starting Shut down");
14076: #not converted to using infrastruture and probably shouldn't be
14077:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14078: #converted
14079: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14080:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14081: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14082: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14083: #1.1 only
14084: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14085: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14086: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14087: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14088:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14089:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14090:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14091:    &flushcourselogs();
14092:    &logthis("Shutting down");
14093: }
14094: 
14095: sub get_dns {
14096:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14097:     if (!$ignore_cache) {
14098: 	my ($content,$cached)=
14099: 	    &Apache::lonnet::is_cached_new('dns',$url);
14100: 	if ($cached) {
14101: 	    &$func($content,$hashref);
14102: 	    return;
14103: 	}
14104:     }
14105: 
14106:     my %alldns;
14107:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14108:         foreach my $dns (<$config>) {
14109: 	    next if ($dns !~ /^\^(\S*)/x);
14110:             my $line = $1;
14111:             my ($host,$protocol) = split(/:/,$line);
14112:             if ($protocol ne 'https') {
14113:                 $protocol = 'http';
14114:             }
14115: 	    $alldns{$host} = $protocol;
14116:         }
14117:         close($config);
14118:     }
14119:     while (%alldns) {
14120: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14121: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14122:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14123:         delete($alldns{$dns});
14124: 	next if ($response->is_error());
14125:         if ($url eq '/adm/dns/loncapaCRL') {
14126:             return &$func($response);
14127:         } else {
14128: 	    my @content = split("\n",$response->content);
14129: 	    unless ($nocache) {
14130: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14131: 	    }
14132: 	    &$func(\@content,$hashref);
14133:             return;
14134:         }
14135:     }
14136:     my $which = (split('/',$url,4))[3];
14137:     if ($which eq 'loncapaCRL') {
14138:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14139:         if (-e $diskfile) {
14140:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14141:         } else {
14142:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14143:         }
14144:     } else {
14145:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14146:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14147:             my @content = <$config>;
14148:             close($config);
14149:             &$func(\@content,$hashref);
14150:         }
14151:     }
14152:     return;
14153: }
14154: 
14155: # ------------------------------------------------------Get DNS checksums file
14156: sub parse_dns_checksums_tab {
14157:     my ($lines,$hashref) = @_;
14158:     my $lonhost = $perlvar{'lonHostID'};
14159:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14160:     my $loncaparev = &get_server_loncaparev($machine_dom);
14161:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14162:     my $webconfdir = '/etc/httpd/conf';
14163:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14164:         $webconfdir = '/etc/apache2';
14165:     } elsif ($distro =~ /^sles(\d+)$/) {
14166:         if ($1 >= 10) {
14167:             $webconfdir = '/etc/apache2';
14168:         }
14169:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14170:         if ($1 >= 10.0) {
14171:             $webconfdir = '/etc/apache2';
14172:         }
14173:     }
14174:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14175:     my (%chksum,%revnum);
14176:     if (ref($lines) eq 'ARRAY') {
14177:         chomp(@{$lines});
14178:         my $version = shift(@{$lines});
14179:         if ($version eq $release) {  
14180:             foreach my $line (@{$lines}) {
14181:                 my ($file,$version,$shasum) = split(/,/,$line);
14182:                 if ($file =~ m{^/etc/httpd/conf}) {
14183:                     if ($webconfdir eq '/etc/apache2') {
14184:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14185:                     }
14186:                 }
14187:                 $chksum{$file} = $shasum;
14188:                 $revnum{$file} = $version;
14189:             }
14190:             if (ref($hashref) eq 'HASH') {
14191:                 %{$hashref} = (
14192:                                 sums     => \%chksum,
14193:                                 versions => \%revnum,
14194:                               );
14195:             }
14196:         }
14197:     }
14198:     return;
14199: }
14200: 
14201: sub fetch_dns_checksums {
14202:     my %checksums;
14203:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14204:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14205:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14206:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14207:              \%checksums);
14208:     return \%checksums;
14209: }
14210: 
14211: sub fetch_crl_pemfile {
14212:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14213: }
14214: 
14215: sub save_crl_pem {
14216:     my ($response) = @_;
14217:     my ($msg,$hadchanges);
14218:     if (ref($response)) {
14219:         my $now = time;
14220:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14221:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14222:         if (open(my $fh,'>',"$tmpcrl")) {
14223:             print $fh $response->content;
14224:             close($fh);
14225:             if (-e $lonca) {
14226:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14227:                     my $check = <PIPE>;
14228:                     close(PIPE);
14229:                     chomp($check);
14230:                     if ($check eq 'verify OK') {
14231:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14232:                         my $backup;
14233:                         if (-e $dest) {
14234:                             if (&File::Copy::move($dest,"$dest.bak")) {
14235:                                 $backup = 'ok';
14236:                             }
14237:                         }
14238:                         if (&File::Copy::move($tmpcrl,$dest)) {
14239:                             $msg = 'ok';
14240:                             if ($backup) {
14241:                                 my (%oldnums,%newnums);
14242:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14243:                                     while (<PIPE>) {
14244:                                         $oldnums{(split(/:/))[1]} = 1;
14245:                                     }
14246:                                     close(PIPE);
14247:                                 }
14248:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14249:                                     while(<PIPE>) {
14250:                                         $newnums{(split(/:/))[1]} = 1;
14251:                                     }
14252:                                     close(PIPE);
14253:                                 }
14254:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14255:                                     unless (exists($oldnums{$key})) {
14256:                                         $hadchanges = 1;
14257:                                         last;
14258:                                     }
14259:                                 }
14260:                                 unless ($hadchanges) {
14261:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14262:                                         unless (exists($newnums{$key})) {
14263:                                             $hadchanges = 1;
14264:                                             last;
14265:                                         }
14266:                                     }
14267:                                 }
14268:                             }
14269:                         }
14270:                     } else {
14271:                         unlink($tmpcrl);
14272:                     }
14273:                 } else {
14274:                     unlink($tmpcrl);
14275:                 }
14276:             } else {
14277:                 unlink($tmpcrl);
14278:             }
14279:         }
14280:     }
14281:     return ($msg,$hadchanges);
14282: }
14283: 
14284: # ------------------------------------------------------------ Read domain file
14285: {
14286:     my $loaded;
14287:     my %domain;
14288: 
14289:     sub parse_domain_tab {
14290: 	my ($lines) = @_;
14291: 	foreach my $line (@$lines) {
14292: 	    next if ($line =~ /^(\#|\s*$ )/x);
14293: 
14294: 	    chomp($line);
14295: 	    my ($name,@elements) = split(/:/,$line,9);
14296: 	    my %this_domain;
14297: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14298: 			       'lang_def', 'city', 'longi', 'lati',
14299: 			       'primary') {
14300: 		$this_domain{$field} = shift(@elements);
14301: 	    }
14302: 	    $domain{$name} = \%this_domain;
14303: 	}
14304:     }
14305: 
14306:     sub reset_domain_info {
14307: 	undef($loaded);
14308: 	undef(%domain);
14309:     }
14310: 
14311:     sub load_domain_tab {
14312: 	my ($ignore_cache,$nocache) = @_;
14313: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14314: 	my $fh;
14315: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14316: 	    my @lines = <$fh>;
14317: 	    &parse_domain_tab(\@lines);
14318: 	}
14319: 	close($fh);
14320: 	$loaded = 1;
14321:     }
14322: 
14323:     sub domain {
14324: 	&load_domain_tab() if (!$loaded);
14325: 
14326: 	my ($name,$what) = @_;
14327: 	return if ( !exists($domain{$name}) );
14328: 
14329: 	if (!$what) {
14330: 	    return $domain{$name}{'description'};
14331: 	}
14332: 	return $domain{$name}{$what};
14333:     }
14334: 
14335:     sub domain_info {
14336:         &load_domain_tab() if (!$loaded);
14337:         return %domain;
14338:     }
14339: 
14340: }
14341: 
14342: 
14343: # ------------------------------------------------------------- Read hosts file
14344: {
14345:     my %hostname;
14346:     my %hostdom;
14347:     my %libserv;
14348:     my $loaded;
14349:     my %name_to_host;
14350:     my %internetdom;
14351:     my %LC_dns_serv;
14352: 
14353:     sub parse_hosts_tab {
14354: 	my ($file) = @_;
14355: 	foreach my $configline (@$file) {
14356: 	    next if ($configline =~ /^(\#|\s*$ )/x);
14357:             chomp($configline);
14358: 	    if ($configline =~ /^\^/) {
14359:                 if ($configline =~ /^\^([\w.\-]+)/) {
14360:                     $LC_dns_serv{$1} = 1;
14361:                 }
14362:                 next;
14363:             }
14364: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
14365: 	    $name=~s/\s//g;
14366: 	    if ($id && $domain && $role && $name) {
14367:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
14368:                     my $curr = $hostname{$id};
14369:                     my $skip;
14370:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
14371:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
14372:                             $skip = 1;
14373:                         } else {
14374:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
14375:                         }
14376:                     }
14377:                     unless ($skip) {
14378:                         push(@{$name_to_host{$name}},$id);
14379:                     }
14380:                 } else {
14381:                     push(@{$name_to_host{$name}},$id);
14382:                 }
14383: 		$hostname{$id}=$name;
14384: 		$hostdom{$id}=$domain;
14385: 		if ($role eq 'library') { $libserv{$id}=$name; }
14386:                 if (defined($protocol)) {
14387:                     if ($protocol eq 'https') {
14388:                         $protocol{$id} = $protocol;
14389:                     } else {
14390:                         $protocol{$id} = 'http'; 
14391:                     }
14392:                 } else {
14393:                     $protocol{$id} = 'http';
14394:                 }
14395:                 if (defined($intdom)) {
14396:                     $internetdom{$id} = $intdom;
14397:                 }
14398: 	    }
14399: 	}
14400:     }
14401:     
14402:     sub reset_hosts_info {
14403: 	&purge_remembered();
14404: 	&reset_domain_info();
14405: 	&reset_hosts_ip_info();
14406:         undef(%internetdom);
14407: 	undef(%name_to_host);
14408: 	undef(%hostname);
14409: 	undef(%hostdom);
14410: 	undef(%libserv);
14411: 	undef($loaded);
14412:     }
14413: 
14414:     sub load_hosts_tab {
14415: 	my ($ignore_cache,$nocache) = @_;
14416: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
14417: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
14418: 	my @config = <$config>;
14419: 	&parse_hosts_tab(\@config);
14420: 	close($config);
14421: 	$loaded=1;
14422:     }
14423: 
14424:     sub hostname {
14425: 	&load_hosts_tab() if (!$loaded);
14426: 
14427: 	my ($lonid) = @_;
14428: 	return $hostname{$lonid};
14429:     }
14430: 
14431:     sub all_hostnames {
14432: 	&load_hosts_tab() if (!$loaded);
14433: 
14434: 	return %hostname;
14435:     }
14436: 
14437:     sub all_names {
14438:         my ($ignore_cache,$nocache) = @_;
14439: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
14440: 
14441: 	return %name_to_host;
14442:     }
14443: 
14444:     sub all_host_domain {
14445:         &load_hosts_tab() if (!$loaded);
14446:         return %hostdom;
14447:     }
14448: 
14449:     sub all_host_intdom {
14450:         &load_hosts_tab() if (!$loaded);
14451:         return %internetdom;
14452:     }
14453: 
14454:     sub is_library {
14455: 	&load_hosts_tab() if (!$loaded);
14456: 
14457: 	return exists($libserv{$_[0]});
14458:     }
14459: 
14460:     sub all_library {
14461: 	&load_hosts_tab() if (!$loaded);
14462: 
14463: 	return %libserv;
14464:     }
14465: 
14466:     sub unique_library {
14467: 	#2x reverse removes all hostnames that appear more than once
14468:         my %unique = reverse &all_library();
14469:         return reverse %unique;
14470:     }
14471: 
14472:     sub get_servers {
14473: 	&load_hosts_tab() if (!$loaded);
14474: 
14475: 	my ($domain,$type) = @_;
14476: 	my %possible_hosts = ($type eq 'library') ? %libserv
14477: 	                                          : %hostname;
14478: 	my %result;
14479: 	if (ref($domain) eq 'ARRAY') {
14480: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14481: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
14482: 		    $result{$host} = $hostname;
14483: 		}
14484: 	    }
14485: 	} else {
14486: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14487: 		if ($hostdom{$host} eq $domain) {
14488: 		    $result{$host} = $hostname;
14489: 		}
14490: 	    }
14491: 	}
14492: 	return %result;
14493:     }
14494: 
14495:     sub get_unique_servers {
14496:         my %unique = reverse &get_servers(@_);
14497: 	return reverse %unique;
14498:     }
14499: 
14500:     sub host_domain {
14501: 	&load_hosts_tab() if (!$loaded);
14502: 
14503: 	my ($lonid) = @_;
14504: 	return $hostdom{$lonid};
14505:     }
14506: 
14507:     sub all_domains {
14508: 	&load_hosts_tab() if (!$loaded);
14509: 
14510: 	my %seen;
14511: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
14512: 	return @uniq;
14513:     }
14514: 
14515:     sub internet_dom {
14516:         &load_hosts_tab() if (!$loaded);
14517: 
14518:         my ($lonid) = @_;
14519:         return $internetdom{$lonid};
14520:     }
14521: 
14522:     sub is_LC_dns {
14523:         &load_hosts_tab() if (!$loaded);
14524: 
14525:         my ($hostname) = @_;
14526:         return exists($LC_dns_serv{$hostname});
14527:     }
14528: 
14529: }
14530: 
14531: { 
14532:     my %iphost;
14533:     my %name_to_ip;
14534:     my %lonid_to_ip;
14535: 
14536:     sub get_hosts_from_ip {
14537: 	my ($ip) = @_;
14538: 	my %iphosts = &get_iphost();
14539: 	if (ref($iphosts{$ip})) {
14540: 	    return @{$iphosts{$ip}};
14541: 	}
14542: 	return;
14543:     }
14544:     
14545:     sub reset_hosts_ip_info {
14546: 	undef(%iphost);
14547: 	undef(%name_to_ip);
14548: 	undef(%lonid_to_ip);
14549:     }
14550: 
14551:     sub get_host_ip {
14552: 	my ($lonid) = @_;
14553: 	if (exists($lonid_to_ip{$lonid})) {
14554: 	    return $lonid_to_ip{$lonid};
14555: 	}
14556: 	my $name=&hostname($lonid);
14557:    	my $ip = gethostbyname($name);
14558: 	return if (!$ip || length($ip) ne 4);
14559: 	$ip=inet_ntoa($ip);
14560: 	$name_to_ip{$name}   = $ip;
14561: 	$lonid_to_ip{$lonid} = $ip;
14562: 	return $ip;
14563:     }
14564:     
14565:     sub get_iphost {
14566: 	my ($ignore_cache,$nocache) = @_;
14567: 
14568: 	if (!$ignore_cache) {
14569: 	    if (%iphost) {
14570: 		return %iphost;
14571: 	    }
14572: 	    my ($ip_info,$cached)=
14573: 		&Apache::lonnet::is_cached_new('iphost','iphost');
14574: 	    if ($cached) {
14575: 		%iphost      = %{$ip_info->[0]};
14576: 		%name_to_ip  = %{$ip_info->[1]};
14577: 		%lonid_to_ip = %{$ip_info->[2]};
14578: 		return %iphost;
14579: 	    }
14580: 	}
14581: 
14582: 	# get yesterday's info for fallback
14583: 	my %old_name_to_ip;
14584: 	my ($ip_info,$cached)=
14585: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
14586: 	if ($cached) {
14587: 	    %old_name_to_ip = %{$ip_info->[1]};
14588: 	}
14589: 
14590: 	my %name_to_host = &all_names($ignore_cache,$nocache);
14591: 	foreach my $name (keys(%name_to_host)) {
14592: 	    my $ip;
14593: 	    if (!exists($name_to_ip{$name})) {
14594: 		$ip = gethostbyname($name);
14595: 		if (!$ip || length($ip) ne 4) {
14596: 		    if (defined($old_name_to_ip{$name})) {
14597: 			$ip = $old_name_to_ip{$name};
14598: 			&logthis("Can't find $name defaulting to old $ip");
14599: 		    } else {
14600: 			&logthis("Name $name no IP found");
14601: 			next;
14602: 		    }
14603: 		} else {
14604: 		    $ip=inet_ntoa($ip);
14605: 		}
14606: 		$name_to_ip{$name} = $ip;
14607: 	    } else {
14608: 		$ip = $name_to_ip{$name};
14609: 	    }
14610: 	    foreach my $id (@{ $name_to_host{$name} }) {
14611: 		$lonid_to_ip{$id} = $ip;
14612: 	    }
14613: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
14614: 	}
14615:         unless ($nocache) {
14616: 	    &do_cache_new('iphost','iphost',
14617: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
14618: 		          48*60*60);
14619:         }
14620: 
14621: 	return %iphost;
14622:     }
14623: 
14624:     #
14625:     #  Given a DNS returns the loncapa host name for that DNS 
14626:     # 
14627:     sub host_from_dns {
14628:         my ($dns) = @_;
14629:         my @hosts;
14630:         my $ip;
14631: 
14632:         if (exists($name_to_ip{$dns})) {
14633:             $ip = $name_to_ip{$dns};
14634:         }
14635:         if (!$ip) {
14636:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
14637:             if (length($ip) == 4) { 
14638: 	        $ip   = &IO::Socket::inet_ntoa($ip);
14639:             }
14640:         }
14641:         if ($ip) {
14642: 	    @hosts = get_hosts_from_ip($ip);
14643: 	    return $hosts[0];
14644:         }
14645:         return undef;
14646:     }
14647: 
14648:     sub get_internet_names {
14649:         my ($lonid) = @_;
14650:         return if ($lonid eq '');
14651:         my ($idnref,$cached)=
14652:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
14653:         if ($cached) {
14654:             return $idnref;
14655:         }
14656:         my $ip = &get_host_ip($lonid);
14657:         my @hosts = &get_hosts_from_ip($ip);
14658:         my %iphost = &get_iphost();
14659:         my (@idns,%seen);
14660:         foreach my $id (@hosts) {
14661:             my $dom = &host_domain($id);
14662:             my $prim_id = &domain($dom,'primary');
14663:             my $prim_ip = &get_host_ip($prim_id);
14664:             next if ($seen{$prim_ip});
14665:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
14666:                 foreach my $id (@{$iphost{$prim_ip}}) {
14667:                     my $intdom = &internet_dom($id);
14668:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
14669:                         push(@idns,$intdom);
14670:                     }
14671:                 }
14672:             }
14673:             $seen{$prim_ip} = 1;
14674:         }
14675:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
14676:     }
14677: 
14678: }
14679: 
14680: sub all_loncaparevs {
14681:     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);
14682: }
14683: 
14684: # ---------------------------------------------------------- Read loncaparev table
14685: {
14686:     sub load_loncaparevs { 
14687:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
14688:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
14689:                 while (my $configline=<$config>) {
14690:                     chomp($configline);
14691:                     my ($hostid,$loncaparev)=split(/:/,$configline);
14692:                     $loncaparevs{$hostid}=$loncaparev;
14693:                 }
14694:                 close($config);
14695:             }
14696:         }
14697:     }
14698: }
14699: 
14700: # ---------------------------------------------------------- Read serverhostID table
14701: {
14702:     sub load_serverhomeIDs {
14703:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
14704:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
14705:                 while (my $configline=<$config>) {
14706:                     chomp($configline);
14707:                     my ($name,$id)=split(/:/,$configline);
14708:                     $serverhomeIDs{$name}=$id;
14709:                 }
14710:                 close($config);
14711:             }
14712:         }
14713:     }
14714: }
14715: 
14716: 
14717: BEGIN {
14718: 
14719: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
14720:     unless ($readit) {
14721: {
14722:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
14723:     %perlvar = (%perlvar,%{$configvars});
14724: }
14725: 
14726: 
14727: # ------------------------------------------------------ Read spare server file
14728: {
14729:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
14730: 
14731:     while (my $configline=<$config>) {
14732:        chomp($configline);
14733:        if ($configline) {
14734: 	   my ($host,$type) = split(':',$configline,2);
14735: 	   if (!defined($type) || $type eq '') { $type = 'default' };
14736: 	   push(@{ $spareid{$type} }, $host);
14737:        }
14738:     }
14739:     close($config);
14740: }
14741: # ------------------------------------------------------------ Read permissions
14742: {
14743:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
14744: 
14745:     while (my $configline=<$config>) {
14746: 	chomp($configline);
14747: 	if ($configline) {
14748: 	    my ($role,$perm)=split(/ /,$configline);
14749: 	    if ($perm ne '') { $pr{$role}=$perm; }
14750: 	}
14751:     }
14752:     close($config);
14753: }
14754: 
14755: # -------------------------------------------- Read plain texts for permissions
14756: {
14757:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
14758: 
14759:     while (my $configline=<$config>) {
14760: 	chomp($configline);
14761: 	if ($configline) {
14762: 	    my ($short,@plain)=split(/:/,$configline);
14763:             %{$prp{$short}} = ();
14764: 	    if (@plain > 0) {
14765:                 $prp{$short}{'std'} = $plain[0];
14766:                 for (my $i=1; $i<@plain; $i++) {
14767:                     $prp{$short}{'alt'.$i} = $plain[$i];  
14768:                 }
14769:             }
14770: 	}
14771:     }
14772:     close($config);
14773: }
14774: 
14775: # ---------------------------------------------------------- Read package table
14776: {
14777:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
14778: 
14779:     while (my $configline=<$config>) {
14780: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
14781: 	chomp($configline);
14782: 	my ($short,$plain)=split(/:/,$configline);
14783: 	my ($pack,$name)=split(/\&/,$short);
14784: 	if ($plain ne '') {
14785: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
14786: 	    $packagetab{$short}=$plain; 
14787: 	}
14788:     }
14789:     close($config);
14790: }
14791: 
14792: # ---------------------------------------------------------- Read loncaparev table
14793: 
14794: &load_loncaparevs();
14795: 
14796: # ---------------------------------------------------------- Read serverhostID table
14797: 
14798: &load_serverhomeIDs();
14799: 
14800: # ---------------------------------------------------------- Read releaseslist XML
14801: {
14802:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
14803:     if (-e $file) {
14804:         my $parser = HTML::LCParser->new($file);
14805:         while (my $token = $parser->get_token()) {
14806:             if ($token->[0] eq 'S') {
14807:                 my $item = $token->[1];
14808:                 my $name = $token->[2]{'name'};
14809:                 my $value = $token->[2]{'value'};
14810:                 my $valuematch = $token->[2]{'valuematch'};
14811:                 my $namematch = $token->[2]{'namematch'};
14812:                 if ($item eq 'parameter') {
14813:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
14814:                         my $release = $parser->get_text();
14815:                         $release =~ s/(^\s*|\s*$ )//gx;
14816:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
14817:                     }
14818:                 } elsif ($item ne '' && $name ne '') {
14819:                     my $release = $parser->get_text();
14820:                     $release =~ s/(^\s*|\s*$ )//gx;
14821:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
14822:                 }
14823:             }
14824:         }
14825:     }
14826: }
14827: 
14828: # ---------------------------------------------------------- Read managers table
14829: {
14830:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
14831:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
14832:             while (my $configline=<$config>) {
14833:                 chomp($configline);
14834:                 next if ($configline =~ /^\#/);
14835:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
14836:                     $managerstab{$configline} = 1;
14837:                 }
14838:             }
14839:             close($config);
14840:         }
14841:     }
14842: }
14843: 
14844: # ------------- set up temporary directory
14845: {
14846:     $tmpdir = LONCAPA::tempdir();
14847: 
14848: }
14849: 
14850: # ------------- set default texengine (domain default overrides this)
14851: {
14852:     $deftex = LONCAPA::texengine();
14853: }
14854: 
14855: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
14856: 				'compress_threshold'=> 20_000,
14857:  			        });
14858: 
14859: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
14860: $dumpcount=0;
14861: $locknum=0;
14862: 
14863: &logtouch();
14864: &logthis('<font color="yellow">INFO: Read configuration</font>');
14865: $readit=1;
14866:     {
14867: 	use integer;
14868: 	my $test=(2**32)+1;
14869: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
14870: 	&logthis(" Detected 64bit platform ($_64bit)");
14871:     }
14872: }
14873: }
14874: 
14875: 1;
14876: __END__
14877: 
14878: =pod
14879: 
14880: =head1 NAME
14881: 
14882: Apache::lonnet - Subroutines to ask questions about things in the network.
14883: 
14884: =head1 SYNOPSIS
14885: 
14886: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
14887: 
14888:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
14889: 
14890: Common parameters:
14891: 
14892: =over 4
14893: 
14894: =item *
14895: 
14896: $uname : an internal username (if $cname expecting a course Id specifically)
14897: 
14898: =item *
14899: 
14900: $udom : a domain (if $cdom expecting a course's domain specifically)
14901: 
14902: =item *
14903: 
14904: $symb : a resource instance identifier
14905: 
14906: =item *
14907: 
14908: $namespace : the name of a .db file that contains the data needed or
14909: being set.
14910: 
14911: =back
14912: 
14913: =head1 OVERVIEW
14914: 
14915: lonnet provides subroutines which interact with the
14916: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
14917: about classes, users, and resources.
14918: 
14919: For many of these objects you can also use this to store data about
14920: them or modify them in various ways.
14921: 
14922: =head2 Symbs
14923: 
14924: To identify a specific instance of a resource, LON-CAPA uses symbols
14925: or "symbs"X<symb>. These identifiers are built from the URL of the
14926: map, the resource number of the resource in the map, and the URL of
14927: the resource itself. The latter is somewhat redundant, but might help
14928: if maps change.
14929: 
14930: An example is
14931: 
14932:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
14933: 
14934: The respective map entry is
14935: 
14936:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
14937:   title="Problem 2">
14938:  </resource>
14939: 
14940: Symbs are used by the random number generator, as well as to store and
14941: restore data specific to a certain instance of for example a problem.
14942: 
14943: =head2 Storing And Retrieving Data
14944: 
14945: X<store()>X<cstore()>X<restore()>Three of the most important functions
14946: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
14947: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
14948: is is the non-critical message twin of cstore. These functions are for
14949: handlers to store a perl hash to a user's permanent data space in an
14950: easy manner, and to retrieve it again on another call. It is expected
14951: that a handler would use this once at the beginning to retrieve data,
14952: and then again once at the end to send only the new data back.
14953: 
14954: The data is stored in the user's data directory on the user's
14955: homeserver under the ID of the course.
14956: 
14957: The hash that is returned by restore will have all of the previous
14958: value for all of the elements of the hash.
14959: 
14960: Example:
14961: 
14962:  #creating a hash
14963:  my %hash;
14964:  $hash{'foo'}='bar';
14965: 
14966:  #storing it
14967:  &Apache::lonnet::cstore(\%hash);
14968: 
14969:  #changing a value
14970:  $hash{'foo'}='notbar';
14971: 
14972:  #adding a new value
14973:  $hash{'bar'}='foo';
14974:  &Apache::lonnet::cstore(\%hash);
14975: 
14976:  #retrieving the hash
14977:  my %history=&Apache::lonnet::restore();
14978: 
14979:  #print the hash
14980:  foreach my $key (sort(keys(%history))) {
14981:    print("\%history{$key} = $history{$key}");
14982:  }
14983: 
14984: Will print out:
14985: 
14986:  %history{1:foo} = bar
14987:  %history{1:keys} = foo:timestamp
14988:  %history{1:timestamp} = 990455579
14989:  %history{2:bar} = foo
14990:  %history{2:foo} = notbar
14991:  %history{2:keys} = foo:bar:timestamp
14992:  %history{2:timestamp} = 990455580
14993:  %history{bar} = foo
14994:  %history{foo} = notbar
14995:  %history{timestamp} = 990455580
14996:  %history{version} = 2
14997: 
14998: Note that the special hash entries C<keys>, C<version> and
14999: C<timestamp> were added to the hash. C<version> will be equal to the
15000: total number of versions of the data that have been stored. The
15001: C<timestamp> attribute will be the UNIX time the hash was
15002: stored. C<keys> is available in every historical section to list which
15003: keys were added or changed at a specific historical revision of a
15004: hash.
15005: 
15006: B<Warning>: do not store the hash that restore returns directly. This
15007: will cause a mess since it will restore the historical keys as if the
15008: were new keys. I.E. 1:foo will become 1:1:foo etc.
15009: 
15010: Calling convention:
15011: 
15012:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15013:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15014: 
15015: For more detailed information, see lonnet specific documentation.
15016: 
15017: =head1 RETURN MESSAGES
15018: 
15019: =over 4
15020: 
15021: =item * B<con_lost>: unable to contact remote host
15022: 
15023: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15024: when the connection is brought back up
15025: 
15026: =item * B<con_failed>: unable to contact remote host and unable to save message
15027: for later delivery
15028: 
15029: =item * B<error:>: an error a occurred, a description of the error follows the :
15030: 
15031: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15032: that was requested
15033: 
15034: =back
15035: 
15036: =head1 PUBLIC SUBROUTINES
15037: 
15038: =head2 Session Environment Functions
15039: 
15040: =over 4
15041: 
15042: =item * 
15043: X<appenv()>
15044: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15045: the user envirnoment file, and will be restored for each access this
15046: user makes during this session, also modifies the %env for the current
15047: process. Optional rolesarrayref - if defined contains a reference to an array
15048: of roles which are exempt from the restriction on modifying user.role entries 
15049: in the user's environment.db and in %env.    
15050: 
15051: =item *
15052: X<delenv()>
15053: B<delenv($delthis,$regexp)>: removes all items from the session
15054: environment file that begin with $delthis. If the 
15055: optional second arg - $regexp - is true, $delthis is treated as a 
15056: regular expression, otherwise \Q$delthis\E is used. 
15057: The values are also deleted from the current processes %env.
15058: 
15059: =item * get_env_multiple($name) 
15060: 
15061: gets $name from the %env hash, it seemlessly handles the cases where multiple
15062: values may be defined and end up as an array ref.
15063: 
15064: returns an array of values
15065: 
15066: =back
15067: 
15068: =head2 User Information
15069: 
15070: =over 4
15071: 
15072: =item *
15073: X<queryauthenticate()>
15074: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15075: authentication scheme
15076: 
15077: =item *
15078: X<authenticate()>
15079: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15080: authenticate user from domain's lib servers (first use the current
15081: one). C<$upass> should be the users password.
15082: $checkdefauth is optional (value is 1 if a check should be made to
15083:    authenticate user using default authentication method, and allow
15084:    account creation if username does not have account in the domain).
15085: $clientcancheckhost is optional (value is 1 if checking whether the
15086:    server can host will occur on the client side in lonauth.pm).   
15087: 
15088: =item *
15089: X<homeserver()>
15090: B<homeserver($uname,$udom)>: find the server which has
15091: the user's directory and files (there must be only one), this caches
15092: the answer, and also caches if there is a borken connection.
15093: 
15094: =item *
15095: X<idget()>
15096: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15097: a list of student/employee IDs or clicker IDs
15098: (student/employee IDs are a unique resource in a domain, there must be 
15099: only 1 ID per username, and only 1 username per ID in a specific domain).
15100: clickerIDs are not necessarily unique, as students might share clickers.
15101: (returns hash: id=>name,id=>name)
15102: 
15103: =item *
15104: X<idrget()>
15105: B<idrget($udom,@unames)>: find the IDs behind a list of
15106: usernames (returns hash: name=>id,name=>id)
15107: 
15108: =item *
15109: X<idput()>
15110: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15111: names and associated student/employee IDs or clicker IDs.
15112: 
15113: =item *
15114: X<iddel()>
15115: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15116: student/employee ID or clicker ID username look-ups from domain.
15117: The homeserver ($uhome) and namespace ($namespace) are optional.
15118: If no $uhome is provided, it will be determined usig &homeserver()
15119: for each user.  If no $namespace is provided, the default is ids.
15120: 
15121: =item *
15122: X<updateclickers()>
15123: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15124: clicker ID-to-username look-ups in clickers.db on library server.
15125: Permitted actions are add or del (i.e., add or delete). The 
15126: clickers.db contains clickerID as keys (escaped), and each corresponding
15127: value is an escaped comma-separated list of usernames (for whom the
15128: library server is the homeserver), who registered that particular ID.
15129: If $critical is true, the update will be sent via &critical, otherwise
15130: &reply() will be used.
15131: 
15132: =item *
15133: X<rolesinit()>
15134: B<rolesinit($udom,$username)>: get user privileges.
15135: returns user role, first access and timer interval hashes
15136: 
15137: =item *
15138: X<privileged()>
15139: B<privileged($username,$domain)>: returns a true if user has a
15140: privileged and active role (i.e. su or dc), false otherwise.
15141: 
15142: =item *
15143: X<getsection()>
15144: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15145: course $cname, return section name/number or '' for "not in course"
15146: and '-1' for "no section"
15147: 
15148: =item *
15149: X<userenvironment()>
15150: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15151: passed in @what from the requested user's environment, returns a hash
15152: 
15153: =item * 
15154: X<userlog_query()>
15155: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15156: activity.log file. %filters defines filters applied when parsing the
15157: log file. These can be start or end timestamps, or the type of action
15158: - log to look for Login or Logout events, check for Checkin or
15159: Checkout, role for role selection. The response is in the form
15160: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15161: escaped strings of the action recorded in the activity.log file.
15162: 
15163: =back
15164: 
15165: =head2 User Roles
15166: 
15167: =over 4
15168: 
15169: =item *
15170: 
15171: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15172: returns codes for allowed actions.
15173: 
15174: The first argument is required, all others are optional.
15175: 
15176: $priv is the privilege being checked.
15177: $uri contains additional information about what is being checked for access (e.g.,
15178: URL, course ID etc.). 
15179: $symb is the unique resource instance identifier in a course; if needed,
15180: but not provided, it will be retrieved via a call to &symbread(). 
15181: $role is the role for which a priv is being checked (only used if priv is evb). 
15182: $clientip is the user's IP address (only used when checking for access to portfolio 
15183: files).
15184: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15185: prevents recursive calls to &allowed.
15186: 
15187:  F: full access
15188:  U,I,K: authentication modes (cxx only)
15189:  '': forbidden
15190:  1: user needs to choose course
15191:  2: browse allowed
15192:  A: passphrase authentication needed
15193:  B: access temporarily blocked because of a blocking event in a course.
15194:  D: access blocked because access is required via session initiated via deep-link 
15195: 
15196: =item *
15197: 
15198: constructaccess($url,$setpriv) : check for access to construction space URL
15199: 
15200: See if the owner domain and name in the URL match those in the
15201: expected environment.  If so, return three element list
15202: ($ownername,$ownerdomain,$ownerhome).
15203: 
15204: Otherwise return the null string.
15205: 
15206: If second argument 'setpriv' is true, it assigns the privileges,
15207: and returns the same three element list, unless the owner has
15208: blocked "ad hoc" Domain Coordinator access to the Author Space,
15209: in which case the null string is returned.
15210: 
15211: =item *
15212: 
15213: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15214: define a custom role rolename set privileges in format of lonTabs/roles.tab
15215: for system, domain, and course level. $uname and $udom are optional (current
15216: user's username and domain will be used when either of $uname or $udom are absent.
15217: 
15218: =item *
15219: 
15220: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15221: (rolesplain.tab); plain text explanation of a user role term.
15222: $type is Course (default) or Community.
15223: If $forcedefault evaluates to true, text returned will be default 
15224: text for $type. Otherwise, if this is a course, the text returned 
15225: will be a custom name for the role (if defined in the course's 
15226: environment).  If no custom name is defined the default is returned.
15227:    
15228: =item *
15229: 
15230: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15231: All arguments are optional. Returns a hash of a roles, either for
15232: co-author/assistant author roles for a user's Construction Space
15233: (default), or if $context is 'userroles', roles for the user himself,
15234: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15235: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15236: For each key, value is set to colon-separated start and end times for
15237: the role.  If no username and domain are specified, will default to
15238: current user/domain. Types, roles, and roledoms are references to arrays
15239: of role statuses (active, future or previous), roles 
15240: (e.g., cc,in, st etc.) and domains of the roles which can be used
15241: to restrict the list of roles reported. If no array ref is 
15242: provided for types, will default to return only active roles.
15243: 
15244: =item *
15245: 
15246: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15247: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15248: 
15249: Additional optional arguments are: $type (if role checking is to be restricted 
15250: to certain user status types -- previous (expired roles), active (currently
15251: available roles) or future (roles available in the future), and
15252: $hideprivileged -- if true will not report course roles for users who
15253: have active Domain Coordinator role in course's domain or in additional
15254: domains (specified in 'Domains to check for privileged users' in course
15255: environment -- set via:  Course Settings -> Classlists and staff listing).
15256: 
15257: =item *
15258: 
15259: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15260: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15261: $possdomains and $possroles are optional array refs -- to domains to check and
15262: roles to check.  If $possdomains is not specified, a dump will be done of the
15263: users' roles.db to check for a dc or su role in any domain. This can be
15264: time consuming if &privileged is called repeatedly (e.g., when displaying a
15265: classlist), so in such cases, supplying a $possdomains array is preferred, as
15266: this then allows &privileged_by_domain() to be used, which caches the identity
15267: of privileged users, eliminating the need for repeated calls to &dump().
15268: 
15269: =item *
15270: 
15271: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15272: where the outer hash keys are domains specified in the $possdomains array ref,
15273: next inner hash keys are privileged roles specified in the $roles array ref,
15274: and the innermost hash contains key = value pairs for username:domain = end:start
15275: for active or future "privileged" users with that role in that domain. To avoid
15276: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15277: innerhash are cached using priv_$role and $dom as the identifiers.
15278: 
15279: =back
15280: 
15281: =head2 User Modification
15282: 
15283: =over 4
15284: 
15285: =item *
15286: 
15287: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15288: user for the level given by URL.  Optional start and end dates (leave empty
15289: string or zero for "no date")
15290: 
15291: =item *
15292: 
15293: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15294: change a users, password, possible return values are: ok,
15295: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15296: refused
15297: 
15298: =item *
15299: 
15300: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15301: 
15302: =item *
15303: 
15304: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15305:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15306: 
15307: will update user information (firstname,middlename,lastname,generation,
15308: permanentemail), and if forceid is true, student/employee ID also.
15309: A user's institutional affiliation(s) can also be updated.
15310: User information fields will not be overwritten with empty entries 
15311: unless the field is included in the $candelete array reference.
15312: This array is included when a single user is modified via "Manage Users",
15313: or when Autoupdate.pl is run by cron in a domain.
15314: 
15315: =item *
15316: 
15317: modifystudent
15318: 
15319: modify a student's enrollment and identification information.
15320: The course id is resolved based on the current user's environment.  
15321: This means the invoking user must be a course coordinator or otherwise
15322: associated with a course.
15323: 
15324: This call is essentially a wrapper for lonnet::modifyuser and
15325: lonnet::modify_student_enrollment
15326: 
15327: Inputs: 
15328: 
15329: =over 4
15330: 
15331: =item B<$udom> Student's loncapa domain
15332: 
15333: =item B<$uname> Student's loncapa login name
15334: 
15335: =item B<$uid> Student/Employee ID
15336: 
15337: =item B<$umode> Student's authentication mode
15338: 
15339: =item B<$upass> Student's password
15340: 
15341: =item B<$first> Student's first name
15342: 
15343: =item B<$middle> Student's middle name
15344: 
15345: =item B<$last> Student's last name
15346: 
15347: =item B<$gene> Student's generation
15348: 
15349: =item B<$usec> Student's section in course
15350: 
15351: =item B<$end> Unix time of the roles expiration
15352: 
15353: =item B<$start> Unix time of the roles start date
15354: 
15355: =item B<$forceid> If defined, allow $uid to be changed
15356: 
15357: =item B<$desiredhome> server to use as home server for student
15358: 
15359: =item B<$email> Student's permanent e-mail address
15360: 
15361: =item B<$type> Type of enrollment (auto or manual)
15362: 
15363: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
15364: 
15365: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
15366: 
15367: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
15368: 
15369: =item B<$context> role change context (shown in User Management Logs display in a course)
15370: 
15371: =item B<$inststatus> institutional status of user - : separated string of escaped status types
15372: 
15373: =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.
15374: 
15375: =back
15376: 
15377: =item *
15378: 
15379: modify_student_enrollment
15380: 
15381: Change a student's enrollment status in a class.  The environment variable
15382: 'role.request.course' must be defined for this function to proceed.
15383: 
15384: Inputs:
15385: 
15386: =over 4
15387: 
15388: =item $udom, student's domain
15389: 
15390: =item $uname, student's name
15391: 
15392: =item $uid, student's user id
15393: 
15394: =item $first, student's first name
15395: 
15396: =item $middle
15397: 
15398: =item $last
15399: 
15400: =item $gene
15401: 
15402: =item $usec
15403: 
15404: =item $end
15405: 
15406: =item $start
15407: 
15408: =item $type
15409: 
15410: =item $locktype
15411: 
15412: =item $cid
15413: 
15414: =item $selfenroll
15415: 
15416: =item $context
15417: 
15418: =item $credits, number of credits student will earn from this class
15419: 
15420: =item $instsec, institutional course section code for student
15421: 
15422: =back
15423: 
15424: 
15425: =item *
15426: 
15427: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
15428: custom role; give a custom role to a user for the level given by URL.  Specify
15429: name and domain of role author, and role name
15430: 
15431: =item *
15432: 
15433: revokerole($udom,$uname,$url,$role) : revoke a role for url
15434: 
15435: =item *
15436: 
15437: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
15438: 
15439: =back
15440: 
15441: =head2 Course Infomation
15442: 
15443: =over 4
15444: 
15445: =item *
15446: 
15447: coursedescription($courseid,$options) : returns a hash of information about the
15448: specified course id, including all environment settings for the
15449: course, the description of the course will be in the hash under the
15450: key 'description'
15451: 
15452: $options is an optional parameter that if supplied is a hash reference that controls
15453: what how this function works.  It has the following key/values:
15454: 
15455: =over 4
15456: 
15457: =item freshen_cache
15458: 
15459: If defined, and the environment cache for the course is valid, it is 
15460: returned in the returned hash.
15461: 
15462: =item one_time
15463: 
15464: If defined, the last cache time is set to _now_
15465: 
15466: =item user
15467: 
15468: If defined, the supplied username is used instead of the current user.
15469: 
15470: 
15471: =back
15472: 
15473: =item *
15474: 
15475: resdata($name,$domain,$type,@which) : request for current parameter
15476: setting for a specific $type, where $type is either 'course' or 'user',
15477: @what should be a list of parameters to ask about. This routine caches
15478: answers for 10 minutes.
15479: 
15480: =item *
15481: 
15482: get_courseresdata($courseid, $domain) : dump the entire course resource
15483: data base, returning a hash that is keyed by the resource name and has
15484: values that are the resource value.  I believe that the timestamps and
15485: versions are also returned.
15486: 
15487: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
15488: supplemental content area. This routine caches the number of files for 
15489: 10 minutes.
15490: 
15491: =back
15492: 
15493: =head2 Course Modification
15494: 
15495: =over 4
15496: 
15497: =item *
15498: 
15499: writecoursepref($courseid,%prefs) : write preferences (environment
15500: database) for a course
15501: 
15502: =item *
15503: 
15504: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
15505: 
15506: =item *
15507: 
15508: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
15509: 
15510: =item *
15511: 
15512: is_course($courseid), is_course($cdom, $cnum)
15513: 
15514: Accepts either a combined $courseid (in the form of domain_courseid) or the
15515: two component version $cdom, $cnum. It checks if the specified course exists.
15516: 
15517: Returns:
15518:     undef if the course doesn't exist, otherwise
15519:     in scalar context the combined courseid.
15520:     in list context the two components of the course identifier, domain and 
15521:     courseid.    
15522: 
15523: =back
15524: 
15525: =head2 Bubblesheet Configuration
15526: 
15527: =over 4
15528: 
15529: =item *
15530: 
15531: get_scantron_config($which)
15532: 
15533: $which - the name of the configuration to parse from the file.
15534: 
15535: Parses and returns the bubblesheet configuration line selected as a
15536: hash of configuration file fields.
15537: 
15538: 
15539: Returns:
15540:     If the named configuration is not in the file, an empty
15541:     hash is returned.
15542: 
15543:     a hash with the fields
15544:       name         - internal name for the this configuration setup
15545:       description  - text to display to operator that describes this config
15546:       CODElocation - if 0 or the string 'none'
15547:                           - no CODE exists for this config
15548:                      if -1 || the string 'letter'
15549:                           - a CODE exists for this config and is
15550:                             a string of letters
15551:                      Unsupported value (but planned for future support)
15552:                           if a positive integer
15553:                                - The CODE exists as the first n items from
15554:                                  the question section of the form
15555:                           if the string 'number'
15556:                                - The CODE exists for this config and is
15557:                                  a string of numbers
15558:       CODEstart   - (only matter if a CODE exists) column in the line where
15559:                      the CODE starts
15560:       CODElength  - length of the CODE
15561:       IDstart     - column where the student/employee ID starts
15562:       IDlength    - length of the student/employee ID info
15563:       Qstart      - column where the information from the bubbled
15564:                     'questions' start
15565:       Qlength     - number of columns comprising a single bubble line from
15566:                     the sheet. (usually either 1 or 10)
15567:       Qon         - either a single character representing the character used
15568:                     to signal a bubble was chosen in the positional setup, or
15569:                     the string 'letter' if the letter of the chosen bubble is
15570:                     in the final, or 'number' if a number representing the
15571:                     chosen bubble is in the file (1->A 0->J)
15572:       Qoff        - the character used to represent that a bubble was
15573:                     left blank
15574:       PaperID     - if the scanning process generates a unique number for each
15575:                     sheet scanned the column that this ID number starts in
15576:       PaperIDlength - number of columns that comprise the unique ID number
15577:                       for the sheet of paper
15578:       FirstName   - column that the first name starts in
15579:       FirstNameLength - number of columns that the first name spans
15580:       LastName    - column that the last name starts in
15581:       LastNameLength - number of columns that the last name spans
15582:       BubblesPerRow - number of bubbles available in each row used to
15583:                       bubble an answer. (If not specified, 10 assumed).
15584: 
15585: 
15586: =item *
15587: 
15588: get_scantronformat_file($cdom)
15589: 
15590: $cdom - the course's domain (optional); if not supplied, uses
15591: domain for current $env{'request.course.id'}.
15592: 
15593: Returns an array containing lines from the scantron format file for
15594: the domain of the course.
15595: 
15596: If a url for a custom.tab file is listed in domain's configuration.db,
15597: lines are from this file.
15598: 
15599: Otherwise, if a default.tab has been published in RES space by the
15600: domainconfig user, lines are from this file.
15601: 
15602: Otherwise, fall back to getting lines from the legacy file on the
15603: local server:  /home/httpd/lonTabs/default_scantronformat.tab
15604: 
15605: =back
15606: 
15607: =head2 Resource Subroutines
15608: 
15609: =over 4
15610: 
15611: =item *
15612: 
15613: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
15614: 
15615: =item *
15616: 
15617: repcopy($filename) : subscribes to the requested file, and attempts to
15618: replicate from the owning library server, Might return
15619: 'unavailable', 'not_found', 'forbidden', 'ok', or
15620: 'bad_request', also attempts to grab the metadata for the
15621: resource. Expects the local filesystem pathname
15622: (/home/httpd/html/res/....)
15623: 
15624: =back
15625: 
15626: =head2 Resource Information
15627: 
15628: =over 4
15629: 
15630: =item *
15631: 
15632: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
15633: and returns the value of a variety of different possible values,
15634: $varname should be a request string, and the other parameters can be
15635: used to specify who and what one is asking about. Ordinarily, $cid 
15636: does not need to be specified, as it is retrived from 
15637: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
15638: within lonuserstate::loadmap() when initializing a course, before
15639: $env{'request.course.id'} has been set, so it needs to be provided
15640: in that one case.
15641: 
15642: Possible values for $varname are environment.lastname (or other item
15643: from the envirnment hash), user.name (or someother aspect about the
15644: user), resource.0.maxtries (or some other part and parameter of a
15645: resource)
15646: 
15647: =item *
15648: 
15649: directcondval($number) : get current value of a condition; reads from a state
15650: string
15651: 
15652: =item *
15653: 
15654: condval($condidx) : value of condition index based on state
15655: 
15656: =item *
15657: 
15658: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
15659: resource's metadata, $what should be either a specific key, or either
15660: 'keys' (to get a list of possible keys) or 'packages' to get a list of
15661: packages that this resource currently uses, the last 3 arguments are 
15662: only used internally for recursive metadata.
15663: 
15664: the toolsymb is only used where the uri is for an external tool (for which
15665: the uri as well as the symb are guaranteed to be unique).
15666: 
15667: this function automatically caches all requests except any made recursively
15668: to retrieve a list of metadata keys for an imported library file ($liburi is 
15669: defined).
15670: 
15671: =item *
15672: 
15673: metadata_query($query,$custom,$customshow) : make a metadata query against the
15674: network of library servers; returns file handle of where SQL and regex results
15675: will be stored for query
15676: 
15677: =item *
15678: 
15679: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
15680: return symbolic list entry (all arguments optional). 
15681: 
15682: Args: filename is the filename (including path) for the file for which a symb 
15683: is required; donotrecurse, if true will prevent calls to allowed() being made 
15684: to check access status if more than one resource was found in the bighash 
15685: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
15686: a randompick); ignorecachednull, if true will prevent a symb of '' being 
15687: returned if $env{$cache_str} is defined as ''; checkforblock if true will
15688: cause possible symbs to be checked to determine if they are subject to content
15689: blocking, if so they will not be included as possible symbs; possibles is a
15690: ref to a hash, which, as a side effect, will be populated with all possible 
15691: symbs (content blocking not tested).
15692:  
15693: returns the data handle
15694: 
15695: =item *
15696: 
15697: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
15698: and is a possible symb for the URL in $thisfn, and if is an encrypted
15699: resource that the user accessed using /enc/ returns a 1 on success, 0
15700: on failure, user must be in a course, as it assumes the existence of
15701: the course initial hash, and uses $env('request.course.id'}.  The third
15702: arg is an optional reference to a scalar.  If this arg is passed in the 
15703: call to symbverify, it will be set to 1 if the symb has been set to be 
15704: encrypted; otherwise it will be null.  
15705: 
15706: =item *
15707: 
15708: symbclean($symb) : removes versions numbers from a symb, returns the
15709: cleaned symb
15710: 
15711: =item *
15712: 
15713: is_on_map($uri) : checks if the $uri is somewhere on the current
15714: course map, user must be in a course for it to work.
15715: 
15716: =item *
15717: 
15718: numval($salt) : return random seed value (addend for rndseed)
15719: 
15720: =item *
15721: 
15722: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
15723: a random seed, all arguments are optional, if they aren't sent it uses the
15724: environment to derive them. Note: if symb isn't sent and it can't get one
15725: from &symbread it will use the current time as its return value
15726: 
15727: =item *
15728: 
15729: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
15730: unfakeable, receipt
15731: 
15732: =item *
15733: 
15734: receipt() : API to ireceipt working off of env values; given out to users
15735: 
15736: =item *
15737: 
15738: countacc($url) : count the number of accesses to a given URL
15739: 
15740: =item *
15741: 
15742: 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
15743: 
15744: =item *
15745: 
15746: 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)
15747: 
15748: =item *
15749: 
15750: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
15751: 
15752: =item *
15753: 
15754: devalidate($symb) : devalidate temporary spreadsheet calculations,
15755: forcing spreadsheet to reevaluate the resource scores next time.
15756: 
15757: =item * 
15758: 
15759: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
15760: when viewing in course context.
15761: 
15762:  input: six args -- filename (decluttered), course number, course domain,
15763:                     url, symb (if registered) and group (if this is a 
15764:                     group item -- e.g., bulletin board, group page etc.).
15765: 
15766:  output: array of five scalars --
15767:          $cfile -- url for file editing if editable on current server
15768:          $home -- homeserver of resource (i.e., for author if published,
15769:                                           or course if uploaded.).
15770:          $switchserver --  1 if server switch will be needed.
15771:          $forceedit -- 1 if icon/link should be to go to edit mode 
15772:          $forceview -- 1 if icon/link should be to go to view mode
15773: 
15774: =item *
15775: 
15776: is_course_upload($file,$cnum,$cdom)
15777: 
15778: Used in course context to determine if current file was uploaded to 
15779: the course (i.e., would be found in /userfiles/docs on the course's 
15780: homeserver.
15781: 
15782:   input: 3 args -- filename (decluttered), course number and course domain.
15783:   output: boolean -- 1 if file was uploaded.
15784: 
15785: =back
15786: 
15787: =head2 Storing/Retreiving Data
15788: 
15789: =over 4
15790: 
15791: =item *
15792: 
15793: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
15794: permanently for this url; hashref needs to be given and should be a \%hashname;
15795: the remaining args aren't required and if they aren't passed or are '' they will
15796: be derived from the env (with the exception of $laststore, which is an 
15797: optional arg used when a user's submission is stored in grading).
15798: $laststore is $version=$timestamp, where $version is the most recent version
15799: number retrieved for the corresponding $symb in the $namespace db file, and
15800: $timestamp is the timestamp for that transaction (UNIX time).
15801: $laststore is currently only passed when cstore() is called by 
15802: structuretags::finalize_storage().
15803: 
15804: =item *
15805: 
15806: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
15807: but uses critical subroutine
15808: 
15809: =item *
15810: 
15811: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
15812: all args are optional
15813: 
15814: =item *
15815: 
15816: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
15817: dumps the complete (or key matching regexp) namespace into a hash
15818: ($udom, $uname, $regexp, $range are optional) for a namespace that is
15819: normally &store()ed into
15820: 
15821: $range should be either an integer '100' (give me the first 100
15822:                                            matching records)
15823:               or be  two integers sperated by a - with no spaces
15824:                  '30-50' (give me the 30th through the 50th matching
15825:                           records)
15826: 
15827: 
15828: =item *
15829: 
15830: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
15831: replaces a &store() version of data with a replacement set of data
15832: for a particular resource in a namespace passed in the $storehash hash 
15833: reference. If $tolog is true, the transaction is logged in the courselog
15834: with an action=PUTSTORE.
15835: 
15836: =item *
15837: 
15838: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
15839: works very similar to store/cstore, but all data is stored in a
15840: temporary location and can be reset using tmpreset, $storehash should
15841: be a hash reference, returns nothing on success
15842: 
15843: =item *
15844: 
15845: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
15846: similar to restore, but all data is stored in a temporary location and
15847: can be reset using tmpreset. Returns a hash of values on success,
15848: error string otherwise.
15849: 
15850: =item *
15851: 
15852: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
15853: deltes all keys for $symb form the temporary storage hash.
15854: 
15855: =item *
15856: 
15857: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
15858: reference filled in from namesp ($udom and $uname are optional)
15859: 
15860: =item *
15861: 
15862: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
15863: namesp ($udom and $uname are optional)
15864: 
15865: =item *
15866: 
15867: dump($namespace,$udom,$uname,$regexp,$range) : 
15868: dumps the complete (or key matching regexp) namespace into a hash
15869: ($udom, $uname, $regexp, $range are optional)
15870: 
15871: $range should be either an integer '100' (give me the first 100
15872:                                            matching records)
15873:               or be  two integers sperated by a - with no spaces
15874:                  '30-50' (give me the 30th through the 50th matching
15875:                           records)
15876: =item *
15877: 
15878: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
15879: $store can be a scalar, an array reference, or if the amount to be 
15880: incremented is > 1, a hash reference.
15881: 
15882: ($udom and $uname are optional)
15883: 
15884: =item *
15885: 
15886: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
15887: ($udom and $uname are optional)
15888: 
15889: =item *
15890: 
15891: cput($namespace,$storehash,$udom,$uname) : critical put
15892: ($udom and $uname are optional)
15893: 
15894: =item *
15895: 
15896: newput($namespace,$storehash,$udom,$uname) :
15897: 
15898: Attempts to store the items in the $storehash, but only if they don't
15899: currently exist, if this succeeds you can be certain that you have 
15900: successfully created a new key value pair in the $namespace db.
15901: 
15902: 
15903: Args:
15904:  $namespace: name of database to store values to
15905:  $storehash: hashref to store to the db
15906:  $udom: (optional) domain of user containing the db
15907:  $uname: (optional) name of user caontaining the db
15908: 
15909: Returns:
15910:  'ok' -> succeeded in storing all keys of $storehash
15911:  'key_exists: <key>' -> failed to anything out of $storehash, as at
15912:                         least <key> already existed in the db (other
15913:                         requested keys may also already exist)
15914:  'error: <msg>' -> unable to tie the DB or other error occurred
15915:  'con_lost' -> unable to contact request server
15916:  'refused' -> action was not allowed by remote machine
15917: 
15918: 
15919: =item *
15920: 
15921: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
15922: reference filled in from namesp (encrypts the return communication)
15923: ($udom and $uname are optional)
15924: 
15925: =item *
15926: 
15927: log($udom,$name,$home,$message) : write to permanent log for user; use
15928: critical subroutine
15929: 
15930: =item *
15931: 
15932: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
15933: array reference filled in from namespace found in domain level on either
15934: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
15935: 
15936: =item *
15937: 
15938: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
15939: domain level either on specified domain server ($uhome) or primary domain 
15940: server ($udom and $uhome are optional)
15941: 
15942: =item * 
15943: 
15944: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
15945: for: authentication, language, quotas, timezone, date locale, and portal URL in
15946: the target domain.
15947: 
15948: May also include additional key => value pairs for the following groups:
15949: 
15950: =over
15951: 
15952: =item
15953: disk quotas (MB allocated by default to portfolios and authoring spaces).
15954: 
15955: =over
15956: 
15957: =item defaultquota, authorquota
15958: 
15959: =back
15960: 
15961: =item
15962: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
15963: portfolio for users).
15964: 
15965: =over
15966: 
15967: =item
15968: aboutme, blog, webdav, portfolio
15969: 
15970: =back
15971: 
15972: =item
15973: requestcourses: ability to request courses, and how requests are processed.
15974: 
15975: =over
15976: 
15977: =item
15978: official, unofficial, community, textbook, placement
15979: 
15980: =back
15981: 
15982: =item
15983: inststatus: types of institutional affiliation, and order in which they are displayed.
15984: 
15985: =over
15986: 
15987: =item
15988: inststatustypes, inststatusorder, inststatusguest
15989: 
15990: =back
15991: 
15992: =item
15993: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
15994: for course's uploaded content.
15995: 
15996: =over
15997: 
15998: =item
15999: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16000: communityquota, textbookquota, placementquota
16001: 
16002: =back
16003: 
16004: =item
16005: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16006: on your servers.
16007: 
16008: =over
16009: 
16010: =item 
16011: remotesessions, hostedsessions
16012: 
16013: =back
16014: 
16015: =back
16016: 
16017: In cases where a domain coordinator has never used the "Set Domain Configuration"
16018: utility to create a configuration.db file on a domain's primary library server 
16019: only the following domain defaults: auth_def, auth_arg_def, lang_def
16020: -- corresponding values are authentication type (internal, krb4, krb5,
16021: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16022: will be available. Values are retrieved from cache (if current), unless the
16023: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16024: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16025: 
16026: Typical usage:
16027: 
16028: %domdefaults = &get_domain_defaults($target_domain);
16029: 
16030: =back
16031: 
16032: =head2 Network Status Functions
16033: 
16034: =over 4
16035: 
16036: =item *
16037: 
16038: dirlist() : return directory list based on URI (first arg).
16039: 
16040: Inputs: 1 required, 5 optional.
16041: 
16042: =over
16043: 
16044: =item 
16045: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16046: 
16047: =item
16048: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16049: 
16050: =item
16051: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16052: 
16053: =item
16054: $getpropath - boolean: 1 if prepend path using &propath(). 
16055: 
16056: =item
16057: $getuserdir - boolean: 1 if prepend path for "userfiles".
16058: 
16059: =item 
16060: $alternateRoot - path to prepend in place of path from $uri.
16061: 
16062: =back
16063: 
16064: Returns: Array of up to two items.
16065: 
16066: =over
16067: 
16068: a reference to an array of files/subdirectories
16069: 
16070: =over
16071: 
16072: Each element in the array of files/subdirectories is a & separated list of
16073: item name and the result of running stat on the item.  If dirlist was requested
16074: for a file instead of a directory, the item name will be ''. For a directory 
16075: listing, if the item is a metadata file, the element will end &N&M 
16076: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16077: default copyright set (1).  
16078: 
16079: =back
16080: 
16081: a scalar containing error condition (if encountered).
16082: 
16083: =over
16084: 
16085: =item 
16086: no_host (no homeserver identified for $username:$domain).
16087: 
16088: =item 
16089: no_such_host (server contacted for listing not identified as valid host).
16090: 
16091: =item 
16092: con_lost (connection to remote server failed).
16093: 
16094: =item 
16095: refused (invalid $username:$domain received on lond side).
16096: 
16097: =item 
16098: no_such_dir (directory at specified path on lond side does not exist). 
16099: 
16100: =item 
16101: empty (directory at specified path on lond side is empty).
16102: 
16103: =over
16104: 
16105: This is currently not encountered because the &ls3, &ls2, 
16106: &ls (_handler) routines on the lond side do not filter out
16107: . and .. from a directory listing. 
16108: 
16109: =back
16110: 
16111: =back
16112: 
16113: =back
16114: 
16115: =item *
16116: 
16117: spareserver() : find server with least workload from spare.tab
16118: 
16119: 
16120: =item *
16121: 
16122: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16123: if there is no corresponding loncapa host.
16124: 
16125: =back
16126: 
16127: 
16128: =head2 Apache Request
16129: 
16130: =over 4
16131: 
16132: =item *
16133: 
16134: ssi($url,%hash) : server side include, does a complete request cycle on url to
16135: localhost, posts hash
16136: 
16137: =back
16138: 
16139: =head2 Data to String to Data
16140: 
16141: =over 4
16142: 
16143: =item *
16144: 
16145: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16146: and '&' separators, supports elements that are arrayrefs and hashrefs
16147: 
16148: =item *
16149: 
16150: hashref2str($hashref) : convert a hashref into a string complete with
16151: escaping and '=' and '&' separators, supports elements that are
16152: arrayrefs and hashrefs
16153: 
16154: =item *
16155: 
16156: arrayref2str($arrayref) : convert an arrayref into a string complete
16157: with escaping and '&' separators, supports elements that are arrayrefs
16158: and hashrefs
16159: 
16160: =item *
16161: 
16162: str2hash($string) : convert string to hash using unescaping and
16163: splitting on '=' and '&', supports elements that are arrayrefs and
16164: hashrefs
16165: 
16166: =item *
16167: 
16168: str2array($string) : convert string to hash using unescaping and
16169: splitting on '&', supports elements that are arrayrefs and hashrefs
16170: 
16171: =back
16172: 
16173: =head2 Logging Routines
16174: 
16175: 
16176: These routines allow one to make log messages in the lonnet.log and
16177: lonnet.perm logfiles.
16178: 
16179: =over 4
16180: 
16181: =item *
16182: 
16183: logtouch() : make sure the logfile, lonnet.log, exists
16184: 
16185: =item *
16186: 
16187: logthis() : append message to the normal lonnet.log file, it gets
16188: preiodically rolled over and deleted.
16189: 
16190: =item *
16191: 
16192: logperm() : append a permanent message to lonnet.perm.log, this log
16193: file never gets deleted by any automated portion of the system, only
16194: messages of critical importance should go in here.
16195: 
16196: 
16197: =back
16198: 
16199: =head2 General File Helper Routines
16200: 
16201: =over 4
16202: 
16203: =item *
16204: 
16205: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16206: (a) files in /uploaded
16207:   (i) If a local copy of the file exists - 
16208:       compares modification date of local copy with last-modified date for 
16209:       definitive version stored on home server for course. If local copy is 
16210:       stale, requests a new version from the home server and stores it. 
16211:       If the original has been removed from the home server, then local copy 
16212:       is unlinked.
16213:   (ii) If local copy does not exist -
16214:       requests the file from the home server and stores it. 
16215:   
16216:   If $caller is 'uploadrep':  
16217:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16218:     for request for files originally uploaded via DOCS. 
16219:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16220:   
16221:   Otherwise:
16222:      This indicates a call from the content generation phase of the request.
16223:      -  returns the entire contents of the file or -1.
16224:      
16225: (b) files in /res
16226:    - returns the entire contents of a file or -1; 
16227:    it properly subscribes to and replicates the file if neccessary.
16228: 
16229: 
16230: =item *
16231: 
16232: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16233:                   reference
16234: 
16235: returns either a stat() list of data about the file or an empty list
16236: if the file doesn't exist or couldn't find out about it (connection
16237: problems or user unknown)
16238: 
16239: =item *
16240: 
16241: filelocation($dir,$file) : returns file system location of a file
16242: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16243: directory that relative $file lookups are to looked in ($dir of /a/dir
16244: and a file of ../bob will become /a/bob)
16245: 
16246: =item *
16247: 
16248: hreflocation($dir,$file) : returns file system location or a URL; same as
16249: filelocation except for hrefs
16250: 
16251: =item *
16252: 
16253: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16254: also removes beginning /home/httpd/html unless /priv/ follows it.
16255: 
16256: =back
16257: 
16258: =head2 Usererfile file routines (/uploaded*)
16259: 
16260: =over 4
16261: 
16262: =item *
16263: 
16264: userfileupload(): main rotine for putting a file in a user or course's
16265:                   filespace, arguments are,
16266: 
16267:  formname - required - this is the name of the element in $env where the
16268:            filename, and the contents of the file to create/modifed exist
16269:            the filename is in $env{'form.'.$formname.'.filename'} and the
16270:            contents of the file is located in $env{'form.'.$formname}
16271:  context - if coursedoc, store the file in the course of the active role
16272:              of the current user; 
16273:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16274:            if 'canceloverwrite': delete file in tmp/overwrites directory
16275:  subdir - required - subdirectory to put the file in under ../userfiles/
16276:          if undefined, it will be placed in "unknown"
16277: 
16278:  (This routine calls clean_filename() to remove any dangerous
16279:  characters from the filename, and then calls finuserfileupload() to
16280:  complete the transaction)
16281: 
16282:  returns either the url of the uploaded file (/uploaded/....) if successful
16283:  and /adm/notfound.html if unsuccessful
16284: 
16285: =item *
16286: 
16287: clean_filename(): routine for cleaing a filename up for storage in
16288:                  userfile space, argument is:
16289: 
16290:  filename - proposed filename
16291: 
16292: returns: the new clean filename
16293: 
16294: =item *
16295: 
16296: finishuserfileupload(): routine that creates and sends the file to
16297: userspace, probably shouldn't be called directly
16298: 
16299:   docuname: username or courseid of destination for the file
16300:   docudom: domain of user/course of destination for the file
16301:   formname: same as for userfileupload()
16302:   fname: filename (including subdirectories) for the file
16303:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16304:           if hashref, and context is scantron, will convert csv format to standard format
16305:   allfiles: reference to hash used to store objects found by parser
16306:   codebase: reference to hash used for codebases of java objects found by parser
16307:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16308:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16309:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16310:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16311:   context: if 'overwrite', will move the uploaded file from its temporary location to
16312:             userfiles to facilitate overwriting a previously uploaded file with same name.
16313:   mimetype: reference to scalar to accommodate mime type determined
16314:             from File::MMagic if $parser = parse.
16315: 
16316:  returns either the url of the uploaded file (/uploaded/....) if successful
16317:  and /adm/notfound.html if unsuccessful (or an error message if context 
16318:  was 'overwrite').
16319:  
16320: 
16321: =item *
16322: 
16323: renameuserfile(): renames an existing userfile to a new name
16324: 
16325:   Args:
16326:    docuname: username or courseid of destination for the file
16327:    docudom: domain of user/course of destination for the file
16328:    old: current file name (including any subdirs under userfiles)
16329:    new: desired file name (including any subdirs under userfiles)
16330: 
16331: =item *
16332: 
16333: mkdiruserfile(): creates a directory is a userfiles dir
16334: 
16335:   Args:
16336:    docuname: username or courseid of destination for the file
16337:    docudom: domain of user/course of destination for the file
16338:    dir: dir to create (including any subdirs under userfiles)
16339: 
16340: =item *
16341: 
16342: removeuserfile(): removes a file that exists in userfiles
16343: 
16344:   Args:
16345:    docuname: username or courseid of destination for the file
16346:    docudom: domain of user/course of destination for the file
16347:    fname: filname to delete (including any subdirs under userfiles)
16348: 
16349: =item *
16350: 
16351: removeuploadedurl(): convience function for removeuserfile()
16352: 
16353:   Args:
16354:    url:  a full /uploaded/... url to delete
16355: 
16356: =item * 
16357: 
16358: get_portfile_permissions():
16359:   Args:
16360:     domain: domain of user or course contain the portfolio files
16361:     user: name of user or num of course contain the portfolio files
16362:   Returns:
16363:     hashref of a dump of the proper file_permissions.db
16364:    
16365: 
16366: =item * 
16367: 
16368: get_access_controls():
16369: 
16370: Args:
16371:   current_permissions: the hash ref returned from get_portfile_permissions()
16372:   group: (optional) the group you want the files associated with
16373:   file: (optional) the file you want access info on
16374: 
16375: Returns:
16376:     a hash (keys are file names) of hashes containing
16377:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
16378:         values are XML containing access control settings (see below) 
16379: 
16380: Internal notes:
16381: 
16382:  access controls are stored in file_permissions.db as key=value pairs.
16383:     key -> path to file/file_name\0uniqueID:scope_end_start
16384:         where scope -> public,guest,course,group,domains or users.
16385:               end -> UNIX time for end of access (0 -> no end date)
16386:               start -> UNIX time for start of access
16387: 
16388:     value -> XML description of access control
16389:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
16390:             <start></start>
16391:             <end></end>
16392: 
16393:             <password></password>  for scope type = guest
16394: 
16395:             <domain></domain>     for scope type = course or group
16396:             <number></number>
16397:             <roles id="">
16398:              <role></role>
16399:              <access></access>
16400:              <section></section>
16401:              <group></group>
16402:             </roles>
16403: 
16404:             <dom></dom>         for scope type = domains
16405: 
16406:             <users>             for scope type = users
16407:              <user>
16408:               <uname></uname>
16409:               <udom></udom>
16410:              </user>
16411:             </users>
16412:            </scope> 
16413:               
16414:  Access data is also aggregated for each file in an additional key=value pair:
16415:  key -> path to file/file_name\0accesscontrol 
16416:  value -> reference to hash
16417:           hash contains key = value pairs
16418:           where key = uniqueID:scope_end_start
16419:                 value = UNIX time record was last updated
16420: 
16421:           Used to improve speed of look-ups of access controls for each file.  
16422:  
16423:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
16424: 
16425: =item *
16426: 
16427: modify_access_controls():
16428: 
16429: Modifies access controls for a portfolio file
16430: Args
16431: 1. file name
16432: 2. reference to hash of required changes,
16433: 3. domain
16434: 4. username
16435:   where domain,username are the domain of the portfolio owner 
16436:   (either a user or a course) 
16437: 
16438: Returns:
16439: 1. result of additions or updates ('ok' or 'error', with error message). 
16440: 2. result of deletions ('ok' or 'error', with error message).
16441: 3. reference to hash of any new or updated access controls.
16442: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
16443:    key = integer (inbound ID)
16444:    value = uniqueID
16445: 
16446: =item *
16447: 
16448: get_timebased_id():
16449: 
16450: Attempts to get a unique timestamp-based suffix for use with items added to a 
16451: course via the Course Editor (e.g., folders, composite pages, 
16452: group bulletin boards).
16453: 
16454: Args: (first three required; six others optional)
16455: 
16456: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
16457:    docssequence, or name of group
16458: 
16459: 2. keyid (alphanumeric): name of temporary locking key in hash,
16460:    e.g., num, boardids
16461: 
16462: 3. namespace: name of gdbm file used to store suffixes already assigned;  
16463:    file will be named nohist_namespace.db
16464: 
16465: 4. cdom: domain of course; default is current course domain from %env
16466: 
16467: 5. cnum: course number; default is current course number from %env
16468: 
16469: 6. idtype: set to concat if an additional digit is to be appended to the 
16470:    unix timestamp to form the suffix, if the plain timestamp is already
16471:    in use.  Default is to not do this, but simply increment the unix 
16472:    timestamp by 1 until a unique key is obtained.
16473: 
16474: 7. who: holder of locking key; defaults to user:domain for user.
16475: 
16476: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
16477:    retrying); default is 3.
16478: 
16479: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
16480: 
16481: Returns:
16482: 
16483: 1. suffix obtained (numeric)
16484: 
16485: 2. result of deleting locking key (ok if deleted, or lock never obtained)
16486: 
16487: 3. error: contains (localized) error message if an error occurred.
16488: 
16489: 
16490: =back
16491: 
16492: =head2 HTTP Helper Routines
16493: 
16494: =over 4
16495: 
16496: =item *
16497: 
16498: escape() : unpack non-word characters into CGI-compatible hex codes
16499: 
16500: =item *
16501: 
16502: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
16503: 
16504: =back
16505: 
16506: =head1 PRIVATE SUBROUTINES
16507: 
16508: =head2 Underlying communication routines (Shouldn't call)
16509: 
16510: =over 4
16511: 
16512: =item *
16513: 
16514: subreply() : tries to pass a message to lonc, returns con_lost if incapable
16515: 
16516: =item *
16517: 
16518: reply() : uses subreply to send a message to remote machine, logs all failures
16519: 
16520: =item *
16521: 
16522: critical() : passes a critical message to another server; if cannot
16523: get through then place message in connection buffer directory and
16524: returns con_delayed, if incapable of saving message, returns
16525: con_failed
16526: 
16527: =item *
16528: 
16529: reconlonc() : tries to reconnect lonc client processes.
16530: 
16531: =back
16532: 
16533: =head2 Resource Access Logging
16534: 
16535: =over 4
16536: 
16537: =item *
16538: 
16539: flushcourselogs() : flush (save) buffer logs and access logs
16540: 
16541: =item *
16542: 
16543: courselog($what) : save message for course in hash
16544: 
16545: =item *
16546: 
16547: courseacclog($what) : save message for course using &courselog().  Perform
16548: special processing for specific resource types (problems, exams, quizzes, etc).
16549: 
16550: =item *
16551: 
16552: goodbye() : flush course logs and log shutting down; it is called in srm.conf
16553: as a PerlChildExitHandler
16554: 
16555: =back
16556: 
16557: =head2 Other
16558: 
16559: =over 4
16560: 
16561: =item *
16562: 
16563: symblist($mapname,%newhash) : update symbolic storage links
16564: 
16565: =back
16566: 
16567: =cut
16568: 

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