File:  [LON-CAPA] / loncom / lonnet / perl / lonnet.pm
Revision 1.1424: download - view: text, annotated - select for diffs
Mon Sep 28 00:10:29 2020 UTC (3 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6518.
  More comprehensive access to symb for a requested URL to determine when
  access should be blocked and why, if content blocking in use.

    1: # The LearningOnline Network
    2: # TCP networking package
    3: #
    4: # $Id: lonnet.pm,v 1.1424 2020/09/28 00:10:29 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonnet.pm
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: This file is an interface to the lonc processes of
   39: the LON-CAPA network as well as set of elaborated functions for handling information
   40: necessary for navigating through a given cluster of LON-CAPA machines within a
   41: domain. There are over 40 specialized functions in this module which handle the
   42: reading and transmission of metadata, user information (ids, names, environments, roles,
   43: logs), file information (storage, reading, directories, extensions, replication, embedded
   44: styles and descriptors), educational resources (course descriptions, section names and
   45: numbers), url hashing (to assign roles on a url basis), and translating abbreviated symbols to
   46: and from more descriptive phrases or explanations.
   47: 
   48: This is part of the LearningOnline Network with CAPA project
   49: described at http://www.lon-capa.org.
   50: 
   51: =head1 Package Variables
   52: 
   53: These are largely undocumented, so if you decipher one please note it here.
   54: 
   55: =over 4
   56: 
   57: =item $processmarker
   58: 
   59: Contains the time this process was started and this servers host id.
   60: 
   61: =item $dumpcount
   62: 
   63: Counts the number of times a message log flush has been attempted (regardless
   64: of success) by this process.  Used as part of the filename when messages are
   65: delayed.
   66: 
   67: =back
   68: 
   69: =cut
   70: 
   71: package Apache::lonnet;
   72: 
   73: use strict;
   74: use HTTP::Date;
   75: use Image::Magick;
   76: use CGI::Cookie;
   77: 
   78: use Encode;
   79: 
   80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $deftex
   81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
   82:             %managerstab $passwdmin);
   83: 
   84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
   85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
   86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
   87:     %courseownerbuf, %coursetypebuf,$locknum);
   88: 
   89: use IO::Socket;
   90: use GDBM_File;
   91: use HTML::LCParser;
   92: use Fcntl qw(:flock);
   93: use Storable qw(thaw nfreeze);
   94: use Time::HiRes qw( sleep gettimeofday tv_interval );
   95: use Cache::Memcached;
   96: use Digest::MD5;
   97: use Math::Random;
   98: use File::MMagic;
   99: use 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: sub delusersession {
 1086:     my ($lonid,$udom,$uname) = @_;
 1087:     my $uprimary_id = &domain($udom,'primary');
 1088:     my $uintdom = &internet_dom($uprimary_id);
 1089:     my $intdom = &internet_dom($lonid);
 1090:     my $serverhomedom = &host_domain($lonid);
 1091:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1092:         return &reply(join(':','delusersession',
 1093:                             map {&escape($_)} ($udom,$uname)),$lonid);
 1094:     }
 1095:     return;
 1096: }
 1097: 
 1098: # check if user's browser sent load balancer cookie and server still has session
 1099: # and is not overloaded.
 1100: sub check_for_balancer_cookie {
 1101:     my ($r,$update_mtime) = @_;
 1102:     my ($otherserver,$cookie);
 1103:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
 1104:     if (exists($cookies{'balanceID'})) {
 1105:         my $balid = $cookies{'balanceID'};
 1106:         $cookie=&LONCAPA::clean_handle($balid->value);
 1107:         my $balancedir=$r->dir_config('lonBalanceDir');
 1108:         if ((-d $balancedir) && (-e "$balancedir/$cookie.id")) {
 1109:             if ($cookie =~ /^($match_domain)_($match_username)_[a-f0-9]+$/) {
 1110:                 my ($possudom,$possuname) = ($1,$2);
 1111:                 my $has_session = 0;
 1112:                 if ((&domain($possudom) ne '') &&
 1113:                     (&homeserver($possuname,$possudom) ne 'no_host')) {
 1114:                     my $try_server;
 1115:                     my $opened = open(my $idf,'+<',"$balancedir/$cookie.id");
 1116:                     if ($opened) {
 1117:                         flock($idf,LOCK_SH);
 1118:                         while (my $line = <$idf>) {
 1119:                             chomp($line);
 1120:                             if (&hostname($line) ne '') {
 1121:                                 $try_server = $line;
 1122:                                 last;
 1123:                             }
 1124:                         }
 1125:                         close($idf);
 1126:                         if (($try_server) &&
 1127:                             (&has_user_session($try_server,$possudom,$possuname))) {
 1128:                             my $lowest_load = 30000;
 1129:                             ($otherserver,$lowest_load) =
 1130:                                 &compare_server_load($try_server,undef,$lowest_load);
 1131:                             if ($otherserver ne '' && $lowest_load < 100) {
 1132:                                 $has_session = 1;
 1133:                             } else {
 1134:                                 undef($otherserver);
 1135:                             }
 1136:                         }
 1137:                     }
 1138:                 }
 1139:                 if ($has_session) {
 1140:                     if ($update_mtime) {
 1141:                         my $atime = my $mtime = time;
 1142:                         utime($atime,$mtime,"$balancedir/$cookie.id");
 1143:                     }
 1144:                 } else {
 1145:                     unlink("$balancedir/$cookie.id");
 1146:                 }
 1147:             }
 1148:         }
 1149:     }
 1150:     return ($otherserver,$cookie);
 1151: }
 1152: 
 1153: sub delbalcookie {
 1154:     my ($cookie,$balancer) =@_;
 1155:     if ($cookie =~ /^($match_domain)\_($match_username)\_[a-f0-9]{32}$/) {
 1156:         my ($udom,$uname) = ($1,$2);
 1157:         my $uprimary_id = &domain($udom,'primary');
 1158:         my $uintdom = &internet_dom($uprimary_id);
 1159:         my $intdom = &internet_dom($balancer);
 1160:         my $serverhomedom = &host_domain($balancer);
 1161:         if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1162:             return &reply("delbalcookie:$cookie",$balancer);
 1163:         }
 1164:     }
 1165: }
 1166: 
 1167: # -------------------------------- ask if server already has a session for user
 1168: sub has_user_session {
 1169:     my ($lonid,$udom,$uname) = @_;
 1170:     my $result = &reply(join(':','userhassession',
 1171: 			     map {&escape($_)} ($udom,$uname)),$lonid);
 1172:     return 1 if ($result eq 'ok');
 1173: 
 1174:     return 0;
 1175: }
 1176: 
 1177: # --------- determine least loaded server in a user's domain which allows login
 1178: 
 1179: sub choose_server {
 1180:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
 1181:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
 1182:     my %servers = &get_servers($udom);
 1183:     my $lowest_load = 30000;
 1184:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
 1185:     if ($skiploadbal) {
 1186:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
 1187:         unless (defined($cached)) {
 1188:             my $cachetime = 60*60*24;
 1189:             my %domconfig =
 1190:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1191:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1192:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
 1193:                                            $cachetime);
 1194:             }
 1195:         }
 1196:     }
 1197:     foreach my $lonhost (keys(%servers)) {
 1198:         if ($skiploadbal) {
 1199:             if (ref($balancers) eq 'HASH') {
 1200:                 next if (exists($balancers->{$lonhost}));
 1201:             }
 1202:         }
 1203:         my $loginvia;
 1204:         if ($checkloginvia) {
 1205:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
 1206:             if ($loginvia) {
 1207:                 my ($server,$path) = split(/:/,$loginvia);
 1208:                 ($login_host, $lowest_load) =
 1209:                     &compare_server_load($server, $login_host, $lowest_load, $required);
 1210:                 if ($login_host eq $server) {
 1211:                     $portal_path = $path;
 1212:                     $isredirect = 1;
 1213:                 }
 1214:             } else {
 1215:                 ($login_host, $lowest_load) =
 1216:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1217:                 if ($login_host eq $lonhost) {
 1218:                     $portal_path = '';
 1219:                     $isredirect = ''; 
 1220:                 }
 1221:             }
 1222:         } else {
 1223:             ($login_host, $lowest_load) =
 1224:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
 1225:         }
 1226:     }
 1227:     if ($login_host ne '') {
 1228:         $hostname = &hostname($login_host);
 1229:     }
 1230:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
 1231: }
 1232: 
 1233: sub get_course_sessions {
 1234:     my ($cnum,$cdom,$lastactivity) = @_;
 1235:     my %servers = &internet_dom_servers($cdom);
 1236:     my %returnhash;
 1237:     foreach my $server (sort(keys(%servers))) {
 1238:         my $rep = &reply("coursesessions:$cdom:$cnum:$lastactivity",$server);
 1239:         my @pairs=split(/\&/,$rep);
 1240:         unless (($rep eq 'unknown_cmd') || ($rep =~ /^error/)) {
 1241:             foreach my $item (@pairs) {
 1242:                 my ($key,$value)=split(/=/,$item,2);
 1243:                 $key = &unescape($key);
 1244:                 next if ($key =~ /^error: 2 /);
 1245:                 if (exists($returnhash{$key})) {
 1246:                     next if ($value < $returnhash{$key});
 1247:                 }
 1248:                 $returnhash{$key}=$value;
 1249:             }
 1250:         }
 1251:     }
 1252:     return %returnhash;
 1253: }
 1254: 
 1255: # --------------------------------------------- Try to change a user's password
 1256: 
 1257: sub changepass {
 1258:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
 1259:     $currentpass = &escape($currentpass);
 1260:     $newpass     = &escape($newpass);
 1261:     my $lonhost = $perlvar{'lonHostID'};
 1262:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
 1263: 		       $server);
 1264:     if (! $answer) {
 1265: 	&logthis("No reply on password change request to $server ".
 1266: 		 "by $uname in domain $udom.");
 1267:     } elsif ($answer =~ "^ok") {
 1268:         &logthis("$uname in $udom successfully changed their password ".
 1269: 		 "on $server.");
 1270:     } elsif ($answer =~ "^pwchange_failure") {
 1271: 	&logthis("$uname in $udom was unable to change their password ".
 1272: 		 "on $server.  The action was blocked by either lcpasswd ".
 1273: 		 "or pwchange");
 1274:     } elsif ($answer =~ "^non_authorized") {
 1275:         &logthis("$uname in $udom did not get their password correct when ".
 1276: 		 "attempting to change it on $server.");
 1277:     } elsif ($answer =~ "^auth_mode_error") {
 1278:         &logthis("$uname in $udom attempted to change their password despite ".
 1279: 		 "not being locally or internally authenticated on $server.");
 1280:     } elsif ($answer =~ "^unknown_user") {
 1281:         &logthis("$uname in $udom attempted to change their password ".
 1282: 		 "on $server but were unable to because $server is not ".
 1283: 		 "their home server.");
 1284:     } elsif ($answer =~ "^refused") {
 1285: 	&logthis("$server refused to change $uname in $udom password because ".
 1286: 		 "it was sent an unencrypted request to change the password.");
 1287:     } elsif ($answer =~ "invalid_client") {
 1288:         &logthis("$server refused to change $uname in $udom password because ".
 1289:                  "it was a reset by e-mail originating from an invalid server.");
 1290:     } elsif ($answer =~ "^prioruse") {
 1291:        &logthis("$server refused to change $uname in $udom password because ".
 1292:                 "the password had been used before");
 1293:     }
 1294:     return $answer;
 1295: }
 1296: 
 1297: # ----------------------- Try to determine user's current authentication scheme
 1298: 
 1299: sub queryauthenticate {
 1300:     my ($uname,$udom)=@_;
 1301:     my $uhome=&homeserver($uname,$udom);
 1302:     if (!$uhome) {
 1303: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
 1304: 	return 'no_host';
 1305:     }
 1306:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
 1307:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
 1308: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1309:     }
 1310:     return $answer;
 1311: }
 1312: 
 1313: # --------- Try to authenticate user from domain's lib servers (first this one)
 1314: 
 1315: sub authenticate {
 1316:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
 1317:     $upass=&escape($upass);
 1318:     $uname= &LONCAPA::clean_username($uname);
 1319:     my $uhome=&homeserver($uname,$udom,1);
 1320:     my $newhome;
 1321:     if ((!$uhome) || ($uhome eq 'no_host')) {
 1322: # Maybe the machine was offline and only re-appeared again recently?
 1323:         &reconlonc();
 1324: # One more
 1325: 	$uhome=&homeserver($uname,$udom,1);
 1326:         if (($uhome eq 'no_host') && $checkdefauth) {
 1327:             if (defined(&domain($udom,'primary'))) {
 1328:                 $newhome=&domain($udom,'primary');
 1329:             }
 1330:             if ($newhome ne '') {
 1331:                 $uhome = $newhome;
 1332:             }
 1333:         }
 1334: 	if ((!$uhome) || ($uhome eq 'no_host')) {
 1335: 	    &logthis("User $uname at $udom is unknown in authenticate");
 1336: 	    return 'no_host';
 1337:         }
 1338:     }
 1339:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
 1340:     if ($answer eq 'authorized') {
 1341:         if ($newhome) {
 1342:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
 1343:             return 'no_account_on_host'; 
 1344:         } else {
 1345:             &logthis("User $uname at $udom authorized by $uhome");
 1346:             return $uhome;
 1347:         }
 1348:     }
 1349:     if ($answer eq 'non_authorized') {
 1350: 	&logthis("User $uname at $udom rejected by $uhome");
 1351: 	return 'no_host'; 
 1352:     }
 1353:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
 1354:     return 'no_host';
 1355: }
 1356: 
 1357: sub can_host_session {
 1358:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
 1359:     my $canhost = 1;
 1360:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
 1361:     if (ref($remotesessions) eq 'HASH') {
 1362:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
 1363:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
 1364:                 $canhost = 0;
 1365:             } else {
 1366:                 $canhost = 1;
 1367:             }
 1368:         }
 1369:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
 1370:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
 1371:                 $canhost = 1;
 1372:             } else {
 1373:                 $canhost = 0;
 1374:             }
 1375:         }
 1376:         if ($canhost) {
 1377:             if ($remotesessions->{'version'} ne '') {
 1378:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
 1379:                 if ($reqmajor ne '' && $reqminor ne '') {
 1380:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
 1381:                         my $major = $1;
 1382:                         my $minor = $2;
 1383:                         if (($major < $reqmajor ) ||
 1384:                             (($major == $reqmajor) && ($minor < $reqminor))) {
 1385:                             $canhost = 0;
 1386:                         }
 1387:                     } else {
 1388:                         $canhost = 0;
 1389:                     }
 1390:                 }
 1391:             }
 1392:         }
 1393:     }
 1394:     if ($canhost) {
 1395:         if (ref($hostedsessions) eq 'HASH') {
 1396:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1397:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1398:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
 1399:                 if (($uint_dom ne '') && 
 1400:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
 1401:                     $canhost = 0;
 1402:                 } else {
 1403:                     $canhost = 1;
 1404:                 }
 1405:             }
 1406:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
 1407:                 if (($uint_dom ne '') && 
 1408:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
 1409:                     $canhost = 1;
 1410:                 } else {
 1411:                     $canhost = 0;
 1412:                 }
 1413:             }
 1414:         }
 1415:     }
 1416:     return $canhost;
 1417: }
 1418: 
 1419: sub spare_can_host {
 1420:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
 1421:     my $canhost=1;
 1422:     my $try_server_hostname = &hostname($try_server);
 1423:     my $serverhomeID = &get_server_homeID($try_server_hostname);
 1424:     my $serverhomedom = &host_domain($serverhomeID);
 1425:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
 1426:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
 1427:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
 1428:             $canhost = 0;
 1429:         }
 1430:     }
 1431:     if (($canhost) && ($uint_dom)) {
 1432:         my @intdoms;
 1433:         my $internet_names = &get_internet_names($try_server);
 1434:         if (ref($internet_names) eq 'ARRAY') {
 1435:             @intdoms = @{$internet_names};
 1436:         }
 1437:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1438:             my $remoterev = &get_server_loncaparev(undef,$try_server);
 1439:             $canhost = &can_host_session($udom,$try_server,$remoterev,
 1440:                                          $remotesessions,
 1441:                                          $defdomdefaults{'hostedsessions'});
 1442:         }
 1443:     }
 1444:     return $canhost;
 1445: }
 1446: 
 1447: sub this_host_spares {
 1448:     my ($dom) = @_;
 1449:     my ($dom_in_use,$lonhost_in_use,$result);
 1450:     my @hosts = &current_machine_ids();
 1451:     foreach my $lonhost (@hosts) {
 1452:         if (&host_domain($lonhost) eq $dom) {
 1453:             $dom_in_use = $dom;
 1454:             $lonhost_in_use = $lonhost;
 1455:             last;
 1456:         }
 1457:     }
 1458:     if ($dom_in_use ne '') {
 1459:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1460:     }
 1461:     if (ref($result) ne 'HASH') {
 1462:         $lonhost_in_use = $perlvar{'lonHostID'};
 1463:         $dom_in_use = &host_domain($lonhost_in_use);
 1464:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
 1465:         if (ref($result) ne 'HASH') {
 1466:             $result = \%spareid;
 1467:         }
 1468:     }
 1469:     return $result;
 1470: }
 1471: 
 1472: sub spares_for_offload  {
 1473:     my ($dom_in_use,$lonhost_in_use) = @_;
 1474:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
 1475:     if (defined($cached)) {
 1476:         return $result;
 1477:     } else {
 1478:         my $cachetime = 60*60*24;
 1479:         my %domconfig =
 1480:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
 1481:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
 1482:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
 1483:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
 1484:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
 1485:                 }
 1486:             }
 1487:         }
 1488:     }
 1489:     return;
 1490: }
 1491: 
 1492: sub get_lonbalancer_config {
 1493:     my ($servers) = @_;
 1494:     my ($currbalancer,$currtargets);
 1495:     if (ref($servers) eq 'HASH') {
 1496:         foreach my $server (keys(%{$servers})) {
 1497:             my %what = (
 1498:                          spareid => 1,
 1499:                          perlvar => 1,
 1500:                        );
 1501:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
 1502:             if ($result eq 'ok') {
 1503:                 if (ref($returnhash) eq 'HASH') {
 1504:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
 1505:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
 1506:                             $currbalancer = $server;
 1507:                             $currtargets = {};
 1508:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
 1509:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
 1510:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
 1511:                                 }
 1512:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
 1513:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
 1514:                                 }
 1515:                             }
 1516:                             last;
 1517:                         }
 1518:                     }
 1519:                 }
 1520:             }
 1521:         }
 1522:     }
 1523:     return ($currbalancer,$currtargets);
 1524: }
 1525: 
 1526: sub check_loadbalancing {
 1527:     my ($uname,$udom,$caller) = @_;
 1528:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
 1529:         $rule_in_effect,$offloadto,$otherserver,$setcookie,$dom_balancers);
 1530:     my $lonhost = $perlvar{'lonHostID'};
 1531:     my @hosts = &current_machine_ids();
 1532:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1533:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
 1534:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
 1535:     my $serverhomedom = &host_domain($lonhost);
 1536:     my $domneedscache;
 1537:     my $cachetime = 60*60*24;
 1538: 
 1539:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
 1540:         $dom_in_use = $udom;
 1541:         $homeintdom = 1;
 1542:     } else {
 1543:         $dom_in_use = $serverhomedom;
 1544:     }
 1545:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
 1546:     unless (defined($cached)) {
 1547:         my %domconfig =
 1548:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
 1549:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1550:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
 1551:         } else {
 1552:             $domneedscache = $dom_in_use;
 1553:         }
 1554:     }
 1555:     if (ref($result) eq 'HASH') {
 1556:         ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1557:             &check_balancer_result($result,@hosts);
 1558:         if ($is_balancer) {
 1559:             if (ref($currrules) eq 'HASH') {
 1560:                 if ($homeintdom) {
 1561:                     if ($uname ne '') {
 1562:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
 1563:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
 1564:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
 1565:                                 $rule_in_effect = $currrules->{'_LC_author'};
 1566:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
 1567:                                 $rule_in_effect = $currrules->{'_LC_adv'}
 1568:                             }
 1569:                         }
 1570:                         if ($rule_in_effect eq '') {
 1571:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
 1572:                             if ($userenv{'inststatus'} ne '') {
 1573:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
 1574:                                 my ($othertitle,$usertypes,$types) =
 1575:                                     &Apache::loncommon::sorted_inst_types($udom);
 1576:                                 if (ref($types) eq 'ARRAY') {
 1577:                                     foreach my $type (@{$types}) {
 1578:                                         if (grep(/^\Q$type\E$/,@statuses)) {
 1579:                                             if (exists($currrules->{$type})) {
 1580:                                                 $rule_in_effect = $currrules->{$type};
 1581:                                             }
 1582:                                         }
 1583:                                     }
 1584:                                 }
 1585:                             } else {
 1586:                                 if (exists($currrules->{'default'})) {
 1587:                                     $rule_in_effect = $currrules->{'default'};
 1588:                                 }
 1589:                             }
 1590:                         }
 1591:                     } else {
 1592:                         if (exists($currrules->{'default'})) {
 1593:                             $rule_in_effect = $currrules->{'default'};
 1594:                         }
 1595:                     }
 1596:                 } else {
 1597:                     if ($currrules->{'_LC_external'} ne '') {
 1598:                         $rule_in_effect = $currrules->{'_LC_external'};
 1599:                     }
 1600:                 }
 1601:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1602:                                                        $uname,$udom);
 1603:             }
 1604:         }
 1605:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
 1606:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
 1607:         unless (defined($cached)) {
 1608:             my %domconfig =
 1609:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
 1610:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1611:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
 1612:             } else {
 1613:                 $domneedscache = $serverhomedom;
 1614:             }
 1615:         }
 1616:         if (ref($result) eq 'HASH') {
 1617:             ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers) =
 1618:                 &check_balancer_result($result,@hosts);
 1619:             if ($is_balancer) {
 1620:                 if (ref($currrules) eq 'HASH') {
 1621:                     if ($currrules->{'_LC_internetdom'} ne '') {
 1622:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
 1623:                     }
 1624:                 }
 1625:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
 1626:                                                        $uname,$udom);
 1627:             }
 1628:         } else {
 1629:             if ($perlvar{'lonBalancer'} eq 'yes') {
 1630:                 $is_balancer = 1;
 1631:                 $offloadto = &this_host_spares($dom_in_use);
 1632:             }
 1633:             unless (defined($cached)) {
 1634:                 $domneedscache = $serverhomedom;
 1635:             }
 1636:         }
 1637:     } else {
 1638:         if ($perlvar{'lonBalancer'} eq 'yes') {
 1639:             $is_balancer = 1;
 1640:             $offloadto = &this_host_spares($dom_in_use);
 1641:         }
 1642:         unless (defined($cached)) {
 1643:             $domneedscache = $serverhomedom;
 1644:         }
 1645:     }
 1646:     if ($domneedscache) {
 1647:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
 1648:     }
 1649:     if ($is_balancer) {
 1650:         my $lowest_load = 30000;
 1651:         if (ref($offloadto) eq 'HASH') {
 1652:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
 1653:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
 1654:                     ($otherserver,$lowest_load) =
 1655:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1656:                 }
 1657:             }
 1658:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
 1659: 
 1660:             if (!$found_server) {
 1661:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
 1662:                     foreach my $try_server (@{$offloadto->{'default'}}) {
 1663:                         ($otherserver,$lowest_load) =
 1664:                             &compare_server_load($try_server,$otherserver,$lowest_load);
 1665:                     }
 1666:                 }
 1667:             }
 1668:         } elsif (ref($offloadto) eq 'ARRAY') {
 1669:             if (@{$offloadto} == 1) {
 1670:                 $otherserver = $offloadto->[0];
 1671:             } elsif (@{$offloadto} > 1) {
 1672:                 foreach my $try_server (@{$offloadto}) {
 1673:                     ($otherserver,$lowest_load) =
 1674:                         &compare_server_load($try_server,$otherserver,$lowest_load);
 1675:                 }
 1676:             }
 1677:         }
 1678:         unless ($caller eq 'login') {
 1679:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
 1680:                 $is_balancer = 0;
 1681:                 if ($uname ne '' && $udom ne '') {
 1682:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
 1683:                         &appenv({'user.loadbalexempt'     => $lonhost,
 1684:                                  'user.loadbalcheck.time' => time});
 1685:                     }
 1686:                 }
 1687:             }
 1688:         }
 1689:         unless ($homeintdom) {
 1690:             undef($setcookie);
 1691:         }
 1692:     }
 1693:     return ($is_balancer,$otherserver,$setcookie,$offloadto,$dom_balancers);
 1694: }
 1695: 
 1696: sub check_balancer_result {
 1697:     my ($result,@hosts) = @_;
 1698:     my ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1699:     if (ref($result) eq 'HASH') {
 1700:         if ($result->{'lonhost'} ne '') {
 1701:             my $currbalancer = $result->{'lonhost'};
 1702:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
 1703:                 $is_balancer = 1;
 1704:                 $currtargets = $result->{'targets'};
 1705:                 $currrules = $result->{'rules'};
 1706:             }
 1707:             $dom_balancers = $currbalancer;
 1708:         } else {
 1709:             if (keys(%{$result})) {
 1710:                 foreach my $key (keys(%{$result})) {
 1711:                     if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
 1712:                         (ref($result->{$key}) eq 'HASH')) {
 1713:                         $is_balancer = 1;
 1714:                         $currrules = $result->{$key}{'rules'};
 1715:                         $currtargets = $result->{$key}{'targets'};
 1716:                         $setcookie = $result->{$key}{'cookie'};
 1717:                         last;
 1718:                     }
 1719:                 }
 1720:                 $dom_balancers = join(',',sort(keys(%{$result})));
 1721:             }
 1722:         }
 1723:     }
 1724:     return ($is_balancer,$currtargets,$currrules,$setcookie,$dom_balancers);
 1725: }
 1726: 
 1727: sub get_loadbalancer_targets {
 1728:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
 1729:     my $offloadto;
 1730:     if ($rule_in_effect eq 'none') {
 1731:         return [$perlvar{'lonHostID'}];
 1732:     } elsif ($rule_in_effect eq '') {
 1733:         $offloadto = $currtargets;
 1734:     } else {
 1735:         if ($rule_in_effect eq 'homeserver') {
 1736:             my $homeserver = &homeserver($uname,$udom);
 1737:             if ($homeserver ne 'no_host') {
 1738:                 $offloadto = [$homeserver];
 1739:             }
 1740:         } elsif ($rule_in_effect eq 'externalbalancer') {
 1741:             my %domconfig =
 1742:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
 1743:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
 1744:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
 1745:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
 1746:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
 1747:                     }
 1748:                 }
 1749:             } else {
 1750:                 my %servers = &internet_dom_servers($udom);
 1751:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
 1752:                 if (&hostname($remotebalancer) ne '') {
 1753:                     $offloadto = [$remotebalancer];
 1754:                 }
 1755:             }
 1756:         } elsif (&hostname($rule_in_effect) ne '') {
 1757:             $offloadto = [$rule_in_effect];
 1758:         }
 1759:     }
 1760:     return $offloadto;
 1761: }
 1762: 
 1763: sub internet_dom_servers {
 1764:     my ($dom) = @_;
 1765:     my (%uniqservers,%servers);
 1766:     my $primaryserver = &hostname(&domain($dom,'primary'));
 1767:     my @machinedoms = &machine_domains($primaryserver);
 1768:     foreach my $mdom (@machinedoms) {
 1769:         my %currservers = %servers;
 1770:         my %server = &get_servers($mdom);
 1771:         %servers = (%currservers,%server);
 1772:     }
 1773:     my %by_hostname;
 1774:     foreach my $id (keys(%servers)) {
 1775:         push(@{$by_hostname{$servers{$id}}},$id);
 1776:     }
 1777:     foreach my $hostname (sort(keys(%by_hostname))) {
 1778:         if (@{$by_hostname{$hostname}} > 1) {
 1779:             my $match = 0;
 1780:             foreach my $id (@{$by_hostname{$hostname}}) {
 1781:                 if (&host_domain($id) eq $dom) {
 1782:                     $uniqservers{$id} = $hostname;
 1783:                     $match = 1;
 1784:                 }
 1785:             }
 1786:             unless ($match) {
 1787:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1788:             }
 1789:         } else {
 1790:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 1791:         }
 1792:     }
 1793:     return %uniqservers;
 1794: }
 1795: 
 1796: sub trusted_domains {
 1797:     my ($cmdtype,$calldom) = @_;
 1798:     my ($trusted,$untrusted);
 1799:     if (&domain($calldom) eq '') {
 1800:         return ($trusted,$untrusted);
 1801:     }
 1802:     unless ($cmdtype =~ /^(content|shared|enroll|coaurem|othcoau|domroles|catalog|reqcrs|msg)$/) {
 1803:         return ($trusted,$untrusted);
 1804:     }
 1805:     my $callprimary = &domain($calldom,'primary');
 1806:     my $intcalldom = &Apache::lonnet::internet_dom($callprimary);
 1807:     if ($intcalldom eq '') {
 1808:         return ($trusted,$untrusted);
 1809:     }
 1810: 
 1811:     my ($trustconfig,$cached)=&Apache::lonnet::is_cached_new('trust',$calldom);
 1812:     unless (defined($cached)) {
 1813:         my %domconfig = &Apache::lonnet::get_dom('configuration',['trust'],$calldom);
 1814:         &Apache::lonnet::do_cache_new('trust',$calldom,$domconfig{'trust'},3600);
 1815:         $trustconfig = $domconfig{'trust'};
 1816:     }
 1817:     if (ref($trustconfig)) {
 1818:         my (%possexc,%possinc,@allexc,@allinc); 
 1819:         if (ref($trustconfig->{$cmdtype}) eq 'HASH') {
 1820:             if (ref($trustconfig->{$cmdtype}->{'exc'}) eq 'ARRAY') {
 1821:                 map { $possexc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'exc'}}; 
 1822:             }
 1823:             if (ref($trustconfig->{$cmdtype}->{'inc'}) eq 'ARRAY') {
 1824:                 $possinc{$intcalldom} = 1;
 1825:                 map { $possinc{$_} = 1; } @{$trustconfig->{$cmdtype}->{'inc'}};
 1826:             }
 1827:         }
 1828:         if (keys(%possexc)) {
 1829:             if (keys(%possinc)) {
 1830:                 foreach my $key (sort(keys(%possexc))) {
 1831:                     next if ($key eq $intcalldom);
 1832:                     unless ($possinc{$key}) {
 1833:                         push(@allexc,$key);
 1834:                     }
 1835:                 }
 1836:             } else {
 1837:                 @allexc = sort(keys(%possexc));
 1838:             }
 1839:         }
 1840:         if (keys(%possinc)) {
 1841:             $possinc{$intcalldom} = 1;
 1842:             @allinc = sort(keys(%possinc));
 1843:         }
 1844:         if ((@allexc > 0) || (@allinc > 0)) {
 1845:             my %doms_by_intdom;
 1846:             my %allintdoms = &all_host_intdom();
 1847:             my %alldoms = &all_host_domain();
 1848:             foreach my $key (%allintdoms) {
 1849:                 if (ref($doms_by_intdom{$allintdoms{$key}}) eq 'ARRAY') {
 1850:                     unless (grep(/^\Q$alldoms{$key}\E$/,@{$doms_by_intdom{$allintdoms{$key}}})) {
 1851:                         push(@{$doms_by_intdom{$allintdoms{$key}}},$alldoms{$key});
 1852:                     }
 1853:                 } else {
 1854:                     $doms_by_intdom{$allintdoms{$key}} = [$alldoms{$key}]; 
 1855:                 }
 1856:             }
 1857:             foreach my $exc (@allexc) {
 1858:                 if (ref($doms_by_intdom{$exc}) eq 'ARRAY') {
 1859:                     push(@{$untrusted},@{$doms_by_intdom{$exc}});
 1860:                 }
 1861:             }
 1862:             foreach my $inc (@allinc) {
 1863:                 if (ref($doms_by_intdom{$inc}) eq 'ARRAY') {
 1864:                     push(@{$trusted},@{$doms_by_intdom{$inc}});
 1865:                 }
 1866:             }
 1867:         }
 1868:     }
 1869:     return ($trusted,$untrusted);
 1870: }
 1871: 
 1872: sub will_trust {
 1873:     my ($cmdtype,$domain,$possdom) = @_;
 1874:     return 1 if ($domain eq $possdom);
 1875:     my ($trustedref,$untrustedref) = &trusted_domains($cmdtype,$possdom);
 1876:     my $willtrust; 
 1877:     if ((ref($trustedref) eq 'ARRAY') && (@{$trustedref} > 0)) {
 1878:         if (grep(/^\Q$domain\E$/,@{$trustedref})) {
 1879:             $willtrust = 1;
 1880:         }
 1881:     } elsif ((ref($untrustedref) eq 'ARRAY') && (@{$untrustedref} > 0)) {
 1882:         unless (grep(/^\Q$domain\E$/,@{$untrustedref})) {
 1883:             $willtrust = 1;
 1884:         }
 1885:     } else {
 1886:         $willtrust = 1;
 1887:     }
 1888:     return $willtrust;
 1889: }
 1890: 
 1891: # ---------------------- Find the homebase for a user from domain's lib servers
 1892: 
 1893: my %homecache;
 1894: sub homeserver {
 1895:     my ($uname,$udom,$ignoreBadCache)=@_;
 1896:     my $index="$uname:$udom";
 1897: 
 1898:     if (exists($homecache{$index})) { return $homecache{$index}; }
 1899: 
 1900:     my %servers = &get_servers($udom,'library');
 1901:     foreach my $tryserver (keys(%servers)) {
 1902:         next if ($ignoreBadCache ne 'true' && 
 1903: 		 exists($badServerCache{$tryserver}));
 1904: 
 1905: 	my $answer=reply("home:$udom:$uname",$tryserver);
 1906: 	if ($answer eq 'found') {
 1907: 	    delete($badServerCache{$tryserver}); 
 1908: 	    return $homecache{$index}=$tryserver;
 1909: 	} elsif ($answer eq 'no_host') {
 1910: 	    $badServerCache{$tryserver}=1;
 1911: 	}
 1912:     }    
 1913:     return 'no_host';
 1914: }
 1915: 
 1916: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
 1917: 
 1918: sub idget {
 1919:     my ($udom,$idsref,$namespace)=@_;
 1920:     my %returnhash=();
 1921:     my @ids=(); 
 1922:     if (ref($idsref) eq 'ARRAY') {
 1923:         @ids = @{$idsref};
 1924:     } else {
 1925:         return %returnhash; 
 1926:     }
 1927:     if ($namespace eq '') {
 1928:         $namespace = 'ids';
 1929:     }
 1930:     
 1931:     my %servers = &get_servers($udom,'library');
 1932:     foreach my $tryserver (keys(%servers)) {
 1933: 	my $idlist=join('&', map { &escape($_); } @ids);
 1934: 	if ($namespace eq 'ids') {
 1935: 	    $idlist=~tr/A-Z/a-z/;
 1936: 	}
 1937: 	my $reply;
 1938: 	if ($namespace eq 'ids') {
 1939: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
 1940: 	} else {
 1941: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
 1942: 	}
 1943: 	my @answer=();
 1944: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
 1945: 	    @answer=split(/\&/,$reply);
 1946: 	}                    ;
 1947: 	my $i;
 1948: 	for ($i=0;$i<=$#ids;$i++) {
 1949: 	    if ($answer[$i]) {
 1950: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
 1951: 	    }
 1952: 	}
 1953:     }
 1954:     return %returnhash;
 1955: }
 1956: 
 1957: # ------------------------------------- Find the IDs behind a list of usernames
 1958: 
 1959: sub idrget {
 1960:     my ($udom,@unames)=@_;
 1961:     my %returnhash=();
 1962:     foreach my $uname (@unames) {
 1963:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
 1964:     }
 1965:     return %returnhash;
 1966: }
 1967: 
 1968: # Store away a list of names and associated student/employee IDs or clicker IDs
 1969: 
 1970: sub idput {
 1971:     my ($udom,$idsref,$uhom,$namespace)=@_;
 1972:     my %servers=();
 1973:     my %ids=();
 1974:     my %byid = ();
 1975:     if (ref($idsref) eq 'HASH') {
 1976:         %ids=%{$idsref};
 1977:     }
 1978:     if ($namespace eq '') {
 1979:         $namespace = 'ids'; 
 1980:     }
 1981:     foreach my $uname (keys(%ids)) {
 1982: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
 1983:         if ($uhom eq '') {
 1984:             $uhom=&homeserver($uname,$udom);
 1985:         }
 1986:         if ($uhom ne 'no_host') {
 1987:             my $esc_unam=&escape($uname);
 1988:             if ($namespace eq 'ids') {
 1989:                 my $id=&escape($ids{$uname});
 1990:                 $id=~tr/A-Z/a-z/;
 1991:                 my $esc_unam=&escape($uname);
 1992:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
 1993:             } else {
 1994:                 my @currids = split(/,/,$ids{$uname});
 1995:                 foreach my $id (@currids) {
 1996:                     $byid{$uhom}{$id} .= $uname.',';
 1997:                 }
 1998:             }
 1999:         }
 2000:     }
 2001:     if ($namespace eq 'clickers') {
 2002:         foreach my $server (keys(%byid)) {
 2003:             if (ref($byid{$server}) eq 'HASH') {
 2004:                 foreach my $id (keys(%{$byid{$server}})) {
 2005:                     $byid{$server} =~ s/,$//;
 2006:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
 2007:                 }
 2008:             }
 2009:         }
 2010:     }
 2011:     foreach my $server (keys(%servers)) {
 2012:         $servers{$server} =~ s/\&$//;
 2013:         if ($namespace eq 'ids') {     
 2014:             &critical('idput:'.$udom.':'.$servers{$server},$server);
 2015:         } else {
 2016:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
 2017:         }
 2018:     }
 2019: }
 2020: 
 2021: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
 2022: 
 2023: sub iddel {
 2024:     my ($udom,$idshashref,$uhome,$namespace)=@_;
 2025:     my %result=();
 2026:     my %ids=();
 2027:     my %byid = ();
 2028:     if (ref($idshashref) eq 'HASH') {
 2029:         %ids=%{$idshashref};
 2030:     } else {
 2031:         return %result;
 2032:     }
 2033:     if ($namespace eq '') {
 2034:         $namespace = 'ids';
 2035:     }
 2036:     my %servers=();
 2037:     while (my ($id,$unamestr) = each(%ids)) {
 2038:         if ($namespace eq 'ids') {
 2039:             my $uhom = $uhome;
 2040:             if ($uhom eq '') { 
 2041:                 $uhom=&homeserver($unamestr,$udom);
 2042:             }
 2043:             if ($uhom ne 'no_host') {
 2044:                 $servers{$uhom}.='&'.&escape($id);
 2045:             }
 2046:          } else {
 2047:             my @curritems = split(/,/,$ids{$id});
 2048:             foreach my $uname (@curritems) {
 2049:                 my $uhom = $uhome;
 2050:                 if ($uhom eq '') {
 2051:                     $uhom=&homeserver($uname,$udom);
 2052:                 }
 2053:                 if ($uhom ne 'no_host') { 
 2054:                     $byid{$uhom}{$id} .= $uname.',';
 2055:                 }
 2056:             }
 2057:         }
 2058:     }
 2059:     if ($namespace eq 'clickers') {
 2060:         foreach my $server (keys(%byid)) {
 2061:             if (ref($byid{$server}) eq 'HASH') {
 2062:                 foreach my $id (keys(%{$byid{$server}})) {
 2063:                     $byid{$server}{$id} =~ s/,$//;
 2064:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
 2065:                 }
 2066:             }
 2067:         }
 2068:     }
 2069:     foreach my $server (keys(%servers)) {
 2070:         $servers{$server} =~ s/\&$//;
 2071:         if ($namespace eq 'ids') {
 2072:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
 2073:         } elsif ($namespace eq 'clickers') {
 2074:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
 2075:         }
 2076:     }
 2077:     return %result;
 2078: }
 2079: 
 2080: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
 2081: 
 2082: sub updateclickers {
 2083:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
 2084:     my %clickers;
 2085:     if (ref($idshashref) eq 'HASH') {
 2086:         %clickers=%{$idshashref};
 2087:     } else {
 2088:         return;
 2089:     }
 2090:     my $items='';
 2091:     foreach my $item (keys(%clickers)) {
 2092:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
 2093:     }
 2094:     $items=~s/\&$//;
 2095:     my $request = "updateclickers:$udom:$action:$items";
 2096:     if ($critical) {
 2097:         return &critical($request,$uhome);
 2098:     } else {
 2099:         return &reply($request,$uhome);
 2100:     }
 2101: }
 2102: 
 2103: # ------------------------------dump from db file owned by domainconfig user
 2104: sub dump_dom {
 2105:     my ($namespace, $udom, $regexp) = @_;
 2106: 
 2107:     $udom ||= $env{'user.domain'};
 2108: 
 2109:     return () unless $udom;
 2110: 
 2111:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
 2112: }
 2113: 
 2114: # ------------------------------------------ get items from domain db files   
 2115: 
 2116: sub get_dom {
 2117:     my ($namespace,$storearr,$udom,$uhome)=@_;
 2118:     return if ($udom eq 'public');
 2119:     my $items='';
 2120:     foreach my $item (@$storearr) {
 2121:         $items.=&escape($item).'&';
 2122:     }
 2123:     $items=~s/\&$//;
 2124:     if (!$udom) {
 2125:         $udom=$env{'user.domain'};
 2126:         return if ($udom eq 'public');
 2127:         if (defined(&domain($udom,'primary'))) {
 2128:             $uhome=&domain($udom,'primary');
 2129:         } else {
 2130:             undef($uhome);
 2131:         }
 2132:     } else {
 2133:         if (!$uhome) {
 2134:             if (defined(&domain($udom,'primary'))) {
 2135:                 $uhome=&domain($udom,'primary');
 2136:             }
 2137:         }
 2138:     }
 2139:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2140:         my $rep;
 2141:         if ($namespace =~ /^enc/) {
 2142:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
 2143:         } else {
 2144:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
 2145:         }
 2146:         my %returnhash;
 2147:         if ($rep eq '' || $rep =~ /^error: 2 /) {
 2148:             return %returnhash;
 2149:         }
 2150:         my @pairs=split(/\&/,$rep);
 2151:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 2152:             return @pairs;
 2153:         }
 2154:         my $i=0;
 2155:         foreach my $item (@$storearr) {
 2156:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
 2157:             $i++;
 2158:         }
 2159:         return %returnhash;
 2160:     } else {
 2161:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
 2162:     }
 2163: }
 2164: 
 2165: # -------------------------------------------- put items in domain db files 
 2166: 
 2167: sub put_dom {
 2168:     my ($namespace,$storehash,$udom,$uhome)=@_;
 2169:     if (!$udom) {
 2170:         $udom=$env{'user.domain'};
 2171:         if (defined(&domain($udom,'primary'))) {
 2172:             $uhome=&domain($udom,'primary');
 2173:         } else {
 2174:             undef($uhome);
 2175:         }
 2176:     } else {
 2177:         if (!$uhome) {
 2178:             if (defined(&domain($udom,'primary'))) {
 2179:                 $uhome=&domain($udom,'primary');
 2180:             }
 2181:         }
 2182:     } 
 2183:     if ($udom && $uhome && ($uhome ne 'no_host')) {
 2184:         my $items='';
 2185:         foreach my $item (keys(%$storehash)) {
 2186:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 2187:         }
 2188:         $items=~s/\&$//;
 2189:         if ($namespace =~ /^enc/) {
 2190:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
 2191:         } else {
 2192:             return &reply("putdom:$udom:$namespace:$items",$uhome);
 2193:         }
 2194:     } else {
 2195:         &logthis("put_dom failed - no homeserver and/or domain");
 2196:     }
 2197: }
 2198: 
 2199: # --------------------- newput for items in db file owned by domainconfig user
 2200: sub newput_dom {
 2201:     my ($namespace,$storehash,$udom) = @_;
 2202:     my $result;
 2203:     if (!$udom) {
 2204:         $udom=$env{'user.domain'};
 2205:     }
 2206:     if ($udom) {
 2207:         my $uname = &get_domainconfiguser($udom);
 2208:         $result = &newput($namespace,$storehash,$udom,$uname);
 2209:     }
 2210:     return $result;
 2211: }
 2212: 
 2213: # --------------------- delete for items in db file owned by domainconfig user
 2214: sub del_dom {
 2215:     my ($namespace,$storearr,$udom)=@_;
 2216:     if (ref($storearr) eq 'ARRAY') {
 2217:         if (!$udom) {
 2218:             $udom=$env{'user.domain'};
 2219:         }
 2220:         if ($udom) {
 2221:             my $uname = &get_domainconfiguser($udom); 
 2222:             return &del($namespace,$storearr,$udom,$uname);
 2223:         }
 2224:     }
 2225: }
 2226: 
 2227: # ----------------------------------construct domainconfig user for a domain 
 2228: sub get_domainconfiguser {
 2229:     my ($udom) = @_;
 2230:     return $udom.'-domainconfig';
 2231: }
 2232: 
 2233: sub retrieve_inst_usertypes {
 2234:     my ($udom) = @_;
 2235:     my (%returnhash,@order);
 2236:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 2237:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
 2238:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
 2239:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
 2240:     } else {
 2241:         if (defined(&domain($udom,'primary'))) {
 2242:             my $uhome=&domain($udom,'primary');
 2243:             my $rep=&reply("inst_usertypes:$udom",$uhome);
 2244:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
 2245:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
 2246:                 return (\%returnhash,\@order);
 2247:             }
 2248:             my ($hashitems,$orderitems) = split(/:/,$rep); 
 2249:             my @pairs=split(/\&/,$hashitems);
 2250:             foreach my $item (@pairs) {
 2251:                 my ($key,$value)=split(/=/,$item,2);
 2252:                 $key = &unescape($key);
 2253:                 next if ($key =~ /^error: 2 /);
 2254:                 $returnhash{$key}=&thaw_unescape($value);
 2255:             }
 2256:             my @esc_order = split(/\&/,$orderitems);
 2257:             foreach my $item (@esc_order) {
 2258:                 push(@order,&unescape($item));
 2259:             }
 2260:         } else {
 2261:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
 2262:         }
 2263:         return (\%returnhash,\@order);
 2264:     }
 2265: }
 2266: 
 2267: sub is_domainimage {
 2268:     my ($url) = @_;
 2269:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
 2270:         if (&domain($1) ne '') {
 2271:             return '1';
 2272:         }
 2273:     }
 2274:     return;
 2275: }
 2276: 
 2277: sub inst_directory_query {
 2278:     my ($srch) = @_;
 2279:     my $udom = $srch->{'srchdomain'};
 2280:     my %results;
 2281:     my $homeserver = &domain($udom,'primary');
 2282:     my $outcome;
 2283:     if ($homeserver ne '') {
 2284:         unless ($homeserver eq $perlvar{'lonHostID'}) {
 2285:             if ($srch->{'srchby'} eq 'email') {
 2286:                 my $lcrev = &get_server_loncaparev($udom,$homeserver);
 2287:                 my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2288:                 if (($major eq '' && $minor eq '') || ($major < 2) ||
 2289:                     (($major == 2) && ($minor < 12))) {
 2290:                     return;
 2291:                 }
 2292:             }
 2293:         }
 2294: 	my $queryid=&reply("querysend:instdirsearch:".
 2295: 			   &escape($srch->{'srchby'}).':'.
 2296: 			   &escape($srch->{'srchterm'}).':'.
 2297: 			   &escape($srch->{'srchtype'}),$homeserver);
 2298: 	my $host=&hostname($homeserver);
 2299: 	if ($queryid !~/^\Q$host\E\_/) {
 2300: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
 2301: 	    return;
 2302: 	}
 2303: 	my $response = &get_query_reply($queryid);
 2304: 	my $maxtries = 5;
 2305: 	my $tries = 1;
 2306: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2307: 	    $response = &get_query_reply($queryid);
 2308: 	    $tries ++;
 2309: 	}
 2310: 
 2311:         if (!&error($response) && $response ne 'refused') {
 2312:             if ($response eq 'unavailable') {
 2313:                 $outcome = $response;
 2314:             } else {
 2315:                 $outcome = 'ok';
 2316:                 my @matches = split(/\n/,$response);
 2317:                 foreach my $match (@matches) {
 2318:                     my ($key,$value) = split(/=/,$match);
 2319:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
 2320:                 }
 2321:             }
 2322:         }
 2323:     }
 2324:     return ($outcome,%results);
 2325: }
 2326: 
 2327: sub usersearch {
 2328:     my ($srch) = @_;
 2329:     my $dom = $srch->{'srchdomain'};
 2330:     my %results;
 2331:     my %libserv = &all_library();
 2332:     my $query = 'usersearch';
 2333:     foreach my $tryserver (keys(%libserv)) {
 2334:         if (&host_domain($tryserver) eq $dom) {
 2335:             unless ($tryserver eq $perlvar{'lonHostID'}) {
 2336:                 if ($srch->{'srchby'} eq 'email') {
 2337:                     my $lcrev = &get_server_loncaparev($dom,$tryserver);
 2338:                     my ($major,$minor) = ($lcrev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
 2339:                     next if (($major eq '' && $minor eq '') || ($major < 2) ||
 2340:                              (($major == 2) && ($minor < 12)));
 2341:                 }
 2342:             }
 2343:             my $host=&hostname($tryserver);
 2344:             my $queryid=
 2345:                 &reply("querysend:".&escape($query).':'.
 2346:                        &escape($srch->{'srchby'}).':'.
 2347:                        &escape($srch->{'srchtype'}).':'.
 2348:                        &escape($srch->{'srchterm'}),$tryserver);
 2349:             if ($queryid !~/^\Q$host\E\_/) {
 2350:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
 2351:                 next;
 2352:             }
 2353:             my $reply = &get_query_reply($queryid);
 2354:             my $maxtries = 1;
 2355:             my $tries = 1;
 2356:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 2357:                 $reply = &get_query_reply($queryid);
 2358:                 $tries ++;
 2359:             }
 2360:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 2361:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
 2362:             } else {
 2363:                 my @matches;
 2364:                 if ($reply =~ /\n/) {
 2365:                     @matches = split(/\n/,$reply);
 2366:                 } else {
 2367:                     @matches = split(/\&/,$reply);
 2368:                 }
 2369:                 foreach my $match (@matches) {
 2370:                     my ($uname,$udom,%userhash);
 2371:                     foreach my $entry (split(/:/,$match)) {
 2372:                         my ($key,$value) =
 2373:                             map {&unescape($_);} split(/=/,$entry);
 2374:                         $userhash{$key} = $value;
 2375:                         if ($key eq 'username') {
 2376:                             $uname = $value;
 2377:                         } elsif ($key eq 'domain') {
 2378:                             $udom = $value;
 2379:                         }
 2380:                     }
 2381:                     $results{$uname.':'.$udom} = \%userhash;
 2382:                 }
 2383:             }
 2384:         }
 2385:     }
 2386:     return %results;
 2387: }
 2388: 
 2389: sub get_instuser {
 2390:     my ($udom,$uname,$id) = @_;
 2391:     my $homeserver = &domain($udom,'primary');
 2392:     my ($outcome,%results);
 2393:     if ($homeserver ne '') {
 2394:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
 2395:                            &escape($id).':'.&escape($udom),$homeserver);
 2396:         my $host=&hostname($homeserver);
 2397:         if ($queryid !~/^\Q$host\E\_/) {
 2398:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
 2399:             return;
 2400:         }
 2401:         my $response = &get_query_reply($queryid);
 2402:         my $maxtries = 5;
 2403:         my $tries = 1;
 2404:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
 2405:             $response = &get_query_reply($queryid);
 2406:             $tries ++;
 2407:         }
 2408:         if (!&error($response) && $response ne 'refused') {
 2409:             if ($response eq 'unavailable') {
 2410:                 $outcome = $response;
 2411:             } else {
 2412:                 $outcome = 'ok';
 2413:                 my @matches = split(/\n/,$response);
 2414:                 foreach my $match (@matches) {
 2415:                     my ($key,$value) = split(/=/,$match);
 2416:                     $results{&unescape($key)} = &thaw_unescape($value);
 2417:                 }
 2418:             }
 2419:         }
 2420:     }
 2421:     my %userinfo;
 2422:     if (ref($results{$uname}) eq 'HASH') {
 2423:         %userinfo = %{$results{$uname}};
 2424:     } 
 2425:     return ($outcome,%userinfo);
 2426: }
 2427: 
 2428: sub get_multiple_instusers {
 2429:     my ($udom,$users,$caller) = @_;
 2430:     my ($outcome,$results);
 2431:     if (ref($users) eq 'HASH') {
 2432:         my $count = keys(%{$users}); 
 2433:         my $requested = &freeze_escape($users);
 2434:         my $homeserver = &domain($udom,'primary');
 2435:         if ($homeserver ne '') {
 2436:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
 2437:             my $host=&hostname($homeserver);
 2438:             if ($queryid !~/^\Q$host\E\_/) {
 2439:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
 2440:                          ' for host: '.$homeserver.'in domain '.$udom);
 2441:                 return ($outcome,$results);
 2442:             }
 2443:             my $response = &get_query_reply($queryid);
 2444:             my $maxtries = 5;
 2445:             if ($count > 100) {
 2446:                 $maxtries = 1+int($count/20);
 2447:             }
 2448:             my $tries = 1;
 2449:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
 2450:                 $response = &get_query_reply($queryid);
 2451:                 $tries ++;
 2452:             }
 2453:             if ($response eq '') {
 2454:                 $results = {};
 2455:                 foreach my $key (keys(%{$users})) {
 2456:                     my ($uname,$id);
 2457:                     if ($caller eq 'id') {
 2458:                         $id = $key;
 2459:                     } else {
 2460:                         $uname = $key;
 2461:                     }
 2462:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
 2463:                     $outcome = $resp;
 2464:                     if ($resp eq 'ok') {
 2465:                         %{$results} = (%{$results}, %info);
 2466:                     } else {
 2467:                         last;
 2468:                     }
 2469:                 }
 2470:             } elsif(!&error($response) && ($response ne 'refused')) {
 2471:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
 2472:                     $outcome = $response;
 2473:                 } else {
 2474:                     ($outcome,my $userdata) = split(/=/,$response,2);
 2475:                     if ($outcome eq 'ok') {
 2476:                         $results = &thaw_unescape($userdata); 
 2477:                     }
 2478:                 }
 2479:             }
 2480:         }
 2481:     }
 2482:     return ($outcome,$results);
 2483: }
 2484: 
 2485: sub inst_rulecheck {
 2486:     my ($udom,$uname,$id,$item,$rules) = @_;
 2487:     my %returnhash;
 2488:     if ($udom ne '') {
 2489:         if (ref($rules) eq 'ARRAY') {
 2490:             @{$rules} = map {&escape($_);} (@{$rules});
 2491:             my $rulestr = join(':',@{$rules});
 2492:             my $homeserver=&domain($udom,'primary');
 2493:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2494:                 my $response;
 2495:                 if ($item eq 'username') {                
 2496:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
 2497:                                               ':'.&escape($uname).':'.$rulestr,
 2498:                                               $homeserver));
 2499:                 } elsif ($item eq 'id') {
 2500:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
 2501:                                               ':'.&escape($id).':'.$rulestr,
 2502:                                               $homeserver));
 2503:                 } elsif ($item eq 'selfcreate') {
 2504:                     $response=&unescape(&reply('instselfcreatecheck:'.
 2505:                                                &escape($udom).':'.&escape($uname).
 2506:                                               ':'.$rulestr,$homeserver));
 2507:                 }
 2508:                 if ($response ne 'refused') {
 2509:                     my @pairs=split(/\&/,$response);
 2510:                     foreach my $item (@pairs) {
 2511:                         my ($key,$value)=split(/=/,$item,2);
 2512:                         $key = &unescape($key);
 2513:                         next if ($key =~ /^error: 2 /);
 2514:                         $returnhash{$key}=&thaw_unescape($value);
 2515:                     }
 2516:                 }
 2517:             }
 2518:         }
 2519:     }
 2520:     return %returnhash;
 2521: }
 2522: 
 2523: sub inst_userrules {
 2524:     my ($udom,$check) = @_;
 2525:     my (%ruleshash,@ruleorder);
 2526:     if ($udom ne '') {
 2527:         my $homeserver=&domain($udom,'primary');
 2528:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
 2529:             my $response;
 2530:             if ($check eq 'id') {
 2531:                 $response=&reply('instidrules:'.&escape($udom),
 2532:                                  $homeserver);
 2533:             } elsif ($check eq 'email') {
 2534:                 $response=&reply('instemailrules:'.&escape($udom),
 2535:                                  $homeserver);
 2536:             } else {
 2537:                 $response=&reply('instuserrules:'.&escape($udom),
 2538:                                  $homeserver);
 2539:             }
 2540:             if (($response ne 'refused') && ($response ne 'error') && 
 2541:                 ($response ne 'unknown_cmd') && 
 2542:                 ($response ne 'no_such_host')) {
 2543:                 my ($hashitems,$orderitems) = split(/:/,$response);
 2544:                 my @pairs=split(/\&/,$hashitems);
 2545:                 foreach my $item (@pairs) {
 2546:                     my ($key,$value)=split(/=/,$item,2);
 2547:                     $key = &unescape($key);
 2548:                     next if ($key =~ /^error: 2 /);
 2549:                     $ruleshash{$key}=&thaw_unescape($value);
 2550:                 }
 2551:                 my @esc_order = split(/\&/,$orderitems);
 2552:                 foreach my $item (@esc_order) {
 2553:                     push(@ruleorder,&unescape($item));
 2554:                 }
 2555:             }
 2556:         }
 2557:     }
 2558:     return (\%ruleshash,\@ruleorder);
 2559: }
 2560: 
 2561: # ------------- Get Authentication, Language and User Tools Defaults for Domain
 2562: 
 2563: sub get_domain_defaults {
 2564:     my ($domain,$ignore_cache) = @_;
 2565:     return if (($domain eq '') || ($domain eq 'public'));
 2566:     my $cachetime = 60*60*24;
 2567:     unless ($ignore_cache) {
 2568:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
 2569:         if (defined($cached)) {
 2570:             if (ref($result) eq 'HASH') {
 2571:                 return %{$result};
 2572:             }
 2573:         }
 2574:     }
 2575:     my %domdefaults;
 2576:     my %domconfig =
 2577:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
 2578:                                   'requestcourses','inststatus',
 2579:                                   'coursedefaults','usersessions',
 2580:                                   'requestauthor','selfenrollment',
 2581:                                   'coursecategories','ssl','autoenroll',
 2582:                                   'trust','helpsettings'],$domain);
 2583:     my @coursetypes = ('official','unofficial','community','textbook','placement');
 2584:     if (ref($domconfig{'defaults'}) eq 'HASH') {
 2585:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
 2586:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
 2587:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
 2588:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
 2589:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
 2590:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
 2591:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
 2592:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
 2593:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
 2594:     } else {
 2595:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
 2596:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
 2597:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
 2598:     }
 2599:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 2600:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 2601:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
 2602:         } else {
 2603:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
 2604:         }
 2605:         my @usertools = ('aboutme','blog','webdav','portfolio');
 2606:         foreach my $item (@usertools) {
 2607:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
 2608:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
 2609:             }
 2610:         }
 2611:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 2612:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
 2613:         }
 2614:     }
 2615:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 2616:         foreach my $item ('official','unofficial','community','textbook','placement') {
 2617:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
 2618:         }
 2619:     }
 2620:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
 2621:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
 2622:     }
 2623:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
 2624:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
 2625:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
 2626:         }
 2627:     }
 2628:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 2629:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
 2630:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
 2631:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
 2632:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
 2633:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
 2634:         }
 2635:         foreach my $type (@coursetypes) {
 2636:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
 2637:                 unless ($type eq 'community') {
 2638:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
 2639:                 }
 2640:             }
 2641:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
 2642:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
 2643:             }
 2644:             if ($domdefaults{'postsubmit'} eq 'on') {
 2645:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
 2646:                     $domdefaults{$type.'postsubtimeout'} = 
 2647:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
 2648:                 }
 2649:             }
 2650:         }
 2651:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
 2652:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
 2653:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
 2654:                 if (@clonecodes) {
 2655:                     $domdefaults{'canclone'} = join('+',@clonecodes);
 2656:                 }
 2657:             }
 2658:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
 2659:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
 2660:         }
 2661:         if ($domconfig{'coursedefaults'}{'texengine'}) {
 2662:             $domdefaults{'texengine'} = $domconfig{'coursedefaults'}{'texengine'};
 2663:         } 
 2664:     }
 2665:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
 2666:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
 2667:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
 2668:         }
 2669:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
 2670:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
 2671:         }
 2672:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
 2673:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
 2674:         }
 2675:     }
 2676:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
 2677:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
 2678:             my @settings = ('types','registered','enroll_dates','access_dates','section',
 2679:                             'approval','limit');
 2680:             foreach my $type (@coursetypes) {
 2681:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
 2682:                     my @mgrdc = ();
 2683:                     foreach my $item (@settings) {
 2684:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
 2685:                             push(@mgrdc,$item);
 2686:                         }
 2687:                     }
 2688:                     if (@mgrdc) {
 2689:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
 2690:                     }
 2691:                 }
 2692:             }
 2693:         }
 2694:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
 2695:             foreach my $type (@coursetypes) {
 2696:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
 2697:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
 2698:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
 2699:                     }
 2700:                 }
 2701:             }
 2702:         }
 2703:     }
 2704:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2705:         $domdefaults{'catauth'} = 'std';
 2706:         $domdefaults{'catunauth'} = 'std';
 2707:         if ($domconfig{'coursecategories'}{'auth'}) {
 2708:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
 2709:         }
 2710:         if ($domconfig{'coursecategories'}{'unauth'}) {
 2711:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
 2712:         }
 2713:     }
 2714:     if (ref($domconfig{'ssl'}) eq 'HASH') {
 2715:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
 2716:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
 2717:         }
 2718:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
 2719:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
 2720:         }
 2721:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
 2722:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
 2723:         }
 2724:     }
 2725:     if (ref($domconfig{'trust'}) eq 'HASH') {
 2726:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
 2727:         foreach my $prefix (@prefixes) {
 2728:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
 2729:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
 2730:             }
 2731:         }
 2732:     }
 2733:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 2734:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
 2735:     }
 2736:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
 2737:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
 2738:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
 2739:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
 2740:         }
 2741:     }
 2742:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
 2743:     return %domdefaults;
 2744: }
 2745: 
 2746: sub get_dom_cats {
 2747:     my ($dom) = @_;
 2748:     return unless (&domain($dom));
 2749:     my ($cats,$cached)=&is_cached_new('cats',$dom);
 2750:     unless (defined($cached)) {
 2751:         my %domconfig = &get_dom('configuration',['coursecategories'],$dom);
 2752:         if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 2753:             if (ref($domconfig{'coursecategories'}{'cats'}) eq 'HASH') {
 2754:                 %{$cats} = %{$domconfig{'coursecategories'}{'cats'}};
 2755:             } else {
 2756:                 $cats = {};
 2757:             }
 2758:         } else {
 2759:             $cats = {};
 2760:         }
 2761:         &Apache::lonnet::do_cache_new('cats',$dom,$cats,3600);
 2762:     }
 2763:     return $cats;
 2764: }
 2765: 
 2766: sub get_dom_instcats {
 2767:     my ($dom) = @_;
 2768:     return unless (&domain($dom));
 2769:     my ($instcats,$cached)=&is_cached_new('instcats',$dom);
 2770:     unless (defined($cached)) {
 2771:         my (%coursecodes,%codes,@codetitles,%cat_titles,%cat_order);
 2772:         my $totcodes = &retrieve_instcodes(\%coursecodes,$dom);
 2773:         if ($totcodes > 0) {
 2774:             my $caller = 'global';
 2775:             if (&auto_instcode_format($caller,$dom,\%coursecodes,\%codes,
 2776:                                       \@codetitles,\%cat_titles,\%cat_order) eq 'ok') {
 2777:                 $instcats = {
 2778:                                 codes => \%codes,
 2779:                                 codetitles => \@codetitles,
 2780:                                 cat_titles => \%cat_titles,
 2781:                                 cat_order => \%cat_order,
 2782:                             };
 2783:                 &do_cache_new('instcats',$dom,$instcats,3600);
 2784:             }
 2785:         }
 2786:     }
 2787:     return $instcats;
 2788: }
 2789: 
 2790: sub retrieve_instcodes {
 2791:     my ($coursecodes,$dom) = @_;
 2792:     my $totcodes;
 2793:     my %courses = &courseiddump($dom,'.',1,'.','.','.',undef,undef,'Course');
 2794:     foreach my $course (keys(%courses)) {
 2795:         if (ref($courses{$course}) eq 'HASH') {
 2796:             if ($courses{$course}{'inst_code'} ne '') {
 2797:                 $$coursecodes{$course} = $courses{$course}{'inst_code'};
 2798:                 $totcodes ++;
 2799:             }
 2800:         }
 2801:     }
 2802:     return $totcodes;
 2803: }
 2804: 
 2805: sub course_portal_url {
 2806:     my ($cnum,$cdom) = @_;
 2807:     my $chome = &homeserver($cnum,$cdom);
 2808:     my $hostname = &hostname($chome);
 2809:     my $protocol = $protocol{$chome};
 2810:     $protocol = 'http' if ($protocol ne 'https');
 2811:     my %domdefaults = &get_domain_defaults($cdom);
 2812:     my $firsturl;
 2813:     if ($domdefaults{'portal_def'}) {
 2814:         $firsturl = $domdefaults{'portal_def'};
 2815:     } else {
 2816:         $firsturl = $protocol.'://'.$hostname;
 2817:     }
 2818:     return $firsturl;
 2819: }
 2820: 
 2821: # --------------------------------------------- Get domain config for passwords
 2822: 
 2823: sub get_passwdconf {
 2824:     my ($dom) = @_;
 2825:     my (%passwdconf,$gotconf,$lookup);
 2826:     my ($result,$cached)=&is_cached_new('passwdconf',$dom);
 2827:     if (defined($cached)) {
 2828:         if (ref($result) eq 'HASH') {
 2829:             %passwdconf = %{$result};
 2830:             $gotconf = 1;
 2831:         }
 2832:     }
 2833:     unless ($gotconf) {
 2834:         my %domconfig = &get_dom('configuration',['passwords'],$dom);
 2835:         if (ref($domconfig{'passwords'}) eq 'HASH') {
 2836:             %passwdconf = %{$domconfig{'passwords'}};
 2837:         }
 2838:         my $cachetime = 24*60*60;
 2839:         &do_cache_new('passwdconf',$dom,\%passwdconf,$cachetime);
 2840:     }
 2841:     return %passwdconf;
 2842: }
 2843: 
 2844: # --------------------------------------------------- Assign a key to a student
 2845: 
 2846: sub assign_access_key {
 2847: #
 2848: # a valid key looks like uname:udom#comments
 2849: # comments are being appended
 2850: #
 2851:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
 2852:     $kdom=
 2853:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
 2854:     $knum=
 2855:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
 2856:     $cdom=
 2857:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2858:     $cnum=
 2859:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2860:     $udom=$env{'user.name'} unless (defined($udom));
 2861:     $uname=$env{'user.domain'} unless (defined($uname));
 2862:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
 2863:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
 2864:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
 2865:                                                   # assigned to this person
 2866:                                                   # - this should not happen,
 2867:                                                   # unless something went wrong
 2868:                                                   # the first time around
 2869: # ready to assign
 2870:         $logentry=$1.'; '.$logentry;
 2871:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
 2872:                                                  $kdom,$knum) eq 'ok') {
 2873: # key now belongs to user
 2874: 	    my $envkey='key.'.$cdom.'_'.$cnum;
 2875:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
 2876:                 &appenv({'environment.'.$envkey => $ckey});
 2877:                 return 'ok';
 2878:             } else {
 2879:                 return 
 2880:   'error: Count not permanently assign key, will need to be re-entered later.';
 2881: 	    }
 2882:         } else {
 2883:             return 'error: Could not assign key, try again later.';
 2884:         }
 2885:     } elsif (!$existing{$ckey}) {
 2886: # the key does not exist
 2887: 	return 'error: The key does not exist';
 2888:     } else {
 2889: # the key is somebody else's
 2890: 	return 'error: The key is already in use';
 2891:     }
 2892: }
 2893: 
 2894: # ------------------------------------------ put an additional comment on a key
 2895: 
 2896: sub comment_access_key {
 2897: #
 2898: # a valid key looks like uname:udom#comments
 2899: # comments are being appended
 2900: #
 2901:     my ($ckey,$cdom,$cnum,$logentry)=@_;
 2902:     $cdom=
 2903:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2904:     $cnum=
 2905:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2906:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2907:     if ($existing{$ckey}) {
 2908:         $existing{$ckey}.='; '.$logentry;
 2909: # ready to assign
 2910:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
 2911:                                                  $cdom,$cnum) eq 'ok') {
 2912: 	    return 'ok';
 2913:         } else {
 2914: 	    return 'error: Count not store comment.';
 2915:         }
 2916:     } else {
 2917: # the key does not exist
 2918: 	return 'error: The key does not exist';
 2919:     }
 2920: }
 2921: 
 2922: # ------------------------------------------------------ Generate a set of keys
 2923: 
 2924: sub generate_access_keys {
 2925:     my ($number,$cdom,$cnum,$logentry)=@_;
 2926:     $cdom=
 2927:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2928:     $cnum=
 2929:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2930:     unless (&allowed('mky',$cdom)) { return 0; }
 2931:     unless (($cdom) && ($cnum)) { return 0; }
 2932:     if ($number>10000) { return 0; }
 2933:     sleep(2); # make sure don't get same seed twice
 2934:     srand(time()^($$+($$<<15))); # from "Programming Perl"
 2935:     my $total=0;
 2936:     for (my $i=1;$i<=$number;$i++) {
 2937:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
 2938:                   sprintf("%lx",int(100000*rand)).'-'.
 2939:                   sprintf("%lx",int(100000*rand));
 2940:        $newkey=~s/1/g/g; # folks mix up 1 and l
 2941:        $newkey=~s/0/h/g; # and also 0 and O
 2942:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
 2943:        if ($existing{$newkey}) {
 2944:            $i--;
 2945:        } else {
 2946: 	  if (&put('accesskeys',
 2947:               { $newkey => '# generated '.localtime().
 2948:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
 2949:                            '; '.$logentry },
 2950: 		   $cdom,$cnum) eq 'ok') {
 2951:               $total++;
 2952: 	  }
 2953:        }
 2954:     }
 2955:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
 2956:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
 2957:     return $total;
 2958: }
 2959: 
 2960: # ------------------------------------------------------- Validate an accesskey
 2961: 
 2962: sub validate_access_key {
 2963:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
 2964:     $cdom=
 2965:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
 2966:     $cnum=
 2967:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
 2968:     $udom=$env{'user.domain'} unless (defined($udom));
 2969:     $uname=$env{'user.name'} unless (defined($uname));
 2970:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
 2971:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
 2972: }
 2973: 
 2974: # ------------------------------------- Find the section of student in a course
 2975: sub devalidate_getsection_cache {
 2976:     my ($udom,$unam,$courseid)=@_;
 2977:     my $hashid="$udom:$unam:$courseid";
 2978:     &devalidate_cache_new('getsection',$hashid);
 2979: }
 2980: 
 2981: sub courseid_to_courseurl {
 2982:     my ($courseid) = @_;
 2983:     #already url style courseid
 2984:     return $courseid if ($courseid =~ m{^/});
 2985: 
 2986:     if (exists($env{'course.'.$courseid.'.num'})) {
 2987: 	my $cnum = $env{'course.'.$courseid.'.num'};
 2988: 	my $cdom = $env{'course.'.$courseid.'.domain'};
 2989: 	return "/$cdom/$cnum";
 2990:     }
 2991: 
 2992:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
 2993:     if (exists($courseinfo{'num'})) {
 2994: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
 2995:     }
 2996: 
 2997:     return undef;
 2998: }
 2999: 
 3000: sub getsection {
 3001:     my ($udom,$unam,$courseid)=@_;
 3002:     my $cachetime=1800;
 3003: 
 3004:     my $hashid="$udom:$unam:$courseid";
 3005:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
 3006:     if (defined($cached)) { return $result; }
 3007: 
 3008:     my %Pending; 
 3009:     my %Expired;
 3010:     #
 3011:     # Each role can either have not started yet (pending), be active, 
 3012:     #    or have expired.
 3013:     #
 3014:     # If there is an active role, we are done.
 3015:     #
 3016:     # If there is more than one role which has not started yet, 
 3017:     #     choose the one which will start sooner
 3018:     # If there is one role which has not started yet, return it.
 3019:     #
 3020:     # If there is more than one expired role, choose the one which ended last.
 3021:     # If there is a role which has expired, return it.
 3022:     #
 3023:     $courseid = &courseid_to_courseurl($courseid);
 3024:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
 3025:     foreach my $key (keys(%roleshash)) {
 3026:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
 3027:         my $section=$1;
 3028:         if ($key eq $courseid.'_st') { $section=''; }
 3029:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
 3030:         my $now=time;
 3031:         if (defined($end) && $end && ($now > $end)) {
 3032:             $Expired{$end}=$section;
 3033:             next;
 3034:         }
 3035:         if (defined($start) && $start && ($now < $start)) {
 3036:             $Pending{$start}=$section;
 3037:             next;
 3038:         }
 3039:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
 3040:     }
 3041:     #
 3042:     # Presumedly there will be few matching roles from the above
 3043:     # loop and the sorting time will be negligible.
 3044:     if (scalar(keys(%Pending))) {
 3045:         my ($time) = sort {$a <=> $b} keys(%Pending);
 3046:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
 3047:     } 
 3048:     if (scalar(keys(%Expired))) {
 3049:         my @sorted = sort {$a <=> $b} keys(%Expired);
 3050:         my $time = pop(@sorted);
 3051:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
 3052:     }
 3053:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
 3054: }
 3055: 
 3056: sub save_cache {
 3057:     &purge_remembered();
 3058:     #&Apache::loncommon::validate_page();
 3059:     undef(%env);
 3060:     undef($env_loaded);
 3061: }
 3062: 
 3063: my $to_remember=-1;
 3064: my %remembered;
 3065: my %accessed;
 3066: my $kicks=0;
 3067: my $hits=0;
 3068: sub make_key {
 3069:     my ($name,$id) = @_;
 3070:     if (length($id) > 65 
 3071: 	&& length(&escape($id)) > 200) {
 3072: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
 3073:     }
 3074:     return &escape($name.':'.$id);
 3075: }
 3076: 
 3077: sub devalidate_cache_new {
 3078:     my ($name,$id,$debug) = @_;
 3079:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
 3080:     my $remembered_id=$name.':'.$id;
 3081:     $id=&make_key($name,$id);
 3082:     $memcache->delete($id);
 3083:     delete($remembered{$remembered_id});
 3084:     delete($accessed{$remembered_id});
 3085: }
 3086: 
 3087: sub is_cached_new {
 3088:     my ($name,$id,$debug) = @_;
 3089:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
 3090:     if (exists($remembered{$remembered_id})) {
 3091: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
 3092: 	$accessed{$remembered_id}=[&gettimeofday()];
 3093: 	$hits++;
 3094: 	return ($remembered{$remembered_id},1);
 3095:     }
 3096:     $id=&make_key($name,$id);
 3097:     my $value = $memcache->get($id);
 3098:     if (!(defined($value))) {
 3099: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
 3100: 	return (undef,undef);
 3101:     }
 3102:     if ($value eq '__undef__') {
 3103: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
 3104: 	$value=undef;
 3105:     }
 3106:     &make_room($remembered_id,$value,$debug);
 3107:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
 3108:     return ($value,1);
 3109: }
 3110: 
 3111: sub do_cache_new {
 3112:     my ($name,$id,$value,$time,$debug) = @_;
 3113:     my $remembered_id=$name.':'.$id;
 3114:     $id=&make_key($name,$id);
 3115:     my $setvalue=$value;
 3116:     if (!defined($setvalue)) {
 3117: 	$setvalue='__undef__';
 3118:     }
 3119:     if (!defined($time) ) {
 3120: 	$time=600;
 3121:     }
 3122:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
 3123:     my $result = $memcache->set($id,$setvalue,$time);
 3124:     if (! $result) {
 3125: 	&logthis("caching of id -> $id  failed");
 3126: 	$memcache->disconnect_all();
 3127:     }
 3128:     # need to make a copy of $value
 3129:     &make_room($remembered_id,$value,$debug);
 3130:     return $value;
 3131: }
 3132: 
 3133: sub make_room {
 3134:     my ($remembered_id,$value,$debug)=@_;
 3135: 
 3136:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
 3137:                                     : $value;
 3138:     if ($to_remember<0) { return; }
 3139:     $accessed{$remembered_id}=[&gettimeofday()];
 3140:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
 3141:     my $to_kick;
 3142:     my $max_time=0;
 3143:     foreach my $other (keys(%accessed)) {
 3144: 	if (&tv_interval($accessed{$other}) > $max_time) {
 3145: 	    $to_kick=$other;
 3146: 	    $max_time=&tv_interval($accessed{$other});
 3147: 	}
 3148:     }
 3149:     delete($remembered{$to_kick});
 3150:     delete($accessed{$to_kick});
 3151:     $kicks++;
 3152:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
 3153:     return;
 3154: }
 3155: 
 3156: sub purge_remembered {
 3157:     #&logthis("Tossing ".scalar(keys(%remembered)));
 3158:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
 3159:     undef(%remembered);
 3160:     undef(%accessed);
 3161: }
 3162: # ------------------------------------- Read an entry from a user's environment
 3163: 
 3164: sub userenvironment {
 3165:     my ($udom,$unam,@what)=@_;
 3166:     my $items;
 3167:     foreach my $item (@what) {
 3168:         $items.=&escape($item).'&';
 3169:     }
 3170:     $items=~s/\&$//;
 3171:     my %returnhash=();
 3172:     my $uhome = &homeserver($unam,$udom);
 3173:     unless ($uhome eq 'no_host') {
 3174:         my @answer=split(/\&/, 
 3175:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
 3176:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
 3177:             return %returnhash;
 3178:         }
 3179:         my $i;
 3180:         for ($i=0;$i<=$#what;$i++) {
 3181: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
 3182:         }
 3183:     }
 3184:     return %returnhash;
 3185: }
 3186: 
 3187: # ---------------------------------------------------------- Get a studentphoto
 3188: sub studentphoto {
 3189:     my ($udom,$unam,$ext) = @_;
 3190:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3191:     if (defined($env{'request.course.id'})) {
 3192:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
 3193:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
 3194:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
 3195:             } else {
 3196:                 my ($result,$perm_reqd)=
 3197: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3198:                 if ($result eq 'ok') {
 3199:                     if (!($perm_reqd eq 'yes')) {
 3200:                         return(&retrievestudentphoto($udom,$unam,$ext));
 3201:                     }
 3202:                 }
 3203:             }
 3204:         }
 3205:     } else {
 3206:         my ($result,$perm_reqd) = 
 3207: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
 3208:         if ($result eq 'ok') {
 3209:             if (!($perm_reqd eq 'yes')) {
 3210:                 return(&retrievestudentphoto($udom,$unam,$ext));
 3211:             }
 3212:         }
 3213:     }
 3214:     return '/adm/lonKaputt/lonlogo_broken.gif';
 3215: }
 3216: 
 3217: sub retrievestudentphoto {
 3218:     my ($udom,$unam,$ext,$type) = @_;
 3219:     my $home=&Apache::lonnet::homeserver($unam,$udom);
 3220:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
 3221:     if ($ret eq 'ok') {
 3222:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
 3223:         if ($type eq 'thumbnail') {
 3224:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
 3225:         }
 3226:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
 3227:         return $tokenurl;
 3228:     } else {
 3229:         if ($type eq 'thumbnail') {
 3230:             return '/adm/lonKaputt/genericstudent_tn.gif';
 3231:         } else { 
 3232:             return '/adm/lonKaputt/lonlogo_broken.gif';
 3233:         }
 3234:     }
 3235: }
 3236: 
 3237: # -------------------------------------------------------------------- New chat
 3238: 
 3239: sub chatsend {
 3240:     my ($newentry,$anon,$group)=@_;
 3241:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
 3242:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 3243:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
 3244:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
 3245: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
 3246: 		   &escape($newentry)).':'.$group,$chome);
 3247: }
 3248: 
 3249: # ------------------------------------------ Find current version of a resource
 3250: 
 3251: sub getversion {
 3252:     my $fname=&clutter(shift);
 3253:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
 3254:     return &currentversion(&filelocation('',$fname));
 3255: }
 3256: 
 3257: sub currentversion {
 3258:     my $fname=shift;
 3259:     my $author=$fname;
 3260:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3261:     my ($udom,$uname)=split(/\//,$author);
 3262:     my $home=&homeserver($uname,$udom);
 3263:     if ($home eq 'no_host') { 
 3264:         return -1; 
 3265:     }
 3266:     my $answer=&reply("currentversion:$fname",$home);
 3267:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3268: 	return -1;
 3269:     }
 3270:     return $answer;
 3271: }
 3272: 
 3273: #
 3274: # Return special version number of resource if set by override, empty otherwise
 3275: #
 3276: sub usedversion {
 3277:     my $fname=shift;
 3278:     unless ($fname) { $fname=$env{'request.uri'}; }
 3279:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
 3280:     if ($urlversion) { return $urlversion; }
 3281:     return '';
 3282: }
 3283: 
 3284: # ----------------------------- Subscribe to a resource, return URL if possible
 3285: 
 3286: sub subscribe {
 3287:     my $fname=shift;
 3288:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
 3289:     $fname=~s/[\n\r]//g;
 3290:     my $author=$fname;
 3291:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3292:     my ($udom,$uname)=split(/\//,$author);
 3293:     my $home=homeserver($uname,$udom);
 3294:     if ($home eq 'no_host') {
 3295:         return 'not_found';
 3296:     }
 3297:     my $answer=reply("sub:$fname",$home);
 3298:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
 3299: 	$answer.=' by '.$home;
 3300:     }
 3301:     return $answer;
 3302: }
 3303:     
 3304: # -------------------------------------------------------------- Replicate file
 3305: 
 3306: sub repcopy {
 3307:     my $filename=shift;
 3308:     $filename=~s/\/+/\//g;
 3309:     my $londocroot = $perlvar{'lonDocRoot'};
 3310:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
 3311:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
 3312:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
 3313: 	$filename=~m{^/*(uploaded|editupload)/}) {
 3314: 	return &repcopy_userfile($filename);
 3315:     }
 3316:     $filename=~s/[\n\r]//g;
 3317:     my $transname="$filename.in.transfer";
 3318: # FIXME: this should flock
 3319:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
 3320:     my $remoteurl=subscribe($filename);
 3321:     if ($remoteurl =~ /^con_lost by/) {
 3322: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3323:            return 'unavailable';
 3324:     } elsif ($remoteurl eq 'not_found') {
 3325: 	   #&logthis("Subscribe returned not_found: $filename");
 3326: 	   return 'not_found';
 3327:     } elsif ($remoteurl =~ /^rejected by/) {
 3328: 	   &logthis("Subscribe returned $remoteurl: $filename");
 3329:            return 'forbidden';
 3330:     } elsif ($remoteurl eq 'directory') {
 3331:            return 'ok';
 3332:     } else {
 3333:         my $author=$filename;
 3334:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3335:         my ($udom,$uname)=split(/\//,$author);
 3336:         my $home=homeserver($uname,$udom);
 3337:         unless ($home eq $perlvar{'lonHostID'}) {
 3338:            my @parts=split(/\//,$filename);
 3339:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3340:            if ($path ne "$londocroot/res") {
 3341:                &logthis("Malconfiguration for replication: $filename");
 3342: 	       return 'bad_request';
 3343:            }
 3344:            my $count;
 3345:            for ($count=5;$count<$#parts;$count++) {
 3346:                $path.="/$parts[$count]";
 3347:                if ((-e $path)!=1) {
 3348: 		   mkdir($path,0777);
 3349:                }
 3350:            }
 3351:            my $request=new HTTP::Request('GET',"$remoteurl");
 3352:            my $response;
 3353:            if ($remoteurl =~ m{/raw/}) {
 3354:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
 3355:            } else {
 3356:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
 3357:            }
 3358:            if ($response->is_error()) {
 3359: 	       unlink($transname);
 3360:                my $message=$response->status_line;
 3361:                &logthis("<font color=\"blue\">WARNING:"
 3362:                        ." LWP get: $message: $filename</font>");
 3363:                return 'unavailable';
 3364:            } else {
 3365: 	       if ($remoteurl!~/\.meta$/) {
 3366:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
 3367:                   my $mresponse;
 3368:                   if ($remoteurl =~ m{/raw/}) {
 3369:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
 3370:                   } else {
 3371:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
 3372:                   }
 3373:                   if ($mresponse->is_error()) {
 3374: 		      unlink($filename.'.meta');
 3375:                       &logthis(
 3376:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
 3377:                   }
 3378: 	       }
 3379:                rename($transname,$filename);
 3380:                return 'ok';
 3381:            }
 3382:        }
 3383:     }
 3384: }
 3385: 
 3386: # ------------------------------------------------- Unsubscribe from a resource
 3387: 
 3388: sub unsubscribe {
 3389:     my ($fname) = @_;
 3390:     my $answer;
 3391:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return $answer; }
 3392:     $fname=~s/[\n\r]//g;
 3393:     my $author=$fname;
 3394:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
 3395:     my ($udom,$uname)=split(/\//,$author);
 3396:     my $home=homeserver($uname,$udom);
 3397:     if ($home eq 'no_host') {
 3398:         $answer = 'no_host';
 3399:     } elsif (grep { $_ eq $home } &current_machine_ids()) {
 3400:         $answer = 'home';
 3401:     } else {
 3402:         my $defdom = $perlvar{'lonDefDomain'};
 3403:         if (&will_trust('content',$defdom,$udom)) {
 3404:             $answer = reply("unsub:$fname",$home);
 3405:         } else {
 3406:             $answer = 'untrusted';
 3407:         }
 3408:     }
 3409:     return $answer;
 3410: }
 3411: 
 3412: # ------------------------------------------------ Get server side include body
 3413: sub ssi_body {
 3414:     my ($filelink,%form)=@_;
 3415:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
 3416:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
 3417:     }
 3418:     my $output='';
 3419:     my $response;
 3420:     if ($filelink=~/^https?\:/) {
 3421:        ($output,$response)=&externalssi($filelink);
 3422:     } else {
 3423:        $filelink .= $filelink=~/\?/ ? '&' : '?';
 3424:        $filelink .= 'inhibitmenu=yes';
 3425:        ($output,$response)=&ssi($filelink,%form);
 3426:     }
 3427:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
 3428:     $output=~s/^.*?\<body[^\>]*\>//si;
 3429:     $output=~s/\<\/body\s*\>.*?$//si;
 3430:     if (wantarray) {
 3431:         return ($output, $response);
 3432:     } else {
 3433:         return $output;
 3434:     }
 3435: }
 3436: 
 3437: # --------------------------------------------------------- Server Side Include
 3438: 
 3439: sub absolute_url {
 3440:     my ($host_name) = @_;
 3441:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
 3442:     if ($host_name eq '') {
 3443: 	$host_name = $ENV{'SERVER_NAME'};
 3444:     }
 3445:     return $protocol.$host_name;
 3446: }
 3447: 
 3448: #
 3449: #   Server side include.
 3450: # Parameters:
 3451: #  fn     Possibly encrypted resource name/id.
 3452: #  form   Hash that describes how the rendering should be done
 3453: #         and other things.
 3454: # Returns:
 3455: #   Scalar context: The content of the response.
 3456: #   Array context:  2 element list of the content and the full response object.
 3457: #     
 3458: sub ssi {
 3459: 
 3460:     my ($fn,%form)=@_;
 3461:     my $request;
 3462: 
 3463:     $form{'no_update_last_known'}=1;
 3464:     &Apache::lonenc::check_encrypt(\$fn);
 3465:     if (%form) {
 3466:       $request=new HTTP::Request('POST',&absolute_url().$fn);
 3467:       $request->content(join('&',map { 
 3468:             my $name = escape($_);
 3469:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
 3470:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
 3471:             : &escape($form{$_}) );    
 3472:         } keys(%form)));
 3473:     } else {
 3474:       $request=new HTTP::Request('GET',&absolute_url().$fn);
 3475:     }
 3476: 
 3477:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
 3478:     my $lonhost = $perlvar{'lonHostID'};
 3479:     my $islocal;
 3480:     if (($env{'request.course.id'}) &&
 3481:         ($form{'grade_courseid'} eq $env{'request.course.id'}) &&
 3482:         ($form{'grade_username'} ne '') && ($form{'grade_domain'} ne '') &&
 3483:         ($form{'grade_symb'} ne '') &&
 3484:         (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
 3485:                                  ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:'')))) {
 3486:         $islocal = 1;
 3487:     }
 3488:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,
 3489:                                                 '','','',$islocal);
 3490: 
 3491:     if (wantarray) {
 3492: 	return ($response->content, $response);
 3493:     } else {
 3494: 	return $response->content;
 3495:     }
 3496: }
 3497: 
 3498: sub externalssi {
 3499:     my ($url)=@_;
 3500:     my $request=new HTTP::Request('GET',$url);
 3501:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
 3502:     if (wantarray) {
 3503:         return ($response->content, $response);
 3504:     } else {
 3505:         return $response->content;
 3506:     }
 3507: }
 3508: 
 3509: 
 3510: # If the local copy of a replicated resource is outdated, trigger a  
 3511: # connection from the homeserver to flush the delayed queue. If no update 
 3512: # happens, remove local copies of outdated resource (and corresponding
 3513: # metadata file).
 3514: 
 3515: sub remove_stale_resfile {
 3516:     my ($url) = @_;
 3517:     my $removed;
 3518:     if ($url=~m{^/res/($match_domain)/($match_username)/}) {
 3519:         my $audom = $1;
 3520:         my $auname = $2;
 3521:         unless (($url =~ /\.\d+\.\w+$/) || ($url =~ m{^/res/lib/templates/})) {
 3522:             my $homeserver = &homeserver($auname,$audom);
 3523:             unless (($homeserver eq 'no_host') ||
 3524:                     (grep { $_ eq $homeserver } &current_machine_ids())) {
 3525:                 my $fname = &filelocation('',$url);
 3526:                 if (-e $fname) {
 3527:                     my $hostname = &hostname($homeserver);
 3528:                     if ($hostname) {
 3529:                         my $protocol = $protocol{$homeserver};
 3530:                         $protocol = 'http' if ($protocol ne 'https');
 3531:                         my $uri = &declutter($url);
 3532:                         my $request=new HTTP::Request('HEAD',$protocol.'://'.$hostname.'/raw/'.$uri);
 3533:                         my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,5,0,1);
 3534:                         if ($response->is_success()) {
 3535:                             my $remmodtime = &HTTP::Date::str2time( $response->header('Last-modified') );
 3536:                             my $locmodtime = (stat($fname))[9];
 3537:                             if ($locmodtime < $remmodtime) {
 3538:                                 my $stale;
 3539:                                 my $answer = &reply('pong',$homeserver);
 3540:                                 if ($answer eq $homeserver.':'.$perlvar{'lonHostID'}) {
 3541:                                     sleep(0.2);
 3542:                                     $locmodtime = (stat($fname))[9];
 3543:                                     if ($locmodtime < $remmodtime) {
 3544:                                         my $posstransfer = $fname.'.in.transfer';
 3545:                                         if ((-e $posstransfer) && ($remmodtime < (stat($posstransfer))[9])) {
 3546:                                             $removed = 1;
 3547:                                         } else {
 3548:                                             $stale = 1;
 3549:                                         }
 3550:                                     } else {
 3551:                                         $removed = 1;
 3552:                                     }
 3553:                                 } else {
 3554:                                     $stale = 1;
 3555:                                 }
 3556:                                 if ($stale) {
 3557:                                     if (unlink($fname)) {
 3558:                                         if ($uri!~/\.meta$/) {
 3559:                                             if (-e $fname.'.meta') {
 3560:                                                 unlink($fname.'.meta');
 3561:                                             }
 3562:                                         }
 3563:                                         my $unsubresult = &unsubscribe($fname);
 3564:                                         unless ($unsubresult eq 'ok') {
 3565:                                             &logthis("no unsub of $fname from $homeserver, reason: $unsubresult");
 3566:                                         }
 3567:                                         $removed = 1;
 3568:                                     }
 3569:                                 }
 3570:                             }
 3571:                         }
 3572:                     }
 3573:                 }
 3574:             }
 3575:         }
 3576:     }
 3577:     return $removed;
 3578: }
 3579: 
 3580: # -------------------------------- Allow a /uploaded/ URI to be vouched for
 3581: 
 3582: sub allowuploaded {
 3583:     my ($srcurl,$url)=@_;
 3584:     $url=&clutter(&declutter($url));
 3585:     my $dir=$url;
 3586:     $dir=~s/\/[^\/]+$//;
 3587:     my %httpref=();
 3588:     my $httpurl=&hreflocation('',$url);
 3589:     $httpref{'httpref.'.$httpurl}=$srcurl;
 3590:     &Apache::lonnet::appenv(\%httpref);
 3591: }
 3592: 
 3593: #
 3594: # Determine if the current user should be able to edit a particular resource,
 3595: # when viewing in course context.
 3596: # (a) When viewing resource used to determine if "Edit" item is included in 
 3597: #     Functions.
 3598: # (b) When displaying folder contents in course editor, used to determine if
 3599: #     "Edit" link will be displayed alongside resource.
 3600: #
 3601: #  input: six args -- filename (decluttered), course number, course domain,
 3602: #                   url, symb (if registered) and group (if this is a group
 3603: #                   item -- e.g., bulletin board, group page etc.).
 3604: #  output: array of five scalars -- 
 3605: #          $cfile -- url for file editing if editable on current server
 3606: #          $home -- homeserver of resource (i.e., for author if published,
 3607: #                                           or course if uploaded.).
 3608: #          $switchserver --  1 if server switch will be needed.
 3609: #          $forceedit -- 1 if icon/link should be to go to edit mode 
 3610: #          $forceview -- 1 if icon/link should be to go to view mode
 3611: #
 3612: 
 3613: sub can_edit_resource {
 3614:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
 3615:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
 3616: #
 3617: # For aboutme pages user can only edit his/her own.
 3618: #
 3619:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
 3620:         my ($sdom,$sname) = ($1,$2);
 3621:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
 3622:             $home = $env{'user.home'};
 3623:             $cfile = $resurl;
 3624:             if ($env{'form.forceedit'}) {
 3625:                 $forceview = 1;
 3626:             } else {
 3627:                 $forceedit = 1;
 3628:             }
 3629:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3630:         } else {
 3631:             return;
 3632:         }
 3633:     }
 3634: 
 3635:     if ($env{'request.course.id'}) {
 3636:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
 3637:         if ($group ne '') {
 3638: # if this is a group homepage or group bulletin board, check group privs
 3639:             my $allowed = 0;
 3640:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
 3641:                 if ((&allowed('mdg',$env{'request.course.id'}.
 3642:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3643:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3644:                     $allowed = 1;
 3645:                 }
 3646:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
 3647:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
 3648:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
 3649:                     $allowed = 1;
 3650:                 }
 3651:             }
 3652:             if ($allowed) {
 3653:                 $home=&homeserver($cnum,$cdom);
 3654:                 if ($env{'form.forceedit'}) {
 3655:                     $forceview = 1;
 3656:                 } else {
 3657:                     $forceedit = 1;
 3658:                 }
 3659:                 $cfile = $resurl;
 3660:             } else {
 3661:                 return;
 3662:             }
 3663:         } else {
 3664:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
 3665:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
 3666:                     return;
 3667:                 }
 3668:             } elsif (!$crsedit) {
 3669: #
 3670: # No edit allowed where CC has switched to student role.
 3671: #
 3672:                 return;
 3673:             }
 3674:         }
 3675:     }
 3676: 
 3677:     if ($file ne '') {
 3678:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
 3679:             if (&is_course_upload($file,$cnum,$cdom)) {
 3680:                 $uploaded = 1;
 3681:                 $incourse = 1;
 3682:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
 3683:                     $cfile = &hreflocation('',$file);
 3684:                     if ($env{'form.forceedit'}) {
 3685:                         $forceview = 1;
 3686:                     } else {
 3687:                         $forceedit = 1;
 3688:                     }
 3689:                 }
 3690:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
 3691:                 $incourse = 1;
 3692:                 if ($env{'form.forceedit'}) {
 3693:                     $forceview = 1;
 3694:                 } else {
 3695:                     $forceedit = 1;
 3696:                 }
 3697:                 $cfile = $resurl;
 3698:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
 3699:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
 3700:                     $incourse = 1;
 3701:                     if ($env{'form.forceedit'}) {
 3702:                         $forceview = 1;
 3703:                     } else {
 3704:                         $forceedit = 1;
 3705:                     }
 3706:                     $cfile = $resurl;
 3707:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
 3708:                     $incourse = 1;
 3709:                     $cfile = $resurl.'/smpedit';
 3710:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
 3711:                     $incourse = 1;
 3712:                     if ($env{'form.forceedit'}) {
 3713:                         $forceview = 1;
 3714:                     } else {
 3715:                         $forceedit = 1;
 3716:                     }
 3717:                     $cfile = $resurl;
 3718:                 } elsif (($resurl =~ m{^/ext/}) && ($symb ne '')) {
 3719:                     my ($map,$id,$res) = &decode_symb($symb);
 3720:                     if ($map =~ /\.page$/) {
 3721:                         $incourse = 1;
 3722:                         if ($env{'form.forceedit'}) {
 3723:                             $forceview = 1;
 3724:                             $cfile = $map;
 3725:                         } else {
 3726:                             $forceedit = 1;
 3727:                             $cfile =  '/adm/wrapper'.$resurl;
 3728:                         }
 3729:                     }
 3730:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3731:                     $incourse = 1;
 3732:                     if ($env{'form.forceedit'}) {
 3733:                         $forceview = 1;
 3734:                     } else {
 3735:                         $forceedit = 1;
 3736:                     }
 3737:                     $cfile = $resurl;
 3738:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3739:                     $incourse = 1;
 3740:                     if ($env{'form.forceedit'}) {
 3741:                         $forceview = 1;
 3742:                     } else {
 3743:                         $forceedit = 1;
 3744:                     }
 3745:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3746:                 }
 3747:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 3748:                 my $template = '/res/lib/templates/simpleproblem.problem';
 3749:                 if (&is_on_map($template)) { 
 3750:                     $incourse = 1;
 3751:                     $forceview = 1;
 3752:                     $cfile = $template;
 3753:                 }
 3754:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3755:                 $incourse = 1;
 3756:                 if ($env{'form.forceedit'}) {
 3757:                     $forceview = 1;
 3758:                 } else {
 3759:                     $forceedit = 1;
 3760:                 }
 3761:                 $cfile = $resurl;
 3762:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
 3763:                 $incourse = 1;
 3764:                 if ($env{'form.forceedit'}) {
 3765:                     $forceview = 1;
 3766:                 } else {
 3767:                     $forceedit = 1;
 3768:                 }
 3769:                 $cfile = $resurl;
 3770:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
 3771:                 $incourse = 1;
 3772:                 $forceview = 1;
 3773:                 if ($symb) {
 3774:                     my ($map,$id,$res)=&decode_symb($symb);
 3775:                     $env{'request.symb'} = $symb;
 3776:                     $cfile = &clutter($res);
 3777:                 } else {
 3778:                     $cfile = $env{'form.suppurl'};
 3779:                     my $escfile = &unescape($cfile);
 3780:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
 3781:                         $cfile = '/adm/wrapper'.$escfile;
 3782:                     } else {
 3783:                         $escfile =~ s{^http://}{};
 3784:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
 3785:                     }
 3786:                 }
 3787:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
 3788:                 if ($env{'form.forceedit'}) {
 3789:                     $forceview = 1;
 3790:                 } else {
 3791:                     $forceedit = 1;
 3792:                 }
 3793:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
 3794:             }
 3795:         }
 3796:         if ($uploaded || $incourse) {
 3797:             $home=&homeserver($cnum,$cdom);
 3798:         } elsif ($file !~ m{/$}) {
 3799:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
 3800:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
 3801:             # Check that the user has permission to edit this resource
 3802:             my $setpriv = 1;
 3803:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
 3804:             if (defined($cfudom)) {
 3805:                 $home=&homeserver($cfuname,$cfudom);
 3806:                 $cfile=$file;
 3807:             }
 3808:         }
 3809:         if (($cfile ne '') && (!$incourse || $uploaded) && 
 3810:             (($home ne '') && ($home ne 'no_host'))) {
 3811:             my @ids=&current_machine_ids();
 3812:             unless (grep(/^\Q$home\E$/,@ids)) {
 3813:                 $switchserver=1;
 3814:             }
 3815:         }
 3816:     }
 3817:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
 3818: }
 3819: 
 3820: sub is_course_upload {
 3821:     my ($file,$cnum,$cdom) = @_;
 3822:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
 3823:     $uploadpath =~ s{^\/}{};
 3824:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
 3825:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
 3826:         return 1;
 3827:     }
 3828:     return;
 3829: }
 3830: 
 3831: sub in_course {
 3832:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
 3833:     if ($hideprivileged) {
 3834:         my $skipuser;
 3835:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
 3836:         my @possdoms = ($cdom);  
 3837:         if ($coursehash{'checkforpriv'}) { 
 3838:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
 3839:         }
 3840:         if (&privileged($uname,$udom,\@possdoms)) {
 3841:             $skipuser = 1;
 3842:             if ($coursehash{'nothideprivileged'}) {
 3843:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 3844:                     my $user;
 3845:                     if ($item =~ /:/) {
 3846:                         $user = $item;
 3847:                     } else {
 3848:                         $user = join(':',split(/[\@]/,$item));
 3849:                     }
 3850:                     if ($user eq $uname.':'.$udom) {
 3851:                         undef($skipuser);
 3852:                         last;
 3853:                     }
 3854:                 }
 3855:             }
 3856:             if ($skipuser) {
 3857:                 return 0;
 3858:             }
 3859:         }
 3860:     }
 3861:     $type ||= 'any';
 3862:     if (!defined($cdom) || !defined($cnum)) {
 3863:         my $cid  = $env{'request.course.id'};
 3864:         $cdom = $env{'course.'.$cid.'.domain'};
 3865:         $cnum = $env{'course.'.$cid.'.num'};
 3866:     }
 3867:     my $typesref;
 3868:     if (($type eq 'any') || ($type eq 'all')) {
 3869:         $typesref = ['active','previous','future'];
 3870:     } elsif ($type eq 'previous' || $type eq 'future') {
 3871:         $typesref = [$type];
 3872:     }
 3873:     my %roles = &get_my_roles($uname,$udom,'userroles',
 3874:                               $typesref,undef,[$cdom]);
 3875:     my ($tmp) = keys(%roles);
 3876:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
 3877:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
 3878:     if (@course_roles > 0) {
 3879:         return 1;
 3880:     }
 3881:     return 0;
 3882: }
 3883: 
 3884: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
 3885: # input: action, courseID, current domain, intended
 3886: #        path to file, source of file, instruction to parse file for objects,
 3887: #        ref to hash for embedded objects,
 3888: #        ref to hash for codebase of java objects.
 3889: #        reference to scalar to accommodate mime type determined
 3890: #          from File::MMagic if $parser = parse.
 3891: #
 3892: # output: url to file (if action was uploaddoc), 
 3893: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
 3894: #
 3895: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
 3896: # course.
 3897: #
 3898: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3899: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
 3900: #          course's home server.
 3901: #
 3902: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
 3903: #          be copied from $source (current location) to 
 3904: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3905: #         and will then be copied to
 3906: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
 3907: #         course's home server.
 3908: #
 3909: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3910: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
 3911: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
 3912: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
 3913: #         in course's home server.
 3914: #
 3915: 
 3916: sub process_coursefile {
 3917:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
 3918:         $mimetype)=@_;
 3919:     my $fetchresult;
 3920:     my $home=&homeserver($docuname,$docudom);
 3921:     if ($action eq 'propagate') {
 3922:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3923: 			     $home);
 3924:     } else {
 3925:         my $fpath = '';
 3926:         my $fname = $file;
 3927:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3928:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3929:         my $filepath = &build_filepath($fpath);
 3930:         if ($action eq 'copy') {
 3931:             if ($source eq '') {
 3932:                 $fetchresult = 'no source file';
 3933:                 return $fetchresult;
 3934:             } else {
 3935:                 my $destination = $filepath.'/'.$fname;
 3936:                 rename($source,$destination);
 3937:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3938:                                  $home);
 3939:             }
 3940:         } elsif ($action eq 'uploaddoc') {
 3941:             open(my $fh,'>',$filepath.'/'.$fname);
 3942:             print $fh $env{'form.'.$source};
 3943:             close($fh);
 3944:             if ($parser eq 'parse') {
 3945:                 my $mm = new File::MMagic;
 3946:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
 3947:                 if ($type eq 'text/html') {
 3948:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
 3949:                     unless ($parse_result eq 'ok') {
 3950:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
 3951:                     }
 3952:                 }
 3953:                 if (ref($mimetype)) {
 3954:                     $$mimetype = $type;
 3955:                 } 
 3956:             }
 3957:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 3958:                                  $home);
 3959:             if ($fetchresult eq 'ok') {
 3960:                 return '/uploaded/'.$fpath.'/'.$fname;
 3961:             } else {
 3962:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3963:                         ' to host '.$home.': '.$fetchresult);
 3964:                 return '/adm/notfound.html';
 3965:             }
 3966:         }
 3967:     }
 3968:     unless ( $fetchresult eq 'ok') {
 3969:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 3970:              ' to host '.$home.': '.$fetchresult);
 3971:     }
 3972:     return $fetchresult;
 3973: }
 3974: 
 3975: sub build_filepath {
 3976:     my ($fpath) = @_;
 3977:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
 3978:     unless ($fpath eq '') {
 3979:         my @parts=split('/',$fpath);
 3980:         foreach my $part (@parts) {
 3981:             $filepath.= '/'.$part;
 3982:             if ((-e $filepath)!=1) {
 3983:                 mkdir($filepath,0777);
 3984:             }
 3985:         }
 3986:     }
 3987:     return $filepath;
 3988: }
 3989: 
 3990: sub store_edited_file {
 3991:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
 3992:     my $file = $primary_url;
 3993:     $file =~ s#^/uploaded/$docudom/$docuname/##;
 3994:     my $fpath = '';
 3995:     my $fname = $file;
 3996:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
 3997:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
 3998:     my $filepath = &build_filepath($fpath);
 3999:     open(my $fh,'>',$filepath.'/'.$fname);
 4000:     print $fh $content;
 4001:     close($fh);
 4002:     my $home=&homeserver($docuname,$docudom);
 4003:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
 4004: 			  $home);
 4005:     if ($$fetchresult eq 'ok') {
 4006:         return '/uploaded/'.$fpath.'/'.$fname;
 4007:     } else {
 4008:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
 4009: 		 ' to host '.$home.': '.$$fetchresult);
 4010:         return '/adm/notfound.html';
 4011:     }
 4012: }
 4013: 
 4014: sub clean_filename {
 4015:     my ($fname,$args)=@_;
 4016: # Replace Windows backslashes by forward slashes
 4017:     $fname=~s/\\/\//g;
 4018:     if (!$args->{'keep_path'}) {
 4019:         # Get rid of everything but the actual filename
 4020: 	$fname=~s/^.*\/([^\/]+)$/$1/;
 4021:     }
 4022: # Replace spaces by underscores
 4023:     $fname=~s/\s+/\_/g;
 4024: # Transliterate non-ascii text to ascii
 4025:     my $lang = &Apache::lonlocal::current_language();
 4026:     $fname = &LONCAPA::transliterate::fname_to_ascii($fname,$lang);
 4027: # Replace all other weird characters by nothing
 4028:     $fname=~s{[^/\w\.\-]}{}g;
 4029: # Replace all .\d. sequences with _\d. so they no longer look like version
 4030: # numbers
 4031:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
 4032:     return $fname;
 4033: }
 4034: 
 4035: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
 4036: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
 4037: # image with the same aspect ratio as the original, but with dimensions which do 
 4038: # not exceed $resizewidth and $resizeheight.
 4039:  
 4040: sub resizeImage {
 4041:     my ($img_path,$resizewidth,$resizeheight) = @_;
 4042:     my $ima = Image::Magick->new;
 4043:     my $resized;
 4044:     if (-e $img_path) {
 4045:         $ima->Read($img_path);
 4046:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
 4047:             my $width = $ima->Get('width');
 4048:             my $height = $ima->Get('height');
 4049:             if ($width > $resizewidth) {
 4050: 	        my $factor = $width/$resizewidth;
 4051:                 my $newheight = $height/$factor;
 4052:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
 4053:                 $resized = 1;
 4054:             }
 4055:         }
 4056:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
 4057:             my $width = $ima->Get('width');
 4058:             my $height = $ima->Get('height');
 4059:             if ($height > $resizeheight) {
 4060:                 my $factor = $height/$resizeheight;
 4061:                 my $newwidth = $width/$factor;
 4062:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
 4063:                 $resized = 1;
 4064:             }
 4065:         }
 4066:         if ($resized) {
 4067:             $ima->Write($img_path);
 4068:         }
 4069:     }
 4070:     return;
 4071: }
 4072: 
 4073: # --------------- Take an uploaded file and put it into the userfiles directory
 4074: # input: $formname - the contents of the file are in $env{"form.$formname"}
 4075: #                    the desired filename is in $env{"form.$formname.filename"}
 4076: #        $context - possible values: coursedoc, existingfile, overwrite, 
 4077: #                                    canceloverwrite, scantron or ''.
 4078: #                   if 'coursedoc': upload to the current course
 4079: #                   if 'existingfile': write file to tmp/overwrites directory 
 4080: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
 4081: #                   $context is passed as argument to &finishuserfileupload
 4082: #        $subdir - directory in userfile to store the file into
 4083: #        $parser - instruction to parse file for objects ($parser = parse) or
 4084: #                  if context is 'scantron', $parser is hashref of csv column mapping
 4085: #                  (e.g.,{ PaperID => 0, LastName => 1, FirstName => 2, ID => 3, 
 4086: #                          Section => 4, CODE => 5, FirstQuestion => 9 }).
 4087: #        $allfiles - reference to hash for embedded objects
 4088: #        $codebase - reference to hash for codebase of java objects
 4089: #        $desuname - username for permanent storage of uploaded file
 4090: #        $dsetudom - domain for permanaent storage of uploaded file
 4091: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
 4092: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
 4093: #        $resizewidth - width (pixels) to which to resize uploaded image
 4094: #        $resizeheight - height (pixels) to which to resize uploaded image
 4095: #        $mimetype - reference to scalar to accommodate mime type determined
 4096: #                    from File::MMagic.
 4097: # 
 4098: # output: url of file in userspace, or error: <message> 
 4099: #             or /adm/notfound.html if failure to upload occurse
 4100: 
 4101: sub userfileupload {
 4102:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
 4103:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
 4104:     if (!defined($subdir)) { $subdir='unknown'; }
 4105:     my $fname=$env{'form.'.$formname.'.filename'};
 4106:     $fname=&clean_filename($fname);
 4107:     # See if there is anything left
 4108:     unless ($fname) { return 'error: no uploaded file'; }
 4109:     # If filename now begins with a . prepend unix timestamp _ milliseconds
 4110:     if ($fname =~ /^\./) {
 4111:         my ($s,$usec) = &gettimeofday();
 4112:         while (length($usec) < 6) {
 4113:             $usec = '0'.$usec;
 4114:         }
 4115:         $fname = $s.'_'.substr($usec,0,3).$fname;
 4116:     }
 4117:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
 4118:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
 4119:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
 4120:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4121:         my $now = time;
 4122:         my $filepath;
 4123:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
 4124:              $filepath = 'tmp/helprequests/'.$now;
 4125:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
 4126:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
 4127:                          '_'.$env{'user.domain'}.'/pending';
 4128:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
 4129:             my ($docuname,$docudom);
 4130:             if ($destudom =~ /^$match_domain$/) {
 4131:                 $docudom = $destudom;
 4132:             } else {
 4133:                 $docudom = $env{'user.domain'};
 4134:             }
 4135:             if ($destuname =~ /^$match_username$/) {
 4136:                 $docuname = $destuname;
 4137:             } else {
 4138:                 $docuname = $env{'user.name'};
 4139:             }
 4140:             if (exists($env{'form.group'})) {
 4141:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4142:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4143:             }
 4144:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
 4145:             if ($context eq 'canceloverwrite') {
 4146:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
 4147:                 if (-e  $tempfile) {
 4148:                     my @info = stat($tempfile);
 4149:                     if ($info[9] eq $env{'form.timestamp'}) {
 4150:                         unlink($tempfile);
 4151:                     }
 4152:                 }
 4153:                 return;
 4154:             }
 4155:         }
 4156:         # Create the directory if not present
 4157:         my @parts=split(/\//,$filepath);
 4158:         my $fullpath = $perlvar{'lonDaemons'};
 4159:         for (my $i=0;$i<@parts;$i++) {
 4160:             $fullpath .= '/'.$parts[$i];
 4161:             if ((-e $fullpath)!=1) {
 4162:                 mkdir($fullpath,0777);
 4163:             }
 4164:         }
 4165:         open(my $fh,'>',$fullpath.'/'.$fname);
 4166:         print $fh $env{'form.'.$formname};
 4167:         close($fh);
 4168:         if ($context eq 'existingfile') {
 4169:             my @info = stat($fullpath.'/'.$fname);
 4170:             return ($fullpath.'/'.$fname,$info[9]);
 4171:         } else {
 4172:             return $fullpath.'/'.$fname;
 4173:         }
 4174:     }
 4175:     if ($subdir eq 'scantron') {
 4176:         $fname = 'scantron_orig_'.$fname;
 4177:     } else {
 4178:         $fname="$subdir/$fname";
 4179:     }
 4180:     if ($context eq 'coursedoc') {
 4181: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4182: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4183:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
 4184:             return &finishuserfileupload($docuname,$docudom,
 4185: 					 $formname,$fname,$parser,$allfiles,
 4186: 					 $codebase,$thumbwidth,$thumbheight,
 4187:                                          $resizewidth,$resizeheight,$context,$mimetype);
 4188:         } else {
 4189:             if ($env{'form.folder'}) {
 4190:                 $fname=$env{'form.folder'}.'/'.$fname;
 4191:             }
 4192:             return &process_coursefile('uploaddoc',$docuname,$docudom,
 4193: 				       $fname,$formname,$parser,
 4194: 				       $allfiles,$codebase,$mimetype);
 4195:         }
 4196:     } elsif (defined($destuname)) {
 4197:         my $docuname=$destuname;
 4198:         my $docudom=$destudom;
 4199: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4200: 				     $parser,$allfiles,$codebase,
 4201:                                      $thumbwidth,$thumbheight,
 4202:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4203:     } else {
 4204:         my $docuname=$env{'user.name'};
 4205:         my $docudom=$env{'user.domain'};
 4206:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
 4207:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
 4208:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 4209:         }
 4210: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
 4211: 				     $parser,$allfiles,$codebase,
 4212:                                      $thumbwidth,$thumbheight,
 4213:                                      $resizewidth,$resizeheight,$context,$mimetype);
 4214:     }
 4215: }
 4216: 
 4217: sub finishuserfileupload {
 4218:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
 4219:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
 4220:     my $path=$docudom.'/'.$docuname.'/';
 4221:     my $filepath=$perlvar{'lonDocRoot'};
 4222:   
 4223:     my ($fnamepath,$file,$fetchthumb);
 4224:     $file=$fname;
 4225:     if ($fname=~m|/|) {
 4226:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4227: 	$path.=$fnamepath.'/';
 4228:     }
 4229:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
 4230:     my $count;
 4231:     for ($count=4;$count<=$#parts;$count++) {
 4232:         $filepath.="/$parts[$count]";
 4233:         if ((-e $filepath)!=1) {
 4234: 	    mkdir($filepath,0777);
 4235:         }
 4236:     }
 4237: 
 4238: # Save the file
 4239:     {
 4240: 	if (!open(FH,'>',$filepath.'/'.$file)) {
 4241: 	    &logthis('Failed to create '.$filepath.'/'.$file);
 4242: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
 4243: 	    return '/adm/notfound.html';
 4244: 	}
 4245:         if ($context eq 'overwrite') {
 4246:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
 4247:             my $target = $filepath.'/'.$file;
 4248:             if (-e $source) {
 4249:                 my @info = stat($source);
 4250:                 if ($info[9] eq $env{'form.timestamp'}) {   
 4251:                     unless (&File::Copy::move($source,$target)) {
 4252:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
 4253:                         return "Moving from $source failed";
 4254:                     }
 4255:                 } else {
 4256:                     return "Temporary file: $source had unexpected date/time for last modification";
 4257:                 }
 4258:             } else {
 4259:                 return "Temporary file: $source missing";
 4260:             }
 4261:         } elsif (!print FH ($env{'form.'.$formname})) {
 4262: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
 4263: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
 4264: 	    return '/adm/notfound.html';
 4265: 	}
 4266: 	close(FH);
 4267:         if ($resizewidth && $resizeheight) {
 4268:             my $mm = new File::MMagic;
 4269:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
 4270:             if ($mime_type =~ m{^image/}) {
 4271: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
 4272:             }  
 4273: 	}
 4274:     }
 4275:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
 4276:         if (ref($mimetype)) {
 4277:             if ($$mimetype eq '') {
 4278:                 my $mm = new File::MMagic;
 4279:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
 4280:                 $$mimetype = $type;
 4281:             }
 4282:         }
 4283:     }
 4284:     if (($context ne 'scantron') && ($parser eq 'parse')) {
 4285:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
 4286:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
 4287:                                                        $allfiles,$codebase);
 4288:             unless ($parse_result eq 'ok') {
 4289:                 &logthis('Failed to parse '.$filepath.$file.
 4290: 	   	         ' for embedded media: '.$parse_result); 
 4291:             }
 4292:         }
 4293:     } elsif (($context eq 'scantron') && (ref($parser) eq 'HASH')) {
 4294:         my $format = $env{'form.scantron_format'};
 4295:         &bubblesheet_converter($docudom,$filepath.'/'.$file,$parser,$format);
 4296:     }
 4297:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4298:         my $input = $filepath.'/'.$file;
 4299:         my $output = $filepath.'/'.'tn-'.$file;
 4300:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4301:         my @args = ('convert','-sample',$thumbsize,$input,$output);
 4302:         system({$args[0]} @args);
 4303:         if (-e $filepath.'/'.'tn-'.$file) {
 4304:             $fetchthumb  = 1; 
 4305:         }
 4306:     }
 4307:  
 4308: # Notify homeserver to grep it
 4309: #
 4310:     my $docuhome=&homeserver($docuname,$docudom);	
 4311:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
 4312:     if ($fetchresult eq 'ok') {
 4313:         if ($fetchthumb) {
 4314:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
 4315:             if ($thumbresult ne 'ok') {
 4316:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
 4317:                          $docuhome.': '.$thumbresult);
 4318:             }
 4319:         }
 4320: #
 4321: # Return the URL to it
 4322:         return '/uploaded/'.$path.$file;
 4323:     } else {
 4324:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
 4325: 		 ': '.$fetchresult);
 4326:         return '/adm/notfound.html';
 4327:     }
 4328: }
 4329: 
 4330: sub extract_embedded_items {
 4331:     my ($fullpath,$allfiles,$codebase,$content) = @_;
 4332:     my @state = ();
 4333:     my (%lastids,%related,%shockwave,%flashvars);
 4334:     my %javafiles = (
 4335:                       codebase => '',
 4336:                       code => '',
 4337:                       archive => ''
 4338:                     );
 4339:     my %mediafiles = (
 4340:                       src => '',
 4341:                       movie => '',
 4342:                      );
 4343:     my $p;
 4344:     if ($content) {
 4345:         $p = HTML::LCParser->new($content);
 4346:     } else {
 4347:         $p = HTML::LCParser->new($fullpath);
 4348:     }
 4349:     while (my $t=$p->get_token()) {
 4350: 	if ($t->[0] eq 'S') {
 4351: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 4352: 	    push(@state, $tagname);
 4353:             if (lc($tagname) eq 'allow') {
 4354:                 &add_filetype($allfiles,$attr->{'src'},'src');
 4355:             }
 4356: 	    if (lc($tagname) eq 'img') {
 4357: 		&add_filetype($allfiles,$attr->{'src'},'src');
 4358: 	    }
 4359: 	    if (lc($tagname) eq 'a') {
 4360:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
 4361:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4362:                 }
 4363: 	    }
 4364:             if (lc($tagname) eq 'script') {
 4365:                 my $src;
 4366:                 if ($attr->{'archive'} =~ /\.jar$/i) {
 4367:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
 4368:                 } else {
 4369:                     if ($attr->{'src'} ne '') {
 4370:                         $src = $attr->{'src'};
 4371:                         &add_filetype($allfiles,$src,'src');
 4372:                     }
 4373:                 }
 4374:                 my $text = $p->get_trimmed_text();
 4375:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
 4376:                     my @swfargs = split(/,/,$1);
 4377:                     foreach my $item (@swfargs) {
 4378:                         $item =~ s/["']//g;
 4379:                         $item =~ s/^\s+//;
 4380:                         $item =~ s/\s+$//;
 4381:                     }
 4382:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
 4383:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
 4384:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
 4385:                         } else {
 4386:                             $related{$swfargs[0]} = [$swfargs[2]];
 4387:                         }
 4388:                     }
 4389:                 }
 4390:             }
 4391:             if (lc($tagname) eq 'link') {
 4392:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
 4393:                     &add_filetype($allfiles,$attr->{'href'},'href');
 4394:                 }
 4395:             }
 4396: 	    if (lc($tagname) eq 'object' ||
 4397: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
 4398: 		foreach my $item (keys(%javafiles)) {
 4399: 		    $javafiles{$item} = '';
 4400: 		}
 4401:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
 4402:                     $lastids{lc($tagname)} = $attr->{'id'};
 4403:                 }
 4404: 	    }
 4405: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
 4406: 		my $name = lc($attr->{'name'});
 4407: 		foreach my $item (keys(%javafiles)) {
 4408: 		    if ($name eq $item) {
 4409: 			$javafiles{$item} = $attr->{'value'};
 4410: 			last;
 4411: 		    }
 4412: 		}
 4413:                 my $pathfrom;
 4414: 		foreach my $item (keys(%mediafiles)) {
 4415: 		    if ($name eq $item) {
 4416:                         $pathfrom = $attr->{'value'};
 4417:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
 4418: 			&add_filetype($allfiles,$pathfrom,$name);
 4419: 			last;
 4420: 		    }
 4421: 		}
 4422:                 if ($name eq 'flashvars') {
 4423:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
 4424:                 }
 4425:                 if ($pathfrom ne '') {
 4426:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
 4427:                                          $pathfrom);
 4428:                 }
 4429: 	    }
 4430: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
 4431: 		foreach my $item (keys(%javafiles)) {
 4432: 		    if ($attr->{$item}) {
 4433: 			$javafiles{$item} = $attr->{$item};
 4434: 			last;
 4435: 		    }
 4436: 		}
 4437: 		foreach my $item (keys(%mediafiles)) {
 4438: 		    if ($attr->{$item}) {
 4439: 			&add_filetype($allfiles,$attr->{$item},$item);
 4440: 			last;
 4441: 		    }
 4442: 		}
 4443:                 if (lc($tagname) eq 'embed') {
 4444:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
 4445:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
 4446:                                              $attr->{'src'});
 4447:                     }
 4448:                 }
 4449: 	    }
 4450:             if (lc($tagname) eq 'iframe') {
 4451:                 my $src = $attr->{'src'} ;
 4452:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
 4453:                     &add_filetype($allfiles,$src,'src');
 4454:                 } elsif ($src =~ m{^/}) {
 4455:                     if ($env{'request.course.id'}) {
 4456:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 4457:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 4458:                         my $url = &hreflocation('',$fullpath);
 4459:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
 4460:                             my $relpath = $1;
 4461:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
 4462:                                 &add_filetype($allfiles,$1,'src');
 4463:                             }
 4464:                         }
 4465:                     }
 4466:                 }
 4467:             }
 4468:             if ($t->[4] =~ m{/>$}) {
 4469:                 pop(@state);
 4470:             }
 4471: 	} elsif ($t->[0] eq 'E') {
 4472: 	    my ($tagname) = ($t->[1]);
 4473: 	    if ($javafiles{'codebase'} ne '') {
 4474: 		$javafiles{'codebase'} .= '/';
 4475: 	    }  
 4476: 	    if (lc($tagname) eq 'applet' ||
 4477: 		lc($tagname) eq 'object' ||
 4478: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
 4479: 		) {
 4480: 		foreach my $item (keys(%javafiles)) {
 4481: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
 4482: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
 4483: 			&add_filetype($allfiles,$file,$item);
 4484: 		    }
 4485: 		}
 4486: 	    } 
 4487: 	    pop @state;
 4488: 	}
 4489:     }
 4490:     foreach my $id (sort(keys(%flashvars))) {
 4491:         if ($shockwave{$id} ne '') {
 4492:             my @pairs = split(/\&/,$flashvars{$id});
 4493:             foreach my $pair (@pairs) {
 4494:                 my ($key,$value) = split(/\=/,$pair);
 4495:                 if ($key eq 'thumb') {
 4496:                     &add_filetype($allfiles,$value,$key);
 4497:                 } elsif ($key eq 'content') {
 4498:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
 4499:                     my ($ext) = ($value =~ /\.([^.]+)$/);
 4500:                     if ($ext ne '') {
 4501:                         &add_filetype($allfiles,$path.$value,$ext);
 4502:                     }
 4503:                 }
 4504:             }
 4505:         }
 4506:     }
 4507:     return 'ok';
 4508: }
 4509: 
 4510: sub add_filetype {
 4511:     my ($allfiles,$file,$type)=@_;
 4512:     if (exists($allfiles->{$file})) {
 4513: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
 4514: 	    push(@{$allfiles->{$file}}, &escape($type));
 4515: 	}
 4516:     } else {
 4517: 	@{$allfiles->{$file}} = (&escape($type));
 4518:     }
 4519: }
 4520: 
 4521: sub embedded_dependency {
 4522:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
 4523:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
 4524:         if (($identifier ne '') &&
 4525:             (ref($related->{$identifier}) eq 'ARRAY') &&
 4526:             ($pathfrom ne '')) {
 4527:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
 4528:             foreach my $dep (@{$related->{$identifier}}) {
 4529:                 &add_filetype($allfiles,$path.$dep,'object');
 4530:             }
 4531:         }
 4532:     }
 4533:     return;
 4534: }
 4535: 
 4536: sub bubblesheet_converter {
 4537:     my ($cdom,$fullpath,$config,$format) = @_;
 4538:     if ((&domain($cdom) ne '') &&
 4539:         ($fullpath =~ m{^\Q$perlvar{'lonDocRoot'}/userfiles/$cdom/\E$match_courseid/scantron_orig}) &&
 4540:         (-e $fullpath) && (ref($config) eq 'HASH') && ($format ne '')) {
 4541:         my (%csvcols,%csvoptions);
 4542:         if (ref($config->{'fields'}) eq 'HASH') {  
 4543:             %csvcols = %{$config->{'fields'}};
 4544:         }
 4545:         if (ref($config->{'options'}) eq 'HASH') {
 4546:             %csvoptions = %{$config->{'options'}};
 4547:         }
 4548:         my %csvbynum = reverse(%csvcols);
 4549:         my %scantronconf = &get_scantron_config($format,$cdom);
 4550:         if (keys(%scantronconf)) {
 4551:             my %bynum = (
 4552:                           $scantronconf{CODEstart} => 'CODEstart',
 4553:                           $scantronconf{IDstart}   => 'IDstart',
 4554:                           $scantronconf{PaperID}   => 'PaperID',
 4555:                           $scantronconf{FirstName} => 'FirstName',
 4556:                           $scantronconf{LastName}  => 'LastName',
 4557:                           $scantronconf{Qstart}    => 'Qstart',
 4558:                         );
 4559:             my @ordered;
 4560:             foreach my $item (sort { $a <=> $b } keys(%bynum)) {
 4561:                 push(@ordered,$bynum{$item});
 4562:             }
 4563:             my %mapstart = (
 4564:                               CODEstart => 'CODE',
 4565:                               IDstart   => 'ID',
 4566:                               PaperID   => 'PaperID',
 4567:                               FirstName => 'FirstName',
 4568:                               LastName  => 'LastName',
 4569:                               Qstart    => 'FirstQuestion',
 4570:                            );
 4571:             my %maplength = (
 4572:                               CODEstart => 'CODElength',
 4573:                               IDstart   => 'IDlength',
 4574:                               PaperID   => 'PaperIDlength',
 4575:                               FirstName => 'FirstNamelength',
 4576:                               LastName  => 'LastNamelength',
 4577:             );
 4578:             if (open(my $fh,'<',$fullpath)) {
 4579:                 my $output;
 4580:                 my %lettdig = &letter_to_digits();
 4581:                 my %diglett = reverse(%lettdig);
 4582:                 my $numletts = scalar(keys(%lettdig));
 4583:                 my $num = 0;
 4584:                 while (my $line=<$fh>) {
 4585:                     $num ++;
 4586:                     next if (($num == 1) && ($csvoptions{'hdr'} == 1));
 4587:                     $line =~ s{[\r\n]+$}{};
 4588:                     my %found;
 4589:                     my @values = split(/,/,$line);
 4590:                     my ($qstart,$record);
 4591:                     for (my $i=0; $i<@values; $i++) {
 4592:                         if ((($qstart ne '') && ($i > $qstart)) ||
 4593:                             ($csvbynum{$i} eq 'FirstQuestion')) {
 4594:                             if ($values[$i] eq '') {
 4595:                                 $values[$i] = $scantronconf{'Qoff'};
 4596:                             } elsif ($scantronconf{'Qon'} eq 'number') {
 4597:                                 if ($values[$i] =~ /^[A-Ja-j]$/) {
 4598:                                     $values[$i] = $lettdig{uc($values[$i])};
 4599:                                 }
 4600:                             } elsif ($scantronconf{'Qon'} eq 'letter') {
 4601:                                 if ($values[$i] =~ /^[0-9]$/) {
 4602:                                     $values[$i] = $diglett{$values[$i]};
 4603:                                 }
 4604:                             } else {
 4605:                                 if ($values[$i] =~ /^[0-9A-Ja-j]$/) {
 4606:                                     my $digit;
 4607:                                     if ($values[$i] =~ /^[A-Ja-j]$/) {
 4608:                                         $digit = $lettdig{uc($values[$i])}-1;
 4609:                                         if ($values[$i] eq 'J') {
 4610:                                             $digit += $numletts;
 4611:                                         }
 4612:                                     } elsif ($values[$i] =~ /^[0-9]$/) {
 4613:                                         $digit = $values[$i]-1;
 4614:                                         if ($values[$i] eq '0') {
 4615:                                             $digit += $numletts;
 4616:                                         }
 4617:                                     }
 4618:                                     my $qval='';
 4619:                                     for (my $j=0; $j<$scantronconf{'Qlength'}; $j++) {
 4620:                                         if ($j == $digit) {
 4621:                                             $qval .= $scantronconf{'Qon'};
 4622:                                         } else {
 4623:                                             $qval .= $scantronconf{'Qoff'};
 4624:                                         }
 4625:                                     }
 4626:                                     $values[$i] = $qval;
 4627:                                 }
 4628:                             }
 4629:                             if (length($values[$i]) > $scantronconf{'Qlength'}) {
 4630:                                 $values[$i] = substr($values[$i],0,$scantronconf{'Qlength'});
 4631:                             }
 4632:                             my $numblank = $scantronconf{'Qlength'} - length($values[$i]);
 4633:                             if ($numblank > 0) {
 4634:                                  $values[$i] .= ($scantronconf{'Qoff'} x $numblank);
 4635:                             }
 4636:                             if ($csvbynum{$i} eq 'FirstQuestion') {
 4637:                                 $qstart = $i;
 4638:                                 $found{$csvbynum{$i}} = $values[$i];
 4639:                             } else {
 4640:                                 $found{'FirstQuestion'} .= $values[$i];
 4641:                             }
 4642:                         } elsif (exists($csvbynum{$i})) {
 4643:                             if ($csvoptions{'rem'}) {
 4644:                                 $values[$i] =~ s/^\s+//;
 4645:                             }
 4646:                             if (($csvbynum{$i} eq 'PaperID') && ($csvoptions{'pad'})) {
 4647:                                 while (length($values[$i]) < $scantronconf{$maplength{$csvbynum{$i}}}) {
 4648:                                     $values[$i] = '0'.$values[$i];
 4649:                                 }
 4650:                             }
 4651:                             $found{$csvbynum{$i}} = $values[$i];
 4652:                         }
 4653:                     }
 4654:                     foreach my $item (@ordered) {
 4655:                         my $currlength = 1+length($record);
 4656:                         my $numspaces = $scantronconf{$item} - $currlength;
 4657:                         if ($numspaces > 0) {
 4658:                             $record .= (' ' x $numspaces);
 4659:                         }
 4660:                         if (($mapstart{$item} ne '') && (exists($found{$mapstart{$item}}))) {
 4661:                             unless ($item eq 'Qstart') {
 4662:                                 if (length($found{$mapstart{$item}}) > $scantronconf{$maplength{$item}}) {
 4663:                                     $found{$mapstart{$item}} = substr($found{$mapstart{$item}},0,$scantronconf{$maplength{$item}});
 4664:                                 }
 4665:                             }
 4666:                             $record .= $found{$mapstart{$item}};
 4667:                         }
 4668:                     }
 4669:                     $output .= "$record\n";
 4670:                 }
 4671:                 close($fh);
 4672:                 if ($output) {
 4673:                     if (open(my $fh,'>',$fullpath)) {
 4674:                         print $fh $output;
 4675:                         close($fh);
 4676:                     }
 4677:                 }
 4678:             }
 4679:         }
 4680:         return;
 4681:     }
 4682: }
 4683: 
 4684: sub letter_to_digits {
 4685:     my %lettdig = (
 4686:                     A => 1,
 4687:                     B => 2,
 4688:                     C => 3,
 4689:                     D => 4,
 4690:                     E => 5,
 4691:                     F => 6,
 4692:                     G => 7,
 4693:                     H => 8,
 4694:                     I => 9,
 4695:                     J => 0,
 4696:                   );
 4697:     return %lettdig;
 4698: }
 4699: 
 4700: sub get_scantron_config {
 4701:     my ($which,$cdom) = @_;
 4702:     my @lines = &get_scantronformat_file($cdom);
 4703:     my %config;
 4704:     #FIXME probably should move to XML it has already gotten a bit much now
 4705:     foreach my $line (@lines) {
 4706:         my ($name,$descrip)=split(/:/,$line);
 4707:         if ($name ne $which ) { next; }
 4708:         chomp($line);
 4709:         my @config=split(/:/,$line);
 4710:         $config{'name'}=$config[0];
 4711:         $config{'description'}=$config[1];
 4712:         $config{'CODElocation'}=$config[2];
 4713:         $config{'CODEstart'}=$config[3];
 4714:         $config{'CODElength'}=$config[4];
 4715:         $config{'IDstart'}=$config[5];
 4716:         $config{'IDlength'}=$config[6];
 4717:         $config{'Qstart'}=$config[7];
 4718:         $config{'Qlength'}=$config[8];
 4719:         $config{'Qoff'}=$config[9];
 4720:         $config{'Qon'}=$config[10];
 4721:         $config{'PaperID'}=$config[11];
 4722:         $config{'PaperIDlength'}=$config[12];
 4723:         $config{'FirstName'}=$config[13];
 4724:         $config{'FirstNamelength'}=$config[14];
 4725:         $config{'LastName'}=$config[15];
 4726:         $config{'LastNamelength'}=$config[16];
 4727:         $config{'BubblesPerRow'}=$config[17];
 4728:         last;
 4729:     }
 4730:     return %config;
 4731: }
 4732: 
 4733: sub get_scantronformat_file {
 4734:     my ($cdom) = @_;
 4735:     if ($cdom eq '') {
 4736:         $cdom= $env{'course.'.$env{'request.course.id'}.'.domain'};
 4737:     }
 4738:     my %domconfig = &get_dom('configuration',['scantron'],$cdom);
 4739:     my $gottab = 0;
 4740:     my @lines;
 4741:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4742:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4743:             my $formatfile = &getfile($perlvar{'lonDocRoot'}.$domconfig{'scantron'}{'scantronformat'});
 4744:             if ($formatfile ne '-1') {
 4745:                 @lines = split("\n",$formatfile,-1);
 4746:                 $gottab = 1;
 4747:             }
 4748:         }
 4749:     }
 4750:     if (!$gottab) {
 4751:         my $confname = $cdom.'-domainconfig';
 4752:         my $default = $perlvar{'lonDocRoot'}.'/res/'.$cdom.'/'.$confname.'/default.tab';
 4753:         my $formatfile = &getfile($default);
 4754:         if ($formatfile ne '-1') {
 4755:             @lines = split("\n",$formatfile,-1);
 4756:             $gottab = 1;
 4757:         }
 4758:     }
 4759:     if (!$gottab) {
 4760:         my @domains = &current_machine_domains();
 4761:         if (grep(/^\Q$cdom\E$/,@domains)) {
 4762:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/scantronformat.tab')) {
 4763:                 @lines = <$fh>;
 4764:                 close($fh);
 4765:             }
 4766:         } else {
 4767:             if (open(my $fh,'<',$perlvar{'lonTabDir'}.'/default_scantronformat.tab')) {
 4768:                 @lines = <$fh>;
 4769:                 close($fh);
 4770:             }
 4771:         }
 4772:     }
 4773:     return @lines;
 4774: }
 4775: 
 4776: sub removeuploadedurl {
 4777:     my ($url)=@_;	
 4778:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
 4779:     return &removeuserfile($uname,$udom,$fname);
 4780: }
 4781: 
 4782: sub removeuserfile {
 4783:     my ($docuname,$docudom,$fname)=@_;
 4784:     my $home=&homeserver($docuname,$docudom);    
 4785:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
 4786:     if ($result eq 'ok') {	
 4787:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
 4788:             my $metafile = $fname.'.meta';
 4789:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
 4790: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
 4791:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
 4792:             my $sqlresult = 
 4793:                 &update_portfolio_table($docuname,$docudom,$file,
 4794:                                         'portfolio_metadata',$group,
 4795:                                         'delete');
 4796:         }
 4797:     }
 4798:     return $result;
 4799: }
 4800: 
 4801: sub mkdiruserfile {
 4802:     my ($docuname,$docudom,$dir)=@_;
 4803:     my $home=&homeserver($docuname,$docudom);
 4804:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
 4805: }
 4806: 
 4807: sub renameuserfile {
 4808:     my ($docuname,$docudom,$old,$new)=@_;
 4809:     my $home=&homeserver($docuname,$docudom);
 4810:     my $result = &reply("renameuserfile:$docudom:$docuname:".
 4811:                         &escape("$old").':'.&escape("$new"),$home);
 4812:     if ($result eq 'ok') {
 4813:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
 4814:             my $oldmeta = $old.'.meta';
 4815:             my $newmeta = $new.'.meta';
 4816:             my $metaresult = 
 4817:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
 4818: 	    my $url = "/uploaded/$docudom/$docuname/$old";
 4819:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
 4820:             my $sqlresult = 
 4821:                 &update_portfolio_table($docuname,$docudom,$file,
 4822:                                         'portfolio_metadata',$group,
 4823:                                         'delete');
 4824:         }
 4825:     }
 4826:     return $result;
 4827: }
 4828: 
 4829: # ------------------------------------------------------------------------- Log
 4830: 
 4831: sub log {
 4832:     my ($dom,$nam,$hom,$what)=@_;
 4833:     return critical("log:$dom:$nam:$what",$hom);
 4834: }
 4835: 
 4836: # ------------------------------------------------------------------ Course Log
 4837: #
 4838: # This routine flushes several buffers of non-mission-critical nature
 4839: #
 4840: 
 4841: sub flushcourselogs {
 4842:     &logthis('Flushing log buffers');
 4843: #
 4844: # course logs
 4845: # This is a log of all transactions in a course, which can be used
 4846: # for data mining purposes
 4847: #
 4848: # It also collects the courseid database, which lists last transaction
 4849: # times and course titles for all courseids
 4850: #
 4851:     my %courseidbuffer=();
 4852:     foreach my $crsid (keys(%courselogs)) {
 4853:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
 4854: 		          &escape($courselogs{$crsid}),
 4855: 		          $coursehombuf{$crsid}) eq 'ok') {
 4856: 	    delete $courselogs{$crsid};
 4857:         } else {
 4858:             &logthis('Failed to flush log buffer for '.$crsid);
 4859:             if (length($courselogs{$crsid})>40000) {
 4860:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
 4861:                         " exceeded maximum size, deleting.</font>");
 4862:                delete $courselogs{$crsid};
 4863:             }
 4864:         }
 4865:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
 4866:             'description' => $coursedescrbuf{$crsid},
 4867:             'inst_code'    => $courseinstcodebuf{$crsid},
 4868:             'type'        => $coursetypebuf{$crsid},
 4869:             'owner'       => $courseownerbuf{$crsid},
 4870:         };
 4871:     }
 4872: #
 4873: # Write course id database (reverse lookup) to homeserver of courses 
 4874: # Is used in pickcourse
 4875: #
 4876:     foreach my $crs_home (keys(%courseidbuffer)) {
 4877:         my $response = &courseidput(&host_domain($crs_home),
 4878:                                     $courseidbuffer{$crs_home},
 4879:                                     $crs_home,'timeonly');
 4880:     }
 4881: #
 4882: # File accesses
 4883: # Writes to the dynamic metadata of resources to get hit counts, etc.
 4884: #
 4885:     foreach my $entry (keys(%accesshash)) {
 4886:         if ($entry =~ /___count$/) {
 4887:             my ($dom,$name);
 4888:             ($dom,$name,undef)=
 4889: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
 4890:             if (! defined($dom) || $dom eq '' || 
 4891:                 ! defined($name) || $name eq '') {
 4892:                 my $cid = $env{'request.course.id'};
 4893:                 $dom  = $env{'request.'.$cid.'.domain'};
 4894:                 $name = $env{'request.'.$cid.'.num'};
 4895:             }
 4896:             my $value = $accesshash{$entry};
 4897:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
 4898:             my %temphash=($url => $value);
 4899:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
 4900:             if ($result eq 'ok') {
 4901:                 delete $accesshash{$entry};
 4902:             }
 4903:         } else {
 4904:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
 4905:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
 4906:             my %temphash=($entry => $accesshash{$entry});
 4907:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
 4908:                 delete $accesshash{$entry};
 4909:             }
 4910:         }
 4911:     }
 4912: #
 4913: # Roles
 4914: # Reverse lookup of user roles for course faculty/staff and co-authorship
 4915: #
 4916:     foreach my $entry (keys(%userrolehash)) {
 4917:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
 4918: 	    split(/\:/,$entry);
 4919:         if (&Apache::lonnet::put('nohist_userroles',
 4920:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
 4921:                 $rudom,$runame) eq 'ok') {
 4922: 	    delete $userrolehash{$entry};
 4923:         }
 4924:     }
 4925: #
 4926: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
 4927: #
 4928:     my %domrolebuffer = ();
 4929:     foreach my $entry (keys(%domainrolehash)) {
 4930:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
 4931:         if ($domrolebuffer{$rudom}) {
 4932:             $domrolebuffer{$rudom}.='&'.&escape($entry).
 4933:                       '='.&escape($domainrolehash{$entry});
 4934:         } else {
 4935:             $domrolebuffer{$rudom}.=&escape($entry).
 4936:                       '='.&escape($domainrolehash{$entry});
 4937:         }
 4938:         delete $domainrolehash{$entry};
 4939:     }
 4940:     foreach my $dom (keys(%domrolebuffer)) {
 4941: 	my %servers;
 4942: 	if (defined(&domain($dom,'primary'))) {
 4943: 	    my $primary=&domain($dom,'primary');
 4944: 	    my $hostname=&hostname($primary);
 4945: 	    $servers{$primary} = $hostname;
 4946: 	} else { 
 4947: 	    %servers = &get_servers($dom,'library');
 4948: 	}
 4949: 	foreach my $tryserver (keys(%servers)) {
 4950: 	    if (&reply('domroleput:'.$dom.':'.
 4951: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
 4952: 		last;
 4953: 	    } else {  
 4954: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
 4955: 	    }
 4956:         }
 4957:     }
 4958:     $dumpcount++;
 4959: }
 4960: 
 4961: sub courselog {
 4962:     my $what=shift;
 4963:     $what=time.':'.$what;
 4964:     unless ($env{'request.course.id'}) { return ''; }
 4965:     $coursedombuf{$env{'request.course.id'}}=
 4966:        $env{'course.'.$env{'request.course.id'}.'.domain'};
 4967:     $coursenumbuf{$env{'request.course.id'}}=
 4968:        $env{'course.'.$env{'request.course.id'}.'.num'};
 4969:     $coursehombuf{$env{'request.course.id'}}=
 4970:        $env{'course.'.$env{'request.course.id'}.'.home'};
 4971:     $coursedescrbuf{$env{'request.course.id'}}=
 4972:        $env{'course.'.$env{'request.course.id'}.'.description'};
 4973:     $courseinstcodebuf{$env{'request.course.id'}}=
 4974:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
 4975:     $courseownerbuf{$env{'request.course.id'}}=
 4976:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
 4977:     $coursetypebuf{$env{'request.course.id'}}=
 4978:        $env{'course.'.$env{'request.course.id'}.'.type'};
 4979:     if (defined $courselogs{$env{'request.course.id'}}) {
 4980: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
 4981:     } else {
 4982: 	$courselogs{$env{'request.course.id'}}.=$what;
 4983:     }
 4984:     if (length($courselogs{$env{'request.course.id'}})>4048) {
 4985: 	&flushcourselogs();
 4986:     }
 4987: }
 4988: 
 4989: sub courseacclog {
 4990:     my $fnsymb=shift;
 4991:     unless ($env{'request.course.id'}) { return ''; }
 4992:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
 4993:     if ($fnsymb=~/$LONCAPA::assess_re/) {
 4994:         $what.=':POST';
 4995:         # FIXME: Probably ought to escape things....
 4996: 	foreach my $key (keys(%env)) {
 4997:             if ($key=~/^form\.(.*)/) {
 4998:                 my $formitem = $1;
 4999:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
 5000:                     $what.=':'.$formitem.'='.$env{$key};
 5001:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
 5002:                     $what.=':'.$formitem.'='.$env{$key};
 5003:                 }
 5004:             }
 5005:         }
 5006:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
 5007:         # FIXME: We should not be depending on a form parameter that someone
 5008:         # editing lonsearchcat.pm might change in the future.
 5009:         if ($env{'form.phase'} eq 'course_search') {
 5010:             $what.= ':POST';
 5011:             # FIXME: Probably ought to escape things....
 5012:             foreach my $element ('courseexp','crsfulltext','crsrelated',
 5013:                                  'crsdiscuss') {
 5014:                 $what.=':'.$element.'='.$env{'form.'.$element};
 5015:             }
 5016:         }
 5017:     }
 5018:     &courselog($what);
 5019: }
 5020: 
 5021: sub countacc {
 5022:     my $url=&declutter(shift);
 5023:     return if (! defined($url) || $url eq '');
 5024:     unless ($env{'request.course.id'}) { return ''; }
 5025: #
 5026: # Mark that this url was used in this course
 5027: #
 5028:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
 5029: #
 5030: # Increase the access count for this resource in this child process
 5031: #
 5032:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
 5033:     $accesshash{$key}++;
 5034: }
 5035: 
 5036: sub linklog {
 5037:     my ($from,$to)=@_;
 5038:     $from=&declutter($from);
 5039:     $to=&declutter($to);
 5040:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
 5041:     $accesshash{$to.'___'.$from.'___goto'}=1;
 5042: }
 5043: 
 5044: sub statslog {
 5045:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
 5046:     if ($users<2) { return; }
 5047:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
 5048:             'course'       => $env{'request.course.id'},
 5049:             'sections'     => '"all"',
 5050:             'num_students' => $users,
 5051:             'part'         => $part,
 5052:             'symb'         => $symb,
 5053:             'mean_tries'   => $av_attempts,
 5054:             'deg_of_diff'  => $degdiff});
 5055:     foreach my $key (keys(%dynstore)) {
 5056:         $accesshash{$key}=$dynstore{$key};
 5057:     }
 5058: }
 5059:   
 5060: sub userrolelog {
 5061:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
 5062:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
 5063:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5064:        $userrolehash
 5065:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5066:                     =$tend.':'.$tstart;
 5067:     }
 5068:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
 5069:        $userrolehash
 5070:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
 5071:                     =$tend.':'.$tstart;
 5072:     }
 5073:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
 5074:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
 5075:        $domainrolehash
 5076:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
 5077:                     = $tend.':'.$tstart;
 5078:     }
 5079: }
 5080: 
 5081: sub courserolelog {
 5082:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
 5083:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
 5084:         my $cdom = $1;
 5085:         my $cnum = $2;
 5086:         my $sec = $3;
 5087:         my $namespace = 'rolelog';
 5088:         my %storehash = (
 5089:                            role    => $trole,
 5090:                            start   => $tstart,
 5091:                            end     => $tend,
 5092:                            selfenroll => $selfenroll,
 5093:                            context    => $context,
 5094:                         );
 5095:         if ($trole eq 'gr') {
 5096:             $namespace = 'groupslog';
 5097:             $storehash{'group'} = $sec;
 5098:         } else {
 5099:             $storehash{'section'} = $sec;
 5100:         }
 5101:         &write_log('course',$namespace,\%storehash,$delflag,$username,
 5102:                    $domain,$cnum,$cdom);
 5103:         if (($trole ne 'st') || ($sec ne '')) {
 5104:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
 5105:         }
 5106:     }
 5107:     return;
 5108: }
 5109: 
 5110: sub domainrolelog {
 5111:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5112:     if ($area =~ m{^/($match_domain)/$}) {
 5113:         my $cdom = $1;
 5114:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
 5115:         my $namespace = 'rolelog';
 5116:         my %storehash = (
 5117:                            role    => $trole,
 5118:                            start   => $tstart,
 5119:                            end     => $tend,
 5120:                            context => $context,
 5121:                         );
 5122:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
 5123:                    $domain,$domconfiguser,$cdom);
 5124:     }
 5125:     return;
 5126: 
 5127: }
 5128: 
 5129: sub coauthorrolelog {
 5130:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
 5131:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
 5132:         my $audom = $1;
 5133:         my $auname = $2;
 5134:         my $namespace = 'rolelog';
 5135:         my %storehash = (
 5136:                            role    => $trole,
 5137:                            start   => $tstart,
 5138:                            end     => $tend,
 5139:                            context => $context,
 5140:                         );
 5141:         &write_log('author',$namespace,\%storehash,$delflag,$username,
 5142:                    $domain,$auname,$audom);
 5143:     }
 5144:     return;
 5145: }
 5146: 
 5147: sub get_course_adv_roles {
 5148:     my ($cid,$codes) = @_;
 5149:     $cid=$env{'request.course.id'} unless (defined($cid));
 5150:     my %coursehash=&coursedescription($cid);
 5151:     my $crstype = &Apache::loncommon::course_type($cid);
 5152:     my %nothide=();
 5153:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5154:         if ($user !~ /:/) {
 5155: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
 5156:         } else {
 5157:             $nothide{$user}=1;
 5158:         }
 5159:     }
 5160:     my @possdoms = ($coursehash{'domain'});
 5161:     if ($coursehash{'checkforpriv'}) {
 5162:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
 5163:     }
 5164:     my %returnhash=();
 5165:     my %dumphash=
 5166:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
 5167:     my $now=time;
 5168:     my %privileged;
 5169:     foreach my $entry (keys(%dumphash)) {
 5170: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5171:         if (($tstart) && ($tstart<0)) { next; }
 5172:         if (($tend) && ($tend<$now)) { next; }
 5173:         if (($tstart) && ($now<$tstart)) { next; }
 5174:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
 5175: 	if ($username eq '' || $domain eq '') { next; }
 5176:         if ((&privileged($username,$domain,\@possdoms)) &&
 5177:             (!$nothide{$username.':'.$domain})) { next; }
 5178: 	if ($role eq 'cr') { next; }
 5179:         if ($codes) {
 5180:             if ($section) { $role .= ':'.$section; }
 5181:             if ($returnhash{$role}) {
 5182:                 $returnhash{$role}.=','.$username.':'.$domain;
 5183:             } else {
 5184:                 $returnhash{$role}=$username.':'.$domain;
 5185:             }
 5186:         } else {
 5187:             my $key=&plaintext($role,$crstype);
 5188:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
 5189:             if ($returnhash{$key}) {
 5190: 	        $returnhash{$key}.=','.$username.':'.$domain;
 5191:             } else {
 5192:                 $returnhash{$key}=$username.':'.$domain;
 5193:             }
 5194:         }
 5195:     }
 5196:     return %returnhash;
 5197: }
 5198: 
 5199: sub get_my_roles {
 5200:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
 5201:     unless (defined($uname)) { $uname=$env{'user.name'}; }
 5202:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
 5203:     my (%dumphash,%nothide);
 5204:     if ($context eq 'userroles') {
 5205:         %dumphash = &dump('roles',$udom,$uname);
 5206:     } else {
 5207:         %dumphash = &dump('nohist_userroles',$udom,$uname);
 5208:         if ($hidepriv) {
 5209:             my %coursehash=&coursedescription($udom.'_'.$uname);
 5210:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
 5211:                 if ($user !~ /:/) {
 5212:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
 5213:                 } else {
 5214:                     $nothide{$user} = 1;
 5215:                 }
 5216:             }
 5217:         }
 5218:     }
 5219:     my %returnhash=();
 5220:     my $now=time;
 5221:     my %privileged;
 5222:     foreach my $entry (keys(%dumphash)) {
 5223:         my ($role,$tend,$tstart);
 5224:         if ($context eq 'userroles') {
 5225:             next if ($entry =~ /^rolesdef/);
 5226: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
 5227:         } else {
 5228:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
 5229:         }
 5230:         if (($tstart) && ($tstart<0)) { next; }
 5231:         my $status = 'active';
 5232:         if (($tend) && ($tend<=$now)) {
 5233:             $status = 'previous';
 5234:         } 
 5235:         if (($tstart) && ($now<$tstart)) {
 5236:             $status = 'future';
 5237:         }
 5238:         if (ref($types) eq 'ARRAY') {
 5239:             if (!grep(/^\Q$status\E$/,@{$types})) {
 5240:                 next;
 5241:             } 
 5242:         } else {
 5243:             if ($status ne 'active') {
 5244:                 next;
 5245:             }
 5246:         }
 5247:         my ($rolecode,$username,$domain,$section,$area);
 5248:         if ($context eq 'userroles') {
 5249:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
 5250:             (undef,$domain,$username,$section) = split(/\//,$area);
 5251:         } else {
 5252:             ($role,$username,$domain,$section) = split(/\:/,$entry);
 5253:         }
 5254:         if (ref($roledoms) eq 'ARRAY') {
 5255:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
 5256:                 next;
 5257:             }
 5258:         }
 5259:         if (ref($roles) eq 'ARRAY') {
 5260:             if (!grep(/^\Q$role\E$/,@{$roles})) {
 5261:                 if ($role =~ /^cr\//) {
 5262:                     if (!grep(/^cr$/,@{$roles})) {
 5263:                         next;
 5264:                     }
 5265:                 } elsif ($role =~ /^gr\//) {
 5266:                     if (!grep(/^gr$/,@{$roles})) {
 5267:                         next;
 5268:                     }
 5269:                 } else {
 5270:                     next;
 5271:                 }
 5272:             }
 5273:         }
 5274:         if ($hidepriv) {
 5275:             my @privroles = ('dc','su');
 5276:             if ($context eq 'userroles') {
 5277:                 next if (grep(/^\Q$role\E$/,@privroles));
 5278:             } else {
 5279:                 my $possdoms = [$domain];
 5280:                 if (ref($roledoms) eq 'ARRAY') {
 5281:                    push(@{$possdoms},@{$roledoms}); 
 5282:                 }
 5283:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
 5284:                     if (!$nothide{$username.':'.$domain}) {
 5285:                         next;
 5286:                     }
 5287:                 }
 5288:             }
 5289:         }
 5290:         if ($withsec) {
 5291:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
 5292:                 $tstart.':'.$tend;
 5293:         } else {
 5294:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
 5295:         }
 5296:     }
 5297:     return %returnhash;
 5298: }
 5299: 
 5300: sub get_all_adhocroles {
 5301:     my ($dom) = @_;
 5302:     my @roles_by_num = ();
 5303:     my %domdefaults = &get_domain_defaults($dom);
 5304:     my (%description,%access_in_dom,%access_info);
 5305:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
 5306:         my $count = 0;
 5307:         my %domcurrent = %{$domdefaults{'adhocroles'}};
 5308:         my %ordered;
 5309:         foreach my $role (sort(keys(%domcurrent))) {
 5310:             my ($order,$desc,$access_in_dom);
 5311:             if (ref($domcurrent{$role}) eq 'HASH') {
 5312:                 $order = $domcurrent{$role}{'order'};
 5313:                 $desc = $domcurrent{$role}{'desc'};
 5314:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
 5315:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
 5316:             }
 5317:             if ($order eq '') {
 5318:                 $order = $count;
 5319:             }
 5320:             $ordered{$order} = $role;
 5321:             if ($desc ne '') {
 5322:                 $description{$role} = $desc;
 5323:             } else {
 5324:                 $description{$role}= $role;
 5325:             }
 5326:             $count++;
 5327:         }
 5328:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 5329:             push(@roles_by_num,$ordered{$item});
 5330:         }
 5331:     }
 5332:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
 5333: }
 5334: 
 5335: sub get_my_adhocroles {
 5336:     my ($cid,$checkreg) = @_;
 5337:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
 5338:     if ($env{'request.course.id'} eq $cid) {
 5339:         $cdom = $env{'course.'.$cid.'.domain'};
 5340:         $cnum = $env{'course.'.$cid.'.num'};
 5341:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
 5342:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
 5343:         $cdom = $1;
 5344:         $cnum = $2;
 5345:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
 5346:                                      $cdom,$cnum);
 5347:     }
 5348:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
 5349:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5350:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
 5351:         if ($rosterhash{$user} ne '') {
 5352:             my $type = (split(/:/,$rosterhash{$user}))[5];
 5353:             return ([],{}) if ($type eq 'auto');
 5354:         }
 5355:     }
 5356:     if (($cdom ne '') && ($cnum ne ''))  {
 5357:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
 5358:             my $then=$env{'user.login.time'};
 5359:             my $update=$env{'user.update.time'};
 5360:             if (!$update) {
 5361:                 $update = $then;
 5362:             }
 5363:             my @liveroles;
 5364:             foreach my $role ('dh','da') {
 5365:                 if ($env{"user.role.$role./$cdom/"}) {
 5366:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
 5367:                     my $limit = $update;
 5368:                     if ($env{'request.role'} eq "$role./$cdom/") {
 5369:                         $limit = $then;
 5370:                     }
 5371:                     my $activerole = 1;
 5372:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
 5373:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
 5374:                     if ($activerole) {
 5375:                         push(@liveroles,$role);
 5376:                     }
 5377:                 }
 5378:             }
 5379:             if (@liveroles) {
 5380:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
 5381:                     my ($accessref,$accessinfo,%access_in_dom);
 5382:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
 5383:                     if (ref($roles_by_num) eq 'ARRAY') {
 5384:                         if (@{$roles_by_num}) {
 5385:                             my %settings;
 5386:                             if ($env{'request.course.id'} eq $cid) {
 5387:                                 foreach my $envkey (keys(%env)) {
 5388:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
 5389:                                         $settings{$1} = $env{$envkey};
 5390:                                     }
 5391:                                 }
 5392:                             } else {
 5393:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
 5394:                             }
 5395:                             my %setincrs;
 5396:                             if ($settings{'internal.adhocaccess'}) {
 5397:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
 5398:                             }
 5399:                             my @statuses;
 5400:                             if ($env{'environment.inststatus'}) {
 5401:                                 @statuses = split(/,/,$env{'environment.inststatus'});
 5402:                             }
 5403:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
 5404:                             if (ref($accessref) eq 'HASH') {
 5405:                                 %access_in_dom = %{$accessref};
 5406:                             }
 5407:                             foreach my $role (@{$roles_by_num}) {
 5408:                                 my ($curraccess,@okstatus,@personnel);
 5409:                                 if ($setincrs{$role}) {
 5410:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
 5411:                                     if ($curraccess eq 'status') {
 5412:                                         @okstatus = split(/\&/,$rest);
 5413:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5414:                                         @personnel = split(/\&/,$rest);
 5415:                                     }
 5416:                                 } else {
 5417:                                     $curraccess = $access_in_dom{$role};
 5418:                                     if (ref($accessinfo) eq 'HASH') {
 5419:                                         if ($curraccess eq 'status') {
 5420:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5421:                                                 @okstatus = @{$accessinfo->{$role}};
 5422:                                             }
 5423:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5424:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
 5425:                                                 @personnel = @{$accessinfo->{$role}};
 5426:                                             }
 5427:                                         }
 5428:                                     }
 5429:                                 }
 5430:                                 if ($curraccess eq 'none') {
 5431:                                     next;
 5432:                                 } elsif ($curraccess eq 'all') {
 5433:                                     push(@possroles,$role);
 5434:                                 } elsif ($curraccess eq 'dh') {
 5435:                                     if (grep(/^dh$/,@liveroles)) {
 5436:                                         push(@possroles,$role);
 5437:                                     } else {
 5438:                                         next;
 5439:                                     }
 5440:                                 } elsif ($curraccess eq 'da') {
 5441:                                     if (grep(/^da$/,@liveroles)) {
 5442:                                         push(@possroles,$role);
 5443:                                     } else {
 5444:                                         next;
 5445:                                     }
 5446:                                 } elsif ($curraccess eq 'status') {
 5447:                                     if (@okstatus) {
 5448:                                         if (!@statuses) {
 5449:                                             if (grep(/^default$/,@okstatus)) {
 5450:                                                 push(@possroles,$role);
 5451:                                             }
 5452:                                         } else {
 5453:                                             foreach my $status (@okstatus) {
 5454:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
 5455:                                                     push(@possroles,$role);
 5456:                                                     last;
 5457:                                                 }
 5458:                                             }
 5459:                                         }
 5460:                                     }
 5461:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
 5462:                                     if (grep(/^\Q$user\E$/,@personnel)) {
 5463:                                         if ($curraccess eq 'exc') {
 5464:                                             push(@possroles,$role);
 5465:                                         }
 5466:                                     } elsif ($curraccess eq 'inc') {
 5467:                                         push(@possroles,$role);
 5468:                                     }
 5469:                                 }
 5470:                             }
 5471:                         }
 5472:                     }
 5473:                 }
 5474:             }
 5475:         }
 5476:     }
 5477:     unless (ref($description) eq 'HASH') {
 5478:         if (ref($roles_by_num) eq 'ARRAY') {
 5479:             my %desc;
 5480:             map { $desc{$_} = $_; } (@{$roles_by_num});
 5481:             $description = \%desc;
 5482:         } else {
 5483:             $description = {};
 5484:         }
 5485:     }
 5486:     return (\@possroles,$description);
 5487: }
 5488: 
 5489: # ----------------------------------------------------- Frontpage Announcements
 5490: #
 5491: #
 5492: 
 5493: sub postannounce {
 5494:     my ($server,$text)=@_;
 5495:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
 5496:     unless ($text=~/\w/) { $text=''; }
 5497:     return &reply('setannounce:'.&escape($text),$server);
 5498: }
 5499: 
 5500: sub getannounce {
 5501: 
 5502:     if (open(my $fh,"<",$perlvar{'lonDocRoot'}.'/announcement.txt')) {
 5503: 	my $announcement='';
 5504: 	while (my $line = <$fh>) { $announcement .= $line; }
 5505: 	close($fh);
 5506: 	if ($announcement=~/\w/) { 
 5507: 	    return 
 5508:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
 5509:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
 5510: 	} else {
 5511: 	    return '';
 5512: 	}
 5513:     } else {
 5514: 	return '';
 5515:     }
 5516: }
 5517: 
 5518: # ---------------------------------------------------------- Course ID routines
 5519: # Deal with domain's nohist_courseid.db files
 5520: #
 5521: 
 5522: sub courseidput {
 5523:     my ($domain,$storehash,$coursehome,$caller) = @_;
 5524:     return unless (ref($storehash) eq 'HASH');
 5525:     my $outcome;
 5526:     if ($caller eq 'timeonly') {
 5527:         my $cids = '';
 5528:         foreach my $item (keys(%$storehash)) {
 5529:             $cids.=&escape($item).'&';
 5530:         }
 5531:         $cids=~s/\&$//;
 5532:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
 5533:                           $coursehome);       
 5534:     } else {
 5535:         my $items = '';
 5536:         foreach my $item (keys(%$storehash)) {
 5537:             $items.= &escape($item).'='.
 5538:                      &freeze_escape($$storehash{$item}).'&';
 5539:         }
 5540:         $items=~s/\&$//;
 5541:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
 5542:                           $coursehome);
 5543:     }
 5544:     if ($outcome eq 'unknown_cmd') {
 5545:         my $what;
 5546:         foreach my $cid (keys(%$storehash)) {
 5547:             $what .= &escape($cid).'=';
 5548:             foreach my $item ('description','inst_code','owner','type') {
 5549:                 $what .= &escape($storehash->{$cid}{$item}).':';
 5550:             }
 5551:             $what =~ s/\:$/&/;
 5552:         }
 5553:         $what =~ s/\&$//;  
 5554:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
 5555:     } else {
 5556:         return $outcome;
 5557:     }
 5558: }
 5559: 
 5560: sub courseiddump {
 5561:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
 5562:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
 5563:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
 5564:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
 5565:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
 5566:     my $as_hash = 1;
 5567:     my %returnhash;
 5568:     if (!$domfilter) { $domfilter=''; }
 5569:     my %libserv = &all_library();
 5570:     foreach my $tryserver (keys(%libserv)) {
 5571:         if ( (  $hostidflag == 1 
 5572: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
 5573: 	     || (!defined($hostidflag)) ) {
 5574: 
 5575: 	    if (($domfilter eq '') ||
 5576: 		(&host_domain($tryserver) eq $domfilter)) {
 5577:                 my $rep;
 5578:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
 5579:                     $rep = LONCAPA::Lond::dump_course_id_handler(
 5580:                         join(":", (&host_domain($tryserver), $sincefilter, 
 5581:                                 &escape($descfilter), &escape($instcodefilter), 
 5582:                                 &escape($ownerfilter), &escape($coursefilter),
 5583:                                 &escape($typefilter), &escape($regexp_ok), 
 5584:                                 $as_hash, &escape($selfenrollonly), 
 5585:                                 &escape($catfilter), $showhidden, $caller, 
 5586:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
 5587:                                 &escape($createdbefore), &escape($createdafter), 
 5588:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
 5589:                                 $reqcrsdom,&escape($reqinstcode))));
 5590:                 } else {
 5591:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
 5592:                              $sincefilter.':'.&escape($descfilter).':'.
 5593:                              &escape($instcodefilter).':'.&escape($ownerfilter).
 5594:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
 5595:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
 5596:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
 5597:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
 5598:                              &escape($cc_clone).':'.$cloneonly.':'.
 5599:                              &escape($createdbefore).':'.&escape($createdafter).':'.
 5600:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
 5601:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
 5602:                 }
 5603:                      
 5604:                 my @pairs=split(/\&/,$rep);
 5605:                 foreach my $item (@pairs) {
 5606:                     my ($key,$value)=split(/\=/,$item,2);
 5607:                     $key = &unescape($key);
 5608:                     next if ($key =~ /^error: 2 /);
 5609:                     my $result = &thaw_unescape($value);
 5610:                     if (ref($result) eq 'HASH') {
 5611:                         $returnhash{$key}=$result;
 5612:                     } else {
 5613:                         my @responses = split(/:/,$value);
 5614:                         my @items = ('description','inst_code','owner','type');
 5615:                         for (my $i=0; $i<@responses; $i++) {
 5616:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
 5617:                         }
 5618:                     }
 5619:                 }
 5620:             }
 5621:         }
 5622:     }
 5623:     return %returnhash;
 5624: }
 5625: 
 5626: sub courselastaccess {
 5627:     my ($cdom,$cnum,$hostidref) = @_;
 5628:     my %returnhash;
 5629:     if ($cdom && $cnum) {
 5630:         my $chome = &homeserver($cnum,$cdom);
 5631:         if ($chome ne 'no_host') {
 5632:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
 5633:             &extract_lastaccess(\%returnhash,$rep);
 5634:         }
 5635:     } else {
 5636:         if (!$cdom) { $cdom=''; }
 5637:         my %libserv = &all_library();
 5638:         foreach my $tryserver (keys(%libserv)) {
 5639:             if (ref($hostidref) eq 'ARRAY') {
 5640:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
 5641:             } 
 5642:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
 5643:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
 5644:                 &extract_lastaccess(\%returnhash,$rep);
 5645:             }
 5646:         }
 5647:     }
 5648:     return %returnhash;
 5649: }
 5650: 
 5651: sub extract_lastaccess {
 5652:     my ($returnhash,$rep) = @_;
 5653:     if (ref($returnhash) eq 'HASH') {
 5654:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
 5655:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
 5656:                  $rep eq '') {
 5657:             my @pairs=split(/\&/,$rep);
 5658:             foreach my $item (@pairs) {
 5659:                 my ($key,$value)=split(/\=/,$item,2);
 5660:                 $key = &unescape($key);
 5661:                 next if ($key =~ /^error: 2 /);
 5662:                 $returnhash->{$key} = &thaw_unescape($value);
 5663:             }
 5664:         }
 5665:     }
 5666:     return;
 5667: }
 5668: 
 5669: # ---------------------------------------------------------- DC e-mail
 5670: 
 5671: sub dcmailput {
 5672:     my ($domain,$msgid,$message,$server)=@_;
 5673:     my $status = &Apache::lonnet::critical(
 5674:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
 5675:        &escape($message),$server);
 5676:     return $status;
 5677: }
 5678: 
 5679: sub dcmaildump {
 5680:     my ($dom,$startdate,$enddate,$senders) = @_;
 5681:     my %returnhash=();
 5682: 
 5683:     if (defined(&domain($dom,'primary'))) {
 5684:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
 5685:                                                          &escape($enddate).':';
 5686: 	my @esc_senders=map { &escape($_)} @$senders;
 5687: 	$cmd.=&escape(join('&',@esc_senders));
 5688: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
 5689:             my ($key,$value) = split(/\=/,$line,2);
 5690:             if (($key) && ($value)) {
 5691:                 $returnhash{&unescape($key)} = &unescape($value);
 5692:             }
 5693:         }
 5694:     }
 5695:     return %returnhash;
 5696: }
 5697: # ---------------------------------------------------------- Domain roles
 5698: 
 5699: sub get_domain_roles {
 5700:     my ($dom,$roles,$startdate,$enddate)=@_;
 5701:     if ((!defined($startdate)) || ($startdate eq '')) {
 5702:         $startdate = '.';
 5703:     }
 5704:     if ((!defined($enddate)) || ($enddate eq '')) {
 5705:         $enddate = '.';
 5706:     }
 5707:     my $rolelist;
 5708:     if (ref($roles) eq 'ARRAY') {
 5709:         $rolelist = join('&',@{$roles});
 5710:     }
 5711:     my %personnel = ();
 5712: 
 5713:     my %servers = &get_servers($dom,'library');
 5714:     foreach my $tryserver (keys(%servers)) {
 5715: 	%{$personnel{$tryserver}}=();
 5716: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
 5717: 					    &escape($startdate).':'.
 5718: 					    &escape($enddate).':'.
 5719: 					    &escape($rolelist), $tryserver))) {
 5720: 	    my ($key,$value) = split(/\=/,$line,2);
 5721: 	    if (($key) && ($value)) {
 5722: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
 5723: 	    }
 5724: 	}
 5725:     }
 5726:     return %personnel;
 5727: }
 5728: 
 5729: sub get_active_domroles {
 5730:     my ($dom,$roles) = @_;
 5731:     return () unless (ref($roles) eq 'ARRAY');
 5732:     my $now = time;
 5733:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
 5734:     my %domroles;
 5735:     foreach my $server (keys(%dompersonnel)) {
 5736:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 5737:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 5738:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 5739:         }
 5740:     }
 5741:     return %domroles;
 5742: }
 5743: 
 5744: # ----------------------------------------------------------- Interval timing 
 5745: 
 5746: {
 5747: # Caches needed for speedup of navmaps
 5748: # We don't want to cache this for very long at all (5 seconds at most)
 5749: # 
 5750: # The user for whom we cache
 5751: my $cachedkey='';
 5752: # The cached times for this user
 5753: my %cachedtimes=();
 5754: # When this was last done
 5755: my $cachedtime='';
 5756: 
 5757: sub load_all_first_access {
 5758:     my ($uname,$udom,$ignorecache)=@_;
 5759:     if (($cachedkey eq $uname.':'.$udom) &&
 5760:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
 5761:         (!$ignorecache)) {
 5762:         return;
 5763:     }
 5764:     $cachedtime=time;
 5765:     $cachedkey=$uname.':'.$udom;
 5766:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
 5767: }
 5768: 
 5769: sub get_first_access {
 5770:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
 5771:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5772:     if ($argsymb) { $symb=$argsymb; }
 5773:     my ($map,$id,$res)=&decode_symb($symb);
 5774:     if ($argmap) { $map = $argmap; }
 5775:     if ($type eq 'course') {
 5776: 	$res='course';
 5777:     } elsif ($type eq 'map') {
 5778: 	$res=&symbread($map);
 5779:     } else {
 5780: 	$res=$symb;
 5781:     }
 5782:     &load_all_first_access($uname,$udom,$ignorecache);
 5783:     return $cachedtimes{"$courseid\0$res"};
 5784: }
 5785: 
 5786: sub set_first_access {
 5787:     my ($type,$interval)=@_;
 5788:     my ($symb,$courseid,$udom,$uname)=&whichuser();
 5789:     my ($map,$id,$res)=&decode_symb($symb);
 5790:     if ($type eq 'course') {
 5791: 	$res='course';
 5792:     } elsif ($type eq 'map') {
 5793: 	$res=&symbread($map);
 5794:     } else {
 5795: 	$res=$symb;
 5796:     }
 5797:     $cachedkey='';
 5798:     my $firstaccess=&get_first_access($type,$symb,$map);
 5799:     if ($firstaccess) {
 5800:         &logthis("First access time already set ($firstaccess) when attempting ".
 5801:                  "to set new value (type: $type, extent: $res) for $uname:$udom ".
 5802:                  "in $courseid");
 5803:         return 'already_set';
 5804:     } else {
 5805:         my $start = time;
 5806: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
 5807:                           $udom,$uname);
 5808:         if ($putres eq 'ok') {
 5809:             &put('timerinterval',{"$courseid\0$res"=>$interval},
 5810:                  $udom,$uname); 
 5811:             &appenv(
 5812:                      {
 5813:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
 5814:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
 5815:                      }
 5816:                   );
 5817:             if (($cachedtime) && (abs($start-$cachedtime) < 5)) {
 5818:                 $cachedtimes{"$courseid\0$res"} = $start;
 5819:             }
 5820:         } elsif ($putres ne 'refused') {
 5821:             &logthis("Result: $putres when attempting to set first access time ".
 5822:                      "(type: $type, extent: $res) for $uname:$udom in $courseid");
 5823:         }
 5824:         return $putres;
 5825:     }
 5826:     return 'already_set';
 5827: }
 5828: }
 5829: 
 5830: # --------------------------------------------- Set Expire Date for Spreadsheet
 5831: 
 5832: sub expirespread {
 5833:     my ($uname,$udom,$stype,$usymb)=@_;
 5834:     my $cid=$env{'request.course.id'}; 
 5835:     if ($cid) {
 5836:        my $now=time;
 5837:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
 5838:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
 5839:                             $env{'course.'.$cid.'.num'}.
 5840: 	        	    ':nohist_expirationdates:'.
 5841:                             &escape($key).'='.$now,
 5842:                             $env{'course.'.$cid.'.home'})
 5843:     }
 5844:     return 'ok';
 5845: }
 5846: 
 5847: # ----------------------------------------------------- Devalidate Spreadsheets
 5848: 
 5849: sub devalidate {
 5850:     my ($symb,$uname,$udom)=@_;
 5851:     my $cid=$env{'request.course.id'}; 
 5852:     if ($cid) {
 5853:         # delete the stored spreadsheets for
 5854:         # - the student level sheet of this user in course's homespace
 5855:         # - the assessment level sheet for this resource 
 5856:         #   for this user in user's homespace
 5857: 	# - current conditional state info
 5858: 	my $key=$uname.':'.$udom.':';
 5859:         my $status=
 5860: 	    &del('nohist_calculatedsheets',
 5861: 		 [$key.'studentcalc:'],
 5862: 		 $env{'course.'.$cid.'.domain'},
 5863: 		 $env{'course.'.$cid.'.num'})
 5864: 		.' '.
 5865: 	    &del('nohist_calculatedsheets_'.$cid,
 5866: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
 5867:         unless ($status eq 'ok ok') {
 5868:            &logthis('Could not devalidate spreadsheet '.
 5869:                     $uname.' at '.$udom.' for '.
 5870: 		    $symb.': '.$status);
 5871:         }
 5872: 	&delenv('user.state.'.$cid);
 5873:     }
 5874: }
 5875: 
 5876: sub get_scalar {
 5877:     my ($string,$end) = @_;
 5878:     my $value;
 5879:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
 5880: 	$value = $1;
 5881:     } elsif ($$string =~ s/^([^&]*?)&//) {
 5882: 	$value = $1;
 5883:     }
 5884:     return &unescape($value);
 5885: }
 5886: 
 5887: sub array2str {
 5888:   my (@array) = @_;
 5889:   my $result=&arrayref2str(\@array);
 5890:   $result=~s/^__ARRAY_REF__//;
 5891:   $result=~s/__END_ARRAY_REF__$//;
 5892:   return $result;
 5893: }
 5894: 
 5895: sub arrayref2str {
 5896:   my ($arrayref) = @_;
 5897:   my $result='__ARRAY_REF__';
 5898:   foreach my $elem (@$arrayref) {
 5899:     if(ref($elem) eq 'ARRAY') {
 5900:       $result.=&arrayref2str($elem).'&';
 5901:     } elsif(ref($elem) eq 'HASH') {
 5902:       $result.=&hashref2str($elem).'&';
 5903:     } elsif(ref($elem)) {
 5904:       #print("Got a ref of ".(ref($elem))." skipping.");
 5905:     } else {
 5906:       $result.=&escape($elem).'&';
 5907:     }
 5908:   }
 5909:   $result=~s/\&$//;
 5910:   $result .= '__END_ARRAY_REF__';
 5911:   return $result;
 5912: }
 5913: 
 5914: sub hash2str {
 5915:   my (%hash) = @_;
 5916:   my $result=&hashref2str(\%hash);
 5917:   $result=~s/^__HASH_REF__//;
 5918:   $result=~s/__END_HASH_REF__$//;
 5919:   return $result;
 5920: }
 5921: 
 5922: sub hashref2str {
 5923:   my ($hashref)=@_;
 5924:   my $result='__HASH_REF__';
 5925:   foreach my $key (sort(keys(%$hashref))) {
 5926:     if (ref($key) eq 'ARRAY') {
 5927:       $result.=&arrayref2str($key).'=';
 5928:     } elsif (ref($key) eq 'HASH') {
 5929:       $result.=&hashref2str($key).'=';
 5930:     } elsif (ref($key)) {
 5931:       $result.='=';
 5932:       #print("Got a ref of ".(ref($key))." skipping.");
 5933:     } else {
 5934: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
 5935:     }
 5936: 
 5937:     if(ref($hashref->{$key}) eq 'ARRAY') {
 5938:       $result.=&arrayref2str($hashref->{$key}).'&';
 5939:     } elsif(ref($hashref->{$key}) eq 'HASH') {
 5940:       $result.=&hashref2str($hashref->{$key}).'&';
 5941:     } elsif(ref($hashref->{$key})) {
 5942:        $result.='&';
 5943:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
 5944:     } else {
 5945:       $result.=&escape($hashref->{$key}).'&';
 5946:     }
 5947:   }
 5948:   $result=~s/\&$//;
 5949:   $result .= '__END_HASH_REF__';
 5950:   return $result;
 5951: }
 5952: 
 5953: sub str2hash {
 5954:     my ($string)=@_;
 5955:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
 5956:     return %$hash;
 5957: }
 5958: 
 5959: sub str2hashref {
 5960:   my ($string) = @_;
 5961: 
 5962:   my %hash;
 5963: 
 5964:   if($string !~ /^__HASH_REF__/) {
 5965:       if (! ($string eq '' || !defined($string))) {
 5966: 	  $hash{'error'}='Not hash reference';
 5967:       }
 5968:       return (\%hash, $string);
 5969:   }
 5970: 
 5971:   $string =~ s/^__HASH_REF__//;
 5972: 
 5973:   while($string !~ /^__END_HASH_REF__/) {
 5974:       #key
 5975:       my $key='';
 5976:       if($string =~ /^__HASH_REF__/) {
 5977:           ($key, $string)=&str2hashref($string);
 5978:           if(defined($key->{'error'})) {
 5979:               $hash{'error'}='Bad data';
 5980:               return (\%hash, $string);
 5981:           }
 5982:       } elsif($string =~ /^__ARRAY_REF__/) {
 5983:           ($key, $string)=&str2arrayref($string);
 5984:           if($key->[0] eq 'Array reference error') {
 5985:               $hash{'error'}='Bad data';
 5986:               return (\%hash, $string);
 5987:           }
 5988:       } else {
 5989:           $string =~ s/^(.*?)=//;
 5990: 	  $key=&unescape($1);
 5991:       }
 5992:       $string =~ s/^=//;
 5993: 
 5994:       #value
 5995:       my $value='';
 5996:       if($string =~ /^__HASH_REF__/) {
 5997:           ($value, $string)=&str2hashref($string);
 5998:           if(defined($value->{'error'})) {
 5999:               $hash{'error'}='Bad data';
 6000:               return (\%hash, $string);
 6001:           }
 6002:       } elsif($string =~ /^__ARRAY_REF__/) {
 6003:           ($value, $string)=&str2arrayref($string);
 6004:           if($value->[0] eq 'Array reference error') {
 6005:               $hash{'error'}='Bad data';
 6006:               return (\%hash, $string);
 6007:           }
 6008:       } else {
 6009: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
 6010:       }
 6011:       $string =~ s/^&//;
 6012: 
 6013:       $hash{$key}=$value;
 6014:   }
 6015: 
 6016:   $string =~ s/^__END_HASH_REF__//;
 6017: 
 6018:   return (\%hash, $string);
 6019: }
 6020: 
 6021: sub str2array {
 6022:     my ($string)=@_;
 6023:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
 6024:     return @$array;
 6025: }
 6026: 
 6027: sub str2arrayref {
 6028:   my ($string) = @_;
 6029:   my @array;
 6030: 
 6031:   if($string !~ /^__ARRAY_REF__/) {
 6032:       if (! ($string eq '' || !defined($string))) {
 6033: 	  $array[0]='Array reference error';
 6034:       }
 6035:       return (\@array, $string);
 6036:   }
 6037: 
 6038:   $string =~ s/^__ARRAY_REF__//;
 6039: 
 6040:   while($string !~ /^__END_ARRAY_REF__/) {
 6041:       my $value='';
 6042:       if($string =~ /^__HASH_REF__/) {
 6043:           ($value, $string)=&str2hashref($string);
 6044:           if(defined($value->{'error'})) {
 6045:               $array[0] ='Array reference error';
 6046:               return (\@array, $string);
 6047:           }
 6048:       } elsif($string =~ /^__ARRAY_REF__/) {
 6049:           ($value, $string)=&str2arrayref($string);
 6050:           if($value->[0] eq 'Array reference error') {
 6051:               $array[0] ='Array reference error';
 6052:               return (\@array, $string);
 6053:           }
 6054:       } else {
 6055: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
 6056:       }
 6057:       $string =~ s/^&//;
 6058: 
 6059:       push(@array, $value);
 6060:   }
 6061: 
 6062:   $string =~ s/^__END_ARRAY_REF__//;
 6063: 
 6064:   return (\@array, $string);
 6065: }
 6066: 
 6067: # -------------------------------------------------------------------Temp Store
 6068: 
 6069: sub tmpreset {
 6070:   my ($symb,$namespace,$domain,$stuname) = @_;
 6071:   if (!$symb) {
 6072:     $symb=&symbread();
 6073:     if (!$symb) { $symb= $env{'request.url'}; }
 6074:   }
 6075:   $symb=escape($symb);
 6076: 
 6077:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6078:   $namespace=~s/\//\_/g;
 6079:   $namespace=~s/\W//g;
 6080: 
 6081:   if (!$domain) { $domain=$env{'user.domain'}; }
 6082:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6083:   if ($domain eq 'public' && $stuname eq 'public') {
 6084:       $stuname=$ENV{'REMOTE_ADDR'};
 6085:   }
 6086:   my $path=LONCAPA::tempdir();
 6087:   my %hash;
 6088:   if (tie(%hash,'GDBM_File',
 6089: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6090: 	  &GDBM_WRCREAT(),0640)) {
 6091:     foreach my $key (keys(%hash)) {
 6092:       if ($key=~ /:$symb/) {
 6093: 	delete($hash{$key});
 6094:       }
 6095:     }
 6096:   }
 6097: }
 6098: 
 6099: sub tmpstore {
 6100:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
 6101: 
 6102:   if (!$symb) {
 6103:     $symb=&symbread();
 6104:     if (!$symb) { $symb= $env{'request.url'}; }
 6105:   }
 6106:   $symb=escape($symb);
 6107: 
 6108:   if (!$namespace) {
 6109:     # I don't think we would ever want to store this for a course.
 6110:     # it seems this will only be used if we don't have a course.
 6111:     #$namespace=$env{'request.course.id'};
 6112:     #if (!$namespace) {
 6113:       $namespace=$env{'request.state'};
 6114:     #}
 6115:   }
 6116:   $namespace=~s/\//\_/g;
 6117:   $namespace=~s/\W//g;
 6118:   if (!$domain) { $domain=$env{'user.domain'}; }
 6119:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6120:   if ($domain eq 'public' && $stuname eq 'public') {
 6121:       $stuname=$ENV{'REMOTE_ADDR'};
 6122:   }
 6123:   my $now=time;
 6124:   my %hash;
 6125:   my $path=LONCAPA::tempdir();
 6126:   if (tie(%hash,'GDBM_File',
 6127: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6128: 	  &GDBM_WRCREAT(),0640)) {
 6129:     $hash{"version:$symb"}++;
 6130:     my $version=$hash{"version:$symb"};
 6131:     my $allkeys=''; 
 6132:     foreach my $key (keys(%$storehash)) {
 6133:       $allkeys.=$key.':';
 6134:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
 6135:     }
 6136:     $hash{"$version:$symb:timestamp"}=$now;
 6137:     $allkeys.='timestamp';
 6138:     $hash{"$version:keys:$symb"}=$allkeys;
 6139:     if (untie(%hash)) {
 6140:       return 'ok';
 6141:     } else {
 6142:       return "error:$!";
 6143:     }
 6144:   } else {
 6145:     return "error:$!";
 6146:   }
 6147: }
 6148: 
 6149: # -----------------------------------------------------------------Temp Restore
 6150: 
 6151: sub tmprestore {
 6152:   my ($symb,$namespace,$domain,$stuname) = @_;
 6153: 
 6154:   if (!$symb) {
 6155:     $symb=&symbread();
 6156:     if (!$symb) { $symb= $env{'request.url'}; }
 6157:   }
 6158:   $symb=escape($symb);
 6159: 
 6160:   if (!$namespace) { $namespace=$env{'request.state'}; }
 6161: 
 6162:   if (!$domain) { $domain=$env{'user.domain'}; }
 6163:   if (!$stuname) { $stuname=$env{'user.name'}; }
 6164:   if ($domain eq 'public' && $stuname eq 'public') {
 6165:       $stuname=$ENV{'REMOTE_ADDR'};
 6166:   }
 6167:   my %returnhash;
 6168:   $namespace=~s/\//\_/g;
 6169:   $namespace=~s/\W//g;
 6170:   my %hash;
 6171:   my $path=LONCAPA::tempdir();
 6172:   if (tie(%hash,'GDBM_File',
 6173: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
 6174: 	  &GDBM_READER(),0640)) {
 6175:     my $version=$hash{"version:$symb"};
 6176:     $returnhash{'version'}=$version;
 6177:     my $scope;
 6178:     for ($scope=1;$scope<=$version;$scope++) {
 6179:       my $vkeys=$hash{"$scope:keys:$symb"};
 6180:       my @keys=split(/:/,$vkeys);
 6181:       my $key;
 6182:       $returnhash{"$scope:keys"}=$vkeys;
 6183:       foreach $key (@keys) {
 6184: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6185: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
 6186:       }
 6187:     }
 6188:     if (!(untie(%hash))) {
 6189:       return "error:$!";
 6190:     }
 6191:   } else {
 6192:     return "error:$!";
 6193:   }
 6194:   return %returnhash;
 6195: }
 6196: 
 6197: # ----------------------------------------------------------------------- Store
 6198: 
 6199: sub store {
 6200:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6201:     my $home='';
 6202: 
 6203:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6204: 
 6205:     $symb=&symbclean($symb);
 6206:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6207: 
 6208:     if (!$domain) { $domain=$env{'user.domain'}; }
 6209:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6210: 
 6211:     &devalidate($symb,$stuname,$domain);
 6212: 
 6213:     $symb=escape($symb);
 6214:     if (!$namespace) { 
 6215:        unless ($namespace=$env{'request.course.id'}) { 
 6216:           return ''; 
 6217:        } 
 6218:     }
 6219:     if (!$home) { $home=$env{'user.home'}; }
 6220: 
 6221:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6222:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6223: 
 6224:     my $namevalue='';
 6225:     foreach my $key (keys(%$storehash)) {
 6226:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6227:     }
 6228:     $namevalue=~s/\&$//;
 6229:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
 6230:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6231: }
 6232: 
 6233: # -------------------------------------------------------------- Critical Store
 6234: 
 6235: sub cstore {
 6236:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
 6237:     my $home='';
 6238: 
 6239:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6240: 
 6241:     $symb=&symbclean($symb);
 6242:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
 6243: 
 6244:     if (!$domain) { $domain=$env{'user.domain'}; }
 6245:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6246: 
 6247:     &devalidate($symb,$stuname,$domain);
 6248: 
 6249:     $symb=escape($symb);
 6250:     if (!$namespace) { 
 6251:        unless ($namespace=$env{'request.course.id'}) { 
 6252:           return ''; 
 6253:        } 
 6254:     }
 6255:     if (!$home) { $home=$env{'user.home'}; }
 6256: 
 6257:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
 6258:     $$storehash{'host'}=$perlvar{'lonHostID'};
 6259: 
 6260:     my $namevalue='';
 6261:     foreach my $key (keys(%$storehash)) {
 6262:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 6263:     }
 6264:     $namevalue=~s/\&$//;
 6265:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
 6266:     return critical
 6267:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
 6268: }
 6269: 
 6270: # --------------------------------------------------------------------- Restore
 6271: 
 6272: sub restore {
 6273:     my ($symb,$namespace,$domain,$stuname) = @_;
 6274:     my $home='';
 6275: 
 6276:     if ($stuname) { $home=&homeserver($stuname,$domain); }
 6277: 
 6278:     if (!$symb) {
 6279:         return if ($namespace eq 'courserequests');
 6280:         unless ($symb=escape(&symbread())) { return ''; }
 6281:     } else {
 6282:         unless ($namespace eq 'courserequests') {
 6283:             $symb=&escape(&symbclean($symb));
 6284:         }
 6285:     }
 6286:     if (!$namespace) { 
 6287:        unless ($namespace=$env{'request.course.id'}) { 
 6288:           return ''; 
 6289:        } 
 6290:     }
 6291:     if (!$domain) { $domain=$env{'user.domain'}; }
 6292:     if (!$stuname) { $stuname=$env{'user.name'}; }
 6293:     if (!$home) { $home=$env{'user.home'}; }
 6294:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
 6295: 
 6296:     my %returnhash=();
 6297:     foreach my $line (split(/\&/,$answer)) {
 6298: 	my ($name,$value)=split(/\=/,$line);
 6299:         $returnhash{&unescape($name)}=&thaw_unescape($value);
 6300:     }
 6301:     my $version;
 6302:     for ($version=1;$version<=$returnhash{'version'};$version++) {
 6303:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
 6304:           $returnhash{$item}=$returnhash{$version.':'.$item};
 6305:        }
 6306:     }
 6307:     return %returnhash;
 6308: }
 6309: 
 6310: # ---------------------------------------------------------- Course Description
 6311: #
 6312: #  
 6313: 
 6314: sub coursedescription {
 6315:     my ($courseid,$args)=@_;
 6316:     $courseid=~s/^\///;
 6317:     $courseid=~s/\_/\//g;
 6318:     my ($cdomain,$cnum)=split(/\//,$courseid);
 6319:     my $chome=&homeserver($cnum,$cdomain);
 6320:     my $normalid=$cdomain.'_'.$cnum;
 6321:     # need to always cache even if we get errors otherwise we keep 
 6322:     # trying and trying and trying to get the course description.
 6323:     my %envhash=();
 6324:     my %returnhash=();
 6325:     
 6326:     my $expiretime=600;
 6327:     if ($env{'request.course.id'} eq $normalid) {
 6328: 	$expiretime=120;
 6329:     }
 6330: 
 6331:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
 6332:     if (!$args->{'freshen_cache'}
 6333: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
 6334: 	foreach my $key (keys(%env)) {
 6335: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
 6336: 	    my ($setting) = $1;
 6337: 	    $returnhash{$setting} = $env{$key};
 6338: 	}
 6339: 	return %returnhash;
 6340:     }
 6341: 
 6342:     # get the data again
 6343: 
 6344:     if (!$args->{'one_time'}) {
 6345: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
 6346:     }
 6347: 
 6348:     if ($chome ne 'no_host') {
 6349:        %returnhash=&dump('environment',$cdomain,$cnum);
 6350:        if (!exists($returnhash{'con_lost'})) {
 6351: 	   my $username = $env{'user.name'}; # Defult username
 6352: 	   if(defined $args->{'user'}) {
 6353: 	       $username = $args->{'user'};
 6354: 	   }
 6355:            $returnhash{'home'}= $chome;
 6356: 	   $returnhash{'domain'} = $cdomain;
 6357: 	   $returnhash{'num'} = $cnum;
 6358:            if (!defined($returnhash{'type'})) {
 6359:                $returnhash{'type'} = 'Course';
 6360:            }
 6361:            while (my ($name,$value) = each %returnhash) {
 6362:                $envhash{'course.'.$normalid.'.'.$name}=$value;
 6363:            }
 6364:            $returnhash{'url'}=&clutter($returnhash{'url'});
 6365:            $returnhash{'fn'}=LONCAPA::tempdir() .
 6366: 	       $username.'_'.$cdomain.'_'.$cnum;
 6367:            $envhash{'course.'.$normalid.'.home'}=$chome;
 6368:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
 6369:            $envhash{'course.'.$normalid.'.num'}=$cnum;
 6370:        }
 6371:     }
 6372:     if (!$args->{'one_time'}) {
 6373: 	&appenv(\%envhash);
 6374:     }
 6375:     return %returnhash;
 6376: }
 6377: 
 6378: sub update_released_required {
 6379:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
 6380:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
 6381:         $cid = $env{'request.course.id'};
 6382:         $cdom = $env{'course.'.$cid.'.domain'};
 6383:         $cnum = $env{'course.'.$cid.'.num'};
 6384:         $chome = $env{'course.'.$cid.'.home'};
 6385:     }
 6386:     if ($needsrelease) {
 6387:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
 6388:         my $needsupdate;
 6389:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
 6390:             $needsupdate = 1;
 6391:         } else {
 6392:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
 6393:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
 6394:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
 6395:                 $needsupdate = 1;
 6396:             }
 6397:         }
 6398:         if ($needsupdate) {
 6399:             my %needshash = (
 6400:                              'internal.releaserequired' => $needsrelease,
 6401:                             );
 6402:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
 6403:             if ($putresult eq 'ok') {
 6404:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
 6405:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
 6406:                 if (ref($crsinfo{$cid}) eq 'HASH') {
 6407:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
 6408:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
 6409:                 }
 6410:             }
 6411:         }
 6412:     }
 6413:     return;
 6414: }
 6415: 
 6416: # -------------------------------------------------See if a user is privileged
 6417: 
 6418: sub privileged {
 6419:     my ($username,$domain,$possdomains,$possroles)=@_;
 6420:     my $now = time;
 6421:     my $roles;
 6422:     if (ref($possroles) eq 'ARRAY') {
 6423:         $roles = $possroles; 
 6424:     } else {
 6425:         $roles = ['dc','su'];
 6426:     }
 6427:     if (ref($possdomains) eq 'ARRAY') {
 6428:         my %privileged = &privileged_by_domain($possdomains,$roles);
 6429:         foreach my $dom (@{$possdomains}) {
 6430:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
 6431:                 (ref($privileged{$dom}) eq 'HASH')) {
 6432:                 foreach my $role (@{$roles}) {
 6433:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6434:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
 6435:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
 6436:                             return 1 unless (($end && $end < $now) ||
 6437:                                              ($start && $start > $now));
 6438:                         }
 6439:                     }
 6440:                 }
 6441:             }
 6442:         }
 6443:     } else {
 6444:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
 6445:         my $now = time;
 6446: 
 6447:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
 6448:             my ($trole, $tend, $tstart) = split(/_/, $role);
 6449:             if (grep(/^\Q$trole\E$/,@{$roles})) {
 6450:                 return 1 unless ($tend && $tend < $now) 
 6451:                         or ($tstart && $tstart > $now);
 6452:             }
 6453:         }
 6454:     }
 6455:     return 0;
 6456: }
 6457: 
 6458: sub privileged_by_domain {
 6459:     my ($domains,$roles) = @_;
 6460:     my %privileged = ();
 6461:     my $cachetime = 60*60*24;
 6462:     my $now = time;
 6463:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
 6464:         return %privileged;
 6465:     }
 6466:     foreach my $dom (@{$domains}) {
 6467:         next if (ref($privileged{$dom}) eq 'HASH');
 6468:         my $needroles;
 6469:         foreach my $role (@{$roles}) {
 6470:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
 6471:             if (defined($cached)) {
 6472:                 if (ref($result) eq 'HASH') {
 6473:                     $privileged{$dom}{$role} = $result;
 6474:                 }
 6475:             } else {
 6476:                 $needroles = 1;
 6477:             }
 6478:         }
 6479:         if ($needroles) {
 6480:             my %dompersonnel = &get_domain_roles($dom,$roles);
 6481:             $privileged{$dom} = {};
 6482:             foreach my $server (keys(%dompersonnel)) {
 6483:                 if (ref($dompersonnel{$server}) eq 'HASH') {
 6484:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
 6485:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
 6486:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
 6487:                         next if ($end && $end < $now);
 6488:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
 6489:                             $dompersonnel{$server}{$item};
 6490:                     }
 6491:                 }
 6492:             }
 6493:             if (ref($privileged{$dom}) eq 'HASH') {
 6494:                 foreach my $role (@{$roles}) {
 6495:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
 6496:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
 6497:                     } else {
 6498:                         my %hash = ();
 6499:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
 6500:                     }
 6501:                 }
 6502:             }
 6503:         }
 6504:     }
 6505:     return %privileged;
 6506: }
 6507: 
 6508: # -------------------------------------------------------- Get user privileges
 6509: 
 6510: sub rolesinit {
 6511:     my ($domain, $username) = @_;
 6512:     my %userroles = ('user.login.time' => time);
 6513:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
 6514: 
 6515:     # firstaccess and timerinterval are related to timed maps/resources. 
 6516:     # also, blocking can be triggered by an activating timer
 6517:     # it's saved in the user's %env.
 6518:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
 6519:     my %timerinterval = &dump('timerinterval', $domain, $username);
 6520:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
 6521:         %timerintchk, %timerintenv);
 6522: 
 6523:     foreach my $key (keys(%firstaccess)) {
 6524:         my ($cid, $rest) = split(/\0/, $key);
 6525:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
 6526:     }
 6527: 
 6528:     foreach my $key (keys(%timerinterval)) {
 6529:         my ($cid,$rest) = split(/\0/,$key);
 6530:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
 6531:     }
 6532: 
 6533:     my %allroles=();
 6534:     my %allgroups=();
 6535: 
 6536:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
 6537:         my $role = $rolesdump{$area};
 6538:         $area =~ s/\_\w\w$//;
 6539: 
 6540:         my ($trole, $tend, $tstart, $group_privs);
 6541: 
 6542:         if ($role =~ /^cr/) {
 6543:         # Custom role, defined by a user 
 6544:         # e.g., user.role.cr/msu/smith/mynewrole
 6545:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
 6546:                 $trole = $1;
 6547:                 ($tend, $tstart) = split('_', $2);
 6548:             } else {
 6549:                 $trole = $role;
 6550:             }
 6551:         } elsif ($role =~ m|^gr/|) {
 6552:         # Role of member in a group, defined within a course/community
 6553:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
 6554:             ($trole, $tend, $tstart) = split(/_/, $role);
 6555:             next if $tstart eq '-1';
 6556:             ($trole, $group_privs) = split(/\//, $trole);
 6557:             $group_privs = &unescape($group_privs);
 6558:         } else {
 6559:         # Just a normal role, defined in roles.tab
 6560:             ($trole, $tend, $tstart) = split(/_/,$role);
 6561:         }
 6562: 
 6563:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
 6564:                  $username);
 6565:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
 6566: 
 6567:         # role expired or not available yet?
 6568:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
 6569:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
 6570: 
 6571:         next if $area eq '' or $trole eq '';
 6572: 
 6573:         my $spec = "$trole.$area";
 6574:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
 6575: 
 6576:         if ($trole =~ /^cr\//) {
 6577:         # Custom role, defined by a user
 6578:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 6579:         } elsif ($trole eq 'gr') {
 6580:         # Role of a member in a group, defined within a course/community
 6581:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
 6582:             next;
 6583:         } else {
 6584:         # Normal role, defined in roles.tab
 6585:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 6586:         }
 6587: 
 6588:         my $cid = $tdomain.'_'.$trest;
 6589:         unless ($firstaccchk{$cid}) {
 6590:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
 6591:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
 6592:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
 6593:                         $coursetimerstarts{$cid}{$item}; 
 6594:                 }
 6595:             }
 6596:             $firstaccchk{$cid} = 1;
 6597:         }
 6598:         unless ($timerintchk{$cid}) {
 6599:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
 6600:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
 6601:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
 6602:                        $coursetimerintervals{$cid}{$item};
 6603:                 }
 6604:             }
 6605:             $timerintchk{$cid} = 1;
 6606:         }
 6607:     }
 6608: 
 6609:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
 6610:                                                           \%allroles, \%allgroups);
 6611:     $env{'user.adv'} = $userroles{'user.adv'};
 6612:     $env{'user.rar'} = $userroles{'user.rar'};
 6613: 
 6614:     return (\%userroles,\%firstaccenv,\%timerintenv);
 6615: }
 6616: 
 6617: sub set_arearole {
 6618:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
 6619:     unless ($nolog) {
 6620: # log the associated role with the area
 6621:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
 6622:     }
 6623:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
 6624: }
 6625: 
 6626: sub custom_roleprivs {
 6627:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
 6628:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
 6629:     my $homsvr = &homeserver($rauthor,$rdomain);
 6630:     if (&hostname($homsvr) ne '') {
 6631:         my ($rdummy,$roledef)=
 6632:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
 6633:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
 6634:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
 6635:             if (defined($syspriv)) {
 6636:                 if ($trest =~ /^$match_community$/) {
 6637:                     $syspriv =~ s/bre\&S//; 
 6638:                 }
 6639:                 $$allroles{'cm./'}.=':'.$syspriv;
 6640:                 $$allroles{$spec.'./'}.=':'.$syspriv;
 6641:             }
 6642:             if ($tdomain ne '') {
 6643:                 if (defined($dompriv)) {
 6644:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
 6645:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
 6646:                 }
 6647:                 if (($trest ne '') && (defined($coursepriv))) {
 6648:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
 6649:                         my $rolename = $1;
 6650:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
 6651:                     }
 6652:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
 6653:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
 6654:                 }
 6655:             }
 6656:         }
 6657:     }
 6658: }
 6659: 
 6660: sub course_adhocrole_privs {
 6661:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
 6662:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
 6663:     if ($overrides{"internal.adhocpriv.$rolename"}) {
 6664:         my (%currprivs,%storeprivs);
 6665:         foreach my $item (split(/:/,$coursepriv)) {
 6666:             my ($priv,$restrict) = split(/\&/,$item);
 6667:             $currprivs{$priv} = $restrict;
 6668:         }
 6669:         my (%possadd,%possremove,%full);
 6670:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
 6671:             my ($priv,$restrict)=split(/\&/,$item);
 6672:             $full{$priv} = $restrict;
 6673:         }
 6674:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
 6675:              next if ($item eq '');
 6676:              my ($rule,$rest) = split(/=/,$item);
 6677:              next unless (($rule eq 'off') || ($rule eq 'on'));
 6678:              foreach my $priv (split(/:/,$rest)) {
 6679:                  if ($priv ne '') {
 6680:                      if ($rule eq 'off') {
 6681:                          $possremove{$priv} = 1;
 6682:                      } else {
 6683:                          $possadd{$priv} = 1;
 6684:                      }
 6685:                  }
 6686:              }
 6687:          }
 6688:          foreach my $priv (sort(keys(%full))) {
 6689:              if (exists($currprivs{$priv})) {
 6690:                  unless (exists($possremove{$priv})) {
 6691:                      $storeprivs{$priv} = $currprivs{$priv};
 6692:                  }
 6693:              } elsif (exists($possadd{$priv})) {
 6694:                  $storeprivs{$priv} = $full{$priv};
 6695:              }
 6696:          }
 6697:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
 6698:      }
 6699:      return $coursepriv;
 6700: }
 6701: 
 6702: sub group_roleprivs {
 6703:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
 6704:     my $access = 1;
 6705:     my $now = time;
 6706:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
 6707:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
 6708:     if ($access) {
 6709:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
 6710:         $$allgroups{$course}{$group} .=':'.$group_privs;
 6711:     }
 6712: }
 6713: 
 6714: sub standard_roleprivs {
 6715:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
 6716:     if (defined($pr{$trole.':s'})) {
 6717:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
 6718:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
 6719:     }
 6720:     if ($tdomain ne '') {
 6721:         if (defined($pr{$trole.':d'})) {
 6722:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6723:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
 6724:         }
 6725:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
 6726:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
 6727:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
 6728:         }
 6729:     }
 6730: }
 6731: 
 6732: sub set_userprivs {
 6733:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
 6734:     my $author=0;
 6735:     my $adv=0;
 6736:     my $rar=0;
 6737:     my %grouproles = ();
 6738:     if (keys(%{$allgroups}) > 0) {
 6739:         my @groupkeys; 
 6740:         foreach my $role (keys(%{$allroles})) {
 6741:             push(@groupkeys,$role);
 6742:         }
 6743:         if (ref($groups_roles) eq 'HASH') {
 6744:             foreach my $key (keys(%{$groups_roles})) {
 6745:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
 6746:                     push(@groupkeys,$key);
 6747:                 }
 6748:             }
 6749:         }
 6750:         if (@groupkeys > 0) {
 6751:             foreach my $role (@groupkeys) {
 6752:                 my ($trole,$area,$sec,$extendedarea);
 6753:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
 6754:                     $trole = $1;
 6755:                     $area = $2;
 6756:                     $sec = $3;
 6757:                     $extendedarea = $area.$sec;
 6758:                     if (exists($$allgroups{$area})) {
 6759:                         foreach my $group (keys(%{$$allgroups{$area}})) {
 6760:                             my $spec = $trole.'.'.$extendedarea;
 6761:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
 6762:                                                 $$allgroups{$area}{$group};
 6763:                         }
 6764:                     }
 6765:                 }
 6766:             }
 6767:         }
 6768:     }
 6769:     foreach my $group (keys(%grouproles)) {
 6770:         $$allroles{$group} = $grouproles{$group};
 6771:     }
 6772:     foreach my $role (keys(%{$allroles})) {
 6773:         my %thesepriv;
 6774:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
 6775:         foreach my $item (split(/:/,$$allroles{$role})) {
 6776:             if ($item ne '') {
 6777:                 my ($privilege,$restrictions)=split(/&/,$item);
 6778:                 if ($restrictions eq '') {
 6779:                     $thesepriv{$privilege}='F';
 6780:                 } elsif ($thesepriv{$privilege} ne 'F') {
 6781:                     $thesepriv{$privilege}.=$restrictions;
 6782:                 }
 6783:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
 6784:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
 6785:             }
 6786:         }
 6787:         my $thesestr='';
 6788:         foreach my $priv (sort(keys(%thesepriv))) {
 6789: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
 6790: 	}
 6791:         $userroles->{'user.priv.'.$role} = $thesestr;
 6792:     }
 6793:     return ($author,$adv,$rar);
 6794: }
 6795: 
 6796: sub role_status {
 6797:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
 6798:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
 6799:         my ($one,$two) = split(m{\./},$rolekey,2);
 6800:         (undef,undef,$$role) = split(/\./,$one,3);
 6801:         unless (!defined($$role) || $$role eq '') {
 6802:             $$where = '/'.$two;
 6803:             $$trolecode=$$role.'.'.$$where;
 6804:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
 6805:             $$tstatus='is';
 6806:             if ($$tstart && $$tstart>$update) {
 6807:                 $$tstatus='future';
 6808:                 if ($$tstart<$now) {
 6809:                     if ($$tstart && $$tstart>$refresh) {
 6810:                         if (($$where ne '') && ($$role ne '')) {
 6811:                             my (%allroles,%allgroups,$group_privs,
 6812:                                 %groups_roles,@rolecodes);
 6813:                             my %userroles = (
 6814:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
 6815:                             );
 6816:                             @rolecodes = ('cm'); 
 6817:                             my $spec=$$role.'.'.$$where;
 6818:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
 6819:                             if ($$role =~ /^cr\//) {
 6820:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
 6821:                                 push(@rolecodes,'cr');
 6822:                             } elsif ($$role eq 'gr') {
 6823:                                 push(@rolecodes,$$role);
 6824:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
 6825:                                                     $env{'user.name'});
 6826:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
 6827:                                 (undef,my $group_privs) = split(/\//,$trole);
 6828:                                 $group_privs = &unescape($group_privs);
 6829:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
 6830:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
 6831:                                 &get_groups_roles($tdomain,$trest,
 6832:                                                   \%course_roles,\@rolecodes,
 6833:                                                   \%groups_roles);
 6834:                             } else {
 6835:                                 push(@rolecodes,$$role);
 6836:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
 6837:                             }
 6838:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
 6839:                                                                    \%groups_roles);
 6840:                             &appenv(\%userroles,\@rolecodes);
 6841:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6842:                         }
 6843:                     }
 6844:                     $$tstatus = 'is';
 6845:                 }
 6846:             }
 6847:             if ($$tend) {
 6848:                 if ($$tend<$update) {
 6849:                     $$tstatus='expired';
 6850:                 } elsif ($$tend<$now) {
 6851:                     $$tstatus='will_not';
 6852:                 }
 6853:             }
 6854:         }
 6855:     }
 6856: }
 6857: 
 6858: sub get_groups_roles {
 6859:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
 6860:     return unless((ref($cdom_courseroles) eq 'HASH') && 
 6861:                   (ref($rolecodes) eq 'ARRAY') && 
 6862:                   (ref($groups_roles) eq 'HASH')); 
 6863:     if (keys(%{$cdom_courseroles}) > 0) {
 6864:         my ($cnum) = ($rest =~ /^($match_courseid)/);
 6865:         if ($cdom ne '' && $cnum ne '') {
 6866:             foreach my $key (keys(%{$cdom_courseroles})) {
 6867:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
 6868:                     my $crsrole = $1;
 6869:                     my $crssec = $2;
 6870:                     if ($crsrole =~ /^cr/) {
 6871:                         unless (grep(/^cr$/,@{$rolecodes})) {
 6872:                             push(@{$rolecodes},'cr');
 6873:                         }
 6874:                     } else {
 6875:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
 6876:                             push(@{$rolecodes},$crsrole);
 6877:                         }
 6878:                     }
 6879:                     my $rolekey = "$crsrole./$cdom/$cnum";
 6880:                     if ($crssec ne '') {
 6881:                         $rolekey .= "/$crssec";
 6882:                     }
 6883:                     $rolekey .= './';
 6884:                     $groups_roles->{$rolekey} = $rolecodes;
 6885:                 }
 6886:             }
 6887:         }
 6888:     }
 6889:     return;
 6890: }
 6891: 
 6892: sub delete_env_groupprivs {
 6893:     my ($where,$courseroles,$possroles) = @_;
 6894:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
 6895:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
 6896:     unless (ref($courseroles->{$udom}) eq 'HASH') {
 6897:         %{$courseroles->{$udom}} =
 6898:             &get_my_roles('','','userroles',['active'],
 6899:                           $possroles,[$udom],1);
 6900:     }
 6901:     if (ref($courseroles->{$udom}) eq 'HASH') {
 6902:         foreach my $item (keys(%{$courseroles->{$udom}})) {
 6903:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
 6904:             my $area = '/'.$cdom.'/'.$cnum;
 6905:             my $privkey = "user.priv.$crsrole.$area";
 6906:             if ($crssec ne '') {
 6907:                 $privkey .= '/'.$crssec;
 6908:             }
 6909:             $privkey .= ".$area/$group";
 6910:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
 6911:         }
 6912:     }
 6913:     return;
 6914: }
 6915: 
 6916: sub check_adhoc_privs {
 6917:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
 6918:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
 6919:     if ($sec) {
 6920:         $cckey .= '/'.$sec;
 6921:     } 
 6922:     my $setprivs;
 6923:     if ($env{$cckey}) {
 6924:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
 6925:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
 6926:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
 6927:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6928:             $setprivs = 1;
 6929:         }
 6930:     } else {
 6931:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
 6932:         $setprivs = 1;
 6933:     }
 6934:     return $setprivs;
 6935: }
 6936: 
 6937: sub set_adhoc_privileges {
 6938: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
 6939:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
 6940:     my $area = '/'.$dcdom.'/'.$pickedcourse;
 6941:     if ($sec ne '') {
 6942:         $area .= '/'.$sec;
 6943:     }
 6944:     my $spec = $role.'.'.$area;
 6945:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
 6946:                                   $env{'user.name'},1);
 6947:     my %rolehash = ();
 6948:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
 6949:         my $rolename = $1;
 6950:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
 6951:         my %domdef = &get_domain_defaults($dcdom);
 6952:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
 6953:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
 6954:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
 6955:             }
 6956:         }
 6957:     } else {
 6958:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
 6959:     }
 6960:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
 6961:     &appenv(\%userroles,[$role,'cm']);
 6962:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
 6963:     unless (($caller eq 'constructaccess' && $env{'request.course.id'}) ||
 6964:             ($caller eq 'tiny')) {
 6965:         &appenv( {'request.role'        => $spec,
 6966:                   'request.role.domain' => $dcdom,
 6967:                   'request.course.sec'  => $sec,
 6968:                  }
 6969:                );
 6970:         my $tadv=0;
 6971:         if (&allowed('adv') eq 'F') { $tadv=1; }
 6972:         &appenv({'request.role.adv'    => $tadv});
 6973:     }
 6974: }
 6975: 
 6976: # --------------------------------------------------------------- get interface
 6977: 
 6978: sub get {
 6979:    my ($namespace,$storearr,$udomain,$uname)=@_;
 6980:    my $items='';
 6981:    foreach my $item (@$storearr) {
 6982:        $items.=&escape($item).'&';
 6983:    }
 6984:    $items=~s/\&$//;
 6985:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 6986:    if (!$uname) { $uname=$env{'user.name'}; }
 6987:    my $uhome=&homeserver($uname,$udomain);
 6988: 
 6989:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
 6990:    my @pairs=split(/\&/,$rep);
 6991:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
 6992:      return @pairs;
 6993:    }
 6994:    my %returnhash=();
 6995:    my $i=0;
 6996:    foreach my $item (@$storearr) {
 6997:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 6998:       $i++;
 6999:    }
 7000:    return %returnhash;
 7001: }
 7002: 
 7003: # --------------------------------------------------------------- del interface
 7004: 
 7005: sub del {
 7006:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7007:    my $items='';
 7008:    foreach my $item (@$storearr) {
 7009:        $items.=&escape($item).'&';
 7010:    }
 7011: 
 7012:    $items=~s/\&$//;
 7013:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7014:    if (!$uname) { $uname=$env{'user.name'}; }
 7015:    my $uhome=&homeserver($uname,$udomain);
 7016:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
 7017: }
 7018: 
 7019: # -------------------------------------------------------------- dump interface
 7020: 
 7021: sub unserialize {
 7022:     my ($rep, $escapedkeys) = @_;
 7023: 
 7024:     return {} if $rep =~ /^error/;
 7025: 
 7026:     my %returnhash=();
 7027: 	foreach my $item (split(/\&/,$rep)) {
 7028: 	    my ($key, $value) = split(/=/, $item, 2);
 7029: 	    $key = unescape($key) unless $escapedkeys;
 7030: 	    next if $key =~ /^error: 2 /;
 7031: 	    $returnhash{$key} = &thaw_unescape($value);
 7032: 	}
 7033:     #return %returnhash;
 7034:     return \%returnhash;
 7035: }        
 7036: 
 7037: # see Lond::dump_with_regexp
 7038: # if $escapedkeys hash keys won't get unescaped.
 7039: sub dump {
 7040:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
 7041:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7042:     if (!$uname) { $uname=$env{'user.name'}; }
 7043:     my $uhome=&homeserver($uname,$udomain);
 7044: 
 7045:     if ($regexp) {
 7046:         $regexp=&escape($regexp);
 7047:     } else {
 7048:         $regexp='.';
 7049:     }
 7050:     if (grep { $_ eq $uhome } current_machine_ids()) {
 7051:         # user is hosted on this machine
 7052:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
 7053:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
 7054:         return %{unserialize($reply, $escapedkeys)};
 7055:     }
 7056:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
 7057:     my @pairs=split(/\&/,$rep);
 7058:     my %returnhash=();
 7059:     if (!($rep =~ /^error/ )) {
 7060: 	foreach my $item (@pairs) {
 7061: 	    my ($key,$value)=split(/=/,$item,2);
 7062:         $key = unescape($key) unless $escapedkeys;
 7063:         #$key = &unescape($key);
 7064: 	    next if ($key =~ /^error: 2 /);
 7065: 	    $returnhash{$key}=&thaw_unescape($value);
 7066: 	}
 7067:     }
 7068:     return %returnhash;
 7069: }
 7070: 
 7071: 
 7072: # --------------------------------------------------------- dumpstore interface
 7073: 
 7074: sub dumpstore {
 7075:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
 7076:    # same as dump but keys must be escaped. They may contain colon separated
 7077:    # lists of values that may themself contain colons (e.g. symbs).
 7078:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
 7079: }
 7080: 
 7081: # -------------------------------------------------------------- keys interface
 7082: 
 7083: sub getkeys {
 7084:    my ($namespace,$udomain,$uname)=@_;
 7085:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7086:    if (!$uname) { $uname=$env{'user.name'}; }
 7087:    my $uhome=&homeserver($uname,$udomain);
 7088:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
 7089:    my @keyarray=();
 7090:    foreach my $key (split(/\&/,$rep)) {
 7091:       next if ($key =~ /^error: 2 /);
 7092:       push(@keyarray,&unescape($key));
 7093:    }
 7094:    return @keyarray;
 7095: }
 7096: 
 7097: # --------------------------------------------------------------- currentdump
 7098: sub currentdump {
 7099:    my ($courseid,$sdom,$sname)=@_;
 7100:    $courseid = $env{'request.course.id'} if (! defined($courseid));
 7101:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
 7102:    $sname    = $env{'user.name'}         if (! defined($sname));
 7103:    my $uhome = &homeserver($sname,$sdom);
 7104:    my $rep;
 7105: 
 7106:    if (grep { $_ eq $uhome } current_machine_ids()) {
 7107:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
 7108:                    $courseid)));
 7109:    } else {
 7110:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
 7111:    }
 7112: 
 7113:    return if ($rep =~ /^(error:|no_such_host)/);
 7114:    #
 7115:    my %returnhash=();
 7116:    #
 7117:    if ($rep eq 'unknown_cmd') {
 7118:        # an old lond will not know currentdump
 7119:        # Do a dump and make it look like a currentdump
 7120:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
 7121:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
 7122:        my %hash = @tmp;
 7123:        @tmp=();
 7124:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
 7125:    } else {
 7126:        my @pairs=split(/\&/,$rep);
 7127:        foreach my $pair (@pairs) {
 7128:            my ($key,$value)=split(/=/,$pair,2);
 7129:            my ($symb,$param) = split(/:/,$key);
 7130:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
 7131:                                                         &thaw_unescape($value);
 7132:        }
 7133:    }
 7134:    return %returnhash;
 7135: }
 7136: 
 7137: sub convert_dump_to_currentdump{
 7138:     my %hash = %{shift()};
 7139:     my %returnhash;
 7140:     # Code ripped from lond, essentially.  The only difference
 7141:     # here is the unescaping done by lonnet::dump().  Conceivably
 7142:     # we might run in to problems with parameter names =~ /^v\./
 7143:     while (my ($key,$value) = each(%hash)) {
 7144:         my ($v,$symb,$param) = split(/:/,$key);
 7145: 	$symb  = &unescape($symb);
 7146: 	$param = &unescape($param);
 7147:         next if ($v eq 'version' || $symb eq 'keys');
 7148:         next if (exists($returnhash{$symb}) &&
 7149:                  exists($returnhash{$symb}->{$param}) &&
 7150:                  $returnhash{$symb}->{'v.'.$param} > $v);
 7151:         $returnhash{$symb}->{$param}=$value;
 7152:         $returnhash{$symb}->{'v.'.$param}=$v;
 7153:     }
 7154:     #
 7155:     # Remove all of the keys in the hashes which keep track of
 7156:     # the version of the parameter.
 7157:     while (my ($symb,$param_hash) = each(%returnhash)) {
 7158:         # use a foreach because we are going to delete from the hash.
 7159:         foreach my $key (keys(%$param_hash)) {
 7160:             delete($param_hash->{$key}) if ($key =~ /^v\./);
 7161:         }
 7162:     }
 7163:     return \%returnhash;
 7164: }
 7165: 
 7166: # ------------------------------------------------------ critical inc interface
 7167: 
 7168: sub cinc {
 7169:     return &inc(@_,'critical');
 7170: }
 7171: 
 7172: # --------------------------------------------------------------- inc interface
 7173: 
 7174: sub inc {
 7175:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
 7176:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7177:     if (!$uname) { $uname=$env{'user.name'}; }
 7178:     my $uhome=&homeserver($uname,$udomain);
 7179:     my $items='';
 7180:     if (! ref($store)) {
 7181:         # got a single value, so use that instead
 7182:         $items = &escape($store).'=&';
 7183:     } elsif (ref($store) eq 'SCALAR') {
 7184:         $items = &escape($$store).'=&';        
 7185:     } elsif (ref($store) eq 'ARRAY') {
 7186:         $items = join('=&',map {&escape($_);} @{$store});
 7187:     } elsif (ref($store) eq 'HASH') {
 7188:         while (my($key,$value) = each(%{$store})) {
 7189:             $items.= &escape($key).'='.&escape($value).'&';
 7190:         }
 7191:     }
 7192:     $items=~s/\&$//;
 7193:     if ($critical) {
 7194: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
 7195:     } else {
 7196: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
 7197:     }
 7198: }
 7199: 
 7200: # --------------------------------------------------------------- put interface
 7201: 
 7202: sub put {
 7203:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7204:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7205:    if (!$uname) { $uname=$env{'user.name'}; }
 7206:    my $uhome=&homeserver($uname,$udomain);
 7207:    my $items='';
 7208:    foreach my $item (keys(%$storehash)) {
 7209:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7210:    }
 7211:    $items=~s/\&$//;
 7212:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7213: }
 7214: 
 7215: # ------------------------------------------------------------ newput interface
 7216: 
 7217: sub newput {
 7218:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7219:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7220:    if (!$uname) { $uname=$env{'user.name'}; }
 7221:    my $uhome=&homeserver($uname,$udomain);
 7222:    my $items='';
 7223:    foreach my $key (keys(%$storehash)) {
 7224:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
 7225:    }
 7226:    $items=~s/\&$//;
 7227:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
 7228: }
 7229: 
 7230: # ---------------------------------------------------------  putstore interface
 7231: 
 7232: sub putstore {
 7233:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
 7234:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7235:    if (!$uname) { $uname=$env{'user.name'}; }
 7236:    my $uhome=&homeserver($uname,$udomain);
 7237:    my $items='';
 7238:    foreach my $key (keys(%$storehash)) {
 7239:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7240:    }
 7241:    $items=~s/\&$//;
 7242:    my $esc_symb=&escape($symb);
 7243:    my $esc_v=&escape($version);
 7244:    my $reply =
 7245:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
 7246: 	      $uhome);
 7247:    if (($tolog) && ($reply eq 'ok')) {
 7248:        my $namevalue='';
 7249:        foreach my $key (keys(%{$storehash})) {
 7250:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
 7251:        }
 7252:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
 7253:                      '&host='.&escape($perlvar{'lonHostID'}).
 7254:                      '&version='.$esc_v.
 7255:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
 7256:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
 7257:    }
 7258:    if ($reply eq 'unknown_cmd') {
 7259:        # gfall back to way things use to be done
 7260:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
 7261: 			    $uname);
 7262:    }
 7263:    return $reply;
 7264: }
 7265: 
 7266: sub old_putstore {
 7267:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
 7268:     if (!$udomain) { $udomain=$env{'user.domain'}; }
 7269:     if (!$uname) { $uname=$env{'user.name'}; }
 7270:     my $uhome=&homeserver($uname,$udomain);
 7271:     my %newstorehash;
 7272:     foreach my $item (keys(%$storehash)) {
 7273: 	my $key = $version.':'.&escape($symb).':'.$item;
 7274: 	$newstorehash{$key} = $storehash->{$item};
 7275:     }
 7276:     my $items='';
 7277:     my %allitems = ();
 7278:     foreach my $item (keys(%newstorehash)) {
 7279: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
 7280: 	    my $key = $1.':keys:'.$2;
 7281: 	    $allitems{$key} .= $3.':';
 7282: 	}
 7283: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
 7284:     }
 7285:     foreach my $item (keys(%allitems)) {
 7286: 	$allitems{$item} =~ s/\:$//;
 7287: 	$items.= $item.'='.$allitems{$item}.'&';
 7288:     }
 7289:     $items=~s/\&$//;
 7290:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
 7291: }
 7292: 
 7293: # ------------------------------------------------------ critical put interface
 7294: 
 7295: sub cput {
 7296:    my ($namespace,$storehash,$udomain,$uname)=@_;
 7297:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7298:    if (!$uname) { $uname=$env{'user.name'}; }
 7299:    my $uhome=&homeserver($uname,$udomain);
 7300:    my $items='';
 7301:    foreach my $item (keys(%$storehash)) {
 7302:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7303:    }
 7304:    $items=~s/\&$//;
 7305:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
 7306: }
 7307: 
 7308: # -------------------------------------------------------------- eget interface
 7309: 
 7310: sub eget {
 7311:    my ($namespace,$storearr,$udomain,$uname)=@_;
 7312:    my $items='';
 7313:    foreach my $item (@$storearr) {
 7314:        $items.=&escape($item).'&';
 7315:    }
 7316:    $items=~s/\&$//;
 7317:    if (!$udomain) { $udomain=$env{'user.domain'}; }
 7318:    if (!$uname) { $uname=$env{'user.name'}; }
 7319:    my $uhome=&homeserver($uname,$udomain);
 7320:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
 7321:    my @pairs=split(/\&/,$rep);
 7322:    my %returnhash=();
 7323:    my $i=0;
 7324:    foreach my $item (@$storearr) {
 7325:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
 7326:       $i++;
 7327:    }
 7328:    return %returnhash;
 7329: }
 7330: 
 7331: # ------------------------------------------------------------ tmpput interface
 7332: sub tmpput {
 7333:     my ($storehash,$server,$context)=@_;
 7334:     my $items='';
 7335:     foreach my $item (keys(%$storehash)) {
 7336: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
 7337:     }
 7338:     $items=~s/\&$//;
 7339:     if (defined($context)) {
 7340:         $items .= ':'.&escape($context);
 7341:     }
 7342:     return &reply("tmpput:$items",$server);
 7343: }
 7344: 
 7345: # ------------------------------------------------------------ tmpget interface
 7346: sub tmpget {
 7347:     my ($token,$server)=@_;
 7348:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7349:     my $rep=&reply("tmpget:$token",$server);
 7350:     my %returnhash;
 7351:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
 7352:         return %returnhash;
 7353:     }
 7354:     foreach my $item (split(/\&/,$rep)) {
 7355: 	my ($key,$value)=split(/=/,$item);
 7356: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
 7357:     }
 7358:     return %returnhash;
 7359: }
 7360: 
 7361: # ------------------------------------------------------------ tmpdel interface
 7362: sub tmpdel {
 7363:     my ($token,$server)=@_;
 7364:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
 7365:     return &reply("tmpdel:$token",$server);
 7366: }
 7367: 
 7368: # ------------------------------------------------------------ get_timebased_id 
 7369: 
 7370: sub get_timebased_id {
 7371:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
 7372:         $maxtries) = @_;
 7373:     my ($newid,$error,$dellock);
 7374:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
 7375:         return ('','ok','invalid call to get suffix');
 7376:     }
 7377: 
 7378: # set defaults for any optional args for which values were not supplied
 7379:     if ($who eq '') {
 7380:         $who = $env{'user.name'}.':'.$env{'user.domain'};
 7381:     }
 7382:     if (!$locktries) {
 7383:         $locktries = 3;
 7384:     }
 7385:     if (!$maxtries) {
 7386:         $maxtries = 10;
 7387:     }
 7388:     
 7389:     if (($cdom eq '') || ($cnum eq '')) {
 7390:         if ($env{'request.course.id'}) {
 7391:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 7392:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 7393:         }
 7394:         if (($cdom eq '') || ($cnum eq '')) {
 7395:             return ('','ok','call to get suffix not in course context');
 7396:         }
 7397:     }
 7398: 
 7399: # construct locking item
 7400:     my $lockhash = {
 7401:                       $prefix."\0".'locked_'.$keyid => $who,
 7402:                    };
 7403:     my $tries = 0;
 7404: 
 7405: # attempt to get lock on nohist_$namespace file
 7406:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7407:     while (($gotlock ne 'ok') && $tries <$locktries) {
 7408:         $tries ++;
 7409:         sleep 1;
 7410:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
 7411:     }
 7412: 
 7413: # attempt to get unique identifier, based on current timestamp
 7414:     if ($gotlock eq 'ok') {
 7415:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
 7416:         my $id = time;
 7417:         $newid = $id;
 7418:         if ($idtype eq 'addcode') {
 7419:             $newid .= &sixnum_code();
 7420:         }
 7421:         my $idtries = 0;
 7422:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
 7423:             if ($idtype eq 'concat') {
 7424:                 $newid = $id.$idtries;
 7425:             } elsif ($idtype eq 'addcode') {
 7426:                 $newid = $newid.&sixnum_code();
 7427:             } else {
 7428:                 $newid ++;
 7429:             }
 7430:             $idtries ++;
 7431:         }
 7432:         if (!exists($inuse{$prefix."\0".$newid})) {
 7433:             my %new_item =  (
 7434:                               $prefix."\0".$newid => $who,
 7435:                             );
 7436:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
 7437:                                                  $cdom,$cnum);
 7438:             if ($putresult ne 'ok') {
 7439:                 undef($newid);
 7440:                 $error = 'error saving new item: '.$putresult;
 7441:             }
 7442:         } else {
 7443:              undef($newid);
 7444:              $error = ('error: no unique suffix available for the new item ');
 7445:         }
 7446: #  remove lock
 7447:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
 7448:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
 7449:     } else {
 7450:         $error = "error: could not obtain lockfile\n";
 7451:         $dellock = 'ok';
 7452:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
 7453:             $dellock = 'nolock';
 7454:         }
 7455:     }
 7456:     return ($newid,$dellock,$error);
 7457: }
 7458: 
 7459: sub sixnum_code {
 7460:     my $code;
 7461:     for (0..6) {
 7462:         $code .= int( rand(9) );
 7463:     }
 7464:     return $code;
 7465: }
 7466: 
 7467: # -------------------------------------------------- portfolio access checking
 7468: 
 7469: sub portfolio_access {
 7470:     my ($requrl,$clientip) = @_;
 7471:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
 7472:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
 7473:     if ($result) {
 7474:         my %setters;
 7475:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7476:             my ($startblock,$endblock) =
 7477:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
 7478:             if ($startblock && $endblock) {
 7479:                 return 'B';
 7480:             }
 7481:         } else {
 7482:             my ($startblock,$endblock) =
 7483:                 &Apache::loncommon::blockcheck(\%setters,'port');
 7484:             if ($startblock && $endblock) {
 7485:                 return 'B';
 7486:             }
 7487:         }
 7488:     }
 7489:     if ($result eq 'ok') {
 7490:        return 'F';
 7491:     } elsif ($result =~ /^[^:]+:guest_/) {
 7492:        return 'A';
 7493:     }
 7494:     return '';
 7495: }
 7496: 
 7497: sub get_portfolio_access {
 7498:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
 7499: 
 7500:     if (!ref($access_hash)) {
 7501: 	my $current_perms = &get_portfile_permissions($udom,$unum);
 7502: 	my %access_controls = &get_access_controls($current_perms,$group,
 7503: 						   $file_name);
 7504: 	$access_hash = $access_controls{$file_name};
 7505:     }
 7506: 
 7507:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
 7508:     my $now = time;
 7509:     if (ref($access_hash) eq 'HASH') {
 7510:         foreach my $key (keys(%{$access_hash})) {
 7511:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 7512:             if ($start > $now) {
 7513:                 next;
 7514:             }
 7515:             if ($end && $end<$now) {
 7516:                 next;
 7517:             }
 7518:             if ($scope eq 'public') {
 7519:                 $public = $key;
 7520:                 last;
 7521:             } elsif ($scope eq 'guest') {
 7522:                 $guest = $key;
 7523:             } elsif ($scope eq 'domains') {
 7524:                 push(@domains,$key);
 7525:             } elsif ($scope eq 'users') {
 7526:                 push(@users,$key);
 7527:             } elsif ($scope eq 'course') {
 7528:                 push(@courses,$key);
 7529:             } elsif ($scope eq 'group') {
 7530:                 push(@groups,$key);
 7531:             } elsif ($scope eq 'ip') {
 7532:                 push(@ips,$key);
 7533:             }
 7534:         }
 7535:         if ($public) {
 7536:             return 'ok';
 7537:         } elsif (@ips > 0) {
 7538:             my $allowed;
 7539:             foreach my $ipkey (@ips) {
 7540:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
 7541:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
 7542:                         $allowed = 1;
 7543:                         last; 
 7544:                     }
 7545:                 }
 7546:             }
 7547:             if ($allowed) {
 7548:                 return 'ok';
 7549:             }
 7550:         }
 7551:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
 7552:             if ($guest) {
 7553:                 return $guest;
 7554:             }
 7555:         } else {
 7556:             if (@domains > 0) {
 7557:                 foreach my $domkey (@domains) {
 7558:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
 7559:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
 7560:                             return 'ok';
 7561:                         }
 7562:                     }
 7563:                 }
 7564:             }
 7565:             if (@users > 0) {
 7566:                 foreach my $userkey (@users) {
 7567:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
 7568:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
 7569:                             if (ref($item) eq 'HASH') {
 7570:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
 7571:                                     ($item->{'udom'} eq $env{'user.domain'})) {
 7572:                                     return 'ok';
 7573:                                 }
 7574:                             }
 7575:                         }
 7576:                     } 
 7577:                 }
 7578:             }
 7579:             my %roleshash;
 7580:             my @courses_and_groups = @courses;
 7581:             push(@courses_and_groups,@groups); 
 7582:             if (@courses_and_groups > 0) {
 7583:                 my (%allgroups,%allroles); 
 7584:                 my ($start,$end,$role,$sec,$group);
 7585:                 foreach my $envkey (%env) {
 7586:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7587:                         my $cid = $2.'_'.$3; 
 7588:                         if ($1 eq 'gr') {
 7589:                             $group = $4;
 7590:                             $allgroups{$cid}{$group} = $env{$envkey};
 7591:                         } else {
 7592:                             if ($4 eq '') {
 7593:                                 $sec = 'none';
 7594:                             } else {
 7595:                                 $sec = $4;
 7596:                             }
 7597:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7598:                         }
 7599:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
 7600:                         my $cid = $2.'_'.$3;
 7601:                         if ($4 eq '') {
 7602:                             $sec = 'none';
 7603:                         } else {
 7604:                             $sec = $4;
 7605:                         }
 7606:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
 7607:                     }
 7608:                 }
 7609:                 if (keys(%allroles) == 0) {
 7610:                     return;
 7611:                 }
 7612:                 foreach my $key (@courses_and_groups) {
 7613:                     my %content = %{$$access_hash{$key}};
 7614:                     my $cnum = $content{'number'};
 7615:                     my $cdom = $content{'domain'};
 7616:                     my $cid = $cdom.'_'.$cnum;
 7617:                     if (!exists($allroles{$cid})) {
 7618:                         next;
 7619:                     }    
 7620:                     foreach my $role_id (keys(%{$content{'roles'}})) {
 7621:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
 7622:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
 7623:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
 7624:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
 7625:                         foreach my $role (keys(%{$allroles{$cid}})) {
 7626:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
 7627:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
 7628:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
 7629:                                         if (grep/^all$/,@sections) {
 7630:                                             return 'ok';
 7631:                                         } else {
 7632:                                             if (grep/^$sec$/,@sections) {
 7633:                                                 return 'ok';
 7634:                                             }
 7635:                                         }
 7636:                                     }
 7637:                                 }
 7638:                                 if (keys(%{$allgroups{$cid}}) == 0) {
 7639:                                     if (grep/^none$/,@groups) {
 7640:                                         return 'ok';
 7641:                                     }
 7642:                                 } else {
 7643:                                     if (grep/^all$/,@groups) {
 7644:                                         return 'ok';
 7645:                                     } 
 7646:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
 7647:                                         if (grep/^$group$/,@groups) {
 7648:                                             return 'ok';
 7649:                                         }
 7650:                                     }
 7651:                                 } 
 7652:                             }
 7653:                         }
 7654:                     }
 7655:                 }
 7656:             }
 7657:             if ($guest) {
 7658:                 return $guest;
 7659:             }
 7660:         }
 7661:     }
 7662:     return;
 7663: }
 7664: 
 7665: sub course_group_datechecker {
 7666:     my ($dates,$now,$status) = @_;
 7667:     my ($start,$end) = split(/\./,$dates);
 7668:     if (!$start && !$end) {
 7669:         return 'ok';
 7670:     }
 7671:     if (grep/^active$/,@{$status}) {
 7672:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
 7673:             return 'ok';
 7674:         }
 7675:     }
 7676:     if (grep/^previous$/,@{$status}) {
 7677:         if ($end > $now ) {
 7678:             return 'ok';
 7679:         }
 7680:     }
 7681:     if (grep/^future$/,@{$status}) {
 7682:         if ($start > $now) {
 7683:             return 'ok';
 7684:         }
 7685:     }
 7686:     return; 
 7687: }
 7688: 
 7689: sub parse_portfolio_url {
 7690:     my ($url) = @_;
 7691: 
 7692:     my ($type,$udom,$unum,$group,$file_name);
 7693:     
 7694:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
 7695: 	$type = 1;
 7696:         $udom = $1;
 7697:         $unum = $2;
 7698:         $file_name = $3;
 7699:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
 7700: 	$type = 2;
 7701:         $udom = $1;
 7702:         $unum = $2;
 7703:         $group = $3;
 7704:         $file_name = $3.'/'.$4;
 7705:     }
 7706:     if (wantarray) {
 7707: 	return ($type,$udom,$unum,$file_name,$group);
 7708:     }
 7709:     return $type;
 7710: }
 7711: 
 7712: sub is_portfolio_url {
 7713:     my ($url) = @_;
 7714:     return scalar(&parse_portfolio_url($url));
 7715: }
 7716: 
 7717: sub is_portfolio_file {
 7718:     my ($file) = @_;
 7719:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
 7720:         return 1;
 7721:     }
 7722:     return;
 7723: }
 7724: 
 7725: sub usertools_access {
 7726:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
 7727:     my ($access,%tools);
 7728:     if ($context eq '') {
 7729:         $context = 'tools';
 7730:     }
 7731:     if ($context eq 'requestcourses') {
 7732:         %tools = (
 7733:                       official   => 1,
 7734:                       unofficial => 1,
 7735:                       community  => 1,
 7736:                       textbook   => 1,
 7737:                       placement  => 1,
 7738:                       lti        => 1,
 7739:                  );
 7740:     } elsif ($context eq 'requestauthor') {
 7741:         %tools = (
 7742:                       requestauthor => 1,
 7743:                  );
 7744:     } else {
 7745:         %tools = (
 7746:                       aboutme   => 1,
 7747:                       blog      => 1,
 7748:                       webdav    => 1,
 7749:                       portfolio => 1,
 7750:                  );
 7751:     }
 7752:     return if (!defined($tools{$tool}));
 7753: 
 7754:     if (($udom eq '') || ($uname eq '')) {
 7755:         $udom = $env{'user.domain'};
 7756:         $uname = $env{'user.name'};
 7757:     }
 7758: 
 7759:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7760:         if ($action ne 'reload') {
 7761:             if ($context eq 'requestcourses') {
 7762:                 return $env{'environment.canrequest.'.$tool};
 7763:             } elsif ($context eq 'requestauthor') {
 7764:                 return $env{'environment.canrequest.author'};
 7765:             } else {
 7766:                 return $env{'environment.availabletools.'.$tool};
 7767:             }
 7768:         }
 7769:     }
 7770: 
 7771:     my ($toolstatus,$inststatus,$envkey);
 7772:     if ($context eq 'requestauthor') {
 7773:         $envkey = $context; 
 7774:     } else {
 7775:         $envkey = $context.'.'.$tool;
 7776:     }
 7777: 
 7778:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
 7779:          ($action ne 'reload')) {
 7780:         $toolstatus = $env{'environment.'.$envkey};
 7781:         $inststatus = $env{'environment.inststatus'};
 7782:     } else {
 7783:         if (ref($userenvref) eq 'HASH') {
 7784:             $toolstatus = $userenvref->{$envkey};
 7785:             $inststatus = $userenvref->{'inststatus'};
 7786:         } else {
 7787:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
 7788:             $toolstatus = $userenv{$envkey};
 7789:             $inststatus = $userenv{'inststatus'};
 7790:         }
 7791:     }
 7792: 
 7793:     if ($toolstatus ne '') {
 7794:         if ($toolstatus) {
 7795:             $access = 1;
 7796:         } else {
 7797:             $access = 0;
 7798:         }
 7799:         return $access;
 7800:     }
 7801: 
 7802:     my ($is_adv,%domdef);
 7803:     if (ref($is_advref) eq 'HASH') {
 7804:         $is_adv = $is_advref->{'is_adv'};
 7805:     } else {
 7806:         $is_adv = &is_advanced_user($udom,$uname);
 7807:     }
 7808:     if (ref($domdefref) eq 'HASH') {
 7809:         %domdef = %{$domdefref};
 7810:     } else {
 7811:         %domdef = &get_domain_defaults($udom);
 7812:     }
 7813:     if (ref($domdef{$tool}) eq 'HASH') {
 7814:         if ($is_adv) {
 7815:             if ($domdef{$tool}{'_LC_adv'} ne '') {
 7816:                 if ($domdef{$tool}{'_LC_adv'}) { 
 7817:                     $access = 1;
 7818:                 } else {
 7819:                     $access = 0;
 7820:                 }
 7821:                 return $access;
 7822:             }
 7823:         }
 7824:         if ($inststatus ne '') {
 7825:             my ($hasaccess,$hasnoaccess);
 7826:             foreach my $affiliation (split(/:/,$inststatus)) {
 7827:                 if ($domdef{$tool}{$affiliation} ne '') { 
 7828:                     if ($domdef{$tool}{$affiliation}) {
 7829:                         $hasaccess = 1;
 7830:                     } else {
 7831:                         $hasnoaccess = 1;
 7832:                     }
 7833:                 }
 7834:             }
 7835:             if ($hasaccess || $hasnoaccess) {
 7836:                 if ($hasaccess) {
 7837:                     $access = 1;
 7838:                 } elsif ($hasnoaccess) {
 7839:                     $access = 0; 
 7840:                 }
 7841:                 return $access;
 7842:             }
 7843:         } else {
 7844:             if ($domdef{$tool}{'default'} ne '') {
 7845:                 if ($domdef{$tool}{'default'}) {
 7846:                     $access = 1;
 7847:                 } elsif ($domdef{$tool}{'default'} == 0) {
 7848:                     $access = 0;
 7849:                 }
 7850:                 return $access;
 7851:             }
 7852:         }
 7853:     } else {
 7854:         if (($context eq 'tools') && ($tool ne 'webdav')) {
 7855:             $access = 1;
 7856:         } else {
 7857:             $access = 0;
 7858:         }
 7859:         return $access;
 7860:     }
 7861: }
 7862: 
 7863: sub is_course_owner {
 7864:     my ($cdom,$cnum,$udom,$uname) = @_;
 7865:     if (($udom eq '') || ($uname eq '')) {
 7866:         $udom = $env{'user.domain'};
 7867:         $uname = $env{'user.name'};
 7868:     }
 7869:     unless (($udom eq '') || ($uname eq '')) {
 7870:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
 7871:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
 7872:                 return 1;
 7873:             } else {
 7874:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
 7875:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
 7876:                     return 1;
 7877:                 }
 7878:             }
 7879:         }
 7880:     }
 7881:     return;
 7882: }
 7883: 
 7884: sub is_advanced_user {
 7885:     my ($udom,$uname) = @_;
 7886:     if ($udom ne '' && $uname ne '') {
 7887:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
 7888:             if (wantarray) {
 7889:                 return ($env{'user.adv'},$env{'user.author'});
 7890:             } else {
 7891:                 return $env{'user.adv'};
 7892:             }
 7893:         }
 7894:     }
 7895:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
 7896:     my %allroles;
 7897:     my ($is_adv,$is_author);
 7898:     foreach my $role (keys(%roleshash)) {
 7899:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
 7900:         my $area = '/'.$tdomain.'/'.$trest;
 7901:         if ($sec ne '') {
 7902:             $area .= '/'.$sec;
 7903:         }
 7904:         if (($area ne '') && ($trole ne '')) {
 7905:             my $spec=$trole.'.'.$area;
 7906:             if ($trole =~ /^cr\//) {
 7907:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
 7908:             } elsif ($trole ne 'gr') {
 7909:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
 7910:             }
 7911:             if ($trole eq 'au') {
 7912:                 $is_author = 1;
 7913:             }
 7914:         }
 7915:     }
 7916:     foreach my $role (keys(%allroles)) {
 7917:         last if ($is_adv);
 7918:         foreach my $item (split(/:/,$allroles{$role})) {
 7919:             if ($item ne '') {
 7920:                 my ($privilege,$restrictions)=split(/&/,$item);
 7921:                 if ($privilege eq 'adv') {
 7922:                     $is_adv = 1;
 7923:                     last;
 7924:                 }
 7925:             }
 7926:         }
 7927:     }
 7928:     if (wantarray) {
 7929:         return ($is_adv,$is_author);
 7930:     }
 7931:     return $is_adv;
 7932: }
 7933: 
 7934: sub check_can_request {
 7935:     my ($dom,$can_request,$request_domains,$uname,$udom) = @_;
 7936:     my $canreq = 0;
 7937:     if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 7938:         $uname = $env{'user.name'};
 7939:         $udom = $env{'user.domain'};
 7940:     }
 7941:     my ($types,$typename) = &Apache::loncommon::course_types();
 7942:     my @options = ('approval','validate','autolimit');
 7943:     my $optregex = join('|',@options);
 7944:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
 7945:         foreach my $type (@{$types}) {
 7946:             if (&usertools_access($uname,$udom,$type,undef,
 7947:                                   'requestcourses')) {
 7948:                 $canreq ++;
 7949:                 if (ref($request_domains) eq 'HASH') {
 7950:                     push(@{$request_domains->{$type}},$udom);
 7951:                 }
 7952:                 if ($dom eq $udom) {
 7953:                     $can_request->{$type} = 1;
 7954:                 }
 7955:             }
 7956:             if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '') &&
 7957:                 ($env{'environment.reqcrsotherdom.'.$type} ne '')) {
 7958:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
 7959:                 if (@curr > 0) {
 7960:                     foreach my $item (@curr) {
 7961:                         if (ref($request_domains) eq 'HASH') {
 7962:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
 7963:                             if ($otherdom ne '') {
 7964:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
 7965:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
 7966:                                         push(@{$request_domains->{$type}},$otherdom);
 7967:                                     }
 7968:                                 } else {
 7969:                                     push(@{$request_domains->{$type}},$otherdom);
 7970:                                 }
 7971:                             }
 7972:                         }
 7973:                     }
 7974:                     unless ($dom eq $env{'user.domain'}) {
 7975:                         $canreq ++;
 7976:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
 7977:                             $can_request->{$type} = 1;
 7978:                         }
 7979:                     }
 7980:                 }
 7981:             }
 7982:         }
 7983:     }
 7984:     return $canreq;
 7985: }
 7986: 
 7987: # ---------------------------------------------- Custom access rule evaluation
 7988: 
 7989: sub customaccess {
 7990:     my ($priv,$uri)=@_;
 7991:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
 7992:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
 7993:     $udom = &LONCAPA::clean_domain($udom);
 7994:     $ucrs = &LONCAPA::clean_username($ucrs);
 7995:     my $access=0;
 7996:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
 7997: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
 7998: 	if ($type eq 'user') {
 7999: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8000: 		my ($tdom,$tuname)=split(m{/},$scope);
 8001: 		if ($tdom) {
 8002: 		    if ($tdom ne $env{'user.domain'}) { next; }
 8003: 		}
 8004: 		if ($tuname) {
 8005: 		    if ($tuname ne $env{'user.name'}) { next; }
 8006: 		}
 8007: 		$access=($effect eq 'allow');
 8008: 		last;
 8009: 	    }
 8010: 	} else {
 8011: 	    if ($role) {
 8012: 		if ($role ne $urole) { next; }
 8013: 	    }
 8014: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
 8015: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
 8016: 		if ($tdom) {
 8017: 		    if ($tdom ne $udom) { next; }
 8018: 		}
 8019: 		if ($tcrs) {
 8020: 		    if ($tcrs ne $ucrs) { next; }
 8021: 		}
 8022: 		if ($tsec) {
 8023: 		    if ($tsec ne $usec) { next; }
 8024: 		}
 8025: 		$access=($effect eq 'allow');
 8026: 		last;
 8027: 	    }
 8028: 	    if ($realm eq '' && $role eq '') {
 8029: 		$access=($effect eq 'allow');
 8030: 	    }
 8031: 	}
 8032:     }
 8033:     return $access;
 8034: }
 8035: 
 8036: # ------------------------------------------------- Check for a user privilege
 8037: 
 8038: sub allowed {
 8039:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck,$ignorecache)=@_;
 8040:     my $ver_orguri=$uri;
 8041:     $uri=&deversion($uri);
 8042:     my $orguri=$uri;
 8043:     $uri=&declutter($uri);
 8044: 
 8045:     if ($priv eq 'evb') {
 8046: # Evade communication block restrictions for specified role in a course
 8047:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
 8048:             return $1;
 8049:         } else {
 8050:             return;
 8051:         }
 8052:     }
 8053: 
 8054:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
 8055: # Free bre access to adm and meta resources
 8056:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
 8057: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
 8058: 	&& ($priv eq 'bre')) {
 8059: 	return 'F';
 8060:     }
 8061: 
 8062: # Free bre access to user's own portfolio contents
 8063:     my ($space,$domain,$name,@dir)=split('/',$uri);
 8064:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
 8065: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
 8066:         my %setters;
 8067:         my ($startblock,$endblock) = 
 8068:             &Apache::loncommon::blockcheck(\%setters,'port');
 8069:         if ($startblock && $endblock) {
 8070:             return 'B';
 8071:         } else {
 8072:             return 'F';
 8073:         }
 8074:     }
 8075: 
 8076: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
 8077:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
 8078:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
 8079:         if (exists($env{'request.course.id'})) {
 8080:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8081:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8082:             if (($domain eq $cdom) && ($name eq $cnum)) {
 8083:                 my $courseprivid=$env{'request.course.id'};
 8084:                 $courseprivid=~s/\_/\//;
 8085:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
 8086:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
 8087:                     return $1; 
 8088:                 } else {
 8089:                     if ($env{'request.course.sec'}) {
 8090:                         $courseprivid.='/'.$env{'request.course.sec'};
 8091:                     }
 8092:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
 8093:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
 8094:                         return $2;
 8095:                     }
 8096:                 }
 8097:             }
 8098:         }
 8099:     }
 8100: 
 8101: # Free bre to public access
 8102: 
 8103:     if ($priv eq 'bre') {
 8104:         my $copyright;
 8105:         unless ($uri =~ /ext\.tool/) {
 8106:             $copyright=&metadata($uri,'copyright');
 8107:         }
 8108: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
 8109:            return 'F'; 
 8110:         }
 8111:         if ($copyright eq 'priv') {
 8112:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8113: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
 8114: 		return '';
 8115:             }
 8116:         }
 8117:         if ($copyright eq 'domain') {
 8118:             $uri=~/([^\/]+)\/([^\/]+)\//;
 8119: 	    unless (($env{'user.domain'} eq $1) ||
 8120:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
 8121: 		return '';
 8122:             }
 8123:         }
 8124:         if ($env{'request.role'}=~ /li\.\//) {
 8125:             # Library role, so allow browsing of resources in this domain.
 8126:             return 'F';
 8127:         }
 8128:         if ($copyright eq 'custom') {
 8129: 	    unless (&customaccess($priv,$uri)) { return ''; }
 8130:         }
 8131:     }
 8132:     # Domain coordinator is trying to create a course
 8133:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
 8134:         # uri is the requested domain in this case.
 8135:         # comparison to 'request.role.domain' shows if the user has selected
 8136:         # a role of dc for the domain in question.
 8137:         return 'F' if ($uri eq $env{'request.role.domain'});
 8138:     }
 8139: 
 8140:     my $thisallowed='';
 8141:     my $statecond=0;
 8142:     my $courseprivid='';
 8143: 
 8144:     my $ownaccess;
 8145:     # Community Coordinator or Assistant Co-author browsing resource space.
 8146:     if (($priv eq 'bro') && ($env{'user.author'})) {
 8147:         if ($uri eq '') {
 8148:             $ownaccess = 1;
 8149:         } else {
 8150:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
 8151:                 my $udom = $env{'user.domain'};
 8152:                 my $uname = $env{'user.name'};
 8153:                 if ($uri =~ m{^\Q$udom\E/?$}) {
 8154:                     $ownaccess = 1;
 8155:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
 8156:                     unless ($uri =~ m{\.\./}) {
 8157:                         $ownaccess = 1;
 8158:                     }
 8159:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
 8160:                     my $now = time;
 8161:                     if ($uri =~ m{^([^/]+)/?$}) {
 8162:                         my $adom = $1;
 8163:                         foreach my $key (keys(%env)) {
 8164:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
 8165:                                 my ($start,$end) = split('.',$env{$key});
 8166:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8167:                                     $ownaccess = 1;
 8168:                                     last;
 8169:                                 }
 8170:                             }
 8171:                         }
 8172:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
 8173:                         my $adom = $1;
 8174:                         my $aname = $2;
 8175:                         foreach my $role ('ca','aa') { 
 8176:                             if ($env{"user.role.$role./$adom/$aname"}) {
 8177:                                 my ($start,$end) =
 8178:                                     split('.',$env{"user.role.$role./$adom/$aname"});
 8179:                                 if (($now >= $start) && (!$end || $end < $now)) {
 8180:                                     $ownaccess = 1;
 8181:                                     last;
 8182:                                 }
 8183:                             }
 8184:                         }
 8185:                     }
 8186:                 }
 8187:             }
 8188:         }
 8189:     }
 8190: 
 8191: # Course
 8192: 
 8193:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
 8194:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8195:             $thisallowed.=$1;
 8196:         }
 8197:     }
 8198: 
 8199: # Domain
 8200: 
 8201:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
 8202:        =~/\Q$priv\E\&([^\:]*)/) {
 8203:         unless (($priv eq 'bro') && (!$ownaccess)) {
 8204:             $thisallowed.=$1;
 8205:         }
 8206:     }
 8207: 
 8208: # User who is not author or co-author might still be able to edit
 8209: # resource of an author in the domain (e.g., if Domain Coordinator).
 8210:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
 8211:         (&allowed('mdc',$env{'request.course.id'}))) {
 8212:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
 8213:             $thisallowed.=$1;
 8214:         }
 8215:     }
 8216: 
 8217: # Course: uri itself is a course
 8218:     my $courseuri=$uri;
 8219:     $courseuri=~s/\_(\d)/\/$1/;
 8220:     $courseuri=~s/^([^\/])/\/$1/;
 8221: 
 8222:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
 8223:        =~/\Q$priv\E\&([^\:]*)/) {
 8224:         if ($priv eq 'mip') {
 8225:             my $rem = $1;
 8226:             if (($uri ne '') && ($env{'request.course.id'} eq $uri) &&
 8227:                 ($env{'course.'.$env{'request.course.id'}.'.internal.courseowner'} eq $env{'user.name'}.':'.$env{'user.domain'})) {
 8228:                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8229:                 if ($cdom ne '') {
 8230:                     my %passwdconf = &get_passwdconf($cdom);
 8231:                     if (ref($passwdconf{'crsownerchg'}) eq 'HASH') {
 8232:                         if (ref($passwdconf{'crsownerchg'}{'by'}) eq 'ARRAY') {
 8233:                             if (@{$passwdconf{'crsownerchg'}{'by'}}) {
 8234:                                 my @inststatuses = split(':',$env{'environment.inststatus'});
 8235:                                 unless (@inststatuses) {
 8236:                                     @inststatuses = ('default');
 8237:                                 }
 8238:                                 foreach my $status (@inststatuses) {
 8239:                                     if (grep(/^\Q$status\E$/,@{$passwdconf{'crsownerchg'}{'by'}})) {
 8240:                                         $thisallowed.=$rem;
 8241:                                     }
 8242:                                 }
 8243:                             }
 8244:                         }
 8245:                     }
 8246:                 }
 8247:             }
 8248:         } else {
 8249:             unless (($priv eq 'bro') && (!$ownaccess)) {
 8250:                 $thisallowed.=$1;
 8251:             }
 8252:         }
 8253:     }
 8254: 
 8255: # URI is an uploaded document for this course, default permissions don't matter
 8256: # not allowing 'edit' access (editupload) to uploaded course docs
 8257:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
 8258: 	$thisallowed='';
 8259:         my ($match)=&is_on_map($uri);
 8260:         if ($match) {
 8261:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
 8262:                   =~/\Q$priv\E\&([^\:]*)/) {
 8263:                 my $value = $1;
 8264:                 my $deeplinkblock = &deeplink_check($priv,$symb,$uri);
 8265:                 if ($deeplinkblock) {
 8266:                     $thisallowed='D';
 8267:                 } elsif ($noblockcheck) {
 8268:                     $thisallowed.=$value;
 8269:                 } else {
 8270:                     my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8271:                     if (@blockers > 0) {
 8272:                         $thisallowed = 'B';
 8273:                     } else {
 8274:                         $thisallowed.=$value;
 8275:                     }
 8276:                 }
 8277:             }
 8278:         } else {
 8279:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
 8280:             if ($refuri) {
 8281:                 if ($refuri =~ m|^/adm/|) {
 8282:                     $thisallowed='F';
 8283:                 } else {
 8284:                     $refuri=&declutter($refuri);
 8285:                     my ($match) = &is_on_map($refuri);
 8286:                     if ($match) {
 8287:                         my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8288:                         if ($deeplinkblock) {
 8289:                             $thisallowed='D';
 8290:                         } elsif ($noblockcheck) {
 8291:                             $thisallowed='F';
 8292:                         } else {
 8293:                             my @blockers = &has_comm_blocking($priv,$symb,$refuri,$ignorecache);
 8294:                             if (@blockers > 0) {
 8295:                                 $thisallowed = 'B';
 8296:                             } else {
 8297:                                 $thisallowed='F';
 8298:                             }
 8299:                         }
 8300:                     }
 8301:                 }
 8302:             }
 8303:         }
 8304:     }
 8305: 
 8306:     if ($priv eq 'bre'
 8307: 	&& $thisallowed ne 'F' 
 8308: 	&& $thisallowed ne '2'
 8309: 	&& &is_portfolio_url($uri)) {
 8310: 	$thisallowed = &portfolio_access($uri,$clientip);
 8311:     }
 8312: 
 8313: # Full access at system, domain or course-wide level? Exit.
 8314:     if ($thisallowed=~/F/) {
 8315: 	return 'F';
 8316:     }
 8317: 
 8318: # If this is generating or modifying users, exit with special codes
 8319: 
 8320:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
 8321: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
 8322: 	    my ($audom,$auname)=split('/',$uri);
 8323: # no author name given, so this just checks on the general right to make a co-author in this domain
 8324: 	    unless ($auname) { return $thisallowed; }
 8325: # an author name is given, so we are about to actually make a co-author for a certain account
 8326: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
 8327: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
 8328: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
 8329: 	}
 8330: 	return $thisallowed;
 8331:     }
 8332: #
 8333: # Gathered so far: system, domain and course wide privileges
 8334: #
 8335: # Course: See if uri or referer is an individual resource that is part of 
 8336: # the course
 8337: 
 8338:     if ($env{'request.course.id'}) {
 8339: 
 8340: # If this is modifying password (internal auth) domains must match for user and user's role.
 8341: 
 8342:         if ($priv eq 'mip') {
 8343:             if ($env{'user.domain'} eq $env{'request.role.domain'}) {
 8344:                 return $thisallowed;
 8345:             } else {
 8346:                 return '';
 8347:             }
 8348:         }
 8349: 
 8350:        $courseprivid=$env{'request.course.id'};
 8351:        if ($env{'request.course.sec'}) {
 8352:           $courseprivid.='/'.$env{'request.course.sec'};
 8353:        }
 8354:        $courseprivid=~s/\_/\//;
 8355:        my $checkreferer=1;
 8356:        my ($match,$cond)=&is_on_map($uri);
 8357:        if ($match) {
 8358:            $statecond=$cond;
 8359:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8360:                =~/\Q$priv\E\&([^\:]*)/) {
 8361:                my $value = $1;
 8362:                if ($priv eq 'bre') {
 8363:                    if ($noblockcheck) {
 8364:                        $thisallowed.=$value;
 8365:                    } else {
 8366:                        my @blockers = &has_comm_blocking($priv,$symb,$uri,$ignorecache);
 8367:                        if (@blockers > 0) {
 8368:                            $thisallowed = 'B';
 8369:                        } else {
 8370:                            $thisallowed.=$value;
 8371:                        }
 8372:                    }
 8373:                } else {
 8374:                    $thisallowed.=$value;
 8375:                }
 8376:                $checkreferer=0;
 8377:            }
 8378:        }
 8379: 
 8380:        if ($checkreferer) {
 8381: 	  my $refuri=$env{'httpref.'.$orguri};
 8382:             unless ($refuri) {
 8383:                 foreach my $key (keys(%env)) {
 8384: 		    if ($key=~/^httpref\..*\*/) {
 8385: 			my $pattern=$key;
 8386:                         $pattern=~s/^httpref\.\/res\///;
 8387:                         $pattern=~s/\*/\[\^\/\]\+/g;
 8388:                         $pattern=~s/\//\\\//g;
 8389:                         if ($orguri=~/$pattern/) {
 8390: 			    $refuri=$env{$key};
 8391:                         }
 8392:                     }
 8393:                 }
 8394:             }
 8395: 
 8396:          if ($refuri) { 
 8397: 	  $refuri=&declutter($refuri);
 8398:           my ($match,$cond)=&is_on_map($refuri);
 8399:             if ($match) {
 8400:               my $refstatecond=$cond;
 8401:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
 8402:                   =~/\Q$priv\E\&([^\:]*)/) {
 8403:                   my $value = $1;
 8404:                   if ($priv eq 'bre') {
 8405:                       my $deeplinkblock = &deeplink_check($priv,$symb,$refuri);
 8406:                       if ($deeplinkblock) {
 8407:                           $thisallowed = 'D';
 8408:                       } elsif ($noblockcheck) {
 8409:                           $thisallowed.=$value;
 8410:                       } else {
 8411:                           my @blockers = &has_comm_blocking($priv,'',$refuri,$ignorecache);
 8412:                           if (@blockers > 0) {
 8413:                               $thisallowed = 'B';
 8414:                           } else {
 8415:                               $thisallowed.=$value;
 8416:                           }
 8417:                       }
 8418:                   } else {
 8419:                       $thisallowed.=$value;
 8420:                   }
 8421:                   $uri=$refuri;
 8422:                   $statecond=$refstatecond;
 8423:               }
 8424:           }
 8425:         }
 8426:        }
 8427:    }
 8428: 
 8429: #
 8430: # Gathered now: all privileges that could apply, and condition number
 8431: # 
 8432: #
 8433: # Full or no access?
 8434: #
 8435: 
 8436:     if ($thisallowed=~/F/) {
 8437: 	return 'F';
 8438:     }
 8439: 
 8440:     unless ($thisallowed) {
 8441:         return '';
 8442:     }
 8443: 
 8444: # Restrictions exist, deal with them
 8445: #
 8446: #   C:according to course preferences
 8447: #   R:according to resource settings
 8448: #   L:unless locked
 8449: #   X:according to user session state
 8450: #
 8451: 
 8452: # Possibly locked functionality, check all courses
 8453: # Locks might take effect only after 10 minutes cache expiration for other
 8454: # courses, and 2 minutes for current course
 8455: 
 8456:     my $envkey;
 8457:     if ($thisallowed=~/L/) {
 8458:         foreach $envkey (keys(%env)) {
 8459:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
 8460:                my $courseid=$2;
 8461:                my $roleid=$1.'.'.$2;
 8462:                $courseid=~s/^\///;
 8463:                my $expiretime=600;
 8464:                if ($env{'request.role'} eq $roleid) {
 8465: 		  $expiretime=120;
 8466:                }
 8467: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
 8468:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
 8469:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
 8470: 		   &coursedescription($courseid,{'freshen_cache' => 1});
 8471:                }
 8472:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8473:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
 8474: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
 8475:                        &log($env{'user.domain'},$env{'user.name'},
 8476:                             $env{'user.home'},
 8477:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
 8478:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8479:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8480: 		       return '';
 8481:                    }
 8482:                }
 8483:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
 8484:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
 8485: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
 8486:                        &log($env{'user.domain'},$env{'user.name'},
 8487:                             $env{'user.home'},
 8488:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
 8489:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
 8490:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
 8491: 		       return '';
 8492:                    }
 8493:                }
 8494: 	   }
 8495:        }
 8496:     }
 8497: 
 8498: #
 8499: # Rest of the restrictions depend on selected course
 8500: #
 8501: 
 8502:     unless ($env{'request.course.id'}) {
 8503: 	if ($thisallowed eq 'A') {
 8504: 	    return 'A';
 8505:         } elsif ($thisallowed eq 'B') {
 8506:             return 'B';
 8507: 	} else {
 8508: 	    return '1';
 8509: 	}
 8510:     }
 8511: 
 8512: #
 8513: # Now user is definitely in a course
 8514: #
 8515: 
 8516: 
 8517: # Course preferences
 8518: 
 8519:    if ($thisallowed=~/C/) {
 8520:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8521:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
 8522:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
 8523: 	   =~/\Q$rolecode\E/) {
 8524: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8525: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8526: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
 8527: 			$env{'request.course.id'});
 8528: 	   }
 8529:            return '';
 8530:        }
 8531: 
 8532:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
 8533: 	   =~/\Q$unamedom\E/) {
 8534: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
 8535: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
 8536: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
 8537: 			$env{'request.course.id'});
 8538: 	   }
 8539:            return '';
 8540:        }
 8541:    }
 8542: 
 8543: # Resource preferences
 8544: 
 8545:    if ($thisallowed=~/R/) {
 8546:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
 8547:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
 8548: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
 8549: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
 8550: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
 8551: 	   }
 8552: 	   return '';
 8553:        }
 8554:    }
 8555: 
 8556: # Restricted by state or randomout?
 8557: 
 8558:    if ($thisallowed=~/X/) {
 8559:       if ($env{'acc.randomout'}) {
 8560: 	 if (!$symb) { $symb=&symbread($uri,1); }
 8561:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
 8562:             return ''; 
 8563:          }
 8564:       }
 8565:       if (&condval($statecond)) {
 8566: 	 return '2';
 8567:       } else {
 8568:          return '';
 8569:       }
 8570:    }
 8571: 
 8572:     if ($thisallowed eq 'A') {
 8573: 	return 'A';
 8574:     } elsif ($thisallowed eq 'B') {
 8575:         return 'B';
 8576:     } elsif ($thisallowed eq 'D') {
 8577:         return 'D';
 8578:     }
 8579:    return 'F';
 8580: }
 8581: 
 8582: # ------------------------------------------- Check construction space access
 8583: 
 8584: sub constructaccess {
 8585:     my ($url,$setpriv)=@_;
 8586: 
 8587: # We do not allow editing of previous versions of files
 8588:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
 8589: 
 8590: # Get username and domain from URL
 8591:     my ($ownername,$ownerdomain,$ownerhome);
 8592: 
 8593:     ($ownerdomain,$ownername) =
 8594:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
 8595: 
 8596: # The URL does not really point to any authorspace, forget it
 8597:     unless (($ownername) && ($ownerdomain)) { return ''; }
 8598: 
 8599: # Now we need to see if the user has access to the authorspace of
 8600: # $ownername at $ownerdomain
 8601: 
 8602:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
 8603: # Real author for this?
 8604:        $ownerhome = $env{'user.home'};
 8605:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
 8606:           return ($ownername,$ownerdomain,$ownerhome);
 8607:        }
 8608:     } else {
 8609: # Co-author for this?
 8610:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
 8611:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
 8612:             $ownerhome = &homeserver($ownername,$ownerdomain);
 8613:             return ($ownername,$ownerdomain,$ownerhome);
 8614:         }
 8615:         if ($env{'request.course.id'}) {
 8616:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
 8617:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
 8618:                 if (&allowed('mdc',$env{'request.course.id'})) {
 8619:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
 8620:                     return ($ownername,$ownerdomain,$ownerhome);
 8621:                 }
 8622:             }
 8623:         }
 8624:     }
 8625: 
 8626: # We don't have any access right now. If we are not possibly going to do anything about this,
 8627: # we might as well leave
 8628:    unless ($setpriv) { return ''; }
 8629: 
 8630: # Backdoor access?
 8631:     my $allowed=&allowed('eco',$ownerdomain);
 8632: # Nope
 8633:     unless ($allowed) { return ''; }
 8634: # Looks like we may have access, but could be locked by the owner of the construction space
 8635:     if ($allowed eq 'U') {
 8636:         my %blocked=&get('environment',['domcoord.author'],
 8637:                          $ownerdomain,$ownername);
 8638: # Is blocked by owner
 8639:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
 8640:     }
 8641:     if (($allowed eq 'F') || ($allowed eq 'U')) {
 8642: # Grant temporary access
 8643:         my $then=$env{'user.login.time'};
 8644:         my $update=$env{'user.update.time'};
 8645:         if (!$update) { $update = $then; }
 8646:         my $refresh=$env{'user.refresh.time'};
 8647:         if (!$refresh) { $refresh = $update; }
 8648:         my $now = time;
 8649:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
 8650:                            $now,'ca','constructaccess');
 8651:         $ownerhome = &homeserver($ownername,$ownerdomain);
 8652:         return($ownername,$ownerdomain,$ownerhome);
 8653:     }
 8654: # No business here
 8655:     return '';
 8656: }
 8657: 
 8658: # ----------------------------------------------------------- Content Blocking
 8659: 
 8660: {
 8661: # Caches for faster Course Contents display where content blocking
 8662: # is in operation (i.e., interval param set) for timed quiz.
 8663: #
 8664: # User for whom data are being temporarily cached.
 8665: my $cacheduser='';
 8666: # Course for which data are being temporarily cached.
 8667: my $cachedcid='';
 8668: # List of blocks passed to &get_commblock_resources();
 8669: my $cachedblocks='';
 8670: # Cached blockers for this user (a hash of blocking items). 
 8671: my %cachedblockers=();
 8672: # When the data were last cached.
 8673: my $cachedlast='';
 8674: 
 8675: sub load_all_blockers {
 8676:     my ($uname,$udom,$blocks)=@_;
 8677:     if (($uname ne '') && ($udom ne '')) { 
 8678:         if (($cacheduser eq $uname.':'.$udom) &&
 8679:             ($cachedcid eq $env{'request.course.id'}) &&
 8680:             (abs($cachedlast-time)<5) &&
 8681:             (((ref($blocks) eq 'HASH') &&
 8682:               ($cachedblocks eq join(',',sort(keys(%{$blocks}))))) ||
 8683:              (!ref($blocks) && $cachedblocks eq ''))) {
 8684:             return;
 8685:         }
 8686:     }
 8687:     $cachedlast=time;
 8688:     $cacheduser=$uname.':'.$udom;
 8689:     $cachedcid=$env{'request.course.id'};
 8690:     %cachedblockers = &get_commblock_resources($blocks);
 8691:     if ((ref($blocks) eq 'HASH') && (keys(%{$blocks}) > 0)) {
 8692:         $cachedblocks = join(',',sort(keys(%{$blocks})));
 8693:     }
 8694:     return;
 8695: }
 8696: 
 8697: sub get_comm_blocks {
 8698:     my ($cdom,$cnum) = @_;
 8699:     if ($cdom eq '' || $cnum eq '') {
 8700:         return unless ($env{'request.course.id'});
 8701:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8702:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8703:     }
 8704:     my %commblocks;
 8705:     my $hashid=$cdom.'_'.$cnum;
 8706:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
 8707:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
 8708:         %commblocks = %{$blocksref};
 8709:     } else {
 8710:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
 8711:         my $cachetime = 600;
 8712:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
 8713:     }
 8714:     return %commblocks;
 8715: }
 8716: 
 8717: sub get_commblock_resources {
 8718:     my ($blocks) = @_;
 8719:     my %blockers = ();
 8720:     return %blockers unless ($env{'request.course.id'});
 8721:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8722:     my %commblocks;
 8723:     if (ref($blocks) eq 'HASH') {
 8724:         %commblocks = %{$blocks};
 8725:     } else {
 8726:         %commblocks = &get_comm_blocks();
 8727:     }
 8728:     return %blockers unless (keys(%commblocks) > 0); 
 8729:     my $navmap = Apache::lonnavmaps::navmap->new();
 8730:     return %blockers unless (ref($navmap));
 8731:     my $now = time;
 8732:     foreach my $block (keys(%commblocks)) {
 8733:         if ($block =~ /^(\d+)____(\d+)$/) {
 8734:             my ($start,$end) = ($1,$2);
 8735:             if ($start <= $now && $end >= $now) {
 8736:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8737:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8738:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8739:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8740:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
 8741:                             }
 8742:                         }
 8743:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8744:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8745:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8746:                             }
 8747:                         }
 8748:                     }
 8749:                 }
 8750:             }
 8751:         } elsif ($block =~ /^firstaccess____(.+)$/) {
 8752:             my $item = $1;
 8753:             my @to_test;
 8754:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
 8755:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
 8756:                     my @interval;
 8757:                     my $type = 'map';
 8758:                     if ($item eq 'course') {
 8759:                         $type = 'course';
 8760:                         @interval=&EXT("resource.0.interval");
 8761:                     } else {
 8762:                         if ($item =~ /___\d+___/) {
 8763:                             $type = 'resource';
 8764:                             @interval=&EXT("resource.0.interval",$item);
 8765:                             if (ref($navmap)) {                        
 8766:                                 my $res = $navmap->getBySymb($item); 
 8767:                                 push(@to_test,$res);
 8768:                             }
 8769:                         } else {
 8770:                             my $mapsymb = &symbread($item,1);
 8771:                             if ($mapsymb) {
 8772:                                 if (ref($navmap)) {
 8773:                                     my $mapres = $navmap->getBySymb($mapsymb);
 8774:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
 8775:                                     foreach my $res (@to_test) {
 8776:                                         my $symb = $res->symb();
 8777:                                         next if ($symb eq $mapsymb);
 8778:                                         if ($symb ne '') {
 8779:                                             @interval=&EXT("resource.0.interval",$symb);
 8780:                                             if ($interval[1] eq 'map') {
 8781:                                                 last;
 8782:                                             }
 8783:                                         }
 8784:                                     }
 8785:                                 }
 8786:                             }
 8787:                         }
 8788:                     }
 8789:                     if ($interval[0] =~ /^(\d+)/) {
 8790:                         my $timelimit = $1; 
 8791:                         my $first_access;
 8792:                         if ($type eq 'resource') {
 8793:                             $first_access=&get_first_access($interval[1],$item);
 8794:                         } elsif ($type eq 'map') {
 8795:                             $first_access=&get_first_access($interval[1],undef,$item);
 8796:                         } else {
 8797:                             $first_access=&get_first_access($interval[1]);
 8798:                         }
 8799:                         if ($first_access) {
 8800:                             my $timesup = $first_access+$timelimit;
 8801:                             if ($timesup > $now) {
 8802:                                 my $activeblock;
 8803:                                 foreach my $res (@to_test) {
 8804:                                     if ($res->answerable()) {
 8805:                                         $activeblock = 1;
 8806:                                         last;
 8807:                                     }
 8808:                                 }
 8809:                                 if ($activeblock) {
 8810:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
 8811:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
 8812:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
 8813:                                          }
 8814:                                     }
 8815:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
 8816:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
 8817:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
 8818:                                         }
 8819:                                     }
 8820:                                 }
 8821:                             }
 8822:                         }
 8823:                     }
 8824:                 }
 8825:             }
 8826:         }
 8827:     }
 8828:     return %blockers;
 8829: }
 8830: 
 8831: sub has_comm_blocking {
 8832:     my ($priv,$symb,$uri,$nosymbcache,$blocked,$blocks) = @_;
 8833:     my @blockers;
 8834:     return unless ($env{'request.course.id'});
 8835:     return unless ($priv eq 'bre');
 8836:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
 8837:     return if ($env{'request.state'} eq 'construct');
 8838:     &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
 8839:     return unless (keys(%cachedblockers) > 0);
 8840:     my (%possibles,@symbs);
 8841:     if (!$symb) {
 8842:         $symb = &symbread($uri,1,1,'',\%possibles,$nosymbcache);
 8843:     }
 8844:     if ($symb) {
 8845:         @symbs = ($symb);
 8846:     } elsif (keys(%possibles)) { 
 8847:         @symbs = keys(%possibles);
 8848:     }
 8849:     my $noblock;
 8850:     foreach my $symb (@symbs) {
 8851:         last if ($noblock);
 8852:         my ($map,$resid,$resurl)=&decode_symb($symb);
 8853:         foreach my $block (keys(%cachedblockers)) {
 8854:             if ($block =~ /^firstaccess____(.+)$/) {
 8855:                 my $item = $1;
 8856:                 unless ($blocked) {
 8857:                     if (($item eq $map) || ($item eq $symb)) {
 8858:                         $noblock = 1;
 8859:                         last;
 8860:                     }
 8861:                 }
 8862:             }
 8863:             if (ref($cachedblockers{$block}) eq 'HASH') {
 8864:                 if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
 8865:                     if ($cachedblockers{$block}{'resources'}{$symb}) {
 8866:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8867:                             push(@blockers,$block);
 8868:                         }
 8869:                     }
 8870:                 }
 8871:                 if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
 8872:                     if ($cachedblockers{$block}{'maps'}{$map}) {
 8873:                         unless (grep(/^\Q$block\E$/,@blockers)) {
 8874:                             push(@blockers,$block);
 8875:                         }
 8876:                     }
 8877:                 }
 8878:             }
 8879:         }
 8880:     }
 8881:     unless ($noblock) { 
 8882:         return @blockers;
 8883:     }
 8884:     return;
 8885: }
 8886: }
 8887: 
 8888: sub deeplink_check {
 8889:     my ($priv,$symb,$uri) = @_;
 8890:     return unless ($env{'request.course.id'});
 8891:     return unless ($priv eq 'bre');
 8892:     return if ($env{'request.state'} eq 'construct');
 8893:     return if ($env{'request.role.adv'});
 8894:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 8895:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 8896:     my (%possibles,@symbs);
 8897:     if (!$symb) {
 8898:         $symb = &symbread($uri,1,1,1,\%possibles);
 8899:     }
 8900:     if ($symb) {
 8901:         @symbs = ($symb);
 8902:     } elsif (keys(%possibles)) {
 8903:         @symbs = keys(%possibles);
 8904:     }
 8905: 
 8906:     my ($login,$switchrole,$allow);
 8907:     if ($env{'request.deeplink.login'} =~ m{^\Q/tiny/$cdom/\E(\w+)$}) {
 8908:         my $key = $1;
 8909:         my $tinyurl;
 8910:         my ($result,$cached)=&Apache::lonnet::is_cached_new('tiny',$cdom."\0".$key);
 8911:         if (defined($cached)) {
 8912:              $tinyurl = $result;
 8913:         } else {
 8914:              my $configuname = &Apache::lonnet::get_domainconfiguser($cdom);
 8915:              my %currtiny = &Apache::lonnet::get('tiny',[$key],$cdom,$configuname);
 8916:              if ($currtiny{$key} ne '') {
 8917:                  $tinyurl = $currtiny{$key};
 8918:                  &Apache::lonnet::do_cache_new('tiny',$cdom."\0".$key,$currtiny{$key},600);
 8919:              }
 8920:         }
 8921:         if ($tinyurl ne '') {
 8922:             my ($cnumreq,$posslogin) = split(/\&/,$tinyurl);
 8923:             if ($cnumreq eq $cnum) {
 8924:                 $login = $posslogin;
 8925:             } else {
 8926:                 $switchrole = 1;
 8927:             }
 8928:         }
 8929:     }
 8930:     foreach my $symb (@symbs) {
 8931:         last if ($allow);
 8932:         my $deeplink = &EXT("resource.0.deeplink",$symb);
 8933:         if ($deeplink eq '') {
 8934:             $allow = 1;
 8935:         } else {
 8936:             my ($listed,$scope,$access) = split(/,/,$deeplink);
 8937:             if ($access eq 'any') {
 8938:                 $allow = 1;
 8939:             } elsif ($login) {
 8940:                 if ($access eq 'only') {
 8941:                     if ($scope eq 'res') {
 8942:                         if ($symb eq $login) {
 8943:                             $allow = 1;
 8944:                         }
 8945:                     } elsif ($scope eq 'map') {
 8946: #FIXME Compare map for $env{'request.deeplink.login'} with map for $symb
 8947:                     } elsif ($scope eq 'rec') {
 8948: #FIXME Recurse up for $env{'request.deeplink.login'} with map for $symb
 8949:                     }
 8950:                 } else {
 8951:                     my ($acctype,$item) = split(/:/,$access);
 8952:                     if (($acctype eq 'lti') && ($env{'user.linkprotector'})) {
 8953:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.linkprotector'}))) {
 8954:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8955:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.linkproturis'}))) {
 8956:                                 $allow = 1;
 8957:                             }
 8958:                         }
 8959:                     } elsif (($acctype eq 'key') && ($env{'user.deeplinkkey'})) {
 8960:                         if (grep(/^\Q$item\E$/,split(/,/,$env{'user.deeplinkkey'}))) {
 8961:                             my %tinyurls = &get('tiny',[$symb],$cdom,$cnum);
 8962:                             if (grep(/\Q$tinyurls{$symb}\E$/,split(/,/,$env{'user.keyedlinkuri'}))) {
 8963:                                 $allow = 1;
 8964:                             }
 8965:                         }
 8966:                     }
 8967:                 }
 8968:             }
 8969:         }
 8970:     }
 8971:     return if ($allow);
 8972:     return 1;
 8973: }
 8974: 
 8975: # -------------------------------- Deversion and split uri into path an filename   
 8976: 
 8977: #
 8978: #   Removes the version from a URI and
 8979: #   splits it in to its filename and path to the filename.
 8980: #   Seems like File::Basename could have done this more clearly.
 8981: #   Parameters:
 8982: #      $uri   - input URI
 8983: #   Returns:
 8984: #     Two element list consisting of 
 8985: #     $pathname  - the URI up to and excluding the trailing /
 8986: #     $filename  - The part of the URI following the last /
 8987: #  NOTE:
 8988: #    Another realization of this is simply:
 8989: #    use File::Basename;
 8990: #    ...
 8991: #    $uri = shift;
 8992: #    $filename = basename($uri);
 8993: #    $path     = dirname($uri);
 8994: #    return ($filename, $path);
 8995: #
 8996: #     The implementation below is probably faster however.
 8997: #
 8998: sub split_uri_for_cond {
 8999:     my $uri=&deversion(&declutter(shift));
 9000:     my @uriparts=split(/\//,$uri);
 9001:     my $filename=pop(@uriparts);
 9002:     my $pathname=join('/',@uriparts);
 9003:     return ($pathname,$filename);
 9004: }
 9005: # --------------------------------------------------- Is a resource on the map?
 9006: 
 9007: sub is_on_map {
 9008:     my ($pathname,$filename) = &split_uri_for_cond(shift);
 9009:     #Trying to find the conditional for the file
 9010:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 9011: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 9012:     if ($match) {
 9013: 	return (1,$1);
 9014:     } else {
 9015: 	return (0,0);
 9016:     }
 9017: }
 9018: 
 9019: # --------------------------------------------------------- Get symb from alias
 9020: 
 9021: sub get_symb_from_alias {
 9022:     my $symb=shift;
 9023:     my ($map,$resid,$url)=&decode_symb($symb);
 9024: # Already is a symb
 9025:     if ($url) { return $symb; }
 9026: # Must be an alias
 9027:     my $aliassymb='';
 9028:     my %bighash;
 9029:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
 9030:                             &GDBM_READER(),0640)) {
 9031:         my $rid=$bighash{'mapalias_'.$symb};
 9032: 	if ($rid) {
 9033: 	    my ($mapid,$resid)=split(/\./,$rid);
 9034: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
 9035: 				    $resid,$bighash{'src_'.$rid});
 9036: 	}
 9037:         untie %bighash;
 9038:     }
 9039:     return $aliassymb;
 9040: }
 9041: 
 9042: # ----------------------------------------------------------------- Define Role
 9043: 
 9044: sub definerole {
 9045:   if (allowed('mcr','/')) {
 9046:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
 9047:     foreach my $role (split(':',$sysrole)) {
 9048: 	my ($crole,$cqual)=split(/\&/,$role);
 9049:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
 9050:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
 9051: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9052:                return "refused:s:$crole&$cqual"; 
 9053:             }
 9054:         }
 9055:     }
 9056:     foreach my $role (split(':',$domrole)) {
 9057: 	my ($crole,$cqual)=split(/\&/,$role);
 9058:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
 9059:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
 9060: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
 9061:                return "refused:d:$crole&$cqual"; 
 9062:             }
 9063:         }
 9064:     }
 9065:     foreach my $role (split(':',$courole)) {
 9066: 	my ($crole,$cqual)=split(/\&/,$role);
 9067:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
 9068:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
 9069: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
 9070:                return "refused:c:$crole&$cqual"; 
 9071:             }
 9072:         }
 9073:     }
 9074:     my $uhome;
 9075:     if (($uname ne '') && ($udom ne '')) {
 9076:         $uhome = &homeserver($uname,$udom);
 9077:         return $uhome if ($uhome eq 'no_host');
 9078:     } else {
 9079:         $uname = $env{'user.name'};
 9080:         $udom = $env{'user.domain'};
 9081:         $uhome = $env{'user.home'};
 9082:     }
 9083:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
 9084:                 "$udom:$uname:rolesdef_$rolename=".
 9085:                 escape($sysrole.'_'.$domrole.'_'.$courole);
 9086:     return reply($command,$uhome);
 9087:   } else {
 9088:     return 'refused';
 9089:   }
 9090: }
 9091: 
 9092: # ---------------- Make a metadata query against the network of library servers
 9093: 
 9094: sub metadata_query {
 9095:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
 9096:     my %rhash;
 9097:     my %libserv = &all_library();
 9098:     my @server_list = (defined($server_array) ? @$server_array
 9099:                                               : keys(%libserv) );
 9100:     for my $server (@server_list) {
 9101:         my $domains = ''; 
 9102:         if (ref($domains_hash) eq 'HASH') {
 9103:             $domains = $domains_hash->{$server}; 
 9104:         }
 9105: 	unless ($custom or $customshow) {
 9106: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
 9107: 	    $rhash{$server}=$reply;
 9108: 	}
 9109: 	else {
 9110: 	    my $reply=&reply("querysend:".&escape($query).':'.
 9111: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
 9112: 			     $server);
 9113: 	    $rhash{$server}=$reply;
 9114: 	}
 9115:     }
 9116:     return \%rhash;
 9117: }
 9118: 
 9119: # ----------------------------------------- Send log queries and wait for reply
 9120: 
 9121: sub log_query {
 9122:     my ($uname,$udom,$query,%filters)=@_;
 9123:     my $uhome=&homeserver($uname,$udom);
 9124:     if ($uhome eq 'no_host') { return 'error: no_host'; }
 9125:     my $uhost=&hostname($uhome);
 9126:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
 9127:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
 9128:                        $uhome);
 9129:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
 9130:     return get_query_reply($queryid);
 9131: }
 9132: 
 9133: # -------------------------- Update MySQL table for portfolio file
 9134: 
 9135: sub update_portfolio_table {
 9136:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
 9137:     if ($group ne '') {
 9138:         $file_name =~s /^\Q$group\E//;
 9139:     }
 9140:     my $homeserver = &homeserver($uname,$udom);
 9141:     my $queryid=
 9142:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
 9143:                ':'.&escape($file_name).':'.$action,$homeserver);
 9144:     my $reply = &get_query_reply($queryid);
 9145:     return $reply;
 9146: }
 9147: 
 9148: # -------------------------- Update MySQL allusers table
 9149: 
 9150: sub update_allusers_table {
 9151:     my ($uname,$udom,$names) = @_;
 9152:     my $homeserver = &homeserver($uname,$udom);
 9153:     my $queryid=
 9154:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
 9155:                'lastname='.&escape($names->{'lastname'}).'%%'.
 9156:                'firstname='.&escape($names->{'firstname'}).'%%'.
 9157:                'middlename='.&escape($names->{'middlename'}).'%%'.
 9158:                'generation='.&escape($names->{'generation'}).'%%'.
 9159:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
 9160:                'id='.&escape($names->{'id'}),$homeserver);
 9161:     return;
 9162: }
 9163: 
 9164: # ------- Request retrieval of institutional classlists for course(s)
 9165: 
 9166: sub fetch_enrollment_query {
 9167:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
 9168:     my ($homeserver,$sleep,$loopmax);
 9169:     my $maxtries = 1;
 9170:     if ($context eq 'automated') {
 9171:         $homeserver = $perlvar{'lonHostID'};
 9172:         $sleep = 2;
 9173:         $loopmax = 100;
 9174:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
 9175:     } else {
 9176:         $homeserver = &homeserver($cnum,$dom);
 9177:     }
 9178:     my $host=&hostname($homeserver);
 9179:     my $cmd = '';
 9180:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9181:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9182:     }
 9183:     $cmd =~ s/%%$//;
 9184:     $cmd = &escape($cmd);
 9185:     my $query = 'fetchenrollment';
 9186:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
 9187:     unless ($queryid=~/^\Q$host\E\_/) { 
 9188:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
 9189:         return 'error: '.$queryid;
 9190:     }
 9191:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9192:     my $tries = 1;
 9193:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9194:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
 9195:         $tries ++;
 9196:     }
 9197:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9198:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9199:     } else {
 9200:         my @responses = split(/:/,$reply);
 9201:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
 9202:             foreach my $line (@responses) {
 9203:                 my ($key,$value) = split(/=/,$line,2);
 9204:                 $$replyref{$key} = $value;
 9205:             }
 9206:         } else {
 9207:             my $pathname = LONCAPA::tempdir();
 9208:             foreach my $line (@responses) {
 9209:                 my ($key,$value) = split(/=/,$line);
 9210:                 $$replyref{$key} = $value;
 9211:                 if ($value > 0) {
 9212:                     foreach my $item (@{$$affiliatesref{$key}}) {
 9213:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
 9214:                         my $destname = $pathname.'/'.$filename;
 9215:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
 9216:                         if ($xml_classlist =~ /^error/) {
 9217:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
 9218:                         } else {
 9219:                             if ( open(FILE,">",$destname) ) {
 9220:                                 print FILE &unescape($xml_classlist);
 9221:                                 close(FILE);
 9222:                             } else {
 9223:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
 9224:                             }
 9225:                         }
 9226:                     }
 9227:                 }
 9228:             }
 9229:         }
 9230:         return 'ok';
 9231:     }
 9232:     return 'error';
 9233: }
 9234: 
 9235: sub get_query_reply {
 9236:     my ($queryid,$sleep,$loopmax) = @_;;
 9237:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
 9238:         $sleep = 0.2;
 9239:     }
 9240:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
 9241:         $loopmax = 100;
 9242:     }
 9243:     my $replyfile=LONCAPA::tempdir().$queryid;
 9244:     my $reply='';
 9245:     for (1..$loopmax) {
 9246: 	sleep($sleep);
 9247:         if (-e $replyfile.'.end') {
 9248: 	    if (open(my $fh,"<",$replyfile)) {
 9249: 		$reply = join('',<$fh>);
 9250: 		close($fh);
 9251: 	   } else { return 'error: reply_file_error'; }
 9252:            return &unescape($reply);
 9253: 	}
 9254:     }
 9255:     return 'timeout:'.$queryid;
 9256: }
 9257: 
 9258: sub courselog_query {
 9259: #
 9260: # possible filters:
 9261: # url: url or symb
 9262: # username
 9263: # domain
 9264: # action: view, submit, grade
 9265: # start: timestamp
 9266: # end: timestamp
 9267: #
 9268:     my (%filters)=@_;
 9269:     unless ($env{'request.course.id'}) { return 'no_course'; }
 9270:     if ($filters{'url'}) {
 9271: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
 9272:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
 9273:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
 9274:     }
 9275:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 9276:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 9277:     return &log_query($cname,$cdom,'courselog',%filters);
 9278: }
 9279: 
 9280: sub userlog_query {
 9281: #
 9282: # possible filters:
 9283: # action: log check role
 9284: # start: timestamp
 9285: # end: timestamp
 9286: #
 9287:     my ($uname,$udom,%filters)=@_;
 9288:     return &log_query($uname,$udom,'userlog',%filters);
 9289: }
 9290: 
 9291: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
 9292: 
 9293: sub auto_run {
 9294:     my ($cnum,$cdom) = @_;
 9295:     my $response = 0;
 9296:     my $settings;
 9297:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
 9298:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 9299:         $settings = $domconfig{'autoenroll'};
 9300:         if ($settings->{'run'} eq '1') {
 9301:             $response = 1;
 9302:         }
 9303:     } else {
 9304:         my $homeserver;
 9305:         if (&is_course($cdom,$cnum)) {
 9306:             $homeserver = &homeserver($cnum,$cdom);
 9307:         } else {
 9308:             $homeserver = &domain($cdom,'primary');
 9309:         }
 9310:         if ($homeserver ne 'no_host') {
 9311:             $response = &reply('autorun:'.$cdom,$homeserver);
 9312:         }
 9313:     }
 9314:     return $response;
 9315: }
 9316: 
 9317: sub auto_get_sections {
 9318:     my ($cnum,$cdom,$inst_coursecode) = @_;
 9319:     my $homeserver;
 9320:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
 9321:         $homeserver = &homeserver($cnum,$cdom);
 9322:     }
 9323:     if (!defined($homeserver)) { 
 9324:         if ($cdom =~ /^$match_domain$/) {
 9325:             $homeserver = &domain($cdom,'primary');
 9326:         }
 9327:     }
 9328:     my @secs;
 9329:     if (defined($homeserver)) {
 9330:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
 9331:         unless ($response eq 'refused') {
 9332:             @secs = split(/:/,$response);
 9333:         }
 9334:     }
 9335:     return @secs;
 9336: }
 9337: 
 9338: sub auto_new_course {
 9339:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
 9340:     my $homeserver = &homeserver($cnum,$cdom);
 9341:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
 9342:     return $response;
 9343: }
 9344: 
 9345: sub auto_validate_courseID {
 9346:     my ($cnum,$cdom,$inst_course_id) = @_;
 9347:     my $homeserver = &homeserver($cnum,$cdom);
 9348:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
 9349:     return $response;
 9350: }
 9351: 
 9352: sub auto_validate_instcode {
 9353:     my ($cnum,$cdom,$instcode,$owner) = @_;
 9354:     my ($homeserver,$response);
 9355:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9356:         $homeserver = &homeserver($cnum,$cdom);
 9357:     }
 9358:     if (!defined($homeserver)) {
 9359:         if ($cdom =~ /^$match_domain$/) {
 9360:             $homeserver = &domain($cdom,'primary');
 9361:         }
 9362:     }
 9363:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
 9364:                         &escape($instcode).':'.&escape($owner),$homeserver));
 9365:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
 9366:     return ($outcome,$description,$defaultcredits);
 9367: }
 9368: 
 9369: sub auto_create_password {
 9370:     my ($cnum,$cdom,$authparam,$udom) = @_;
 9371:     my ($homeserver,$response);
 9372:     my $create_passwd = 0;
 9373:     my $authchk = '';
 9374:     if ($udom =~ /^$match_domain$/) {
 9375:         $homeserver = &domain($udom,'primary');
 9376:     }
 9377:     if ($homeserver eq '') {
 9378:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
 9379:             $homeserver = &homeserver($cnum,$cdom);
 9380:         }
 9381:     }
 9382:     if ($homeserver eq '') {
 9383:         $authchk = 'nodomain';
 9384:     } else {
 9385:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
 9386:         if ($response eq 'refused') {
 9387:             $authchk = 'refused';
 9388:         } else {
 9389:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
 9390:         }
 9391:     }
 9392:     return ($authparam,$create_passwd,$authchk);
 9393: }
 9394: 
 9395: sub auto_photo_permission {
 9396:     my ($cnum,$cdom,$students) = @_;
 9397:     my $homeserver = &homeserver($cnum,$cdom);
 9398:     my ($outcome,$perm_reqd,$conditions) = 
 9399: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
 9400:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9401: 	return (undef,undef);
 9402:     }
 9403:     return ($outcome,$perm_reqd,$conditions);
 9404: }
 9405: 
 9406: sub auto_checkphotos {
 9407:     my ($uname,$udom,$pid) = @_;
 9408:     my $homeserver = &homeserver($uname,$udom);
 9409:     my ($result,$resulttype);
 9410:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
 9411: 				   &escape($uname).':'.&escape($pid),
 9412: 				   $homeserver));
 9413:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9414: 	return (undef,undef);
 9415:     }
 9416:     if ($outcome) {
 9417:         ($result,$resulttype) = split(/:/,$outcome);
 9418:     } 
 9419:     return ($result,$resulttype);
 9420: }
 9421: 
 9422: sub auto_photochoice {
 9423:     my ($cnum,$cdom) = @_;
 9424:     my $homeserver = &homeserver($cnum,$cdom);
 9425:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
 9426: 						       &escape($cdom),
 9427: 						       $homeserver)));
 9428:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
 9429: 	return (undef,undef);
 9430:     }
 9431:     return ($update,$comment);
 9432: }
 9433: 
 9434: sub auto_photoupdate {
 9435:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
 9436:     my $homeserver = &homeserver($cnum,$dom);
 9437:     my $host=&hostname($homeserver);
 9438:     my $cmd = '';
 9439:     my $maxtries = 1;
 9440:     foreach my $affiliate (keys(%{$affiliatesref})) {
 9441:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
 9442:     }
 9443:     $cmd =~ s/%%$//;
 9444:     $cmd = &escape($cmd);
 9445:     my $query = 'institutionalphotos';
 9446:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
 9447:     unless ($queryid=~/^\Q$host\E\_/) {
 9448:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
 9449:         return 'error: '.$queryid;
 9450:     }
 9451:     my $reply = &get_query_reply($queryid);
 9452:     my $tries = 1;
 9453:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
 9454:         $reply = &get_query_reply($queryid);
 9455:         $tries ++;
 9456:     }
 9457:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
 9458:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
 9459:     } else {
 9460:         my @responses = split(/:/,$reply);
 9461:         my $outcome = shift(@responses); 
 9462:         foreach my $item (@responses) {
 9463:             my ($key,$value) = split(/=/,$item);
 9464:             $$photo{$key} = $value;
 9465:         }
 9466:         return $outcome;
 9467:     }
 9468:     return 'error';
 9469: }
 9470: 
 9471: sub auto_instcode_format {
 9472:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
 9473: 	$cat_order) = @_;
 9474:     my $courses = '';
 9475:     my @homeservers;
 9476:     if ($caller eq 'global') {
 9477: 	my %servers = &get_servers($codedom,'library');
 9478: 	foreach my $tryserver (keys(%servers)) {
 9479: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9480: 		push(@homeservers,$tryserver);
 9481: 	    }
 9482:         }
 9483:     } elsif ($caller eq 'requests') {
 9484:         if ($codedom =~ /^$match_domain$/) {
 9485:             my $chome = &domain($codedom,'primary');
 9486:             unless ($chome eq 'no_host') {
 9487:                 push(@homeservers,$chome);
 9488:             }
 9489:         }
 9490:     } else {
 9491:         push(@homeservers,&homeserver($caller,$codedom));
 9492:     }
 9493:     foreach my $code (keys(%{$instcodes})) {
 9494:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
 9495:     }
 9496:     chop($courses);
 9497:     my $ok_response = 0;
 9498:     my $response;
 9499:     while (@homeservers > 0 && $ok_response == 0) {
 9500:         my $server = shift(@homeservers); 
 9501:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
 9502:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
 9503:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
 9504: 		split(/:/,$response);
 9505:             %{$codes} = (%{$codes},&str2hash($codes_str));
 9506:             push(@{$codetitles},&str2array($codetitles_str));
 9507:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
 9508:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
 9509:             $ok_response = 1;
 9510:         }
 9511:     }
 9512:     if ($ok_response) {
 9513:         return 'ok';
 9514:     } else {
 9515:         return $response;
 9516:     }
 9517: }
 9518: 
 9519: sub auto_instcode_defaults {
 9520:     my ($domain,$returnhash,$code_order) = @_;
 9521:     my @homeservers;
 9522: 
 9523:     my %servers = &get_servers($domain,'library');
 9524:     foreach my $tryserver (keys(%servers)) {
 9525: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9526: 	    push(@homeservers,$tryserver);
 9527: 	}
 9528:     }
 9529: 
 9530:     my $response;
 9531:     foreach my $server (@homeservers) {
 9532:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
 9533:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9534: 	
 9535: 	foreach my $pair (split(/\&/,$response)) {
 9536: 	    my ($name,$value)=split(/\=/,$pair);
 9537: 	    if ($name eq 'code_order') {
 9538: 		@{$code_order} = split(/\&/,&unescape($value));
 9539: 	    } else {
 9540: 		$returnhash->{&unescape($name)}=&unescape($value);
 9541: 	    }
 9542: 	}
 9543: 	return 'ok';
 9544:     }
 9545: 
 9546:     return $response;
 9547: }
 9548: 
 9549: sub auto_possible_instcodes {
 9550:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
 9551:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
 9552:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9553:         return;
 9554:     }
 9555:     my (@homeservers,$uhome);
 9556:     if (defined(&domain($domain,'primary'))) {
 9557:         $uhome=&domain($domain,'primary');
 9558:         push(@homeservers,&domain($domain,'primary'));
 9559:     } else {
 9560:         my %servers = &get_servers($domain,'library');
 9561:         foreach my $tryserver (keys(%servers)) {
 9562:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
 9563:                 push(@homeservers,$tryserver);
 9564:             }
 9565:         }
 9566:     }
 9567:     my $response;
 9568:     foreach my $server (@homeservers) {
 9569:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
 9570:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
 9571:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
 9572:             split(':',$response);
 9573:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
 9574:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
 9575:         foreach my $item (split('&',$cat_title)) {   
 9576:             my ($name,$value)=split('=',$item);
 9577:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
 9578:         }
 9579:         foreach my $item (split('&',$cat_order)) {
 9580:             my ($name,$value)=split('=',$item);
 9581:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
 9582:         }
 9583:         return 'ok';
 9584:     }
 9585:     return $response;
 9586: }
 9587: 
 9588: sub auto_courserequest_checks {
 9589:     my ($dom) = @_;
 9590:     my ($homeserver,%validations);
 9591:     if ($dom =~ /^$match_domain$/) {
 9592:         $homeserver = &domain($dom,'primary');
 9593:     }
 9594:     unless ($homeserver eq 'no_host') {
 9595:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
 9596:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9597:             my @items = split(/&/,$response);
 9598:             foreach my $item (@items) {
 9599:                 my ($key,$value) = split('=',$item);
 9600:                 $validations{&unescape($key)} = &thaw_unescape($value);
 9601:             }
 9602:         }
 9603:     }
 9604:     return %validations; 
 9605: }
 9606: 
 9607: sub auto_courserequest_validation {
 9608:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
 9609:     my ($homeserver,$response);
 9610:     if ($dom =~ /^$match_domain$/) {
 9611:         $homeserver = &domain($dom,'primary');
 9612:     }
 9613:     unless ($homeserver eq 'no_host') {
 9614:         my $customdata;
 9615:         if (ref($custominfo) eq 'HASH') {
 9616:             $customdata = &freeze_escape($custominfo);
 9617:         }
 9618:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
 9619:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
 9620:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
 9621:                                     $customdata,$homeserver));
 9622:     }
 9623:     return $response;
 9624: }
 9625: 
 9626: sub auto_validate_class_sec {
 9627:     my ($cdom,$cnum,$owners,$inst_class) = @_;
 9628:     my $homeserver = &homeserver($cnum,$cdom);
 9629:     my $ownerlist;
 9630:     if (ref($owners) eq 'ARRAY') {
 9631:         $ownerlist = join(',',@{$owners});
 9632:     } else {
 9633:         $ownerlist = $owners;
 9634:     }
 9635:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
 9636:                         &escape($ownerlist).':'.$cdom,$homeserver);
 9637:     return $response;
 9638: }
 9639: 
 9640: sub auto_validate_instclasses {
 9641:     my ($cdom,$cnum,$owners,$classesref) = @_;
 9642:     my ($homeserver,%validations);
 9643:     $homeserver = &homeserver($cnum,$cdom);
 9644:     unless ($homeserver eq 'no_host') {
 9645:         my $ownerlist;
 9646:         if (ref($owners) eq 'ARRAY') {
 9647:             $ownerlist = join(',',@{$owners});
 9648:         } else {
 9649:             $ownerlist = $owners;
 9650:         }
 9651:         if (ref($classesref) eq 'HASH') {
 9652:             my $classes = &freeze_escape($classesref);
 9653:             my $response=&reply('autovalidateinstclasses:'.&escape($ownerlist).
 9654:                                 ':'.$cdom.':'.$classes,$homeserver);
 9655:             unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9656:                 my @items = split(/&/,$response);
 9657:                 foreach my $item (@items) {
 9658:                     my ($key,$value) = split('=',$item);
 9659:                     $validations{&unescape($key)} = &thaw_unescape($value);
 9660:                 }
 9661:             }
 9662:         }
 9663:     }
 9664:     return %validations;
 9665: }
 9666: 
 9667: sub auto_crsreq_update {
 9668:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
 9669:         $code,$accessstart,$accessend,$inbound) = @_;
 9670:     my ($homeserver,%crsreqresponse);
 9671:     if ($cdom =~ /^$match_domain$/) {
 9672:         $homeserver = &domain($cdom,'primary');
 9673:     }
 9674:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9675:         my $info;
 9676:         if (ref($inbound) eq 'HASH') {
 9677:             $info = &freeze_escape($inbound);
 9678:         }
 9679:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
 9680:                             ':'.&escape($action).':'.&escape($ownername).':'.
 9681:                             &escape($ownerdomain).':'.&escape($fullname).':'.
 9682:                             &escape($title).':'.&escape($code).':'.
 9683:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
 9684:                             $homeserver);
 9685:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
 9686:             my @items = split(/&/,$response);
 9687:             foreach my $item (@items) {
 9688:                 my ($key,$value) = split('=',$item);
 9689:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
 9690:             }
 9691:         }
 9692:     }
 9693:     return \%crsreqresponse;
 9694: }
 9695: 
 9696: sub auto_export_grades {
 9697:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
 9698:     my ($homeserver,%exportresponse);
 9699:     if ($cdom =~ /^$match_domain$/) {
 9700:         $homeserver = &domain($cdom,'primary');
 9701:     }
 9702:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
 9703:         my $info;
 9704:         if (ref($inforef) eq 'HASH') {
 9705:             $info = &freeze_escape($inforef);
 9706:         }
 9707:         if (ref($gradesref) eq 'HASH') {
 9708:             my $grades = &freeze_escape($gradesref);
 9709:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
 9710:                                 $info.':'.$grades,$homeserver);
 9711:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
 9712:                 my @items = split(/&/,$response);
 9713:                 foreach my $item (@items) {
 9714:                     my ($key,$value) = split('=',$item);
 9715:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
 9716:                 }
 9717:             }
 9718:         }
 9719:     }
 9720:     return \%exportresponse;
 9721: }
 9722: 
 9723: sub check_instcode_cloning {
 9724:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
 9725:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
 9726:         return;
 9727:     }
 9728:     my $canclone;
 9729:     if (@{$code_order} > 0) {
 9730:         my $instcoderegexp ='^';
 9731:         my @clonecodes = split(/\&/,$cloner);
 9732:         foreach my $item (@{$code_order}) {
 9733:             if (grep(/^\Q$item\E=/,@clonecodes)) {
 9734:                 foreach my $pair (@clonecodes) {
 9735:                     my ($key,$val) = split(/\=/,$pair,2);
 9736:                     $val = &unescape($val);
 9737:                     if ($key eq $item) {
 9738:                         $instcoderegexp .= '('.$val.')';
 9739:                         last;
 9740:                     }
 9741:                 }
 9742:             } else {
 9743:                 $instcoderegexp .= $codedefaults->{$item};
 9744:             }
 9745:         }
 9746:         $instcoderegexp .= '$';
 9747:         my (@from,@to);
 9748:         eval {
 9749:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9750:                (@to) = ($clonetocode =~ /$instcoderegexp/);
 9751:         };
 9752:         if ((@from > 0) && (@to > 0)) {
 9753:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9754:             if (!@diffs) {
 9755:                 $canclone = 1;
 9756:             }
 9757:         }
 9758:     }
 9759:     return $canclone;
 9760: }
 9761: 
 9762: sub default_instcode_cloning {
 9763:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
 9764:     my (%codedefaults,@code_order,$canclone);
 9765:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
 9766:         %codedefaults = %{$codedefaultsref};
 9767:         @code_order = @{$codeorderref};
 9768:     } elsif ($clonedom) {
 9769:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
 9770:     }
 9771:     if (($domdefclone) && (@code_order)) {
 9772:         my @clonecodes = split(/\+/,$domdefclone);
 9773:         my $instcoderegexp ='^';
 9774:         foreach my $item (@code_order) {
 9775:             if (grep(/^\Q$item\E$/,@clonecodes)) {
 9776:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
 9777:             } else {
 9778:                 $instcoderegexp .= $codedefaults{$item};
 9779:             }
 9780:         }
 9781:         $instcoderegexp .= '$';
 9782:         my (@from,@to);
 9783:         eval {
 9784:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
 9785:             (@to) = ($clonetocode =~ /$instcoderegexp/);
 9786:         };
 9787:         if ((@from > 0) && (@to > 0)) {
 9788:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
 9789:             if (!@diffs) {
 9790:                 $canclone = 1;
 9791:             }
 9792:         }
 9793:     }
 9794:     return $canclone;
 9795: }
 9796: 
 9797: # ------------------------------------------------------- Course Group routines
 9798: 
 9799: sub get_coursegroups {
 9800:     my ($cdom,$cnum,$group,$namespace) = @_;
 9801:     return(&dump($namespace,$cdom,$cnum,$group));
 9802: }
 9803: 
 9804: sub modify_coursegroup {
 9805:     my ($cdom,$cnum,$groupsettings) = @_;
 9806:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
 9807: }
 9808: 
 9809: sub toggle_coursegroup_status {
 9810:     my ($cdom,$cnum,$group,$action) = @_;
 9811:     my ($from_namespace,$to_namespace);
 9812:     if ($action eq 'delete') {
 9813:         $from_namespace = 'coursegroups';
 9814:         $to_namespace = 'deleted_groups';
 9815:     } else {
 9816:         $from_namespace = 'deleted_groups';
 9817:         $to_namespace = 'coursegroups';
 9818:     }
 9819:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
 9820:     if (my $tmp = &error(%curr_group)) {
 9821:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
 9822:         return ('read error',$tmp);
 9823:     } else {
 9824:         my %savedsettings = %curr_group; 
 9825:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
 9826:         my $deloutcome;
 9827:         if ($result eq 'ok') {
 9828:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
 9829:         } else {
 9830:             return ('write error',$result);
 9831:         }
 9832:         if ($deloutcome eq 'ok') {
 9833:             return 'ok';
 9834:         } else {
 9835:             return ('delete error',$deloutcome);
 9836:         }
 9837:     }
 9838: }
 9839: 
 9840: sub modify_group_roles {
 9841:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
 9842:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
 9843:     my $role = 'gr/'.&escape($userprivs);
 9844:     my ($uname,$udom) = split(/:/,$user);
 9845:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
 9846:     if ($result eq 'ok') {
 9847:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
 9848:     }
 9849:     return $result;
 9850: }
 9851: 
 9852: sub modify_coursegroup_membership {
 9853:     my ($cdom,$cnum,$membership) = @_;
 9854:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
 9855:     return $result;
 9856: }
 9857: 
 9858: sub get_active_groups {
 9859:     my ($udom,$uname,$cdom,$cnum) = @_;
 9860:     my $now = time;
 9861:     my %groups = ();
 9862:     foreach my $key (keys(%env)) {
 9863:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
 9864:             my ($start,$end) = split(/\./,$env{$key});
 9865:             if (($end!=0) && ($end<$now)) { next; }
 9866:             if (($start!=0) && ($start>$now)) { next; }
 9867:             if ($1 eq $cdom && $2 eq $cnum) {
 9868:                 $groups{$3} = $env{$key} ;
 9869:             }
 9870:         }
 9871:     }
 9872:     return %groups;
 9873: }
 9874: 
 9875: sub get_group_membership {
 9876:     my ($cdom,$cnum,$group) = @_;
 9877:     return(&dump('groupmembership',$cdom,$cnum,$group));
 9878: }
 9879: 
 9880: sub get_users_groups {
 9881:     my ($udom,$uname,$courseid) = @_;
 9882:     my @usersgroups;
 9883:     my $cachetime=1800;
 9884: 
 9885:     my $hashid="$udom:$uname:$courseid";
 9886:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
 9887:     if (defined($cached)) {
 9888:         @usersgroups = split(/:/,$grouplist);
 9889:     } else {  
 9890:         $grouplist = '';
 9891:         my $courseurl = &courseid_to_courseurl($courseid);
 9892:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
 9893:         my $access_end = $env{'course.'.$courseid.
 9894:                               '.default_enrollment_end_date'};
 9895:         my $now = time;
 9896:         foreach my $key (keys(%roleshash)) {
 9897:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
 9898:                 my $group = $1;
 9899:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
 9900:                     my $start = $2;
 9901:                     my $end = $1;
 9902:                     if ($start == -1) { next; } # deleted from group
 9903:                     if (($start!=0) && ($start>$now)) { next; }
 9904:                     if (($end!=0) && ($end<$now)) {
 9905:                         if ($access_end && $access_end < $now) {
 9906:                             if ($access_end - $end < 86400) {
 9907:                                 push(@usersgroups,$group);
 9908:                             }
 9909:                         }
 9910:                         next;
 9911:                     }
 9912:                     push(@usersgroups,$group);
 9913:                 }
 9914:             }
 9915:         }
 9916:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
 9917:         $grouplist = join(':',@usersgroups);
 9918:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
 9919:     }
 9920:     return @usersgroups;
 9921: }
 9922: 
 9923: sub devalidate_getgroups_cache {
 9924:     my ($udom,$uname,$cdom,$cnum)=@_;
 9925:     my $courseid = $cdom.'_'.$cnum;
 9926: 
 9927:     my $hashid="$udom:$uname:$courseid";
 9928:     &devalidate_cache_new('getgroups',$hashid);
 9929: }
 9930: 
 9931: # ------------------------------------------------------------------ Plain Text
 9932: 
 9933: sub plaintext {
 9934:     my ($short,$type,$cid,$forcedefault) = @_;
 9935:     if ($short =~ m{^cr/}) {
 9936: 	return (split('/',$short))[-1];
 9937:     }
 9938:     if (!defined($cid)) {
 9939:         $cid = $env{'request.course.id'};
 9940:     }
 9941:     my %rolenames = (
 9942:                       Course    => 'std',
 9943:                       Community => 'alt1',
 9944:                       Placement => 'std',
 9945:                     );
 9946:     if ($cid ne '') {
 9947:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
 9948:             unless ($forcedefault) {
 9949:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
 9950:                 &Apache::lonlocal::mt_escape(\$roletext);
 9951:                 return &Apache::lonlocal::mt($roletext);
 9952:             }
 9953:         }
 9954:     }
 9955:     if ((defined($type)) && (defined($rolenames{$type})) &&
 9956:         (defined($rolenames{$type})) && 
 9957:         (defined($prp{$short}{$rolenames{$type}}))) {
 9958:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
 9959:     } elsif ($cid ne '') {
 9960:         my $crstype = $env{'course.'.$cid.'.type'};
 9961:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
 9962:             (defined($prp{$short}{$rolenames{$crstype}}))) {
 9963:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
 9964:         }
 9965:     }
 9966:     return &Apache::lonlocal::mt($prp{$short}{'std'});
 9967: }
 9968: 
 9969: # ----------------------------------------------------------------- Assign Role
 9970: 
 9971: sub assignrole {
 9972:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
 9973:         $context)=@_;
 9974:     my $mrole;
 9975:     if ($role =~ /^cr\//) {
 9976:         my $cwosec=$url;
 9977:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
 9978: 	unless (&allowed('ccr',$cwosec)) {
 9979:            my $refused = 1;
 9980:            if ($context eq 'requestcourses') {
 9981:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
 9982:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
 9983:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
 9984:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
 9985:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
 9986:                            if ($crsenv{'internal.courseowner'} eq
 9987:                                $env{'user.name'}.':'.$env{'user.domain'}) {
 9988:                                $refused = '';
 9989:                            }
 9990:                        }
 9991:                    }
 9992:                }
 9993:            }
 9994:            if ($refused) {
 9995:                &logthis('Refused custom assignrole: '.
 9996:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
 9997:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
 9998:                return 'refused';
 9999:            }
10000:         }
10001:         $mrole='cr';
10002:     } elsif ($role =~ /^gr\//) {
10003:         my $cwogrp=$url;
10004:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
10005:         unless (&allowed('mdg',$cwogrp)) {
10006:             &logthis('Refused group assignrole: '.
10007:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
10008:                     $env{'user.name'}.' at '.$env{'user.domain'});
10009:             return 'refused';
10010:         }
10011:         $mrole='gr';
10012:     } else {
10013:         my $cwosec=$url;
10014:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
10015:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
10016:             my $refused;
10017:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
10018:                 if (!(&allowed('c'.$role,$url))) {
10019:                     $refused = 1;
10020:                 }
10021:             } else {
10022:                 $refused = 1;
10023:             }
10024:             if ($refused) {
10025:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
10026:                 if (!$selfenroll && (($context eq 'course') || ($context eq 'ltienroll' && $env{'request.lti.login'}))) {
10027:                     my %crsenv;
10028:                     if ($role eq 'cc' || $role eq 'co') {
10029:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10030:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
10031:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
10032:                                 if ($crsenv{'internal.courseowner'} eq 
10033:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10034:                                     $refused = '';
10035:                                 }
10036:                             }
10037:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
10038:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
10039:                                 if ($crsenv{'internal.courseowner'} eq 
10040:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
10041:                                     $refused = '';
10042:                                 }
10043:                             }
10044:                         }
10045:                     }
10046:                 } elsif (($selfenroll == 1) && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
10047:                     if ($role eq 'st') {
10048:                         $refused = '';
10049:                     } elsif (($context eq 'ltienroll') && ($env{'request.lti.login'})) {
10050:                         $refused = '';
10051:                     }
10052:                 } elsif ($context eq 'requestcourses') {
10053:                     my @possroles = ('st','ta','ep','in','cc','co');
10054:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
10055:                         my $wrongcc;
10056:                         if ($cnum =~ /^$match_community$/) {
10057:                             $wrongcc = 1 if ($role eq 'cc');
10058:                         } else {
10059:                             $wrongcc = 1 if ($role eq 'co');
10060:                         }
10061:                         unless ($wrongcc) {
10062:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
10063:                             if ($crsenv{'internal.courseowner'} eq 
10064:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
10065:                                 $refused = '';
10066:                             }
10067:                         }
10068:                     }
10069:                 } elsif ($context eq 'requestauthor') {
10070:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
10071:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
10072:                         if ($env{'environment.requestauthor'} eq 'automatic') {
10073:                             $refused = '';
10074:                         } else {
10075:                             my %domdefaults = &get_domain_defaults($udom);
10076:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
10077:                                 my $checkbystatus;
10078:                                 if ($env{'user.adv'}) { 
10079:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
10080:                                     if ($disposition eq 'automatic') {
10081:                                         $refused = '';
10082:                                     } elsif ($disposition eq '') {
10083:                                         $checkbystatus = 1;
10084:                                     } 
10085:                                 } else {
10086:                                     $checkbystatus = 1;
10087:                                 }
10088:                                 if ($checkbystatus) {
10089:                                     if ($env{'environment.inststatus'}) {
10090:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
10091:                                         foreach my $type (@inststatuses) {
10092:                                             if (($type ne '') &&
10093:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
10094:                                                 $refused = '';
10095:                                             }
10096:                                         }
10097:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
10098:                                         $refused = '';
10099:                                     }
10100:                                 }
10101:                             }
10102:                         }
10103:                     }
10104:                 }
10105:                 if ($refused) {
10106:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
10107:                              ' '.$role.' '.$end.' '.$start.' by '.
10108: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
10109:                     return 'refused';
10110:                 }
10111:             }
10112:         } elsif ($role eq 'au') {
10113:             if ($url ne '/'.$udom.'/') {
10114:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
10115:                          ' to assign author role for '.$uname.':'.$udom.
10116:                          ' in domain: '.$url.' refused (wrong domain).');
10117:                 return 'refused';
10118:             }
10119:         }
10120:         $mrole=$role;
10121:     }
10122:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
10123:                 "$udom:$uname:$url".'_'."$mrole=$role";
10124:     if ($end) { $command.='_'.$end; }
10125:     if ($start) {
10126: 	if ($end) { 
10127:            $command.='_'.$start; 
10128:         } else {
10129:            $command.='_0_'.$start;
10130:         }
10131:     }
10132:     my $origstart = $start;
10133:     my $origend = $end;
10134:     my $delflag;
10135: # actually delete
10136:     if ($deleteflag) {
10137: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
10138: # modify command to delete the role
10139:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
10140:                 "$udom:$uname:$url".'_'."$mrole";
10141: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
10142: # set start and finish to negative values for userrolelog
10143:            $start=-1;
10144:            $end=-1;
10145:            $delflag = 1;
10146:         }
10147:     }
10148: # send command
10149:     my $answer=&reply($command,&homeserver($uname,$udom));
10150: # log new user role if status is ok
10151:     if ($answer eq 'ok') {
10152: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
10153:         if (($role eq 'cc') || ($role eq 'in') ||
10154:             ($role eq 'ep') || ($role eq 'ad') ||
10155:             ($role eq 'ta') || ($role eq 'st') ||
10156:             ($role=~/^cr/) || ($role eq 'gr') ||
10157:             ($role eq 'co')) {
10158: # for course roles, perform group memberships changes triggered by role change.
10159:             unless ($role =~ /^gr/) {
10160:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
10161:                                                  $origstart,$selfenroll,$context);
10162:             }
10163:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10164:                            $selfenroll,$context);
10165:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
10166:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
10167:                  ($role eq 'da')) {
10168:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10169:                            $context);
10170:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
10171:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
10172:                              $context); 
10173:         }
10174:         if ($role eq 'cc') {
10175:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
10176:         }
10177:     }
10178:     return $answer;
10179: }
10180: 
10181: sub autoupdate_coowners {
10182:     my ($url,$end,$start,$uname,$udom) = @_;
10183:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
10184:     if (($cdom ne '') && ($cnum ne '')) {
10185:         my $now = time;
10186:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
10187:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
10188:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
10189:             my $instcode = $coursehash{'internal.coursecode'};
10190:             if ($instcode ne '') {
10191:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
10192:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
10193:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
10194:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
10195:                         if ($result eq 'valid') {
10196:                             if ($coursehash{'internal.co-owners'}) {
10197:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10198:                                     push(@newcoowners,$coowner);
10199:                                 }
10200:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
10201:                                     push(@newcoowners,$uname.':'.$udom);
10202:                                 }
10203:                                 @newcoowners = sort(@newcoowners);
10204:                             } else {
10205:                                 push(@newcoowners,$uname.':'.$udom);
10206:                             }
10207:                         } else {
10208:                             if ($coursehash{'internal.co-owners'}) {
10209:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
10210:                                     unless ($coowner eq $uname.':'.$udom) {
10211:                                         push(@newcoowners,$coowner);
10212:                                     }
10213:                                 }
10214:                                 unless (@newcoowners > 0) {
10215:                                     $delcoowners = 1;
10216:                                     $coowners = '';
10217:                                 }
10218:                             }
10219:                         }
10220:                         if (@newcoowners || $delcoowners) {
10221:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
10222:                                             $delcoowners,@newcoowners);
10223:                         }
10224:                     }
10225:                 }
10226:             }
10227:         }
10228:     }
10229: }
10230: 
10231: sub store_coowners {
10232:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
10233:     my $cid = $cdom.'_'.$cnum;
10234:     my ($coowners,$delresult,$putresult);
10235:     if (@newcoowners) {
10236:         $coowners = join(',',@newcoowners);
10237:         my %coownershash = (
10238:                             'internal.co-owners' => $coowners,
10239:                            );
10240:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
10241:         if ($putresult eq 'ok') {
10242:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
10243:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
10244:             }
10245:         }
10246:     }
10247:     if ($delcoowners) {
10248:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
10249:         if ($delresult eq 'ok') {
10250:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
10251:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
10252:             }
10253:         }
10254:     }
10255:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
10256:         my %crsinfo =
10257:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
10258:         if (ref($crsinfo{$cid}) eq 'HASH') {
10259:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
10260:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
10261:         }
10262:     }
10263: }
10264: 
10265: # -------------------------------------------------- Modify user authentication
10266: # Overrides without validation
10267: 
10268: sub modifyuserauth {
10269:     my ($udom,$uname,$umode,$upass)=@_;
10270:     my $uhome=&homeserver($uname,$udom);
10271:     my $allowed;
10272:     if (&allowed('mau',$udom)) {
10273:         $allowed = 1;
10274:     } elsif (($umode eq 'internal') && ($udom eq $env{'user.domain'}) &&
10275:              ($env{'request.course.id'}) && (&allowed('mip',$env{'request.course.id'})) &&
10276:              (!$env{'course.'.$env{'request.course.id'}.'.internal.nopasswdchg'})) {
10277:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
10278:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
10279:         if (($cdom ne '') && ($cnum ne '')) {
10280:             my $is_owner = &is_course_owner($cdom,$cnum);
10281:             if ($is_owner) {
10282:                 $allowed = 1;
10283:             }
10284:         }
10285:     }
10286:     unless ($allowed) { return 'refused'; }
10287:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
10288:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10289:              ' in domain '.$env{'request.role.domain'});  
10290:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
10291: 		     &escape($upass),$uhome);
10292:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
10293:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
10294:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10295:     &log($udom,,$uname,$uhome,
10296:         'Authentication changed by '.$env{'user.domain'}.', '.
10297:                                      $env{'user.name'}.', '.$umode.
10298:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
10299:     unless ($reply eq 'ok') {
10300:         &logthis('Authentication mode error: '.$reply);
10301: 	return 'error: '.$reply;
10302:     }   
10303:     return 'ok';
10304: }
10305: 
10306: # --------------------------------------------------------------- Modify a user
10307: 
10308: sub modifyuser {
10309:     my ($udom,    $uname, $uid,
10310:         $umode,   $upass, $first,
10311:         $middle,  $last,  $gene,
10312:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
10313:     $udom= &LONCAPA::clean_domain($udom);
10314:     $uname=&LONCAPA::clean_username($uname);
10315:     my $showcandelete = 'none';
10316:     if (ref($candelete) eq 'ARRAY') {
10317:         if (@{$candelete} > 0) {
10318:             $showcandelete = join(', ',@{$candelete});
10319:         }
10320:     }
10321:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
10322:              $umode.', '.$first.', '.$middle.', '.
10323: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
10324:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
10325:                                      ' desiredhome not specified'). 
10326:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
10327:              ' in domain '.$env{'request.role.domain'});
10328:     my $uhome=&homeserver($uname,$udom,'true');
10329:     my $newuser;
10330:     if ($uhome eq 'no_host') {
10331:         $newuser = 1;
10332:         unless (($umode && ($upass ne '')) || ($umode eq 'localauth') ||
10333:                 ($umode eq 'lti')) {
10334:             return 'error: more information needed to create new user';
10335:         }
10336:     }
10337: # ----------------------------------------------------------------- Create User
10338:     if (($uhome eq 'no_host') && 
10339: 	(($umode && $upass) || ($umode eq 'localauth') || ($umode eq 'lti'))) {
10340:         my $unhome='';
10341:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
10342:             $unhome = $desiredhome;
10343: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
10344: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
10345:         } else { # load balancing routine for determining $unhome
10346:             my $loadm=10000000;
10347: 	    my %servers = &get_servers($udom,'library');
10348: 	    foreach my $tryserver (keys(%servers)) {
10349: 		my $answer=reply('load',$tryserver);
10350: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
10351: 		    $loadm=$answer;
10352: 		    $unhome=$tryserver;
10353: 		}
10354: 	    }
10355:         }
10356:         if (($unhome eq '') || ($unhome eq 'no_host')) {
10357: 	    return 'error: unable to find a home server for '.$uname.
10358:                    ' in domain '.$udom;
10359:         }
10360:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
10361:                          &escape($upass),$unhome);
10362: 	unless ($reply eq 'ok') {
10363:             return 'error: '.$reply;
10364:         }   
10365:         $uhome=&homeserver($uname,$udom,'true');
10366:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
10367: 	    return 'error: unable verify users home machine.';
10368:         }
10369:     }   # End of creation of new user
10370: # ---------------------------------------------------------------------- Add ID
10371:     if ($uid) {
10372:        $uid=~tr/A-Z/a-z/;
10373:        my %uidhash=&idrget($udom,$uname);
10374:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
10375:          && (!$forceid)) {
10376: 	  unless ($uid eq $uidhash{$uname}) {
10377: 	      return 'error: user id "'.$uid.'" does not match '.
10378:                   'current user id "'.$uidhash{$uname}.'".';
10379:           }
10380:        } else {
10381: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
10382:        }
10383:     }
10384: # -------------------------------------------------------------- Add names, etc
10385:     my @tmp=&get('environment',
10386: 		   ['firstname','middlename','lastname','generation','id',
10387:                     'permanentemail','inststatus'],
10388: 		   $udom,$uname);
10389:     my (%names,%oldnames);
10390:     if ($tmp[0] =~ m/^error:.*/) { 
10391:         %names=(); 
10392:     } else {
10393:         %names = @tmp;
10394:         %oldnames = %names;
10395:     }
10396: #
10397: # If name, email and/or uid are blank (e.g., because an uploaded file
10398: # of users did not contain them), do not overwrite existing values
10399: # unless field is in $candelete array ref.  
10400: #
10401: 
10402:     my @fields = ('firstname','middlename','lastname','generation',
10403:                   'permanentemail','id');
10404:     my %newvalues;
10405:     if (ref($candelete) eq 'ARRAY') {
10406:         foreach my $field (@fields) {
10407:             if (grep(/^\Q$field\E$/,@{$candelete})) {
10408:                 if ($field eq 'firstname') {
10409:                     $names{$field} = $first;
10410:                 } elsif ($field eq 'middlename') {
10411:                     $names{$field} = $middle;
10412:                 } elsif ($field eq 'lastname') {
10413:                     $names{$field} = $last;
10414:                 } elsif ($field eq 'generation') { 
10415:                     $names{$field} = $gene;
10416:                 } elsif ($field eq 'permanentemail') {
10417:                     $names{$field} = $email;
10418:                 } elsif ($field eq 'id') {
10419:                     $names{$field}  = $uid;
10420:                 }
10421:             }
10422:         }
10423:     }
10424:     if ($first)  { $names{'firstname'}  = $first; }
10425:     if (defined($middle)) { $names{'middlename'} = $middle; }
10426:     if ($last)   { $names{'lastname'}   = $last; }
10427:     if (defined($gene))   { $names{'generation'} = $gene; }
10428:     if ($email) {
10429:        $email=~s/[^\w\@\.\-\,]//gs;
10430:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
10431:     }
10432:     if ($uid) { $names{'id'}  = $uid; }
10433:     if (defined($inststatus)) {
10434:         $names{'inststatus'} = '';
10435:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
10436:         if (ref($usertypes) eq 'HASH') {
10437:             my @okstatuses; 
10438:             foreach my $item (split(/:/,$inststatus)) {
10439:                 if (defined($usertypes->{$item})) {
10440:                     push(@okstatuses,$item);  
10441:                 }
10442:             }
10443:             if (@okstatuses) {
10444:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
10445:             }
10446:         }
10447:     }
10448:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
10449:                  $umode.', '.$first.', '.$middle.', '.
10450:                  $last.', '.$gene.', '.$email.', '.$inststatus;
10451:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
10452:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
10453:     } else {
10454:         $logmsg .= ' during self creation';
10455:     }
10456:     my $changed;
10457:     if ($newuser) {
10458:         $changed = 1;
10459:     } else {
10460:         foreach my $field (@fields) {
10461:             if ($names{$field} ne $oldnames{$field}) {
10462:                 $changed = 1;
10463:                 last;
10464:             }
10465:         }
10466:     }
10467:     unless ($changed) {
10468:         $logmsg = 'No changes in user information needed for: '.$logmsg;
10469:         &logthis($logmsg);
10470:         return 'ok';
10471:     }
10472:     my $reply = &put('environment', \%names, $udom,$uname);
10473:     if ($reply ne 'ok') { 
10474:         return 'error: '.$reply;
10475:     }
10476:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
10477:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
10478:     }
10479:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
10480:     &devalidate_cache_new('namescache',$uname.':'.$udom);
10481:     $logmsg = 'Success modifying user '.$logmsg;
10482:     &logthis($logmsg);
10483:     return 'ok';
10484: }
10485: 
10486: # -------------------------------------------------------------- Modify student
10487: 
10488: sub modifystudent {
10489:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
10490:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
10491:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
10492:     if (!$cid) {
10493: 	unless ($cid=$env{'request.course.id'}) {
10494: 	    return 'not_in_class';
10495: 	}
10496:     }
10497: # --------------------------------------------------------------- Make the user
10498:     my $reply=&modifyuser
10499: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
10500:          $desiredhome,$email,$inststatus);
10501:     unless ($reply eq 'ok') { return $reply; }
10502:     # This will cause &modify_student_enrollment to get the uid from the
10503:     # student's environment
10504:     $uid = undef if (!$forceid);
10505:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
10506:                                         $gene,$usec,$end,$start,$type,$locktype,
10507:                                         $cid,$selfenroll,$context,$credits,$instsec);
10508:     return $reply;
10509: }
10510: 
10511: sub modify_student_enrollment {
10512:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
10513:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
10514:     my ($cdom,$cnum,$chome);
10515:     if (!$cid) {
10516: 	unless ($cid=$env{'request.course.id'}) {
10517: 	    return 'not_in_class';
10518: 	}
10519: 	$cdom=$env{'course.'.$cid.'.domain'};
10520: 	$cnum=$env{'course.'.$cid.'.num'};
10521:     } else {
10522: 	($cdom,$cnum)=split(/_/,$cid);
10523:     }
10524:     $chome=$env{'course.'.$cid.'.home'};
10525:     if (!$chome) {
10526: 	$chome=&homeserver($cnum,$cdom);
10527:     }
10528:     if (!$chome) { return 'unknown_course'; }
10529:     # Make sure the user exists
10530:     my $uhome=&homeserver($uname,$udom);
10531:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10532: 	return 'error: no such user';
10533:     }
10534:     # Get student data if we were not given enough information
10535:     if (!defined($first)  || $first  eq '' || 
10536:         !defined($last)   || $last   eq '' || 
10537:         !defined($uid)    || $uid    eq '' || 
10538:         !defined($middle) || $middle eq '' || 
10539:         !defined($gene)   || $gene   eq '') {
10540:         # They did not supply us with enough data to enroll the student, so
10541:         # we need to pick up more information.
10542:         my %tmp = &get('environment',
10543:                        ['firstname','middlename','lastname', 'generation','id']
10544:                        ,$udom,$uname);
10545: 
10546:         #foreach my $key (keys(%tmp)) {
10547:         #    &logthis("key $key = ".$tmp{$key});
10548:         #}
10549:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
10550:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
10551:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
10552:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
10553:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
10554:     }
10555:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
10556:     my $user = "$uname:$udom";
10557:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
10558:     my $reply=cput('classlist',
10559: 		   {$user => 
10560: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
10561: 		   $cdom,$cnum);
10562:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
10563:         &devalidate_getsection_cache($udom,$uname,$cid);
10564:     } else { 
10565: 	return 'error: '.$reply;
10566:     }
10567:     # Add student role to user
10568:     my $uurl='/'.$cid;
10569:     $uurl=~s/\_/\//g;
10570:     if ($usec) {
10571: 	$uurl.='/'.$usec;
10572:     }
10573:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
10574:                              $selfenroll,$context);
10575:     if ($result ne 'ok') {
10576:         if ($old_entry{$user} ne '') {
10577:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
10578:         } else {
10579:             $reply = &del('classlist',[$user],$cdom,$cnum);
10580:         }
10581:     }
10582:     return $result; 
10583: }
10584: 
10585: sub format_name {
10586:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
10587:     my $name;
10588:     if ($first ne 'lastname') {
10589: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
10590:     } else {
10591: 	if ($lastname=~/\S/) {
10592: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
10593: 	    $name=~s/\s+,/,/;
10594: 	} else {
10595: 	    $name.= $firstname.' '.$middlename.' '.$generation;
10596: 	}
10597:     }
10598:     $name=~s/^\s+//;
10599:     $name=~s/\s+$//;
10600:     $name=~s/\s+/ /g;
10601:     return $name;
10602: }
10603: 
10604: # ------------------------------------------------- Write to course preferences
10605: 
10606: sub writecoursepref {
10607:     my ($courseid,%prefs)=@_;
10608:     $courseid=~s/^\///;
10609:     $courseid=~s/\_/\//g;
10610:     my ($cdomain,$cnum)=split(/\//,$courseid);
10611:     my $chome=homeserver($cnum,$cdomain);
10612:     if (($chome eq '') || ($chome eq 'no_host')) { 
10613: 	return 'error: no such course';
10614:     }
10615:     my $cstring='';
10616:     foreach my $pref (keys(%prefs)) {
10617: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
10618:     }
10619:     $cstring=~s/\&$//;
10620:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
10621: }
10622: 
10623: # ---------------------------------------------------------- Make/modify course
10624: 
10625: sub createcourse {
10626:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
10627:         $course_owner,$crstype,$cnum,$context,$category,$callercontext)=@_;
10628:     $url=&declutter($url);
10629:     my $cid='';
10630:     if ($context eq 'requestcourses') {
10631:         my $can_create = 0;
10632:         my ($ownername,$ownerdom) = split(':',$course_owner);
10633:         if ($udom eq $ownerdom) {
10634:             my $reload;
10635:             if (($callercontext eq 'auto') &&
10636:                ($ownerdom eq $env{'user.domain'}) && ($ownername eq $env{'user.name'})) {
10637:                 $reload = 'reload';
10638:             }
10639:             if (&usertools_access($ownername,$ownerdom,$category,$reload,
10640:                                   $context)) {
10641:                 $can_create = 1;
10642:             }
10643:         } else {
10644:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
10645:                                            $category);
10646:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
10647:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
10648:                 if (@curr > 0) {
10649:                     my @options = qw(approval validate autolimit);
10650:                     my $optregex = join('|',@options);
10651:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
10652:                         $can_create = 1;
10653:                     }
10654:                 }
10655:             }
10656:         }
10657:         if ($can_create) {
10658:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
10659:                 unless (&allowed('ccc',$udom)) {
10660:                     return 'refused'; 
10661:                 }
10662:             }
10663:         } else {
10664:             return 'refused';
10665:         }
10666:     } elsif (!&allowed('ccc',$udom)) {
10667:         return 'refused';
10668:     }
10669: # --------------------------------------------------------------- Get Unique ID
10670:     my $uname;
10671:     if ($cnum =~ /^$match_courseid$/) {
10672:         my $chome=&homeserver($cnum,$udom,'true');
10673:         if (($chome eq '') || ($chome eq 'no_host')) {
10674:             $uname = $cnum;
10675:         } else {
10676:             $uname = &generate_coursenum($udom,$crstype);
10677:         }
10678:     } else {
10679:         $uname = &generate_coursenum($udom,$crstype);
10680:     }
10681:     return $uname if ($uname =~ /^error/);
10682: # -------------------------------------------------- Check supplied server name
10683:     if (!defined($course_server)) {
10684:         if (defined(&domain($udom,'primary'))) {
10685:             $course_server = &domain($udom,'primary');
10686:         } else {
10687:             $course_server = $env{'user.home'}; 
10688:         }
10689:     }
10690:     my %host_servers =
10691:         &Apache::lonnet::get_servers($udom,'library');
10692:     unless ($host_servers{$course_server}) {
10693:         return 'error: invalid home server for course: '.$course_server;
10694:     }
10695: # ------------------------------------------------------------- Make the course
10696:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
10697:                       $course_server);
10698:     unless ($reply eq 'ok') { return 'error: '.$reply; }
10699:     my $uhome=&homeserver($uname,$udom,'true');
10700:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
10701: 	return 'error: no such course';
10702:     }
10703: # ----------------------------------------------------------------- Course made
10704: # log existence
10705:     my $now = time;
10706:     my $newcourse = {
10707:                     $udom.'_'.$uname => {
10708:                                      description => $description,
10709:                                      inst_code   => $inst_code,
10710:                                      owner       => $course_owner,
10711:                                      type        => $crstype,
10712:                                      creator     => $env{'user.name'}.':'.
10713:                                                     $env{'user.domain'},
10714:                                      created     => $now,
10715:                                      context     => $context,
10716:                                                 },
10717:                     };
10718:     &courseidput($udom,$newcourse,$uhome,'notime');
10719: # set toplevel url
10720:     my $topurl=$url;
10721:     unless ($nonstandard) {
10722: # ------------------------------------------ For standard courses, make top url
10723:         my $mapurl=&clutter($url);
10724:         if ($mapurl eq '/res/') { $mapurl=''; }
10725:         $env{'form.initmap'}=(<<ENDINITMAP);
10726: <map>
10727: <resource id="1" type="start"></resource>
10728: <resource id="2" src="$mapurl"></resource>
10729: <resource id="3" type="finish"></resource>
10730: <link index="1" from="1" to="2"></link>
10731: <link index="2" from="2" to="3"></link>
10732: </map>
10733: ENDINITMAP
10734:         $topurl=&declutter(
10735:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
10736:                           );
10737:     }
10738: # ----------------------------------------------------------- Write preferences
10739:     &writecoursepref($udom.'_'.$uname,
10740:                      ('description'              => $description,
10741:                       'url'                      => $topurl,
10742:                       'internal.creator'         => $env{'user.name'}.':'.
10743:                                                     $env{'user.domain'},
10744:                       'internal.created'         => $now,
10745:                       'internal.creationcontext' => $context)
10746:                     );
10747:     return '/'.$udom.'/'.$uname;
10748: }
10749: 
10750: # ------------------------------------------------------------------- Create ID
10751: sub generate_coursenum {
10752:     my ($udom,$crstype) = @_;
10753:     my $domdesc = &domain($udom);
10754:     return 'error: invalid domain' if ($domdesc eq '');
10755:     my $first;
10756:     if ($crstype eq 'Community') {
10757:         $first = '0';
10758:     } else {
10759:         $first = int(1+rand(9)); 
10760:     } 
10761:     my $uname=$first.
10762:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10763:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
10764:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10765: # ----------------------------------------------- Make sure that does not exist
10766:     my $uhome=&homeserver($uname,$udom,'true');
10767:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
10768:         if ($crstype eq 'Community') {
10769:             $first = '0';
10770:         } else {
10771:             $first = int(1+rand(9));
10772:         }
10773:         $uname=$first.
10774:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
10775:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
10776:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
10777:         $uhome=&homeserver($uname,$udom,'true');
10778:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
10779:             return 'error: unable to generate unique course-ID';
10780:         }
10781:     }
10782:     return $uname;
10783: }
10784: 
10785: sub is_course {
10786:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
10787:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
10788: 
10789:     return unless (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/));
10790:     my $uhome=&homeserver($cnum,$cdom);
10791:     my $iscourse;
10792:     if (grep { $_ eq $uhome } current_machine_ids()) {
10793:         $iscourse = &LONCAPA::Lond::is_course($cdom,$cnum);
10794:     } else {
10795:         my $hashid = $cdom.':'.$cnum;
10796:         ($iscourse,my $cached) = &is_cached_new('iscourse',$hashid);
10797:         unless (defined($cached)) {
10798:             my %courses = &courseiddump($cdom, '.', 1, '.', '.',
10799:                                         $cnum,undef,undef,'.');
10800:             $iscourse = 0;
10801:             if (exists($courses{$cdom.'_'.$cnum})) {
10802:                 $iscourse = 1;
10803:             }
10804:             &do_cache_new('iscourse',$hashid,$iscourse,3600);
10805:         }
10806:     }
10807:     return unless ($iscourse);
10808:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
10809: }
10810: 
10811: sub store_userdata {
10812:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
10813:     my $result;
10814:     if ($datakey ne '') {
10815:         if (ref($storehash) eq 'HASH') {
10816:             if ($udom eq '' || $uname eq '') {
10817:                 $udom = $env{'user.domain'};
10818:                 $uname = $env{'user.name'};
10819:             }
10820:             my $uhome=&homeserver($uname,$udom);
10821:             if (($uhome eq '') || ($uhome eq 'no_host')) {
10822:                 $result = 'error: no_host';
10823:             } else {
10824:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
10825:                 $storehash->{'host'} = $perlvar{'lonHostID'};
10826: 
10827:                 my $namevalue='';
10828:                 foreach my $key (keys(%{$storehash})) {
10829:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
10830:                 }
10831:                 $namevalue=~s/\&$//;
10832:                 unless ($namespace eq 'courserequests') {
10833:                     $datakey = &escape($datakey);
10834:                 }
10835:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
10836:                                   $namevalue,$uhome);
10837:             }
10838:         } else {
10839:             $result = 'error: data to store was not a hash reference'; 
10840:         }
10841:     } else {
10842:         $result= 'error: invalid requestkey'; 
10843:     }
10844:     return $result;
10845: }
10846: 
10847: # ---------------------------------------------------------- Assign Custom Role
10848: 
10849: sub assigncustomrole {
10850:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
10851:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
10852:                        $end,$start,$deleteflag,$selfenroll,$context);
10853: }
10854: 
10855: # ----------------------------------------------------------------- Revoke Role
10856: 
10857: sub revokerole {
10858:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
10859:     my $now=time;
10860:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
10861: }
10862: 
10863: # ---------------------------------------------------------- Revoke Custom Role
10864: 
10865: sub revokecustomrole {
10866:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
10867:     my $now=time;
10868:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
10869:            $deleteflag,$selfenroll,$context);
10870: }
10871: 
10872: # ------------------------------------------------------------ Disk usage
10873: sub diskusage {
10874:     my ($udom,$uname,$directorypath,$getpropath)=@_;
10875:     $directorypath =~ s/\/$//;
10876:     my $listing=&reply('du2:'.&escape($directorypath).':'
10877:                        .&escape($getpropath).':'.&escape($uname).':'
10878:                        .&escape($udom),homeserver($uname,$udom));
10879:     if ($listing eq 'unknown_cmd') {
10880:         if ($getpropath) {
10881:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
10882:         }
10883:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
10884:     }
10885:     return $listing;
10886: }
10887: 
10888: sub is_locked {
10889:     my ($file_name, $domain, $user, $which) = @_;
10890:     my @check;
10891:     my $is_locked;
10892:     push (@check,$file_name);
10893:     my %locked = &get('file_permissions',\@check,
10894: 		      $env{'user.domain'},$env{'user.name'});
10895:     my ($tmp)=keys(%locked);
10896:     if ($tmp=~/^error:/) { undef(%locked); }
10897:     
10898:     if (ref($locked{$file_name}) eq 'ARRAY') {
10899:         $is_locked = 'false';
10900:         foreach my $entry (@{$locked{$file_name}}) {
10901:            if (ref($entry) eq 'ARRAY') {
10902:                $is_locked = 'true';
10903:                if (ref($which) eq 'ARRAY') {
10904:                    push(@{$which},$entry);
10905:                } else {
10906:                    last;
10907:                }
10908:            }
10909:        }
10910:     } else {
10911:         $is_locked = 'false';
10912:     }
10913:     return $is_locked;
10914: }
10915: 
10916: sub declutter_portfile {
10917:     my ($file) = @_;
10918:     $file =~ s{^(/portfolio/|portfolio/)}{/};
10919:     return $file;
10920: }
10921: 
10922: # ------------------------------------------------------------- Mark as Read Only
10923: 
10924: sub mark_as_readonly {
10925:     my ($domain,$user,$files,$what) = @_;
10926:     my %current_permissions = &dump('file_permissions',$domain,$user);
10927:     my ($tmp)=keys(%current_permissions);
10928:     if ($tmp=~/^error:/) { undef(%current_permissions); }
10929:     foreach my $file (@{$files}) {
10930: 	$file = &declutter_portfile($file);
10931:         push(@{$current_permissions{$file}},$what);
10932:     }
10933:     &put('file_permissions',\%current_permissions,$domain,$user);
10934:     return;
10935: }
10936: 
10937: # ------------------------------------------------------------Save Selected Files
10938: 
10939: sub save_selected_files {
10940:     my ($user, $path, @files) = @_;
10941:     my $filename = $user."savedfiles";
10942:     my @other_files = &files_not_in_path($user, $path);
10943:     open (OUT,'>',LONCAPA::tempdir().$filename);
10944:     foreach my $file (@files) {
10945:         print (OUT $env{'form.currentpath'}.$file."\n");
10946:     }
10947:     foreach my $file (@other_files) {
10948:         print (OUT $file."\n");
10949:     }
10950:     close (OUT);
10951:     return 'ok';
10952: }
10953: 
10954: sub clear_selected_files {
10955:     my ($user) = @_;
10956:     my $filename = $user."savedfiles";
10957:     open (OUT,'>',LONCAPA::tempdir().$filename);
10958:     print (OUT undef);
10959:     close (OUT);
10960:     return ("ok");    
10961: }
10962: 
10963: sub files_in_path {
10964:     my ($user, $path) = @_;
10965:     my $filename = $user."savedfiles";
10966:     my %return_files;
10967:     open (IN,'<',LONCAPA::tempdir().$filename);
10968:     while (my $line_in = <IN>) {
10969:         chomp ($line_in);
10970:         my @paths_and_file = split (m!/!, $line_in);
10971:         my $file_part = pop (@paths_and_file);
10972:         my $path_part = join ('/', @paths_and_file);
10973:         $path_part.='/';
10974:         my $path_and_file = $path_part.$file_part;
10975:         if ($path_part eq $path) {
10976:             $return_files{$file_part}= 'selected';
10977:         }
10978:     }
10979:     close (IN);
10980:     return (\%return_files);
10981: }
10982: 
10983: # called in portfolio select mode, to show files selected NOT in current directory
10984: sub files_not_in_path {
10985:     my ($user, $path) = @_;
10986:     my $filename = $user."savedfiles";
10987:     my @return_files;
10988:     my $path_part;
10989:     open(IN, '<',LONCAPA::tempdir().$filename);
10990:     while (my $line = <IN>) {
10991:         #ok, I know it's clunky, but I want it to work
10992:         my @paths_and_file = split(m|/|, $line);
10993:         my $file_part = pop(@paths_and_file);
10994:         chomp($file_part);
10995:         my $path_part = join('/', @paths_and_file);
10996:         $path_part .= '/';
10997:         my $path_and_file = $path_part.$file_part;
10998:         if ($path_part ne $path) {
10999:             push(@return_files, ($path_and_file));
11000:         }
11001:     }
11002:     close(OUT);
11003:     return (@return_files);
11004: }
11005: 
11006: #------------------------------Submitted/Handedback Portfolio Files Versioning
11007:  
11008: sub portfiles_versioning {
11009:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
11010:     my $portfolio_root = '/userfiles/portfolio';
11011:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
11012:     foreach my $file (@{$portfiles}) {
11013:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
11014:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
11015:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
11016:         my $getpropath = 1;
11017:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
11018:                                              $stu_name,$getpropath);
11019:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
11020:         my $new_answer = 
11021:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
11022:         if ($new_answer ne 'problem getting file') {
11023:             push(@{$versioned_portfiles}, $directory.$new_answer);
11024:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
11025:                               [$symb,$env{'request.course.id'},'graded']);
11026:         }
11027:     }
11028: }
11029: 
11030: sub get_next_version {
11031:     my ($answer_name, $answer_ext, $dir_list) = @_;
11032:     my $version;
11033:     if (ref($dir_list) eq 'ARRAY') {
11034:         foreach my $row (@{$dir_list}) {
11035:             my ($file) = split(/\&/,$row,2);
11036:             my ($file_name,$file_version,$file_ext) =
11037:                 &file_name_version_ext($file);
11038:             if (($file_name eq $answer_name) &&
11039:                 ($file_ext eq $answer_ext)) {
11040:                      # gets here if filename and extension match,
11041:                      # regardless of version
11042:                 if ($file_version ne '') {
11043:                     # a versioned file is found  so save it for later
11044:                     if ($file_version > $version) {
11045:                         $version = $file_version;
11046:                     }
11047:                 }
11048:             }
11049:         }
11050:     }
11051:     $version ++;
11052:     return($version);
11053: }
11054: 
11055: sub version_selected_portfile {
11056:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
11057:     my ($answer_name,$answer_ver,$answer_ext) =
11058:         &file_name_version_ext($file_name);
11059:     my $new_answer;
11060:     $env{'form.copy'} =
11061:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
11062:     if($env{'form.copy'} eq '-1') {
11063:         $new_answer = 'problem getting file';
11064:     } else {
11065:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
11066:         my $copy_result = 
11067:             &finishuserfileupload($stu_name,$domain,'copy',
11068:                                   '/portfolio'.$directory.$new_answer);
11069:     }
11070:     undef($env{'form.copy'});
11071:     return ($new_answer);
11072: }
11073: 
11074: sub file_name_version_ext {
11075:     my ($file)=@_;
11076:     my @file_parts = split(/\./, $file);
11077:     my ($name,$version,$ext);
11078:     if (@file_parts > 1) {
11079:         $ext=pop(@file_parts);
11080:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
11081:             $version=pop(@file_parts);
11082:         }
11083:         $name=join('.',@file_parts);
11084:     } else {
11085:         $name=join('.',@file_parts);
11086:     }
11087:     return($name,$version,$ext);
11088: }
11089: 
11090: #----------------------------------------------Get portfolio file permissions
11091: 
11092: sub get_portfile_permissions {
11093:     my ($domain,$user) = @_;
11094:     my %current_permissions = &dump('file_permissions',$domain,$user);
11095:     my ($tmp)=keys(%current_permissions);
11096:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11097:     return \%current_permissions;
11098: }
11099: 
11100: #---------------------------------------------Get portfolio file access controls
11101: 
11102: sub get_access_controls {
11103:     my ($current_permissions,$group,$file) = @_;
11104:     my %access;
11105:     my $real_file = $file;
11106:     $file =~ s/\.meta$//;
11107:     if (defined($file)) {
11108:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
11109:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
11110:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
11111:             }
11112:         }
11113:     } else {
11114:         foreach my $key (keys(%{$current_permissions})) {
11115:             if ($key =~ /\0accesscontrol$/) {
11116:                 if (defined($group)) {
11117:                     if ($key !~ m-^\Q$group\E/-) {
11118:                         next;
11119:                     }
11120:                 }
11121:                 my ($fullpath) = split(/\0/,$key);
11122:                 if (ref($$current_permissions{$key}) eq 'HASH') {
11123:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
11124:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
11125:                     }
11126:                 }
11127:             }
11128:         }
11129:     }
11130:     return %access;
11131: }
11132: 
11133: sub modify_access_controls {
11134:     my ($file_name,$changes,$domain,$user)=@_;
11135:     my ($outcome,$deloutcome);
11136:     my %store_permissions;
11137:     my %new_values;
11138:     my %new_control;
11139:     my %translation;
11140:     my @deletions = ();
11141:     my $now = time;
11142:     if (exists($$changes{'activate'})) {
11143:         if (ref($$changes{'activate'}) eq 'HASH') {
11144:             my @newitems = sort(keys(%{$$changes{'activate'}}));
11145:             my $numnew = scalar(@newitems);
11146:             for (my $i=0; $i<$numnew; $i++) {
11147:                 my $newkey = $newitems[$i];
11148:                 my $newid = &Apache::loncommon::get_cgi_id();
11149:                 if ($newkey =~ /^\d+:/) { 
11150:                     $newkey =~ s/^(\d+)/$newid/;
11151:                     $translation{$1} = $newid;
11152:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
11153:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
11154:                     $translation{$1} = $newid;
11155:                 }
11156:                 $new_values{$file_name."\0".$newkey} = 
11157:                                           $$changes{'activate'}{$newitems[$i]};
11158:                 $new_control{$newkey} = $now;
11159:             }
11160:         }
11161:     }
11162:     my %todelete;
11163:     my %changed_items;
11164:     foreach my $action ('delete','update') {
11165:         if (exists($$changes{$action})) {
11166:             if (ref($$changes{$action}) eq 'HASH') {
11167:                 foreach my $key (keys(%{$$changes{$action}})) {
11168:                     my ($itemnum) = ($key =~ /^([^:]+):/);
11169:                     if ($action eq 'delete') { 
11170:                         $todelete{$itemnum} = 1;
11171:                     } else {
11172:                         $changed_items{$itemnum} = $key;
11173:                     }
11174:                 }
11175:             }
11176:         }
11177:     }
11178:     # get lock on access controls for file.
11179:     my $lockhash = {
11180:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
11181:                                                        ':'.$env{'user.domain'},
11182:                    }; 
11183:     my $tries = 0;
11184:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11185:    
11186:     while (($gotlock ne 'ok') && $tries < 10) {
11187:         $tries ++;
11188:         sleep(0.1);
11189:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
11190:     }
11191:     if ($gotlock eq 'ok') {
11192:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
11193:         my ($tmp)=keys(%curr_permissions);
11194:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
11195:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
11196:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
11197:             if (ref($curr_controls) eq 'HASH') {
11198:                 foreach my $control_item (keys(%{$curr_controls})) {
11199:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
11200:                     if (defined($todelete{$itemnum})) {
11201:                         push(@deletions,$file_name."\0".$control_item);
11202:                     } else {
11203:                         if (defined($changed_items{$itemnum})) {
11204:                             $new_control{$changed_items{$itemnum}} = $now;
11205:                             push(@deletions,$file_name."\0".$control_item);
11206:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
11207:                         } else {
11208:                             $new_control{$control_item} = $$curr_controls{$control_item};
11209:                         }
11210:                     }
11211:                 }
11212:             }
11213:         }
11214:         my ($group);
11215:         if (&is_course($domain,$user)) {
11216:             ($group,my $file) = split(/\//,$file_name,2);
11217:         }
11218:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
11219:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
11220:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
11221:         #  remove lock
11222:         my @del_lock = ($file_name."\0".'locked_access_records');
11223:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
11224:         my $sqlresult =
11225:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
11226:                                     $group);
11227:     } else {
11228:         $outcome = "error: could not obtain lockfile\n";  
11229:     }
11230:     return ($outcome,$deloutcome,\%new_values,\%translation);
11231: }
11232: 
11233: sub make_public_indefinitely {
11234:     my (@requrl) = @_;
11235:     return &automated_portfile_access('public',\@requrl);
11236: }
11237: 
11238: sub automated_portfile_access {
11239:     my ($accesstype,$addsref,$delsref,$info) = @_;
11240:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
11241:         return 'invalid';
11242:     }
11243:     my %urls;
11244:     if (ref($addsref) eq 'ARRAY') {
11245:         foreach my $requrl (@{$addsref}) {
11246:             if (&is_portfolio_url($requrl)) {
11247:                 unless (exists($urls{$requrl})) {
11248:                     $urls{$requrl} = 'add';
11249:                 }
11250:             }
11251:         }
11252:     }
11253:     if (ref($delsref) eq 'ARRAY') {
11254:         foreach my $requrl (@{$delsref}) { 
11255:             if (&is_portfolio_url($requrl)) {
11256:                 unless (exists($urls{$requrl})) {
11257:                     $urls{$requrl} = 'delete'; 
11258:                 }
11259:             }
11260:         }
11261:     }
11262:     unless (keys(%urls)) {
11263:         return 'invalid';
11264:     }
11265:     my $ip;
11266:     if ($accesstype eq 'ip') {
11267:         if (ref($info) eq 'HASH') {
11268:             if ($info->{'ip'} ne '') {
11269:                 $ip = $info->{'ip'};
11270:             }
11271:         }
11272:         if ($ip eq '') {
11273:             return 'invalid';
11274:         }
11275:     }
11276:     my $errors;
11277:     my $now = time;
11278:     my %current_perms;
11279:     foreach my $requrl (sort(keys(%urls))) {
11280:         my $action;
11281:         if ($urls{$requrl} eq 'add') {
11282:             $action = 'activate';
11283:         } else {
11284:             $action = 'none';
11285:         }
11286:         my $aclnum = 0;
11287:         my (undef,$udom,$unum,$file_name,$group) =
11288:             &parse_portfolio_url($requrl);
11289:         unless (exists($current_perms{$unum.':'.$udom})) {
11290:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
11291:         }
11292:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
11293:                                                    $group,$file_name);
11294:         foreach my $key (keys(%{$access_controls{$file_name}})) {
11295:             my ($num,$scope,$end,$start) = 
11296:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
11297:             if ($scope eq $accesstype) {
11298:                 if (($start <= $now) && ($end == 0)) {
11299:                     if ($accesstype eq 'ip') {
11300:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
11301:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
11302:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
11303:                                     if ($urls{$requrl} eq 'add') {
11304:                                         $action = 'none';
11305:                                         last;
11306:                                     } else {
11307:                                         $action = 'delete';
11308:                                         $aclnum = $num;
11309:                                         last;
11310:                                     }
11311:                                 }
11312:                             }
11313:                         }
11314:                     } elsif ($accesstype eq 'public') {
11315:                         if ($urls{$requrl} eq 'add') {
11316:                             $action = 'none';
11317:                             last;
11318:                         } else {
11319:                             $action = 'delete';
11320:                             $aclnum = $num;
11321:                             last;
11322:                         }
11323:                     }
11324:                 } elsif ($accesstype eq 'public') {
11325:                     $action = 'update';
11326:                     $aclnum = $num;
11327:                     last;
11328:                 }
11329:             }
11330:         }
11331:         if ($action eq 'none') {
11332:             next;
11333:         } else {
11334:             my %changes;
11335:             my $newend = 0;
11336:             my $newstart = $now;
11337:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
11338:             $changes{$action}{$newkey} = {
11339:                 type => $accesstype,
11340:                 time => {
11341:                     start => $newstart,
11342:                     end   => $newend,
11343:                 },
11344:             };
11345:             if ($accesstype eq 'ip') {
11346:                 $changes{$action}{$newkey}{'ip'} = [$ip];
11347:             }
11348:             my ($outcome,$deloutcome,$new_values,$translation) =
11349:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
11350:             unless ($outcome eq 'ok') {
11351:                 $errors .= $outcome.' ';
11352:             }
11353:         }
11354:     }
11355:     if ($errors) {
11356:         $errors =~ s/\s$//;
11357:         return $errors;
11358:     } else {
11359:         return 'ok';
11360:     }
11361: }
11362: 
11363: #------------------------------------------------------Get Marked as Read Only
11364: 
11365: sub get_marked_as_readonly {
11366:     my ($domain,$user,$what,$group) = @_;
11367:     my $current_permissions = &get_portfile_permissions($domain,$user);
11368:     my @readonly_files;
11369:     my $cmp1=$what;
11370:     if (ref($what)) { $cmp1=join('',@{$what}) };
11371:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11372:         if (defined($group)) {
11373:             if ($file_name !~ m-^\Q$group\E/-) {
11374:                 next;
11375:             }
11376:         }
11377:         if (ref($value) eq "ARRAY"){
11378:             foreach my $stored_what (@{$value}) {
11379:                 my $cmp2=$stored_what;
11380:                 if (ref($stored_what) eq 'ARRAY') {
11381:                     $cmp2=join('',@{$stored_what});
11382:                 }
11383:                 if ($cmp1 eq $cmp2) {
11384:                     push(@readonly_files, $file_name);
11385:                     last;
11386:                 } elsif (!defined($what)) {
11387:                     push(@readonly_files, $file_name);
11388:                     last;
11389:                 }
11390:             }
11391:         }
11392:     }
11393:     return @readonly_files;
11394: }
11395: #-----------------------------------------------------------Get Marked as Read Only Hash
11396: 
11397: sub get_marked_as_readonly_hash {
11398:     my ($current_permissions,$group,$what) = @_;
11399:     my %readonly_files;
11400:     while (my ($file_name,$value) = each(%{$current_permissions})) {
11401:         if (defined($group)) {
11402:             if ($file_name !~ m-^\Q$group\E/-) {
11403:                 next;
11404:             }
11405:         }
11406:         if (ref($value) eq "ARRAY"){
11407:             foreach my $stored_what (@{$value}) {
11408:                 if (ref($stored_what) eq 'ARRAY') {
11409:                     foreach my $lock_descriptor(@{$stored_what}) {
11410:                         if ($lock_descriptor eq 'graded') {
11411:                             $readonly_files{$file_name} = 'graded';
11412:                         } elsif ($lock_descriptor eq 'handback') {
11413:                             $readonly_files{$file_name} = 'handback';
11414:                         } else {
11415:                             if (!exists($readonly_files{$file_name})) {
11416:                                 $readonly_files{$file_name} = 'locked';
11417:                             }
11418:                         }
11419:                     }
11420:                 } 
11421:             }
11422:         } 
11423:     }
11424:     return %readonly_files;
11425: }
11426: # ------------------------------------------------------------ Unmark as Read Only
11427: 
11428: sub unmark_as_readonly {
11429:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
11430:     # for portfolio submissions, $what contains [$symb,$crsid] 
11431:     my ($domain,$user,$what,$file_name,$group) = @_;
11432:     $file_name = &declutter_portfile($file_name);
11433:     my $symb_crs = $what;
11434:     if (ref($what)) { $symb_crs=join('',@$what); }
11435:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
11436:     my ($tmp)=keys(%current_permissions);
11437:     if ($tmp=~/^error:/) { undef(%current_permissions); }
11438:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
11439:     foreach my $file (@readonly_files) {
11440: 	my $clean_file = &declutter_portfile($file);
11441: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
11442: 	my $current_locks = $current_permissions{$file};
11443:         my @new_locks;
11444:         my @del_keys;
11445:         if (ref($current_locks) eq "ARRAY"){
11446:             foreach my $locker (@{$current_locks}) {
11447:                 my $compare=$locker;
11448:                 if (ref($locker) eq 'ARRAY') {
11449:                     $compare=join('',@{$locker});
11450:                     if ($compare ne $symb_crs) {
11451:                         push(@new_locks, $locker);
11452:                     }
11453:                 }
11454:             }
11455:             if (scalar(@new_locks) > 0) {
11456:                 $current_permissions{$file} = \@new_locks;
11457:             } else {
11458:                 push(@del_keys, $file);
11459:                 &del('file_permissions',\@del_keys, $domain, $user);
11460:                 delete($current_permissions{$file});
11461:             }
11462:         }
11463:     }
11464:     &put('file_permissions',\%current_permissions,$domain,$user);
11465:     return;
11466: }
11467: 
11468: # ------------------------------------------------------------ Directory lister
11469: 
11470: sub dirlist {
11471:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
11472:     $uri=~s/^\///;
11473:     $uri=~s/\/$//;
11474:     my ($udom, $uname);
11475:     if ($getuserdir) {
11476:         $udom = $userdomain;
11477:         $uname = $username;
11478:     } else {
11479:         (undef,$udom,$uname)=split(/\//,$uri);
11480:         if(defined($userdomain)) {
11481:             $udom = $userdomain;
11482:         }
11483:         if(defined($username)) {
11484:             $uname = $username;
11485:         }
11486:     }
11487:     my ($dirRoot,$listing,@listing_results);
11488: 
11489:     $dirRoot = $perlvar{'lonDocRoot'};
11490:     if (defined($getpropath)) {
11491:         $dirRoot = &propath($udom,$uname);
11492:         $dirRoot =~ s/\/$//;
11493:     } elsif (defined($getuserdir)) {
11494:         my $subdir=$uname.'__';
11495:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
11496:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
11497:                    ."/$udom/$subdir/$uname";
11498:     } elsif (defined($alternateRoot)) {
11499:         $dirRoot = $alternateRoot;
11500:     }
11501: 
11502:     if($udom) {
11503:         if($uname) {
11504:             my $uhome = &homeserver($uname,$udom);
11505:             if ($uhome eq 'no_host') {
11506:                 return ([],'no_host');
11507:             }
11508:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
11509:                               .$getuserdir.':'.&escape($dirRoot)
11510:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
11511:             if ($listing eq 'unknown_cmd') {
11512:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
11513:             } else {
11514:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11515:             }
11516:             if ($listing eq 'unknown_cmd') {
11517:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
11518:                 @listing_results = split(/:/,$listing);
11519:             } else {
11520:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
11521:             }
11522:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
11523:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
11524:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11525:                 return ([],$listing);
11526:             } else {
11527:                 return (\@listing_results);
11528:             }
11529:         } elsif(!$alternateRoot) {
11530:             my (%allusers,%listerror);
11531: 	    my %servers = &get_servers($udom,'library');
11532:  	    foreach my $tryserver (keys(%servers)) {
11533:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
11534:                                   &escape($udom),$tryserver);
11535:                 if ($listing eq 'unknown_cmd') {
11536: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
11537: 				      $udom, $tryserver);
11538:                 } else {
11539:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
11540:                 }
11541: 		if ($listing eq 'unknown_cmd') {
11542: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
11543: 				      $udom, $tryserver);
11544: 		    @listing_results = split(/:/,$listing);
11545: 		} else {
11546: 		    @listing_results =
11547: 			map { &unescape($_); } split(/:/,$listing);
11548: 		}
11549:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
11550:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
11551:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
11552:                     $listerror{$tryserver} = $listing;
11553:                 } else {
11554: 		    foreach my $line (@listing_results) {
11555: 			my ($entry) = split(/&/,$line,2);
11556: 			$allusers{$entry} = 1;
11557: 		    }
11558: 		}
11559:             }
11560:             my @alluserslist=();
11561:             foreach my $user (sort(keys(%allusers))) {
11562:                 push(@alluserslist,$user.'&user');
11563:             }
11564: 
11565:             if (!%listerror) {
11566:                 # no errors
11567:                 return (\@alluserslist);
11568:             } elsif (scalar(keys(%servers)) == 1) {
11569:                 # one library server, one error 
11570:                 my ($key) = keys(%listerror);
11571:                 return (\@alluserslist, $listerror{$key});
11572:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
11573:                 # con_lost indicates that we might miss data from at least one
11574:                 # library server
11575:                 return (\@alluserslist, 'con_lost');
11576:             } else {
11577:                 # multiple library servers and no con_lost -> data should be
11578:                 # complete. 
11579:                 return (\@alluserslist);
11580:             }
11581: 
11582:         } else {
11583:             return ([],'missing username');
11584:         }
11585:     } elsif(!defined($getpropath)) {
11586:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
11587:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
11588:         return (\@all_domains);
11589:     } else {
11590:         return ([],'missing domain');
11591:     }
11592: }
11593: 
11594: # --------------------------------------------- GetFileTimestamp
11595: # This function utilizes dirlist and returns the date stamp for
11596: # when it was last modified.  It will also return an error of -1
11597: # if an error occurs
11598: 
11599: sub GetFileTimestamp {
11600:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
11601:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
11602:     $studentName   = &LONCAPA::clean_username($studentName);
11603:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
11604:                                     undef,$getuserdir);
11605:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11606:         return -1;
11607:     }
11608:     if (ref($fileref) eq 'ARRAY') {
11609:         my @stats = split('&',$fileref->[0]);
11610:         # @stats contains first the filename, then the stat output
11611:         return $stats[10]; # so this is 10 instead of 9.
11612:     } else {
11613:         return -1;
11614:     }
11615: }
11616: 
11617: sub stat_file {
11618:     my ($uri) = @_;
11619:     $uri = &clutter_with_no_wrapper($uri);
11620: 
11621:     my ($udom,$uname,$file);
11622:     if ($uri =~ m-^/(uploaded|editupload)/-) {
11623: 	($udom,$uname,$file) =
11624: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
11625: 	$file = 'userfiles/'.$file;
11626:     }
11627:     if ($uri =~ m-^/res/-) {
11628: 	($udom,$uname) = 
11629: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
11630: 	$file = $uri;
11631:     }
11632: 
11633:     if (!$udom || !$uname || !$file) {
11634: 	# unable to handle the uri
11635: 	return ();
11636:     }
11637:     my $getpropath;
11638:     if ($file =~ /^userfiles\//) {
11639:         $getpropath = 1;
11640:     }
11641:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
11642:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
11643:         return ();
11644:     } else {
11645:         if (ref($listref) eq 'ARRAY') {
11646:             my @stats = split('&',$listref->[0]);
11647: 	    shift(@stats); #filename is first
11648: 	    return @stats;
11649:         }
11650:     }
11651:     return ();
11652: }
11653: 
11654: # --------------------------------------------------------- recursedirs
11655: # Recursive function to traverse either a specific user's Authoring Space
11656: # or corresponding Published Resource Space, and populate the hash ref:
11657: # $dirhashref with URLs of all directories, and if $filehashref hash
11658: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
11659: # or .rights files in resource space, and .meta, .save, .log, and .bak
11660: # files in Authoring Space.
11661: #
11662: # Inputs:
11663: #
11664: # $is_home - true if current server is home server for user's space
11665: # $context - either: priv, or res respectively for Authoring or Resource Space.
11666: # $docroot - Document root (i.e., /home/httpd/html
11667: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
11668: # $relpath - Current path (relative to top level).
11669: # $dirhashref - reference to hash to populate with URLs of directories (Required)
11670: # $filehashref - reference to hash to populate with URLs of files (Optional)
11671: #
11672: # Returns: nothing
11673: #
11674: # Side Effects: populates $dirhashref, and $filehashref (if provided).
11675: #
11676: # Currently used by interface/londocs.pm to create linked select boxes for
11677: # directory and filename to import a Course "Author" resource into a course, and
11678: # also to create linked select boxes for Authoring Space and Directory to choose
11679: # save location for creation of a new "standard" problem from the Course Editor.
11680: #
11681: 
11682: sub recursedirs {
11683:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
11684:     return unless (ref($dirhashref) eq 'HASH');
11685:     my $currpath = $docroot.$toppath;
11686:     if ($relpath) {
11687:         $currpath .= "/$relpath";
11688:     }
11689:     my $savefile;
11690:     if (ref($filehashref)) {
11691:         $savefile = 1;
11692:     }
11693:     if ($is_home) {
11694:         if (opendir(my $dirh,$currpath)) {
11695:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
11696:                 next if ($item eq '');
11697:                 if (-d "$currpath/$item") {
11698:                     my $newpath;
11699:                     if ($relpath) {
11700:                         $newpath = "$relpath/$item";
11701:                     } else {
11702:                         $newpath = $item;
11703:                     }
11704:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11705:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11706:                 } elsif ($savefile) {
11707:                     if ($context eq 'priv') {
11708:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11709:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11710:                         }
11711:                     } else {
11712:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
11713:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
11714:                         }
11715:                     }
11716:                 }
11717:             }
11718:             closedir($dirh);
11719:         }
11720:     } else {
11721:         my ($dirlistref,$listerror) =
11722:             &dirlist($toppath.$relpath);
11723:         my @dir_lines;
11724:         my $dirptr=16384;
11725:         if (ref($dirlistref) eq 'ARRAY') {
11726:             foreach my $dir_line (sort
11727:                               {
11728:                                   my ($afile)=split('&',$a,2);
11729:                                   my ($bfile)=split('&',$b,2);
11730:                                   return (lc($afile) cmp lc($bfile));
11731:                               } (@{$dirlistref})) {
11732:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
11733:                     split(/\&/,$dir_line,16);
11734:                 $item =~ s/\s+$//;
11735:                 next if (($item =~ /^\.\.?$/) || ($obs));
11736:                 if ($dirptr&$testdir) {
11737:                     my $newpath;
11738:                     if ($relpath) {
11739:                         $newpath = "$relpath/$item";
11740:                     } else {
11741:                         $relpath = '/';
11742:                         $newpath = $item;
11743:                     }
11744:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
11745:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
11746:                 } elsif ($savefile) {
11747:                     if ($context eq 'priv') {
11748:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
11749:                             $filehashref->{$relpath}{$item} = 1;
11750:                         }
11751:                     } else {
11752:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
11753:                             $filehashref->{$relpath}{$item} = 1;
11754:                         }
11755:                     }
11756:                 }
11757:             }
11758:         }
11759:     }
11760:     return;
11761: }
11762: 
11763: # -------------------------------------------------------- Value of a Condition
11764: 
11765: # gets the value of a specific preevaluated condition
11766: #    stored in the string  $env{user.state.<cid>}
11767: # or looks up a condition reference in the bighash and if if hasn't
11768: # already been evaluated recurses into docondval to get the value of
11769: # the condition, then memoizing it to 
11770: #   $env{user.state.<cid>.<condition>}
11771: sub directcondval {
11772:     my $number=shift;
11773:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
11774: 	&Apache::lonuserstate::evalstate();
11775:     }
11776:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
11777: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
11778:     } elsif ($number =~ /^_/) {
11779: 	my $sub_condition;
11780: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
11781: 		&GDBM_READER(),0640)) {
11782: 	    $sub_condition=$bighash{'conditions'.$number};
11783: 	    untie(%bighash);
11784: 	}
11785: 	my $value = &docondval($sub_condition);
11786: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
11787: 	return $value;
11788:     }
11789:     if ($env{'user.state.'.$env{'request.course.id'}}) {
11790:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
11791:     } else {
11792:        return 2;
11793:     }
11794: }
11795: 
11796: # get the collection of conditions for this resource
11797: sub condval {
11798:     my $condidx=shift;
11799:     my $allpathcond='';
11800:     foreach my $cond (split(/\|/,$condidx)) {
11801: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
11802: 	    $allpathcond.=
11803: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
11804: 	}
11805:     }
11806:     $allpathcond=~s/\|$//;
11807:     return &docondval($allpathcond);
11808: }
11809: 
11810: #evaluates an expression of conditions
11811: sub docondval {
11812:     my ($allpathcond) = @_;
11813:     my $result=0;
11814:     if ($env{'request.course.id'}
11815: 	&& defined($allpathcond)) {
11816: 	my $operand='|';
11817: 	my @stack;
11818: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
11819: 	    if ($chunk eq '(') {
11820: 		push @stack,($operand,$result);
11821: 	    } elsif ($chunk eq ')') {
11822: 		my $before=pop @stack;
11823: 		if (pop @stack eq '&') {
11824: 		    $result=$result>$before?$before:$result;
11825: 		} else {
11826: 		    $result=$result>$before?$result:$before;
11827: 		}
11828: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
11829: 		$operand=$chunk;
11830: 	    } else {
11831: 		my $new=directcondval($chunk);
11832: 		if ($operand eq '&') {
11833: 		    $result=$result>$new?$new:$result;
11834: 		} else {
11835: 		    $result=$result>$new?$result:$new;
11836: 		}
11837: 	    }
11838: 	}
11839:     }
11840:     return $result;
11841: }
11842: 
11843: # ---------------------------------------------------- Devalidate courseresdata
11844: 
11845: sub devalidatecourseresdata {
11846:     my ($coursenum,$coursedomain)=@_;
11847:     my $hashid=$coursenum.':'.$coursedomain;
11848:     &devalidate_cache_new('courseres',$hashid);
11849: }
11850: 
11851: 
11852: # --------------------------------------------------- Course Resourcedata Query
11853: #
11854: #  Parameters:
11855: #      $coursenum    - Number of the course.
11856: #      $coursedomain - Domain at which the course was created.
11857: #  Returns:
11858: #     A hash of the course parameters along (I think) with timestamps
11859: #     and version info.
11860: 
11861: sub get_courseresdata {
11862:     my ($coursenum,$coursedomain)=@_;
11863:     my $coursehom=&homeserver($coursenum,$coursedomain);
11864:     my $hashid=$coursenum.':'.$coursedomain;
11865:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
11866:     my %dumpreply;
11867:     unless (defined($cached)) {
11868: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
11869: 	$result=\%dumpreply;
11870: 	my ($tmp) = keys(%dumpreply);
11871: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
11872: 	    &do_cache_new('courseres',$hashid,$result,600);
11873: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
11874: 	    return $tmp;
11875: 	} elsif ($tmp =~ /^(error)/) {
11876: 	    $result=undef;
11877: 	    &do_cache_new('courseres',$hashid,$result,600);
11878: 	}
11879:     }
11880:     return $result;
11881: }
11882: 
11883: sub devalidateuserresdata {
11884:     my ($uname,$udom)=@_;
11885:     my $hashid="$udom:$uname";
11886:     &devalidate_cache_new('userres',$hashid);
11887: }
11888: 
11889: sub get_userresdata {
11890:     my ($uname,$udom)=@_;
11891:     #most student don\'t have any data set, check if there is some data
11892:     if (&EXT_cache_status($udom,$uname)) { return undef; }
11893: 
11894:     my $hashid="$udom:$uname";
11895:     my ($result,$cached)=&is_cached_new('userres',$hashid);
11896:     if (!defined($cached)) {
11897: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
11898: 	$result=\%resourcedata;
11899: 	&do_cache_new('userres',$hashid,$result,600);
11900:     }
11901:     my ($tmp)=keys(%$result);
11902:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
11903: 	return $result;
11904:     }
11905:     #error 2 occurs when the .db doesn't exist
11906:     if ($tmp!~/error: 2 /) {
11907:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
11908: 	    &logthis("<font color=\"blue\">WARNING:".
11909: 		     " Trying to get resource data for ".
11910: 		     $uname." at ".$udom.": ".
11911: 		     $tmp."</font>");
11912:         }
11913:     } elsif ($tmp=~/error: 2 /) {
11914: 	#&EXT_cache_set($udom,$uname);
11915: 	&do_cache_new('userres',$hashid,undef,600);
11916: 	undef($tmp); # not really an error so don't send it back
11917:     }
11918:     return $tmp;
11919: }
11920: #----------------------------------------------- resdata - return resource data
11921: #  Purpose:
11922: #    Return resource data for either users or for a course.
11923: #  Parameters:
11924: #     $name      - Course/user name.
11925: #     $domain    - Name of the domain the user/course is registered on.
11926: #     $type      - Type of thing $name is (must be 'course' or 'user')
11927: #     $mapp      - decluttered URL of enclosing map  
11928: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
11929: #     $recurseup - Ref to array of map URLs, starting with map containing
11930: #                  $mapp up through hierarchy of nested maps to top level map.  
11931: #     $courseid  - CourseID (first part of param identifier).
11932: #     $modifier  - Middle part of param identifier.
11933: #     $what      - Last part of param identifier.
11934: #     @which     - Array of names of resources desired.
11935: #  Returns:
11936: #     The value of the first reasource in @which that is found in the
11937: #     resource hash.
11938: #  Exceptional Conditions:
11939: #     If the $type passed in is not valid (not the string 'course' or 
11940: #     'user', an undefined  reference is returned.
11941: #     If none of the resources are found, an undef is returned
11942: sub resdata {
11943:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
11944:         $modifier,$what,@which)=@_;
11945:     my $result;
11946:     if ($type eq 'course') {
11947: 	$result=&get_courseresdata($name,$domain);
11948:     } elsif ($type eq 'user') {
11949: 	$result=&get_userresdata($name,$domain);
11950:     }
11951:     if (!ref($result)) { return $result; }    
11952:     foreach my $item (@which) {
11953:         if ($item->[1] eq 'course') {
11954:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
11955:                 unless ($$recursed) {
11956:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
11957:                     $$recursed = 1;
11958:                 }
11959:                 foreach my $item (@${recurseup}) {
11960:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
11961:                     last if (defined($result->{$norecursechk}));
11962:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
11963:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
11964:                 }
11965:             }
11966:         }
11967:         if (defined($result->{$item->[0]})) {
11968: 	    return [$result->{$item->[0]},$item->[1]];
11969: 	}
11970:     }
11971:     return undef;
11972: }
11973: 
11974: sub get_domain_lti {
11975:     my ($cdom,$context) = @_;
11976:     my ($name,%lti);
11977:     if ($context eq 'consumer') {
11978:         $name = 'ltitools';
11979:     } elsif ($context eq 'provider') {
11980:         $name = 'lti';
11981:     } else {
11982:         return %lti;
11983:     }
11984:     my ($result,$cached)=&is_cached_new($name,$cdom);
11985:     if (defined($cached)) {
11986:         if (ref($result) eq 'HASH') {
11987:             %lti = %{$result};
11988:         }
11989:     } else {
11990:         my %domconfig = &get_dom('configuration',[$name],$cdom);
11991:         if (ref($domconfig{$name}) eq 'HASH') {
11992:             %lti = %{$domconfig{$name}};
11993:             my %encdomconfig = &get_dom('encconfig',[$name],$cdom);
11994:             if (ref($encdomconfig{$name}) eq 'HASH') {
11995:                 foreach my $id (keys(%lti)) {
11996:                     if (ref($encdomconfig{$name}{$id}) eq 'HASH') {
11997:                         foreach my $item ('key','secret') {
11998:                             $lti{$id}{$item} = $encdomconfig{$name}{$id}{$item};
11999:                         }
12000:                     }
12001:                 }
12002:             }
12003:         }
12004:         my $cachetime = 24*60*60;
12005:         &do_cache_new($name,$cdom,\%lti,$cachetime);
12006:     }
12007:     return %lti;
12008: }
12009: 
12010: sub get_numsuppfiles {
12011:     my ($cnum,$cdom,$ignorecache)=@_;
12012:     my $hashid=$cnum.':'.$cdom;
12013:     my ($suppcount,$cached);
12014:     unless ($ignorecache) {
12015:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
12016:     }
12017:     unless (defined($cached)) {
12018:         my $chome=&homeserver($cnum,$cdom);
12019:         unless ($chome eq 'no_host') {
12020:             ($suppcount,my $supptools,my $errors) = (0,0,0);
12021:             my $suppmap = 'supplemental.sequence';
12022:             ($suppcount,$supptools,$errors) =
12023:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,
12024:                                                          $supptools,$errors);
12025:         }
12026:         &do_cache_new('suppcount',$hashid,$suppcount,600);
12027:     }
12028:     return $suppcount;
12029: }
12030: 
12031: #
12032: # EXT resource caching routines
12033: #
12034: 
12035: {
12036: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
12037: #
12038: # The course for which we cache
12039: my $cachedmapkey='';
12040: # The cached recursive maps for this course
12041: my %cachedmaps=();
12042: # When this was last done
12043: my $cachedmaptime='';
12044: 
12045: sub clear_EXT_cache_status {
12046:     &delenv('cache.EXT.');
12047: }
12048: 
12049: sub EXT_cache_status {
12050:     my ($target_domain,$target_user) = @_;
12051:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12052:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
12053:         # We know already the user has no data
12054:         return 1;
12055:     } else {
12056:         return 0;
12057:     }
12058: }
12059: 
12060: sub EXT_cache_set {
12061:     my ($target_domain,$target_user) = @_;
12062:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
12063:     #&appenv({$cachename => time});
12064: }
12065: 
12066: # --------------------------------------------------------- Value of a Variable
12067: sub EXT {
12068: 
12069:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
12070:     unless ($varname) { return ''; }
12071:     #get real user name/domain, courseid and symb
12072:     my $courseid;
12073:     my $publicuser;
12074:     if ($symbparm) {
12075: 	$symbparm=&get_symb_from_alias($symbparm);
12076:     }
12077:     if (!($uname && $udom)) {
12078:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
12079:       if (!$symbparm) {	$symbparm=$cursymb; }
12080:     } else {
12081: 	$courseid=$env{'request.course.id'};
12082:     }
12083:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
12084:     my $rest;
12085:     if (defined($therest[0])) {
12086:        $rest=join('.',@therest);
12087:     } else {
12088:        $rest='';
12089:     }
12090: 
12091:     my $qualifierrest=$qualifier;
12092:     if ($rest) { $qualifierrest.='.'.$rest; }
12093:     my $spacequalifierrest=$space;
12094:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
12095:     if ($realm eq 'user') {
12096: # --------------------------------------------------------------- user.resource
12097: 	if ($space eq 'resource') {
12098: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
12099: 		  || defined($Apache::lonhomework::parsing_a_task))
12100: 		 &&
12101: 		 ($symbparm eq &symbread()) ) {	
12102: 		# if we are in the middle of processing the resource the
12103: 		# get the value we are planning on committing
12104:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
12105:                     return $Apache::lonhomework::results{$qualifierrest};
12106:                 } else {
12107:                     return $Apache::lonhomework::history{$qualifierrest};
12108:                 }
12109: 	    } else {
12110: 		my %restored;
12111: 		if ($publicuser || $env{'request.state'} eq 'construct') {
12112: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
12113: 		} else {
12114: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
12115: 		}
12116: 		return $restored{$qualifierrest};
12117: 	    }
12118: # ----------------------------------------------------------------- user.access
12119:         } elsif ($space eq 'access') {
12120: 	    # FIXME - not supporting calls for a specific user
12121:             return &allowed($qualifier,$rest);
12122: # ------------------------------------------ user.preferences, user.environment
12123:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
12124: 	    if (($uname eq $env{'user.name'}) &&
12125: 		($udom eq $env{'user.domain'})) {
12126: 		return $env{join('.',('environment',$qualifierrest))};
12127: 	    } else {
12128: 		my %returnhash;
12129: 		if (!$publicuser) {
12130: 		    %returnhash=&userenvironment($udom,$uname,
12131: 						 $qualifierrest);
12132: 		}
12133: 		return $returnhash{$qualifierrest};
12134: 	    }
12135: # ----------------------------------------------------------------- user.course
12136:         } elsif ($space eq 'course') {
12137: 	    # FIXME - not supporting calls for a specific user
12138:             return $env{join('.',('request.course',$qualifier))};
12139: # ------------------------------------------------------------------- user.role
12140:         } elsif ($space eq 'role') {
12141: 	    # FIXME - not supporting calls for a specific user
12142:             my ($role,$where)=split(/\./,$env{'request.role'});
12143:             if ($qualifier eq 'value') {
12144: 		return $role;
12145:             } elsif ($qualifier eq 'extent') {
12146:                 return $where;
12147:             }
12148: # ----------------------------------------------------------------- user.domain
12149:         } elsif ($space eq 'domain') {
12150:             return $udom;
12151: # ------------------------------------------------------------------- user.name
12152:         } elsif ($space eq 'name') {
12153:             return $uname;
12154: # ---------------------------------------------------- Any other user namespace
12155:         } else {
12156: 	    my %reply;
12157: 	    if (!$publicuser) {
12158: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
12159: 	    }
12160: 	    return $reply{$qualifierrest};
12161:         }
12162:     } elsif ($realm eq 'query') {
12163: # ---------------------------------------------- pull stuff out of query string
12164:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
12165: 						[$spacequalifierrest]);
12166: 	return $env{'form.'.$spacequalifierrest}; 
12167:    } elsif ($realm eq 'request') {
12168: # ------------------------------------------------------------- request.browser
12169:         if ($space eq 'browser') {
12170:             return $env{'browser.'.$qualifier};
12171: # ------------------------------------------------------------ request.filename
12172:         } else {
12173:             return $env{'request.'.$spacequalifierrest};
12174:         }
12175:     } elsif ($realm eq 'course') {
12176: # ---------------------------------------------------------- course.description
12177:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
12178:     } elsif ($realm eq 'resource') {
12179: 
12180: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
12181: 	    if (!$symbparm) { $symbparm=&symbread(); }
12182: 	}
12183: 
12184:         if ($qualifier eq '') {
12185: 	    if ($space eq 'title') {
12186: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
12187: 	        return &gettitle($symbparm);
12188: 	    }
12189: 	
12190: 	    if ($space eq 'map') {
12191: 	        my ($map) = &decode_symb($symbparm);
12192: 	        return &symbread($map);
12193: 	    }
12194:             if ($space eq 'maptitle') {
12195:                 my ($map) = &decode_symb($symbparm);
12196:                 return &gettitle($map);
12197:             }
12198: 	    if ($space eq 'filename') {
12199: 	        if ($symbparm) {
12200: 		    return &clutter((&decode_symb($symbparm))[2]);
12201: 	        }
12202: 	        return &hreflocation('',$env{'request.filename'});
12203: 	    }
12204: 
12205:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
12206:                 if ($space eq 'visibleparts') {
12207:                     my $navmap = Apache::lonnavmaps::navmap->new();
12208:                     my $item;
12209:                     if (ref($navmap)) {
12210:                         my $res = $navmap->getBySymb($symbparm);
12211:                         my $parts = $res->parts();
12212:                         if (ref($parts) eq 'ARRAY') {
12213:                             $item = join(',',@{$parts});
12214:                         }
12215:                         undef($navmap);
12216:                     }
12217:                     return $item;
12218:                 }
12219:             }
12220:         }
12221: 
12222: 	my ($section, $group, @groups, @recurseup, $recursed);
12223: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
12224:         if (($courseid eq '') && ($cid)) {
12225:             $courseid = $cid;
12226:         }
12227: 	if (($symbparm && $courseid) && 
12228: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
12229: 
12230: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
12231: 
12232: # ----------------------------------------------------- Cascading lookup scheme
12233: 	    my $symbp=$symbparm;
12234: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
12235: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
12236:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
12237: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
12238: 	    if (($env{'user.name'} eq $uname) &&
12239: 		($env{'user.domain'} eq $udom)) {
12240: 		$section=$env{'request.course.sec'};
12241:                 @groups = split(/:/,$env{'request.course.groups'});  
12242:                 @groups=&sort_course_groups($courseid,@groups); 
12243: 	    } else {
12244: 		if (! defined($usection)) {
12245: 		    $section=&getsection($udom,$uname,$courseid);
12246: 		} else {
12247: 		    $section = $usection;
12248: 		}
12249:                 @groups = &get_users_groups($udom,$uname,$courseid);
12250: 	    }
12251: 
12252: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
12253: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
12254:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
12255: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
12256: 
12257: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
12258: 	    my $courselevelr=$courseid.'.'.$symbparm;
12259:             $courseleveli=$courseid.'.'.$recurseparm;
12260: 	    $courselevelm=$courseid.'.'.$mapparm;
12261: 
12262: # ----------------------------------------------------------- first, check user
12263: 
12264: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
12265:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
12266: 				       ([$courselevelr,'resource'],
12267: 					[$courselevelm,'map'     ],
12268:                                         [$courseleveli,'map'     ],
12269: 					[$courselevel, 'course'  ]));
12270: 	    if (defined($userreply)) { return &get_reply($userreply); }
12271: 
12272: # ------------------------------------------------ second, check some of course
12273:             my $coursereply;
12274:             if (@groups > 0) {
12275:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
12276:                                        $recurseparm,$mapparm,$spacequalifierrest,
12277:                                        $mapp,\$recursed,\@recurseup);
12278:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
12279:             }
12280: 
12281: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12282: 				  $env{'course.'.$courseid.'.domain'},
12283: 				  'course',$mapp,\$recursed,\@recurseup,
12284:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
12285: 				  ([$seclevelr,   'resource'],
12286: 				   [$seclevelm,   'map'     ],
12287:                                    [$secleveli,   'map'     ],
12288: 				   [$seclevel,    'course'  ],
12289: 				   [$courselevelr,'resource']));
12290: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12291: 
12292: # ------------------------------------------------------ third, check map parms
12293: 	    my %parmhash=();
12294: 	    my $thisparm='';
12295: 	    if (tie(%parmhash,'GDBM_File',
12296: 		    $env{'request.course.fn'}.'_parms.db',
12297: 		    &GDBM_READER(),0640)) {
12298: 		$thisparm=$parmhash{$symbparm};
12299: 		untie(%parmhash);
12300: 	    }
12301: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
12302: 	}
12303: # ------------------------------------------ fourth, look in resource metadata
12304:  
12305:         my $what = $spacequalifierrest;
12306: 	$what=~s/\./\_/;
12307: 	my $filename;
12308: 	if (!$symbparm) { $symbparm=&symbread(); }
12309: 	if ($symbparm) {
12310: 	    $filename=(&decode_symb($symbparm))[2];
12311: 	} else {
12312: 	    $filename=$env{'request.filename'};
12313: 	}
12314:         my $toolsymb;
12315:         if (($filename =~ /ext\.tool$/) && ($what ne '0_gradable')) {
12316:             $toolsymb = $symbparm;
12317:         }
12318: 	my $metadata=&metadata($filename,$what,$toolsymb);
12319: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12320: 	$metadata=&metadata($filename,'parameter_'.$what,$toolsymb);
12321: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
12322: 
12323: # ----------------------------------------------- fifth, look in rest of course
12324: 	if ($symbparm && defined($courseid) && 
12325: 	    $courseid eq $env{'request.course.id'}) {
12326: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
12327: 				     $env{'course.'.$courseid.'.domain'},
12328: 				     'course',$mapp,\$recursed,\@recurseup,
12329:                                      $courseid,'.',$spacequalifierrest,
12330: 				     ([$courselevelm,'map'   ],
12331:                                       [$courseleveli,'map'   ],
12332: 				      [$courselevel, 'course']));
12333: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
12334: 	}
12335: # ------------------------------------------------------------------ Cascade up
12336: 	unless ($space eq '0') {
12337: 	    my @parts=split(/_/,$space);
12338: 	    my $id=pop(@parts);
12339: 	    my $part=join('_',@parts);
12340: 	    if ($part eq '') { $part='0'; }
12341: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
12342: 				 $symbparm,$udom,$uname,$section,1);
12343: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
12344: 	}
12345: 	if ($recurse) { return undef; }
12346: 	my $pack_def=&packages_tab_default($filename,$varname,$toolsymb);
12347: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
12348: # ---------------------------------------------------- Any other user namespace
12349:     } elsif ($realm eq 'environment') {
12350: # ----------------------------------------------------------------- environment
12351: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
12352: 	    return $env{'environment.'.$spacequalifierrest};
12353: 	} else {
12354: 	    if ($uname eq 'anonymous' && $udom eq '') {
12355: 		return '';
12356: 	    }
12357: 	    my %returnhash=&userenvironment($udom,$uname,
12358: 					    $spacequalifierrest);
12359: 	    return $returnhash{$spacequalifierrest};
12360: 	}
12361:     } elsif ($realm eq 'system') {
12362: # ----------------------------------------------------------------- system.time
12363: 	if ($space eq 'time') {
12364: 	    return time;
12365:         }
12366:     } elsif ($realm eq 'server') {
12367: # ----------------------------------------------------------------- system.time
12368: 	if ($space eq 'name') {
12369: 	    return $ENV{'SERVER_NAME'};
12370:         }
12371:     } elsif ($realm eq 'client') {
12372:         if ($space eq 'remote_addr') {
12373:             return $ENV{'REMOTE_ADDR'};
12374:         }
12375:     }
12376:     return '';
12377: }
12378: 
12379: sub get_reply {
12380:     my ($reply_value) = @_;
12381:     if (ref($reply_value) eq 'ARRAY') {
12382:         if (wantarray) {
12383: 	    return @$reply_value;
12384:         }
12385:         return $reply_value->[0];
12386:     } else {
12387:         return $reply_value;
12388:     }
12389: }
12390: 
12391: sub check_group_parms {
12392:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
12393:         $recursed,$recurseupref) = @_;
12394:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
12395:                   [$what,'course']);
12396:     my $coursereply;
12397:     foreach my $group (@{$groups}) {
12398:         my @groupitems = ();
12399:         foreach my $level (@levels) {
12400:              my $item = $courseid.'.['.$group.'].'.$level->[0];
12401:              push(@groupitems,[$item,$level->[1]]);
12402:         }
12403:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
12404:                                    $env{'course.'.$courseid.'.domain'},
12405:                                    'course',$mapp,$recursed,$recurseupref,
12406:                                    $courseid,'.['.$group.'].',$what,
12407:                                    @groupitems);
12408:         last if (defined($coursereply));
12409:     }
12410:     return $coursereply;
12411: }
12412: 
12413: sub get_map_hierarchy {
12414:     my ($mapname,$courseid) = @_;
12415:     my @recurseup = ();
12416:     if ($mapname) {
12417:         if (($cachedmapkey eq $courseid) &&
12418:             (abs($cachedmaptime-time)<5)) {
12419:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
12420:                 return @{$cachedmaps{$mapname}};
12421:             }
12422:         }
12423:         my $navmap = Apache::lonnavmaps::navmap->new();
12424:         if (ref($navmap)) {
12425:             @recurseup = $navmap->recurseup_maps($mapname);
12426:             undef($navmap);
12427:             $cachedmaps{$mapname} = \@recurseup;
12428:             $cachedmaptime=time;
12429:             $cachedmapkey=$courseid;
12430:         }
12431:     }
12432:     return @recurseup;
12433: }
12434: 
12435: }
12436: 
12437: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
12438:     my ($courseid,@groups) = @_;
12439:     @groups = sort(@groups);
12440:     return @groups;
12441: }
12442: 
12443: sub packages_tab_default {
12444:     my ($uri,$varname,$toolsymb)=@_;
12445:     my (undef,$part,$name)=split(/\./,$varname);
12446: 
12447:     my (@extension,@specifics,$do_default);
12448:     foreach my $package (split(/,/,&metadata($uri,'packages',$toolsymb))) {
12449: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
12450: 	if ($pack_type eq 'default') {
12451: 	    $do_default=1;
12452: 	} elsif ($pack_type eq 'extension') {
12453: 	    push(@extension,[$package,$pack_type,$pack_part]);
12454: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
12455: 	    # only look at packages defaults for packages that this id is
12456: 	    push(@specifics,[$package,$pack_type,$pack_part]);
12457: 	}
12458:     }
12459:     # first look for a package that matches the requested part id
12460:     foreach my $package (@specifics) {
12461: 	my (undef,$pack_type,$pack_part)=@{$package};
12462: 	next if ($pack_part ne $part);
12463: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12464: 	    return $packagetab{"$pack_type&$name&default"};
12465: 	}
12466:     }
12467:     # look for any possible matching non extension_ package
12468:     foreach my $package (@specifics) {
12469: 	my (undef,$pack_type,$pack_part)=@{$package};
12470: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12471: 	    return $packagetab{"$pack_type&$name&default"};
12472: 	}
12473: 	if ($pack_type eq 'part') { $pack_part='0'; }
12474: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
12475: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
12476: 	}
12477:     }
12478:     # look for any posible extension_ match
12479:     foreach my $package (@extension) {
12480: 	my ($package,$pack_type)=@{$package};
12481: 	if (defined($packagetab{"$pack_type&$name&default"})) {
12482: 	    return $packagetab{"$pack_type&$name&default"};
12483: 	}
12484: 	if (defined($packagetab{$package."&$name&default"})) {
12485: 	    return $packagetab{$package."&$name&default"};
12486: 	}
12487:     }
12488:     # look for a global default setting
12489:     if ($do_default && defined($packagetab{"default&$name&default"})) {
12490: 	return $packagetab{"default&$name&default"};
12491:     }
12492:     return undef;
12493: }
12494: 
12495: sub add_prefix_and_part {
12496:     my ($prefix,$part)=@_;
12497:     my $keyroot;
12498:     if (defined($prefix) && $prefix !~ /^__/) {
12499: 	# prefix that has a part already
12500: 	$keyroot=$prefix;
12501:     } elsif (defined($prefix)) {
12502: 	# prefix that is missing a part
12503: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
12504:     } else {
12505: 	# no prefix at all
12506: 	if (defined($part)) { $keyroot='_'.$part; }
12507:     }
12508:     return $keyroot;
12509: }
12510: 
12511: # ---------------------------------------------------------------- Get metadata
12512: 
12513: my %metaentry;
12514: my %importedpartids;
12515: my %importedrespids;
12516: sub metadata {
12517:     my ($uri,$what,$toolsymb,$liburi,$prefix,$depthcount)=@_;
12518:     $uri=&declutter($uri);
12519:     # if it is a non metadata possible uri return quickly
12520:     if (($uri eq '') || 
12521: 	(($uri =~ m|^/*adm/|) && 
12522: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
12523:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
12524: 	return undef;
12525:     }
12526:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
12527: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
12528: 	return undef;
12529:     }
12530:     my $filename=$uri;
12531:     $uri=~s/\.meta$//;
12532: #
12533: # Is the metadata already cached?
12534: # Look at timestamp of caching
12535: # Everything is cached by the main uri, libraries are never directly cached
12536: #
12537:     if (!defined($liburi)) {
12538: 	my ($result,$cached)=&is_cached_new('meta',$uri);
12539: 	if (defined($cached)) { return $result->{':'.$what}; }
12540:     }
12541: 
12542: #
12543: # If the uri is for an external tool the file from
12544: # which metadata should be retrieved depends on whether
12545: # the tool had been configured to be gradable (set in the Course
12546: # Editor or Resource Editor).
12547: #
12548: # If a valid symb has been included as the third arg in the call
12549: # to &metadata() that can be used to retrieve the value of
12550: # parameter_0_gradable set for the resource, and included in the
12551: # uploaded map containing the tool. The value is retrieved via
12552: # &EXT(), if a valid symb is available.  Otherwise the value of
12553: # gradable in the exttool_$marker.db file for the tool instance
12554: # is retrieved via &get().
12555: #
12556: # When lonuserstate::traceroute() calls lonnet::EXT() for 
12557: # hiddenresource and encrypturl (during course initialization)
12558: # the map-level parameter for resource.0.gradable included in the 
12559: # uploaded map containing the tool will not yet have been stored
12560: # in the user_course_parms.db file for the user's session, so in 
12561: # this case fall back to retrieving gradable status from the
12562: # exttool_$marker.db file.
12563: #
12564: # In order to avoid an infinite loop, &metadata() will return
12565: # before a call to &EXT(), if the uri is for an external tool
12566: # and the $what for which metadata is being requested is
12567: # parameter_0_gradable or 0_gradable.
12568: #
12569: 
12570:     if ($uri =~ /ext\.tool$/) {
12571:         if (($what eq 'parameter_0_gradable') || ($what eq '0_gradable')) {
12572:             return;
12573:         } else {
12574:             my ($checked,$use_passback);
12575:             if ($toolsymb ne '') {
12576:                 (undef,undef,my $tooluri) = &decode_symb($toolsymb);
12577:                 if (($tooluri eq $uri) && (&EXT('resource.0.gradable',$toolsymb))) {
12578:                     $checked = 1;
12579:                     if (&EXT('resource.0.gradable',$toolsymb) =~ /^yes$/i) {
12580:                         $use_passback = 1;
12581:                     }
12582:                 }
12583:             }
12584:             unless ($checked) {
12585:                 my ($ignore,$cdom,$cnum,$marker) = split(m{/},$uri);
12586:                 $marker=~s/\D//g;
12587:                 if ($marker) {
12588:                     my %toolsettings=&get('exttool_'.$marker,['gradable'],$cdom,$cnum);
12589:                     $use_passback = $toolsettings{'gradable'};
12590:                 }
12591:             }
12592:             if ($use_passback) {
12593:                 $filename = '/home/httpd/html/res/lib/templates/LTIpassback.tool';
12594:             } else {
12595:                 $filename = '/home/httpd/html/res/lib/templates/LTIstandard.tool';
12596:             }
12597:         }
12598:     }
12599: 
12600:     {
12601: # Imported parts would go here
12602:         my @origfiletagids=();
12603:         my $importedparts=0;
12604: 
12605: # Imported responseids would go here
12606:         my $importedresponses=0;
12607: #
12608: # Is this a recursive call for a library?
12609: #
12610: #	if (! exists($metacache{$uri})) {
12611: #	    $metacache{$uri}={};
12612: #	}
12613: 	my $cachetime = 60*60;
12614:         if ($liburi) {
12615: 	    $liburi=&declutter($liburi);
12616:             $filename=$liburi;
12617:         } else {
12618: 	    &devalidate_cache_new('meta',$uri);
12619: 	    undef(%metaentry);
12620: 	}
12621:         my %metathesekeys=();
12622:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
12623: 	my $metastring;
12624: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
12625: 	    my $which = &hreflocation('','/'.($liburi || $uri));
12626: 	    $metastring = 
12627: 		&Apache::lonnet::ssi_body($which,
12628: 					  ('grade_target' => 'meta'));
12629: 	    $cachetime = 1; # only want this cached in the child not long term
12630: 	} elsif (($uri !~ m -^(editupload)/-) && 
12631:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
12632: 	    my $file=&filelocation('',&clutter($filename));
12633: 	    #push(@{$metaentry{$uri.'.file'}},$file);
12634: 	    $metastring=&getfile($file);
12635: 	}
12636:         my $parser=HTML::LCParser->new(\$metastring);
12637:         my $token;
12638:         undef %metathesekeys;
12639:         while ($token=$parser->get_token) {
12640: 	    if ($token->[0] eq 'S') {
12641: 		if (defined($token->[2]->{'package'})) {
12642: #
12643: # This is a package - get package info
12644: #
12645: 		    my $package=$token->[2]->{'package'};
12646: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12647: 		    if (defined($token->[2]->{'id'})) { 
12648: 			$keyroot.='_'.$token->[2]->{'id'}; 
12649: 		    }
12650: 		    if ($metaentry{':packages'}) {
12651: 			$metaentry{':packages'}.=','.$package.$keyroot;
12652: 		    } else {
12653: 			$metaentry{':packages'}=$package.$keyroot;
12654: 		    }
12655: 		    foreach my $pack_entry (keys(%packagetab)) {
12656: 			my $part=$keyroot;
12657: 			$part=~s/^\_//;
12658: 			if ($pack_entry=~/^\Q$package\E\&/ || 
12659: 			    $pack_entry=~/^\Q$package\E_0\&/) {
12660: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
12661: 			    # ignore package.tab specified default values
12662:                             # here &package_tab_default() will fetch those
12663: 			    if ($subp eq 'default') { next; }
12664: 			    my $value=$packagetab{$pack_entry};
12665: 			    my $unikey;
12666: 			    if ($pack =~ /_0$/) {
12667: 				$unikey='parameter_0_'.$name;
12668: 				$part=0;
12669: 			    } else {
12670: 				$unikey='parameter'.$keyroot.'_'.$name;
12671: 			    }
12672: 			    if ($subp eq 'display') {
12673: 				$value.=' [Part: '.$part.']';
12674: 			    }
12675: 			    $metaentry{':'.$unikey.'.part'}=$part;
12676: 			    $metathesekeys{$unikey}=1;
12677: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12678: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
12679: 			    }
12680: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
12681: 				$metaentry{':'.$unikey}=
12682: 				    $metaentry{':'.$unikey.'.default'};
12683: 			    }
12684: 			}
12685: 		    }
12686: 		} else {
12687: #
12688: # This is not a package - some other kind of start tag
12689: #
12690: 		    my $entry=$token->[1];
12691: 		    my $unikey='';
12692: 
12693: 		    if ($entry eq 'import') {
12694: #
12695: # Importing a library here
12696: #
12697:                         my $location=$parser->get_text('/import');
12698:                         my $dir=$filename;
12699:                         $dir=~s|[^/]*$||;
12700:                         $location=&filelocation($dir,$location);
12701: 
12702:                         my $importid=$token->[2]->{'id'};
12703:                         my $importmode=$token->[2]->{'importmode'};
12704: #
12705: # Check metadata for imported file to
12706: # see if it contained response items
12707: #
12708:                         my ($origfile,@libfilekeys);
12709:                         my %currmetaentry = %metaentry;
12710:                         @libfilekeys = split(/,/,&metadata($location,'keys',undef,undef,undef,
12711:                                                            $depthcount+1));
12712:                         if (grep(/^responseorder$/,@libfilekeys)) {
12713:                             my $libresponseorder = &metadata($location,'responseorder',undef,undef,
12714:                                                              undef,$depthcount+1);
12715:                             if ($libresponseorder ne '') {
12716:                                 if ($#origfiletagids<0) {
12717:                                     undef(%importedrespids);
12718:                                     undef(%importedpartids);
12719:                                 }
12720:                                 my @respids = split(/\s*,\s*/,$libresponseorder);
12721:                                 if (@respids) {
12722:                                     $importedrespids{$importid} = join(',',map { $importid.'_'.$_ } @respids);
12723:                                 }
12724:                                 if ($importedrespids{$importid} ne '') {
12725:                                     $importedresponses = 1;
12726: # We need to get the original file and the imported file to get the response order correct
12727: # Load and inspect original file
12728:                                     if ($#origfiletagids<0) {
12729:                                         my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12730:                                         $origfile=&getfile($origfilelocation);
12731:                                         @origfiletagids=($origfile=~/<((?:\w+)response|import|part)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12732:                                     }
12733:                                 }
12734:                             }
12735:                         }
12736: # Do not overwrite contents of %metaentry hash for resource itself with 
12737: # hash populated for imported library file
12738:                         %metaentry = %currmetaentry;
12739:                         undef(%currmetaentry);
12740:                         if ($importmode eq 'part') {
12741: # Import as part(s)
12742:                            $importedparts=1;
12743: # We need to get the original file and the imported file to get the part order correct
12744: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
12745: # Load and inspect original file if we didn't do that already
12746:                            if ($#origfiletagids<0) {
12747:                                undef(%importedrespids);
12748:                                undef(%importedpartids);
12749:                                if ($origfile eq '') {
12750:                                    my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
12751:                                    $origfile=&getfile($origfilelocation);
12752:                                    @origfiletagids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12753:                                }
12754:                            }
12755:                            my @impfilepartids;
12756: # If <partorder> tag is included in metadata for the imported file
12757: # get the parts in the imported file from that.
12758:                            if (grep(/^partorder$/,@libfilekeys)) {
12759:                                %currmetaentry = %metaentry;
12760:                                my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12761:                                                             $depthcount+1);
12762:                                %metaentry = %currmetaentry;
12763:                                undef(%currmetaentry);
12764:                                if ($libpartorder ne '') {
12765:                                    @impfilepartids=split(/\s*,\s*/,$libpartorder);
12766:                                }
12767:                            } else {
12768: # If no <partorder> tag available, load and inspect imported file
12769:                                my $impfile=&getfile($location);
12770:                                @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
12771:                            }
12772:                            if ($#impfilepartids>=0) {
12773: # This problem had parts
12774:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
12775:                            } else {
12776: # Importing by turning a single problem into a problem part
12777: # It gets the import-tags ID as part-ID
12778:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
12779:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
12780:                            }
12781:                         } else {
12782: # Import as problem or as normal import
12783:                             $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
12784:                             unless ($importmode eq 'problem') {
12785: # Normal import
12786:                                 if (defined($token->[2]->{'id'})) {
12787:                                     $unikey.='_'.$token->[2]->{'id'};
12788:                                 }
12789:                             }
12790: # Check metadata for imported file to
12791: # see if it contained parts
12792:                             if (grep(/^partorder$/,@libfilekeys)) {
12793:                                 %currmetaentry = %metaentry;
12794:                                 my $libpartorder = &metadata($location,'partorder',undef,undef,undef,
12795:                                                              $depthcount+1);
12796:                                 %metaentry = %currmetaentry;
12797:                                 undef(%currmetaentry);
12798:                                 if ($libpartorder ne '') {
12799:                                     $importedparts = 1;
12800:                                     $importedpartids{$token->[2]->{'id'}}=$libpartorder;
12801:                                 }
12802:                             }
12803:                         }
12804: 			if ($depthcount<20) {
12805: 			    my $metadata = 
12806: 				&metadata($uri,'keys',$toolsymb,$location,$unikey,
12807: 					  $depthcount+1);
12808: 			    foreach my $meta (split(',',$metadata)) {
12809: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
12810: 				$metathesekeys{$meta}=1;
12811: 			    }
12812:                         }
12813: 		    } else {
12814: #
12815: # Not importing, some other kind of non-package, non-library start tag
12816: # 
12817:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
12818:                         if (defined($token->[2]->{'id'})) {
12819:                             $unikey.='_'.$token->[2]->{'id'};
12820:                         }
12821: 			if (defined($token->[2]->{'name'})) { 
12822: 			    $unikey.='_'.$token->[2]->{'name'}; 
12823: 			}
12824: 			$metathesekeys{$unikey}=1;
12825: 			foreach my $param (@{$token->[3]}) {
12826: 			    $metaentry{':'.$unikey.'.'.$param} =
12827: 				$token->[2]->{$param};
12828: 			}
12829: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
12830: 			my $default=$metaentry{':'.$unikey.'.default'};
12831: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
12832: 		 # only ws inside the tag, and not in default, so use default
12833: 		 # as value
12834: 			    $metaentry{':'.$unikey}=$default;
12835: 			} elsif ( $internaltext =~ /\S/ ) {
12836: 		  # something interesting inside the tag
12837: 			    $metaentry{':'.$unikey}=$internaltext;
12838: 			} else {
12839: 		  # no interesting values, don't set a default
12840: 			}
12841: # end of not-a-package not-a-library import
12842: 		    }
12843: # end of not-a-package start tag
12844: 		}
12845: # the next is the end of "start tag"
12846: 	    }
12847: 	}
12848: 	my ($extension) = ($uri =~ /\.(\w+)$/);
12849: 	$extension = lc($extension);
12850: 	if ($extension eq 'htm') { $extension='html'; }
12851: 
12852: 	foreach my $key (keys(%packagetab)) {
12853: 	    #no specific packages #how's our extension
12854: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
12855: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
12856: 					 \%metathesekeys);
12857: 	}
12858: 
12859: 	if (!exists($metaentry{':packages'})
12860: 	    || $packagetab{"import_defaults&extension_$extension"}) {
12861: 	    foreach my $key (keys(%packagetab)) {
12862: 		#no specific packages well let's get default then
12863: 		if ($key!~/^default&/) { next; }
12864: 		&metadata_create_package_def($uri,$key,'default',
12865: 					     \%metathesekeys);
12866: 	    }
12867: 	}
12868: # are there custom rights to evaluate
12869: 	if ($metaentry{':copyright'} eq 'custom') {
12870: 
12871:     #
12872:     # Importing a rights file here
12873:     #
12874: 	    unless ($depthcount) {
12875: 		my $location=$metaentry{':customdistributionfile'};
12876: 		my $dir=$filename;
12877: 		$dir=~s|[^/]*$||;
12878: 		$location=&filelocation($dir,$location);
12879: 		my $rights_metadata =
12880: 		    &metadata($uri,'keys',$toolsymb,$location,'_rights',
12881: 			      $depthcount+1);
12882: 		foreach my $rights (split(',',$rights_metadata)) {
12883: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
12884: 		    $metathesekeys{$rights}=1;
12885: 		}
12886: 	    }
12887: 	}
12888: 	# uniqifiy package listing
12889: 	my %seen;
12890: 	my @uniq_packages =
12891: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
12892: 	$metaentry{':packages'} = join(',',@uniq_packages);
12893: 
12894:         if (($importedresponses) || ($importedparts)) {
12895:             if ($importedparts) {
12896: # We had imported parts and need to rebuild partorder
12897:                 $metaentry{':partorder'}='';
12898:                 $metathesekeys{'partorder'}=1;
12899:             }
12900:             if ($importedresponses) {
12901: # We had imported responses and need to rebuil responseorder
12902:                 $metaentry{':responseorder'}='';
12903:                 $metathesekeys{'responseorder'}=1;
12904:             }
12905:             for (my $index=0;$index<$#origfiletagids;$index+=2) {
12906:                 my $origid = $origfiletagids[$index+1];
12907:                 if ($origfiletagids[$index] eq 'part') {
12908: # Original part, part of the problem
12909:                     if ($importedparts) {
12910:                         $metaentry{':partorder'}.=','.$origid;
12911:                     }
12912:                 } elsif ($origfiletagids[$index] eq 'import') {
12913:                     if ($importedparts) {
12914: # We have imported parts at this position
12915:                         if ($importedpartids{$origid} ne '') {
12916:                             $metaentry{':partorder'}.=','.$importedpartids{$origid};
12917:                         }
12918:                     }
12919:                     if ($importedresponses) {
12920: # We have imported responses at this position
12921:                         if ($importedrespids{$origid} ne '') {
12922:                             $metaentry{':responseorder'}.=','.$importedrespids{$origid};
12923:                         }
12924:                     }
12925:                 } else {
12926: # Original response item, part of the problem
12927:                     if ($importedresponses) {
12928:                         $metaentry{':responseorder'}.=','.$origid;
12929:                     }
12930:                 }
12931:             }
12932:             if ($importedparts) {
12933:                 $metaentry{':partorder'}=~s/^\,//;
12934:             }
12935:             if ($importedresponses) {
12936:                 $metaentry{':responseorder'}=~s/^\,//;
12937:             }
12938:         }
12939: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
12940: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
12941: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
12942:         unless ($liburi) {
12943: 	    &do_cache_new('meta',$uri,\%metaentry,$cachetime);
12944:         }
12945: # this is the end of "was not already recently cached
12946:     }
12947:     return $metaentry{':'.$what};
12948: }
12949: 
12950: sub metadata_create_package_def {
12951:     my ($uri,$key,$package,$metathesekeys)=@_;
12952:     my ($pack,$name,$subp)=split(/\&/,$key);
12953:     if ($subp eq 'default') { next; }
12954:     
12955:     if (defined($metaentry{':packages'})) {
12956: 	$metaentry{':packages'}.=','.$package;
12957:     } else {
12958: 	$metaentry{':packages'}=$package;
12959:     }
12960:     my $value=$packagetab{$key};
12961:     my $unikey;
12962:     $unikey='parameter_0_'.$name;
12963:     $metaentry{':'.$unikey.'.part'}=0;
12964:     $$metathesekeys{$unikey}=1;
12965:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
12966: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
12967:     }
12968:     if (defined($metaentry{':'.$unikey.'.default'})) {
12969: 	$metaentry{':'.$unikey}=
12970: 	    $metaentry{':'.$unikey.'.default'};
12971:     }
12972: }
12973: 
12974: sub metadata_generate_part0 {
12975:     my ($metadata,$metacache,$uri) = @_;
12976:     my %allnames;
12977:     foreach my $metakey (keys(%$metadata)) {
12978: 	if ($metakey=~/^parameter\_(.*)/) {
12979: 	  my $part=$$metacache{':'.$metakey.'.part'};
12980: 	  my $name=$$metacache{':'.$metakey.'.name'};
12981: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
12982: 	    $allnames{$name}=$part;
12983: 	  }
12984: 	}
12985:     }
12986:     foreach my $name (keys(%allnames)) {
12987:       $$metadata{"parameter_0_$name"}=1;
12988:       my $key=":parameter_0_$name";
12989:       $$metacache{"$key.part"}='0';
12990:       $$metacache{"$key.name"}=$name;
12991:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
12992: 					   $allnames{$name}.'_'.$name.
12993: 					   '.type'};
12994:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
12995: 			     '.display'};
12996:       my $expr='[Part: '.$allnames{$name}.']';
12997:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
12998:       $$metacache{"$key.display"}=$olddis;
12999:     }
13000: }
13001: 
13002: # ------------------------------------------------------ Devalidate title cache
13003: 
13004: sub devalidate_title_cache {
13005:     my ($url)=@_;
13006:     if (!$env{'request.course.id'}) { return; }
13007:     my $symb=&symbread($url);
13008:     if (!$symb) { return; }
13009:     my $key=$env{'request.course.id'}."\0".$symb;
13010:     &devalidate_cache_new('title',$key);
13011: }
13012: 
13013: # ------------------------------------------------- Get the title of a course
13014: 
13015: sub current_course_title {
13016:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
13017: }
13018: # ------------------------------------------------- Get the title of a resource
13019: 
13020: sub gettitle {
13021:     my $urlsymb=shift;
13022:     my $symb=&symbread($urlsymb);
13023:     if ($symb) {
13024: 	my $key=$env{'request.course.id'}."\0".$symb;
13025: 	my ($result,$cached)=&is_cached_new('title',$key);
13026: 	if (defined($cached)) { 
13027: 	    return $result;
13028: 	}
13029: 	my ($map,$resid,$url)=&decode_symb($symb);
13030: 	my $title='';
13031: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
13032: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
13033: 	} else {
13034: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13035: 		    &GDBM_READER(),0640)) {
13036: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
13037: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
13038: 		untie(%bighash);
13039: 	    }
13040: 	}
13041: 	$title=~s/\&colon\;/\:/gs;
13042: 	if ($title) {
13043: # Remember both $symb and $title for dynamic metadata
13044:             $accesshash{$symb.'___crstitle'}=$title;
13045:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
13046: # Cache this title and then return it
13047: 	    return &do_cache_new('title',$key,$title,600);
13048: 	}
13049: 	$urlsymb=$url;
13050:     }
13051:     my $title=&metadata($urlsymb,'title');
13052:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
13053:     return $title;
13054: }
13055: 
13056: sub get_slot {
13057:     my ($which,$cnum,$cdom)=@_;
13058:     if (!$cnum || !$cdom) {
13059: 	(undef,my $courseid)=&whichuser();
13060: 	$cdom=$env{'course.'.$courseid.'.domain'};
13061: 	$cnum=$env{'course.'.$courseid.'.num'};
13062:     }
13063:     my $key=join("\0",'slots',$cdom,$cnum,$which);
13064:     my %slotinfo;
13065:     if (exists($remembered{$key})) {
13066: 	$slotinfo{$which} = $remembered{$key};
13067:     } else {
13068: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
13069: 	&Apache::lonhomework::showhash(%slotinfo);
13070: 	my ($tmp)=keys(%slotinfo);
13071: 	if ($tmp=~/^error:/) { return (); }
13072: 	$remembered{$key} = $slotinfo{$which};
13073:     }
13074:     if (ref($slotinfo{$which}) eq 'HASH') {
13075: 	return %{$slotinfo{$which}};
13076:     }
13077:     return $slotinfo{$which};
13078: }
13079: 
13080: sub get_reservable_slots {
13081:     my ($cnum,$cdom,$uname,$udom) = @_;
13082:     my $now = time;
13083:     my $reservable_info;
13084:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
13085:     if (exists($remembered{$key})) {
13086:         $reservable_info = $remembered{$key};
13087:     } else {
13088:         my %resv;
13089:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
13090:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
13091:         $reservable_info = \%resv;
13092:         $remembered{$key} = $reservable_info;
13093:     }
13094:     return $reservable_info;
13095: }
13096: 
13097: sub get_course_slots {
13098:     my ($cnum,$cdom) = @_;
13099:     my $hashid=$cnum.':'.$cdom;
13100:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
13101:     if (defined($cached)) {
13102:         if (ref($result) eq 'HASH') {
13103:             return %{$result};
13104:         }
13105:     } else {
13106:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
13107:         my ($tmp) = keys(%slots);
13108:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
13109:             &do_cache_new('allslots',$hashid,\%slots,600);
13110:             return %slots;
13111:         }
13112:     }
13113:     return;
13114: }
13115: 
13116: sub devalidate_slots_cache {
13117:     my ($cnum,$cdom)=@_;
13118:     my $hashid=$cnum.':'.$cdom;
13119:     &devalidate_cache_new('allslots',$hashid);
13120: }
13121: 
13122: sub get_coursechange {
13123:     my ($cdom,$cnum) = @_;
13124:     if ($cdom eq '' || $cnum eq '') {
13125:         return unless ($env{'request.course.id'});
13126:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
13127:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
13128:     }
13129:     my $hashid=$cdom.'_'.$cnum;
13130:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
13131:     if ((defined($cached)) && ($change ne '')) {
13132:         return $change;
13133:     } else {
13134:         my %crshash;
13135:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
13136:         if ($crshash{'internal.contentchange'} eq '') {
13137:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
13138:             if ($change eq '') {
13139:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
13140:                 $change = $crshash{'internal.created'};
13141:             }
13142:         } else {
13143:             $change = $crshash{'internal.contentchange'};
13144:         }
13145:         my $cachetime = 600;
13146:         &do_cache_new('crschange',$hashid,$change,$cachetime);
13147:     }
13148:     return $change;
13149: }
13150: 
13151: sub devalidate_coursechange_cache {
13152:     my ($cnum,$cdom)=@_;
13153:     my $hashid=$cnum.':'.$cdom;
13154:     &devalidate_cache_new('crschange',$hashid);
13155: }
13156: 
13157: # ------------------------------------------------- Update symbolic store links
13158: 
13159: sub symblist {
13160:     my ($mapname,%newhash)=@_;
13161:     $mapname=&deversion(&declutter($mapname));
13162:     my %hash;
13163:     if (($env{'request.course.fn'}) && (%newhash)) {
13164:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13165:                       &GDBM_WRCREAT(),0640)) {
13166: 	    foreach my $url (keys(%newhash)) {
13167: 		next if ($url eq 'last_known'
13168: 			 && $env{'form.no_update_last_known'});
13169: 		$hash{declutter($url)}=&encode_symb($mapname,
13170: 						    $newhash{$url}->[1],
13171: 						    $newhash{$url}->[0]);
13172:             }
13173:             if (untie(%hash)) {
13174: 		return 'ok';
13175:             }
13176:         }
13177:     }
13178:     return 'error';
13179: }
13180: 
13181: # --------------------------------------------------------------- Verify a symb
13182: 
13183: sub symbverify {
13184:     my ($symb,$thisurl,$encstate)=@_;
13185:     my $thisfn=$thisurl;
13186:     $thisfn=&declutter($thisfn);
13187: # direct jump to resource in page or to a sequence - will construct own symbs
13188:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
13189: # check URL part
13190:     my ($map,$resid,$url)=&decode_symb($symb);
13191: 
13192:     unless ($url eq $thisfn) { return 0; }
13193: 
13194:     $symb=&symbclean($symb);
13195:     $thisurl=&deversion($thisurl);
13196:     $thisfn=&deversion($thisfn);
13197: 
13198:     my %bighash;
13199:     my $okay=0;
13200: 
13201:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13202:                             &GDBM_READER(),0640)) {
13203:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
13204:             $thisurl =~ s/\?.+$//;
13205:             if ($map =~ m{^uploaded/.+\.page$}) {
13206:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
13207:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
13208:             }
13209:         }
13210:         my $ids;
13211:         if ($map =~ m{^uploaded/.+\.page$}) {
13212:             $ids=$bighash{'ids_'.&clutter_with_no_wrapper($thisurl)};
13213:         } else {
13214:             $ids=$bighash{'ids_'.&clutter($thisurl)};
13215:         }
13216:         unless ($ids) {
13217:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
13218:             $ids=$bighash{$idkey};
13219:         }
13220:         if ($ids) {
13221: # ------------------------------------------------------------------- Has ID(s)
13222:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
13223:                 $symb =~ s/\?.+$//;
13224:             }
13225: 	    foreach my $id (split(/\,/,$ids)) {
13226: 	       my ($mapid,$resid)=split(/\./,$id);
13227:                if (
13228:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
13229:    eq $symb) {
13230:                    if (ref($encstate)) {
13231:                        $$encstate = $bighash{'encrypted_'.$id};
13232:                    }
13233: 		   if (($env{'request.role.adv'}) ||
13234: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
13235:                        ($thisurl eq '/adm/navmaps')) {
13236: 		       $okay=1;
13237:                        last;
13238: 		   }
13239: 	       }
13240: 	   }
13241:         }
13242: 	untie(%bighash);
13243:     }
13244:     return $okay;
13245: }
13246: 
13247: # --------------------------------------------------------------- Clean-up symb
13248: 
13249: sub symbclean {
13250:     my $symb=shift;
13251:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13252: # remove version from map
13253:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
13254: 
13255: # remove version from URL
13256:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
13257: 
13258: # remove wrapper
13259: 
13260:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
13261:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
13262:     return $symb;
13263: }
13264: 
13265: # ---------------------------------------------- Split symb to find map and url
13266: 
13267: sub encode_symb {
13268:     my ($map,$resid,$url)=@_;
13269:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
13270: }
13271: 
13272: sub decode_symb {
13273:     my $symb=shift;
13274:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
13275:     my ($map,$resid,$url)=split(/___/,$symb);
13276:     return (&fixversion($map),$resid,&fixversion($url));
13277: }
13278: 
13279: sub fixversion {
13280:     my $fn=shift;
13281:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
13282:     my %bighash;
13283:     my $uri=&clutter($fn);
13284:     my $key=$env{'request.course.id'}.'_'.$uri;
13285: # is this cached?
13286:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
13287:     if (defined($cached)) { return $result; }
13288: # unfortunately not cached, or expired
13289:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13290: 	    &GDBM_READER(),0640)) {
13291:  	if ($bighash{'version_'.$uri}) {
13292:  	    my $version=$bighash{'version_'.$uri};
13293:  	    unless (($version eq 'mostrecent') || 
13294: 		    ($version==&getversion($uri))) {
13295:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
13296:  	    }
13297:  	}
13298:  	untie %bighash;
13299:     }
13300:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
13301: }
13302: 
13303: sub deversion {
13304:     my $url=shift;
13305:     $url=~s/\.\d+\.(\w+)$/\.$1/;
13306:     return $url;
13307: }
13308: 
13309: # ------------------------------------------------------ Return symb list entry
13310: 
13311: sub symbread {
13312:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles,
13313:         $nocache)=@_;
13314:     my $cache_str='request.symbread.cached.'.$thisfn;
13315:     if (defined($env{$cache_str}) && !$nocache) {
13316:         unless (ref($possibles) eq 'HASH') {
13317:             if ($ignorecachednull) {
13318:                 return $env{$cache_str} unless ($env{$cache_str} eq '');
13319:             } else {
13320:                 return $env{$cache_str};
13321:             }
13322:         }
13323:     }
13324: # no filename provided? try from environment
13325:     unless ($thisfn) {
13326:         if ($env{'request.symb'}) {
13327:             if ($nocache) {
13328:                 return &symbclean($env{'request.symb'});
13329:             } else {
13330:                 return $env{$cache_str}=&symbclean($env{'request.symb'});
13331:             }
13332: 	}
13333: 	$thisfn=$env{'request.filename'};
13334:     }
13335:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
13336: # is that filename actually a symb? Verify, clean, and return
13337:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
13338: 	if (&symbverify($thisfn,$1)) {
13339:             if ($nocache) {
13340:                 return &symbclean($thisfn);
13341:             } else {
13342: 	        return $env{$cache_str}=&symbclean($thisfn);
13343:             }
13344: 	}
13345:     }
13346:     $thisfn=declutter($thisfn);
13347:     my %hash;
13348:     my %bighash;
13349:     my $syval='';
13350:     if (($env{'request.course.fn'}) && ($thisfn)) {
13351:         my $targetfn = $thisfn;
13352:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
13353:             $targetfn = 'adm/wrapper/'.$thisfn;
13354:         }
13355: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
13356: 	    $targetfn=$1;
13357: 	}
13358:         unless ($nocache) {
13359:             if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
13360:                           &GDBM_READER(),0640)) {
13361: 	        $syval=$hash{$targetfn};
13362:                 untie(%hash);
13363:             }
13364:             if ($syval) {
13365:                 my @blockers = &has_comm_blocking('bre',$syval,$thisfn,$nocache);
13366:                 if (@blockers) {
13367:                     $syval='';
13368:                 }
13369:             }
13370:         }
13371: # ---------------------------------------------------------- There was an entry
13372:         if ($syval) {
13373: 	    #unless ($syval=~/\_\d+$/) {
13374: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
13375: 		    #&appenv({'request.ambiguous' => $thisfn});
13376: 		    #return $env{$cache_str}='';
13377: 		#}    
13378: 		#$syval.=$1;
13379: 	    #}
13380:         } else {
13381: # ------------------------------------------------------- Was not in symb table
13382:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
13383:                             &GDBM_READER(),0640)) {
13384: # ---------------------------------------------- Get ID(s) for current resource
13385:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
13386:               unless ($ids) { 
13387:                  $ids=$bighash{'ids_/'.$thisfn};
13388:               }
13389:               unless ($ids) {
13390: # alias?
13391: 		  $ids=$bighash{'mapalias_'.$thisfn};
13392:               }
13393:               if ($ids) {
13394: # ------------------------------------------------------------------- Has ID(s)
13395:                  my @possibilities=split(/\,/,$ids);
13396:                  if ($#possibilities==0) {
13397: # ----------------------------------------------- There is only one possibility
13398: 		     my ($mapid,$resid)=split(/\./,$ids);
13399: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
13400: 						    $resid,$thisfn);
13401:                      if (ref($possibles) eq 'HASH') {
13402:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13403:                              $possibles->{$syval} = 1;
13404:                          }
13405:                      }
13406:                      if ($checkforblock) {
13407:                          unless ($bighash{'randomout_'.$ids} || $env{'request.role.adv'}) {
13408:                              my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
13409:                              if (@blockers) {
13410:                                  $syval = '';
13411:                                  return;
13412:                              }
13413:                          }
13414:                      }
13415:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
13416: # ------------------------------------------ There is more than one possibility
13417:                      my $realpossible=0;
13418:                      foreach my $id (@possibilities) {
13419: 			 my $file=$bighash{'src_'.$id};
13420:                          my $canaccess;
13421:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
13422:                              $canaccess = 1;
13423:                          } else { 
13424:                              $canaccess = &allowed('bre',$file);
13425:                          }
13426:                          if ($canaccess) {
13427:          		     my ($mapid,$resid)=split(/\./,$id);
13428:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
13429:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
13430: 						             $resid,$thisfn);
13431:                                  next if ($bighash{'randomout_'.$id} && !$env{'request.role.adv'});
13432:                                  next unless ($bighash{'encrypted_'.$id} eq $env{'request.enc'});
13433:                                  if ($checkforblock) {
13434:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
13435:                                      if (@blockers > 0) {
13436:                                          $syval = '';
13437:                                      } else {
13438:                                          $syval = $poss_syval;
13439:                                          $realpossible++;
13440:                                      }
13441:                                  } else {
13442:                                      $syval = $poss_syval;
13443:                                      $realpossible++;
13444:                                  }
13445:                                  if ($syval) {
13446:                                      if (ref($possibles) eq 'HASH') {
13447:                                          $possibles->{$syval} = 1;
13448:                                      }
13449:                                  }
13450:                              }
13451: 			 }
13452:                      }
13453: 		     if ($realpossible!=1) { $syval=''; }
13454:                  } else {
13455:                      $syval='';
13456:                  }
13457: 	      }
13458:               untie(%bighash);
13459:            }
13460:         }
13461:         if ($syval) {
13462:             if ($nocache) {
13463:                 return $syval;
13464:             } else {
13465: 	        return $env{$cache_str}=$syval;
13466:             }
13467:         }
13468:     }
13469:     &appenv({'request.ambiguous' => $thisfn});
13470:     return '' if ($nocache);
13471:     return $env{$cache_str}='';
13472: }
13473: 
13474: # ---------------------------------------------------------- Return random seed
13475: 
13476: sub numval {
13477:     my $txt=shift;
13478:     $txt=~tr/A-J/0-9/;
13479:     $txt=~tr/a-j/0-9/;
13480:     $txt=~tr/K-T/0-9/;
13481:     $txt=~tr/k-t/0-9/;
13482:     $txt=~tr/U-Z/0-5/;
13483:     $txt=~tr/u-z/0-5/;
13484:     $txt=~s/\D//g;
13485:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
13486:     return int($txt);
13487: }
13488: 
13489: sub numval2 {
13490:     my $txt=shift;
13491:     $txt=~tr/A-J/0-9/;
13492:     $txt=~tr/a-j/0-9/;
13493:     $txt=~tr/K-T/0-9/;
13494:     $txt=~tr/k-t/0-9/;
13495:     $txt=~tr/U-Z/0-5/;
13496:     $txt=~tr/u-z/0-5/;
13497:     $txt=~s/\D//g;
13498:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13499:     my $total;
13500:     foreach my $val (@txts) { $total+=$val; }
13501:     if ($_64bit) { if ($total > 2**32) { return -1; } }
13502:     return int($total);
13503: }
13504: 
13505: sub numval3 {
13506:     use integer;
13507:     my $txt=shift;
13508:     $txt=~tr/A-J/0-9/;
13509:     $txt=~tr/a-j/0-9/;
13510:     $txt=~tr/K-T/0-9/;
13511:     $txt=~tr/k-t/0-9/;
13512:     $txt=~tr/U-Z/0-5/;
13513:     $txt=~tr/u-z/0-5/;
13514:     $txt=~s/\D//g;
13515:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
13516:     my $total;
13517:     foreach my $val (@txts) { $total+=$val; }
13518:     if ($_64bit) { $total=(($total<<32)>>32); }
13519:     return $total;
13520: }
13521: 
13522: sub digest {
13523:     my ($data)=@_;
13524:     my $digest=&Digest::MD5::md5($data);
13525:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
13526:     my ($e,$f);
13527:     {
13528:         use integer;
13529:         $e=($a+$b);
13530:         $f=($c+$d);
13531:         if ($_64bit) {
13532:             $e=(($e<<32)>>32);
13533:             $f=(($f<<32)>>32);
13534:         }
13535:     }
13536:     if (wantarray) {
13537: 	return ($e,$f);
13538:     } else {
13539: 	my $g;
13540: 	{
13541: 	    use integer;
13542: 	    $g=($e+$f);
13543: 	    if ($_64bit) {
13544: 		$g=(($g<<32)>>32);
13545: 	    }
13546: 	}
13547: 	return $g;
13548:     }
13549: }
13550: 
13551: sub latest_rnd_algorithm_id {
13552:     return '64bit5';
13553: }
13554: 
13555: sub get_rand_alg {
13556:     my ($courseid)=@_;
13557:     if (!$courseid) { $courseid=(&whichuser())[1]; }
13558:     if ($courseid) {
13559: 	return $env{"course.$courseid.rndseed"};
13560:     }
13561:     return &latest_rnd_algorithm_id();
13562: }
13563: 
13564: sub validCODE {
13565:     my ($CODE)=@_;
13566:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
13567:     return 0;
13568: }
13569: 
13570: sub getCODE {
13571:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
13572:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
13573: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
13574: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
13575: 	return $Apache::lonhomework::history{'resource.CODE'};
13576:     }
13577:     return undef;
13578: }
13579: #
13580: #  Determines the random seed for a specific context:
13581: #
13582: # parameters:
13583: #   symb      - in course context the symb for the seed.
13584: #   course_id - The course id of the form domain_coursenum.
13585: #   domain    - Domain for the user.
13586: #   course    - Course for the user.
13587: #   cenv      - environment of the course.
13588: #
13589: # NOTE:
13590: #   All parameters are picked out of the environment if missing
13591: #   or not defined.
13592: #   If a symb cannot be determined the current time is used instead.
13593: #
13594: #  For a given well defined symb, courside, domain, username,
13595: #  and course environment, the seed is reproducible.
13596: #
13597: sub rndseed {
13598:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
13599:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
13600:     if (!defined($symb)) {
13601: 	unless ($symb=$wsymb) { return time; }
13602:     }
13603:     if (!defined $courseid) { 
13604: 	$courseid=$wcourseid; 
13605:     }
13606:     if (!defined $domain) { $domain=$wdomain; }
13607:     if (!defined $username) { $username=$wusername }
13608: 
13609:     my $which;
13610:     if (defined($cenv->{'rndseed'})) {
13611: 	$which = $cenv->{'rndseed'};
13612:     } else {
13613: 	$which =&get_rand_alg($courseid);
13614:     }
13615:     if (defined(&getCODE())) {
13616: 
13617: 	if ($which eq '64bit5') {
13618: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
13619: 	} elsif ($which eq '64bit4') {
13620: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
13621: 	} else {
13622: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
13623: 	}
13624:     } elsif ($which eq '64bit5') {
13625: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
13626:     } elsif ($which eq '64bit4') {
13627: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
13628:     } elsif ($which eq '64bit3') {
13629: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
13630:     } elsif ($which eq '64bit2') {
13631: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
13632:     } elsif ($which eq '64bit') {
13633: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
13634:     }
13635:     return &rndseed_32bit($symb,$courseid,$domain,$username);
13636: }
13637: 
13638: sub rndseed_32bit {
13639:     my ($symb,$courseid,$domain,$username)=@_;
13640:     {
13641: 	use integer;
13642: 	my $symbchck=unpack("%32C*",$symb) << 27;
13643: 	my $symbseed=numval($symb) << 22;
13644: 	my $namechck=unpack("%32C*",$username) << 17;
13645: 	my $nameseed=numval($username) << 12;
13646: 	my $domainseed=unpack("%32C*",$domain) << 7;
13647: 	my $courseseed=unpack("%32C*",$courseid);
13648: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
13649: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13650: 	#&logthis("rndseed :$num:$symb");
13651: 	if ($_64bit) { $num=(($num<<32)>>32); }
13652: 	return $num;
13653:     }
13654: }
13655: 
13656: sub rndseed_64bit {
13657:     my ($symb,$courseid,$domain,$username)=@_;
13658:     {
13659: 	use integer;
13660: 	my $symbchck=unpack("%32S*",$symb) << 21;
13661: 	my $symbseed=numval($symb) << 10;
13662: 	my $namechck=unpack("%32S*",$username);
13663: 	
13664: 	my $nameseed=numval($username) << 21;
13665: 	my $domainseed=unpack("%32S*",$domain) << 10;
13666: 	my $courseseed=unpack("%32S*",$courseid);
13667: 	
13668: 	my $num1=$symbchck+$symbseed+$namechck;
13669: 	my $num2=$nameseed+$domainseed+$courseseed;
13670: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13671: 	#&logthis("rndseed :$num:$symb");
13672: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13673: 	return "$num1,$num2";
13674:     }
13675: }
13676: 
13677: sub rndseed_64bit2 {
13678:     my ($symb,$courseid,$domain,$username)=@_;
13679:     {
13680: 	use integer;
13681: 	# strings need to be an even # of cahracters long, it it is odd the
13682:         # last characters gets thrown away
13683: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13684: 	my $symbseed=numval($symb) << 10;
13685: 	my $namechck=unpack("%32S*",$username.' ');
13686: 	
13687: 	my $nameseed=numval($username) << 21;
13688: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13689: 	my $courseseed=unpack("%32S*",$courseid.' ');
13690: 	
13691: 	my $num1=$symbchck+$symbseed+$namechck;
13692: 	my $num2=$nameseed+$domainseed+$courseseed;
13693: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13694: 	#&logthis("rndseed :$num:$symb");
13695: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13696: 	return "$num1,$num2";
13697:     }
13698: }
13699: 
13700: sub rndseed_64bit3 {
13701:     my ($symb,$courseid,$domain,$username)=@_;
13702:     {
13703: 	use integer;
13704: 	# strings need to be an even # of cahracters long, it it is odd the
13705:         # last characters gets thrown away
13706: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13707: 	my $symbseed=numval2($symb) << 10;
13708: 	my $namechck=unpack("%32S*",$username.' ');
13709: 	
13710: 	my $nameseed=numval2($username) << 21;
13711: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13712: 	my $courseseed=unpack("%32S*",$courseid.' ');
13713: 	
13714: 	my $num1=$symbchck+$symbseed+$namechck;
13715: 	my $num2=$nameseed+$domainseed+$courseseed;
13716: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13717: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13718: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13719: 	
13720: 	return "$num1:$num2";
13721:     }
13722: }
13723: 
13724: sub rndseed_64bit4 {
13725:     my ($symb,$courseid,$domain,$username)=@_;
13726:     {
13727: 	use integer;
13728: 	# strings need to be an even # of cahracters long, it it is odd the
13729:         # last characters gets thrown away
13730: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
13731: 	my $symbseed=numval3($symb) << 10;
13732: 	my $namechck=unpack("%32S*",$username.' ');
13733: 	
13734: 	my $nameseed=numval3($username) << 21;
13735: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
13736: 	my $courseseed=unpack("%32S*",$courseid.' ');
13737: 	
13738: 	my $num1=$symbchck+$symbseed+$namechck;
13739: 	my $num2=$nameseed+$domainseed+$courseseed;
13740: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
13741: 	#&logthis("rndseed :$num1:$num2:$_64bit");
13742: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
13743: 	
13744: 	return "$num1:$num2";
13745:     }
13746: }
13747: 
13748: sub rndseed_64bit5 {
13749:     my ($symb,$courseid,$domain,$username)=@_;
13750:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
13751:     return "$num1:$num2";
13752: }
13753: 
13754: sub rndseed_CODE_64bit {
13755:     my ($symb,$courseid,$domain,$username)=@_;
13756:     {
13757: 	use integer;
13758: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13759: 	my $symbseed=numval2($symb);
13760: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13761: 	my $CODEseed=numval(&getCODE());
13762: 	my $courseseed=unpack("%32S*",$courseid.' ');
13763: 	my $num1=$symbseed+$CODEchck;
13764: 	my $num2=$CODEseed+$courseseed+$symbchck;
13765: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13766: 	#&logthis("rndseed :$num1:$num2:$symb");
13767: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13768: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13769: 	return "$num1:$num2";
13770:     }
13771: }
13772: 
13773: sub rndseed_CODE_64bit4 {
13774:     my ($symb,$courseid,$domain,$username)=@_;
13775:     {
13776: 	use integer;
13777: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
13778: 	my $symbseed=numval3($symb);
13779: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
13780: 	my $CODEseed=numval3(&getCODE());
13781: 	my $courseseed=unpack("%32S*",$courseid.' ');
13782: 	my $num1=$symbseed+$CODEchck;
13783: 	my $num2=$CODEseed+$courseseed+$symbchck;
13784: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
13785: 	#&logthis("rndseed :$num1:$num2:$symb");
13786: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
13787: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
13788: 	return "$num1:$num2";
13789:     }
13790: }
13791: 
13792: sub rndseed_CODE_64bit5 {
13793:     my ($symb,$courseid,$domain,$username)=@_;
13794:     my $code = &getCODE();
13795:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
13796:     return "$num1:$num2";
13797: }
13798: 
13799: sub setup_random_from_rndseed {
13800:     my ($rndseed)=@_;
13801:     if ($rndseed =~/([,:])/) {
13802:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
13803:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
13804:             &Math::Random::random_set_seed_from_phrase($rndseed);
13805:         } else {
13806:             &Math::Random::random_set_seed($num1,$num2);
13807:         }
13808:     } else {
13809: 	&Math::Random::random_set_seed_from_phrase($rndseed);
13810:     }
13811: }
13812: 
13813: sub latest_receipt_algorithm_id {
13814:     return 'receipt3';
13815: }
13816: 
13817: sub recunique {
13818:     my $fucourseid=shift;
13819:     my $unique;
13820:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
13821: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13822: 	$unique=$env{"course.$fucourseid.internal.encseed"};
13823:     } else {
13824: 	$unique=$perlvar{'lonReceipt'};
13825:     }
13826:     return unpack("%32C*",$unique);
13827: }
13828: 
13829: sub recprefix {
13830:     my $fucourseid=shift;
13831:     my $prefix;
13832:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
13833: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
13834: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
13835:     } else {
13836: 	$prefix=$perlvar{'lonHostID'};
13837:     }
13838:     return unpack("%32C*",$prefix);
13839: }
13840: 
13841: sub ireceipt {
13842:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
13843: 
13844:     my $return =&recprefix($fucourseid).'-';
13845: 
13846:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
13847: 	$env{'request.state'} eq 'construct') {
13848: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
13849: 	return $return;
13850:     }
13851: 
13852:     my $cuname=unpack("%32C*",$funame);
13853:     my $cudom=unpack("%32C*",$fudom);
13854:     my $cucourseid=unpack("%32C*",$fucourseid);
13855:     my $cusymb=unpack("%32C*",$fusymb);
13856:     my $cunique=&recunique($fucourseid);
13857:     my $cpart=unpack("%32S*",$part);
13858:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
13859: 
13860: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
13861: 			       
13862: 	$return.= ($cunique%$cuname+
13863: 		   $cunique%$cudom+
13864: 		   $cusymb%$cuname+
13865: 		   $cusymb%$cudom+
13866: 		   $cucourseid%$cuname+
13867: 		   $cucourseid%$cudom+
13868: 		   $cpart%$cuname+
13869: 		   $cpart%$cudom);
13870:     } else {
13871: 	$return.= ($cunique%$cuname+
13872: 		   $cunique%$cudom+
13873: 		   $cusymb%$cuname+
13874: 		   $cusymb%$cudom+
13875: 		   $cucourseid%$cuname+
13876: 		   $cucourseid%$cudom);
13877:     }
13878:     return $return;
13879: }
13880: 
13881: sub receipt {
13882:     my ($part)=@_;
13883:     my ($symb,$courseid,$domain,$name) = &whichuser();
13884:     return &ireceipt($name,$domain,$courseid,$symb,$part);
13885: }
13886: 
13887: sub whichuser {
13888:     my ($passedsymb)=@_;
13889:     my ($symb,$courseid,$domain,$name,$publicuser);
13890:     if (defined($env{'form.grade_symb'})) {
13891: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
13892: 	my $allowed=&allowed('vgr',$tmp_courseid);
13893: 	if (!$allowed &&
13894: 	    exists($env{'request.course.sec'}) &&
13895: 	    $env{'request.course.sec'} !~ /^\s*$/) {
13896: 	    $allowed=&allowed('vgr',$tmp_courseid.
13897: 			      '/'.$env{'request.course.sec'});
13898: 	}
13899: 	if ($allowed) {
13900: 	    ($symb)=&get_env_multiple('form.grade_symb');
13901: 	    $courseid=$tmp_courseid;
13902: 	    ($domain)=&get_env_multiple('form.grade_domain');
13903: 	    ($name)=&get_env_multiple('form.grade_username');
13904: 	    return ($symb,$courseid,$domain,$name,$publicuser);
13905: 	}
13906:     }
13907:     if (!$passedsymb) {
13908: 	$symb=&symbread();
13909:     } else {
13910: 	$symb=$passedsymb;
13911:     }
13912:     $courseid=$env{'request.course.id'};
13913:     $domain=$env{'user.domain'};
13914:     $name=$env{'user.name'};
13915:     if ($name eq 'public' && $domain eq 'public') {
13916: 	if (!defined($env{'form.username'})) {
13917: 	    $env{'form.username'}.=time.rand(10000000);
13918: 	}
13919: 	$name.=$env{'form.username'};
13920:     }
13921:     return ($symb,$courseid,$domain,$name,$publicuser);
13922: 
13923: }
13924: 
13925: # ------------------------------------------------------------ Serves up a file
13926: # returns either the contents of the file or 
13927: # -1 if the file doesn't exist
13928: #
13929: # if the target is a file that was uploaded via DOCS, 
13930: # a check will be made to see if a current copy exists on the local server,
13931: # if it does this will be served, otherwise a copy will be retrieved from
13932: # the home server for the course and stored in /home/httpd/html/userfiles on
13933: # the local server.   
13934: 
13935: sub getfile {
13936:     my ($file) = @_;
13937:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
13938:     &repcopy($file);
13939:     return &readfile($file);
13940: }
13941: 
13942: sub repcopy_userfile {
13943:     my ($file)=@_;
13944:     my $londocroot = $perlvar{'lonDocRoot'};
13945:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
13946:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
13947:     my ($cdom,$cnum,$filename) = 
13948: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
13949:     my $uri="/uploaded/$cdom/$cnum/$filename";
13950:     if (-e "$file") {
13951: # we already have a local copy, check it out
13952: 	my @fileinfo = stat($file);
13953: 	my $rtncode;
13954: 	my $info;
13955: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
13956: 	if ($lwpresp ne 'ok') {
13957: # there is no such file anymore, even though we had a local copy
13958: 	    if ($rtncode eq '404') {
13959: 		unlink($file);
13960: 	    }
13961: 	    return -1;
13962: 	}
13963: 	if ($info < $fileinfo[9]) {
13964: # nice, the file we have is up-to-date, just say okay
13965: 	    return 'ok';
13966: 	} else {
13967: # the file is outdated, get rid of it
13968: 	    unlink($file);
13969: 	}
13970:     }
13971: # one way or the other, at this point, we don't have the file
13972: # construct the correct path for the file
13973:     my @parts = ($cdom,$cnum); 
13974:     if ($filename =~ m|^(.+)/[^/]+$|) {
13975: 	push @parts, split(/\//,$1);
13976:     }
13977:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
13978:     foreach my $part (@parts) {
13979: 	$path .= '/'.$part;
13980: 	if (!-e $path) {
13981: 	    mkdir($path,0770);
13982: 	}
13983:     }
13984: # now the path exists for sure
13985: # get a user agent
13986:     my $transferfile=$file.'.in.transfer';
13987: # FIXME: this should flock
13988:     if (-e $transferfile) { return 'ok'; }
13989:     my $request;
13990:     $uri=~s/^\///;
13991:     my $homeserver = &homeserver($cnum,$cdom);
13992:     my $hostname = &hostname($homeserver);
13993:     my $protocol = $protocol{$homeserver};
13994:     $protocol = 'http' if ($protocol ne 'https');
13995:     $request=new HTTP::Request('GET',$protocol.'://'.$hostname.'/raw/'.$uri);
13996:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
13997: # did it work?
13998:     if ($response->is_error()) {
13999: 	unlink($transferfile);
14000: 	&logthis("Userfile repcopy failed for $uri");
14001: 	return -1;
14002:     }
14003: # worked, rename the transfer file
14004:     rename($transferfile,$file);
14005:     return 'ok';
14006: }
14007: 
14008: sub tokenwrapper {
14009:     my $uri=shift;
14010:     $uri=~s|^https?\://([^/]+)||;
14011:     $uri=~s|^/||;
14012:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
14013:     my $token=$1;
14014:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
14015:     if ($udom && $uname && $file) {
14016: 	$file=~s|(\?\.*)*$||;
14017:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
14018:         my $homeserver = &homeserver($uname,$udom);
14019:         my $hostname = &hostname($homeserver);
14020:         my $protocol = $protocol{$homeserver};
14021:         $protocol = 'http' if ($protocol ne 'https');
14022:         return $protocol.'://'.$hostname.'/'.$uri.
14023:                (($uri=~/\?/)?'&':'?').'token='.$token.
14024:                                '&tokenissued='.$perlvar{'lonHostID'};
14025:     } else {
14026:         return '/adm/notfound.html';
14027:     }
14028: }
14029: 
14030: # call with reqtype HEAD: get last modification time
14031: # call with reqtype GET: get the file contents
14032: # Do not call this with reqtype GET for large files! It loads everything into memory
14033: #
14034: sub getuploaded {
14035:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
14036:     $uri=~s/^\///;
14037:     my $homeserver = &homeserver($cnum,$cdom);
14038:     my $hostname = &hostname($homeserver);
14039:     my $protocol = $protocol{$homeserver};
14040:     $protocol = 'http' if ($protocol ne 'https');
14041:     $uri = $protocol.'://'.$hostname.'/raw/'.$uri;
14042:     my $request=new HTTP::Request($reqtype,$uri);
14043:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
14044:     $$rtncode = $response->code;
14045:     if (! $response->is_success()) {
14046: 	return 'failed';
14047:     }      
14048:     if ($reqtype eq 'HEAD') {
14049: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
14050:     } elsif ($reqtype eq 'GET') {
14051: 	$$info = $response->content;
14052:     }
14053:     return 'ok';
14054: }
14055: 
14056: sub readfile {
14057:     my $file = shift;
14058:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
14059:     my $fh;
14060:     open($fh,"<",$file);
14061:     my $a='';
14062:     while (my $line = <$fh>) { $a .= $line; }
14063:     return $a;
14064: }
14065: 
14066: sub filelocation {
14067:     my ($dir,$file) = @_;
14068:     my $location;
14069:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
14070: 
14071:     if ($file =~ m-^/adm/-) {
14072: 	$file=~s-^/adm/wrapper/-/-;
14073: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14074:     }
14075: 
14076:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
14077:         $location = $file;
14078:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
14079:         my ($udom,$uname,$filename)=
14080:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
14081:         my $home=&homeserver($uname,$udom);
14082:         my $is_me=0;
14083:         my @ids=&current_machine_ids();
14084:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
14085:         if ($is_me) {
14086:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
14087:         } else {
14088:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
14089:   	      $udom.'/'.$uname.'/'.$filename;
14090:         }
14091:     } elsif ($file =~ m-^/adm/-) {
14092: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
14093:     } else {
14094:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
14095:         $file=~s:^/(res|priv)/:/:;
14096:         my $space=$1;
14097:         if ( !( $file =~ m:^/:) ) {
14098:             $location = $dir. '/'.$file;
14099:         } else {
14100:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
14101:         }
14102:     }
14103:     $location=~s://+:/:g; # remove duplicate /
14104:     while ($location=~m{/\.\./}) {
14105: 	if ($location =~ m{/[^/]+/\.\./}) {
14106: 	    $location=~ s{/[^/]+/\.\./}{/}g;
14107: 	} else {
14108: 	    $location=~ s{/\.\./}{/}g;
14109: 	}
14110:     } #remove dir/..
14111:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
14112:     return $location;
14113: }
14114: 
14115: sub hreflocation {
14116:     my ($dir,$file)=@_;
14117:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
14118: 	$file=filelocation($dir,$file);
14119:     } elsif ($file=~m-^/adm/-) {
14120: 	$file=~s-^/adm/wrapper/-/-;
14121: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
14122:     }
14123:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
14124: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
14125:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
14126: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
14127: 	        {/uploaded/$1/$2/}x;
14128:     }
14129:     if ($file=~ m{^/userfiles/}) {
14130: 	$file =~ s{^/userfiles/}{/uploaded/};
14131:     }
14132:     return $file;
14133: }
14134: 
14135: 
14136: 
14137: 
14138: 
14139: sub current_machine_domains {
14140:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
14141: }
14142: 
14143: sub machine_domains {
14144:     my ($hostname) = @_;
14145:     my @domains;
14146:     my %hostname = &all_hostnames();
14147:     while( my($id, $name) = each(%hostname)) {
14148: #	&logthis("-$id-$name-$hostname-");
14149: 	if ($hostname eq $name) {
14150: 	    push(@domains,&host_domain($id));
14151: 	}
14152:     }
14153:     return @domains;
14154: }
14155: 
14156: sub current_machine_ids {
14157:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
14158: }
14159: 
14160: sub machine_ids {
14161:     my ($hostname) = @_;
14162:     $hostname ||= &hostname($perlvar{'lonHostID'});
14163:     my @ids;
14164:     my %name_to_host = &all_names();
14165:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
14166: 	return @{ $name_to_host{$hostname} };
14167:     }
14168:     return;
14169: }
14170: 
14171: sub additional_machine_domains {
14172:     my @domains;
14173:     open(my $fh,"<","$perlvar{'lonTabDir'}/expected_domains.tab");
14174:     while( my $line = <$fh>) {
14175:         $line =~ s/\s//g;
14176:         push(@domains,$line);
14177:     }
14178:     return @domains;
14179: }
14180: 
14181: sub default_login_domain {
14182:     my $domain = $perlvar{'lonDefDomain'};
14183:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
14184:     foreach my $posdom (&current_machine_domains(),
14185:                         &additional_machine_domains()) {
14186:         if (lc($posdom) eq lc($testdomain)) {
14187:             $domain=$posdom;
14188:             last;
14189:         }
14190:     }
14191:     return $domain;
14192: }
14193: 
14194: sub shared_institution {
14195:     my ($dom) = @_;
14196:     my $same_intdom;
14197:     my $hostintdom = &internet_dom($perlvar{'lonHostID'});
14198:     if ($hostintdom ne '') {
14199:         my %iphost = &get_iphost();
14200:         my $primary_id = &domain($dom,'primary');
14201:         my $primary_ip = &get_host_ip($primary_id);
14202:         if (ref($iphost{$primary_ip}) eq 'ARRAY') {
14203:             foreach my $id (@{$iphost{$primary_ip}}) {
14204:                 my $intdom = &internet_dom($id);
14205:                 if ($intdom eq $hostintdom) {
14206:                     $same_intdom = 1;
14207:                     last;
14208:                 }
14209:             }
14210:         }
14211:     }
14212:     return $same_intdom;
14213: }
14214: 
14215: sub uses_sts {
14216:     my ($ignore_cache) = @_;
14217:     my $lonhost = $perlvar{'lonHostID'};
14218:     my $hostname = &hostname($lonhost);
14219:     my $sts_on;
14220:     if ($protocol{$lonhost} eq 'https') {
14221:         my $cachetime = 12*3600;
14222:         if (!$ignore_cache) {
14223:             ($sts_on,my $cached)=&is_cached_new('stspolicy',$lonhost);
14224:             if (defined($cached)) {
14225:                 return $sts_on;
14226:             }
14227:         }
14228:         my $url = $protocol{$lonhost}.'://'.$hostname.'/index.html';
14229:         my $request=new HTTP::Request('HEAD',$url);
14230:         my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,'','','',1);
14231:         if ($response->is_success) {
14232:             my $has_sts = $response->header('Strict-Transport-Security');
14233:             if ($has_sts eq '') {
14234:                 $sts_on = 0;
14235:             } else {
14236:                 if ($has_sts =~ /\Qmax-age=\E(\d+)/) {
14237:                     my $maxage = $1;
14238:                     if ($maxage) {
14239:                         $sts_on = 1;
14240:                     } else {
14241:                         $sts_on = 0;
14242:                     }
14243:                 } else {
14244:                     $sts_on = 0;
14245:                 }
14246:             }
14247:             return &do_cache_new('stspolicy',$lonhost,$sts_on,$cachetime);
14248:         }
14249:     }
14250:     return;
14251: }
14252: 
14253: # ------------------------------------------------------------- Declutters URLs
14254: 
14255: sub declutter {
14256:     my $thisfn=shift;
14257:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
14258:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
14259:         $thisfn=~s{^/home/httpd/html}{};
14260:     }
14261:     $thisfn=~s/^\///;
14262:     $thisfn=~s|^adm/wrapper/||;
14263:     $thisfn=~s|^adm/coursedocs/showdoc/||;
14264:     $thisfn=~s/^res\///;
14265:     $thisfn=~s/^priv\///;
14266:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
14267:         $thisfn=~s/\?.+$//;
14268:     }
14269:     return $thisfn;
14270: }
14271: 
14272: # ------------------------------------------------------------- Clutter up URLs
14273: 
14274: sub clutter {
14275:     my $thisfn='/'.&declutter(shift);
14276:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
14277: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
14278:        $thisfn='/res'.$thisfn; 
14279:     }
14280:     if ($thisfn !~m|^/adm|) {
14281: 	if ($thisfn =~ m|^/ext/|) {
14282: 	    $thisfn='/adm/wrapper'.$thisfn;
14283: 	} else {
14284: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
14285: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
14286: 	    if ($embstyle eq 'ssi'
14287: 		|| ($embstyle eq 'hdn')
14288: 		|| ($embstyle eq 'rat')
14289: 		|| ($embstyle eq 'prv')
14290: 		|| ($embstyle eq 'ign')) {
14291: 		#do nothing with these
14292: 	    } elsif (($embstyle eq 'img') 
14293: 		|| ($embstyle eq 'emb')
14294: 		|| ($embstyle eq 'wrp')) {
14295: 		$thisfn='/adm/wrapper'.$thisfn;
14296: 	    } elsif ($embstyle eq 'unk'
14297: 		     && $thisfn!~/\.(sequence|page)$/) {
14298: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
14299: 	    } else {
14300: #		&logthis("Got a blank emb style");
14301: 	    }
14302: 	}
14303:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
14304:         $thisfn='/adm/wrapper'.$thisfn;
14305:     }
14306:     return $thisfn;
14307: }
14308: 
14309: sub clutter_with_no_wrapper {
14310:     my $uri = &clutter(shift);
14311:     if ($uri =~ m-^/adm/-) {
14312: 	$uri =~ s-^/adm/wrapper/-/-;
14313: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
14314:     }
14315:     return $uri;
14316: }
14317: 
14318: sub freeze_escape {
14319:     my ($value)=@_;
14320:     if (ref($value)) {
14321: 	$value=&nfreeze($value);
14322: 	return '__FROZEN__'.&escape($value);
14323:     }
14324:     return &escape($value);
14325: }
14326: 
14327: 
14328: sub thaw_unescape {
14329:     my ($value)=@_;
14330:     if ($value =~ /^__FROZEN__/) {
14331: 	substr($value,0,10,undef);
14332: 	$value=&unescape($value);
14333: 	return &thaw($value);
14334:     }
14335:     return &unescape($value);
14336: }
14337: 
14338: sub correct_line_ends {
14339:     my ($result)=@_;
14340:     $$result =~s/\r\n/\n/mg;
14341:     $$result =~s/\r/\n/mg;
14342: }
14343: # ================================================================ Main Program
14344: 
14345: sub goodbye {
14346:    &logthis("Starting Shut down");
14347: #not converted to using infrastruture and probably shouldn't be
14348:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
14349: #converted
14350: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
14351:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
14352: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
14353: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
14354: #1.1 only
14355: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
14356: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
14357: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
14358: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
14359:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
14360:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
14361:    &logthis(sprintf("%-20s is %s",'hits',$hits));
14362:    &flushcourselogs();
14363:    &logthis("Shutting down");
14364: }
14365: 
14366: sub get_dns {
14367:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
14368:     if (!$ignore_cache) {
14369: 	my ($content,$cached)=
14370: 	    &Apache::lonnet::is_cached_new('dns',$url);
14371: 	if ($cached) {
14372: 	    &$func($content,$hashref);
14373: 	    return;
14374: 	}
14375:     }
14376: 
14377:     my %alldns;
14378:     if (open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab")) {
14379:         foreach my $dns (<$config>) {
14380: 	    next if ($dns !~ /^\^(\S*)/x);
14381:             my $line = $1;
14382:             my ($host,$protocol) = split(/:/,$line);
14383:             if ($protocol ne 'https') {
14384:                 $protocol = 'http';
14385:             }
14386: 	    $alldns{$host} = $protocol;
14387:         }
14388:         close($config);
14389:     }
14390:     while (%alldns) {
14391: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
14392: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
14393:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
14394:         delete($alldns{$dns});
14395: 	next if ($response->is_error());
14396:         if ($url eq '/adm/dns/loncapaCRL') {
14397:             return &$func($response);
14398:         } else {
14399: 	    my @content = split("\n",$response->content);
14400: 	    unless ($nocache) {
14401: 	        &do_cache_new('dns',$url,\@content,30*24*60*60);
14402: 	    }
14403: 	    &$func(\@content,$hashref);
14404:             return;
14405:         }
14406:     }
14407:     my $which = (split('/',$url,4))[3];
14408:     if ($which eq 'loncapaCRL') {
14409:         my $diskfile = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14410:         if (-e $diskfile) {
14411:             &logthis("unable to contact DNS, on disk file $diskfile not updated");
14412:         } else {
14413:             &logthis("unable to contact DNS, no on disk file $diskfile available");
14414:         }
14415:     } else {
14416:         &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
14417:         if (open(my $config,"<","$perlvar{'lonTabDir'}/dns_$which.tab")) {
14418:             my @content = <$config>;
14419:             close($config);
14420:             &$func(\@content,$hashref);
14421:         }
14422:     }
14423:     return;
14424: }
14425: 
14426: # ------------------------------------------------------Get DNS checksums file
14427: sub parse_dns_checksums_tab {
14428:     my ($lines,$hashref) = @_;
14429:     my $lonhost = $perlvar{'lonHostID'};
14430:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
14431:     my $loncaparev = &get_server_loncaparev($machine_dom);
14432:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
14433:     my $webconfdir = '/etc/httpd/conf';
14434:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
14435:         $webconfdir = '/etc/apache2';
14436:     } elsif ($distro =~ /^sles(\d+)$/) {
14437:         if ($1 >= 10) {
14438:             $webconfdir = '/etc/apache2';
14439:         }
14440:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
14441:         if ($1 >= 10.0) {
14442:             $webconfdir = '/etc/apache2';
14443:         }
14444:     }
14445:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14446:     my (%chksum,%revnum);
14447:     if (ref($lines) eq 'ARRAY') {
14448:         chomp(@{$lines});
14449:         my $version = shift(@{$lines});
14450:         if ($version eq $release) {  
14451:             foreach my $line (@{$lines}) {
14452:                 my ($file,$version,$shasum) = split(/,/,$line);
14453:                 if ($file =~ m{^/etc/httpd/conf}) {
14454:                     if ($webconfdir eq '/etc/apache2') {
14455:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
14456:                     }
14457:                 }
14458:                 $chksum{$file} = $shasum;
14459:                 $revnum{$file} = $version;
14460:             }
14461:             if (ref($hashref) eq 'HASH') {
14462:                 %{$hashref} = (
14463:                                 sums     => \%chksum,
14464:                                 versions => \%revnum,
14465:                               );
14466:             }
14467:         }
14468:     }
14469:     return;
14470: }
14471: 
14472: sub fetch_dns_checksums {
14473:     my %checksums;
14474:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
14475:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
14476:     my ($release,$timestamp) = split(/\-/,$loncaparev);
14477:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
14478:              \%checksums);
14479:     return \%checksums;
14480: }
14481: 
14482: sub fetch_crl_pemfile {
14483:     return &get_dns("/adm/dns/loncapaCRL",\&save_crl_pem,1,1);
14484: }
14485: 
14486: sub save_crl_pem {
14487:     my ($response) = @_;
14488:     my ($msg,$hadchanges);
14489:     if (ref($response)) {
14490:         my $now = time;
14491:         my $lonca = $perlvar{'lonCertificateDirectory'}.'/'.$perlvar{'lonnetCertificateAuthority'};
14492:         my $tmpcrl = $tmpdir.'/'.$perlvar{'lonnetCertRevocationList'}.'_'.$now.'.'.$$.'.tmp';
14493:         if (open(my $fh,'>',"$tmpcrl")) {
14494:             print $fh $response->content;
14495:             close($fh);
14496:             if (-e $lonca) {
14497:                 if (open(PIPE,"openssl crl -in $tmpcrl -inform pem -CAfile $lonca -noout 2>&1 |")) {
14498:                     my $check = <PIPE>;
14499:                     close(PIPE);
14500:                     chomp($check);
14501:                     if ($check eq 'verify OK') {
14502:                         my $dest = "$perlvar{'lonCertificateDirectory'}/$perlvar{'lonnetCertRevocationList'}";
14503:                         my $backup;
14504:                         if (-e $dest) {
14505:                             if (&File::Copy::move($dest,"$dest.bak")) {
14506:                                 $backup = 'ok';
14507:                             }
14508:                         }
14509:                         if (&File::Copy::move($tmpcrl,$dest)) {
14510:                             $msg = 'ok';
14511:                             if ($backup) {
14512:                                 my (%oldnums,%newnums);
14513:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest.bak |grep 'Serial Number' |")) {
14514:                                     while (<PIPE>) {
14515:                                         $oldnums{(split(/:/))[1]} = 1;
14516:                                     }
14517:                                     close(PIPE);
14518:                                 }
14519:                                 if (open(PIPE, "openssl crl -inform PEM -text -noout -in $dest |grep 'Serial Number' |")) {
14520:                                     while(<PIPE>) {
14521:                                         $newnums{(split(/:/))[1]} = 1;
14522:                                     }
14523:                                     close(PIPE);
14524:                                 }
14525:                                 foreach my $key (sort {$b <=> $a } (keys(%newnums))) {
14526:                                     unless (exists($oldnums{$key})) {
14527:                                         $hadchanges = 1;
14528:                                         last;
14529:                                     }
14530:                                 }
14531:                                 unless ($hadchanges) {
14532:                                     foreach my $key (sort {$b <=> $a } (keys(%oldnums))) {
14533:                                         unless (exists($newnums{$key})) {
14534:                                             $hadchanges = 1;
14535:                                             last;
14536:                                         }
14537:                                     }
14538:                                 }
14539:                             }
14540:                         }
14541:                     } else {
14542:                         unlink($tmpcrl);
14543:                     }
14544:                 } else {
14545:                     unlink($tmpcrl);
14546:                 }
14547:             } else {
14548:                 unlink($tmpcrl);
14549:             }
14550:         }
14551:     }
14552:     return ($msg,$hadchanges);
14553: }
14554: 
14555: # ------------------------------------------------------------ Read domain file
14556: {
14557:     my $loaded;
14558:     my %domain;
14559: 
14560:     sub parse_domain_tab {
14561: 	my ($lines) = @_;
14562: 	foreach my $line (@$lines) {
14563: 	    next if ($line =~ /^(\#|\s*$ )/x);
14564: 
14565: 	    chomp($line);
14566: 	    my ($name,@elements) = split(/:/,$line,9);
14567: 	    my %this_domain;
14568: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
14569: 			       'lang_def', 'city', 'longi', 'lati',
14570: 			       'primary') {
14571: 		$this_domain{$field} = shift(@elements);
14572: 	    }
14573: 	    $domain{$name} = \%this_domain;
14574: 	}
14575:     }
14576: 
14577:     sub reset_domain_info {
14578: 	undef($loaded);
14579: 	undef(%domain);
14580:     }
14581: 
14582:     sub load_domain_tab {
14583: 	my ($ignore_cache,$nocache) = @_;
14584: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
14585: 	my $fh;
14586: 	if (open($fh,"<",$perlvar{'lonTabDir'}.'/domain.tab')) {
14587: 	    my @lines = <$fh>;
14588: 	    &parse_domain_tab(\@lines);
14589: 	}
14590: 	close($fh);
14591: 	$loaded = 1;
14592:     }
14593: 
14594:     sub domain {
14595: 	&load_domain_tab() if (!$loaded);
14596: 
14597: 	my ($name,$what) = @_;
14598: 	return if ( !exists($domain{$name}) );
14599: 
14600: 	if (!$what) {
14601: 	    return $domain{$name}{'description'};
14602: 	}
14603: 	return $domain{$name}{$what};
14604:     }
14605: 
14606:     sub domain_info {
14607:         &load_domain_tab() if (!$loaded);
14608:         return %domain;
14609:     }
14610: 
14611: }
14612: 
14613: 
14614: # ------------------------------------------------------------- Read hosts file
14615: {
14616:     my %hostname;
14617:     my %hostdom;
14618:     my %libserv;
14619:     my $loaded;
14620:     my %name_to_host;
14621:     my %internetdom;
14622:     my %LC_dns_serv;
14623: 
14624:     sub parse_hosts_tab {
14625: 	my ($file) = @_;
14626: 	foreach my $configline (@$file) {
14627: 	    next if ($configline =~ /^(\#|\s*$ )/x);
14628:             chomp($configline);
14629: 	    if ($configline =~ /^\^/) {
14630:                 if ($configline =~ /^\^([\w.\-]+)/) {
14631:                     $LC_dns_serv{$1} = 1;
14632:                 }
14633:                 next;
14634:             }
14635: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
14636: 	    $name=~s/\s//g;
14637: 	    if ($id && $domain && $role && $name) {
14638:                 if ((exists($hostname{$id})) && ($hostname{$id} ne '')) {
14639:                     my $curr = $hostname{$id};
14640:                     my $skip;
14641:                     if (ref($name_to_host{$curr}) eq 'ARRAY') {
14642:                         if (($curr eq $name) && (@{$name_to_host{$curr}} == 1)) {
14643:                             $skip = 1;
14644:                         } else {
14645:                             @{$name_to_host{$curr}} = grep { $_ ne $id } @{$name_to_host{$curr}};
14646:                         }
14647:                     }
14648:                     unless ($skip) {
14649:                         push(@{$name_to_host{$name}},$id);
14650:                     }
14651:                 } else {
14652:                     push(@{$name_to_host{$name}},$id);
14653:                 }
14654: 		$hostname{$id}=$name;
14655: 		$hostdom{$id}=$domain;
14656: 		if ($role eq 'library') { $libserv{$id}=$name; }
14657:                 if (defined($protocol)) {
14658:                     if ($protocol eq 'https') {
14659:                         $protocol{$id} = $protocol;
14660:                     } else {
14661:                         $protocol{$id} = 'http'; 
14662:                     }
14663:                 } else {
14664:                     $protocol{$id} = 'http';
14665:                 }
14666:                 if (defined($intdom)) {
14667:                     $internetdom{$id} = $intdom;
14668:                 }
14669: 	    }
14670: 	}
14671:     }
14672:     
14673:     sub reset_hosts_info {
14674: 	&purge_remembered();
14675: 	&reset_domain_info();
14676: 	&reset_hosts_ip_info();
14677:         undef(%internetdom);
14678: 	undef(%name_to_host);
14679: 	undef(%hostname);
14680: 	undef(%hostdom);
14681: 	undef(%libserv);
14682: 	undef($loaded);
14683:     }
14684: 
14685:     sub load_hosts_tab {
14686: 	my ($ignore_cache,$nocache) = @_;
14687: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
14688: 	open(my $config,"<","$perlvar{'lonTabDir'}/hosts.tab");
14689: 	my @config = <$config>;
14690: 	&parse_hosts_tab(\@config);
14691: 	close($config);
14692: 	$loaded=1;
14693:     }
14694: 
14695:     sub hostname {
14696: 	&load_hosts_tab() if (!$loaded);
14697: 
14698: 	my ($lonid) = @_;
14699: 	return $hostname{$lonid};
14700:     }
14701: 
14702:     sub all_hostnames {
14703: 	&load_hosts_tab() if (!$loaded);
14704: 
14705: 	return %hostname;
14706:     }
14707: 
14708:     sub all_names {
14709:         my ($ignore_cache,$nocache) = @_;
14710: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
14711: 
14712: 	return %name_to_host;
14713:     }
14714: 
14715:     sub all_host_domain {
14716:         &load_hosts_tab() if (!$loaded);
14717:         return %hostdom;
14718:     }
14719: 
14720:     sub all_host_intdom {
14721:         &load_hosts_tab() if (!$loaded);
14722:         return %internetdom;
14723:     }
14724: 
14725:     sub is_library {
14726: 	&load_hosts_tab() if (!$loaded);
14727: 
14728: 	return exists($libserv{$_[0]});
14729:     }
14730: 
14731:     sub all_library {
14732: 	&load_hosts_tab() if (!$loaded);
14733: 
14734: 	return %libserv;
14735:     }
14736: 
14737:     sub unique_library {
14738: 	#2x reverse removes all hostnames that appear more than once
14739:         my %unique = reverse &all_library();
14740:         return reverse %unique;
14741:     }
14742: 
14743:     sub get_servers {
14744: 	&load_hosts_tab() if (!$loaded);
14745: 
14746: 	my ($domain,$type) = @_;
14747: 	my %possible_hosts = ($type eq 'library') ? %libserv
14748: 	                                          : %hostname;
14749: 	my %result;
14750: 	if (ref($domain) eq 'ARRAY') {
14751: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14752: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
14753: 		    $result{$host} = $hostname;
14754: 		}
14755: 	    }
14756: 	} else {
14757: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
14758: 		if ($hostdom{$host} eq $domain) {
14759: 		    $result{$host} = $hostname;
14760: 		}
14761: 	    }
14762: 	}
14763: 	return %result;
14764:     }
14765: 
14766:     sub get_unique_servers {
14767:         my %unique = reverse &get_servers(@_);
14768: 	return reverse %unique;
14769:     }
14770: 
14771:     sub host_domain {
14772: 	&load_hosts_tab() if (!$loaded);
14773: 
14774: 	my ($lonid) = @_;
14775: 	return $hostdom{$lonid};
14776:     }
14777: 
14778:     sub all_domains {
14779: 	&load_hosts_tab() if (!$loaded);
14780: 
14781: 	my %seen;
14782: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
14783: 	return @uniq;
14784:     }
14785: 
14786:     sub internet_dom {
14787:         &load_hosts_tab() if (!$loaded);
14788: 
14789:         my ($lonid) = @_;
14790:         return $internetdom{$lonid};
14791:     }
14792: 
14793:     sub is_LC_dns {
14794:         &load_hosts_tab() if (!$loaded);
14795: 
14796:         my ($hostname) = @_;
14797:         return exists($LC_dns_serv{$hostname});
14798:     }
14799: 
14800: }
14801: 
14802: { 
14803:     my %iphost;
14804:     my %name_to_ip;
14805:     my %lonid_to_ip;
14806: 
14807:     sub get_hosts_from_ip {
14808: 	my ($ip) = @_;
14809: 	my %iphosts = &get_iphost();
14810: 	if (ref($iphosts{$ip})) {
14811: 	    return @{$iphosts{$ip}};
14812: 	}
14813: 	return;
14814:     }
14815:     
14816:     sub reset_hosts_ip_info {
14817: 	undef(%iphost);
14818: 	undef(%name_to_ip);
14819: 	undef(%lonid_to_ip);
14820:     }
14821: 
14822:     sub get_host_ip {
14823: 	my ($lonid) = @_;
14824: 	if (exists($lonid_to_ip{$lonid})) {
14825: 	    return $lonid_to_ip{$lonid};
14826: 	}
14827: 	my $name=&hostname($lonid);
14828:    	my $ip = gethostbyname($name);
14829: 	return if (!$ip || length($ip) ne 4);
14830: 	$ip=inet_ntoa($ip);
14831: 	$name_to_ip{$name}   = $ip;
14832: 	$lonid_to_ip{$lonid} = $ip;
14833: 	return $ip;
14834:     }
14835:     
14836:     sub get_iphost {
14837: 	my ($ignore_cache,$nocache) = @_;
14838: 
14839: 	if (!$ignore_cache) {
14840: 	    if (%iphost) {
14841: 		return %iphost;
14842: 	    }
14843: 	    my ($ip_info,$cached)=
14844: 		&Apache::lonnet::is_cached_new('iphost','iphost');
14845: 	    if ($cached) {
14846: 		%iphost      = %{$ip_info->[0]};
14847: 		%name_to_ip  = %{$ip_info->[1]};
14848: 		%lonid_to_ip = %{$ip_info->[2]};
14849: 		return %iphost;
14850: 	    }
14851: 	}
14852: 
14853: 	# get yesterday's info for fallback
14854: 	my %old_name_to_ip;
14855: 	my ($ip_info,$cached)=
14856: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
14857: 	if ($cached) {
14858: 	    %old_name_to_ip = %{$ip_info->[1]};
14859: 	}
14860: 
14861: 	my %name_to_host = &all_names($ignore_cache,$nocache);
14862: 	foreach my $name (keys(%name_to_host)) {
14863: 	    my $ip;
14864: 	    if (!exists($name_to_ip{$name})) {
14865: 		$ip = gethostbyname($name);
14866: 		if (!$ip || length($ip) ne 4) {
14867: 		    if (defined($old_name_to_ip{$name})) {
14868: 			$ip = $old_name_to_ip{$name};
14869: 			&logthis("Can't find $name defaulting to old $ip");
14870: 		    } else {
14871: 			&logthis("Name $name no IP found");
14872: 			next;
14873: 		    }
14874: 		} else {
14875: 		    $ip=inet_ntoa($ip);
14876: 		}
14877: 		$name_to_ip{$name} = $ip;
14878: 	    } else {
14879: 		$ip = $name_to_ip{$name};
14880: 	    }
14881: 	    foreach my $id (@{ $name_to_host{$name} }) {
14882: 		$lonid_to_ip{$id} = $ip;
14883: 	    }
14884: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
14885: 	}
14886:         unless ($nocache) {
14887: 	    &do_cache_new('iphost','iphost',
14888: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
14889: 		          48*60*60);
14890:         }
14891: 
14892: 	return %iphost;
14893:     }
14894: 
14895:     #
14896:     #  Given a DNS returns the loncapa host name for that DNS 
14897:     # 
14898:     sub host_from_dns {
14899:         my ($dns) = @_;
14900:         my @hosts;
14901:         my $ip;
14902: 
14903:         if (exists($name_to_ip{$dns})) {
14904:             $ip = $name_to_ip{$dns};
14905:         }
14906:         if (!$ip) {
14907:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
14908:             if (length($ip) == 4) { 
14909: 	        $ip   = &IO::Socket::inet_ntoa($ip);
14910:             }
14911:         }
14912:         if ($ip) {
14913: 	    @hosts = get_hosts_from_ip($ip);
14914: 	    return $hosts[0];
14915:         }
14916:         return undef;
14917:     }
14918: 
14919:     sub get_internet_names {
14920:         my ($lonid) = @_;
14921:         return if ($lonid eq '');
14922:         my ($idnref,$cached)=
14923:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
14924:         if ($cached) {
14925:             return $idnref;
14926:         }
14927:         my $ip = &get_host_ip($lonid);
14928:         my @hosts = &get_hosts_from_ip($ip);
14929:         my %iphost = &get_iphost();
14930:         my (@idns,%seen);
14931:         foreach my $id (@hosts) {
14932:             my $dom = &host_domain($id);
14933:             my $prim_id = &domain($dom,'primary');
14934:             my $prim_ip = &get_host_ip($prim_id);
14935:             next if ($seen{$prim_ip});
14936:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
14937:                 foreach my $id (@{$iphost{$prim_ip}}) {
14938:                     my $intdom = &internet_dom($id);
14939:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
14940:                         push(@idns,$intdom);
14941:                     }
14942:                 }
14943:             }
14944:             $seen{$prim_ip} = 1;
14945:         }
14946:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
14947:     }
14948: 
14949: }
14950: 
14951: sub all_loncaparevs {
14952:     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);
14953: }
14954: 
14955: # ---------------------------------------------------------- Read loncaparev table
14956: {
14957:     sub load_loncaparevs { 
14958:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
14959:             if (open(my $config,"<","$perlvar{'lonTabDir'}/loncaparevs.tab")) {
14960:                 while (my $configline=<$config>) {
14961:                     chomp($configline);
14962:                     my ($hostid,$loncaparev)=split(/:/,$configline);
14963:                     $loncaparevs{$hostid}=$loncaparev;
14964:                 }
14965:                 close($config);
14966:             }
14967:         }
14968:     }
14969: }
14970: 
14971: # ---------------------------------------------------------- Read serverhostID table
14972: {
14973:     sub load_serverhomeIDs {
14974:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
14975:             if (open(my $config,"<","$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
14976:                 while (my $configline=<$config>) {
14977:                     chomp($configline);
14978:                     my ($name,$id)=split(/:/,$configline);
14979:                     $serverhomeIDs{$name}=$id;
14980:                 }
14981:                 close($config);
14982:             }
14983:         }
14984:     }
14985: }
14986: 
14987: 
14988: BEGIN {
14989: 
14990: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
14991:     unless ($readit) {
14992: {
14993:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
14994:     %perlvar = (%perlvar,%{$configvars});
14995: }
14996: 
14997: 
14998: # ------------------------------------------------------ Read spare server file
14999: {
15000:     open(my $config,"<","$perlvar{'lonTabDir'}/spare.tab");
15001: 
15002:     while (my $configline=<$config>) {
15003:        chomp($configline);
15004:        if ($configline) {
15005: 	   my ($host,$type) = split(':',$configline,2);
15006: 	   if (!defined($type) || $type eq '') { $type = 'default' };
15007: 	   push(@{ $spareid{$type} }, $host);
15008:        }
15009:     }
15010:     close($config);
15011: }
15012: # ------------------------------------------------------------ Read permissions
15013: {
15014:     open(my $config,"<","$perlvar{'lonTabDir'}/roles.tab");
15015: 
15016:     while (my $configline=<$config>) {
15017: 	chomp($configline);
15018: 	if ($configline) {
15019: 	    my ($role,$perm)=split(/ /,$configline);
15020: 	    if ($perm ne '') { $pr{$role}=$perm; }
15021: 	}
15022:     }
15023:     close($config);
15024: }
15025: 
15026: # -------------------------------------------- Read plain texts for permissions
15027: {
15028:     open(my $config,"<","$perlvar{'lonTabDir'}/rolesplain.tab");
15029: 
15030:     while (my $configline=<$config>) {
15031: 	chomp($configline);
15032: 	if ($configline) {
15033: 	    my ($short,@plain)=split(/:/,$configline);
15034:             %{$prp{$short}} = ();
15035: 	    if (@plain > 0) {
15036:                 $prp{$short}{'std'} = $plain[0];
15037:                 for (my $i=1; $i<@plain; $i++) {
15038:                     $prp{$short}{'alt'.$i} = $plain[$i];  
15039:                 }
15040:             }
15041: 	}
15042:     }
15043:     close($config);
15044: }
15045: 
15046: # ---------------------------------------------------------- Read package table
15047: {
15048:     open(my $config,"<","$perlvar{'lonTabDir'}/packages.tab");
15049: 
15050:     while (my $configline=<$config>) {
15051: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
15052: 	chomp($configline);
15053: 	my ($short,$plain)=split(/:/,$configline);
15054: 	my ($pack,$name)=split(/\&/,$short);
15055: 	if ($plain ne '') {
15056: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
15057: 	    $packagetab{$short}=$plain; 
15058: 	}
15059:     }
15060:     close($config);
15061: }
15062: 
15063: # ---------------------------------------------------------- Read loncaparev table
15064: 
15065: &load_loncaparevs();
15066: 
15067: # ---------------------------------------------------------- Read serverhostID table
15068: 
15069: &load_serverhomeIDs();
15070: 
15071: # ---------------------------------------------------------- Read releaseslist XML
15072: {
15073:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
15074:     if (-e $file) {
15075:         my $parser = HTML::LCParser->new($file);
15076:         while (my $token = $parser->get_token()) {
15077:             if ($token->[0] eq 'S') {
15078:                 my $item = $token->[1];
15079:                 my $name = $token->[2]{'name'};
15080:                 my $value = $token->[2]{'value'};
15081:                 my $valuematch = $token->[2]{'valuematch'};
15082:                 my $namematch = $token->[2]{'namematch'};
15083:                 if ($item eq 'parameter') {
15084:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
15085:                         my $release = $parser->get_text();
15086:                         $release =~ s/(^\s*|\s*$ )//gx;
15087:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
15088:                     }
15089:                 } elsif ($item ne '' && $name ne '') {
15090:                     my $release = $parser->get_text();
15091:                     $release =~ s/(^\s*|\s*$ )//gx;
15092:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
15093:                 }
15094:             }
15095:         }
15096:     }
15097: }
15098: 
15099: # ---------------------------------------------------------- Read managers table
15100: {
15101:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
15102:         if (open(my $config,"<","$perlvar{'lonTabDir'}/managers.tab")) {
15103:             while (my $configline=<$config>) {
15104:                 chomp($configline);
15105:                 next if ($configline =~ /^\#/);
15106:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
15107:                     $managerstab{$configline} = 1;
15108:                 }
15109:             }
15110:             close($config);
15111:         }
15112:     }
15113: }
15114: 
15115: # ------------- set up temporary directory
15116: {
15117:     $tmpdir = LONCAPA::tempdir();
15118: 
15119: }
15120: 
15121: # ------------- set default texengine (domain default overrides this)
15122: {
15123:     $deftex = LONCAPA::texengine();
15124: }
15125: 
15126: # ------------- set default minimum length for passwords for internal auth users
15127: {
15128:     $passwdmin = LONCAPA::passwd_min();
15129: }
15130: 
15131: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
15132: 				'compress_threshold'=> 20_000,
15133:  			        });
15134: 
15135: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
15136: $dumpcount=0;
15137: $locknum=0;
15138: 
15139: &logtouch();
15140: &logthis('<font color="yellow">INFO: Read configuration</font>');
15141: $readit=1;
15142:     {
15143: 	use integer;
15144: 	my $test=(2**32)+1;
15145: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
15146: 	&logthis(" Detected 64bit platform ($_64bit)");
15147:     }
15148: }
15149: }
15150: 
15151: 1;
15152: __END__
15153: 
15154: =pod
15155: 
15156: =head1 NAME
15157: 
15158: Apache::lonnet - Subroutines to ask questions about things in the network.
15159: 
15160: =head1 SYNOPSIS
15161: 
15162: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
15163: 
15164:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
15165: 
15166: Common parameters:
15167: 
15168: =over 4
15169: 
15170: =item *
15171: 
15172: $uname : an internal username (if $cname expecting a course Id specifically)
15173: 
15174: =item *
15175: 
15176: $udom : a domain (if $cdom expecting a course's domain specifically)
15177: 
15178: =item *
15179: 
15180: $symb : a resource instance identifier
15181: 
15182: =item *
15183: 
15184: $namespace : the name of a .db file that contains the data needed or
15185: being set.
15186: 
15187: =back
15188: 
15189: =head1 OVERVIEW
15190: 
15191: lonnet provides subroutines which interact with the
15192: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
15193: about classes, users, and resources.
15194: 
15195: For many of these objects you can also use this to store data about
15196: them or modify them in various ways.
15197: 
15198: =head2 Symbs
15199: 
15200: To identify a specific instance of a resource, LON-CAPA uses symbols
15201: or "symbs"X<symb>. These identifiers are built from the URL of the
15202: map, the resource number of the resource in the map, and the URL of
15203: the resource itself. The latter is somewhat redundant, but might help
15204: if maps change.
15205: 
15206: An example is
15207: 
15208:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
15209: 
15210: The respective map entry is
15211: 
15212:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
15213:   title="Problem 2">
15214:  </resource>
15215: 
15216: Symbs are used by the random number generator, as well as to store and
15217: restore data specific to a certain instance of for example a problem.
15218: 
15219: =head2 Storing And Retrieving Data
15220: 
15221: X<store()>X<cstore()>X<restore()>Three of the most important functions
15222: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
15223: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
15224: is is the non-critical message twin of cstore. These functions are for
15225: handlers to store a perl hash to a user's permanent data space in an
15226: easy manner, and to retrieve it again on another call. It is expected
15227: that a handler would use this once at the beginning to retrieve data,
15228: and then again once at the end to send only the new data back.
15229: 
15230: The data is stored in the user's data directory on the user's
15231: homeserver under the ID of the course.
15232: 
15233: The hash that is returned by restore will have all of the previous
15234: value for all of the elements of the hash.
15235: 
15236: Example:
15237: 
15238:  #creating a hash
15239:  my %hash;
15240:  $hash{'foo'}='bar';
15241: 
15242:  #storing it
15243:  &Apache::lonnet::cstore(\%hash);
15244: 
15245:  #changing a value
15246:  $hash{'foo'}='notbar';
15247: 
15248:  #adding a new value
15249:  $hash{'bar'}='foo';
15250:  &Apache::lonnet::cstore(\%hash);
15251: 
15252:  #retrieving the hash
15253:  my %history=&Apache::lonnet::restore();
15254: 
15255:  #print the hash
15256:  foreach my $key (sort(keys(%history))) {
15257:    print("\%history{$key} = $history{$key}");
15258:  }
15259: 
15260: Will print out:
15261: 
15262:  %history{1:foo} = bar
15263:  %history{1:keys} = foo:timestamp
15264:  %history{1:timestamp} = 990455579
15265:  %history{2:bar} = foo
15266:  %history{2:foo} = notbar
15267:  %history{2:keys} = foo:bar:timestamp
15268:  %history{2:timestamp} = 990455580
15269:  %history{bar} = foo
15270:  %history{foo} = notbar
15271:  %history{timestamp} = 990455580
15272:  %history{version} = 2
15273: 
15274: Note that the special hash entries C<keys>, C<version> and
15275: C<timestamp> were added to the hash. C<version> will be equal to the
15276: total number of versions of the data that have been stored. The
15277: C<timestamp> attribute will be the UNIX time the hash was
15278: stored. C<keys> is available in every historical section to list which
15279: keys were added or changed at a specific historical revision of a
15280: hash.
15281: 
15282: B<Warning>: do not store the hash that restore returns directly. This
15283: will cause a mess since it will restore the historical keys as if the
15284: were new keys. I.E. 1:foo will become 1:1:foo etc.
15285: 
15286: Calling convention:
15287: 
15288:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
15289:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
15290: 
15291: For more detailed information, see lonnet specific documentation.
15292: 
15293: =head1 RETURN MESSAGES
15294: 
15295: =over 4
15296: 
15297: =item * B<con_lost>: unable to contact remote host
15298: 
15299: =item * B<con_delayed>: unable to contact remote host, message will be delivered
15300: when the connection is brought back up
15301: 
15302: =item * B<con_failed>: unable to contact remote host and unable to save message
15303: for later delivery
15304: 
15305: =item * B<error:>: an error a occurred, a description of the error follows the :
15306: 
15307: =item * B<no_such_host>: unable to fund a host associated with the user/domain
15308: that was requested
15309: 
15310: =back
15311: 
15312: =head1 PUBLIC SUBROUTINES
15313: 
15314: =head2 Session Environment Functions
15315: 
15316: =over 4
15317: 
15318: =item * 
15319: X<appenv()>
15320: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
15321: the user envirnoment file, and will be restored for each access this
15322: user makes during this session, also modifies the %env for the current
15323: process. Optional rolesarrayref - if defined contains a reference to an array
15324: of roles which are exempt from the restriction on modifying user.role entries 
15325: in the user's environment.db and in %env.    
15326: 
15327: =item *
15328: X<delenv()>
15329: B<delenv($delthis,$regexp)>: removes all items from the session
15330: environment file that begin with $delthis. If the 
15331: optional second arg - $regexp - is true, $delthis is treated as a 
15332: regular expression, otherwise \Q$delthis\E is used. 
15333: The values are also deleted from the current processes %env.
15334: 
15335: =item * get_env_multiple($name) 
15336: 
15337: gets $name from the %env hash, it seemlessly handles the cases where multiple
15338: values may be defined and end up as an array ref.
15339: 
15340: returns an array of values
15341: 
15342: =back
15343: 
15344: =head2 User Information
15345: 
15346: =over 4
15347: 
15348: =item *
15349: X<queryauthenticate()>
15350: B<queryauthenticate($uname,$udom)>: try to determine user's current 
15351: authentication scheme
15352: 
15353: =item *
15354: X<authenticate()>
15355: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
15356: authenticate user from domain's lib servers (first use the current
15357: one). C<$upass> should be the users password.
15358: $checkdefauth is optional (value is 1 if a check should be made to
15359:    authenticate user using default authentication method, and allow
15360:    account creation if username does not have account in the domain).
15361: $clientcancheckhost is optional (value is 1 if checking whether the
15362:    server can host will occur on the client side in lonauth.pm).   
15363: 
15364: =item *
15365: X<homeserver()>
15366: B<homeserver($uname,$udom)>: find the server which has
15367: the user's directory and files (there must be only one), this caches
15368: the answer, and also caches if there is a borken connection.
15369: 
15370: =item *
15371: X<idget()>
15372: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
15373: a list of student/employee IDs or clicker IDs
15374: (student/employee IDs are a unique resource in a domain, there must be 
15375: only 1 ID per username, and only 1 username per ID in a specific domain).
15376: clickerIDs are not necessarily unique, as students might share clickers.
15377: (returns hash: id=>name,id=>name)
15378: 
15379: =item *
15380: X<idrget()>
15381: B<idrget($udom,@unames)>: find the IDs behind a list of
15382: usernames (returns hash: name=>id,name=>id)
15383: 
15384: =item *
15385: X<idput()>
15386: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
15387: names and associated student/employee IDs or clicker IDs.
15388: 
15389: =item *
15390: X<iddel()>
15391: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
15392: student/employee ID or clicker ID username look-ups from domain.
15393: The homeserver ($uhome) and namespace ($namespace) are optional.
15394: If no $uhome is provided, it will be determined usig &homeserver()
15395: for each user.  If no $namespace is provided, the default is ids.
15396: 
15397: =item *
15398: X<updateclickers()>
15399: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
15400: clicker ID-to-username look-ups in clickers.db on library server.
15401: Permitted actions are add or del (i.e., add or delete). The 
15402: clickers.db contains clickerID as keys (escaped), and each corresponding
15403: value is an escaped comma-separated list of usernames (for whom the
15404: library server is the homeserver), who registered that particular ID.
15405: If $critical is true, the update will be sent via &critical, otherwise
15406: &reply() will be used.
15407: 
15408: =item *
15409: X<rolesinit()>
15410: B<rolesinit($udom,$username)>: get user privileges.
15411: returns user role, first access and timer interval hashes
15412: 
15413: =item *
15414: X<privileged()>
15415: B<privileged($username,$domain)>: returns a true if user has a
15416: privileged and active role (i.e. su or dc), false otherwise.
15417: 
15418: =item *
15419: X<getsection()>
15420: B<getsection($udom,$uname,$cname)>: finds the section of student in the
15421: course $cname, return section name/number or '' for "not in course"
15422: and '-1' for "no section"
15423: 
15424: =item *
15425: X<userenvironment()>
15426: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
15427: passed in @what from the requested user's environment, returns a hash
15428: 
15429: =item * 
15430: X<userlog_query()>
15431: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
15432: activity.log file. %filters defines filters applied when parsing the
15433: log file. These can be start or end timestamps, or the type of action
15434: - log to look for Login or Logout events, check for Checkin or
15435: Checkout, role for role selection. The response is in the form
15436: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
15437: escaped strings of the action recorded in the activity.log file.
15438: 
15439: =back
15440: 
15441: =head2 User Roles
15442: 
15443: =over 4
15444: 
15445: =item *
15446: 
15447: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
15448: returns codes for allowed actions.
15449: 
15450: The first argument is required, all others are optional.
15451: 
15452: $priv is the privilege being checked.
15453: $uri contains additional information about what is being checked for access (e.g.,
15454: URL, course ID etc.). 
15455: $symb is the unique resource instance identifier in a course; if needed,
15456: but not provided, it will be retrieved via a call to &symbread(). 
15457: $role is the role for which a priv is being checked (only used if priv is evb). 
15458: $clientip is the user's IP address (only used when checking for access to portfolio 
15459: files).
15460: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
15461: prevents recursive calls to &allowed.
15462: 
15463:  F: full access
15464:  U,I,K: authentication modes (cxx only)
15465:  '': forbidden
15466:  1: user needs to choose course
15467:  2: browse allowed
15468:  A: passphrase authentication needed
15469:  B: access temporarily blocked because of a blocking event in a course.
15470:  D: access blocked because access is required via session initiated via deep-link 
15471: 
15472: =item *
15473: 
15474: constructaccess($url,$setpriv) : check for access to construction space URL
15475: 
15476: See if the owner domain and name in the URL match those in the
15477: expected environment.  If so, return three element list
15478: ($ownername,$ownerdomain,$ownerhome).
15479: 
15480: Otherwise return the null string.
15481: 
15482: If second argument 'setpriv' is true, it assigns the privileges,
15483: and returns the same three element list, unless the owner has
15484: blocked "ad hoc" Domain Coordinator access to the Author Space,
15485: in which case the null string is returned.
15486: 
15487: =item *
15488: 
15489: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
15490: define a custom role rolename set privileges in format of lonTabs/roles.tab
15491: for system, domain, and course level. $uname and $udom are optional (current
15492: user's username and domain will be used when either of $uname or $udom are absent.
15493: 
15494: =item *
15495: 
15496: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
15497: (rolesplain.tab); plain text explanation of a user role term.
15498: $type is Course (default) or Community.
15499: If $forcedefault evaluates to true, text returned will be default 
15500: text for $type. Otherwise, if this is a course, the text returned 
15501: will be a custom name for the role (if defined in the course's 
15502: environment).  If no custom name is defined the default is returned.
15503:    
15504: =item *
15505: 
15506: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
15507: All arguments are optional. Returns a hash of a roles, either for
15508: co-author/assistant author roles for a user's Construction Space
15509: (default), or if $context is 'userroles', roles for the user himself,
15510: In the hash, keys are set to colon-separated $uname,$udom,$role, and
15511: (optionally) if $withsec is true, a fourth colon-separated item - $section.
15512: For each key, value is set to colon-separated start and end times for
15513: the role.  If no username and domain are specified, will default to
15514: current user/domain. Types, roles, and roledoms are references to arrays
15515: of role statuses (active, future or previous), roles 
15516: (e.g., cc,in, st etc.) and domains of the roles which can be used
15517: to restrict the list of roles reported. If no array ref is 
15518: provided for types, will default to return only active roles.
15519: 
15520: =item *
15521: 
15522: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
15523: user: $uname:$udom has a role in the course: $cdom_$cnum. 
15524: 
15525: Additional optional arguments are: $type (if role checking is to be restricted 
15526: to certain user status types -- previous (expired roles), active (currently
15527: available roles) or future (roles available in the future), and
15528: $hideprivileged -- if true will not report course roles for users who
15529: have active Domain Coordinator role in course's domain or in additional
15530: domains (specified in 'Domains to check for privileged users' in course
15531: environment -- set via:  Course Settings -> Classlists and staff listing).
15532: 
15533: =item *
15534: 
15535: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
15536: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
15537: $possdomains and $possroles are optional array refs -- to domains to check and
15538: roles to check.  If $possdomains is not specified, a dump will be done of the
15539: users' roles.db to check for a dc or su role in any domain. This can be
15540: time consuming if &privileged is called repeatedly (e.g., when displaying a
15541: classlist), so in such cases, supplying a $possdomains array is preferred, as
15542: this then allows &privileged_by_domain() to be used, which caches the identity
15543: of privileged users, eliminating the need for repeated calls to &dump().
15544: 
15545: =item *
15546: 
15547: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
15548: where the outer hash keys are domains specified in the $possdomains array ref,
15549: next inner hash keys are privileged roles specified in the $roles array ref,
15550: and the innermost hash contains key = value pairs for username:domain = end:start
15551: for active or future "privileged" users with that role in that domain. To avoid
15552: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
15553: innerhash are cached using priv_$role and $dom as the identifiers.
15554: 
15555: =back
15556: 
15557: =head2 User Modification
15558: 
15559: =over 4
15560: 
15561: =item *
15562: 
15563: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
15564: user for the level given by URL.  Optional start and end dates (leave empty
15565: string or zero for "no date")
15566: 
15567: =item *
15568: 
15569: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
15570: change a users, password, possible return values are: ok,
15571: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
15572: refused
15573: 
15574: =item *
15575: 
15576: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
15577: 
15578: =item *
15579: 
15580: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
15581:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
15582: 
15583: will update user information (firstname,middlename,lastname,generation,
15584: permanentemail), and if forceid is true, student/employee ID also.
15585: A user's institutional affiliation(s) can also be updated.
15586: User information fields will not be overwritten with empty entries 
15587: unless the field is included in the $candelete array reference.
15588: This array is included when a single user is modified via "Manage Users",
15589: or when Autoupdate.pl is run by cron in a domain.
15590: 
15591: =item *
15592: 
15593: modifystudent
15594: 
15595: modify a student's enrollment and identification information.
15596: The course id is resolved based on the current user's environment.  
15597: This means the invoking user must be a course coordinator or otherwise
15598: associated with a course.
15599: 
15600: This call is essentially a wrapper for lonnet::modifyuser and
15601: lonnet::modify_student_enrollment
15602: 
15603: Inputs: 
15604: 
15605: =over 4
15606: 
15607: =item B<$udom> Student's loncapa domain
15608: 
15609: =item B<$uname> Student's loncapa login name
15610: 
15611: =item B<$uid> Student/Employee ID
15612: 
15613: =item B<$umode> Student's authentication mode
15614: 
15615: =item B<$upass> Student's password
15616: 
15617: =item B<$first> Student's first name
15618: 
15619: =item B<$middle> Student's middle name
15620: 
15621: =item B<$last> Student's last name
15622: 
15623: =item B<$gene> Student's generation
15624: 
15625: =item B<$usec> Student's section in course
15626: 
15627: =item B<$end> Unix time of the roles expiration
15628: 
15629: =item B<$start> Unix time of the roles start date
15630: 
15631: =item B<$forceid> If defined, allow $uid to be changed
15632: 
15633: =item B<$desiredhome> server to use as home server for student
15634: 
15635: =item B<$email> Student's permanent e-mail address
15636: 
15637: =item B<$type> Type of enrollment (auto or manual)
15638: 
15639: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
15640: 
15641: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
15642: 
15643: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
15644: 
15645: =item B<$context> role change context (shown in User Management Logs display in a course)
15646: 
15647: =item B<$inststatus> institutional status of user - : separated string of escaped status types
15648: 
15649: =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.
15650: 
15651: =back
15652: 
15653: =item *
15654: 
15655: modify_student_enrollment
15656: 
15657: Change a student's enrollment status in a class.  The environment variable
15658: 'role.request.course' must be defined for this function to proceed.
15659: 
15660: Inputs:
15661: 
15662: =over 4
15663: 
15664: =item $udom, student's domain
15665: 
15666: =item $uname, student's name
15667: 
15668: =item $uid, student's user id
15669: 
15670: =item $first, student's first name
15671: 
15672: =item $middle
15673: 
15674: =item $last
15675: 
15676: =item $gene
15677: 
15678: =item $usec
15679: 
15680: =item $end
15681: 
15682: =item $start
15683: 
15684: =item $type
15685: 
15686: =item $locktype
15687: 
15688: =item $cid
15689: 
15690: =item $selfenroll
15691: 
15692: =item $context
15693: 
15694: =item $credits, number of credits student will earn from this class
15695: 
15696: =item $instsec, institutional course section code for student
15697: 
15698: =back
15699: 
15700: 
15701: =item *
15702: 
15703: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
15704: custom role; give a custom role to a user for the level given by URL.  Specify
15705: name and domain of role author, and role name
15706: 
15707: =item *
15708: 
15709: revokerole($udom,$uname,$url,$role) : revoke a role for url
15710: 
15711: =item *
15712: 
15713: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
15714: 
15715: =back
15716: 
15717: =head2 Course Infomation
15718: 
15719: =over 4
15720: 
15721: =item *
15722: 
15723: coursedescription($courseid,$options) : returns a hash of information about the
15724: specified course id, including all environment settings for the
15725: course, the description of the course will be in the hash under the
15726: key 'description'
15727: 
15728: $options is an optional parameter that if supplied is a hash reference that controls
15729: what how this function works.  It has the following key/values:
15730: 
15731: =over 4
15732: 
15733: =item freshen_cache
15734: 
15735: If defined, and the environment cache for the course is valid, it is 
15736: returned in the returned hash.
15737: 
15738: =item one_time
15739: 
15740: If defined, the last cache time is set to _now_
15741: 
15742: =item user
15743: 
15744: If defined, the supplied username is used instead of the current user.
15745: 
15746: 
15747: =back
15748: 
15749: =item *
15750: 
15751: resdata($name,$domain,$type,@which) : request for current parameter
15752: setting for a specific $type, where $type is either 'course' or 'user',
15753: @what should be a list of parameters to ask about. This routine caches
15754: answers for 10 minutes.
15755: 
15756: =item *
15757: 
15758: get_courseresdata($courseid, $domain) : dump the entire course resource
15759: data base, returning a hash that is keyed by the resource name and has
15760: values that are the resource value.  I believe that the timestamps and
15761: versions are also returned.
15762: 
15763: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
15764: supplemental content area. This routine caches the number of files for 
15765: 10 minutes.
15766: 
15767: =back
15768: 
15769: =head2 Course Modification
15770: 
15771: =over 4
15772: 
15773: =item *
15774: 
15775: writecoursepref($courseid,%prefs) : write preferences (environment
15776: database) for a course
15777: 
15778: =item *
15779: 
15780: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
15781: 
15782: =item *
15783: 
15784: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
15785: 
15786: =item *
15787: 
15788: is_course($courseid), is_course($cdom, $cnum)
15789: 
15790: Accepts either a combined $courseid (in the form of domain_courseid) or the
15791: two component version $cdom, $cnum. It checks if the specified course exists.
15792: 
15793: Returns:
15794:     undef if the course doesn't exist, otherwise
15795:     in scalar context the combined courseid.
15796:     in list context the two components of the course identifier, domain and 
15797:     courseid.    
15798: 
15799: =back
15800: 
15801: =head2 Bubblesheet Configuration
15802: 
15803: =over 4
15804: 
15805: =item *
15806: 
15807: get_scantron_config($which)
15808: 
15809: $which - the name of the configuration to parse from the file.
15810: 
15811: Parses and returns the bubblesheet configuration line selected as a
15812: hash of configuration file fields.
15813: 
15814: 
15815: Returns:
15816:     If the named configuration is not in the file, an empty
15817:     hash is returned.
15818: 
15819:     a hash with the fields
15820:       name         - internal name for the this configuration setup
15821:       description  - text to display to operator that describes this config
15822:       CODElocation - if 0 or the string 'none'
15823:                           - no CODE exists for this config
15824:                      if -1 || the string 'letter'
15825:                           - a CODE exists for this config and is
15826:                             a string of letters
15827:                      Unsupported value (but planned for future support)
15828:                           if a positive integer
15829:                                - The CODE exists as the first n items from
15830:                                  the question section of the form
15831:                           if the string 'number'
15832:                                - The CODE exists for this config and is
15833:                                  a string of numbers
15834:       CODEstart   - (only matter if a CODE exists) column in the line where
15835:                      the CODE starts
15836:       CODElength  - length of the CODE
15837:       IDstart     - column where the student/employee ID starts
15838:       IDlength    - length of the student/employee ID info
15839:       Qstart      - column where the information from the bubbled
15840:                     'questions' start
15841:       Qlength     - number of columns comprising a single bubble line from
15842:                     the sheet. (usually either 1 or 10)
15843:       Qon         - either a single character representing the character used
15844:                     to signal a bubble was chosen in the positional setup, or
15845:                     the string 'letter' if the letter of the chosen bubble is
15846:                     in the final, or 'number' if a number representing the
15847:                     chosen bubble is in the file (1->A 0->J)
15848:       Qoff        - the character used to represent that a bubble was
15849:                     left blank
15850:       PaperID     - if the scanning process generates a unique number for each
15851:                     sheet scanned the column that this ID number starts in
15852:       PaperIDlength - number of columns that comprise the unique ID number
15853:                       for the sheet of paper
15854:       FirstName   - column that the first name starts in
15855:       FirstNameLength - number of columns that the first name spans
15856:       LastName    - column that the last name starts in
15857:       LastNameLength - number of columns that the last name spans
15858:       BubblesPerRow - number of bubbles available in each row used to
15859:                       bubble an answer. (If not specified, 10 assumed).
15860: 
15861: 
15862: =item *
15863: 
15864: get_scantronformat_file($cdom)
15865: 
15866: $cdom - the course's domain (optional); if not supplied, uses
15867: domain for current $env{'request.course.id'}.
15868: 
15869: Returns an array containing lines from the scantron format file for
15870: the domain of the course.
15871: 
15872: If a url for a custom.tab file is listed in domain's configuration.db,
15873: lines are from this file.
15874: 
15875: Otherwise, if a default.tab has been published in RES space by the
15876: domainconfig user, lines are from this file.
15877: 
15878: Otherwise, fall back to getting lines from the legacy file on the
15879: local server:  /home/httpd/lonTabs/default_scantronformat.tab
15880: 
15881: =back
15882: 
15883: =head2 Resource Subroutines
15884: 
15885: =over 4
15886: 
15887: =item *
15888: 
15889: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
15890: 
15891: =item *
15892: 
15893: repcopy($filename) : subscribes to the requested file, and attempts to
15894: replicate from the owning library server, Might return
15895: 'unavailable', 'not_found', 'forbidden', 'ok', or
15896: 'bad_request', also attempts to grab the metadata for the
15897: resource. Expects the local filesystem pathname
15898: (/home/httpd/html/res/....)
15899: 
15900: =back
15901: 
15902: =head2 Resource Information
15903: 
15904: =over 4
15905: 
15906: =item *
15907: 
15908: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
15909: and returns the value of a variety of different possible values,
15910: $varname should be a request string, and the other parameters can be
15911: used to specify who and what one is asking about. Ordinarily, $cid 
15912: does not need to be specified, as it is retrived from 
15913: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
15914: within lonuserstate::loadmap() when initializing a course, before
15915: $env{'request.course.id'} has been set, so it needs to be provided
15916: in that one case.
15917: 
15918: Possible values for $varname are environment.lastname (or other item
15919: from the envirnment hash), user.name (or someother aspect about the
15920: user), resource.0.maxtries (or some other part and parameter of a
15921: resource)
15922: 
15923: =item *
15924: 
15925: directcondval($number) : get current value of a condition; reads from a state
15926: string
15927: 
15928: =item *
15929: 
15930: condval($condidx) : value of condition index based on state
15931: 
15932: =item *
15933: 
15934: metadata($uri,$what,$toolsymb,$liburi,$prefix,$depthcount) : request a
15935: resource's metadata, $what should be either a specific key, or either
15936: 'keys' (to get a list of possible keys) or 'packages' to get a list of
15937: packages that this resource currently uses, the last 3 arguments are 
15938: only used internally for recursive metadata.
15939: 
15940: the toolsymb is only used where the uri is for an external tool (for which
15941: the uri as well as the symb are guaranteed to be unique).
15942: 
15943: this function automatically caches all requests except any made recursively
15944: to retrieve a list of metadata keys for an imported library file ($liburi is 
15945: defined).
15946: 
15947: =item *
15948: 
15949: metadata_query($query,$custom,$customshow) : make a metadata query against the
15950: network of library servers; returns file handle of where SQL and regex results
15951: will be stored for query
15952: 
15953: =item *
15954: 
15955: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
15956: return symbolic list entry (all arguments optional). 
15957: 
15958: Args: filename is the filename (including path) for the file for which a symb 
15959: is required; donotrecurse, if true will prevent calls to allowed() being made 
15960: to check access status if more than one resource was found in the bighash 
15961: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
15962: a randompick); ignorecachednull, if true will prevent a symb of '' being 
15963: returned if $env{$cache_str} is defined as ''; checkforblock if true will
15964: cause possible symbs to be checked to determine if they are subject to content
15965: blocking, if so they will not be included as possible symbs; possibles is a
15966: ref to a hash, which, as a side effect, will be populated with all possible 
15967: symbs (content blocking not tested).
15968:  
15969: returns the data handle
15970: 
15971: =item *
15972: 
15973: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
15974: and is a possible symb for the URL in $thisfn, and if is an encrypted
15975: resource that the user accessed using /enc/ returns a 1 on success, 0
15976: on failure, user must be in a course, as it assumes the existence of
15977: the course initial hash, and uses $env('request.course.id'}.  The third
15978: arg is an optional reference to a scalar.  If this arg is passed in the 
15979: call to symbverify, it will be set to 1 if the symb has been set to be 
15980: encrypted; otherwise it will be null.  
15981: 
15982: =item *
15983: 
15984: symbclean($symb) : removes versions numbers from a symb, returns the
15985: cleaned symb
15986: 
15987: =item *
15988: 
15989: is_on_map($uri) : checks if the $uri is somewhere on the current
15990: course map, user must be in a course for it to work.
15991: 
15992: =item *
15993: 
15994: numval($salt) : return random seed value (addend for rndseed)
15995: 
15996: =item *
15997: 
15998: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
15999: a random seed, all arguments are optional, if they aren't sent it uses the
16000: environment to derive them. Note: if symb isn't sent and it can't get one
16001: from &symbread it will use the current time as its return value
16002: 
16003: =item *
16004: 
16005: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
16006: unfakeable, receipt
16007: 
16008: =item *
16009: 
16010: receipt() : API to ireceipt working off of env values; given out to users
16011: 
16012: =item *
16013: 
16014: countacc($url) : count the number of accesses to a given URL
16015: 
16016: =item *
16017: 
16018: 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
16019: 
16020: =item *
16021: 
16022: 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)
16023: 
16024: =item *
16025: 
16026: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
16027: 
16028: =item *
16029: 
16030: devalidate($symb) : devalidate temporary spreadsheet calculations,
16031: forcing spreadsheet to reevaluate the resource scores next time.
16032: 
16033: =item * 
16034: 
16035: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
16036: when viewing in course context.
16037: 
16038:  input: six args -- filename (decluttered), course number, course domain,
16039:                     url, symb (if registered) and group (if this is a 
16040:                     group item -- e.g., bulletin board, group page etc.).
16041: 
16042:  output: array of five scalars --
16043:          $cfile -- url for file editing if editable on current server
16044:          $home -- homeserver of resource (i.e., for author if published,
16045:                                           or course if uploaded.).
16046:          $switchserver --  1 if server switch will be needed.
16047:          $forceedit -- 1 if icon/link should be to go to edit mode 
16048:          $forceview -- 1 if icon/link should be to go to view mode
16049: 
16050: =item *
16051: 
16052: is_course_upload($file,$cnum,$cdom)
16053: 
16054: Used in course context to determine if current file was uploaded to 
16055: the course (i.e., would be found in /userfiles/docs on the course's 
16056: homeserver.
16057: 
16058:   input: 3 args -- filename (decluttered), course number and course domain.
16059:   output: boolean -- 1 if file was uploaded.
16060: 
16061: =back
16062: 
16063: =head2 Storing/Retreiving Data
16064: 
16065: =over 4
16066: 
16067: =item *
16068: 
16069: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
16070: permanently for this url; hashref needs to be given and should be a \%hashname;
16071: the remaining args aren't required and if they aren't passed or are '' they will
16072: be derived from the env (with the exception of $laststore, which is an 
16073: optional arg used when a user's submission is stored in grading).
16074: $laststore is $version=$timestamp, where $version is the most recent version
16075: number retrieved for the corresponding $symb in the $namespace db file, and
16076: $timestamp is the timestamp for that transaction (UNIX time).
16077: $laststore is currently only passed when cstore() is called by 
16078: structuretags::finalize_storage().
16079: 
16080: =item *
16081: 
16082: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
16083: but uses critical subroutine
16084: 
16085: =item *
16086: 
16087: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
16088: all args are optional
16089: 
16090: =item *
16091: 
16092: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
16093: dumps the complete (or key matching regexp) namespace into a hash
16094: ($udom, $uname, $regexp, $range are optional) for a namespace that is
16095: normally &store()ed into
16096: 
16097: $range should be either an integer '100' (give me the first 100
16098:                                            matching records)
16099:               or be  two integers sperated by a - with no spaces
16100:                  '30-50' (give me the 30th through the 50th matching
16101:                           records)
16102: 
16103: 
16104: =item *
16105: 
16106: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
16107: replaces a &store() version of data with a replacement set of data
16108: for a particular resource in a namespace passed in the $storehash hash 
16109: reference. If $tolog is true, the transaction is logged in the courselog
16110: with an action=PUTSTORE.
16111: 
16112: =item *
16113: 
16114: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
16115: works very similar to store/cstore, but all data is stored in a
16116: temporary location and can be reset using tmpreset, $storehash should
16117: be a hash reference, returns nothing on success
16118: 
16119: =item *
16120: 
16121: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
16122: similar to restore, but all data is stored in a temporary location and
16123: can be reset using tmpreset. Returns a hash of values on success,
16124: error string otherwise.
16125: 
16126: =item *
16127: 
16128: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
16129: deltes all keys for $symb form the temporary storage hash.
16130: 
16131: =item *
16132: 
16133: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16134: reference filled in from namesp ($udom and $uname are optional)
16135: 
16136: =item *
16137: 
16138: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
16139: namesp ($udom and $uname are optional)
16140: 
16141: =item *
16142: 
16143: dump($namespace,$udom,$uname,$regexp,$range) : 
16144: dumps the complete (or key matching regexp) namespace into a hash
16145: ($udom, $uname, $regexp, $range are optional)
16146: 
16147: $range should be either an integer '100' (give me the first 100
16148:                                            matching records)
16149:               or be  two integers sperated by a - with no spaces
16150:                  '30-50' (give me the 30th through the 50th matching
16151:                           records)
16152: =item *
16153: 
16154: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
16155: $store can be a scalar, an array reference, or if the amount to be 
16156: incremented is > 1, a hash reference.
16157: 
16158: ($udom and $uname are optional)
16159: 
16160: =item *
16161: 
16162: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
16163: ($udom and $uname are optional)
16164: 
16165: =item *
16166: 
16167: cput($namespace,$storehash,$udom,$uname) : critical put
16168: ($udom and $uname are optional)
16169: 
16170: =item *
16171: 
16172: newput($namespace,$storehash,$udom,$uname) :
16173: 
16174: Attempts to store the items in the $storehash, but only if they don't
16175: currently exist, if this succeeds you can be certain that you have 
16176: successfully created a new key value pair in the $namespace db.
16177: 
16178: 
16179: Args:
16180:  $namespace: name of database to store values to
16181:  $storehash: hashref to store to the db
16182:  $udom: (optional) domain of user containing the db
16183:  $uname: (optional) name of user caontaining the db
16184: 
16185: Returns:
16186:  'ok' -> succeeded in storing all keys of $storehash
16187:  'key_exists: <key>' -> failed to anything out of $storehash, as at
16188:                         least <key> already existed in the db (other
16189:                         requested keys may also already exist)
16190:  'error: <msg>' -> unable to tie the DB or other error occurred
16191:  'con_lost' -> unable to contact request server
16192:  'refused' -> action was not allowed by remote machine
16193: 
16194: 
16195: =item *
16196: 
16197: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
16198: reference filled in from namesp (encrypts the return communication)
16199: ($udom and $uname are optional)
16200: 
16201: =item *
16202: 
16203: log($udom,$name,$home,$message) : write to permanent log for user; use
16204: critical subroutine
16205: 
16206: =item *
16207: 
16208: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
16209: array reference filled in from namespace found in domain level on either
16210: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
16211: 
16212: =item *
16213: 
16214: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
16215: domain level either on specified domain server ($uhome) or primary domain 
16216: server ($udom and $uhome are optional)
16217: 
16218: =item * 
16219: 
16220: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
16221: for: authentication, language, quotas, timezone, date locale, and portal URL in
16222: the target domain.
16223: 
16224: May also include additional key => value pairs for the following groups:
16225: 
16226: =over
16227: 
16228: =item
16229: disk quotas (MB allocated by default to portfolios and authoring spaces).
16230: 
16231: =over
16232: 
16233: =item defaultquota, authorquota
16234: 
16235: =back
16236: 
16237: =item
16238: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
16239: portfolio for users).
16240: 
16241: =over
16242: 
16243: =item
16244: aboutme, blog, webdav, portfolio
16245: 
16246: =back
16247: 
16248: =item
16249: requestcourses: ability to request courses, and how requests are processed.
16250: 
16251: =over
16252: 
16253: =item
16254: official, unofficial, community, textbook, placement
16255: 
16256: =back
16257: 
16258: =item
16259: inststatus: types of institutional affiliation, and order in which they are displayed.
16260: 
16261: =over
16262: 
16263: =item
16264: inststatustypes, inststatusorder, inststatusguest
16265: 
16266: =back
16267: 
16268: =item
16269: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
16270: for course's uploaded content.
16271: 
16272: =over
16273: 
16274: =item
16275: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
16276: communityquota, textbookquota, placementquota
16277: 
16278: =back
16279: 
16280: =item
16281: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
16282: on your servers.
16283: 
16284: =over
16285: 
16286: =item 
16287: remotesessions, hostedsessions
16288: 
16289: =back
16290: 
16291: =back
16292: 
16293: In cases where a domain coordinator has never used the "Set Domain Configuration"
16294: utility to create a configuration.db file on a domain's primary library server 
16295: only the following domain defaults: auth_def, auth_arg_def, lang_def
16296: -- corresponding values are authentication type (internal, krb4, krb5,
16297: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
16298: will be available. Values are retrieved from cache (if current), unless the
16299: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
16300: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
16301: 
16302: Typical usage:
16303: 
16304: %domdefaults = &get_domain_defaults($target_domain);
16305: 
16306: =back
16307: 
16308: =head2 Network Status Functions
16309: 
16310: =over 4
16311: 
16312: =item *
16313: 
16314: dirlist() : return directory list based on URI (first arg).
16315: 
16316: Inputs: 1 required, 5 optional.
16317: 
16318: =over
16319: 
16320: =item 
16321: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
16322: 
16323: =item
16324: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
16325: 
16326: =item
16327: $username -  username of user/course to be listed. Extracted from $uri if absent. 
16328: 
16329: =item
16330: $getpropath - boolean: 1 if prepend path using &propath(). 
16331: 
16332: =item
16333: $getuserdir - boolean: 1 if prepend path for "userfiles".
16334: 
16335: =item 
16336: $alternateRoot - path to prepend in place of path from $uri.
16337: 
16338: =back
16339: 
16340: Returns: Array of up to two items.
16341: 
16342: =over
16343: 
16344: a reference to an array of files/subdirectories
16345: 
16346: =over
16347: 
16348: Each element in the array of files/subdirectories is a & separated list of
16349: item name and the result of running stat on the item.  If dirlist was requested
16350: for a file instead of a directory, the item name will be ''. For a directory 
16351: listing, if the item is a metadata file, the element will end &N&M 
16352: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
16353: default copyright set (1).  
16354: 
16355: =back
16356: 
16357: a scalar containing error condition (if encountered).
16358: 
16359: =over
16360: 
16361: =item 
16362: no_host (no homeserver identified for $username:$domain).
16363: 
16364: =item 
16365: no_such_host (server contacted for listing not identified as valid host).
16366: 
16367: =item 
16368: con_lost (connection to remote server failed).
16369: 
16370: =item 
16371: refused (invalid $username:$domain received on lond side).
16372: 
16373: =item 
16374: no_such_dir (directory at specified path on lond side does not exist). 
16375: 
16376: =item 
16377: empty (directory at specified path on lond side is empty).
16378: 
16379: =over
16380: 
16381: This is currently not encountered because the &ls3, &ls2, 
16382: &ls (_handler) routines on the lond side do not filter out
16383: . and .. from a directory listing. 
16384: 
16385: =back
16386: 
16387: =back
16388: 
16389: =back
16390: 
16391: =item *
16392: 
16393: spareserver() : find server with least workload from spare.tab
16394: 
16395: 
16396: =item *
16397: 
16398: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
16399: if there is no corresponding loncapa host.
16400: 
16401: =back
16402: 
16403: 
16404: =head2 Apache Request
16405: 
16406: =over 4
16407: 
16408: =item *
16409: 
16410: ssi($url,%hash) : server side include, does a complete request cycle on url to
16411: localhost, posts hash
16412: 
16413: =back
16414: 
16415: =head2 Data to String to Data
16416: 
16417: =over 4
16418: 
16419: =item *
16420: 
16421: hash2str(%hash) : convert a hash into a string complete with escaping and '='
16422: and '&' separators, supports elements that are arrayrefs and hashrefs
16423: 
16424: =item *
16425: 
16426: hashref2str($hashref) : convert a hashref into a string complete with
16427: escaping and '=' and '&' separators, supports elements that are
16428: arrayrefs and hashrefs
16429: 
16430: =item *
16431: 
16432: arrayref2str($arrayref) : convert an arrayref into a string complete
16433: with escaping and '&' separators, supports elements that are arrayrefs
16434: and hashrefs
16435: 
16436: =item *
16437: 
16438: str2hash($string) : convert string to hash using unescaping and
16439: splitting on '=' and '&', supports elements that are arrayrefs and
16440: hashrefs
16441: 
16442: =item *
16443: 
16444: str2array($string) : convert string to hash using unescaping and
16445: splitting on '&', supports elements that are arrayrefs and hashrefs
16446: 
16447: =back
16448: 
16449: =head2 Logging Routines
16450: 
16451: 
16452: These routines allow one to make log messages in the lonnet.log and
16453: lonnet.perm logfiles.
16454: 
16455: =over 4
16456: 
16457: =item *
16458: 
16459: logtouch() : make sure the logfile, lonnet.log, exists
16460: 
16461: =item *
16462: 
16463: logthis() : append message to the normal lonnet.log file, it gets
16464: preiodically rolled over and deleted.
16465: 
16466: =item *
16467: 
16468: logperm() : append a permanent message to lonnet.perm.log, this log
16469: file never gets deleted by any automated portion of the system, only
16470: messages of critical importance should go in here.
16471: 
16472: 
16473: =back
16474: 
16475: =head2 General File Helper Routines
16476: 
16477: =over 4
16478: 
16479: =item *
16480: 
16481: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
16482: (a) files in /uploaded
16483:   (i) If a local copy of the file exists - 
16484:       compares modification date of local copy with last-modified date for 
16485:       definitive version stored on home server for course. If local copy is 
16486:       stale, requests a new version from the home server and stores it. 
16487:       If the original has been removed from the home server, then local copy 
16488:       is unlinked.
16489:   (ii) If local copy does not exist -
16490:       requests the file from the home server and stores it. 
16491:   
16492:   If $caller is 'uploadrep':  
16493:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
16494:     for request for files originally uploaded via DOCS. 
16495:      - returns 'ok' if fresh local copy now available, -1 otherwise.
16496:   
16497:   Otherwise:
16498:      This indicates a call from the content generation phase of the request.
16499:      -  returns the entire contents of the file or -1.
16500:      
16501: (b) files in /res
16502:    - returns the entire contents of a file or -1; 
16503:    it properly subscribes to and replicates the file if neccessary.
16504: 
16505: 
16506: =item *
16507: 
16508: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
16509:                   reference
16510: 
16511: returns either a stat() list of data about the file or an empty list
16512: if the file doesn't exist or couldn't find out about it (connection
16513: problems or user unknown)
16514: 
16515: =item *
16516: 
16517: filelocation($dir,$file) : returns file system location of a file
16518: based on URI; meant to be "fairly clean" absolute reference, $dir is a
16519: directory that relative $file lookups are to looked in ($dir of /a/dir
16520: and a file of ../bob will become /a/bob)
16521: 
16522: =item *
16523: 
16524: hreflocation($dir,$file) : returns file system location or a URL; same as
16525: filelocation except for hrefs
16526: 
16527: =item *
16528: 
16529: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
16530: also removes beginning /home/httpd/html unless /priv/ follows it.
16531: 
16532: =back
16533: 
16534: =head2 Usererfile file routines (/uploaded*)
16535: 
16536: =over 4
16537: 
16538: =item *
16539: 
16540: userfileupload(): main rotine for putting a file in a user or course's
16541:                   filespace, arguments are,
16542: 
16543:  formname - required - this is the name of the element in $env where the
16544:            filename, and the contents of the file to create/modifed exist
16545:            the filename is in $env{'form.'.$formname.'.filename'} and the
16546:            contents of the file is located in $env{'form.'.$formname}
16547:  context - if coursedoc, store the file in the course of the active role
16548:              of the current user; 
16549:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
16550:            if 'canceloverwrite': delete file in tmp/overwrites directory
16551:  subdir - required - subdirectory to put the file in under ../userfiles/
16552:          if undefined, it will be placed in "unknown"
16553: 
16554:  (This routine calls clean_filename() to remove any dangerous
16555:  characters from the filename, and then calls finuserfileupload() to
16556:  complete the transaction)
16557: 
16558:  returns either the url of the uploaded file (/uploaded/....) if successful
16559:  and /adm/notfound.html if unsuccessful
16560: 
16561: =item *
16562: 
16563: clean_filename(): routine for cleaing a filename up for storage in
16564:                  userfile space, argument is:
16565: 
16566:  filename - proposed filename
16567: 
16568: returns: the new clean filename
16569: 
16570: =item *
16571: 
16572: finishuserfileupload(): routine that creates and sends the file to
16573: userspace, probably shouldn't be called directly
16574: 
16575:   docuname: username or courseid of destination for the file
16576:   docudom: domain of user/course of destination for the file
16577:   formname: same as for userfileupload()
16578:   fname: filename (including subdirectories) for the file
16579:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
16580:           if hashref, and context is scantron, will convert csv format to standard format
16581:   allfiles: reference to hash used to store objects found by parser
16582:   codebase: reference to hash used for codebases of java objects found by parser
16583:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
16584:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
16585:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
16586:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
16587:   context: if 'overwrite', will move the uploaded file from its temporary location to
16588:             userfiles to facilitate overwriting a previously uploaded file with same name.
16589:   mimetype: reference to scalar to accommodate mime type determined
16590:             from File::MMagic if $parser = parse.
16591: 
16592:  returns either the url of the uploaded file (/uploaded/....) if successful
16593:  and /adm/notfound.html if unsuccessful (or an error message if context 
16594:  was 'overwrite').
16595:  
16596: 
16597: =item *
16598: 
16599: renameuserfile(): renames an existing userfile to a new name
16600: 
16601:   Args:
16602:    docuname: username or courseid of destination for the file
16603:    docudom: domain of user/course of destination for the file
16604:    old: current file name (including any subdirs under userfiles)
16605:    new: desired file name (including any subdirs under userfiles)
16606: 
16607: =item *
16608: 
16609: mkdiruserfile(): creates a directory is a userfiles dir
16610: 
16611:   Args:
16612:    docuname: username or courseid of destination for the file
16613:    docudom: domain of user/course of destination for the file
16614:    dir: dir to create (including any subdirs under userfiles)
16615: 
16616: =item *
16617: 
16618: removeuserfile(): removes a file that exists in userfiles
16619: 
16620:   Args:
16621:    docuname: username or courseid of destination for the file
16622:    docudom: domain of user/course of destination for the file
16623:    fname: filname to delete (including any subdirs under userfiles)
16624: 
16625: =item *
16626: 
16627: removeuploadedurl(): convience function for removeuserfile()
16628: 
16629:   Args:
16630:    url:  a full /uploaded/... url to delete
16631: 
16632: =item * 
16633: 
16634: get_portfile_permissions():
16635:   Args:
16636:     domain: domain of user or course contain the portfolio files
16637:     user: name of user or num of course contain the portfolio files
16638:   Returns:
16639:     hashref of a dump of the proper file_permissions.db
16640:    
16641: 
16642: =item * 
16643: 
16644: get_access_controls():
16645: 
16646: Args:
16647:   current_permissions: the hash ref returned from get_portfile_permissions()
16648:   group: (optional) the group you want the files associated with
16649:   file: (optional) the file you want access info on
16650: 
16651: Returns:
16652:     a hash (keys are file names) of hashes containing
16653:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
16654:         values are XML containing access control settings (see below) 
16655: 
16656: Internal notes:
16657: 
16658:  access controls are stored in file_permissions.db as key=value pairs.
16659:     key -> path to file/file_name\0uniqueID:scope_end_start
16660:         where scope -> public,guest,course,group,domains or users.
16661:               end -> UNIX time for end of access (0 -> no end date)
16662:               start -> UNIX time for start of access
16663: 
16664:     value -> XML description of access control
16665:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
16666:             <start></start>
16667:             <end></end>
16668: 
16669:             <password></password>  for scope type = guest
16670: 
16671:             <domain></domain>     for scope type = course or group
16672:             <number></number>
16673:             <roles id="">
16674:              <role></role>
16675:              <access></access>
16676:              <section></section>
16677:              <group></group>
16678:             </roles>
16679: 
16680:             <dom></dom>         for scope type = domains
16681: 
16682:             <users>             for scope type = users
16683:              <user>
16684:               <uname></uname>
16685:               <udom></udom>
16686:              </user>
16687:             </users>
16688:            </scope> 
16689:               
16690:  Access data is also aggregated for each file in an additional key=value pair:
16691:  key -> path to file/file_name\0accesscontrol 
16692:  value -> reference to hash
16693:           hash contains key = value pairs
16694:           where key = uniqueID:scope_end_start
16695:                 value = UNIX time record was last updated
16696: 
16697:           Used to improve speed of look-ups of access controls for each file.  
16698:  
16699:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
16700: 
16701: =item *
16702: 
16703: modify_access_controls():
16704: 
16705: Modifies access controls for a portfolio file
16706: Args
16707: 1. file name
16708: 2. reference to hash of required changes,
16709: 3. domain
16710: 4. username
16711:   where domain,username are the domain of the portfolio owner 
16712:   (either a user or a course) 
16713: 
16714: Returns:
16715: 1. result of additions or updates ('ok' or 'error', with error message). 
16716: 2. result of deletions ('ok' or 'error', with error message).
16717: 3. reference to hash of any new or updated access controls.
16718: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
16719:    key = integer (inbound ID)
16720:    value = uniqueID
16721: 
16722: =item *
16723: 
16724: get_timebased_id():
16725: 
16726: Attempts to get a unique timestamp-based suffix for use with items added to a 
16727: course via the Course Editor (e.g., folders, composite pages, 
16728: group bulletin boards).
16729: 
16730: Args: (first three required; six others optional)
16731: 
16732: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
16733:    docssequence, or name of group
16734: 
16735: 2. keyid (alphanumeric): name of temporary locking key in hash,
16736:    e.g., num, boardids
16737: 
16738: 3. namespace: name of gdbm file used to store suffixes already assigned;  
16739:    file will be named nohist_namespace.db
16740: 
16741: 4. cdom: domain of course; default is current course domain from %env
16742: 
16743: 5. cnum: course number; default is current course number from %env
16744: 
16745: 6. idtype: set to concat if an additional digit is to be appended to the 
16746:    unix timestamp to form the suffix, if the plain timestamp is already
16747:    in use.  Default is to not do this, but simply increment the unix 
16748:    timestamp by 1 until a unique key is obtained.
16749: 
16750: 7. who: holder of locking key; defaults to user:domain for user.
16751: 
16752: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
16753:    retrying); default is 3.
16754: 
16755: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
16756: 
16757: Returns:
16758: 
16759: 1. suffix obtained (numeric)
16760: 
16761: 2. result of deleting locking key (ok if deleted, or lock never obtained)
16762: 
16763: 3. error: contains (localized) error message if an error occurred.
16764: 
16765: 
16766: =back
16767: 
16768: =head2 HTTP Helper Routines
16769: 
16770: =over 4
16771: 
16772: =item *
16773: 
16774: escape() : unpack non-word characters into CGI-compatible hex codes
16775: 
16776: =item *
16777: 
16778: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
16779: 
16780: =back
16781: 
16782: =head1 PRIVATE SUBROUTINES
16783: 
16784: =head2 Underlying communication routines (Shouldn't call)
16785: 
16786: =over 4
16787: 
16788: =item *
16789: 
16790: subreply() : tries to pass a message to lonc, returns con_lost if incapable
16791: 
16792: =item *
16793: 
16794: reply() : uses subreply to send a message to remote machine, logs all failures
16795: 
16796: =item *
16797: 
16798: critical() : passes a critical message to another server; if cannot
16799: get through then place message in connection buffer directory and
16800: returns con_delayed, if incapable of saving message, returns
16801: con_failed
16802: 
16803: =item *
16804: 
16805: reconlonc() : tries to reconnect lonc client processes.
16806: 
16807: =back
16808: 
16809: =head2 Resource Access Logging
16810: 
16811: =over 4
16812: 
16813: =item *
16814: 
16815: flushcourselogs() : flush (save) buffer logs and access logs
16816: 
16817: =item *
16818: 
16819: courselog($what) : save message for course in hash
16820: 
16821: =item *
16822: 
16823: courseacclog($what) : save message for course using &courselog().  Perform
16824: special processing for specific resource types (problems, exams, quizzes, etc).
16825: 
16826: =item *
16827: 
16828: goodbye() : flush course logs and log shutting down; it is called in srm.conf
16829: as a PerlChildExitHandler
16830: 
16831: =back
16832: 
16833: =head2 Other
16834: 
16835: =over 4
16836: 
16837: =item *
16838: 
16839: symblist($mapname,%newhash) : update symbolic storage links
16840: 
16841: =back
16842: 
16843: =cut
16844: 

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