Annotation of loncom/lonnet/perl/lonnet.pm, revision 1.1345

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1345  ! raeburn     4: # $Id: lonnet.pm,v 1.1344 2017/05/09 03:04:32 raeburn Exp $
1.178     www         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: #
1.169     harris41   28: ###
                     29: 
1.971     jms        30: =pod
                     31: 
1.972     jms        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: 
1.971     jms        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: 
1.1       albertel   71: package Apache::lonnet;
                     72: 
                     73: use strict;
1.486     www        74: use HTTP::Date;
1.977     amueller   75: use Image::Magick;
                     76: 
1.1182    foxr       77: 
1.1173    foxr       78: use Encode;
                     79: 
1.1245    raeburn    80: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1138    raeburn    81:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
                     82:             %managerstab);
1.871     albertel   83: 
                     84: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     85:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     86:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        87:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        88: 
1.1       albertel   89: use IO::Socket;
1.31      www        90: use GDBM_File;
1.208     albertel   91: use HTML::LCParser;
1.88      www        92: use Fcntl qw(:flock);
1.870     albertel   93: use Storable qw(thaw nfreeze);
1.1289    damieng    94: use Time::HiRes qw( sleep gettimeofday tv_interval );
1.599     albertel   95: use Cache::Memcached;
1.676     albertel   96: use Digest::MD5;
1.790     albertel   97: use Math::Random;
1.1024    raeburn    98: use File::MMagic;
1.807     albertel   99: use LONCAPA qw(:DEFAULT :match);
1.740     www       100: use LONCAPA::Configuration;
1.1160    www       101: use LONCAPA::lonmetadata;
1.1167    droeschl  102: use LONCAPA::Lond;
1.1345  ! raeburn   103: use LONCAPA::LWPReq;
1.1117    foxr      104: 
1.1090    raeburn   105: use File::Copy;
1.676     albertel  106: 
1.195     www       107: my $readit;
1.1288    damieng   108: my $max_connection_retries = 20;     # Or some such value.
1.1       albertel  109: 
1.619     albertel  110: require Exporter;
                    111: 
                    112: our @ISA = qw (Exporter);
                    113: our @EXPORT = qw(%env);
                    114: 
1.449     matthew   115: 
1.1187    raeburn   116: # ------------------------------------ Logging (parameters, docs, slots, roles)
1.729     www       117: {
                    118:     my $logid;
1.1187    raeburn   119:     sub write_log {
1.1188    raeburn   120: 	my ($context,$hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
1.1184    raeburn   121:         if ($context eq 'course') {
                    122:             if (($cnum eq '') || ($cdom eq '')) {
                    123:                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    124:                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    125:             }
1.957     raeburn   126:         }
1.1184    raeburn   127: 	$logid ++;
1.957     raeburn   128:         my $now = time();
                    129: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.1184    raeburn   130:         my $logentry = { 
                    131:                           $id => {
                    132:                                    'exe_uname' => $env{'user.name'},
                    133:                                    'exe_udom'  => $env{'user.domain'},
                    134:                                    'exe_time'  => $now,
                    135:                                    'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    136:                                    'delflag'   => $delflag,
                    137:                                    'logentry'  => $storehash,
                    138:                                    'uname'     => $uname,
                    139:                                    'udom'      => $udom,
                    140:                                   }
                    141:                        };
                    142: 	return &put('nohist_'.$hash_name,$logentry,$cdom,$cnum);
1.729     www       143:     }
                    144: }
1.1       albertel  145: 
1.163     harris41  146: sub logtouch {
                    147:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  148:     unless (-e "$execdir/logs/lonnet.log") {	
                    149: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  150: 	close $fh;
                    151:     }
                    152:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    153:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    154: }
                    155: 
1.1       albertel  156: sub logthis {
                    157:     my $message=shift;
                    158:     my $execdir=$perlvar{'lonDaemons'};
                    159:     my $now=time;
                    160:     my $local=localtime($now);
1.448     albertel  161:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986     foxr      162: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    163: 	print $fh $logstring;
1.448     albertel  164: 	close($fh);
                    165:     }
1.1       albertel  166:     return 1;
                    167: }
                    168: 
                    169: sub logperm {
                    170:     my $message=shift;
                    171:     my $execdir=$perlvar{'lonDaemons'};
                    172:     my $now=time;
                    173:     my $local=localtime($now);
1.448     albertel  174:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    175: 	print $fh "$now:$message:$local\n";
                    176: 	close($fh);
                    177:     }
1.1       albertel  178:     return 1;
                    179: }
                    180: 
1.850     albertel  181: sub create_connection {
1.853     albertel  182:     my ($hostname,$lonid) = @_;
1.851     albertel  183:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  184: 				     Type    => SOCK_STREAM,
                    185: 				     Timeout => 10);
                    186:     return 0 if (!$client);
1.890     albertel  187:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  188:     my $result = <$client>;
                    189:     chomp($result);
                    190:     return 1 if ($result eq 'done');
                    191:     return 0;
                    192: }
                    193: 
1.983     raeburn   194: sub get_server_timezone {
                    195:     my ($cnum,$cdom) = @_;
                    196:     my $home=&homeserver($cnum,$cdom);
                    197:     if ($home ne 'no_host') {
                    198:         my $cachetime = 24*3600;
                    199:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    200:         if (defined($cached)) {
                    201:             return $timezone;
                    202:         } else {
                    203:             my $timezone = &reply('servertimezone',$home);
                    204:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    205:         }
                    206:     }
                    207: }
1.850     albertel  208: 
1.1106    raeburn   209: sub get_server_distarch {
                    210:     my ($lonhost,$ignore_cache) = @_;
                    211:     if (defined($lonhost)) {
                    212:         if (!defined(&hostname($lonhost))) {
                    213:             return;
                    214:         }
                    215:         my $cachetime = 12*3600;
                    216:         if (!$ignore_cache) {
                    217:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    218:             if (defined($cached)) {
                    219:                 return $distarch;
                    220:             }
                    221:         }
                    222:         my $rep = &reply('serverdistarch',$lonhost);
                    223:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    224:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    225:                 $rep eq '') {
                    226:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    227:         }
                    228:     }
                    229:     return;
                    230: }
                    231: 
1.1315    raeburn   232: sub get_servercerts_info {
                    233:     my ($lonhost,$context) = @_;
                    234:     my ($rep,$uselocal);
                    235:     if (grep { $_ eq $lonhost } &current_machine_ids()) {
                    236:         $uselocal = 1;
                    237:     }
1.1316    raeburn   238:     if (($context ne 'cgi') && ($uselocal)) {
1.1315    raeburn   239:         my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
1.1323    raeburn   240:         if ($distro eq '') {
                    241:             $uselocal = 0;
                    242:         } elsif ($distro =~ /^(?:centos|redhat|scientific)(\d+)$/) {
1.1315    raeburn   243:             if ($1 < 6) {
                    244:                 $uselocal = 0;
                    245:             }
                    246:         }
                    247:     }
                    248:     if ($uselocal) {
                    249:         $rep = LONCAPA::Lond::server_certs(\%perlvar);
                    250:     } else {
                    251:         $rep=&reply('servercerts',$lonhost);
                    252:     }
                    253:     my ($result,%returnhash);
                    254:     if (defined($lonhost)) {
                    255:         if (!defined(&hostname($lonhost))) {
                    256:             return;
                    257:         }
                    258:     }
                    259:     if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    260:         ($rep eq 'unknown_cmd')) {
                    261:         $result = $rep;
                    262:     } else {
                    263:         $result = 'ok';
                    264:         my @pairs=split(/\&/,$rep);
                    265:         foreach my $item (@pairs) {
                    266:             my ($key,$value)=split(/=/,$item,2);
                    267:             my $what = &unescape($key);
                    268:             $returnhash{$what}=&thaw_unescape($value);
                    269:         }
                    270:     }
                    271:     return ($result,\%returnhash);
                    272: }
                    273: 
1.993     raeburn   274: sub get_server_loncaparev {
1.1073    raeburn   275:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   276:     if (defined($lonhost)) {
                    277:         if (!defined(&hostname($lonhost))) {
                    278:             undef($lonhost);
                    279:         }
                    280:     }
                    281:     if (!defined($lonhost)) {
                    282:         if (defined(&domain($dom,'primary'))) {
                    283:             $lonhost=&domain($dom,'primary');
                    284:             if ($lonhost eq 'no_host') {
                    285:                 undef($lonhost);
                    286:             }
                    287:         }
                    288:     }
                    289:     if (defined($lonhost)) {
1.1073    raeburn   290:         my $cachetime = 12*3600;
                    291:         if (!$ignore_cache) {
                    292:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    293:             if (defined($cached)) {
                    294:                 return $loncaparev;
                    295:             }
                    296:         }
                    297:         my ($answer,$loncaparev);
                    298:         my @ids=&current_machine_ids();
                    299:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    300:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   301:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   302:                 $loncaparev = $1;
                    303:             }
                    304:         } else {
                    305:             $answer = &reply('serverloncaparev',$lonhost);
                    306:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    307:                 if ($caller eq 'loncron') {
                    308:                     my $protocol = $protocol{$lonhost};
                    309:                     $protocol = 'http' if ($protocol ne 'https');
                    310:                     my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
                    311:                     my $request=new HTTP::Request('GET',$url);
1.1345  ! raeburn   312:                     my $response=&LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar,4,1);
1.1073    raeburn   313:                     unless ($response->is_error()) {
                    314:                         my $content = $response->content;
1.1081    raeburn   315:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   316:                             $loncaparev = $1;
                    317:                         }
                    318:                     }
                    319:                 } else {
                    320:                     $loncaparev = $loncaparevs{$lonhost};
                    321:                 }
1.1081    raeburn   322:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   323:                 $loncaparev = $1;
                    324:             }
1.993     raeburn   325:         }
1.1073    raeburn   326:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   327:     }
                    328: }
                    329: 
1.1074    raeburn   330: sub get_server_homeID {
                    331:     my ($hostname,$ignore_cache,$caller) = @_;
                    332:     unless ($ignore_cache) {
                    333:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    334:         if (defined($cached)) {
                    335:             return $serverhomeID;
                    336:         }
                    337:     }
                    338:     my $cachetime = 12*3600;
                    339:     my $serverhomeID;
                    340:     if ($caller eq 'loncron') { 
                    341:         my @machine_ids = &machine_ids($hostname);
                    342:         foreach my $id (@machine_ids) {
                    343:             my $response = &reply('serverhomeID',$id);
                    344:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    345:                 $serverhomeID = $response;
                    346:                 last;
                    347:             }
                    348:         }
                    349:         if ($serverhomeID eq '') {
                    350:             $serverhomeID = $machine_ids[-1];
                    351:         }
                    352:     } else {
                    353:         $serverhomeID = $serverhomeIDs{$hostname};
                    354:     }
                    355:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    356: }
                    357: 
1.1121    raeburn   358: sub get_remote_globals {
                    359:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   360:     my ($result,%returnhash,%whatneeded);
                    361:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   362:         foreach my $what (sort(keys(%{$whathash}))) {
                    363:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   364:             my ($response,$cached);
1.1121    raeburn   365:             unless ($ignore_cache) {
1.1125    raeburn   366:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   367:             }
                    368:             if (defined($cached)) {
1.1125    raeburn   369:                 $returnhash{$what} = $response;
1.1121    raeburn   370:             } else {
1.1125    raeburn   371:                 $whatneeded{$what} = 1;
1.1121    raeburn   372:             }
                    373:         }
1.1125    raeburn   374:         if (keys(%whatneeded) == 0) {
                    375:             $result = 'ok';
                    376:         } else {
1.1121    raeburn   377:             my $requested = &freeze_escape(\%whatneeded);
                    378:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   379:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    380:                 ($rep eq 'unknown_cmd')) {
                    381:                 $result = $rep;
                    382:             } else {
                    383:                 $result = 'ok';
1.1121    raeburn   384:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   385:                 foreach my $item (@pairs) {
                    386:                     my ($key,$value)=split(/=/,$item,2);
                    387:                     my $what = &unescape($key);
                    388:                     my $hashid = $lonhost.'-'.$what;
                    389:                     $returnhash{$what}=&thaw_unescape($value);
                    390:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   391:                 }
                    392:             }
                    393:         }
                    394:     }
1.1125    raeburn   395:     return ($result,\%returnhash);
1.1121    raeburn   396: }
                    397: 
1.1124    raeburn   398: sub remote_devalidate_cache {
1.1241    raeburn   399:     my ($lonhost,$cachekeys) = @_;
                    400:     my $items;
                    401:     return unless (ref($cachekeys) eq 'ARRAY');
                    402:     my $cachestr = join('&',@{$cachekeys});
                    403:     my $response = &reply('devalidatecache:'.&escape($cachestr),$lonhost);
1.1124    raeburn   404:     return $response;
                    405: }
                    406: 
1.1       albertel  407: # -------------------------------------------------- Non-critical communication
                    408: sub subreply {
                    409:     my ($cmd,$server)=@_;
1.838     albertel  410:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      411:     #
                    412:     #  With loncnew process trimming, there's a timing hole between lonc server
                    413:     #  process exit and the master server picking up the listen on the AF_UNIX
                    414:     #  socket.  In that time interval, a lock file will exist:
                    415: 
                    416:     my $lockfile=$peerfile.".lock";
                    417:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
1.1289    damieng   418: 	sleep(0.1);
1.549     foxr      419:     }
                    420:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      421:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      422:     #
1.550     foxr      423:     #   We'll give the connection a few tries before abandoning it.  If
                    424:     #   connection is not possible, we'll con_lost back to the client.
                    425:     #   
                    426:     my $client;
                    427:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    428: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    429: 				      Type    => SOCK_STREAM,
                    430: 				      Timeout => 10);
1.869     albertel  431: 	if ($client) {
1.550     foxr      432: 	    last;		# Connected!
1.850     albertel  433: 	} else {
1.853     albertel  434: 	    &create_connection(&hostname($server),$server);
1.550     foxr      435: 	}
1.1289    damieng   436:         sleep(0.1);	# Try again later if failed connection.
1.550     foxr      437:     }
                    438:     my $answer;
                    439:     if ($client) {
1.704     albertel  440: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      441: 	$answer=<$client>;
                    442: 	if (!$answer) { $answer="con_lost"; }
                    443: 	chomp($answer);
                    444:     } else {
                    445: 	$answer = 'con_lost';	# Failed connection.
                    446:     }
1.1       albertel  447:     return $answer;
                    448: }
                    449: 
                    450: sub reply {
                    451:     my ($cmd,$server)=@_;
1.838     albertel  452:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  453:     my $answer=subreply($cmd,$server);
1.65      www       454:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  455:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       456:                 " $cmd to $server returned $answer</font>");
                    457:     }
1.1       albertel  458:     return $answer;
                    459: }
                    460: 
                    461: # ----------------------------------------------------------- Send USR1 to lonc
                    462: 
                    463: sub reconlonc {
1.891     albertel  464:     my ($lonid) = @_;
                    465:     if ($lonid) {
1.1295    raeburn   466:         my $hostname = &hostname($lonid);
1.891     albertel  467: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    468: 	if ($hostname && -e $peerfile) {
                    469: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    470: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    471: 					     Type    => SOCK_STREAM,
                    472: 					     Timeout => 10);
                    473: 	    if ($client) {
                    474: 		print $client ("reset_retries\n");
                    475: 		my $answer=<$client>;
                    476: 		#reset just this one.
                    477: 	    }
                    478: 	}
                    479: 	return;
                    480:     }
                    481: 
1.836     www       482:     &logthis("Trying to reconnect lonc");
1.1       albertel  483:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  484:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  485: 	my $loncpid=<$fh>;
                    486:         chomp($loncpid);
                    487:         if (kill 0 => $loncpid) {
                    488: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    489:             kill USR1 => $loncpid;
                    490:             sleep 1;
1.1295    raeburn   491:         } else {
1.12      www       492: 	    &logthis(
1.672     albertel  493:                "<font color=\"blue\">WARNING:".
1.12      www       494:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  495:         }
                    496:     } else {
1.836     www       497: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  498:     }
                    499: }
                    500: 
                    501: # ------------------------------------------------------ Critical communication
1.12      www       502: 
1.1       albertel  503: sub critical {
                    504:     my ($cmd,$server)=@_;
1.838     albertel  505:     unless (&hostname($server)) {
1.672     albertel  506:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       507:                " Critical message to unknown server ($server)</font>");
                    508:         return 'no_such_host';
                    509:     }
1.1       albertel  510:     my $answer=reply($cmd,$server);
                    511:     if ($answer eq 'con_lost') {
1.1295    raeburn   512: 	&reconlonc($server);
1.589     albertel  513: 	my $answer=reply($cmd,$server);
1.1       albertel  514:         if ($answer eq 'con_lost') {
                    515:             my $now=time;
                    516:             my $middlename=$cmd;
1.5       www       517:             $middlename=substr($middlename,0,16);
1.1       albertel  518:             $middlename=~s/\W//g;
                    519:             my $dfilename=
1.305     www       520:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    521:             $dumpcount++;
1.1       albertel  522:             {
1.448     albertel  523: 		my $dfh;
                    524: 		if (open($dfh,">$dfilename")) {
                    525: 		    print $dfh "$cmd\n"; 
                    526: 		    close($dfh);
                    527: 		}
1.1       albertel  528:             }
1.1288    damieng   529:             sleep 1;
1.1       albertel  530:             my $wcmd='';
                    531:             {
1.448     albertel  532: 		my $dfh;
                    533: 		if (open($dfh,"<$dfilename")) {
                    534: 		    $wcmd=<$dfh>; 
                    535: 		    close($dfh);
                    536: 		}
1.1       albertel  537:             }
                    538:             chomp($wcmd);
1.7       www       539:             if ($wcmd eq $cmd) {
1.672     albertel  540: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       541:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  542:                 &logperm("D:$server:$cmd");
                    543: 	        return 'con_delayed';
                    544:             } else {
1.672     albertel  545:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       546:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  547:                 &logperm("F:$server:$cmd");
                    548:                 return 'con_failed';
                    549:             }
                    550:         }
                    551:     }
                    552:     return $answer;
1.405     albertel  553: }
                    554: 
1.755     albertel  555: # ------------------------------------------- check if return value is an error
                    556: 
                    557: sub error {
                    558:     my ($result) = @_;
1.756     albertel  559:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  560: 	if ($2 == 2) { return undef; }
                    561: 	return $1;
                    562:     }
                    563:     return undef;
                    564: }
                    565: 
1.783     albertel  566: sub convert_and_load_session_env {
                    567:     my ($lonidsdir,$handle)=@_;
                    568:     my @profile;
                    569:     {
1.917     albertel  570: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    571: 	if (!$opened) {
1.915     albertel  572: 	    return 0;
                    573: 	}
1.783     albertel  574: 	flock($idf,LOCK_SH);
                    575: 	@profile=<$idf>;
                    576: 	close($idf);
                    577:     }
                    578:     my %temp_env;
                    579:     foreach my $line (@profile) {
1.786     albertel  580: 	if ($line !~ m/=/) {
                    581: 	    return 0;
                    582: 	}
1.783     albertel  583: 	chomp($line);
                    584: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    585: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    586:     }
                    587:     unlink("$lonidsdir/$handle.id");
                    588:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    589: 	    0640)) {
                    590: 	%disk_env = %temp_env;
                    591: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    592: 	untie(%disk_env);
                    593:     }
1.786     albertel  594:     return 1;
1.783     albertel  595: }
                    596: 
1.374     www       597: # ------------------------------------------- Transfer profile into environment
1.780     albertel  598: my $env_loaded;
                    599: sub transfer_profile_to_env {
1.788     albertel  600:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    601:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       602: 
1.720     albertel  603:     if (!defined($lonidsdir)) {
                    604: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    605:     }
                    606:     if (!defined($handle)) {
                    607:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    608:     }
                    609: 
1.786     albertel  610:     my $convert;
                    611:     {
1.917     albertel  612:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    613: 	if (!$opened) {
1.915     albertel  614: 	    return;
                    615: 	}
1.786     albertel  616: 	flock($idf,LOCK_SH);
                    617: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    618: 		&GDBM_READER(),0640)) {
                    619: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    620: 	    untie(%disk_env);
                    621: 	} else {
                    622: 	    $convert = 1;
                    623: 	}
                    624:     }
                    625:     if ($convert) {
                    626: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    627: 	    &logthis("Failed to load session, or convert session.");
                    628: 	}
1.374     www       629:     }
1.783     albertel  630: 
1.786     albertel  631:     my %remove;
1.783     albertel  632:     while ( my $envname = each(%env) ) {
1.433     matthew   633:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    634:             if ($time < time-300) {
1.783     albertel  635:                 $remove{$key}++;
1.433     matthew   636:             }
                    637:         }
                    638:     }
1.783     albertel  639: 
1.619     albertel  640:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  641:     $env_loaded=1;
1.783     albertel  642:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   643:         &delenv($expired_key);
1.374     www       644:     }
1.1       albertel  645: }
                    646: 
1.916     albertel  647: # ---------------------------------------------------- Check for valid session 
                    648: sub check_for_valid_session {
1.1245    raeburn   649:     my ($r,$name,$userhashref) = @_;
1.916     albertel  650:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1337    raeburn   651:     my ($linkname,$pubname);
1.1155    raeburn   652:     if ($name eq '') {
                    653:         $name = 'lonID';
1.1337    raeburn   654:         $linkname = 'lonLinkID';
                    655:         $pubname = 'lonPubID';
1.1155    raeburn   656:     }
                    657:     my $lonid=$cookies{$name};
1.1337    raeburn   658:     if (!$lonid) {
                    659:         if (($name eq 'lonID') && ($ENV{'SERVER_PORT'} != 443) && ($linkname)) {
                    660:             $lonid=$cookies{$linkname};
                    661:         }
                    662:         if (!$lonid) {
                    663:             if (($name eq 'lonID') && ($pubname)) {
                    664:                 $lonid=$cookies{$pubname};
                    665:             }
                    666:         }
                    667:     }
1.916     albertel  668:     return undef if (!$lonid);
                    669: 
                    670:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1155    raeburn   671:     my $lonidsdir;
                    672:     if ($name eq 'lonDAV') {
                    673:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    674:     } else {
                    675:         $lonidsdir=$r->dir_config('lonIDsDir');
                    676:     }
1.916     albertel  677:     return undef if (!-e "$lonidsdir/$handle.id");
                    678: 
1.917     albertel  679:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    680:     return undef if (!$opened);
1.916     albertel  681: 
                    682:     flock($idf,LOCK_SH);
                    683:     my %disk_env;
                    684:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    685: 	    &GDBM_READER(),0640)) {
                    686: 	return undef;	
                    687:     }
                    688: 
                    689:     if (!defined($disk_env{'user.name'})
                    690: 	|| !defined($disk_env{'user.domain'})) {
                    691: 	return undef;
                    692:     }
1.1245    raeburn   693: 
                    694:     if (ref($userhashref) eq 'HASH') {
                    695:         $userhashref->{'name'} = $disk_env{'user.name'};
                    696:         $userhashref->{'domain'} = $disk_env{'user.domain'};
1.1212    raeburn   697:     }
1.1245    raeburn   698: 
1.916     albertel  699:     return $handle;
                    700: }
                    701: 
1.830     albertel  702: sub timed_flock {
                    703:     my ($file,$lock_type) = @_;
                    704:     my $failed=0;
                    705:     eval {
                    706: 	local $SIG{__DIE__}='DEFAULT';
                    707: 	local $SIG{ALRM}=sub {
                    708: 	    $failed=1;
                    709: 	    die("failed lock");
                    710: 	};
                    711: 	alarm(13);
                    712: 	flock($file,$lock_type);
                    713: 	alarm(0);
                    714:     };
                    715:     if ($failed) {
                    716: 	return undef;
                    717:     } else {
                    718: 	return 1;
                    719:     }
                    720: }
                    721: 
1.5       www       722: # ---------------------------------------------------------- Append Environment
                    723: 
                    724: sub appenv {
1.949     raeburn   725:     my ($newenv,$roles) = @_;
                    726:     if (ref($newenv) eq 'HASH') {
                    727:         foreach my $key (keys(%{$newenv})) {
                    728:             my $refused = 0;
                    729: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    730:                 $refused = 1;
                    731:                 if (ref($roles) eq 'ARRAY') {
1.1250    raeburn   732:                     my ($type,$role) = ($key =~ m{^user\.(role|priv)\.(.+?)\./});
1.949     raeburn   733:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    734:                         $refused = 0;
                    735:                     }
                    736:                 }
                    737:             }
                    738:             if ($refused) {
                    739:                 &logthis("<font color=\"blue\">WARNING: ".
                    740:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    741:                          .'</font>');
                    742: 	        delete($newenv->{$key});
                    743:             } else {
                    744:                 $env{$key}=$newenv->{$key};
                    745:             }
                    746:         }
                    747:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    748:         if ($opened
                    749: 	    && &timed_flock($env_file,LOCK_EX)
                    750: 	    &&
                    751: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    752: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    753: 	    while (my ($key,$value) = each(%{$newenv})) {
                    754: 	        $disk_env{$key} = $value;
                    755: 	    }
                    756: 	    untie(%disk_env);
1.35      www       757:         }
1.191     harris41  758:     }
1.56      www       759:     return 'ok';
                    760: }
                    761: # ----------------------------------------------------- Delete from Environment
                    762: 
                    763: sub delenv {
1.1104    raeburn   764:     my ($delthis,$regexp,$roles) = @_;
                    765:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    766:         my $refused = 1;
                    767:         if (ref($roles) eq 'ARRAY') {
                    768:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    769:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    770:                 $refused = 0;
                    771:             }
                    772:         }
                    773:         if ($refused) {
                    774:             &logthis("<font color=\"blue\">WARNING: ".
                    775:                      "Attempt to delete from environment ".$delthis);
                    776:             return 'error';
                    777:         }
1.56      www       778:     }
1.917     albertel  779:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    780:     if ($opened
1.915     albertel  781: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  782: 	&&
                    783: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    784: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  785: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   786: 	    if ($regexp) {
                    787:                 if ($key=~/^$delthis/) {
                    788:                     delete($env{$key});
                    789:                     delete($disk_env{$key});
                    790:                 } 
                    791:             } else {
                    792:                 if ($key=~/^\Q$delthis\E/) {
                    793: 		    delete($env{$key});
                    794: 		    delete($disk_env{$key});
                    795: 	        }
                    796:             }
1.448     albertel  797: 	}
1.783     albertel  798: 	untie(%disk_env);
1.5       www       799:     }
                    800:     return 'ok';
1.369     albertel  801: }
                    802: 
1.790     albertel  803: sub get_env_multiple {
                    804:     my ($name) = @_;
                    805:     my @values;
                    806:     if (defined($env{$name})) {
                    807:         # exists is it an array
                    808:         if (ref($env{$name})) {
                    809:             @values=@{ $env{$name} };
                    810:         } else {
                    811:             $values[0]=$env{$name};
                    812:         }
                    813:     }
                    814:     return(@values);
                    815: }
                    816: 
1.958     www       817: # ------------------------------------------------------------------- Locking
                    818: 
                    819: sub set_lock {
                    820:     my ($text)=@_;
                    821:     $locknum++;
                    822:     my $id=$$.'-'.$locknum;
                    823:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    824:              'session.lock.'.$id => $text});
                    825:     return $id;
                    826: }
                    827: 
                    828: sub get_locks {
                    829:     my $num=0;
                    830:     my %texts=();
                    831:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    832:        if ($lock=~/\w/) {
                    833:           $num++;
                    834:           $texts{$lock}=$env{'session.lock.'.$lock};
                    835:        }
                    836:    }
                    837:    return ($num,%texts);
                    838: }
                    839: 
                    840: sub remove_lock {
                    841:     my ($id)=@_;
                    842:     my $newlocks='';
                    843:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    844:        if (($lock=~/\w/) && ($lock ne $id)) {
                    845:           $newlocks.=','.$lock;
                    846:        }
                    847:     }
                    848:     &appenv({'session.locks' => $newlocks});
                    849:     &delenv('session.lock.'.$id);
                    850: }
                    851: 
                    852: sub remove_all_locks {
                    853:     my $activelocks=$env{'session.locks'};
                    854:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    855:        if ($lock=~/\w/) {
                    856:           &remove_lock($lock);
                    857:        }
                    858:     }
                    859: }
                    860: 
                    861: 
1.369     albertel  862: # ------------------------------------------ Find out current server userload
                    863: sub userload {
                    864:     my $numusers=0;
                    865:     {
                    866: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    867: 	my $filename;
                    868: 	my $curtime=time;
                    869: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  870: 	    next if ($filename eq '.' || $filename eq '..');
                    871: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.404     albertel  872: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  873: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  874: 	}
                    875: 	closedir(LONIDS);
                    876:     }
                    877:     my $userloadpercent=0;
                    878:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    879:     if ($maxuserload) {
1.371     albertel  880: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  881:     }
1.372     albertel  882:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  883:     return $userloadpercent;
1.283     www       884: }
                    885: 
1.1       albertel  886: # ------------------------------ Find server with least workload from spare.tab
1.11      www       887: 
1.1       albertel  888: sub spareserver {
1.1083    raeburn   889:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  890:     my $spare_server;
1.370     albertel  891:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  892:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    893:                                                      :  $userloadpercent;
1.1083    raeburn   894:     my ($uint_dom,$remotesessions);
                    895:     if (($udom ne '') && (&domain($udom) ne '')) {
                    896:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    897:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    898:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    899:         $remotesessions = $udomdefaults{'remotesessions'};
                    900:     }
1.1123    raeburn   901:     my $spareshash = &this_host_spares($udom);
                    902:     if (ref($spareshash) eq 'HASH') {
                    903:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    904:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
1.1279    raeburn   905:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    906:                                              $try_server));
1.1123    raeburn   907: 	        ($spare_server, $lowest_load) =
                    908: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                    909:             }
1.1083    raeburn   910:         }
1.784     albertel  911: 
1.1123    raeburn   912:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    913: 
                    914:         if (!$found_server) {
                    915:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                    916: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
1.1279    raeburn   917:                     next unless (&spare_can_host($udom,$uint_dom,
                    918:                                                  $remotesessions,$try_server));
1.1123    raeburn   919: 	            ($spare_server, $lowest_load) =
                    920: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                    921:                 }
                    922: 	    }
                    923:         }
1.784     albertel  924:     }
                    925: 
                    926:     if (!$want_server_name) {
1.968     raeburn   927:         my $protocol = 'http';
                    928:         if ($protocol{$spare_server} eq 'https') {
                    929:             $protocol = $protocol{$spare_server};
                    930:         }
1.1001    raeburn   931:         if (defined($spare_server)) {
                    932:             my $hostname = &hostname($spare_server);
1.1083    raeburn   933:             if (defined($hostname)) {
1.1001    raeburn   934: 	        $spare_server = $protocol.'://'.$hostname;
                    935:             }
                    936:         }
1.784     albertel  937:     }
                    938:     return $spare_server;
                    939: }
                    940: 
                    941: sub compare_server_load {
1.1253    raeburn   942:     my ($try_server, $spare_server, $lowest_load, $required) = @_;
                    943: 
                    944:     if ($required) {
                    945:         my ($reqdmajor,$reqdminor) = ($required =~ /^(\d+)\.(\d+)$/);
                    946:         my $remoterev = &get_server_loncaparev(undef,$try_server);
                    947:         my ($major,$minor) = ($remoterev =~ /^\'?(\d+)\.(\d+)\.[\w.\-]+\'?$/);
                    948:         if (($major eq '' && $minor eq '') ||
                    949:             (($reqdmajor > $major) || (($reqdmajor == $major) && ($reqdminor > $minor)))) {
                    950:             return ($spare_server,$lowest_load);
                    951:         }
                    952:     }
1.784     albertel  953: 
                    954:     my $loadans     = &reply('load',    $try_server);
                    955:     my $userloadans = &reply('userload',$try_server);
                    956: 
                    957:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn   958: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel  959:     }
                    960: 
                    961:     my $load;
                    962:     if ($loadans =~ /\d/) {
                    963: 	if ($userloadans =~ /\d/) {
                    964: 	    #both are numbers, pick the bigger one
                    965: 	    $load = ($loadans > $userloadans) ? $loadans 
                    966: 		                              : $userloadans;
1.411     albertel  967: 	} else {
1.784     albertel  968: 	    $load = $loadans;
1.411     albertel  969: 	}
1.784     albertel  970:     } else {
                    971: 	$load = $userloadans;
                    972:     }
                    973: 
                    974:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    975: 	$spare_server = $try_server;
                    976: 	$lowest_load  = $load;
1.370     albertel  977:     }
1.784     albertel  978:     return ($spare_server,$lowest_load);
1.202     matthew   979: }
1.914     albertel  980: 
                    981: # --------------------------- ask offload servers if user already has a session
                    982: sub find_existing_session {
                    983:     my ($udom,$uname) = @_;
1.1123    raeburn   984:     my $spareshash = &this_host_spares($udom);
                    985:     if (ref($spareshash) eq 'HASH') {
                    986:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    987:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                    988:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    989:             }
                    990:         }
                    991:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                    992:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                    993:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    994:             }
                    995:         }
1.914     albertel  996:     }
                    997:     return;
                    998: }
                    999: 
                   1000: # -------------------------------- ask if server already has a session for user
                   1001: sub has_user_session {
                   1002:     my ($lonid,$udom,$uname) = @_;
                   1003:     my $result = &reply(join(':','userhassession',
                   1004: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                   1005:     return 1 if ($result eq 'ok');
                   1006: 
                   1007:     return 0;
                   1008: }
                   1009: 
1.1076    raeburn  1010: # --------- determine least loaded server in a user's domain which allows login
                   1011: 
                   1012: sub choose_server {
1.1259    raeburn  1013:     my ($udom,$checkloginvia,$required,$skiploadbal) = @_;
1.1076    raeburn  1014:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn  1015:     my %servers = &get_servers($udom);
1.1076    raeburn  1016:     my $lowest_load = 30000;
1.1259    raeburn  1017:     my ($login_host,$hostname,$portal_path,$isredirect,$balancers);
                   1018:     if ($skiploadbal) {
                   1019:         ($balancers,my $cached)=&is_cached_new('loadbalancing',$udom);
                   1020:         unless (defined($cached)) {
                   1021:             my $cachetime = 60*60*24;
                   1022:             my %domconfig =
                   1023:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1024:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1025:                 $balancers = &do_cache_new('loadbalancing',$udom,$domconfig{'loadbalancing'},
                   1026:                                            $cachetime);
                   1027:             }
                   1028:         }
                   1029:     }
1.1076    raeburn  1030:     foreach my $lonhost (keys(%servers)) {
1.1259    raeburn  1031:         if ($skiploadbal) {
                   1032:             if (ref($balancers) eq 'HASH') {
                   1033:                 next if (exists($balancers->{$lonhost}));
                   1034:             }
                   1035:         }   
1.1115    raeburn  1036:         my $loginvia;
                   1037:         if ($checkloginvia) {
                   1038:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn  1039:             if ($loginvia) {
                   1040:                 my ($server,$path) = split(/:/,$loginvia);
                   1041:                 ($login_host, $lowest_load) =
1.1253    raeburn  1042:                     &compare_server_load($server, $login_host, $lowest_load, $required);
1.1116    raeburn  1043:                 if ($login_host eq $server) {
                   1044:                     $portal_path = $path;
1.1151    raeburn  1045:                     $isredirect = 1;
1.1116    raeburn  1046:                 }
                   1047:             } else {
                   1048:                 ($login_host, $lowest_load) =
1.1253    raeburn  1049:                     &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1116    raeburn  1050:                 if ($login_host eq $lonhost) {
                   1051:                     $portal_path = '';
1.1151    raeburn  1052:                     $isredirect = ''; 
1.1116    raeburn  1053:                 }
                   1054:             }
                   1055:         } else {
1.1076    raeburn  1056:             ($login_host, $lowest_load) =
1.1253    raeburn  1057:                 &compare_server_load($lonhost, $login_host, $lowest_load, $required);
1.1076    raeburn  1058:         }
                   1059:     }
                   1060:     if ($login_host ne '') {
1.1116    raeburn  1061:         $hostname = &hostname($login_host);
1.1076    raeburn  1062:     }
1.1331    raeburn  1063:     return ($login_host,$hostname,$portal_path,$isredirect,$lowest_load);
1.1076    raeburn  1064: }
                   1065: 
1.202     matthew  1066: # --------------------------------------------- Try to change a user's password
                   1067: 
                   1068: sub changepass {
1.799     raeburn  1069:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew  1070:     $currentpass = &escape($currentpass);
                   1071:     $newpass     = &escape($newpass);
1.1030    raeburn  1072:     my $lonhost = $perlvar{'lonHostID'};
                   1073:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew  1074: 		       $server);
                   1075:     if (! $answer) {
                   1076: 	&logthis("No reply on password change request to $server ".
                   1077: 		 "by $uname in domain $udom.");
                   1078:     } elsif ($answer =~ "^ok") {
                   1079:         &logthis("$uname in $udom successfully changed their password ".
                   1080: 		 "on $server.");
                   1081:     } elsif ($answer =~ "^pwchange_failure") {
                   1082: 	&logthis("$uname in $udom was unable to change their password ".
                   1083: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1084: 		 "or pwchange");
                   1085:     } elsif ($answer =~ "^non_authorized") {
                   1086:         &logthis("$uname in $udom did not get their password correct when ".
                   1087: 		 "attempting to change it on $server.");
                   1088:     } elsif ($answer =~ "^auth_mode_error") {
                   1089:         &logthis("$uname in $udom attempted to change their password despite ".
                   1090: 		 "not being locally or internally authenticated on $server.");
                   1091:     } elsif ($answer =~ "^unknown_user") {
                   1092:         &logthis("$uname in $udom attempted to change their password ".
                   1093: 		 "on $server but were unable to because $server is not ".
                   1094: 		 "their home server.");
                   1095:     } elsif ($answer =~ "^refused") {
                   1096: 	&logthis("$server refused to change $uname in $udom password because ".
                   1097: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1098:     } elsif ($answer =~ "invalid_client") {
                   1099:         &logthis("$server refused to change $uname in $udom password because ".
                   1100:                  "it was a reset by e-mail originating from an invalid server.");
1.202     matthew  1101:     }
                   1102:     return $answer;
1.1       albertel 1103: }
                   1104: 
1.169     harris41 1105: # ----------------------- Try to determine user's current authentication scheme
                   1106: 
                   1107: sub queryauthenticate {
                   1108:     my ($uname,$udom)=@_;
1.456     albertel 1109:     my $uhome=&homeserver($uname,$udom);
                   1110:     if (!$uhome) {
                   1111: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1112: 	return 'no_host';
                   1113:     }
                   1114:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1115:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1116: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1117:     }
1.456     albertel 1118:     return $answer;
1.169     harris41 1119: }
                   1120: 
1.1       albertel 1121: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1122: 
1.1       albertel 1123: sub authenticate {
1.1073    raeburn  1124:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1125:     $upass=&escape($upass);
                   1126:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1127:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1128:     my $newhome;
1.836     www      1129:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1130: # Maybe the machine was offline and only re-appeared again recently?
                   1131:         &reconlonc();
                   1132: # One more
1.952     raeburn  1133: 	$uhome=&homeserver($uname,$udom,1);
                   1134:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1135:             if (defined(&domain($udom,'primary'))) {
                   1136:                 $newhome=&domain($udom,'primary');
                   1137:             }
                   1138:             if ($newhome ne '') {
                   1139:                 $uhome = $newhome;
                   1140:             }
                   1141:         }
1.836     www      1142: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1143: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1144: 	    return 'no_host';
                   1145:         }
1.1       albertel 1146:     }
1.1073    raeburn  1147:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1148:     if ($answer eq 'authorized') {
1.952     raeburn  1149:         if ($newhome) {
                   1150:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1151:             return 'no_account_on_host'; 
                   1152:         } else {
                   1153:             &logthis("User $uname at $udom authorized by $uhome");
                   1154:             return $uhome;
                   1155:         }
1.471     albertel 1156:     }
                   1157:     if ($answer eq 'non_authorized') {
                   1158: 	&logthis("User $uname at $udom rejected by $uhome");
                   1159: 	return 'no_host'; 
1.9       www      1160:     }
1.471     albertel 1161:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1162:     return 'no_host';
                   1163: }
                   1164: 
1.1073    raeburn  1165: sub can_host_session {
1.1074    raeburn  1166:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1167:     my $canhost = 1;
1.1074    raeburn  1168:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn  1169:     if (ref($remotesessions) eq 'HASH') {
                   1170:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1171:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1172:                 $canhost = 0;
                   1173:             } else {
                   1174:                 $canhost = 1;
                   1175:             }
                   1176:         }
                   1177:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1178:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1179:                 $canhost = 1;
                   1180:             } else {
                   1181:                 $canhost = 0;
                   1182:             }
                   1183:         }
                   1184:         if ($canhost) {
                   1185:             if ($remotesessions->{'version'} ne '') {
                   1186:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1187:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1188:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1189:                         my $major = $1;
                   1190:                         my $minor = $2;
                   1191:                         if (($major < $reqmajor ) ||
                   1192:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1193:                             $canhost = 0;
                   1194:                         }
                   1195:                     } else {
                   1196:                         $canhost = 0;
                   1197:                     }
                   1198:                 }
                   1199:             }
                   1200:         }
                   1201:     }
                   1202:     if ($canhost) {
                   1203:         if (ref($hostedsessions) eq 'HASH') {
1.1120    raeburn  1204:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1205:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073    raeburn  1206:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1207:                 if (($uint_dom ne '') && 
                   1208:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1209:                     $canhost = 0;
                   1210:                 } else {
                   1211:                     $canhost = 1;
                   1212:                 }
                   1213:             }
                   1214:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1215:                 if (($uint_dom ne '') && 
                   1216:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1217:                     $canhost = 1;
                   1218:                 } else {
                   1219:                     $canhost = 0;
                   1220:                 }
                   1221:             }
                   1222:         }
                   1223:     }
                   1224:     return $canhost;
                   1225: }
                   1226: 
1.1083    raeburn  1227: sub spare_can_host {
                   1228:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1229:     my $canhost=1;
1.1279    raeburn  1230:     my $try_server_hostname = &hostname($try_server);
                   1231:     my $serverhomeID = &get_server_homeID($try_server_hostname);
                   1232:     my $serverhomedom = &host_domain($serverhomeID);
                   1233:     my %defdomdefaults = &get_domain_defaults($serverhomedom);
                   1234:     if (ref($defdomdefaults{'offloadnow'}) eq 'HASH') {
                   1235:         if ($defdomdefaults{'offloadnow'}{$try_server}) {
                   1236:             $canhost = 0;
                   1237:         }
                   1238:     }
                   1239:     if (($canhost) && ($uint_dom)) {
                   1240:         my @intdoms;
                   1241:         my $internet_names = &get_internet_names($try_server);
                   1242:         if (ref($internet_names) eq 'ARRAY') {
                   1243:             @intdoms = @{$internet_names};
                   1244:         }
                   1245:         unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1246:             my $remoterev = &get_server_loncaparev(undef,$try_server);
                   1247:             $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1248:                                          $remotesessions,
                   1249:                                          $defdomdefaults{'hostedsessions'});
                   1250:         }
1.1083    raeburn  1251:     }
                   1252:     return $canhost;
                   1253: }
                   1254: 
1.1123    raeburn  1255: sub this_host_spares {
                   1256:     my ($dom) = @_;
1.1126    raeburn  1257:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1258:     my @hosts = &current_machine_ids();
                   1259:     foreach my $lonhost (@hosts) {
                   1260:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1261:             $dom_in_use = $dom;
                   1262:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1263:             last;
                   1264:         }
                   1265:     }
1.1126    raeburn  1266:     if ($dom_in_use ne '') {
                   1267:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1268:     }
                   1269:     if (ref($result) ne 'HASH') {
                   1270:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1271:         $dom_in_use = &host_domain($lonhost_in_use);
                   1272:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1273:         if (ref($result) ne 'HASH') {
                   1274:             $result = \%spareid;
                   1275:         }
                   1276:     }
                   1277:     return $result;
                   1278: }
                   1279: 
                   1280: sub spares_for_offload  {
                   1281:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1282:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1283:     if (defined($cached)) {
                   1284:         return $result;
                   1285:     } else {
1.1126    raeburn  1286:         my $cachetime = 60*60*24;
                   1287:         my %domconfig =
                   1288:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
                   1289:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1290:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1291:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1292:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1293:                 }
                   1294:             }
                   1295:         }
                   1296:     }
1.1126    raeburn  1297:     return;
1.1123    raeburn  1298: }
                   1299: 
1.1129    raeburn  1300: sub get_lonbalancer_config {
                   1301:     my ($servers) = @_;
                   1302:     my ($currbalancer,$currtargets);
                   1303:     if (ref($servers) eq 'HASH') {
                   1304:         foreach my $server (keys(%{$servers})) {
                   1305:             my %what = (
                   1306:                          spareid => 1,
                   1307:                          perlvar => 1,
                   1308:                        );
                   1309:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1310:             if ($result eq 'ok') {
                   1311:                 if (ref($returnhash) eq 'HASH') {
                   1312:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1313:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1314:                             $currbalancer = $server;
                   1315:                             $currtargets = {};
                   1316:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1317:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1318:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1319:                                 }
                   1320:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1321:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1322:                                 }
                   1323:                             }
                   1324:                             last;
                   1325:                         }
                   1326:                     }
                   1327:                 }
                   1328:             }
                   1329:         }
                   1330:     }
                   1331:     return ($currbalancer,$currtargets);
                   1332: }
                   1333: 
                   1334: sub check_loadbalancing {
1.1331    raeburn  1335:     my ($uname,$udom,$caller) = @_;
1.1191    raeburn  1336:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
                   1337:         $rule_in_effect,$offloadto,$otherserver);
1.1129    raeburn  1338:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1339:     my @hosts = &current_machine_ids();
1.1129    raeburn  1340:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1341:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
                   1342:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                   1343:     my $serverhomedom = &host_domain($lonhost);
1.1307    raeburn  1344:     my $domneedscache;
1.1129    raeburn  1345:     my $cachetime = 60*60*24;
                   1346: 
                   1347:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1348:         $dom_in_use = $udom;
                   1349:         $homeintdom = 1;
                   1350:     } else {
                   1351:         $dom_in_use = $serverhomedom;
                   1352:     }
                   1353:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1354:     unless (defined($cached)) {
                   1355:         my %domconfig =
                   1356:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
                   1357:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1358:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1307    raeburn  1359:         } else {
                   1360:             $domneedscache = $dom_in_use;
1.1129    raeburn  1361:         }
                   1362:     }
                   1363:     if (ref($result) eq 'HASH') {
1.1191    raeburn  1364:         ($is_balancer,$currtargets,$currrules) = 
                   1365:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1366:         if ($is_balancer) {
                   1367:             if (ref($currrules) eq 'HASH') {
                   1368:                 if ($homeintdom) {
                   1369:                     if ($uname ne '') {
                   1370:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1371:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1372:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1373:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1374:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1375:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1376:                             }
                   1377:                         }
                   1378:                         if ($rule_in_effect eq '') {
                   1379:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1380:                             if ($userenv{'inststatus'} ne '') {
                   1381:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1382:                                 my ($othertitle,$usertypes,$types) =
                   1383:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1384:                                 if (ref($types) eq 'ARRAY') {
                   1385:                                     foreach my $type (@{$types}) {
                   1386:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1387:                                             if (exists($currrules->{$type})) {
                   1388:                                                 $rule_in_effect = $currrules->{$type};
                   1389:                                             }
                   1390:                                         }
                   1391:                                     }
                   1392:                                 }
                   1393:                             } else {
                   1394:                                 if (exists($currrules->{'default'})) {
                   1395:                                     $rule_in_effect = $currrules->{'default'};
                   1396:                                 }
                   1397:                             }
                   1398:                         }
                   1399:                     } else {
                   1400:                         if (exists($currrules->{'default'})) {
                   1401:                             $rule_in_effect = $currrules->{'default'};
                   1402:                         }
                   1403:                     }
                   1404:                 } else {
                   1405:                     if ($currrules->{'_LC_external'} ne '') {
                   1406:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1407:                     }
                   1408:                 }
                   1409:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1410:                                                        $uname,$udom);
                   1411:             }
                   1412:         }
                   1413:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
1.1239    raeburn  1414:         ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
1.1129    raeburn  1415:         unless (defined($cached)) {
                   1416:             my %domconfig =
                   1417:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
                   1418:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1307    raeburn  1419:                 $result = &do_cache_new('loadbalancing',$serverhomedom,$domconfig{'loadbalancing'},$cachetime);
                   1420:             } else {
                   1421:                 $domneedscache = $serverhomedom;
1.1129    raeburn  1422:             }
                   1423:         }
                   1424:         if (ref($result) eq 'HASH') {
1.1191    raeburn  1425:             ($is_balancer,$currtargets,$currrules) = 
                   1426:                 &check_balancer_result($result,@hosts);
                   1427:             if ($is_balancer) {
1.1129    raeburn  1428:                 if (ref($currrules) eq 'HASH') {
                   1429:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1430:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1431:                     }
                   1432:                 }
                   1433:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1434:                                                        $uname,$udom);
                   1435:             }
                   1436:         } else {
                   1437:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1438:                 $is_balancer = 1;
                   1439:                 $offloadto = &this_host_spares($dom_in_use);
                   1440:             }
1.1307    raeburn  1441:             unless (defined($cached)) {
                   1442:                 $domneedscache = $serverhomedom;
                   1443:             }
1.1129    raeburn  1444:         }
                   1445:     } else {
                   1446:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1447:             $is_balancer = 1;
                   1448:             $offloadto = &this_host_spares($dom_in_use);
                   1449:         }
1.1307    raeburn  1450:         unless (defined($cached)) {
                   1451:             $domneedscache = $serverhomedom;
                   1452:         }
                   1453:     }
                   1454:     if ($domneedscache) {
                   1455:         &do_cache_new('loadbalancing',$domneedscache,$is_balancer,$cachetime);
1.1129    raeburn  1456:     }
1.1176    raeburn  1457:     if ($is_balancer) {
                   1458:         my $lowest_load = 30000;
                   1459:         if (ref($offloadto) eq 'HASH') {
                   1460:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1461:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1462:                     ($otherserver,$lowest_load) =
                   1463:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1464:                 }
1.1129    raeburn  1465:             }
1.1176    raeburn  1466:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1467: 
1.1176    raeburn  1468:             if (!$found_server) {
                   1469:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1470:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1471:                         ($otherserver,$lowest_load) =
                   1472:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1473:                     }
                   1474:                 }
                   1475:             }
                   1476:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1477:             if (@{$offloadto} == 1) {
                   1478:                 $otherserver = $offloadto->[0];
                   1479:             } elsif (@{$offloadto} > 1) {
                   1480:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1481:                     ($otherserver,$lowest_load) =
                   1482:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1483:                 }
                   1484:             }
                   1485:         }
1.1331    raeburn  1486:         unless ($caller eq 'login') {
                   1487:             if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1488:                 $is_balancer = 0;
                   1489:                 if ($uname ne '' && $udom ne '') {
                   1490:                     if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
1.1176    raeburn  1491:                     
1.1331    raeburn  1492:                         &appenv({'user.loadbalexempt'     => $lonhost,  
                   1493:                                  'user.loadbalcheck.time' => time});
                   1494:                     }
1.1176    raeburn  1495:                 }
1.1129    raeburn  1496:             }
                   1497:         }
                   1498:     }
                   1499:     return ($is_balancer,$otherserver);
                   1500: }
                   1501: 
1.1191    raeburn  1502: sub check_balancer_result {
                   1503:     my ($result,@hosts) = @_;
                   1504:     my ($is_balancer,$currtargets,$currrules);
                   1505:     if (ref($result) eq 'HASH') {
                   1506:         if ($result->{'lonhost'} ne '') {
                   1507:             my $currbalancer = $result->{'lonhost'};
                   1508:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1509:                 $is_balancer = 1;
                   1510:                 $currtargets = $result->{'targets'};
                   1511:                 $currrules = $result->{'rules'};
                   1512:             }
                   1513:         } else {
                   1514:             foreach my $key (keys(%{$result})) {
                   1515:                 if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1516:                     (ref($result->{$key}) eq 'HASH')) {
                   1517:                     $is_balancer = 1;
                   1518:                     $currrules = $result->{$key}{'rules'};
                   1519:                     $currtargets = $result->{$key}{'targets'};
                   1520:                     last;
                   1521:                 }
                   1522:             }
                   1523:         }
                   1524:     }
                   1525:     return ($is_balancer,$currtargets,$currrules);
                   1526: }
                   1527: 
1.1129    raeburn  1528: sub get_loadbalancer_targets {
                   1529:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1530:     my $offloadto;
1.1175    raeburn  1531:     if ($rule_in_effect eq 'none') {
                   1532:         return [$perlvar{'lonHostID'}];
                   1533:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1534:         $offloadto = $currtargets;
                   1535:     } else {
                   1536:         if ($rule_in_effect eq 'homeserver') {
                   1537:             my $homeserver = &homeserver($uname,$udom);
                   1538:             if ($homeserver ne 'no_host') {
                   1539:                 $offloadto = [$homeserver];
                   1540:             }
                   1541:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1542:             my %domconfig =
                   1543:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1544:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1545:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1546:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1547:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1548:                     }
                   1549:                 }
                   1550:             } else {
1.1178    raeburn  1551:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1552:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1553:                 if (&hostname($remotebalancer) ne '') {
                   1554:                     $offloadto = [$remotebalancer];
                   1555:                 }
                   1556:             }
                   1557:         } elsif (&hostname($rule_in_effect) ne '') {
                   1558:             $offloadto = [$rule_in_effect];
                   1559:         }
                   1560:     }
                   1561:     return $offloadto;
                   1562: }
                   1563: 
1.1127    raeburn  1564: sub internet_dom_servers {
                   1565:     my ($dom) = @_;
                   1566:     my (%uniqservers,%servers);
                   1567:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1568:     my @machinedoms = &machine_domains($primaryserver);
                   1569:     foreach my $mdom (@machinedoms) {
                   1570:         my %currservers = %servers;
                   1571:         my %server = &get_servers($mdom);
                   1572:         %servers = (%currservers,%server);
                   1573:     }
                   1574:     my %by_hostname;
                   1575:     foreach my $id (keys(%servers)) {
                   1576:         push(@{$by_hostname{$servers{$id}}},$id);
                   1577:     }
                   1578:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1579:         if (@{$by_hostname{$hostname}} > 1) {
                   1580:             my $match = 0;
                   1581:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1582:                 if (&host_domain($id) eq $dom) {
                   1583:                     $uniqservers{$id} = $hostname;
                   1584:                     $match = 1;
                   1585:                 }
                   1586:             }
                   1587:             unless ($match) {
                   1588:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1589:             }
                   1590:         } else {
                   1591:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1592:         }
                   1593:     }
                   1594:     return %uniqservers;
                   1595: }
                   1596: 
1.1       albertel 1597: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1598: 
1.599     albertel 1599: my %homecache;
1.1       albertel 1600: sub homeserver {
1.230     stredwic 1601:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1602:     my $index="$uname:$udom";
1.426     albertel 1603: 
1.599     albertel 1604:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1605: 
                   1606:     my %servers = &get_servers($udom,'library');
                   1607:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1608:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1609: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1610: 
                   1611: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1612: 	if ($answer eq 'found') {
                   1613: 	    delete($badServerCache{$tryserver}); 
                   1614: 	    return $homecache{$index}=$tryserver;
                   1615: 	} elsif ($answer eq 'no_host') {
                   1616: 	    $badServerCache{$tryserver}=1;
                   1617: 	}
1.1       albertel 1618:     }    
                   1619:     return 'no_host';
1.70      www      1620: }
                   1621: 
1.1300    raeburn  1622: # ----- Find the usernames behind a list of student/employee IDs or clicker IDs
1.70      www      1623: 
                   1624: sub idget {
1.1300    raeburn  1625:     my ($udom,$idsref,$namespace)=@_;
1.70      www      1626:     my %returnhash=();
1.1300    raeburn  1627:     my @ids=(); 
                   1628:     if (ref($idsref) eq 'ARRAY') {
                   1629:         @ids = @{$idsref};
                   1630:     } else {
                   1631:         return %returnhash; 
                   1632:     }
                   1633:     if ($namespace eq '') {
                   1634:         $namespace = 'ids';
                   1635:     }
1.70      www      1636:     
1.841     albertel 1637:     my %servers = &get_servers($udom,'library');
                   1638:     foreach my $tryserver (keys(%servers)) {
1.1299    raeburn  1639: 	my $idlist=join('&', map { &escape($_); } @ids);
1.1300    raeburn  1640: 	if ($namespace eq 'ids') {
                   1641: 	    $idlist=~tr/A-Z/a-z/;
                   1642: 	}
                   1643: 	my $reply;
                   1644: 	if ($namespace eq 'ids') {
                   1645: 	    $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1646: 	} else {
                   1647: 	    $reply=&reply("getdom:$udom:$namespace:$idlist",$tryserver);
                   1648: 	}
1.841     albertel 1649: 	my @answer=();
                   1650: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1651: 	    @answer=split(/\&/,$reply);
                   1652: 	}                    ;
                   1653: 	my $i;
                   1654: 	for ($i=0;$i<=$#ids;$i++) {
                   1655: 	    if ($answer[$i]) {
1.1299    raeburn  1656: 		$returnhash{$ids[$i]}=&unescape($answer[$i]);
1.1300    raeburn  1657: 	    }
1.841     albertel 1658: 	}
1.1300    raeburn  1659:     }
1.70      www      1660:     return %returnhash;
                   1661: }
                   1662: 
                   1663: # ------------------------------------- Find the IDs behind a list of usernames
                   1664: 
                   1665: sub idrget {
                   1666:     my ($udom,@unames)=@_;
                   1667:     my %returnhash=();
1.800     albertel 1668:     foreach my $uname (@unames) {
                   1669:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1670:     }
1.70      www      1671:     return %returnhash;
                   1672: }
                   1673: 
1.1300    raeburn  1674: # Store away a list of names and associated student/employee IDs or clicker IDs
1.70      www      1675: 
                   1676: sub idput {
1.1300    raeburn  1677:     my ($udom,$idsref,$uhom,$namespace)=@_;
1.70      www      1678:     my %servers=();
1.1300    raeburn  1679:     my %ids=();
                   1680:     my %byid = ();
                   1681:     if (ref($idsref) eq 'HASH') {
                   1682:         %ids=%{$idsref};
                   1683:     }
                   1684:     if ($namespace eq '') {
                   1685:         $namespace = 'ids'; 
                   1686:     }
1.800     albertel 1687:     foreach my $uname (keys(%ids)) {
                   1688: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
1.1300    raeburn  1689:         if ($uhom eq '') {
                   1690:             $uhom=&homeserver($uname,$udom);
                   1691:         }
1.70      www      1692:         if ($uhom ne 'no_host') {
1.800     albertel 1693:             my $esc_unam=&escape($uname);
1.1300    raeburn  1694:             if ($namespace eq 'ids') {
                   1695:                 my $id=&escape($ids{$uname});
                   1696:                 $id=~tr/A-Z/a-z/;
                   1697:                 my $esc_unam=&escape($uname);
                   1698:                 $servers{$uhom}.=$id.'='.$esc_unam.'&';
1.70      www      1699:             } else {
1.1300    raeburn  1700:                 my @currids = split(/,/,$ids{$uname});
                   1701:                 foreach my $id (@currids) {
                   1702:                     $byid{$uhom}{$id} .= $uname.',';
                   1703:                 }
                   1704:             }
                   1705:         }
                   1706:     }
                   1707:     if ($namespace eq 'clickers') {
                   1708:         foreach my $server (keys(%byid)) {
                   1709:             if (ref($byid{$server}) eq 'HASH') {
                   1710:                 foreach my $id (keys(%{$byid{$server}})) {
                   1711:                     $byid{$server} =~ s/,$//;
                   1712:                     $servers{$uhom}.=&escape($id).'='.&escape($byid{$server}).'&'; 
                   1713:                 }
1.70      www      1714:             }
                   1715:         }
1.191     harris41 1716:     }
1.800     albertel 1717:     foreach my $server (keys(%servers)) {
1.1300    raeburn  1718:         $servers{$server} =~ s/\&$//;
                   1719:         if ($namespace eq 'ids') {     
                   1720:             &critical('idput:'.$udom.':'.$servers{$server},$server);
                   1721:         } else {
                   1722:             &critical('updateclickers:'.$udom.':add:'.$servers{$server},$server);
                   1723:         }
1.191     harris41 1724:     }
1.344     www      1725: }
                   1726: 
1.1300    raeburn  1727: # ------------- Delete unwanted student/employee IDs or clicker IDs from domain
1.1231    raeburn  1728: 
                   1729: sub iddel {
1.1300    raeburn  1730:     my ($udom,$idshashref,$uhome,$namespace)=@_;
1.1231    raeburn  1731:     my %result=();
1.1300    raeburn  1732:     my %ids=();
                   1733:     my %byid = ();
                   1734:     if (ref($idshashref) eq 'HASH') {
                   1735:         %ids=%{$idshashref};
                   1736:     } else {
1.1231    raeburn  1737:         return %result;
                   1738:     }
1.1300    raeburn  1739:     if ($namespace eq '') {
                   1740:         $namespace = 'ids';
                   1741:     }
1.1231    raeburn  1742:     my %servers=();
1.1300    raeburn  1743:     while (my ($id,$unamestr) = each(%ids)) {
                   1744:         if ($namespace eq 'ids') {
                   1745:             my $uhom = $uhome;
                   1746:             if ($uhom eq '') { 
                   1747:                 $uhom=&homeserver($unamestr,$udom);
                   1748:             }
                   1749:             if ($uhom ne 'no_host') {
                   1750:                 $servers{$uhom}.='&'.&escape($id);
                   1751:             }
                   1752:          } else {
                   1753:             my @curritems = split(/,/,$ids{$id});
                   1754:             foreach my $uname (@curritems) {
                   1755:                 my $uhom = $uhome;
                   1756:                 if ($uhom eq '') {
                   1757:                     $uhom=&homeserver($uname,$udom);
                   1758:                 }
                   1759:                 if ($uhom ne 'no_host') { 
                   1760:                     $byid{$uhom}{$id} .= $uname.',';
                   1761:                 }
                   1762:             }
1.1231    raeburn  1763:         }
1.1300    raeburn  1764:     }
                   1765:     if ($namespace eq 'clickers') {
                   1766:         foreach my $server (keys(%byid)) {
                   1767:             if (ref($byid{$server}) eq 'HASH') {
                   1768:                 foreach my $id (keys(%{$byid{$server}})) {
                   1769:                     $byid{$server}{$id} =~ s/,$//;
                   1770:                     $servers{$server}.=&escape($id).'='.&escape($byid{$server}{$id}).'&';
                   1771:                 }
1.1231    raeburn  1772:             }
                   1773:         }
                   1774:     }
                   1775:     foreach my $server (keys(%servers)) {
1.1300    raeburn  1776:         $servers{$server} =~ s/\&$//;
                   1777:         if ($namespace eq 'ids') {
                   1778:             $result{$server} = &critical('iddel:'.$udom.':'.$servers{$server},$uhome);
                   1779:         } elsif ($namespace eq 'clickers') {
                   1780:             $result{$server} = &critical('updateclickers:'.$udom.':del:'.$servers{$server},$server);
                   1781:         }
1.1231    raeburn  1782:     }
                   1783:     return %result;
                   1784: }
                   1785: 
1.1300    raeburn  1786: # ----- Update clicker ID-to-username look-ups in clickers.db on library server 
                   1787: 
                   1788: sub updateclickers {
                   1789:     my ($udom,$action,$idshashref,$uhome,$critical) = @_;
                   1790:     my %clickers;
                   1791:     if (ref($idshashref) eq 'HASH') {
                   1792:         %clickers=%{$idshashref};
                   1793:     } else {
                   1794:         return;
                   1795:     }
                   1796:     my $items='';
                   1797:     foreach my $item (keys(%clickers)) {
                   1798:         $items.=&escape($item).'='.&escape($clickers{$item}).'&';
                   1799:     }
                   1800:     $items=~s/\&$//;
                   1801:     my $request = "updateclickers:$udom:$action:$items";
                   1802:     if ($critical) {
                   1803:         return &critical($request,$uhome);
                   1804:     } else {
                   1805:         return &reply($request,$uhome);
                   1806:     }
                   1807: }
                   1808: 
1.1023    raeburn  1809: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  1810: sub dump_dom {
1.1165    droeschl 1811:     my ($namespace, $udom, $regexp) = @_;
                   1812: 
                   1813:     $udom ||= $env{'user.domain'};
                   1814: 
                   1815:     return () unless $udom;
                   1816: 
                   1817:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  1818: }
                   1819: 
1.1023    raeburn  1820: # ------------------------------------------ get items from domain db files   
1.806     raeburn  1821: 
                   1822: sub get_dom {
1.860     raeburn  1823:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.1267    raeburn  1824:     return if ($udom eq 'public');
1.806     raeburn  1825:     my $items='';
                   1826:     foreach my $item (@$storearr) {
                   1827:         $items.=&escape($item).'&';
                   1828:     }
                   1829:     $items=~s/\&$//;
1.860     raeburn  1830:     if (!$udom) {
                   1831:         $udom=$env{'user.domain'};
1.1267    raeburn  1832:         return if ($udom eq 'public');
1.860     raeburn  1833:         if (defined(&domain($udom,'primary'))) {
                   1834:             $uhome=&domain($udom,'primary');
                   1835:         } else {
1.874     albertel 1836:             undef($uhome);
1.860     raeburn  1837:         }
                   1838:     } else {
                   1839:         if (!$uhome) {
                   1840:             if (defined(&domain($udom,'primary'))) {
                   1841:                 $uhome=&domain($udom,'primary');
                   1842:             }
                   1843:         }
                   1844:     }
                   1845:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.1344    raeburn  1846:         my $rep;
                   1847:         if ($namespace =~ /^enc/) {
                   1848:             $rep=&reply("encrypt:egetdom:$udom:$namespace:$items",$uhome);
                   1849:         } else {
                   1850:             $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
                   1851:         }
1.866     raeburn  1852:         my %returnhash;
1.875     albertel 1853:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  1854:             return %returnhash;
                   1855:         }
1.806     raeburn  1856:         my @pairs=split(/\&/,$rep);
                   1857:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   1858:             return @pairs;
                   1859:         }
                   1860:         my $i=0;
                   1861:         foreach my $item (@$storearr) {
                   1862:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   1863:             $i++;
                   1864:         }
                   1865:         return %returnhash;
                   1866:     } else {
1.880     banghart 1867:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  1868:     }
                   1869: }
                   1870: 
                   1871: # -------------------------------------------- put items in domain db files 
                   1872: 
                   1873: sub put_dom {
1.860     raeburn  1874:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   1875:     if (!$udom) {
                   1876:         $udom=$env{'user.domain'};
                   1877:         if (defined(&domain($udom,'primary'))) {
                   1878:             $uhome=&domain($udom,'primary');
                   1879:         } else {
1.874     albertel 1880:             undef($uhome);
1.860     raeburn  1881:         }
                   1882:     } else {
                   1883:         if (!$uhome) {
                   1884:             if (defined(&domain($udom,'primary'))) {
                   1885:                 $uhome=&domain($udom,'primary');
                   1886:             }
                   1887:         }
                   1888:     } 
                   1889:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1890:         my $items='';
                   1891:         foreach my $item (keys(%$storehash)) {
                   1892:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   1893:         }
                   1894:         $items=~s/\&$//;
1.1344    raeburn  1895:         if ($namespace =~ /^enc/) {
                   1896:             return &reply("encrypt:putdom:$udom:$namespace:$items",$uhome);
                   1897:         } else {
                   1898:             return &reply("putdom:$udom:$namespace:$items",$uhome);
                   1899:         }
1.806     raeburn  1900:     } else {
1.860     raeburn  1901:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  1902:     }
                   1903: }
                   1904: 
1.1023    raeburn  1905: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  1906: sub newput_dom {
1.1023    raeburn  1907:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  1908:     my $result;
                   1909:     if (!$udom) {
                   1910:         $udom=$env{'user.domain'};
                   1911:     }
1.1023    raeburn  1912:     if ($udom) {
                   1913:         my $uname = &get_domainconfiguser($udom);
                   1914:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  1915:     }
                   1916:     return $result;
                   1917: }
                   1918: 
1.1023    raeburn  1919: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  1920: sub del_dom {
1.1023    raeburn  1921:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  1922:     if (ref($storearr) eq 'ARRAY') {
                   1923:         if (!$udom) {
                   1924:             $udom=$env{'user.domain'};
                   1925:         }
1.1023    raeburn  1926:         if ($udom) {
                   1927:             my $uname = &get_domainconfiguser($udom); 
                   1928:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  1929:         }
                   1930:     }
                   1931: }
                   1932: 
1.1023    raeburn  1933: # ----------------------------------construct domainconfig user for a domain 
                   1934: sub get_domainconfiguser {
                   1935:     my ($udom) = @_;
                   1936:     return $udom.'-domainconfig';
                   1937: }
                   1938: 
1.837     raeburn  1939: sub retrieve_inst_usertypes {
                   1940:     my ($udom) = @_;
                   1941:     my (%returnhash,@order);
1.989     raeburn  1942:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   1943:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   1944:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
1.1256    raeburn  1945:         return ($domdefs{'inststatustypes'},$domdefs{'inststatusorder'});
1.989     raeburn  1946:     } else {
                   1947:         if (defined(&domain($udom,'primary'))) {
                   1948:             my $uhome=&domain($udom,'primary');
                   1949:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   1950:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
1.1256    raeburn  1951:                 &logthis("retrieve_inst_usertypes failed - $rep returned from $uhome in domain: $udom");
1.989     raeburn  1952:                 return (\%returnhash,\@order);
                   1953:             }
                   1954:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   1955:             my @pairs=split(/\&/,$hashitems);
                   1956:             foreach my $item (@pairs) {
                   1957:                 my ($key,$value)=split(/=/,$item,2);
                   1958:                 $key = &unescape($key);
                   1959:                 next if ($key =~ /^error: 2 /);
                   1960:                 $returnhash{$key}=&thaw_unescape($value);
                   1961:             }
                   1962:             my @esc_order = split(/\&/,$orderitems);
                   1963:             foreach my $item (@esc_order) {
                   1964:                 push(@order,&unescape($item));
                   1965:             }
                   1966:         } else {
1.1256    raeburn  1967:             &logthis("retrieve_inst_usertypes failed - no primary domain server for $udom");
1.837     raeburn  1968:         }
1.1256    raeburn  1969:         return (\%returnhash,\@order);
1.837     raeburn  1970:     }
                   1971: }
                   1972: 
1.868     raeburn  1973: sub is_domainimage {
                   1974:     my ($url) = @_;
1.1306    raeburn  1975:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+[^/]-) {
1.868     raeburn  1976:         if (&domain($1) ne '') {
                   1977:             return '1';
                   1978:         }
                   1979:     }
                   1980:     return;
                   1981: }
                   1982: 
1.899     raeburn  1983: sub inst_directory_query {
                   1984:     my ($srch) = @_;
                   1985:     my $udom = $srch->{'srchdomain'};
                   1986:     my %results;
                   1987:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  1988:     my $outcome;
1.899     raeburn  1989:     if ($homeserver ne '') {
1.904     albertel 1990: 	my $queryid=&reply("querysend:instdirsearch:".
                   1991: 			   &escape($srch->{'srchby'}).':'.
                   1992: 			   &escape($srch->{'srchterm'}).':'.
                   1993: 			   &escape($srch->{'srchtype'}),$homeserver);
                   1994: 	my $host=&hostname($homeserver);
                   1995: 	if ($queryid !~/^\Q$host\E\_/) {
1.1343    raeburn  1996: 	    &logthis('institutional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.' in domain '.$udom);
1.904     albertel 1997: 	    return;
                   1998: 	}
                   1999: 	my $response = &get_query_reply($queryid);
                   2000: 	my $maxtries = 5;
                   2001: 	my $tries = 1;
                   2002: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2003: 	    $response = &get_query_reply($queryid);
                   2004: 	    $tries ++;
                   2005: 	}
                   2006: 
                   2007:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  2008:             if ($response eq 'unavailable') {
                   2009:                 $outcome = $response;
                   2010:             } else {
                   2011:                 $outcome = 'ok';
                   2012:                 my @matches = split(/\n/,$response);
                   2013:                 foreach my $match (@matches) {
                   2014:                     my ($key,$value) = split(/=/,$match);
                   2015:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   2016:                 }
1.899     raeburn  2017:             }
                   2018:         }
                   2019:     }
1.909     raeburn  2020:     return ($outcome,%results);
1.899     raeburn  2021: }
                   2022: 
                   2023: sub usersearch {
                   2024:     my ($srch) = @_;
                   2025:     my $dom = $srch->{'srchdomain'};
                   2026:     my %results;
                   2027:     my %libserv = &all_library();
                   2028:     my $query = 'usersearch';
                   2029:     foreach my $tryserver (keys(%libserv)) {
                   2030:         if (&host_domain($tryserver) eq $dom) {
                   2031:             my $host=&hostname($tryserver);
                   2032:             my $queryid=
1.911     raeburn  2033:                 &reply("querysend:".&escape($query).':'.
                   2034:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  2035:                        &escape($srch->{'srchtype'}).':'.
                   2036:                        &escape($srch->{'srchterm'}),$tryserver);
                   2037:             if ($queryid !~/^\Q$host\E\_/) {
                   2038:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  2039:                 next;
1.899     raeburn  2040:             }
                   2041:             my $reply = &get_query_reply($queryid);
                   2042:             my $maxtries = 1;
                   2043:             my $tries = 1;
                   2044:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   2045:                 $reply = &get_query_reply($queryid);
                   2046:                 $tries ++;
                   2047:             }
                   2048:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   2049:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   2050:             } else {
1.911     raeburn  2051:                 my @matches;
                   2052:                 if ($reply =~ /\n/) {
                   2053:                     @matches = split(/\n/,$reply);
                   2054:                 } else {
                   2055:                     @matches = split(/\&/,$reply);
                   2056:                 }
1.899     raeburn  2057:                 foreach my $match (@matches) {
                   2058:                     my ($uname,$udom,%userhash);
1.911     raeburn  2059:                     foreach my $entry (split(/:/,$match)) {
                   2060:                         my ($key,$value) =
                   2061:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  2062:                         $userhash{$key} = $value;
                   2063:                         if ($key eq 'username') {
                   2064:                             $uname = $value;
                   2065:                         } elsif ($key eq 'domain') {
                   2066:                             $udom = $value;
1.911     raeburn  2067:                         }
1.899     raeburn  2068:                     }
                   2069:                     $results{$uname.':'.$udom} = \%userhash;
                   2070:                 }
                   2071:             }
                   2072:         }
                   2073:     }
                   2074:     return %results;
                   2075: }
                   2076: 
1.912     raeburn  2077: sub get_instuser {
                   2078:     my ($udom,$uname,$id) = @_;
                   2079:     my $homeserver = &domain($udom,'primary');
                   2080:     my ($outcome,%results);
                   2081:     if ($homeserver ne '') {
                   2082:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   2083:                            &escape($id).':'.&escape($udom),$homeserver);
                   2084:         my $host=&hostname($homeserver);
                   2085:         if ($queryid !~/^\Q$host\E\_/) {
                   2086:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   2087:             return;
                   2088:         }
                   2089:         my $response = &get_query_reply($queryid);
                   2090:         my $maxtries = 5;
                   2091:         my $tries = 1;
                   2092:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   2093:             $response = &get_query_reply($queryid);
                   2094:             $tries ++;
                   2095:         }
                   2096:         if (!&error($response) && $response ne 'refused') {
                   2097:             if ($response eq 'unavailable') {
                   2098:                 $outcome = $response;
                   2099:             } else {
                   2100:                 $outcome = 'ok';
                   2101:                 my @matches = split(/\n/,$response);
                   2102:                 foreach my $match (@matches) {
                   2103:                     my ($key,$value) = split(/=/,$match);
                   2104:                     $results{&unescape($key)} = &thaw_unescape($value);
                   2105:                 }
                   2106:             }
                   2107:         }
                   2108:     }
                   2109:     my %userinfo;
                   2110:     if (ref($results{$uname}) eq 'HASH') {
                   2111:         %userinfo = %{$results{$uname}};
                   2112:     } 
                   2113:     return ($outcome,%userinfo);
                   2114: }
                   2115: 
1.1290    raeburn  2116: sub get_multiple_instusers {
                   2117:     my ($udom,$users,$caller) = @_;
                   2118:     my ($outcome,$results);
                   2119:     if (ref($users) eq 'HASH') {
                   2120:         my $count = keys(%{$users}); 
                   2121:         my $requested = &freeze_escape($users);
                   2122:         my $homeserver = &domain($udom,'primary');
                   2123:         if ($homeserver ne '') {
                   2124:             my $queryid=&reply('querysend:getmultinstusers:::'.$caller.'='.$requested,$homeserver);
                   2125:             my $host=&hostname($homeserver);
                   2126:             if ($queryid !~/^\Q$host\E\_/) {
                   2127:                 &logthis('get_multiple_instusers invalid queryid: '.$queryid.
                   2128:                          ' for host: '.$homeserver.'in domain '.$udom);
                   2129:                 return ($outcome,$results);
                   2130:             }
                   2131:             my $response = &get_query_reply($queryid);
                   2132:             my $maxtries = 5;
                   2133:             if ($count > 100) {
                   2134:                 $maxtries = 1+int($count/20);
                   2135:             }
                   2136:             my $tries = 1;
                   2137:             while (($response=~/^timeout/) && ($tries <= $maxtries)) {
                   2138:                 $response = &get_query_reply($queryid);
                   2139:                 $tries ++;
                   2140:             }
                   2141:             if ($response eq '') {
                   2142:                 $results = {};
                   2143:                 foreach my $key (keys(%{$users})) {
                   2144:                     my ($uname,$id);
                   2145:                     if ($caller eq 'id') {
                   2146:                         $id = $key;
                   2147:                     } else {
                   2148:                         $uname = $key;
                   2149:                     }
                   2150:                     my ($resp,%info) = &get_instuser($udom,$uname,$id);
1.1291    raeburn  2151:                     $outcome = $resp;
1.1290    raeburn  2152:                     if ($resp eq 'ok') {
                   2153:                         %{$results} = (%{$results}, %info);
                   2154:                     } else {
                   2155:                         last;
                   2156:                     }
                   2157:                 }
                   2158:             } elsif(!&error($response) && ($response ne 'refused')) {
                   2159:                 if (($response eq 'unavailable') || ($response eq 'invalid') || ($response eq 'timeout')) {
                   2160:                     $outcome = $response;
                   2161:                 } else {
1.1291    raeburn  2162:                     ($outcome,my $userdata) = split(/=/,$response,2);
1.1290    raeburn  2163:                     if ($outcome eq 'ok') {
                   2164:                         $results = &thaw_unescape($userdata); 
                   2165:                     }
                   2166:                 }
                   2167:             }
                   2168:         }
                   2169:     }
                   2170:     return ($outcome,$results);
                   2171: }
                   2172: 
1.912     raeburn  2173: sub inst_rulecheck {
1.923     raeburn  2174:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  2175:     my %returnhash;
                   2176:     if ($udom ne '') {
                   2177:         if (ref($rules) eq 'ARRAY') {
                   2178:             @{$rules} = map {&escape($_);} (@{$rules});
                   2179:             my $rulestr = join(':',@{$rules});
                   2180:             my $homeserver=&domain($udom,'primary');
                   2181:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2182:                 my $response;
                   2183:                 if ($item eq 'username') {                
                   2184:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   2185:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  2186:                                               $homeserver));
1.923     raeburn  2187:                 } elsif ($item eq 'id') {
                   2188:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   2189:                                               ':'.&escape($id).':'.$rulestr,
                   2190:                                               $homeserver));
1.945     raeburn  2191:                 } elsif ($item eq 'selfcreate') {
                   2192:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  2193:                                                &escape($udom).':'.&escape($uname).
                   2194:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  2195:                 }
1.912     raeburn  2196:                 if ($response ne 'refused') {
                   2197:                     my @pairs=split(/\&/,$response);
                   2198:                     foreach my $item (@pairs) {
                   2199:                         my ($key,$value)=split(/=/,$item,2);
                   2200:                         $key = &unescape($key);
                   2201:                         next if ($key =~ /^error: 2 /);
                   2202:                         $returnhash{$key}=&thaw_unescape($value);
                   2203:                     }
                   2204:                 }
                   2205:             }
                   2206:         }
                   2207:     }
                   2208:     return %returnhash;
                   2209: }
                   2210: 
                   2211: sub inst_userrules {
1.923     raeburn  2212:     my ($udom,$check) = @_;
1.912     raeburn  2213:     my (%ruleshash,@ruleorder);
                   2214:     if ($udom ne '') {
                   2215:         my $homeserver=&domain($udom,'primary');
                   2216:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  2217:             my $response;
                   2218:             if ($check eq 'id') {
                   2219:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  2220:                                  $homeserver);
1.943     raeburn  2221:             } elsif ($check eq 'email') {
                   2222:                 $response=&reply('instemailrules:'.&escape($udom),
                   2223:                                  $homeserver);
1.923     raeburn  2224:             } else {
                   2225:                 $response=&reply('instuserrules:'.&escape($udom),
                   2226:                                  $homeserver);
                   2227:             }
1.912     raeburn  2228:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  2229:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  2230:                 ($response ne 'no_such_host')) {
                   2231:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   2232:                 my @pairs=split(/\&/,$hashitems);
                   2233:                 foreach my $item (@pairs) {
                   2234:                     my ($key,$value)=split(/=/,$item,2);
                   2235:                     $key = &unescape($key);
                   2236:                     next if ($key =~ /^error: 2 /);
                   2237:                     $ruleshash{$key}=&thaw_unescape($value);
                   2238:                 }
                   2239:                 my @esc_order = split(/\&/,$orderitems);
                   2240:                 foreach my $item (@esc_order) {
                   2241:                     push(@ruleorder,&unescape($item));
                   2242:                 }
                   2243:             }
                   2244:         }
                   2245:     }
                   2246:     return (\%ruleshash,\@ruleorder);
                   2247: }
                   2248: 
1.976     raeburn  2249: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  2250: 
                   2251: sub get_domain_defaults {
1.1240    raeburn  2252:     my ($domain,$ignore_cache) = @_;
1.1242    raeburn  2253:     return if (($domain eq '') || ($domain eq 'public'));
1.943     raeburn  2254:     my $cachetime = 60*60*24;
1.1240    raeburn  2255:     unless ($ignore_cache) {
                   2256:         my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   2257:         if (defined($cached)) {
                   2258:             if (ref($result) eq 'HASH') {
                   2259:                 return %{$result};
                   2260:             }
1.943     raeburn  2261:         }
                   2262:     }
                   2263:     my %domdefaults;
                   2264:     my %domconfig =
1.989     raeburn  2265:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  2266:                                   'requestcourses','inststatus',
1.1183    raeburn  2267:                                   'coursedefaults','usersessions',
1.1258    raeburn  2268:                                   'requestauthor','selfenrollment',
1.1320    raeburn  2269:                                   'coursecategories','ssl','autoenroll',
1.1332    raeburn  2270:                                   'trust','helpsettings'],$domain);
1.1305    raeburn  2271:     my @coursetypes = ('official','unofficial','community','textbook','placement');
1.943     raeburn  2272:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   2273:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   2274:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   2275:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  2276:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  2277:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  2278:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.1340    raeburn  2279:         $domdefaults{'intauth_cost'} = $domconfig{'defaults'}{'intauth_cost'};
                   2280:         $domdefaults{'intauth_switch'} = $domconfig{'defaults'}{'intauth_switch'};
                   2281:         $domdefaults{'intauth_check'} = $domconfig{'defaults'}{'intauth_check'};
1.943     raeburn  2282:     } else {
                   2283:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   2284:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   2285:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   2286:     }
1.976     raeburn  2287:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   2288:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   2289:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   2290:         } else {
                   2291:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229    raeburn  2292:         }
1.1177    raeburn  2293:         my @usertools = ('aboutme','blog','webdav','portfolio');
1.976     raeburn  2294:         foreach my $item (@usertools) {
                   2295:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   2296:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   2297:             }
                   2298:         }
1.1229    raeburn  2299:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   2300:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
                   2301:         }
1.976     raeburn  2302:     }
1.985     raeburn  2303:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1305    raeburn  2304:         foreach my $item ('official','unofficial','community','textbook','placement') {
1.985     raeburn  2305:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2306:         }
                   2307:     }
1.1183    raeburn  2308:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2309:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2310:     }
1.989     raeburn  2311:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
1.1256    raeburn  2312:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
1.989     raeburn  2313:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2314:         }
                   2315:     }
1.1047    raeburn  2316:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1230    raeburn  2317:         $domdefaults{'canuse_pdfforms'} = $domconfig{'coursedefaults'}{'canuse_pdfforms'};
1.1277    raeburn  2318:         $domdefaults{'usejsme'} = $domconfig{'coursedefaults'}{'usejsme'};
                   2319:         $domdefaults{'uselcmath'} = $domconfig{'coursedefaults'}{'uselcmath'};
                   2320:         if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   2321:             $domdefaults{'postsubmit'} = $domconfig{'coursedefaults'}{'postsubmit'}{'client'};
                   2322:         }
1.1254    raeburn  2323:         foreach my $type (@coursetypes) {
                   2324:             if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2325:                 unless ($type eq 'community') {
                   2326:                     $domdefaults{$type.'credits'} = $domconfig{'coursedefaults'}{'coursecredits'}{$type};
                   2327:                 }
                   2328:             }
                   2329:             if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   2330:                 $domdefaults{$type.'quota'} = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   2331:             }
1.1277    raeburn  2332:             if ($domdefaults{'postsubmit'} eq 'on') {
                   2333:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   2334:                     $domdefaults{$type.'postsubtimeout'} = 
                   2335:                         $domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}{$type}; 
                   2336:                 }
                   2337:             }
1.1230    raeburn  2338:         }
1.1286    raeburn  2339:         if (ref($domconfig{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   2340:             if (ref($domconfig{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   2341:                 my @clonecodes = @{$domconfig{'coursedefaults'}{'canclone'}{'instcode'}};
                   2342:                 if (@clonecodes) {
                   2343:                     $domdefaults{'canclone'} = join('+',@clonecodes);
                   2344:                 }
                   2345:             }
                   2346:         } elsif ($domconfig{'coursedefaults'}{'canclone'}) {
                   2347:             $domdefaults{'canclone'}=$domconfig{'coursedefaults'}{'canclone'};
                   2348:         }
1.1047    raeburn  2349:     }
1.1073    raeburn  2350:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2351:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2352:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2353:         }
                   2354:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2355:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2356:         }
1.1279    raeburn  2357:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   2358:             $domdefaults{'offloadnow'} = $domconfig{'usersessions'}{'offloadnow'};
                   2359:         }
1.1073    raeburn  2360:     }
1.1254    raeburn  2361:     if (ref($domconfig{'selfenrollment'}) eq 'HASH') {
                   2362:         if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   2363:             my @settings = ('types','registered','enroll_dates','access_dates','section',
                   2364:                             'approval','limit');
                   2365:             foreach my $type (@coursetypes) {
                   2366:                 if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   2367:                     my @mgrdc = ();
                   2368:                     foreach my $item (@settings) {
                   2369:                         if ($domconfig{'selfenrollment'}{'admin'}{$type}{$item} eq '0') {
                   2370:                             push(@mgrdc,$item);
                   2371:                         }
                   2372:                     }
                   2373:                     if (@mgrdc) {
                   2374:                         $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   2375:                     }
                   2376:                 }
                   2377:             }
                   2378:         }
                   2379:         if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   2380:             foreach my $type (@coursetypes) {
                   2381:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   2382:                     foreach my $item (keys(%{$domconfig{'selfenrollment'}{'default'}{$type}})) {
                   2383:                         $domdefaults{$type.'selfenroll'.$item} = $domconfig{'selfenrollment'}{'default'}{$type}{$item};
                   2384:                     }
                   2385:                 }
                   2386:             }
                   2387:         }
                   2388:     }
1.1258    raeburn  2389:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   2390:         $domdefaults{'catauth'} = 'std';
                   2391:         $domdefaults{'catunauth'} = 'std';
                   2392:         if ($domconfig{'coursecategories'}{'auth'}) { 
                   2393:             $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   2394:         }
                   2395:         if ($domconfig{'coursecategories'}{'unauth'}) {
                   2396:             $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   2397:         }
                   2398:     }
1.1315    raeburn  2399:     if (ref($domconfig{'ssl'}) eq 'HASH') {
                   2400:         if (ref($domconfig{'ssl'}{'replication'}) eq 'HASH') {
                   2401:             $domdefaults{'replication'} = $domconfig{'ssl'}{'replication'};
                   2402:         }
1.1338    raeburn  2403:         if (ref($domconfig{'ssl'}{'connto'}) eq 'HASH') {
                   2404:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connto'};
                   2405:         }
                   2406:         if (ref($domconfig{'ssl'}{'connfrom'}) eq 'HASH') {
                   2407:             $domdefaults{'connect'} = $domconfig{'ssl'}{'connfrom'};
1.1315    raeburn  2408:         }
                   2409:     }
1.1320    raeburn  2410:     if (ref($domconfig{'trust'}) eq 'HASH') {
                   2411:         my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
                   2412:         foreach my $prefix (@prefixes) {
                   2413:             if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
                   2414:                 $domdefaults{'trust'.$prefix} = $domconfig{'trust'}{$prefix};
                   2415:             }
                   2416:         }
                   2417:     }
1.1314    raeburn  2418:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   2419:         $domdefaults{'autofailsafe'} = $domconfig{'autoenroll'}{'autofailsafe'};
                   2420:     }
1.1332    raeburn  2421:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   2422:         $domdefaults{'submitbugs'} = $domconfig{'helpsettings'}{'submitbugs'};
                   2423:         if (ref($domconfig{'helpsettings'}{'adhoc'}) eq 'HASH') {
                   2424:             $domdefaults{'adhocroles'} = $domconfig{'helpsettings'}{'adhoc'};
                   2425:         }
                   2426:     }
1.1219    raeburn  2427:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  2428:     return %domdefaults;
                   2429: }
                   2430: 
1.1311    raeburn  2431: sub course_portal_url {
                   2432:     my ($cnum,$cdom) = @_;
                   2433:     my $chome = &homeserver($cnum,$cdom);
                   2434:     my $hostname = &hostname($chome);
                   2435:     my $protocol = $protocol{$chome};
                   2436:     $protocol = 'http' if ($protocol ne 'https');
                   2437:     my %domdefaults = &get_domain_defaults($cdom);
                   2438:     my $firsturl;
                   2439:     if ($domdefaults{'portal_def'}) {
                   2440:         $firsturl = $domdefaults{'portal_def'};
                   2441:     } else {
                   2442:         $firsturl = $protocol.'://'.$hostname;
                   2443:     }
                   2444:     return $firsturl;
                   2445: }
                   2446: 
1.344     www      2447: # --------------------------------------------------- Assign a key to a student
                   2448: 
                   2449: sub assign_access_key {
1.364     www      2450: #
                   2451: # a valid key looks like uname:udom#comments
                   2452: # comments are being appended
                   2453: #
1.498     www      2454:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   2455:     $kdom=
1.620     albertel 2456:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      2457:     $knum=
1.620     albertel 2458:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      2459:     $cdom=
1.620     albertel 2460:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2461:     $cnum=
1.620     albertel 2462:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2463:     $udom=$env{'user.name'} unless (defined($udom));
                   2464:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      2465:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      2466:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 2467:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      2468:                                                   # assigned to this person
                   2469:                                                   # - this should not happen,
1.345     www      2470:                                                   # unless something went wrong
                   2471:                                                   # the first time around
                   2472: # ready to assign
1.364     www      2473:         $logentry=$1.'; '.$logentry;
1.496     www      2474:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      2475:                                                  $kdom,$knum) eq 'ok') {
1.345     www      2476: # key now belongs to user
1.346     www      2477: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      2478:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  2479:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      2480:                 return 'ok';
                   2481:             } else {
                   2482:                 return 
                   2483:   'error: Count not permanently assign key, will need to be re-entered later.';
                   2484: 	    }
                   2485:         } else {
                   2486:             return 'error: Could not assign key, try again later.';
                   2487:         }
1.364     www      2488:     } elsif (!$existing{$ckey}) {
1.345     www      2489: # the key does not exist
                   2490: 	return 'error: The key does not exist';
                   2491:     } else {
                   2492: # the key is somebody else's
                   2493: 	return 'error: The key is already in use';
                   2494:     }
1.344     www      2495: }
                   2496: 
1.364     www      2497: # ------------------------------------------ put an additional comment on a key
                   2498: 
                   2499: sub comment_access_key {
                   2500: #
                   2501: # a valid key looks like uname:udom#comments
                   2502: # comments are being appended
                   2503: #
                   2504:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   2505:     $cdom=
1.620     albertel 2506:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      2507:     $cnum=
1.620     albertel 2508:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      2509:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   2510:     if ($existing{$ckey}) {
                   2511:         $existing{$ckey}.='; '.$logentry;
                   2512: # ready to assign
1.367     www      2513:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      2514:                                                  $cdom,$cnum) eq 'ok') {
                   2515: 	    return 'ok';
                   2516:         } else {
                   2517: 	    return 'error: Count not store comment.';
                   2518:         }
                   2519:     } else {
                   2520: # the key does not exist
                   2521: 	return 'error: The key does not exist';
                   2522:     }
                   2523: }
                   2524: 
1.344     www      2525: # ------------------------------------------------------ Generate a set of keys
                   2526: 
                   2527: sub generate_access_keys {
1.364     www      2528:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      2529:     $cdom=
1.620     albertel 2530:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2531:     $cnum=
1.620     albertel 2532:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      2533:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      2534:     unless (($cdom) && ($cnum)) { return 0; }
                   2535:     if ($number>10000) { return 0; }
                   2536:     sleep(2); # make sure don't get same seed twice
                   2537:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   2538:     my $total=0;
                   2539:     for (my $i=1;$i<=$number;$i++) {
                   2540:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   2541:                   sprintf("%lx",int(100000*rand)).'-'.
                   2542:                   sprintf("%lx",int(100000*rand));
                   2543:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   2544:        $newkey=~s/0/h/g; # and also 0 and O
                   2545:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   2546:        if ($existing{$newkey}) {
                   2547:            $i--;
                   2548:        } else {
1.364     www      2549: 	  if (&put('accesskeys',
                   2550:               { $newkey => '# generated '.localtime().
1.620     albertel 2551:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      2552:                            '; '.$logentry },
                   2553: 		   $cdom,$cnum) eq 'ok') {
1.344     www      2554:               $total++;
                   2555: 	  }
                   2556:        }
                   2557:     }
1.620     albertel 2558:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      2559:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   2560:     return $total;
                   2561: }
                   2562: 
                   2563: # ------------------------------------------------------- Validate an accesskey
                   2564: 
                   2565: sub validate_access_key {
                   2566:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   2567:     $cdom=
1.620     albertel 2568:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2569:     $cnum=
1.620     albertel 2570:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2571:     $udom=$env{'user.domain'} unless (defined($udom));
                   2572:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      2573:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 2574:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      2575: }
                   2576: 
                   2577: # ------------------------------------- Find the section of student in a course
1.652     albertel 2578: sub devalidate_getsection_cache {
                   2579:     my ($udom,$unam,$courseid)=@_;
                   2580:     my $hashid="$udom:$unam:$courseid";
                   2581:     &devalidate_cache_new('getsection',$hashid);
                   2582: }
1.298     matthew  2583: 
1.815     albertel 2584: sub courseid_to_courseurl {
                   2585:     my ($courseid) = @_;
                   2586:     #already url style courseid
                   2587:     return $courseid if ($courseid =~ m{^/});
                   2588: 
                   2589:     if (exists($env{'course.'.$courseid.'.num'})) {
                   2590: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   2591: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   2592: 	return "/$cdom/$cnum";
                   2593:     }
                   2594: 
                   2595:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   2596:     if (exists($courseinfo{'num'})) {
                   2597: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   2598:     }
                   2599: 
                   2600:     return undef;
                   2601: }
                   2602: 
1.298     matthew  2603: sub getsection {
                   2604:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 2605:     my $cachetime=1800;
1.551     albertel 2606: 
                   2607:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 2608:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 2609:     if (defined($cached)) { return $result; }
                   2610: 
1.298     matthew  2611:     my %Pending; 
                   2612:     my %Expired;
                   2613:     #
                   2614:     # Each role can either have not started yet (pending), be active, 
                   2615:     #    or have expired.
                   2616:     #
                   2617:     # If there is an active role, we are done.
                   2618:     #
                   2619:     # If there is more than one role which has not started yet, 
                   2620:     #     choose the one which will start sooner
                   2621:     # If there is one role which has not started yet, return it.
                   2622:     #
                   2623:     # If there is more than one expired role, choose the one which ended last.
                   2624:     # If there is a role which has expired, return it.
                   2625:     #
1.815     albertel 2626:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  2627:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  2628:     foreach my $key (keys(%roleshash)) {
1.479     albertel 2629:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  2630:         my $section=$1;
                   2631:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  2632:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  2633:         my $now=time;
1.548     albertel 2634:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  2635:             $Expired{$end}=$section;
                   2636:             next;
                   2637:         }
1.548     albertel 2638:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  2639:             $Pending{$start}=$section;
                   2640:             next;
                   2641:         }
1.599     albertel 2642:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  2643:     }
                   2644:     #
                   2645:     # Presumedly there will be few matching roles from the above
                   2646:     # loop and the sorting time will be negligible.
                   2647:     if (scalar(keys(%Pending))) {
                   2648:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 2649:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  2650:     } 
                   2651:     if (scalar(keys(%Expired))) {
                   2652:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   2653:         my $time = pop(@sorted);
1.599     albertel 2654:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  2655:     }
1.599     albertel 2656:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  2657: }
1.70      www      2658: 
1.599     albertel 2659: sub save_cache {
                   2660:     &purge_remembered();
1.722     albertel 2661:     #&Apache::loncommon::validate_page();
1.620     albertel 2662:     undef(%env);
1.780     albertel 2663:     undef($env_loaded);
1.599     albertel 2664: }
1.452     albertel 2665: 
1.599     albertel 2666: my $to_remember=-1;
                   2667: my %remembered;
                   2668: my %accessed;
                   2669: my $kicks=0;
                   2670: my $hits=0;
1.849     albertel 2671: sub make_key {
                   2672:     my ($name,$id) = @_;
1.872     albertel 2673:     if (length($id) > 65 
                   2674: 	&& length(&escape($id)) > 200) {
                   2675: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   2676:     }
1.849     albertel 2677:     return &escape($name.':'.$id);
                   2678: }
                   2679: 
1.599     albertel 2680: sub devalidate_cache_new {
                   2681:     my ($name,$id,$debug) = @_;
                   2682:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.1319    damieng  2683:     my $remembered_id=$name.':'.$id;
1.849     albertel 2684:     $id=&make_key($name,$id);
1.599     albertel 2685:     $memcache->delete($id);
1.1319    damieng  2686:     delete($remembered{$remembered_id});
                   2687:     delete($accessed{$remembered_id});
1.599     albertel 2688: }
                   2689: 
                   2690: sub is_cached_new {
                   2691:     my ($name,$id,$debug) = @_;
1.1319    damieng  2692:     my $remembered_id=$name.':'.$id; # this is to avoid make_key (which is slow) whenever possible
                   2693:     if (exists($remembered{$remembered_id})) {
                   2694: 	if ($debug) { &Apache::lonnet::logthis("Early return $remembered_id of $remembered{$remembered_id} "); }
                   2695: 	$accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 2696: 	$hits++;
1.1319    damieng  2697: 	return ($remembered{$remembered_id},1);
1.599     albertel 2698:     }
1.1319    damieng  2699:     $id=&make_key($name,$id);
1.599     albertel 2700:     my $value = $memcache->get($id);
                   2701:     if (!(defined($value))) {
                   2702: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 2703: 	return (undef,undef);
1.416     albertel 2704:     }
1.599     albertel 2705:     if ($value eq '__undef__') {
                   2706: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   2707: 	$value=undef;
                   2708:     }
1.1319    damieng  2709:     &make_room($remembered_id,$value,$debug);
1.599     albertel 2710:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   2711:     return ($value,1);
                   2712: }
                   2713: 
                   2714: sub do_cache_new {
                   2715:     my ($name,$id,$value,$time,$debug) = @_;
1.1319    damieng  2716:     my $remembered_id=$name.':'.$id;
1.849     albertel 2717:     $id=&make_key($name,$id);
1.599     albertel 2718:     my $setvalue=$value;
                   2719:     if (!defined($setvalue)) {
                   2720: 	$setvalue='__undef__';
                   2721:     }
1.623     albertel 2722:     if (!defined($time) ) {
                   2723: 	$time=600;
                   2724:     }
1.599     albertel 2725:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 2726:     my $result = $memcache->set($id,$setvalue,$time);
                   2727:     if (! $result) {
1.872     albertel 2728: 	&logthis("caching of id -> $id  failed");
1.910     albertel 2729: 	$memcache->disconnect_all();
1.872     albertel 2730:     }
1.600     albertel 2731:     # need to make a copy of $value
1.1319    damieng  2732:     &make_room($remembered_id,$value,$debug);
1.599     albertel 2733:     return $value;
                   2734: }
                   2735: 
                   2736: sub make_room {
1.1319    damieng  2737:     my ($remembered_id,$value,$debug)=@_;
1.919     albertel 2738: 
1.1319    damieng  2739:     $remembered{$remembered_id}= (ref($value)) ? &Storable::dclone($value)
1.919     albertel 2740:                                     : $value;
1.599     albertel 2741:     if ($to_remember<0) { return; }
1.1319    damieng  2742:     $accessed{$remembered_id}=[&gettimeofday()];
1.599     albertel 2743:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   2744:     my $to_kick;
                   2745:     my $max_time=0;
                   2746:     foreach my $other (keys(%accessed)) {
                   2747: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   2748: 	    $to_kick=$other;
                   2749: 	    $max_time=&tv_interval($accessed{$other});
                   2750: 	}
                   2751:     }
                   2752:     delete($remembered{$to_kick});
                   2753:     delete($accessed{$to_kick});
                   2754:     $kicks++;
                   2755:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 2756:     return;
                   2757: }
                   2758: 
1.599     albertel 2759: sub purge_remembered {
1.604     albertel 2760:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   2761:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 2762:     undef(%remembered);
                   2763:     undef(%accessed);
1.428     albertel 2764: }
1.70      www      2765: # ------------------------------------- Read an entry from a user's environment
                   2766: 
                   2767: sub userenvironment {
                   2768:     my ($udom,$unam,@what)=@_;
1.976     raeburn  2769:     my $items;
                   2770:     foreach my $item (@what) {
                   2771:         $items.=&escape($item).'&';
                   2772:     }
                   2773:     $items=~s/\&$//;
1.70      www      2774:     my %returnhash=();
1.1009    raeburn  2775:     my $uhome = &homeserver($unam,$udom);
                   2776:     unless ($uhome eq 'no_host') {
                   2777:         my @answer=split(/\&/, 
                   2778:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  2779:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   2780:             return %returnhash;
                   2781:         }
1.1009    raeburn  2782:         my $i;
                   2783:         for ($i=0;$i<=$#what;$i++) {
                   2784: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   2785:         }
1.70      www      2786:     }
                   2787:     return %returnhash;
1.1       albertel 2788: }
                   2789: 
1.617     albertel 2790: # ---------------------------------------------------------- Get a studentphoto
                   2791: sub studentphoto {
                   2792:     my ($udom,$unam,$ext) = @_;
                   2793:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  2794:     if (defined($env{'request.course.id'})) {
1.708     raeburn  2795:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  2796:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   2797:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   2798:             } else {
                   2799:                 my ($result,$perm_reqd)=
1.707     albertel 2800: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2801:                 if ($result eq 'ok') {
                   2802:                     if (!($perm_reqd eq 'yes')) {
                   2803:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   2804:                     }
                   2805:                 }
                   2806:             }
                   2807:         }
                   2808:     } else {
                   2809:         my ($result,$perm_reqd) = 
1.707     albertel 2810: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2811:         if ($result eq 'ok') {
                   2812:             if (!($perm_reqd eq 'yes')) {
                   2813:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   2814:             }
                   2815:         }
                   2816:     }
                   2817:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   2818: }
                   2819: 
                   2820: sub retrievestudentphoto {
                   2821:     my ($udom,$unam,$ext,$type) = @_;
                   2822:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   2823:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   2824:     if ($ret eq 'ok') {
                   2825:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   2826:         if ($type eq 'thumbnail') {
                   2827:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   2828:         }
                   2829:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   2830:         return $tokenurl;
                   2831:     } else {
                   2832:         if ($type eq 'thumbnail') {
                   2833:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   2834:         } else { 
                   2835:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   2836:         }
1.617     albertel 2837:     }
                   2838: }
                   2839: 
1.263     www      2840: # -------------------------------------------------------------------- New chat
                   2841: 
                   2842: sub chatsend {
1.724     raeburn  2843:     my ($newentry,$anon,$group)=@_;
1.620     albertel 2844:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2845:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2846:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      2847:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 2848: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  2849: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      2850: }
                   2851: 
                   2852: # ------------------------------------------ Find current version of a resource
                   2853: 
                   2854: sub getversion {
                   2855:     my $fname=&clutter(shift);
1.1189    raeburn  2856:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      2857:     return &currentversion(&filelocation('',$fname));
                   2858: }
                   2859: 
                   2860: sub currentversion {
                   2861:     my $fname=shift;
                   2862:     my $author=$fname;
                   2863:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2864:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      2865:     my $home=&homeserver($uname,$udom);
1.292     www      2866:     if ($home eq 'no_host') { 
                   2867:         return -1; 
                   2868:     }
1.1112    www      2869:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      2870:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2871: 	return -1;
                   2872:     }
1.1112    www      2873:     return $answer;
1.263     www      2874: }
                   2875: 
1.1111    www      2876: #
                   2877: # Return special version number of resource if set by override, empty otherwise
                   2878: #
                   2879: sub usedversion {
                   2880:     my $fname=shift;
                   2881:     unless ($fname) { $fname=$env{'request.uri'}; }
                   2882:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   2883:     if ($urlversion) { return $urlversion; }
                   2884:     return '';
                   2885: }
                   2886: 
1.1       albertel 2887: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      2888: 
1.1       albertel 2889: sub subscribe {
                   2890:     my $fname=shift;
1.761     raeburn  2891:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 2892:     $fname=~s/[\n\r]//g;
1.1       albertel 2893:     my $author=$fname;
                   2894:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2895:     my ($udom,$uname)=split(/\//,$author);
                   2896:     my $home=homeserver($uname,$udom);
1.335     albertel 2897:     if ($home eq 'no_host') {
                   2898:         return 'not_found';
1.1       albertel 2899:     }
                   2900:     my $answer=reply("sub:$fname",$home);
1.64      www      2901:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2902: 	$answer.=' by '.$home;
                   2903:     }
1.1       albertel 2904:     return $answer;
                   2905: }
                   2906:     
1.8       www      2907: # -------------------------------------------------------------- Replicate file
                   2908: 
                   2909: sub repcopy {
                   2910:     my $filename=shift;
1.23      www      2911:     $filename=~s/\/+/\//g;
1.1142    raeburn  2912:     my $londocroot = $perlvar{'lonDocRoot'};
                   2913:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  2914:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  2915:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   2916: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 2917: 	return &repcopy_userfile($filename);
                   2918:     }
1.532     albertel 2919:     $filename=~s/[\n\r]//g;
1.8       www      2920:     my $transname="$filename.in.transfer";
1.828     www      2921: # FIXME: this should flock
1.607     raeburn  2922:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      2923:     my $remoteurl=subscribe($filename);
1.64      www      2924:     if ($remoteurl =~ /^con_lost by/) {
                   2925: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2926:            return 'unavailable';
1.8       www      2927:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 2928: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  2929: 	   return 'not_found';
1.64      www      2930:     } elsif ($remoteurl =~ /^rejected by/) {
                   2931: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2932:            return 'forbidden';
1.20      www      2933:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  2934:            return 'ok';
1.8       www      2935:     } else {
1.290     www      2936:         my $author=$filename;
                   2937:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2938:         my ($udom,$uname)=split(/\//,$author);
                   2939:         my $home=homeserver($uname,$udom);
                   2940:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      2941:            my @parts=split(/\//,$filename);
                   2942:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  2943:            if ($path ne "$londocroot/res") {
1.8       www      2944:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  2945: 	       return 'bad_request';
1.8       www      2946:            }
                   2947:            my $count;
                   2948:            for ($count=5;$count<$#parts;$count++) {
                   2949:                $path.="/$parts[$count]";
                   2950:                if ((-e $path)!=1) {
                   2951: 		   mkdir($path,0777);
                   2952:                }
                   2953:            }
                   2954:            my $request=new HTTP::Request('GET',"$remoteurl");
1.1345  ! raeburn  2955:            my $response;
        !          2956:            if ($remoteurl =~ m{/raw/}) {
        !          2957:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',0,1);
        !          2958:            } else {
        !          2959:                $response=&LONCAPA::LWPReq::makerequest($home,$request,$transname,\%perlvar,'',1);
        !          2960:            }
1.8       www      2961:            if ($response->is_error()) {
                   2962: 	       unlink($transname);
                   2963:                my $message=$response->status_line;
1.672     albertel 2964:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      2965:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  2966:                return 'unavailable';
1.8       www      2967:            } else {
1.16      www      2968: 	       if ($remoteurl!~/\.meta$/) {
                   2969:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
1.1345  ! raeburn  2970:                   my $mresponse;
        !          2971:                   if ($remoteurl =~ m{/raw/}) {
        !          2972:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',0,1);
        !          2973:                   } else {
        !          2974:                       $mresponse = &LONCAPA::LWPReq::makerequest($home,$mrequest,$filename.'.meta',\%perlvar,'',1);
        !          2975:                   }
1.16      www      2976:                   if ($mresponse->is_error()) {
                   2977: 		      unlink($filename.'.meta');
                   2978:                       &logthis(
1.672     albertel 2979:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      2980:                   }
                   2981: 	       }
1.8       www      2982:                rename($transname,$filename);
1.607     raeburn  2983:                return 'ok';
1.8       www      2984:            }
1.290     www      2985:        }
1.8       www      2986:     }
1.330     www      2987: }
                   2988: 
                   2989: # ------------------------------------------------ Get server side include body
                   2990: sub ssi_body {
1.381     albertel 2991:     my ($filelink,%form)=@_;
1.606     matthew  2992:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   2993:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   2994:     }
1.953     www      2995:     my $output='';
                   2996:     my $response;
1.980     raeburn  2997:     if ($filelink=~/^https?\:/) {
1.954     raeburn  2998:        ($output,$response)=&externalssi($filelink);
1.953     www      2999:     } else {
1.1004    droeschl 3000:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   3001:        $filelink .= 'inhibitmenu=yes';
1.953     www      3002:        ($output,$response)=&ssi($filelink,%form);
                   3003:     }
1.778     albertel 3004:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 3005:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 3006:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      3007:     if (wantarray) {
                   3008:         return ($output, $response);
                   3009:     } else {
                   3010:         return $output;
                   3011:     }
1.8       www      3012: }
                   3013: 
1.15      www      3014: # --------------------------------------------------------- Server Side Include
                   3015: 
1.782     albertel 3016: sub absolute_url {
                   3017:     my ($host_name) = @_;
                   3018:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   3019:     if ($host_name eq '') {
                   3020: 	$host_name = $ENV{'SERVER_NAME'};
                   3021:     }
                   3022:     return $protocol.$host_name;
                   3023: }
                   3024: 
1.942     foxr     3025: #
                   3026: #   Server side include.
                   3027: # Parameters:
                   3028: #  fn     Possibly encrypted resource name/id.
                   3029: #  form   Hash that describes how the rendering should be done
                   3030: #         and other things.
1.944     foxr     3031: # Returns:
1.950     raeburn  3032: #   Scalar context: The content of the response.
                   3033: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     3034: #     
1.15      www      3035: sub ssi {
                   3036: 
1.944     foxr     3037:     my ($fn,%form)=@_;
1.23      www      3038:     my $request;
1.711     albertel 3039: 
                   3040:     $form{'no_update_last_known'}=1;
1.895     albertel 3041:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      3042:     if (%form) {
1.782     albertel 3043:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1272    droeschl 3044:       $request->content(join('&',map { 
                   3045:             my $name = escape($_);
                   3046:             "$name=" . ( ref($form{$_}) eq 'ARRAY' 
                   3047:             ? join("&$name=", map {escape($_) } @{$form{$_}}) 
                   3048:             : &escape($form{$_}) );    
                   3049:         } keys(%form)));
1.23      www      3050:     } else {
1.782     albertel 3051:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      3052:     }
                   3053: 
1.15      www      3054:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1345  ! raeburn  3055:     my $lonhost = $perlvar{'lonHostID'};
        !          3056:     my $response= &LONCAPA::LWPReq::makerequest($lonhost,$request,'',\%perlvar);
1.1182    foxr     3057: 
1.944     foxr     3058:     if (wantarray) {
1.1345  ! raeburn  3059: 	return ($response->content, $response);
1.944     foxr     3060:     } else {
1.1345  ! raeburn  3061: 	return $response->content;
1.942     foxr     3062:     }
1.324     www      3063: }
                   3064: 
                   3065: sub externalssi {
                   3066:     my ($url)=@_;
                   3067:     my $request=new HTTP::Request('GET',$url);
1.1345  ! raeburn  3068:     my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar);
1.954     raeburn  3069:     if (wantarray) {
                   3070:         return ($response->content, $response);
                   3071:     } else {
                   3072:         return $response->content;
                   3073:     }
1.15      www      3074: }
1.254     www      3075: 
1.492     albertel 3076: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   3077: 
                   3078: sub allowuploaded {
                   3079:     my ($srcurl,$url)=@_;
                   3080:     $url=&clutter(&declutter($url));
                   3081:     my $dir=$url;
                   3082:     $dir=~s/\/[^\/]+$//;
                   3083:     my %httpref=();
                   3084:     my $httpurl=&hreflocation('',$url);
                   3085:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  3086:     &Apache::lonnet::appenv(\%httpref);
1.254     www      3087: }
1.477     raeburn  3088: 
1.1193    raeburn  3089: #
                   3090: # Determine if the current user should be able to edit a particular resource,
                   3091: # when viewing in course context.
                   3092: # (a) When viewing resource used to determine if "Edit" item is included in 
                   3093: #     Functions.
                   3094: # (b) When displaying folder contents in course editor, used to determine if
                   3095: #     "Edit" link will be displayed alongside resource.
                   3096: #
1.1196    raeburn  3097: #  input: six args -- filename (decluttered), course number, course domain,
                   3098: #                   url, symb (if registered) and group (if this is a group
                   3099: #                   item -- e.g., bulletin board, group page etc.).
                   3100: #  output: array of five scalars -- 
1.1193    raeburn  3101: #          $cfile -- url for file editing if editable on current server
                   3102: #          $home -- homeserver of resource (i.e., for author if published,
                   3103: #                                           or course if uploaded.).
                   3104: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  3105: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   3106: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  3107: #
                   3108: 
                   3109: sub can_edit_resource {
1.1194    raeburn  3110:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   3111:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   3112: #
                   3113: # For aboutme pages user can only edit his/her own.
                   3114: #
1.1199    raeburn  3115:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  3116:         my ($sdom,$sname) = ($1,$2);
                   3117:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   3118:             $home = $env{'user.home'};
                   3119:             $cfile = $resurl;
                   3120:             if ($env{'form.forceedit'}) {
                   3121:                 $forceview = 1;
                   3122:             } else {
                   3123:                 $forceedit = 1;
                   3124:             }
                   3125:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   3126:         } else {
                   3127:             return;
                   3128:         }
                   3129:     }
                   3130: 
                   3131:     if ($env{'request.course.id'}) {
                   3132:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
                   3133:         if ($group ne '') {
                   3134: # if this is a group homepage or group bulletin board, check group privs
                   3135:             my $allowed = 0;
1.1197    raeburn  3136:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   3137:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  3138:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  3139:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   3140:                     $allowed = 1;
                   3141:                 }
1.1197    raeburn  3142:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   3143:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   3144:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  3145:                     $allowed = 1;
                   3146:                 }
                   3147:             }
                   3148:             if ($allowed) {
                   3149:                 $home=&homeserver($cnum,$cdom);
                   3150:                 if ($env{'form.forceedit'}) {
                   3151:                     $forceview = 1;
                   3152:                 } else {
                   3153:                     $forceedit = 1;
                   3154:                 }
                   3155:                 $cfile = $resurl;
                   3156:             } else {
                   3157:                 return;
                   3158:             }
                   3159:         } else {
1.1208    raeburn  3160:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3161:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
                   3162:                     return;
                   3163:                 }
                   3164:             } elsif (!$crsedit) {
1.1194    raeburn  3165: #
                   3166: # No edit allowed where CC has switched to student role.
                   3167: #
                   3168:                 return;
                   3169:             }
                   3170:         }
                   3171:     }
                   3172: 
1.1193    raeburn  3173:     if ($file ne '') {
                   3174:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  3175:             if (&is_course_upload($file,$cnum,$cdom)) {
                   3176:                 $uploaded = 1;
                   3177:                 $incourse = 1;
1.1193    raeburn  3178:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   3179:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  3180:                     if ($env{'form.forceedit'}) {
                   3181:                         $forceview = 1;
                   3182:                     } else {
                   3183:                         $forceedit = 1;
                   3184:                     }
1.1194    raeburn  3185:                 }
                   3186:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   3187:                 $incourse = 1;
                   3188:                 if ($env{'form.forceedit'}) {
                   3189:                     $forceview = 1;
                   3190:                 } else {
                   3191:                     $forceedit = 1;
                   3192:                 }
                   3193:                 $cfile = $resurl;
                   3194:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
                   3195:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   3196:                     $incourse = 1;
                   3197:                     if ($env{'form.forceedit'}) {
                   3198:                         $forceview = 1;
                   3199:                     } else {
                   3200:                         $forceedit = 1;
                   3201:                     }
                   3202:                     $cfile = $resurl;
1.1199    raeburn  3203:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  3204:                     $incourse = 1;
                   3205:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  3206:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  3207:                     $incourse = 1;
1.1199    raeburn  3208:                     if ($env{'form.forceedit'}) {
                   3209:                         $forceview = 1;
                   3210:                     } else {
                   3211:                         $forceedit = 1;
                   3212:                     }
                   3213:                     $cfile = $resurl;
1.1343    raeburn  3214:                 } elsif ($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3215:                     $incourse = 1;
                   3216:                     if ($env{'form.forceedit'}) {
                   3217:                         $forceview = 1;
                   3218:                     } else {
                   3219:                         $forceedit = 1;
                   3220:                     }
                   3221:                     $cfile = $resurl;
1.1208    raeburn  3222:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3223:                     $incourse = 1;
                   3224:                     if ($env{'form.forceedit'}) {
                   3225:                         $forceview = 1;
                   3226:                     } else {
                   3227:                         $forceedit = 1;
                   3228:                     }
                   3229:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  3230:                 }
                   3231:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   3232:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   3233:                 if (&is_on_map($template)) { 
                   3234:                     $incourse = 1;
                   3235:                     $forceview = 1;
                   3236:                     $cfile = $template;
1.1193    raeburn  3237:                 }
1.1199    raeburn  3238:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
                   3239:                     $incourse = 1;
                   3240:                     if ($env{'form.forceedit'}) {
                   3241:                         $forceview = 1;
                   3242:                     } else {
                   3243:                         $forceedit = 1;
                   3244:                     }
                   3245:                     $cfile = $resurl;
1.1343    raeburn  3246:             } elsif (($resurl =~ m{^/adm/wrapper/adm/$cdom/$cnum/\d+/ext\.tool$}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
1.1298    raeburn  3247:                 $incourse = 1;
                   3248:                 if ($env{'form.forceedit'}) {
                   3249:                     $forceview = 1;
                   3250:                 } else {
                   3251:                     $forceedit = 1;
                   3252:                 }
                   3253:                 $cfile = $resurl;
1.1199    raeburn  3254:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   3255:                 $incourse = 1;
                   3256:                 $forceview = 1;
                   3257:                 if ($symb) {
                   3258:                     my ($map,$id,$res)=&decode_symb($symb);
                   3259:                     $env{'request.symb'} = $symb;
                   3260:                     $cfile = &clutter($res);
                   3261:                 } else {
                   3262:                     $cfile = $env{'form.suppurl'};
1.1298    raeburn  3263:                     my $escfile = &unescape($cfile);
1.1343    raeburn  3264:                     if ($escfile =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}) {
1.1298    raeburn  3265:                         $cfile = '/adm/wrapper'.$escfile;
                   3266:                     } else {
                   3267:                         $escfile =~ s{^http://}{};
                   3268:                         $cfile = &escape("/adm/wrapper/ext/$escfile");
                   3269:                     }
1.1199    raeburn  3270:                 }
1.1234    raeburn  3271:             } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   3272:                 if ($env{'form.forceedit'}) {
                   3273:                     $forceview = 1;
                   3274:                 } else {
                   3275:                     $forceedit = 1;
                   3276:                 }
                   3277:                 $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1193    raeburn  3278:             }
                   3279:         }
1.1194    raeburn  3280:         if ($uploaded || $incourse) {
                   3281:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  3282:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  3283:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   3284:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   3285:             # Check that the user has permission to edit this resource
                   3286:             my $setpriv = 1;
                   3287:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   3288:             if (defined($cfudom)) {
                   3289:                 $home=&homeserver($cfuname,$cfudom);
                   3290:                 $cfile=$file;
                   3291:             }
                   3292:         }
1.1194    raeburn  3293:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   3294:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  3295:             my @ids=&current_machine_ids();
                   3296:             unless (grep(/^\Q$home\E$/,@ids)) {
                   3297:                 $switchserver=1;
                   3298:             }
                   3299:         }
                   3300:     }
1.1194    raeburn  3301:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  3302: }
                   3303: 
                   3304: sub is_course_upload {
                   3305:     my ($file,$cnum,$cdom) = @_;
                   3306:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   3307:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  3308:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   3309:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  3310:         return 1;
                   3311:     }
                   3312:     return;
                   3313: }
                   3314: 
1.1194    raeburn  3315: sub in_course {
1.1195    raeburn  3316:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   3317:     if ($hideprivileged) {
                   3318:         my $skipuser;
1.1219    raeburn  3319:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   3320:         my @possdoms = ($cdom);  
                   3321:         if ($coursehash{'checkforpriv'}) { 
                   3322:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   3323:         }
                   3324:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  3325:             $skipuser = 1;
                   3326:             if ($coursehash{'nothideprivileged'}) {
                   3327:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3328:                     my $user;
                   3329:                     if ($item =~ /:/) {
                   3330:                         $user = $item;
                   3331:                     } else {
                   3332:                         $user = join(':',split(/[\@]/,$item));
                   3333:                     }
                   3334:                     if ($user eq $uname.':'.$udom) {
                   3335:                         undef($skipuser);
                   3336:                         last;
                   3337:                     }
                   3338:                 }
                   3339:             }
                   3340:             if ($skipuser) {
                   3341:                 return 0;
                   3342:             }
                   3343:         }
                   3344:     }
1.1194    raeburn  3345:     $type ||= 'any';
                   3346:     if (!defined($cdom) || !defined($cnum)) {
                   3347:         my $cid  = $env{'request.course.id'};
                   3348:         $cdom = $env{'course.'.$cid.'.domain'};
                   3349:         $cnum = $env{'course.'.$cid.'.num'};
                   3350:     }
                   3351:     my $typesref;
1.1195    raeburn  3352:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  3353:         $typesref = ['active','previous','future'];
                   3354:     } elsif ($type eq 'previous' || $type eq 'future') {
                   3355:         $typesref = [$type];
                   3356:     }
                   3357:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   3358:                               $typesref,undef,[$cdom]);
                   3359:     my ($tmp) = keys(%roles);
                   3360:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   3361:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   3362:     if (@course_roles > 0) {
                   3363:         return 1;
                   3364:     }
                   3365:     return 0;
                   3366: }
                   3367: 
1.478     albertel 3368: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 3369: # input: action, courseID, current domain, intended
1.637     raeburn  3370: #        path to file, source of file, instruction to parse file for objects,
                   3371: #        ref to hash for embedded objects,
                   3372: #        ref to hash for codebase of java objects.
1.1095    raeburn  3373: #        reference to scalar to accommodate mime type determined
                   3374: #          from File::MMagic if $parser = parse.
1.637     raeburn  3375: #
1.485     raeburn  3376: # output: url to file (if action was uploaddoc), 
                   3377: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  3378: #
1.478     albertel 3379: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   3380: # course.
1.477     raeburn  3381: #
1.478     albertel 3382: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3383: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   3384: #          course's home server.
1.477     raeburn  3385: #
1.478     albertel 3386: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   3387: #          be copied from $source (current location) to 
                   3388: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3389: #         and will then be copied to
                   3390: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   3391: #         course's home server.
1.485     raeburn  3392: #
1.481     raeburn  3393: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 3394: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  3395: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   3396: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   3397: #         in course's home server.
1.637     raeburn  3398: #
1.477     raeburn  3399: 
                   3400: sub process_coursefile {
1.1095    raeburn  3401:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   3402:         $mimetype)=@_;
1.477     raeburn  3403:     my $fetchresult;
1.638     albertel 3404:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  3405:     if ($action eq 'propagate') {
1.638     albertel 3406:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   3407: 			     $home);
1.481     raeburn  3408:     } else {
1.477     raeburn  3409:         my $fpath = '';
                   3410:         my $fname = $file;
1.478     albertel 3411:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  3412:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  3413:         my $filepath = &build_filepath($fpath);
1.481     raeburn  3414:         if ($action eq 'copy') {
                   3415:             if ($source eq '') {
                   3416:                 $fetchresult = 'no source file';
                   3417:                 return $fetchresult;
                   3418:             } else {
                   3419:                 my $destination = $filepath.'/'.$fname;
                   3420:                 rename($source,$destination);
                   3421:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3422:                                  $home);
1.481     raeburn  3423:             }
                   3424:         } elsif ($action eq 'uploaddoc') {
                   3425:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 3426:             print $fh $env{'form.'.$source};
1.481     raeburn  3427:             close($fh);
1.637     raeburn  3428:             if ($parser eq 'parse') {
1.1024    raeburn  3429:                 my $mm = new File::MMagic;
1.1095    raeburn  3430:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   3431:                 if ($type eq 'text/html') {
1.1024    raeburn  3432:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   3433:                     unless ($parse_result eq 'ok') {
                   3434:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   3435:                     }
1.637     raeburn  3436:                 }
1.1095    raeburn  3437:                 if (ref($mimetype)) {
                   3438:                     $$mimetype = $type;
                   3439:                 } 
1.637     raeburn  3440:             }
1.477     raeburn  3441:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3442:                                  $home);
1.481     raeburn  3443:             if ($fetchresult eq 'ok') {
                   3444:                 return '/uploaded/'.$fpath.'/'.$fname;
                   3445:             } else {
                   3446:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 3447:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  3448:                 return '/adm/notfound.html';
                   3449:             }
1.477     raeburn  3450:         }
                   3451:     }
1.485     raeburn  3452:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  3453:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 3454:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  3455:     }
                   3456:     return $fetchresult;
                   3457: }
                   3458: 
1.637     raeburn  3459: sub build_filepath {
                   3460:     my ($fpath) = @_;
                   3461:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   3462:     unless ($fpath eq '') {
                   3463:         my @parts=split('/',$fpath);
                   3464:         foreach my $part (@parts) {
                   3465:             $filepath.= '/'.$part;
                   3466:             if ((-e $filepath)!=1) {
                   3467:                 mkdir($filepath,0777);
                   3468:             }
                   3469:         }
                   3470:     }
                   3471:     return $filepath;
                   3472: }
                   3473: 
                   3474: sub store_edited_file {
1.638     albertel 3475:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  3476:     my $file = $primary_url;
                   3477:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   3478:     my $fpath = '';
                   3479:     my $fname = $file;
                   3480:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   3481:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   3482:     my $filepath = &build_filepath($fpath);
                   3483:     open(my $fh,'>'.$filepath.'/'.$fname);
                   3484:     print $fh $content;
                   3485:     close($fh);
1.638     albertel 3486:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  3487:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3488: 			  $home);
1.637     raeburn  3489:     if ($$fetchresult eq 'ok') {
                   3490:         return '/uploaded/'.$fpath.'/'.$fname;
                   3491:     } else {
1.638     albertel 3492:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   3493: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  3494:         return '/adm/notfound.html';
                   3495:     }
                   3496: }
                   3497: 
1.531     albertel 3498: sub clean_filename {
1.831     albertel 3499:     my ($fname,$args)=@_;
1.315     www      3500: # Replace Windows backslashes by forward slashes
1.257     www      3501:     $fname=~s/\\/\//g;
1.831     albertel 3502:     if (!$args->{'keep_path'}) {
                   3503:         # Get rid of everything but the actual filename
                   3504: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   3505:     }
1.315     www      3506: # Replace spaces by underscores
                   3507:     $fname=~s/\s+/\_/g;
                   3508: # Replace all other weird characters by nothing
1.831     albertel 3509:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 3510: # Replace all .\d. sequences with _\d. so they no longer look like version
                   3511: # numbers
                   3512:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 3513:     return $fname;
                   3514: }
1.1051    raeburn  3515: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   3516: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   3517: # image with the same aspect ratio as the original, but with dimensions which do 
                   3518: # not exceed $resizewidth and $resizeheight.
                   3519:  
1.984     neumanie 3520: sub resizeImage {
1.1051    raeburn  3521:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   3522:     my $ima = Image::Magick->new;
                   3523:     my $resized;
                   3524:     if (-e $img_path) {
                   3525:         $ima->Read($img_path);
                   3526:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   3527:             my $width = $ima->Get('width');
                   3528:             my $height = $ima->Get('height');
                   3529:             if ($width > $resizewidth) {
                   3530: 	        my $factor = $width/$resizewidth;
                   3531:                 my $newheight = $height/$factor;
                   3532:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   3533:                 $resized = 1;
                   3534:             }
                   3535:         }
                   3536:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   3537:             my $width = $ima->Get('width');
                   3538:             my $height = $ima->Get('height');
                   3539:             if ($height > $resizeheight) {
                   3540:                 my $factor = $height/$resizeheight;
                   3541:                 my $newwidth = $width/$factor;
                   3542:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   3543:                 $resized = 1;
                   3544:             }
                   3545:         }
                   3546:         if ($resized) {
                   3547:             $ima->Write($img_path);
                   3548:         }
                   3549:     }
                   3550:     return;
1.977     amueller 3551: }
                   3552: 
1.608     albertel 3553: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 3554: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  3555: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  3556: #        $context - possible values: coursedoc, existingfile, overwrite, 
                   3557: #                                    canceloverwrite, or ''. 
                   3558: #                   if 'coursedoc': upload to the current course
                   3559: #                   if 'existingfile': write file to tmp/overwrites directory 
                   3560: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   3561: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 3562: #        $subdir - directory in userfile to store the file into
1.858     raeburn  3563: #        $parser - instruction to parse file for objects ($parser = parse)    
                   3564: #        $allfiles - reference to hash for embedded objects
                   3565: #        $codebase - reference to hash for codebase of java objects
                   3566: #        $desuname - username for permanent storage of uploaded file
                   3567: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  3568: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   3569: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  3570: #        $resizewidth - width (pixels) to which to resize uploaded image
                   3571: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  3572: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  3573: #                    from File::MMagic.
1.858     raeburn  3574: # 
1.686     albertel 3575: # output: url of file in userspace, or error: <message> 
                   3576: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 3577: 
1.531     albertel 3578: sub userfileupload {
1.1090    raeburn  3579:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  3580:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 3581:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 3582:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 3583:     $fname=&clean_filename($fname);
1.1090    raeburn  3584:     # See if there is anything left
1.257     www      3585:     unless ($fname) { return 'error: no uploaded file'; }
1.1090    raeburn  3586:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   3587:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   3588:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   3589:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  3590:         my $now = time;
1.1090    raeburn  3591:         my $filepath;
1.1095    raeburn  3592:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  3593:              $filepath = 'tmp/helprequests/'.$now;
                   3594:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   3595:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   3596:                          '_'.$env{'user.domain'}.'/pending';
                   3597:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   3598:             my ($docuname,$docudom);
                   3599:             if ($destudom) {
                   3600:                 $docudom = $destudom;
                   3601:             } else {
                   3602:                 $docudom = $env{'user.domain'};
                   3603:             }
                   3604:             if ($destuname) {
                   3605:                 $docuname = $destuname;
                   3606:             } else {
                   3607:                 $docuname = $env{'user.name'};
                   3608:             }
                   3609:             if (exists($env{'form.group'})) {
                   3610:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3611:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3612:             }
                   3613:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   3614:             if ($context eq 'canceloverwrite') {
                   3615:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   3616:                 if (-e  $tempfile) {
                   3617:                     my @info = stat($tempfile);
                   3618:                     if ($info[9] eq $env{'form.timestamp'}) {
                   3619:                         unlink($tempfile);
                   3620:                     }
                   3621:                 }
                   3622:                 return;
1.523     raeburn  3623:             }
                   3624:         }
1.1090    raeburn  3625:         # Create the directory if not present
1.741     raeburn  3626:         my @parts=split(/\//,$filepath);
                   3627:         my $fullpath = $perlvar{'lonDaemons'};
                   3628:         for (my $i=0;$i<@parts;$i++) {
                   3629:             $fullpath .= '/'.$parts[$i];
                   3630:             if ((-e $fullpath)!=1) {
                   3631:                 mkdir($fullpath,0777);
                   3632:             }
                   3633:         }
                   3634:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   3635:         print $fh $env{'form.'.$formname};
                   3636:         close($fh);
1.1090    raeburn  3637:         if ($context eq 'existingfile') {
                   3638:             my @info = stat($fullpath.'/'.$fname);
                   3639:             return ($fullpath.'/'.$fname,$info[9]);
                   3640:         } else {
                   3641:             return $fullpath.'/'.$fname;
                   3642:         }
1.523     raeburn  3643:     }
1.995     raeburn  3644:     if ($subdir eq 'scantron') {
                   3645:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  3646:     } else {
1.995     raeburn  3647:         $fname="$subdir/$fname";
                   3648:     }
1.1090    raeburn  3649:     if ($context eq 'coursedoc') {
1.638     albertel 3650: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3651: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  3652:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 3653:             return &finishuserfileupload($docuname,$docudom,
                   3654: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  3655: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  3656:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  3657:         } else {
1.1218    raeburn  3658:             if ($env{'form.folder'}) {
                   3659:                 $fname=$env{'form.folder'}.'/'.$fname;
                   3660:             }
1.638     albertel 3661:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   3662: 				       $fname,$formname,$parser,
1.1095    raeburn  3663: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  3664:         }
1.719     banghart 3665:     } elsif (defined($destuname)) {
                   3666:         my $docuname=$destuname;
                   3667:         my $docudom=$destudom;
1.860     raeburn  3668: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   3669: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  3670:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  3671:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      3672:     } else {
1.638     albertel 3673:         my $docuname=$env{'user.name'};
                   3674:         my $docudom=$env{'user.domain'};
1.1220    raeburn  3675:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  3676:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3677:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3678:         }
1.860     raeburn  3679: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   3680: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  3681:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  3682:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      3683:     }
1.271     www      3684: }
                   3685: 
                   3686: sub finishuserfileupload {
1.860     raeburn  3687:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  3688:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  3689:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      3690:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 3691:   
1.860     raeburn  3692:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 3693:     $file=$fname;
                   3694:     if ($fname=~m|/|) {
                   3695:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   3696: 	$path.=$fnamepath.'/';
                   3697:     }
1.259     www      3698:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      3699:     my $count;
                   3700:     for ($count=4;$count<=$#parts;$count++) {
                   3701:         $filepath.="/$parts[$count]";
                   3702:         if ((-e $filepath)!=1) {
                   3703: 	    mkdir($filepath,0777);
                   3704:         }
                   3705:     }
1.984     neumanie 3706: 
1.258     www      3707: # Save the file
                   3708:     {
1.701     albertel 3709: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   3710: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   3711: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   3712: 	    return '/adm/notfound.html';
                   3713: 	}
1.1090    raeburn  3714:         if ($context eq 'overwrite') {
1.1117    foxr     3715:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  3716:             my $target = $filepath.'/'.$file;
                   3717:             if (-e $source) {
                   3718:                 my @info = stat($source);
                   3719:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   3720:                     unless (&File::Copy::move($source,$target)) {
                   3721:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   3722:                         return "Moving from $source failed";
                   3723:                     }
                   3724:                 } else {
                   3725:                     return "Temporary file: $source had unexpected date/time for last modification";
                   3726:                 }
                   3727:             } else {
                   3728:                 return "Temporary file: $source missing";
                   3729:             }
                   3730:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 3731: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   3732: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   3733: 	    return '/adm/notfound.html';
                   3734: 	}
1.570     albertel 3735: 	close(FH);
1.1051    raeburn  3736:         if ($resizewidth && $resizeheight) {
                   3737:             my $mm = new File::MMagic;
                   3738:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   3739:             if ($mime_type =~ m{^image/}) {
                   3740: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   3741:             }  
1.977     amueller 3742: 	}
1.258     www      3743:     }
1.1152    raeburn  3744:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   3745:         if (ref($mimetype)) {
                   3746:             if ($$mimetype eq '') {
                   3747:                 my $mm = new File::MMagic;
                   3748:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   3749:                 $$mimetype = $type;
                   3750:             }
                   3751:         }
                   3752:     }
1.637     raeburn  3753:     if ($parser eq 'parse') {
1.1152    raeburn  3754:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  3755:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   3756:                                                        $allfiles,$codebase);
                   3757:             unless ($parse_result eq 'ok') {
                   3758:                 &logthis('Failed to parse '.$filepath.$file.
                   3759: 	   	         ' for embedded media: '.$parse_result); 
                   3760:             }
1.637     raeburn  3761:         }
                   3762:     }
1.860     raeburn  3763:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   3764:         my $input = $filepath.'/'.$file;
                   3765:         my $output = $filepath.'/'.'tn-'.$file;
                   3766:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   3767:         system("convert -sample $thumbsize $input $output");
                   3768:         if (-e $filepath.'/'.'tn-'.$file) {
                   3769:             $fetchthumb  = 1; 
                   3770:         }
                   3771:     }
1.858     raeburn  3772:  
1.259     www      3773: # Notify homeserver to grep it
                   3774: #
1.984     neumanie 3775:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 3776:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      3777:     if ($fetchresult eq 'ok') {
1.860     raeburn  3778:         if ($fetchthumb) {
                   3779:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   3780:             if ($thumbresult ne 'ok') {
                   3781:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   3782:                          $docuhome.': '.$thumbresult);
                   3783:             }
                   3784:         }
1.259     www      3785: #
1.258     www      3786: # Return the URL to it
1.494     albertel 3787:         return '/uploaded/'.$path.$file;
1.263     www      3788:     } else {
1.494     albertel 3789:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   3790: 		 ': '.$fetchresult);
1.263     www      3791:         return '/adm/notfound.html';
1.858     raeburn  3792:     }
1.493     albertel 3793: }
                   3794: 
1.637     raeburn  3795: sub extract_embedded_items {
1.961     raeburn  3796:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  3797:     my @state = ();
1.1164    raeburn  3798:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  3799:     my %javafiles = (
                   3800:                       codebase => '',
                   3801:                       code => '',
                   3802:                       archive => ''
                   3803:                     );
                   3804:     my %mediafiles = (
                   3805:                       src => '',
                   3806:                       movie => '',
                   3807:                      );
1.648     raeburn  3808:     my $p;
                   3809:     if ($content) {
                   3810:         $p = HTML::LCParser->new($content);
                   3811:     } else {
1.961     raeburn  3812:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  3813:     }
1.641     albertel 3814:     while (my $t=$p->get_token()) {
1.640     albertel 3815: 	if ($t->[0] eq 'S') {
                   3816: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 3817: 	    push(@state, $tagname);
1.648     raeburn  3818:             if (lc($tagname) eq 'allow') {
                   3819:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   3820:             }
1.640     albertel 3821: 	    if (lc($tagname) eq 'img') {
                   3822: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   3823: 	    }
1.886     albertel 3824: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  3825:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  3826:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   3827:                 }
1.886     albertel 3828: 	    }
1.645     raeburn  3829:             if (lc($tagname) eq 'script') {
1.1164    raeburn  3830:                 my $src;
1.645     raeburn  3831:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   3832:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   3833:                 } else {
1.1164    raeburn  3834:                     if ($attr->{'src'} ne '') {
                   3835:                         $src = $attr->{'src'};
                   3836:                         &add_filetype($allfiles,$src,'src');
                   3837:                     }
                   3838:                 }
                   3839:                 my $text = $p->get_trimmed_text();
                   3840:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   3841:                     my @swfargs = split(/,/,$1);
                   3842:                     foreach my $item (@swfargs) {
                   3843:                         $item =~ s/["']//g;
                   3844:                         $item =~ s/^\s+//;
                   3845:                         $item =~ s/\s+$//;
                   3846:                     }
                   3847:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   3848:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   3849:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   3850:                         } else {
                   3851:                             $related{$swfargs[0]} = [$swfargs[2]];
                   3852:                         }
                   3853:                     }
1.645     raeburn  3854:                 }
                   3855:             }
                   3856:             if (lc($tagname) eq 'link') {
                   3857:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   3858:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   3859:                 }
                   3860:             }
1.640     albertel 3861: 	    if (lc($tagname) eq 'object' ||
                   3862: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   3863: 		foreach my $item (keys(%javafiles)) {
                   3864: 		    $javafiles{$item} = '';
                   3865: 		}
1.1164    raeburn  3866:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   3867:                     $lastids{lc($tagname)} = $attr->{'id'};
                   3868:                 }
1.640     albertel 3869: 	    }
                   3870: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   3871: 		my $name = lc($attr->{'name'});
                   3872: 		foreach my $item (keys(%javafiles)) {
                   3873: 		    if ($name eq $item) {
                   3874: 			$javafiles{$item} = $attr->{'value'};
                   3875: 			last;
                   3876: 		    }
                   3877: 		}
1.1164    raeburn  3878:                 my $pathfrom;
1.640     albertel 3879: 		foreach my $item (keys(%mediafiles)) {
                   3880: 		    if ($name eq $item) {
1.1164    raeburn  3881:                         $pathfrom = $attr->{'value'};
                   3882:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   3883: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 3884: 			last;
                   3885: 		    }
                   3886: 		}
1.1164    raeburn  3887:                 if ($name eq 'flashvars') {
                   3888:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   3889:                 }
                   3890:                 if ($pathfrom ne '') {
                   3891:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   3892:                                          $pathfrom);
                   3893:                 }
1.640     albertel 3894: 	    }
                   3895: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   3896: 		foreach my $item (keys(%javafiles)) {
                   3897: 		    if ($attr->{$item}) {
                   3898: 			$javafiles{$item} = $attr->{$item};
                   3899: 			last;
                   3900: 		    }
                   3901: 		}
                   3902: 		foreach my $item (keys(%mediafiles)) {
                   3903: 		    if ($attr->{$item}) {
                   3904: 			&add_filetype($allfiles,$attr->{$item},$item);
                   3905: 			last;
                   3906: 		    }
                   3907: 		}
1.1164    raeburn  3908:                 if (lc($tagname) eq 'embed') {
                   3909:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   3910:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   3911:                                              $attr->{'src'});
                   3912:                     }
                   3913:                 }
1.640     albertel 3914: 	    }
1.1243    raeburn  3915:             if (lc($tagname) eq 'iframe') {
                   3916:                 my $src = $attr->{'src'} ;
                   3917:                 if (($src ne '') && ($src !~ m{^(/|https?://)})) {
                   3918:                     &add_filetype($allfiles,$src,'src');
                   3919:                 } elsif ($src =~ m{^/}) {
                   3920:                     if ($env{'request.course.id'}) {
                   3921:                         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3922:                         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3923:                         my $url = &hreflocation('',$fullpath);
                   3924:                         if ($url =~ m{^/uploaded/$cdom/$cnum/docs/(\w+/\d+)/}) {
                   3925:                             my $relpath = $1;
                   3926:                             if ($src =~ m{^/uploaded/$cdom/$cnum/docs/\Q$relpath\E/(.+)$}) {
                   3927:                                 &add_filetype($allfiles,$1,'src');
                   3928:                             }
                   3929:                         }
                   3930:                     }
                   3931:                 }
                   3932:             }
1.1164    raeburn  3933:             if ($t->[4] =~ m{/>$}) {
1.1243    raeburn  3934:                 pop(@state);
1.1164    raeburn  3935:             }
1.640     albertel 3936: 	} elsif ($t->[0] eq 'E') {
                   3937: 	    my ($tagname) = ($t->[1]);
                   3938: 	    if ($javafiles{'codebase'} ne '') {
                   3939: 		$javafiles{'codebase'} .= '/';
                   3940: 	    }  
                   3941: 	    if (lc($tagname) eq 'applet' ||
                   3942: 		lc($tagname) eq 'object' ||
                   3943: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   3944: 		) {
                   3945: 		foreach my $item (keys(%javafiles)) {
                   3946: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   3947: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   3948: 			&add_filetype($allfiles,$file,$item);
                   3949: 		    }
                   3950: 		}
                   3951: 	    } 
                   3952: 	    pop @state;
                   3953: 	}
                   3954:     }
1.1164    raeburn  3955:     foreach my $id (sort(keys(%flashvars))) {
                   3956:         if ($shockwave{$id} ne '') {
                   3957:             my @pairs = split(/\&/,$flashvars{$id});
                   3958:             foreach my $pair (@pairs) {
                   3959:                 my ($key,$value) = split(/\=/,$pair);
                   3960:                 if ($key eq 'thumb') {
                   3961:                     &add_filetype($allfiles,$value,$key);
                   3962:                 } elsif ($key eq 'content') {
                   3963:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   3964:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   3965:                     if ($ext ne '') {
                   3966:                         &add_filetype($allfiles,$path.$value,$ext);
                   3967:                     }
                   3968:                 }
                   3969:             }
                   3970:         }
                   3971:     }
1.637     raeburn  3972:     return 'ok';
                   3973: }
                   3974: 
1.639     albertel 3975: sub add_filetype {
                   3976:     my ($allfiles,$file,$type)=@_;
                   3977:     if (exists($allfiles->{$file})) {
                   3978: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   3979: 	    push(@{$allfiles->{$file}}, &escape($type));
                   3980: 	}
                   3981:     } else {
                   3982: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  3983:     }
                   3984: }
                   3985: 
1.1164    raeburn  3986: sub embedded_dependency {
                   3987:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   3988:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   3989:         if (($identifier ne '') &&
                   3990:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   3991:             ($pathfrom ne '')) {
                   3992:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   3993:             foreach my $dep (@{$related->{$identifier}}) {
                   3994:                 &add_filetype($allfiles,$path.$dep,'object');
                   3995:             }
                   3996:         }
                   3997:     }
                   3998:     return;
                   3999: }
                   4000: 
1.493     albertel 4001: sub removeuploadedurl {
1.984     neumanie 4002:     my ($url)=@_;	
                   4003:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 4004:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 4005: }
                   4006: 
                   4007: sub removeuserfile {
                   4008:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 4009:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  4010:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 4011:     if ($result eq 'ok') {	
1.798     raeburn  4012:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   4013:             my $metafile = $fname.'.meta';
                   4014:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 4015: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 4016:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  4017:             my $sqlresult = 
1.823     albertel 4018:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4019:                                         'portfolio_metadata',$group,
                   4020:                                         'delete');
1.798     raeburn  4021:         }
                   4022:     }
                   4023:     return $result;
1.257     www      4024: }
1.15      www      4025: 
1.530     albertel 4026: sub mkdiruserfile {
                   4027:     my ($docuname,$docudom,$dir)=@_;
                   4028:     my $home=&homeserver($docuname,$docudom);
                   4029:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   4030: }
                   4031: 
1.531     albertel 4032: sub renameuserfile {
                   4033:     my ($docuname,$docudom,$old,$new)=@_;
                   4034:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  4035:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   4036:                         &escape("$old").':'.&escape("$new"),$home);
                   4037:     if ($result eq 'ok') {
                   4038:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   4039:             my $oldmeta = $old.'.meta';
                   4040:             my $newmeta = $new.'.meta';
                   4041:             my $metaresult = 
                   4042:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 4043: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   4044:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  4045:             my $sqlresult = 
1.823     albertel 4046:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  4047:                                         'portfolio_metadata',$group,
                   4048:                                         'delete');
1.798     raeburn  4049:         }
                   4050:     }
                   4051:     return $result;
1.531     albertel 4052: }
                   4053: 
1.14      www      4054: # ------------------------------------------------------------------------- Log
                   4055: 
                   4056: sub log {
                   4057:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      4058:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      4059: }
                   4060: 
                   4061: # ------------------------------------------------------------------ Course Log
1.352     www      4062: #
                   4063: # This routine flushes several buffers of non-mission-critical nature
                   4064: #
1.157     www      4065: 
                   4066: sub flushcourselogs {
1.352     www      4067:     &logthis('Flushing log buffers');
                   4068: #
                   4069: # course logs
                   4070: # This is a log of all transactions in a course, which can be used
                   4071: # for data mining purposes
                   4072: #
                   4073: # It also collects the courseid database, which lists last transaction
                   4074: # times and course titles for all courseids
                   4075: #
                   4076:     my %courseidbuffer=();
1.921     raeburn  4077:     foreach my $crsid (keys(%courselogs)) {
1.352     www      4078:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      4079: 		          &escape($courselogs{$crsid}),
                   4080: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      4081: 	    delete $courselogs{$crsid};
                   4082:         } else {
                   4083:             &logthis('Failed to flush log buffer for '.$crsid);
                   4084:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 4085:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      4086:                         " exceeded maximum size, deleting.</font>");
                   4087:                delete $courselogs{$crsid};
                   4088:             }
1.352     www      4089:         }
1.920     raeburn  4090:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  4091:             'description' => $coursedescrbuf{$crsid},
                   4092:             'inst_code'    => $courseinstcodebuf{$crsid},
                   4093:             'type'        => $coursetypebuf{$crsid},
                   4094:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  4095:         };
1.191     harris41 4096:     }
1.352     www      4097: #
                   4098: # Write course id database (reverse lookup) to homeserver of courses 
                   4099: # Is used in pickcourse
                   4100: #
1.840     albertel 4101:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  4102:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  4103:                                     $courseidbuffer{$crs_home},
                   4104:                                     $crs_home,'timeonly');
1.352     www      4105:     }
                   4106: #
                   4107: # File accesses
                   4108: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   4109: #
1.449     matthew  4110:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  4111:         if ($entry =~ /___count$/) {
                   4112:             my ($dom,$name);
1.807     albertel 4113:             ($dom,$name,undef)=
1.811     albertel 4114: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  4115:             if (! defined($dom) || $dom eq '' || 
                   4116:                 ! defined($name) || $name eq '') {
1.620     albertel 4117:                 my $cid = $env{'request.course.id'};
                   4118:                 $dom  = $env{'request.'.$cid.'.domain'};
                   4119:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  4120:             }
1.450     matthew  4121:             my $value = $accesshash{$entry};
                   4122:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   4123:             my %temphash=($url => $value);
1.449     matthew  4124:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   4125:             if ($result eq 'ok') {
                   4126:                 delete $accesshash{$entry};
                   4127:             }
                   4128:         } else {
1.811     albertel 4129:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      4130:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  4131:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  4132:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   4133:                 delete $accesshash{$entry};
                   4134:             }
1.185     www      4135:         }
1.191     harris41 4136:     }
1.352     www      4137: #
                   4138: # Roles
                   4139: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   4140: #
1.800     albertel 4141:     foreach my $entry (keys(%userrolehash)) {
1.351     www      4142:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      4143: 	    split(/\:/,$entry);
                   4144:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      4145:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      4146:                 $rudom,$runame) eq 'ok') {
                   4147: 	    delete $userrolehash{$entry};
                   4148:         }
                   4149:     }
1.662     raeburn  4150: #
1.1334    raeburn  4151: # Reverse lookup of domain roles (dc, ad, li, sc, dh, da, au)
1.662     raeburn  4152: #
                   4153:     my %domrolebuffer = ();
1.1000    raeburn  4154:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 4155:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  4156:         if ($domrolebuffer{$rudom}) {
                   4157:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   4158:                       '='.&escape($domainrolehash{$entry});
                   4159:         } else {
                   4160:             $domrolebuffer{$rudom}.=&escape($entry).
                   4161:                       '='.&escape($domainrolehash{$entry});
                   4162:         }
                   4163:         delete $domainrolehash{$entry};
                   4164:     }
                   4165:     foreach my $dom (keys(%domrolebuffer)) {
1.1324    raeburn  4166: 	my %servers;
                   4167: 	if (defined(&domain($dom,'primary'))) {
                   4168: 	    my $primary=&domain($dom,'primary');
                   4169: 	    my $hostname=&hostname($primary);
                   4170: 	    $servers{$primary} = $hostname;
                   4171: 	} else { 
                   4172: 	    %servers = &get_servers($dom,'library');
                   4173: 	}
1.841     albertel 4174: 	foreach my $tryserver (keys(%servers)) {
1.1324    raeburn  4175: 	    if (&reply('domroleput:'.$dom.':'.
                   4176: 		       $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   4177: 		last;
                   4178: 	    } else {  
1.841     albertel 4179: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   4180: 	    }
1.662     raeburn  4181:         }
                   4182:     }
1.186     www      4183:     $dumpcount++;
1.157     www      4184: }
                   4185: 
                   4186: sub courselog {
                   4187:     my $what=shift;
1.158     www      4188:     $what=time.':'.$what;
1.620     albertel 4189:     unless ($env{'request.course.id'}) { return ''; }
                   4190:     $coursedombuf{$env{'request.course.id'}}=
                   4191:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   4192:     $coursenumbuf{$env{'request.course.id'}}=
                   4193:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   4194:     $coursehombuf{$env{'request.course.id'}}=
                   4195:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   4196:     $coursedescrbuf{$env{'request.course.id'}}=
                   4197:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   4198:     $courseinstcodebuf{$env{'request.course.id'}}=
                   4199:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   4200:     $courseownerbuf{$env{'request.course.id'}}=
                   4201:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  4202:     $coursetypebuf{$env{'request.course.id'}}=
                   4203:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 4204:     if (defined $courselogs{$env{'request.course.id'}}) {
                   4205: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      4206:     } else {
1.620     albertel 4207: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      4208:     }
1.620     albertel 4209:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      4210: 	&flushcourselogs();
                   4211:     }
1.158     www      4212: }
                   4213: 
                   4214: sub courseacclog {
                   4215:     my $fnsymb=shift;
1.620     albertel 4216:     unless ($env{'request.course.id'}) { return ''; }
                   4217:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      4218:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      4219:         $what.=':POST';
1.583     matthew  4220:         # FIXME: Probably ought to escape things....
1.800     albertel 4221: 	foreach my $key (keys(%env)) {
                   4222:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  4223:                 my $formitem = $1;
                   4224:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   4225:                     $what.=':'.$formitem.'='.$env{$key};
                   4226:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
                   4227:                     $what.=':'.$formitem.'='.$env{$key};
                   4228:                 }
1.158     www      4229:             }
1.191     harris41 4230:         }
1.583     matthew  4231:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   4232:         # FIXME: We should not be depending on a form parameter that someone
                   4233:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 4234:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  4235:             $what.= ':POST';
                   4236:             # FIXME: Probably ought to escape things....
                   4237:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   4238:                                  'crsdiscuss') {
1.620     albertel 4239:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  4240:             }
                   4241:         }
1.158     www      4242:     }
                   4243:     &courselog($what);
1.149     www      4244: }
                   4245: 
1.185     www      4246: sub countacc {
                   4247:     my $url=&declutter(shift);
1.458     matthew  4248:     return if (! defined($url) || $url eq '');
1.620     albertel 4249:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      4250: #
                   4251: # Mark that this url was used in this course
                   4252: #
1.620     albertel 4253:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      4254: #
                   4255: # Increase the access count for this resource in this child process
                   4256: #
1.281     www      4257:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  4258:     $accesshash{$key}++;
1.185     www      4259: }
1.349     www      4260: 
1.361     www      4261: sub linklog {
                   4262:     my ($from,$to)=@_;
                   4263:     $from=&declutter($from);
                   4264:     $to=&declutter($to);
                   4265:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   4266:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   4267: }
1.1160    www      4268: 
                   4269: sub statslog {
                   4270:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   4271:     if ($users<2) { return; }
                   4272:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   4273:             'course'       => $env{'request.course.id'},
                   4274:             'sections'     => '"all"',
                   4275:             'num_students' => $users,
                   4276:             'part'         => $part,
                   4277:             'symb'         => $symb,
                   4278:             'mean_tries'   => $av_attempts,
                   4279:             'deg_of_diff'  => $degdiff});
                   4280:     foreach my $key (keys(%dynstore)) {
                   4281:         $accesshash{$key}=$dynstore{$key};
                   4282:     }
                   4283: }
1.361     www      4284:   
1.349     www      4285: sub userrolelog {
                   4286:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 4287:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      4288:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   4289:        $userrolehash
                   4290:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      4291:                     =$tend.':'.$tstart;
1.662     raeburn  4292:     }
1.1169    droeschl 4293:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 4294:        $userrolehash
                   4295:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   4296:                     =$tend.':'.$tstart;
                   4297:     }
1.1334    raeburn  4298:     if ($trole =~ /^(dc|ad|li|au|dg|sc|dh|da)/ ) {
1.662     raeburn  4299:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   4300:        $domainrolehash
                   4301:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   4302:                     = $tend.':'.$tstart;
                   4303:     }
1.351     www      4304: }
                   4305: 
1.957     raeburn  4306: sub courserolelog {
                   4307:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184    raeburn  4308:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   4309:         my $cdom = $1;
                   4310:         my $cnum = $2;
                   4311:         my $sec = $3;
                   4312:         my $namespace = 'rolelog';
                   4313:         my %storehash = (
                   4314:                            role    => $trole,
                   4315:                            start   => $tstart,
                   4316:                            end     => $tend,
                   4317:                            selfenroll => $selfenroll,
                   4318:                            context    => $context,
                   4319:                         );
                   4320:         if ($trole eq 'gr') {
                   4321:             $namespace = 'groupslog';
                   4322:             $storehash{'group'} = $sec;
                   4323:         } else {
                   4324:             $storehash{'section'} = $sec;
                   4325:         }
1.1188    raeburn  4326:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   4327:                    $domain,$cnum,$cdom);
1.1184    raeburn  4328:         if (($trole ne 'st') || ($sec ne '')) {
                   4329:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  4330:         }
                   4331:     }
                   4332:     return;
                   4333: }
                   4334: 
1.1184    raeburn  4335: sub domainrolelog {
                   4336:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   4337:     if ($area =~ m{^/($match_domain)/$}) {
                   4338:         my $cdom = $1;
                   4339:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   4340:         my $namespace = 'rolelog';
                   4341:         my %storehash = (
                   4342:                            role    => $trole,
                   4343:                            start   => $tstart,
                   4344:                            end     => $tend,
                   4345:                            context => $context,
                   4346:                         );
1.1188    raeburn  4347:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   4348:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  4349:     }
                   4350:     return;
                   4351: 
                   4352: }
                   4353: 
                   4354: sub coauthorrolelog {
                   4355:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   4356:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   4357:         my $audom = $1;
                   4358:         my $auname = $2;
                   4359:         my $namespace = 'rolelog';
                   4360:         my %storehash = (
                   4361:                            role    => $trole,
                   4362:                            start   => $tstart,
                   4363:                            end     => $tend,
                   4364:                            context => $context,
                   4365:                         );
1.1188    raeburn  4366:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   4367:                    $domain,$auname,$audom);
1.1184    raeburn  4368:     }
                   4369:     return;
                   4370: }
                   4371: 
1.351     www      4372: sub get_course_adv_roles {
1.948     raeburn  4373:     my ($cid,$codes) = @_;
1.620     albertel 4374:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      4375:     my %coursehash=&coursedescription($cid);
1.988     raeburn  4376:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      4377:     my %nothide=();
1.800     albertel 4378:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  4379:         if ($user !~ /:/) {
                   4380: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   4381:         } else {
                   4382:             $nothide{$user}=1;
                   4383:         }
1.470     www      4384:     }
1.1219    raeburn  4385:     my @possdoms = ($coursehash{'domain'});
                   4386:     if ($coursehash{'checkforpriv'}) {
                   4387:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   4388:     }
1.351     www      4389:     my %returnhash=();
                   4390:     my %dumphash=
                   4391:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   4392:     my $now=time;
1.997     raeburn  4393:     my %privileged;
1.1000    raeburn  4394:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 4395: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      4396:         if (($tstart) && ($tstart<0)) { next; }
                   4397:         if (($tend) && ($tend<$now)) { next; }
                   4398:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 4399:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 4400: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  4401:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  4402:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 4403: 	if ($role eq 'cr') { next; }
1.948     raeburn  4404:         if ($codes) {
                   4405:             if ($section) { $role .= ':'.$section; }
                   4406:             if ($returnhash{$role}) {
                   4407:                 $returnhash{$role}.=','.$username.':'.$domain;
                   4408:             } else {
                   4409:                 $returnhash{$role}=$username.':'.$domain;
                   4410:             }
1.351     www      4411:         } else {
1.988     raeburn  4412:             my $key=&plaintext($role,$crstype);
1.973     bisitz   4413:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  4414:             if ($returnhash{$key}) {
                   4415: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   4416:             } else {
                   4417:                 $returnhash{$key}=$username.':'.$domain;
                   4418:             }
1.351     www      4419:         }
1.948     raeburn  4420:     }
1.400     www      4421:     return %returnhash;
                   4422: }
                   4423: 
                   4424: sub get_my_roles {
1.937     raeburn  4425:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 4426:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   4427:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  4428:     my (%dumphash,%nothide);
1.1086    raeburn  4429:     if ($context eq 'userroles') {
1.1166    raeburn  4430:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  4431:     } else {
1.1219    raeburn  4432:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  4433:         if ($hidepriv) {
                   4434:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   4435:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   4436:                 if ($user !~ /:/) {
                   4437:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   4438:                 } else {
                   4439:                     $nothide{$user} = 1;
                   4440:                 }
                   4441:             }
                   4442:         }
1.858     raeburn  4443:     }
1.400     www      4444:     my %returnhash=();
                   4445:     my $now=time;
1.999     raeburn  4446:     my %privileged;
1.800     albertel 4447:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  4448:         my ($role,$tend,$tstart);
                   4449:         if ($context eq 'userroles') {
1.1149    raeburn  4450:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  4451: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   4452:         } else {
                   4453:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   4454:         }
1.400     www      4455:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  4456:         my $status = 'active';
1.939     raeburn  4457:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  4458:             $status = 'previous';
                   4459:         } 
                   4460:         if (($tstart) && ($now<$tstart)) {
                   4461:             $status = 'future';
                   4462:         }
                   4463:         if (ref($types) eq 'ARRAY') {
                   4464:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   4465:                 next;
                   4466:             } 
                   4467:         } else {
                   4468:             if ($status ne 'active') {
                   4469:                 next;
                   4470:             }
                   4471:         }
1.867     raeburn  4472:         my ($rolecode,$username,$domain,$section,$area);
                   4473:         if ($context eq 'userroles') {
1.1186    raeburn  4474:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  4475:             (undef,$domain,$username,$section) = split(/\//,$area);
                   4476:         } else {
                   4477:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   4478:         }
1.832     raeburn  4479:         if (ref($roledoms) eq 'ARRAY') {
                   4480:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   4481:                 next;
                   4482:             }
                   4483:         }
                   4484:         if (ref($roles) eq 'ARRAY') {
                   4485:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  4486:                 if ($role =~ /^cr\//) {
                   4487:                     if (!grep(/^cr$/,@{$roles})) {
                   4488:                         next;
                   4489:                     }
1.1104    raeburn  4490:                 } elsif ($role =~ /^gr\//) {
                   4491:                     if (!grep(/^gr$/,@{$roles})) {
                   4492:                         next;
                   4493:                     }
1.922     raeburn  4494:                 } else {
                   4495:                     next;
                   4496:                 }
1.832     raeburn  4497:             }
1.867     raeburn  4498:         }
1.937     raeburn  4499:         if ($hidepriv) {
1.1219    raeburn  4500:             my @privroles = ('dc','su');
1.999     raeburn  4501:             if ($context eq 'userroles') {
1.1219    raeburn  4502:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  4503:             } else {
1.1219    raeburn  4504:                 my $possdoms = [$domain];
                   4505:                 if (ref($roledoms) eq 'ARRAY') {
                   4506:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  4507:                 }
1.1219    raeburn  4508:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  4509:                     if (!$nothide{$username.':'.$domain}) {
                   4510:                         next;
                   4511:                     }
                   4512:                 }
1.937     raeburn  4513:             }
                   4514:         }
1.933     raeburn  4515:         if ($withsec) {
                   4516:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   4517:                 $tstart.':'.$tend;
                   4518:         } else {
                   4519:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   4520:         }
1.832     raeburn  4521:     }
1.373     www      4522:     return %returnhash;
1.399     www      4523: }
                   4524: 
1.1333    raeburn  4525: sub get_all_adhocroles {
                   4526:     my ($dom) = @_;
                   4527:     my @roles_by_num = ();
                   4528:     my %domdefaults = &get_domain_defaults($dom);
                   4529:     my (%description,%access_in_dom,%access_info);
                   4530:     if (ref($domdefaults{'adhocroles'}) eq 'HASH') {
                   4531:         my $count = 0;
                   4532:         my %domcurrent = %{$domdefaults{'adhocroles'}};
                   4533:         my %ordered;
                   4534:         foreach my $role (sort(keys(%domcurrent))) {
                   4535:             my ($order,$desc,$access_in_dom);
                   4536:             if (ref($domcurrent{$role}) eq 'HASH') {
                   4537:                 $order = $domcurrent{$role}{'order'};
                   4538:                 $desc = $domcurrent{$role}{'desc'};
                   4539:                 $access_in_dom{$role} = $domcurrent{$role}{'access'};
                   4540:                 $access_info{$role} = $domcurrent{$role}{$access_in_dom{$role}};
                   4541:             }
                   4542:             if ($order eq '') {
                   4543:                 $order = $count;
                   4544:             }
                   4545:             $ordered{$order} = $role;
                   4546:             if ($desc ne '') {
                   4547:                 $description{$role} = $desc;
                   4548:             } else {
                   4549:                 $description{$role}= $role;
                   4550:             }
                   4551:             $count++;
                   4552:         }
                   4553:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
                   4554:             push(@roles_by_num,$ordered{$item});
                   4555:         }
                   4556:     }
                   4557:     return (\@roles_by_num,\%description,\%access_in_dom,\%access_info);
                   4558: }
                   4559: 
1.1332    raeburn  4560: sub get_my_adhocroles {
1.1333    raeburn  4561:     my ($cid,$checkreg) = @_;
                   4562:     my ($cdom,$cnum,%info,@possroles,$description,$roles_by_num);
                   4563:     if ($env{'request.course.id'} eq $cid) {
                   4564:         $cdom = $env{'course.'.$cid.'.domain'};
                   4565:         $cnum = $env{'course.'.$cid.'.num'};
                   4566:         $info{'internal.coursecode'} = $env{'course.'.$cid.'.internal.coursecode'};
                   4567:     } elsif ($cid =~ /^($match_domain)_($match_courseid)$/) {
                   4568:         $cdom = $1;
                   4569:         $cnum = $2;
                   4570:         %info = &Apache::lonnet::get('environment',['internal.coursecode'],
                   4571:                                      $cdom,$cnum);
                   4572:     }
                   4573:     if (($info{'internal.coursecode'} ne '') && ($checkreg)) {
                   4574:         my $user = $env{'user.name'}.':'.$env{'user.domain'};
                   4575:         my %rosterhash = &get('classlist',[$user],$cdom,$cnum);
                   4576:         if ($rosterhash{$user} ne '') {
                   4577:             my $type = (split(/:/,$rosterhash{$user}))[5];
                   4578:             return ([],{}) if ($type eq 'auto');
                   4579:         }
                   4580:     }
                   4581:     if (($cdom ne '') && ($cnum ne ''))  {
1.1334    raeburn  4582:         if (($env{"user.role.dh./$cdom/"}) || ($env{"user.role.da./$cdom/"})) {
1.1332    raeburn  4583:             my $then=$env{'user.login.time'};
                   4584:             my $update=$env{'user.update.time'};
1.1335    raeburn  4585:             if (!$update) {
                   4586:                 $update = $then;
                   4587:             }
                   4588:             my @liveroles;
1.1334    raeburn  4589:             foreach my $role ('dh','da') {
                   4590:                 if ($env{"user.role.$role./$cdom/"}) {
1.1335    raeburn  4591:                     my ($tstart,$tend)=split(/\./,$env{"user.role.$role./$cdom/"});
1.1334    raeburn  4592:                     my $limit = $update;
                   4593:                     if ($env{'request.role'} eq "$role./$cdom/") {
                   4594:                         $limit = $then;
                   4595:                     }
1.1335    raeburn  4596:                     my $activerole = 1;
                   4597:                     if ($tstart && $tstart>$limit) { $activerole = 0; }
                   4598:                     if ($tend   && $tend  <$limit) { $activerole = 0; }
                   4599:                     if ($activerole) {
                   4600:                         push(@liveroles,$role);
                   4601:                     }
1.1334    raeburn  4602:                 }
1.1332    raeburn  4603:             }
1.1335    raeburn  4604:             if (@liveroles) {
1.1332    raeburn  4605:                 if (&homeserver($cnum,$cdom) ne 'no_host') {
1.1333    raeburn  4606:                     my ($accessref,$accessinfo,%access_in_dom);
                   4607:                     ($roles_by_num,$description,$accessref,$accessinfo) = &get_all_adhocroles($cdom);
                   4608:                     if (ref($roles_by_num) eq 'ARRAY') {
                   4609:                         if (@{$roles_by_num}) {
1.1332    raeburn  4610:                             my %settings;
                   4611:                             if ($env{'request.course.id'} eq $cid) {
                   4612:                                 foreach my $envkey (keys(%env)) {
                   4613:                                     if ($envkey =~ /^\Qcourse.$cid.\E(internal\.adhoc.+)$/) {
                   4614:                                         $settings{$1} = $env{$envkey};
                   4615:                                     }
                   4616:                                 }
                   4617:                             } else {
                   4618:                                 %settings = &dump('environment',$cdom,$cnum,'internal\.adhoc');
                   4619:                             }
                   4620:                             my %setincrs;
                   4621:                             if ($settings{'internal.adhocaccess'}) {
                   4622:                                 map { $setincrs{$_} = 1; } split(/,/,$settings{'internal.adhocaccess'});
                   4623:                             }
                   4624:                             my @statuses;
                   4625:                             if ($env{'environment.inststatus'}) {
                   4626:                                 @statuses = split(/,/,$env{'environment.inststatus'});
                   4627:                             }
                   4628:                             my $user = $env{'user.name'}.':'.$env{'user.domain'};
1.1333    raeburn  4629:                             if (ref($accessref) eq 'HASH') {
                   4630:                                 %access_in_dom = %{$accessref};
                   4631:                             }
                   4632:                             foreach my $role (@{$roles_by_num}) {
1.1332    raeburn  4633:                                 my ($curraccess,@okstatus,@personnel);
                   4634:                                 if ($setincrs{$role}) {
                   4635:                                     ($curraccess,my $rest) = split(/=/,$settings{'internal.adhoc.'.$role});
                   4636:                                     if ($curraccess eq 'status') {
                   4637:                                         @okstatus = split(/\&/,$rest);
                   4638:                                     } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   4639:                                         @personnel = split(/\&/,$rest);
                   4640:                                     }
                   4641:                                 } else {
                   4642:                                     $curraccess = $access_in_dom{$role};
1.1333    raeburn  4643:                                     if (ref($accessinfo) eq 'HASH') {
                   4644:                                         if ($curraccess eq 'status') {
                   4645:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   4646:                                                 @okstatus = @{$accessinfo->{$role}};
                   4647:                                             }
                   4648:                                         } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   4649:                                             if (ref($accessinfo->{$role}) eq 'ARRAY') {
                   4650:                                                 @personnel = @{$accessinfo->{$role}};
                   4651:                                             }
1.1332    raeburn  4652:                                         }
                   4653:                                     }
                   4654:                                 }
                   4655:                                 if ($curraccess eq 'none') {
                   4656:                                     next;
                   4657:                                 } elsif ($curraccess eq 'all') {
                   4658:                                     push(@possroles,$role);
1.1336    raeburn  4659:                                 } elsif ($curraccess eq 'dh') {
1.1335    raeburn  4660:                                     if (grep(/^dh$/,@liveroles)) {
                   4661:                                         push(@possroles,$role);
                   4662:                                     } else {
                   4663:                                         next;
                   4664:                                     }
1.1336    raeburn  4665:                                 } elsif ($curraccess eq 'da') {
1.1335    raeburn  4666:                                     if (grep(/^da$/,@liveroles)) {
                   4667:                                         push(@possroles,$role);
                   4668:                                     } else {
                   4669:                                         next;
                   4670:                                     }
1.1332    raeburn  4671:                                 } elsif ($curraccess eq 'status') {
                   4672:                                     if (@okstatus) {
                   4673:                                         if (!@statuses) {
                   4674:                                             if (grep(/^default$/,@okstatus)) {
                   4675:                                                 push(@possroles,$role);
                   4676:                                             }
                   4677:                                         } else {
                   4678:                                             foreach my $status (@okstatus) {
                   4679:                                                 if (grep(/^\Q$status\E$/,@statuses)) {
                   4680:                                                     push(@possroles,$role);
                   4681:                                                     last;
                   4682:                                                 }
                   4683:                                             }
                   4684:                                         }
                   4685:                                     }
                   4686:                                 } elsif (($curraccess eq 'exc') || ($curraccess eq 'inc')) {
                   4687:                                     if (grep(/^\Q$user\E$/,@personnel)) {
                   4688:                                         if ($curraccess eq 'exc') {
                   4689:                                             push(@possroles,$role);
                   4690:                                         }
                   4691:                                     } elsif ($curraccess eq 'inc') {
                   4692:                                         push(@possroles,$role);
                   4693:                                     }
                   4694:                                 }
                   4695:                             }
                   4696:                         }
                   4697:                     }
                   4698:                 }
                   4699:             }
                   4700:         }
                   4701:     }
1.1333    raeburn  4702:     unless (ref($description) eq 'HASH') {
                   4703:         if (ref($roles_by_num) eq 'ARRAY') {
                   4704:             my %desc;
                   4705:             map { $desc{$_} = $_; } (@{$roles_by_num});
                   4706:             $description = \%desc;
                   4707:         } else {
                   4708:             $description = {};
                   4709:         }
                   4710:     }
                   4711:     return (\@possroles,$description);
1.1332    raeburn  4712: }
                   4713: 
1.399     www      4714: # ----------------------------------------------------- Frontpage Announcements
                   4715: #
                   4716: #
                   4717: 
                   4718: sub postannounce {
                   4719:     my ($server,$text)=@_;
1.844     albertel 4720:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      4721:     unless ($text=~/\w/) { $text=''; }
                   4722:     return &reply('setannounce:'.&escape($text),$server);
                   4723: }
                   4724: 
                   4725: sub getannounce {
1.448     albertel 4726: 
                   4727:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      4728: 	my $announcement='';
1.800     albertel 4729: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 4730: 	close($fh);
1.399     www      4731: 	if ($announcement=~/\w/) { 
                   4732: 	    return 
                   4733:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 4734:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      4735: 	} else {
                   4736: 	    return '';
                   4737: 	}
                   4738:     } else {
                   4739: 	return '';
                   4740:     }
1.351     www      4741: }
1.353     www      4742: 
                   4743: # ---------------------------------------------------------- Course ID routines
                   4744: # Deal with domain's nohist_courseid.db files
                   4745: #
                   4746: 
                   4747: sub courseidput {
1.921     raeburn  4748:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  4749:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  4750:     my $outcome;
                   4751:     if ($caller eq 'timeonly') {
                   4752:         my $cids = '';
                   4753:         foreach my $item (keys(%$storehash)) {
                   4754:             $cids.=&escape($item).'&';
                   4755:         }
                   4756:         $cids=~s/\&$//;
                   4757:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   4758:                           $coursehome);       
                   4759:     } else {
                   4760:         my $items = '';
                   4761:         foreach my $item (keys(%$storehash)) {
                   4762:             $items.= &escape($item).'='.
                   4763:                      &freeze_escape($$storehash{$item}).'&';
                   4764:         }
                   4765:         $items=~s/\&$//;
                   4766:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   4767:                           $coursehome);
1.918     raeburn  4768:     }
                   4769:     if ($outcome eq 'unknown_cmd') {
                   4770:         my $what;
                   4771:         foreach my $cid (keys(%$storehash)) {
                   4772:             $what .= &escape($cid).'=';
1.921     raeburn  4773:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  4774:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  4775:             }
                   4776:             $what =~ s/\:$/&/;
                   4777:         }
                   4778:         $what =~ s/\&$//;  
                   4779:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   4780:     } else {
                   4781:         return $outcome;
                   4782:     }
1.353     www      4783: }
                   4784: 
                   4785: sub courseiddump {
1.921     raeburn  4786:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  4787:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  4788:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1247    raeburn  4789:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner,
1.1287    raeburn  4790:         $hasuniquecode,$reqcrsdom,$reqinstcode)=@_;
1.918     raeburn  4791:     my $as_hash = 1;
                   4792:     my %returnhash;
                   4793:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 4794:     my %libserv = &all_library();
                   4795:     foreach my $tryserver (keys(%libserv)) {
                   4796:         if ( (  $hostidflag == 1 
                   4797: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   4798: 	     || (!defined($hostidflag)) ) {
                   4799: 
1.918     raeburn  4800: 	    if (($domfilter eq '') ||
                   4801: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 4802:                 my $rep;
                   4803:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   4804:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   4805:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   4806:                                 &escape($descfilter), &escape($instcodefilter), 
                   4807:                                 &escape($ownerfilter), &escape($coursefilter),
                   4808:                                 &escape($typefilter), &escape($regexp_ok), 
                   4809:                                 $as_hash, &escape($selfenrollonly), 
                   4810:                                 &escape($catfilter), $showhidden, $caller, 
                   4811:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   4812:                                 &escape($createdbefore), &escape($createdafter), 
1.1287    raeburn  4813:                                 &escape($creationcontext),$domcloner,$hasuniquecode,
                   4814:                                 $reqcrsdom,&escape($reqinstcode))));
1.1180    droeschl 4815:                 } else {
                   4816:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   4817:                              $sincefilter.':'.&escape($descfilter).':'.
                   4818:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   4819:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   4820:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   4821:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   4822:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   4823:                              &escape($cc_clone).':'.$cloneonly.':'.
                   4824:                              &escape($createdbefore).':'.&escape($createdafter).':'.
1.1287    raeburn  4825:                              &escape($creationcontext).':'.$domcloner.':'.$hasuniquecode.
                   4826:                              ':'.$reqcrsdom.':'.&escape($reqinstcode),$tryserver);
1.1180    droeschl 4827:                 }
                   4828:                      
1.918     raeburn  4829:                 my @pairs=split(/\&/,$rep);
                   4830:                 foreach my $item (@pairs) {
                   4831:                     my ($key,$value)=split(/\=/,$item,2);
                   4832:                     $key = &unescape($key);
                   4833:                     next if ($key =~ /^error: 2 /);
                   4834:                     my $result = &thaw_unescape($value);
                   4835:                     if (ref($result) eq 'HASH') {
                   4836:                         $returnhash{$key}=$result;
                   4837:                     } else {
1.921     raeburn  4838:                         my @responses = split(/:/,$value);
                   4839:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  4840:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  4841:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  4842:                         }
1.1008    raeburn  4843:                     }
1.353     www      4844:                 }
                   4845:             }
                   4846:         }
                   4847:     }
                   4848:     return %returnhash;
                   4849: }
                   4850: 
1.1055    raeburn  4851: sub courselastaccess {
                   4852:     my ($cdom,$cnum,$hostidref) = @_;
                   4853:     my %returnhash;
                   4854:     if ($cdom && $cnum) {
                   4855:         my $chome = &homeserver($cnum,$cdom);
                   4856:         if ($chome ne 'no_host') {
                   4857:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   4858:             &extract_lastaccess(\%returnhash,$rep);
                   4859:         }
                   4860:     } else {
                   4861:         if (!$cdom) { $cdom=''; }
                   4862:         my %libserv = &all_library();
                   4863:         foreach my $tryserver (keys(%libserv)) {
                   4864:             if (ref($hostidref) eq 'ARRAY') {
                   4865:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   4866:             } 
                   4867:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   4868:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   4869:                 &extract_lastaccess(\%returnhash,$rep);
                   4870:             }
                   4871:         }
                   4872:     }
                   4873:     return %returnhash;
                   4874: }
                   4875: 
                   4876: sub extract_lastaccess {
                   4877:     my ($returnhash,$rep) = @_;
                   4878:     if (ref($returnhash) eq 'HASH') {
                   4879:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   4880:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   4881:                  $rep eq '') {
                   4882:             my @pairs=split(/\&/,$rep);
                   4883:             foreach my $item (@pairs) {
                   4884:                 my ($key,$value)=split(/\=/,$item,2);
                   4885:                 $key = &unescape($key);
                   4886:                 next if ($key =~ /^error: 2 /);
                   4887:                 $returnhash->{$key} = &thaw_unescape($value);
                   4888:             }
                   4889:         }
                   4890:     }
                   4891:     return;
                   4892: }
                   4893: 
1.658     raeburn  4894: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  4895: 
                   4896: sub dcmailput {
1.685     raeburn  4897:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  4898:     my $status = &Apache::lonnet::critical(
1.740     www      4899:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   4900:        &escape($message),$server);
1.662     raeburn  4901:     return $status;
                   4902: }
                   4903: 
1.658     raeburn  4904: sub dcmaildump {
                   4905:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  4906:     my %returnhash=();
1.846     albertel 4907: 
                   4908:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  4909:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   4910:                                                          &escape($enddate).':';
                   4911: 	my @esc_senders=map { &escape($_)} @$senders;
                   4912: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 4913: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 4914:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  4915:             if (($key) && ($value)) {
                   4916:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  4917:             }
                   4918:         }
                   4919:     }
                   4920:     return %returnhash;
                   4921: }
1.662     raeburn  4922: # ---------------------------------------------------------- Domain roles
                   4923: 
                   4924: sub get_domain_roles {
                   4925:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  4926:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  4927:         $startdate = '.';
                   4928:     }
1.1018    raeburn  4929:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  4930:         $enddate = '.';
                   4931:     }
1.922     raeburn  4932:     my $rolelist;
                   4933:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  4934:         $rolelist = join('&',@{$roles});
1.922     raeburn  4935:     }
1.662     raeburn  4936:     my %personnel = ();
1.841     albertel 4937: 
                   4938:     my %servers = &get_servers($dom,'library');
                   4939:     foreach my $tryserver (keys(%servers)) {
                   4940: 	%{$personnel{$tryserver}}=();
                   4941: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   4942: 					    &escape($startdate).':'.
                   4943: 					    &escape($enddate).':'.
                   4944: 					    &escape($rolelist), $tryserver))) {
                   4945: 	    my ($key,$value) = split(/\=/,$line,2);
                   4946: 	    if (($key) && ($value)) {
                   4947: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   4948: 	    }
                   4949: 	}
1.662     raeburn  4950:     }
                   4951:     return %personnel;
                   4952: }
1.658     raeburn  4953: 
1.1332    raeburn  4954: sub get_active_domroles {
                   4955:     my ($dom,$roles) = @_;
                   4956:     return () unless (ref($roles) eq 'ARRAY');
                   4957:     my $now = time;
                   4958:     my %dompersonnel = &get_domain_roles($dom,$roles,$now,$now);
                   4959:     my %domroles;
                   4960:     foreach my $server (keys(%dompersonnel)) {
                   4961:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   4962:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
                   4963:             $domroles{$uname.':'.$udom} = $dompersonnel{$server}{$user};
                   4964:         }
                   4965:     }
                   4966:     return %domroles;
                   4967: }
                   4968: 
1.1057    www      4969: # ----------------------------------------------------------- Interval timing 
1.149     www      4970: 
1.1153    www      4971: {
                   4972: # Caches needed for speedup of navmaps
                   4973: # We don't want to cache this for very long at all (5 seconds at most)
                   4974: # 
                   4975: # The user for whom we cache
                   4976: my $cachedkey='';
                   4977: # The cached times for this user
                   4978: my %cachedtimes=();
                   4979: # When this was last done
1.1282    raeburn  4980: my $cachedtime='';
1.1153    www      4981: 
                   4982: sub load_all_first_access {
1.1308    raeburn  4983:     my ($uname,$udom,$ignorecache)=@_;
1.1156    www      4984:     if (($cachedkey eq $uname.':'.$udom) &&
1.1308    raeburn  4985:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'}) &&
                   4986:         (!$ignorecache)) {
1.1154    raeburn  4987:         return;
                   4988:     }
                   4989:     $cachedtime=time;
                   4990:     $cachedkey=$uname.':'.$udom;
                   4991:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      4992: }
                   4993: 
1.504     albertel 4994: sub get_first_access {
1.1308    raeburn  4995:     my ($type,$argsymb,$argmap,$ignorecache)=@_;
1.790     albertel 4996:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 4997:     if ($argsymb) { $symb=$argsymb; }
                   4998:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  4999:     if ($argmap) { $map = $argmap; }
1.926     albertel 5000:     if ($type eq 'course') {
                   5001: 	$res='course';
                   5002:     } elsif ($type eq 'map') {
1.588     albertel 5003: 	$res=&symbread($map);
                   5004:     } else {
                   5005: 	$res=$symb;
                   5006:     }
1.1308    raeburn  5007:     &load_all_first_access($uname,$udom,$ignorecache);
1.1153    www      5008:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 5009: }
                   5010: 
                   5011: sub set_first_access {
1.1162    raeburn  5012:     my ($type,$interval)=@_;
1.790     albertel 5013:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 5014:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 5015:     if ($type eq 'course') {
                   5016: 	$res='course';
                   5017:     } elsif ($type eq 'map') {
1.588     albertel 5018: 	$res=&symbread($map);
                   5019:     } else {
                   5020: 	$res=$symb;
                   5021:     }
1.1153    www      5022:     $cachedkey='';
1.1162    raeburn  5023:     my $firstaccess=&get_first_access($type,$symb,$map);
1.505     albertel 5024:     if (!$firstaccess) {
1.1162    raeburn  5025:         my $start = time;
                   5026: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   5027:                           $udom,$uname);
                   5028:         if ($putres eq 'ok') {
                   5029:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   5030:                  $udom,$uname); 
                   5031:             &appenv(
                   5032:                      {
                   5033:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   5034:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   5035:                      }
                   5036:                   );
                   5037:         }
                   5038:         return $putres;
1.505     albertel 5039:     }
                   5040:     return 'already_set';
1.504     albertel 5041: }
1.1153    www      5042: }
1.1282    raeburn  5043: 
1.110     www      5044: # --------------------------------------------- Set Expire Date for Spreadsheet
                   5045: 
                   5046: sub expirespread {
                   5047:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 5048:     my $cid=$env{'request.course.id'}; 
1.110     www      5049:     if ($cid) {
                   5050:        my $now=time;
                   5051:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 5052:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   5053:                             $env{'course.'.$cid.'.num'}.
1.110     www      5054: 	        	    ':nohist_expirationdates:'.
                   5055:                             &escape($key).'='.$now,
1.620     albertel 5056:                             $env{'course.'.$cid.'.home'})
1.110     www      5057:     }
                   5058:     return 'ok';
1.14      www      5059: }
                   5060: 
1.109     www      5061: # ----------------------------------------------------- Devalidate Spreadsheets
                   5062: 
                   5063: sub devalidate {
1.325     www      5064:     my ($symb,$uname,$udom)=@_;
1.620     albertel 5065:     my $cid=$env{'request.course.id'}; 
1.109     www      5066:     if ($cid) {
1.391     matthew  5067:         # delete the stored spreadsheets for
                   5068:         # - the student level sheet of this user in course's homespace
                   5069:         # - the assessment level sheet for this resource 
                   5070:         #   for this user in user's homespace
1.553     albertel 5071: 	# - current conditional state info
1.325     www      5072: 	my $key=$uname.':'.$udom.':';
1.109     www      5073:         my $status=
1.299     matthew  5074: 	    &del('nohist_calculatedsheets',
1.391     matthew  5075: 		 [$key.'studentcalc:'],
1.620     albertel 5076: 		 $env{'course.'.$cid.'.domain'},
                   5077: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 5078: 		.' '.
                   5079: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  5080: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      5081:         unless ($status eq 'ok ok') {
                   5082:            &logthis('Could not devalidate spreadsheet '.
1.325     www      5083:                     $uname.' at '.$udom.' for '.
1.109     www      5084: 		    $symb.': '.$status);
1.133     albertel 5085:         }
1.553     albertel 5086: 	&delenv('user.state.'.$cid);
1.109     www      5087:     }
                   5088: }
                   5089: 
1.265     albertel 5090: sub get_scalar {
                   5091:     my ($string,$end) = @_;
                   5092:     my $value;
                   5093:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   5094: 	$value = $1;
                   5095:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   5096: 	$value = $1;
                   5097:     }
                   5098:     return &unescape($value);
                   5099: }
                   5100: 
                   5101: sub array2str {
                   5102:   my (@array) = @_;
                   5103:   my $result=&arrayref2str(\@array);
                   5104:   $result=~s/^__ARRAY_REF__//;
                   5105:   $result=~s/__END_ARRAY_REF__$//;
                   5106:   return $result;
                   5107: }
                   5108: 
1.204     albertel 5109: sub arrayref2str {
                   5110:   my ($arrayref) = @_;
1.265     albertel 5111:   my $result='__ARRAY_REF__';
1.204     albertel 5112:   foreach my $elem (@$arrayref) {
1.265     albertel 5113:     if(ref($elem) eq 'ARRAY') {
                   5114:       $result.=&arrayref2str($elem).'&';
                   5115:     } elsif(ref($elem) eq 'HASH') {
                   5116:       $result.=&hashref2str($elem).'&';
                   5117:     } elsif(ref($elem)) {
                   5118:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 5119:     } else {
                   5120:       $result.=&escape($elem).'&';
                   5121:     }
                   5122:   }
                   5123:   $result=~s/\&$//;
1.265     albertel 5124:   $result .= '__END_ARRAY_REF__';
1.204     albertel 5125:   return $result;
                   5126: }
                   5127: 
1.168     albertel 5128: sub hash2str {
1.204     albertel 5129:   my (%hash) = @_;
                   5130:   my $result=&hashref2str(\%hash);
1.265     albertel 5131:   $result=~s/^__HASH_REF__//;
                   5132:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 5133:   return $result;
                   5134: }
                   5135: 
                   5136: sub hashref2str {
                   5137:   my ($hashref)=@_;
1.265     albertel 5138:   my $result='__HASH_REF__';
1.800     albertel 5139:   foreach my $key (sort(keys(%$hashref))) {
                   5140:     if (ref($key) eq 'ARRAY') {
                   5141:       $result.=&arrayref2str($key).'=';
                   5142:     } elsif (ref($key) eq 'HASH') {
                   5143:       $result.=&hashref2str($key).'=';
                   5144:     } elsif (ref($key)) {
1.265     albertel 5145:       $result.='=';
1.800     albertel 5146:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 5147:     } else {
1.1132    raeburn  5148: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 5149:     }
                   5150: 
1.800     albertel 5151:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   5152:       $result.=&arrayref2str($hashref->{$key}).'&';
                   5153:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   5154:       $result.=&hashref2str($hashref->{$key}).'&';
                   5155:     } elsif(ref($hashref->{$key})) {
1.265     albertel 5156:        $result.='&';
1.800     albertel 5157:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 5158:     } else {
1.800     albertel 5159:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 5160:     }
                   5161:   }
1.168     albertel 5162:   $result=~s/\&$//;
1.265     albertel 5163:   $result .= '__END_HASH_REF__';
1.168     albertel 5164:   return $result;
                   5165: }
                   5166: 
                   5167: sub str2hash {
1.265     albertel 5168:     my ($string)=@_;
                   5169:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   5170:     return %$hash;
                   5171: }
                   5172: 
                   5173: sub str2hashref {
1.168     albertel 5174:   my ($string) = @_;
1.265     albertel 5175: 
                   5176:   my %hash;
                   5177: 
                   5178:   if($string !~ /^__HASH_REF__/) {
                   5179:       if (! ($string eq '' || !defined($string))) {
                   5180: 	  $hash{'error'}='Not hash reference';
                   5181:       }
                   5182:       return (\%hash, $string);
                   5183:   }
                   5184: 
                   5185:   $string =~ s/^__HASH_REF__//;
                   5186: 
                   5187:   while($string !~ /^__END_HASH_REF__/) {
                   5188:       #key
                   5189:       my $key='';
                   5190:       if($string =~ /^__HASH_REF__/) {
                   5191:           ($key, $string)=&str2hashref($string);
                   5192:           if(defined($key->{'error'})) {
                   5193:               $hash{'error'}='Bad data';
                   5194:               return (\%hash, $string);
                   5195:           }
                   5196:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5197:           ($key, $string)=&str2arrayref($string);
                   5198:           if($key->[0] eq 'Array reference error') {
                   5199:               $hash{'error'}='Bad data';
                   5200:               return (\%hash, $string);
                   5201:           }
                   5202:       } else {
                   5203:           $string =~ s/^(.*?)=//;
1.267     albertel 5204: 	  $key=&unescape($1);
1.265     albertel 5205:       }
                   5206:       $string =~ s/^=//;
                   5207: 
                   5208:       #value
                   5209:       my $value='';
                   5210:       if($string =~ /^__HASH_REF__/) {
                   5211:           ($value, $string)=&str2hashref($string);
                   5212:           if(defined($value->{'error'})) {
                   5213:               $hash{'error'}='Bad data';
                   5214:               return (\%hash, $string);
                   5215:           }
                   5216:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5217:           ($value, $string)=&str2arrayref($string);
                   5218:           if($value->[0] eq 'Array reference error') {
                   5219:               $hash{'error'}='Bad data';
                   5220:               return (\%hash, $string);
                   5221:           }
                   5222:       } else {
                   5223: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   5224:       }
                   5225:       $string =~ s/^&//;
                   5226: 
                   5227:       $hash{$key}=$value;
1.204     albertel 5228:   }
1.265     albertel 5229: 
                   5230:   $string =~ s/^__END_HASH_REF__//;
                   5231: 
                   5232:   return (\%hash, $string);
1.204     albertel 5233: }
                   5234: 
                   5235: sub str2array {
1.265     albertel 5236:     my ($string)=@_;
                   5237:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   5238:     return @$array;
                   5239: }
                   5240: 
                   5241: sub str2arrayref {
1.204     albertel 5242:   my ($string) = @_;
1.265     albertel 5243:   my @array;
                   5244: 
                   5245:   if($string !~ /^__ARRAY_REF__/) {
                   5246:       if (! ($string eq '' || !defined($string))) {
                   5247: 	  $array[0]='Array reference error';
                   5248:       }
                   5249:       return (\@array, $string);
                   5250:   }
                   5251: 
                   5252:   $string =~ s/^__ARRAY_REF__//;
                   5253: 
                   5254:   while($string !~ /^__END_ARRAY_REF__/) {
                   5255:       my $value='';
                   5256:       if($string =~ /^__HASH_REF__/) {
                   5257:           ($value, $string)=&str2hashref($string);
                   5258:           if(defined($value->{'error'})) {
                   5259:               $array[0] ='Array reference error';
                   5260:               return (\@array, $string);
                   5261:           }
                   5262:       } elsif($string =~ /^__ARRAY_REF__/) {
                   5263:           ($value, $string)=&str2arrayref($string);
                   5264:           if($value->[0] eq 'Array reference error') {
                   5265:               $array[0] ='Array reference error';
                   5266:               return (\@array, $string);
                   5267:           }
                   5268:       } else {
                   5269: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   5270:       }
                   5271:       $string =~ s/^&//;
                   5272: 
                   5273:       push(@array, $value);
1.191     harris41 5274:   }
1.265     albertel 5275: 
                   5276:   $string =~ s/^__END_ARRAY_REF__//;
                   5277: 
                   5278:   return (\@array, $string);
1.168     albertel 5279: }
                   5280: 
1.167     albertel 5281: # -------------------------------------------------------------------Temp Store
                   5282: 
1.168     albertel 5283: sub tmpreset {
                   5284:   my ($symb,$namespace,$domain,$stuname) = @_;
                   5285:   if (!$symb) {
                   5286:     $symb=&symbread();
1.620     albertel 5287:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5288:   }
                   5289:   $symb=escape($symb);
                   5290: 
1.620     albertel 5291:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 5292:   $namespace=~s/\//\_/g;
                   5293:   $namespace=~s/\W//g;
                   5294: 
1.620     albertel 5295:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5296:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5297:   if ($domain eq 'public' && $stuname eq 'public') {
                   5298:       $stuname=$ENV{'REMOTE_ADDR'};
                   5299:   }
1.1117    foxr     5300:   my $path=LONCAPA::tempdir();
1.168     albertel 5301:   my %hash;
                   5302:   if (tie(%hash,'GDBM_File',
                   5303: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5304: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  5305:     foreach my $key (keys(%hash)) {
1.180     albertel 5306:       if ($key=~ /:$symb/) {
1.168     albertel 5307: 	delete($hash{$key});
                   5308:       }
                   5309:     }
                   5310:   }
                   5311: }
                   5312: 
1.167     albertel 5313: sub tmpstore {
1.168     albertel 5314:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   5315: 
                   5316:   if (!$symb) {
                   5317:     $symb=&symbread();
1.620     albertel 5318:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5319:   }
                   5320:   $symb=escape($symb);
                   5321: 
                   5322:   if (!$namespace) {
                   5323:     # I don't think we would ever want to store this for a course.
                   5324:     # it seems this will only be used if we don't have a course.
1.620     albertel 5325:     #$namespace=$env{'request.course.id'};
1.168     albertel 5326:     #if (!$namespace) {
1.620     albertel 5327:       $namespace=$env{'request.state'};
1.168     albertel 5328:     #}
                   5329:   }
                   5330:   $namespace=~s/\//\_/g;
                   5331:   $namespace=~s/\W//g;
1.620     albertel 5332:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5333:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5334:   if ($domain eq 'public' && $stuname eq 'public') {
                   5335:       $stuname=$ENV{'REMOTE_ADDR'};
                   5336:   }
1.168     albertel 5337:   my $now=time;
                   5338:   my %hash;
1.1117    foxr     5339:   my $path=LONCAPA::tempdir();
1.168     albertel 5340:   if (tie(%hash,'GDBM_File',
                   5341: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5342: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 5343:     $hash{"version:$symb"}++;
                   5344:     my $version=$hash{"version:$symb"};
                   5345:     my $allkeys=''; 
                   5346:     foreach my $key (keys(%$storehash)) {
                   5347:       $allkeys.=$key.':';
1.591     albertel 5348:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 5349:     }
                   5350:     $hash{"$version:$symb:timestamp"}=$now;
                   5351:     $allkeys.='timestamp';
                   5352:     $hash{"$version:keys:$symb"}=$allkeys;
                   5353:     if (untie(%hash)) {
                   5354:       return 'ok';
                   5355:     } else {
                   5356:       return "error:$!";
                   5357:     }
                   5358:   } else {
                   5359:     return "error:$!";
                   5360:   }
                   5361: }
1.167     albertel 5362: 
1.168     albertel 5363: # -----------------------------------------------------------------Temp Restore
1.167     albertel 5364: 
1.168     albertel 5365: sub tmprestore {
                   5366:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 5367: 
1.168     albertel 5368:   if (!$symb) {
                   5369:     $symb=&symbread();
1.620     albertel 5370:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 5371:   }
                   5372:   $symb=escape($symb);
                   5373: 
1.620     albertel 5374:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 5375: 
1.620     albertel 5376:   if (!$domain) { $domain=$env{'user.domain'}; }
                   5377:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 5378:   if ($domain eq 'public' && $stuname eq 'public') {
                   5379:       $stuname=$ENV{'REMOTE_ADDR'};
                   5380:   }
1.168     albertel 5381:   my %returnhash;
                   5382:   $namespace=~s/\//\_/g;
                   5383:   $namespace=~s/\W//g;
                   5384:   my %hash;
1.1117    foxr     5385:   my $path=LONCAPA::tempdir();
1.168     albertel 5386:   if (tie(%hash,'GDBM_File',
                   5387: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 5388: 	  &GDBM_READER(),0640)) {
1.168     albertel 5389:     my $version=$hash{"version:$symb"};
                   5390:     $returnhash{'version'}=$version;
                   5391:     my $scope;
                   5392:     for ($scope=1;$scope<=$version;$scope++) {
                   5393:       my $vkeys=$hash{"$scope:keys:$symb"};
                   5394:       my @keys=split(/:/,$vkeys);
                   5395:       my $key;
                   5396:       $returnhash{"$scope:keys"}=$vkeys;
                   5397:       foreach $key (@keys) {
1.591     albertel 5398: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   5399: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 5400:       }
                   5401:     }
1.168     albertel 5402:     if (!(untie(%hash))) {
                   5403:       return "error:$!";
                   5404:     }
                   5405:   } else {
                   5406:     return "error:$!";
                   5407:   }
                   5408:   return %returnhash;
1.167     albertel 5409: }
                   5410: 
1.9       www      5411: # ----------------------------------------------------------------------- Store
                   5412: 
                   5413: sub store {
1.1269    raeburn  5414:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      5415:     my $home='';
                   5416: 
1.168     albertel 5417:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      5418: 
1.213     www      5419:     $symb=&symbclean($symb);
1.122     albertel 5420:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      5421: 
1.620     albertel 5422:     if (!$domain) { $domain=$env{'user.domain'}; }
                   5423:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      5424: 
                   5425:     &devalidate($symb,$stuname,$domain);
1.109     www      5426: 
                   5427:     $symb=escape($symb);
1.187     www      5428:     if (!$namespace) { 
1.620     albertel 5429:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      5430:           return ''; 
                   5431:        } 
                   5432:     }
1.620     albertel 5433:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      5434: 
                   5435:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   5436:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   5437: 
1.12      www      5438:     my $namevalue='';
1.800     albertel 5439:     foreach my $key (keys(%$storehash)) {
                   5440:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 5441:     }
1.12      www      5442:     $namevalue=~s/\&$//;
1.187     www      5443:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.1269    raeburn  5444:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.9       www      5445: }
                   5446: 
1.47      www      5447: # -------------------------------------------------------------- Critical Store
                   5448: 
                   5449: sub cstore {
1.1269    raeburn  5450:     my ($storehash,$symb,$namespace,$domain,$stuname,$laststore) = @_;
1.124     www      5451:     my $home='';
                   5452: 
1.168     albertel 5453:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      5454: 
1.213     www      5455:     $symb=&symbclean($symb);
1.122     albertel 5456:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      5457: 
1.620     albertel 5458:     if (!$domain) { $domain=$env{'user.domain'}; }
                   5459:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      5460: 
                   5461:     &devalidate($symb,$stuname,$domain);
1.109     www      5462: 
                   5463:     $symb=escape($symb);
1.187     www      5464:     if (!$namespace) { 
1.620     albertel 5465:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      5466:           return ''; 
                   5467:        } 
                   5468:     }
1.620     albertel 5469:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      5470: 
                   5471:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   5472:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 5473: 
1.47      www      5474:     my $namevalue='';
1.800     albertel 5475:     foreach my $key (keys(%$storehash)) {
                   5476:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 5477:     }
1.47      www      5478:     $namevalue=~s/\&$//;
1.187     www      5479:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      5480:     return critical
1.1269    raeburn  5481:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue:$laststore","$home");
1.47      www      5482: }
                   5483: 
1.9       www      5484: # --------------------------------------------------------------------- Restore
                   5485: 
                   5486: sub restore {
1.124     www      5487:     my ($symb,$namespace,$domain,$stuname) = @_;
                   5488:     my $home='';
                   5489: 
1.168     albertel 5490:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      5491: 
1.122     albertel 5492:     if (!$symb) {
1.1224    raeburn  5493:         return if ($namespace eq 'courserequests');
                   5494:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 5495:     } else {
1.1224    raeburn  5496:         unless ($namespace eq 'courserequests') {
                   5497:             $symb=&escape(&symbclean($symb));
                   5498:         }
1.122     albertel 5499:     }
1.188     www      5500:     if (!$namespace) { 
1.620     albertel 5501:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      5502:           return ''; 
                   5503:        } 
                   5504:     }
1.620     albertel 5505:     if (!$domain) { $domain=$env{'user.domain'}; }
                   5506:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   5507:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 5508:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   5509: 
1.12      www      5510:     my %returnhash=();
1.800     albertel 5511:     foreach my $line (split(/\&/,$answer)) {
                   5512: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 5513:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 5514:     }
1.75      www      5515:     my $version;
                   5516:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 5517:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   5518:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 5519:        }
1.75      www      5520:     }
1.13      www      5521:     return %returnhash;
1.34      www      5522: }
                   5523: 
                   5524: # ---------------------------------------------------------- Course Description
1.1118    foxr     5525: #
                   5526: #  
1.34      www      5527: 
                   5528: sub coursedescription {
1.731     albertel 5529:     my ($courseid,$args)=@_;
1.34      www      5530:     $courseid=~s/^\///;
1.49      www      5531:     $courseid=~s/\_/\//g;
1.34      www      5532:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 5533:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 5534:     my $normalid=$cdomain.'_'.$cnum;
                   5535:     # need to always cache even if we get errors otherwise we keep 
                   5536:     # trying and trying and trying to get the course description.
                   5537:     my %envhash=();
                   5538:     my %returnhash=();
1.731     albertel 5539:     
                   5540:     my $expiretime=600;
                   5541:     if ($env{'request.course.id'} eq $normalid) {
                   5542: 	$expiretime=120;
                   5543:     }
                   5544: 
                   5545:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   5546:     if (!$args->{'freshen_cache'}
                   5547: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   5548: 	foreach my $key (keys(%env)) {
                   5549: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   5550: 	    my ($setting) = $1;
                   5551: 	    $returnhash{$setting} = $env{$key};
                   5552: 	}
                   5553: 	return %returnhash;
                   5554:     }
                   5555: 
1.1118    foxr     5556:     # get the data again
                   5557: 
1.731     albertel 5558:     if (!$args->{'one_time'}) {
                   5559: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   5560:     }
1.811     albertel 5561: 
1.34      www      5562:     if ($chome ne 'no_host') {
1.302     albertel 5563:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 5564:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     5565: 	   my $username = $env{'user.name'}; # Defult username
                   5566: 	   if(defined $args->{'user'}) {
                   5567: 	       $username = $args->{'user'};
                   5568: 	   }
1.129     albertel 5569:            $returnhash{'home'}= $chome;
                   5570: 	   $returnhash{'domain'} = $cdomain;
                   5571: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  5572:            if (!defined($returnhash{'type'})) {
                   5573:                $returnhash{'type'} = 'Course';
                   5574:            }
1.130     albertel 5575:            while (my ($name,$value) = each %returnhash) {
1.53      www      5576:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 5577:            }
1.270     www      5578:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     5579:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     5580: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      5581:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   5582:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   5583:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      5584:        }
                   5585:     }
1.731     albertel 5586:     if (!$args->{'one_time'}) {
1.949     raeburn  5587: 	&appenv(\%envhash);
1.731     albertel 5588:     }
1.302     albertel 5589:     return %returnhash;
1.461     www      5590: }
                   5591: 
1.1080    raeburn  5592: sub update_released_required {
                   5593:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   5594:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   5595:         $cid = $env{'request.course.id'};
                   5596:         $cdom = $env{'course.'.$cid.'.domain'};
                   5597:         $cnum = $env{'course.'.$cid.'.num'};
                   5598:         $chome = $env{'course.'.$cid.'.home'};
                   5599:     }
                   5600:     if ($needsrelease) {
                   5601:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   5602:         my $needsupdate;
                   5603:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   5604:             $needsupdate = 1;
                   5605:         } else {
                   5606:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   5607:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   5608:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   5609:                 $needsupdate = 1;
                   5610:             }
                   5611:         }
                   5612:         if ($needsupdate) {
                   5613:             my %needshash = (
                   5614:                              'internal.releaserequired' => $needsrelease,
                   5615:                             );
                   5616:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   5617:             if ($putresult eq 'ok') {
                   5618:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   5619:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   5620:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   5621:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   5622:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   5623:                 }
                   5624:             }
                   5625:         }
                   5626:     }
                   5627:     return;
                   5628: }
                   5629: 
1.461     www      5630: # -------------------------------------------------See if a user is privileged
                   5631: 
                   5632: sub privileged {
1.1219    raeburn  5633:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 5634:     my $now = time;
1.1219    raeburn  5635:     my $roles;
                   5636:     if (ref($possroles) eq 'ARRAY') {
                   5637:         $roles = $possroles; 
                   5638:     } else {
                   5639:         $roles = ['dc','su'];
                   5640:     }
                   5641:     if (ref($possdomains) eq 'ARRAY') {
                   5642:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   5643:         foreach my $dom (@{$possdomains}) {
                   5644:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   5645:                 (ref($privileged{$dom}) eq 'HASH')) {
                   5646:                 foreach my $role (@{$roles}) {
                   5647:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   5648:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   5649:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   5650:                             return 1 unless (($end && $end < $now) ||
                   5651:                                              ($start && $start > $now));
                   5652:                         }
                   5653:                     }
                   5654:                 }
                   5655:             }
                   5656:         }
                   5657:     } else {
                   5658:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   5659:         my $now = time;
1.1170    droeschl 5660: 
1.1275    musolffc 5661:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys(%rolesdump)}) {
1.1170    droeschl 5662:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  5663:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 5664:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  5665:                         or ($tstart && $tstart > $now);
1.1170    droeschl 5666:             }
1.1219    raeburn  5667:         }
                   5668:     }
                   5669:     return 0;
                   5670: }
1.1170    droeschl 5671: 
1.1219    raeburn  5672: sub privileged_by_domain {
                   5673:     my ($domains,$roles) = @_;
                   5674:     my %privileged = ();
                   5675:     my $cachetime = 60*60*24;
                   5676:     my $now = time;
                   5677:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   5678:         return %privileged;
                   5679:     }
                   5680:     foreach my $dom (@{$domains}) {
                   5681:         next if (ref($privileged{$dom}) eq 'HASH');
                   5682:         my $needroles;
                   5683:         foreach my $role (@{$roles}) {
                   5684:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   5685:             if (defined($cached)) {
                   5686:                 if (ref($result) eq 'HASH') {
                   5687:                     $privileged{$dom}{$role} = $result;
                   5688:                 }
                   5689:             } else {
                   5690:                 $needroles = 1;
                   5691:             }
                   5692:         }
                   5693:         if ($needroles) {
                   5694:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   5695:             $privileged{$dom} = {};
                   5696:             foreach my $server (keys(%dompersonnel)) {
                   5697:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   5698:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   5699:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   5700:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   5701:                         next if ($end && $end < $now);
                   5702:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   5703:                             $dompersonnel{$server}{$item};
                   5704:                     }
                   5705:                 }
                   5706:             }
                   5707:             if (ref($privileged{$dom}) eq 'HASH') {
                   5708:                 foreach my $role (@{$roles}) {
                   5709:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   5710:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   5711:                     } else {
                   5712:                         my %hash = ();
                   5713:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   5714:                     }
                   5715:                 }
                   5716:             }
                   5717:         }
                   5718:     }
                   5719:     return %privileged;
1.9       www      5720: }
1.1       albertel 5721: 
1.103     harris41 5722: # -------------------------------------------------------- Get user privileges
1.11      www      5723: 
                   5724: sub rolesinit {
1.1169    droeschl 5725:     my ($domain, $username) = @_;
                   5726:     my %userroles = ('user.login.time' => time);
                   5727:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   5728: 
                   5729:     # firstaccess and timerinterval are related to timed maps/resources. 
                   5730:     # also, blocking can be triggered by an activating timer
                   5731:     # it's saved in the user's %env.
                   5732:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   5733:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   5734:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
                   5735:         %timerintchk, %timerintenv);
                   5736: 
1.1162    raeburn  5737:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 5738:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  5739:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   5740:     }
1.1169    droeschl 5741: 
1.1162    raeburn  5742:     foreach my $key (keys(%timerinterval)) {
                   5743:         my ($cid,$rest) = split(/\0/,$key);
                   5744:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   5745:     }
1.1169    droeschl 5746: 
1.11      www      5747:     my %allroles=();
1.1162    raeburn  5748:     my %allgroups=();
1.11      www      5749: 
1.1274    raeburn  5750:     for my $area (grep { ! /^rolesdef_/ } keys(%rolesdump)) {
1.1169    droeschl 5751:         my $role = $rolesdump{$area};
                   5752:         $area =~ s/\_\w\w$//;
                   5753: 
                   5754:         my ($trole, $tend, $tstart, $group_privs);
                   5755: 
                   5756:         if ($role =~ /^cr/) {
                   5757:         # Custom role, defined by a user 
                   5758:         # e.g., user.role.cr/msu/smith/mynewrole
                   5759:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   5760:                 $trole = $1;
                   5761:                 ($tend, $tstart) = split('_', $2);
                   5762:             } else {
                   5763:                 $trole = $role;
                   5764:             }
                   5765:         } elsif ($role =~ m|^gr/|) {
                   5766:         # Role of member in a group, defined within a course/community
                   5767:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   5768:             ($trole, $tend, $tstart) = split(/_/, $role);
                   5769:             next if $tstart eq '-1';
                   5770:             ($trole, $group_privs) = split(/\//, $trole);
                   5771:             $group_privs = &unescape($group_privs);
                   5772:         } else {
                   5773:         # Just a normal role, defined in roles.tab
                   5774:             ($trole, $tend, $tstart) = split(/_/,$role);
                   5775:         }
                   5776: 
                   5777:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   5778:                  $username);
                   5779:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   5780: 
                   5781:         # role expired or not available yet?
                   5782:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   5783:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   5784: 
                   5785:         next if $area eq '' or $trole eq '';
                   5786: 
                   5787:         my $spec = "$trole.$area";
                   5788:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   5789: 
                   5790:         if ($trole =~ /^cr\//) {
                   5791:         # Custom role, defined by a user
                   5792:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   5793:         } elsif ($trole eq 'gr') {
                   5794:         # Role of a member in a group, defined within a course/community
                   5795:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   5796:             next;
                   5797:         } else {
                   5798:         # Normal role, defined in roles.tab
                   5799:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   5800:         }
                   5801: 
                   5802:         my $cid = $tdomain.'_'.$trest;
                   5803:         unless ($firstaccchk{$cid}) {
                   5804:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   5805:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   5806:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   5807:                         $coursetimerstarts{$cid}{$item}; 
                   5808:                 }
                   5809:             }
                   5810:             $firstaccchk{$cid} = 1;
                   5811:         }
                   5812:         unless ($timerintchk{$cid}) {
                   5813:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   5814:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   5815:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   5816:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  5817:                 }
1.12      www      5818:             }
1.1169    droeschl 5819:             $timerintchk{$cid} = 1;
1.191     harris41 5820:         }
1.11      www      5821:     }
1.1169    droeschl 5822: 
1.1341    raeburn  5823:     @userroles{'user.author','user.adv','user.rar'} = &set_userprivs(\%userroles,
                   5824:                                                           \%allroles, \%allgroups);
1.1169    droeschl 5825:     $env{'user.adv'} = $userroles{'user.adv'};
1.1341    raeburn  5826:     $env{'user.rar'} = $userroles{'user.rar'};
1.1169    droeschl 5827: 
1.1162    raeburn  5828:     return (\%userroles,\%firstaccenv,\%timerintenv);
1.11      www      5829: }
                   5830: 
1.567     raeburn  5831: sub set_arearole {
1.1215    raeburn  5832:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   5833:     unless ($nolog) {
1.567     raeburn  5834: # log the associated role with the area
1.1215    raeburn  5835:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   5836:     }
1.743     albertel 5837:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  5838: }
                   5839: 
                   5840: sub custom_roleprivs {
                   5841:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   5842:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
1.1250    raeburn  5843:     my $homsvr = &homeserver($rauthor,$rdomain);
1.838     albertel 5844:     if (&hostname($homsvr) ne '') {
1.567     raeburn  5845:         my ($rdummy,$roledef)=
                   5846:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   5847:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   5848:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   5849:             if (defined($syspriv)) {
1.1043    raeburn  5850:                 if ($trest =~ /^$match_community$/) {
                   5851:                     $syspriv =~ s/bre\&S//; 
                   5852:                 }
1.567     raeburn  5853:                 $$allroles{'cm./'}.=':'.$syspriv;
                   5854:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   5855:             }
                   5856:             if ($tdomain ne '') {
                   5857:                 if (defined($dompriv)) {
                   5858:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   5859:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   5860:                 }
                   5861:                 if (($trest ne '') && (defined($coursepriv))) {
1.1332    raeburn  5862:                     if ($trole =~ m{^cr/$tdomain/$tdomain\Q-domainconfig\E/([^/]+)$}) {
                   5863:                         my $rolename = $1;
                   5864:                         $coursepriv = &course_adhocrole_privs($rolename,$tdomain,$trest,$coursepriv);
                   5865:                     }
1.567     raeburn  5866:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   5867:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   5868:                 }
                   5869:             }
                   5870:         }
                   5871:     }
                   5872: }
                   5873: 
1.1332    raeburn  5874: sub course_adhocrole_privs {
                   5875:     my ($rolename,$cdom,$cnum,$coursepriv) = @_;
                   5876:     my %overrides = &get('environment',["internal.adhocpriv.$rolename"],$cdom,$cnum);
                   5877:     if ($overrides{"internal.adhocpriv.$rolename"}) {
                   5878:         my (%currprivs,%storeprivs);
                   5879:         foreach my $item (split(/:/,$coursepriv)) {
                   5880:             my ($priv,$restrict) = split(/\&/,$item);
                   5881:             $currprivs{$priv} = $restrict;
                   5882:         }
                   5883:         my (%possadd,%possremove,%full);
                   5884:         foreach my $item (split(/\:/,$Apache::lonnet::pr{'cr:c'})) {
                   5885:             my ($priv,$restrict)=split(/\&/,$item);
                   5886:             $full{$priv} = $restrict;
                   5887:         }
                   5888:         foreach my $item (split(/,/,$overrides{"internal.adhocpriv.$rolename"})) {
                   5889:              next if ($item eq '');
                   5890:              my ($rule,$rest) = split(/=/,$item);
                   5891:              next unless (($rule eq 'off') || ($rule eq 'on'));
                   5892:              foreach my $priv (split(/:/,$rest)) {
                   5893:                  if ($priv ne '') {
                   5894:                      if ($rule eq 'off') {
                   5895:                          $possremove{$priv} = 1;
                   5896:                      } else {
                   5897:                          $possadd{$priv} = 1;
                   5898:                      }
                   5899:                  }
                   5900:              }
                   5901:          }
                   5902:          foreach my $priv (sort(keys(%full))) {
                   5903:              if (exists($currprivs{$priv})) {
                   5904:                  unless (exists($possremove{$priv})) {
                   5905:                      $storeprivs{$priv} = $currprivs{$priv};
                   5906:                  }
                   5907:              } elsif (exists($possadd{$priv})) {
                   5908:                  $storeprivs{$priv} = $full{$priv};
                   5909:              }
                   5910:          }
                   5911:          $coursepriv = ':'.join(':',map { $_.'&'.$storeprivs{$_}; } sort(keys(%storeprivs)));
                   5912:      }
                   5913:      return $coursepriv;
                   5914: }
                   5915: 
1.678     raeburn  5916: sub group_roleprivs {
                   5917:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   5918:     my $access = 1;
                   5919:     my $now = time;
                   5920:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   5921:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   5922:     if ($access) {
1.811     albertel 5923:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  5924:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   5925:     }
                   5926: }
1.567     raeburn  5927: 
                   5928: sub standard_roleprivs {
                   5929:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   5930:     if (defined($pr{$trole.':s'})) {
                   5931:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   5932:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   5933:     }
                   5934:     if ($tdomain ne '') {
                   5935:         if (defined($pr{$trole.':d'})) {
                   5936:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   5937:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   5938:         }
                   5939:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   5940:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   5941:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   5942:         }
                   5943:     }
                   5944: }
                   5945: 
                   5946: sub set_userprivs {
1.1064    raeburn  5947:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  5948:     my $author=0;
                   5949:     my $adv=0;
1.1341    raeburn  5950:     my $rar=0;
1.678     raeburn  5951:     my %grouproles = ();
                   5952:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  5953:         my @groupkeys; 
1.1000    raeburn  5954:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  5955:             push(@groupkeys,$role);
                   5956:         }
                   5957:         if (ref($groups_roles) eq 'HASH') {
                   5958:             foreach my $key (keys(%{$groups_roles})) {
                   5959:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   5960:                     push(@groupkeys,$key);
                   5961:                 }
                   5962:             }
                   5963:         }
                   5964:         if (@groupkeys > 0) {
                   5965:             foreach my $role (@groupkeys) {
                   5966:                 my ($trole,$area,$sec,$extendedarea);
                   5967:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   5968:                     $trole = $1;
                   5969:                     $area = $2;
                   5970:                     $sec = $3;
                   5971:                     $extendedarea = $area.$sec;
                   5972:                     if (exists($$allgroups{$area})) {
                   5973:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   5974:                             my $spec = $trole.'.'.$extendedarea;
                   5975:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  5976:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  5977:                         }
1.678     raeburn  5978:                     }
                   5979:                 }
                   5980:             }
                   5981:         }
                   5982:     }
1.800     albertel 5983:     foreach my $group (keys(%grouproles)) {
                   5984:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  5985:     }
1.800     albertel 5986:     foreach my $role (keys(%{$allroles})) {
                   5987:         my %thesepriv;
1.941     raeburn  5988:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 5989:         foreach my $item (split(/:/,$$allroles{$role})) {
                   5990:             if ($item ne '') {
                   5991:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  5992:                 if ($restrictions eq '') {
                   5993:                     $thesepriv{$privilege}='F';
                   5994:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   5995:                     $thesepriv{$privilege}.=$restrictions;
                   5996:                 }
                   5997:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
1.1341    raeburn  5998:                 if ($thesepriv{'rar'} eq 'F') { $rar=1; }
1.567     raeburn  5999:             }
                   6000:         }
                   6001:         my $thesestr='';
1.1104    raeburn  6002:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 6003: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   6004: 	}
                   6005:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  6006:     }
1.1341    raeburn  6007:     return ($author,$adv,$rar);
1.567     raeburn  6008: }
                   6009: 
1.994     raeburn  6010: sub role_status {
1.1104    raeburn  6011:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  6012:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
1.1250    raeburn  6013:         my ($one,$two) = split(m{\./},$rolekey,2);
                   6014:         (undef,undef,$$role) = split(/\./,$one,3);
1.994     raeburn  6015:         unless (!defined($$role) || $$role eq '') {
1.1251    raeburn  6016:             $$where = '/'.$two;
1.994     raeburn  6017:             $$trolecode=$$role.'.'.$$where;
                   6018:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   6019:             $$tstatus='is';
1.1104    raeburn  6020:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  6021:                 $$tstatus='future';
1.1034    raeburn  6022:                 if ($$tstart<$now) {
                   6023:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  6024:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  6025:                             my (%allroles,%allgroups,$group_privs,
                   6026:                                 %groups_roles,@rolecodes);
1.1002    raeburn  6027:                             my %userroles = (
                   6028:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   6029:                             );
1.1064    raeburn  6030:                             @rolecodes = ('cm'); 
1.1002    raeburn  6031:                             my $spec=$$role.'.'.$$where;
                   6032:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   6033:                             if ($$role =~ /^cr\//) {
                   6034:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  6035:                                 push(@rolecodes,'cr');
1.1002    raeburn  6036:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  6037:                                 push(@rolecodes,$$role);
1.1002    raeburn  6038:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   6039:                                                     $env{'user.name'});
1.1064    raeburn  6040:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  6041:                                 (undef,my $group_privs) = split(/\//,$trole);
                   6042:                                 $group_privs = &unescape($group_privs);
                   6043:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  6044:                                 my %course_roles = &get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',['active'],['cc','co','in','ta','ep','ad','st','cr'],[$tdomain],1);
1.1104    raeburn  6045:                                 &get_groups_roles($tdomain,$trest,
                   6046:                                                   \%course_roles,\@rolecodes,
                   6047:                                                   \%groups_roles);
1.1002    raeburn  6048:                             } else {
1.1064    raeburn  6049:                                 push(@rolecodes,$$role);
1.1002    raeburn  6050:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   6051:                             }
1.1341    raeburn  6052:                             my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%allroles,\%allgroups,
                   6053:                                                                    \%groups_roles);
1.1064    raeburn  6054:                             &appenv(\%userroles,\@rolecodes);
1.1342    raeburn  6055:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1002    raeburn  6056:                         }
                   6057:                     }
1.1034    raeburn  6058:                     $$tstatus = 'is';
1.1002    raeburn  6059:                 }
1.994     raeburn  6060:             }
                   6061:             if ($$tend) {
1.1104    raeburn  6062:                 if ($$tend<$update) {
1.994     raeburn  6063:                     $$tstatus='expired';
                   6064:                 } elsif ($$tend<$now) {
                   6065:                     $$tstatus='will_not';
                   6066:                 }
                   6067:             }
                   6068:         }
                   6069:     }
                   6070: }
                   6071: 
1.1104    raeburn  6072: sub get_groups_roles {
                   6073:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   6074:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   6075:                   (ref($rolecodes) eq 'ARRAY') && 
                   6076:                   (ref($groups_roles) eq 'HASH')); 
                   6077:     if (keys(%{$cdom_courseroles}) > 0) {
                   6078:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   6079:         if ($cdom ne '' && $cnum ne '') {
                   6080:             foreach my $key (keys(%{$cdom_courseroles})) {
                   6081:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   6082:                     my $crsrole = $1;
                   6083:                     my $crssec = $2;
                   6084:                     if ($crsrole =~ /^cr/) {
                   6085:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   6086:                             push(@{$rolecodes},'cr');
                   6087:                         }
                   6088:                     } else {
                   6089:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   6090:                             push(@{$rolecodes},$crsrole);
                   6091:                         }
                   6092:                     }
                   6093:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   6094:                     if ($crssec ne '') {
                   6095:                         $rolekey .= "/$crssec";
                   6096:                     }
                   6097:                     $rolekey .= './';
                   6098:                     $groups_roles->{$rolekey} = $rolecodes;
                   6099:                 }
                   6100:             }
                   6101:         }
                   6102:     }
                   6103:     return;
                   6104: }
                   6105: 
                   6106: sub delete_env_groupprivs {
                   6107:     my ($where,$courseroles,$possroles) = @_;
                   6108:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   6109:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   6110:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   6111:         %{$courseroles->{$udom}} =
                   6112:             &get_my_roles('','','userroles',['active'],
                   6113:                           $possroles,[$udom],1);
                   6114:     }
                   6115:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   6116:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   6117:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   6118:             my $area = '/'.$cdom.'/'.$cnum;
                   6119:             my $privkey = "user.priv.$crsrole.$area";
                   6120:             if ($crssec ne '') {
                   6121:                 $privkey .= '/'.$crssec;
                   6122:             }
                   6123:             $privkey .= ".$area/$group";
                   6124:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   6125:         }
                   6126:     }
                   6127:     return;
                   6128: }
                   6129: 
1.994     raeburn  6130: sub check_adhoc_privs {
1.1329    raeburn  6131:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller,$sec) = @_;
1.994     raeburn  6132:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1329    raeburn  6133:     if ($sec) {
                   6134:         $cckey .= '/'.$sec;
                   6135:     } 
1.1185    raeburn  6136:     my $setprivs;
1.994     raeburn  6137:     if ($env{$cckey}) {
                   6138:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  6139:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  6140:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1329    raeburn  6141:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  6142:             $setprivs = 1;
1.994     raeburn  6143:         }
                   6144:     } else {
1.1330    raeburn  6145:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller,$sec);
1.1185    raeburn  6146:         $setprivs = 1;
1.994     raeburn  6147:     }
1.1185    raeburn  6148:     return $setprivs;
1.994     raeburn  6149: }
                   6150: 
                   6151: sub set_adhoc_privileges {
1.1326    raeburn  6152: # role can be cc, ca, or cr/<dom>/<dom>-domainconfig/role
1.1329    raeburn  6153:     my ($dcdom,$pickedcourse,$role,$caller,$sec) = @_;
1.994     raeburn  6154:     my $area = '/'.$dcdom.'/'.$pickedcourse;
1.1329    raeburn  6155:     if ($sec ne '') {
                   6156:         $area .= '/'.$sec;
                   6157:     }
1.994     raeburn  6158:     my $spec = $role.'.'.$area;
                   6159:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  6160:                                   $env{'user.name'},1);
1.1326    raeburn  6161:     my %rolehash = ();
1.1332    raeburn  6162:     if ($role =~ m{^\Qcr/$dcdom/$dcdom\E\-domainconfig/(\w+)$}) {
                   6163:         my $rolename = $1;
1.1326    raeburn  6164:         &custom_roleprivs(\%rolehash,$role,$dcdom,$pickedcourse,$spec,$area);
1.1332    raeburn  6165:         my %domdef = &get_domain_defaults($dcdom);
                   6166:         if (ref($domdef{'adhocroles'}) eq 'HASH') {
                   6167:             if (ref($domdef{'adhocroles'}{$rolename}) eq 'HASH') {
                   6168:                 &appenv({'request.role.desc' => $domdef{'adhocroles'}{$rolename}{'desc'},});
                   6169:             }
                   6170:         }
1.1326    raeburn  6171:     } else {
                   6172:         &standard_roleprivs(\%rolehash,$role,$dcdom,$spec,$pickedcourse,$area);
                   6173:     }
1.1341    raeburn  6174:     my ($author,$adv,$rar)= &set_userprivs(\%userroles,\%rolehash);
1.994     raeburn  6175:     &appenv(\%userroles,[$role,'cm']);
1.1342    raeburn  6176:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$spec);
1.1088    raeburn  6177:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
                   6178:         &appenv( {'request.role'        => $spec,
                   6179:                   'request.role.domain' => $dcdom,
1.1332    raeburn  6180:                   'request.course.sec'  => $sec,
1.1088    raeburn  6181:                  }
                   6182:                );
                   6183:         my $tadv=0;
                   6184:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   6185:         &appenv({'request.role.adv'    => $tadv});
                   6186:     }
1.994     raeburn  6187: }
                   6188: 
1.12      www      6189: # --------------------------------------------------------------- get interface
                   6190: 
                   6191: sub get {
1.131     albertel 6192:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      6193:    my $items='';
1.800     albertel 6194:    foreach my $item (@$storearr) {
                   6195:        $items.=&escape($item).'&';
1.191     harris41 6196:    }
1.12      www      6197:    $items=~s/\&$//;
1.620     albertel 6198:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6199:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 6200:    my $uhome=&homeserver($uname,$udomain);
                   6201: 
1.133     albertel 6202:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      6203:    my @pairs=split(/\&/,$rep);
1.273     albertel 6204:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   6205:      return @pairs;
                   6206:    }
1.15      www      6207:    my %returnhash=();
1.42      www      6208:    my $i=0;
1.800     albertel 6209:    foreach my $item (@$storearr) {
                   6210:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      6211:       $i++;
1.191     harris41 6212:    }
1.15      www      6213:    return %returnhash;
1.27      www      6214: }
                   6215: 
                   6216: # --------------------------------------------------------------- del interface
                   6217: 
                   6218: sub del {
1.133     albertel 6219:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      6220:    my $items='';
1.800     albertel 6221:    foreach my $item (@$storearr) {
                   6222:        $items.=&escape($item).'&';
1.191     harris41 6223:    }
1.984     neumanie 6224: 
1.27      www      6225:    $items=~s/\&$//;
1.620     albertel 6226:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6227:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 6228:    my $uhome=&homeserver($uname,$udomain);
                   6229:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      6230: }
                   6231: 
                   6232: # -------------------------------------------------------------- dump interface
                   6233: 
1.1180    droeschl 6234: sub unserialize {
                   6235:     my ($rep, $escapedkeys) = @_;
                   6236: 
                   6237:     return {} if $rep =~ /^error/;
                   6238: 
                   6239:     my %returnhash=();
1.1252    raeburn  6240: 	foreach my $item (split(/\&/,$rep)) {
1.1180    droeschl 6241: 	    my ($key, $value) = split(/=/, $item, 2);
                   6242: 	    $key = unescape($key) unless $escapedkeys;
                   6243: 	    next if $key =~ /^error: 2 /;
1.1252    raeburn  6244: 	    $returnhash{$key} = &thaw_unescape($value);
1.1180    droeschl 6245: 	}
                   6246:     #return %returnhash;
                   6247:     return \%returnhash;
                   6248: }        
                   6249: 
                   6250: # see Lond::dump_with_regexp
                   6251: # if $escapedkeys hash keys won't get unescaped.
1.15      www      6252: sub dump {
1.1180    droeschl 6253:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755     albertel 6254:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6255:     if (!$uname) { $uname=$env{'user.name'}; }
                   6256:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 6257: 
1.1266    raeburn  6258:     if ($regexp) {
                   6259:         $regexp=&escape($regexp);
                   6260:     } else {
                   6261:         $regexp='.';
                   6262:     }
1.1180    droeschl 6263:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   6264:         # user is hosted on this machine
1.1266    raeburn  6265:         my $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  6266:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 6267:         return %{unserialize($reply, $escapedkeys)};
                   6268:     }
1.1166    raeburn  6269:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755     albertel 6270:     my @pairs=split(/\&/,$rep);
                   6271:     my %returnhash=();
1.1098    foxr     6272:     if (!($rep =~ /^error/ )) {
                   6273: 	foreach my $item (@pairs) {
                   6274: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 6275:         $key = unescape($key) unless $escapedkeys;
                   6276:         #$key = &unescape($key);
1.1098    foxr     6277: 	    next if ($key =~ /^error: 2 /);
                   6278: 	    $returnhash{$key}=&thaw_unescape($value);
                   6279: 	}
1.755     albertel 6280:     }
                   6281:     return %returnhash;
1.407     www      6282: }
                   6283: 
1.1098    foxr     6284: 
1.717     albertel 6285: # --------------------------------------------------------- dumpstore interface
                   6286: 
                   6287: sub dumpstore {
                   6288:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 6289:    # same as dump but keys must be escaped. They may contain colon separated
                   6290:    # lists of values that may themself contain colons (e.g. symbs).
                   6291:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 6292: }
                   6293: 
1.407     www      6294: # -------------------------------------------------------------- keys interface
                   6295: 
                   6296: sub getkeys {
                   6297:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 6298:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6299:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      6300:    my $uhome=&homeserver($uname,$udomain);
                   6301:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   6302:    my @keyarray=();
1.800     albertel 6303:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  6304:       next if ($key =~ /^error: 2 /);
1.800     albertel 6305:       push(@keyarray,&unescape($key));
1.407     www      6306:    }
                   6307:    return @keyarray;
1.318     matthew  6308: }
                   6309: 
1.319     matthew  6310: # --------------------------------------------------------------- currentdump
                   6311: sub currentdump {
1.328     matthew  6312:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 6313:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   6314:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   6315:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  6316:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 6317:    my $rep;
                   6318: 
                   6319:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   6320:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   6321:                    $courseid)));
                   6322:    } else {
                   6323:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   6324:    }
                   6325: 
1.318     matthew  6326:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  6327:    #
1.318     matthew  6328:    my %returnhash=();
1.319     matthew  6329:    #
1.1343    raeburn  6330:    if ($rep eq 'unknown_cmd') {
1.319     matthew  6331:        # an old lond will not know currentdump
                   6332:        # Do a dump and make it look like a currentdump
1.822     albertel 6333:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  6334:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   6335:        my %hash = @tmp;
                   6336:        @tmp=();
1.424     matthew  6337:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  6338:    } else {
                   6339:        my @pairs=split(/\&/,$rep);
1.800     albertel 6340:        foreach my $pair (@pairs) {
                   6341:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  6342:            my ($symb,$param) = split(/:/,$key);
                   6343:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 6344:                                                         &thaw_unescape($value);
1.319     matthew  6345:        }
1.191     harris41 6346:    }
1.12      www      6347:    return %returnhash;
1.424     matthew  6348: }
                   6349: 
                   6350: sub convert_dump_to_currentdump{
                   6351:     my %hash = %{shift()};
                   6352:     my %returnhash;
                   6353:     # Code ripped from lond, essentially.  The only difference
                   6354:     # here is the unescaping done by lonnet::dump().  Conceivably
                   6355:     # we might run in to problems with parameter names =~ /^v\./
                   6356:     while (my ($key,$value) = each(%hash)) {
                   6357:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 6358: 	$symb  = &unescape($symb);
                   6359: 	$param = &unescape($param);
1.424     matthew  6360:         next if ($v eq 'version' || $symb eq 'keys');
                   6361:         next if (exists($returnhash{$symb}) &&
                   6362:                  exists($returnhash{$symb}->{$param}) &&
                   6363:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   6364:         $returnhash{$symb}->{$param}=$value;
                   6365:         $returnhash{$symb}->{'v.'.$param}=$v;
                   6366:     }
                   6367:     #
                   6368:     # Remove all of the keys in the hashes which keep track of
                   6369:     # the version of the parameter.
                   6370:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   6371:         # use a foreach because we are going to delete from the hash.
                   6372:         foreach my $key (keys(%$param_hash)) {
                   6373:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   6374:         }
                   6375:     }
                   6376:     return \%returnhash;
1.12      www      6377: }
                   6378: 
1.627     albertel 6379: # ------------------------------------------------------ critical inc interface
                   6380: 
                   6381: sub cinc {
                   6382:     return &inc(@_,'critical');
                   6383: }
                   6384: 
1.449     matthew  6385: # --------------------------------------------------------------- inc interface
                   6386: 
                   6387: sub inc {
1.627     albertel 6388:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 6389:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6390:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  6391:     my $uhome=&homeserver($uname,$udomain);
                   6392:     my $items='';
                   6393:     if (! ref($store)) {
                   6394:         # got a single value, so use that instead
                   6395:         $items = &escape($store).'=&';
                   6396:     } elsif (ref($store) eq 'SCALAR') {
                   6397:         $items = &escape($$store).'=&';        
                   6398:     } elsif (ref($store) eq 'ARRAY') {
                   6399:         $items = join('=&',map {&escape($_);} @{$store});
                   6400:     } elsif (ref($store) eq 'HASH') {
                   6401:         while (my($key,$value) = each(%{$store})) {
                   6402:             $items.= &escape($key).'='.&escape($value).'&';
                   6403:         }
                   6404:     }
                   6405:     $items=~s/\&$//;
1.627     albertel 6406:     if ($critical) {
                   6407: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   6408:     } else {
                   6409: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   6410:     }
1.449     matthew  6411: }
                   6412: 
1.12      www      6413: # --------------------------------------------------------------- put interface
                   6414: 
                   6415: sub put {
1.134     albertel 6416:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 6417:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6418:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 6419:    my $uhome=&homeserver($uname,$udomain);
1.12      www      6420:    my $items='';
1.800     albertel 6421:    foreach my $item (keys(%$storehash)) {
                   6422:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 6423:    }
1.12      www      6424:    $items=~s/\&$//;
1.134     albertel 6425:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      6426: }
                   6427: 
1.631     albertel 6428: # ------------------------------------------------------------ newput interface
                   6429: 
                   6430: sub newput {
                   6431:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   6432:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6433:    if (!$uname) { $uname=$env{'user.name'}; }
                   6434:    my $uhome=&homeserver($uname,$udomain);
                   6435:    my $items='';
                   6436:    foreach my $key (keys(%$storehash)) {
                   6437:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   6438:    }
                   6439:    $items=~s/\&$//;
                   6440:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   6441: }
                   6442: 
                   6443: # ---------------------------------------------------------  putstore interface
                   6444: 
1.524     raeburn  6445: sub putstore {
1.1269    raeburn  6446:    my ($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog)=@_;
1.620     albertel 6447:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6448:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  6449:    my $uhome=&homeserver($uname,$udomain);
                   6450:    my $items='';
1.715     albertel 6451:    foreach my $key (keys(%$storehash)) {
                   6452:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  6453:    }
1.715     albertel 6454:    $items=~s/\&$//;
1.716     albertel 6455:    my $esc_symb=&escape($symb);
                   6456:    my $esc_v=&escape($version);
1.715     albertel 6457:    my $reply =
1.716     albertel 6458:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 6459: 	      $uhome);
1.1269    raeburn  6460:    if (($tolog) && ($reply eq 'ok')) {
                   6461:        my $namevalue='';
                   6462:        foreach my $key (keys(%{$storehash})) {
                   6463:            $namevalue.=&escape($key).'='.&freeze_escape($storehash->{$key}).'&';
                   6464:        }
                   6465:        $namevalue .= 'ip='.&escape($ENV{'REMOTE_ADDR'}).
                   6466:                      '&host='.&escape($perlvar{'lonHostID'}).
                   6467:                      '&version='.$esc_v.
                   6468:                      '&by='.&escape($env{'user.name'}.':'.$env{'user.domain'});
                   6469:        &Apache::lonnet::courselog($symb.':'.$uname.':'.$udomain.':PUTSTORE:'.$namevalue);
                   6470:    }
1.715     albertel 6471:    if ($reply eq 'unknown_cmd') {
1.716     albertel 6472:        # gfall back to way things use to be done
1.715     albertel 6473:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   6474: 			    $uname);
1.524     raeburn  6475:    }
1.715     albertel 6476:    return $reply;
                   6477: }
                   6478: 
                   6479: sub old_putstore {
1.716     albertel 6480:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   6481:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6482:     if (!$uname) { $uname=$env{'user.name'}; }
                   6483:     my $uhome=&homeserver($uname,$udomain);
                   6484:     my %newstorehash;
1.800     albertel 6485:     foreach my $item (keys(%$storehash)) {
                   6486: 	my $key = $version.':'.&escape($symb).':'.$item;
                   6487: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 6488:     }
                   6489:     my $items='';
                   6490:     my %allitems = ();
1.800     albertel 6491:     foreach my $item (keys(%newstorehash)) {
                   6492: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 6493: 	    my $key = $1.':keys:'.$2;
                   6494: 	    $allitems{$key} .= $3.':';
                   6495: 	}
1.800     albertel 6496: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 6497:     }
1.800     albertel 6498:     foreach my $item (keys(%allitems)) {
                   6499: 	$allitems{$item} =~ s/\:$//;
                   6500: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 6501:     }
                   6502:     $items=~s/\&$//;
                   6503:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  6504: }
                   6505: 
1.47      www      6506: # ------------------------------------------------------ critical put interface
                   6507: 
                   6508: sub cput {
1.134     albertel 6509:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 6510:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6511:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 6512:    my $uhome=&homeserver($uname,$udomain);
1.47      www      6513:    my $items='';
1.800     albertel 6514:    foreach my $item (keys(%$storehash)) {
                   6515:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 6516:    }
1.47      www      6517:    $items=~s/\&$//;
1.134     albertel 6518:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      6519: }
                   6520: 
                   6521: # -------------------------------------------------------------- eget interface
                   6522: 
                   6523: sub eget {
1.133     albertel 6524:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      6525:    my $items='';
1.800     albertel 6526:    foreach my $item (@$storearr) {
                   6527:        $items.=&escape($item).'&';
1.191     harris41 6528:    }
1.12      www      6529:    $items=~s/\&$//;
1.620     albertel 6530:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   6531:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 6532:    my $uhome=&homeserver($uname,$udomain);
                   6533:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      6534:    my @pairs=split(/\&/,$rep);
                   6535:    my %returnhash=();
1.42      www      6536:    my $i=0;
1.800     albertel 6537:    foreach my $item (@$storearr) {
                   6538:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      6539:       $i++;
1.191     harris41 6540:    }
1.12      www      6541:    return %returnhash;
                   6542: }
                   6543: 
1.667     albertel 6544: # ------------------------------------------------------------ tmpput interface
                   6545: sub tmpput {
1.802     raeburn  6546:     my ($storehash,$server,$context)=@_;
1.667     albertel 6547:     my $items='';
1.800     albertel 6548:     foreach my $item (keys(%$storehash)) {
                   6549: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 6550:     }
                   6551:     $items=~s/\&$//;
1.802     raeburn  6552:     if (defined($context)) {
                   6553:         $items .= ':'.&escape($context);
                   6554:     }
1.667     albertel 6555:     return &reply("tmpput:$items",$server);
                   6556: }
                   6557: 
                   6558: # ------------------------------------------------------------ tmpget interface
                   6559: sub tmpget {
1.688     albertel 6560:     my ($token,$server)=@_;
                   6561:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   6562:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 6563:     my %returnhash;
1.1328    raeburn  6564:     if ($rep =~ /^(con_lost|error|no_such_host)/i) {
                   6565:         return %returnhash;
                   6566:     }
1.667     albertel 6567:     foreach my $item (split(/\&/,$rep)) {
                   6568: 	my ($key,$value)=split(/=/,$item);
                   6569: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   6570:     }
                   6571:     return %returnhash;
                   6572: }
                   6573: 
1.1113    raeburn  6574: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 6575: sub tmpdel {
                   6576:     my ($token,$server)=@_;
                   6577:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   6578:     return &reply("tmpdel:$token",$server);
                   6579: }
                   6580: 
1.1198    raeburn  6581: # ------------------------------------------------------------ get_timebased_id 
                   6582: 
                   6583: sub get_timebased_id {
                   6584:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   6585:         $maxtries) = @_;
                   6586:     my ($newid,$error,$dellock);
                   6587:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   6588:         return ('','ok','invalid call to get suffix');
                   6589:     }
                   6590: 
                   6591: # set defaults for any optional args for which values were not supplied
                   6592:     if ($who eq '') {
                   6593:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   6594:     }
                   6595:     if (!$locktries) {
                   6596:         $locktries = 3;
                   6597:     }
                   6598:     if (!$maxtries) {
                   6599:         $maxtries = 10;
                   6600:     }
                   6601:     
                   6602:     if (($cdom eq '') || ($cnum eq '')) {
                   6603:         if ($env{'request.course.id'}) {
                   6604:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6605:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6606:         }
                   6607:         if (($cdom eq '') || ($cnum eq '')) {
                   6608:             return ('','ok','call to get suffix not in course context');
                   6609:         }
                   6610:     }
                   6611: 
                   6612: # construct locking item
                   6613:     my $lockhash = {
                   6614:                       $prefix."\0".'locked_'.$keyid => $who,
                   6615:                    };
                   6616:     my $tries = 0;
                   6617: 
                   6618: # attempt to get lock on nohist_$namespace file
                   6619:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   6620:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   6621:         $tries ++;
                   6622:         sleep 1;
                   6623:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   6624:     }
                   6625: 
                   6626: # attempt to get unique identifier, based on current timestamp
                   6627:     if ($gotlock eq 'ok') {
                   6628:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
                   6629:         my $id = time;
                   6630:         $newid = $id;
1.1268    raeburn  6631:         if ($idtype eq 'addcode') {
                   6632:             $newid .= &sixnum_code();
                   6633:         }
1.1198    raeburn  6634:         my $idtries = 0;
                   6635:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   6636:             if ($idtype eq 'concat') {
                   6637:                 $newid = $id.$idtries;
1.1268    raeburn  6638:             } elsif ($idtype eq 'addcode') {
                   6639:                 $newid = $newid.&sixnum_code();
1.1198    raeburn  6640:             } else {
                   6641:                 $newid ++;
                   6642:             }
                   6643:             $idtries ++;
                   6644:         }
                   6645:         if (!exists($inuse{$prefix."\0".$newid})) {
                   6646:             my %new_item =  (
                   6647:                               $prefix."\0".$newid => $who,
                   6648:                             );
                   6649:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
                   6650:                                                  $cdom,$cnum);
                   6651:             if ($putresult ne 'ok') {
                   6652:                 undef($newid);
                   6653:                 $error = 'error saving new item: '.$putresult;
                   6654:             }
                   6655:         } else {
1.1268    raeburn  6656:              undef($newid);
1.1198    raeburn  6657:              $error = ('error: no unique suffix available for the new item ');
                   6658:         }
                   6659: #  remove lock
                   6660:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   6661:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   6662:     } else {
                   6663:         $error = "error: could not obtain lockfile\n";
                   6664:         $dellock = 'ok';
1.1276    raeburn  6665:         if (($prefix eq 'paste') && ($namespace eq 'courseeditor') && ($keyid eq 'num')) {
                   6666:             $dellock = 'nolock';
                   6667:         }
1.1198    raeburn  6668:     }
                   6669:     return ($newid,$dellock,$error);
                   6670: }
                   6671: 
1.1268    raeburn  6672: sub sixnum_code {
                   6673:     my $code;
                   6674:     for (0..6) {
                   6675:         $code .= int( rand(9) );
                   6676:     }
                   6677:     return $code;
                   6678: }
                   6679: 
1.765     albertel 6680: # -------------------------------------------------- portfolio access checking
                   6681: 
                   6682: sub portfolio_access {
1.1270    raeburn  6683:     my ($requrl,$clientip) = @_;
1.765     albertel 6684:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
1.1270    raeburn  6685:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group,$clientip);
1.814     raeburn  6686:     if ($result) {
                   6687:         my %setters;
                   6688:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   6689:             my ($startblock,$endblock) =
                   6690:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   6691:             if ($startblock && $endblock) {
                   6692:                 return 'B';
                   6693:             }
                   6694:         } else {
                   6695:             my ($startblock,$endblock) =
                   6696:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   6697:             if ($startblock && $endblock) {
                   6698:                 return 'B';
                   6699:             }
                   6700:         }
                   6701:     }
1.765     albertel 6702:     if ($result eq 'ok') {
1.766     albertel 6703:        return 'F';
1.765     albertel 6704:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 6705:        return 'A';
1.765     albertel 6706:     }
1.766     albertel 6707:     return '';
1.765     albertel 6708: }
                   6709: 
                   6710: sub get_portfolio_access {
1.1270    raeburn  6711:     my ($udom,$unum,$file_name,$group,$clientip,$access_hash) = @_;
1.767     albertel 6712: 
                   6713:     if (!ref($access_hash)) {
                   6714: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   6715: 	my %access_controls = &get_access_controls($current_perms,$group,
                   6716: 						   $file_name);
                   6717: 	$access_hash = $access_controls{$file_name};
                   6718:     }
                   6719: 
1.1270    raeburn  6720:     my ($public,$guest,@domains,@users,@courses,@groups,@ips);
1.765     albertel 6721:     my $now = time;
                   6722:     if (ref($access_hash) eq 'HASH') {
                   6723:         foreach my $key (keys(%{$access_hash})) {
                   6724:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   6725:             if ($start > $now) {
                   6726:                 next;
                   6727:             }
                   6728:             if ($end && $end<$now) {
                   6729:                 next;
                   6730:             }
                   6731:             if ($scope eq 'public') {
                   6732:                 $public = $key;
                   6733:                 last;
                   6734:             } elsif ($scope eq 'guest') {
                   6735:                 $guest = $key;
                   6736:             } elsif ($scope eq 'domains') {
                   6737:                 push(@domains,$key);
                   6738:             } elsif ($scope eq 'users') {
                   6739:                 push(@users,$key);
                   6740:             } elsif ($scope eq 'course') {
                   6741:                 push(@courses,$key);
                   6742:             } elsif ($scope eq 'group') {
                   6743:                 push(@groups,$key);
1.1270    raeburn  6744:             } elsif ($scope eq 'ip') {
                   6745:                 push(@ips,$key);
1.765     albertel 6746:             }
                   6747:         }
                   6748:         if ($public) {
                   6749:             return 'ok';
1.1270    raeburn  6750:         } elsif (@ips > 0) {
                   6751:             my $allowed;
                   6752:             foreach my $ipkey (@ips) {
                   6753:                 if (ref($access_hash->{$ipkey}{'ip'}) eq 'ARRAY') {
                   6754:                     if (&Apache::loncommon::check_ip_acc(join(',',@{$access_hash->{$ipkey}{'ip'}}),$clientip)) {
                   6755:                         $allowed = 1;
                   6756:                         last; 
                   6757:                     }
                   6758:                 }
                   6759:             }
                   6760:             if ($allowed) {
                   6761:                 return 'ok';
                   6762:             }
1.765     albertel 6763:         }
                   6764:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   6765:             if ($guest) {
                   6766:                 return $guest;
                   6767:             }
                   6768:         } else {
                   6769:             if (@domains > 0) {
                   6770:                 foreach my $domkey (@domains) {
                   6771:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   6772:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   6773:                             return 'ok';
                   6774:                         }
                   6775:                     }
                   6776:                 }
                   6777:             }
                   6778:             if (@users > 0) {
                   6779:                 foreach my $userkey (@users) {
1.865     raeburn  6780:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   6781:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   6782:                             if (ref($item) eq 'HASH') {
                   6783:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   6784:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   6785:                                     return 'ok';
                   6786:                                 }
                   6787:                             }
                   6788:                         }
                   6789:                     } 
1.765     albertel 6790:                 }
                   6791:             }
                   6792:             my %roleshash;
                   6793:             my @courses_and_groups = @courses;
                   6794:             push(@courses_and_groups,@groups); 
                   6795:             if (@courses_and_groups > 0) {
                   6796:                 my (%allgroups,%allroles); 
                   6797:                 my ($start,$end,$role,$sec,$group);
                   6798:                 foreach my $envkey (%env) {
1.1060    raeburn  6799:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 6800:                         my $cid = $2.'_'.$3; 
                   6801:                         if ($1 eq 'gr') {
                   6802:                             $group = $4;
                   6803:                             $allgroups{$cid}{$group} = $env{$envkey};
                   6804:                         } else {
                   6805:                             if ($4 eq '') {
                   6806:                                 $sec = 'none';
                   6807:                             } else {
                   6808:                                 $sec = $4;
                   6809:                             }
                   6810:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   6811:                         }
1.811     albertel 6812:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 6813:                         my $cid = $2.'_'.$3;
                   6814:                         if ($4 eq '') {
                   6815:                             $sec = 'none';
                   6816:                         } else {
                   6817:                             $sec = $4;
                   6818:                         }
                   6819:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   6820:                     }
                   6821:                 }
                   6822:                 if (keys(%allroles) == 0) {
                   6823:                     return;
                   6824:                 }
                   6825:                 foreach my $key (@courses_and_groups) {
                   6826:                     my %content = %{$$access_hash{$key}};
                   6827:                     my $cnum = $content{'number'};
                   6828:                     my $cdom = $content{'domain'};
                   6829:                     my $cid = $cdom.'_'.$cnum;
                   6830:                     if (!exists($allroles{$cid})) {
                   6831:                         next;
                   6832:                     }    
                   6833:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   6834:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   6835:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   6836:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   6837:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   6838:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   6839:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   6840:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   6841:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   6842:                                         if (grep/^all$/,@sections) {
                   6843:                                             return 'ok';
                   6844:                                         } else {
                   6845:                                             if (grep/^$sec$/,@sections) {
                   6846:                                                 return 'ok';
                   6847:                                             }
                   6848:                                         }
                   6849:                                     }
                   6850:                                 }
                   6851:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   6852:                                     if (grep/^none$/,@groups) {
                   6853:                                         return 'ok';
                   6854:                                     }
                   6855:                                 } else {
                   6856:                                     if (grep/^all$/,@groups) {
                   6857:                                         return 'ok';
                   6858:                                     } 
                   6859:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   6860:                                         if (grep/^$group$/,@groups) {
                   6861:                                             return 'ok';
                   6862:                                         }
                   6863:                                     }
                   6864:                                 } 
                   6865:                             }
                   6866:                         }
                   6867:                     }
                   6868:                 }
                   6869:             }
                   6870:             if ($guest) {
                   6871:                 return $guest;
                   6872:             }
                   6873:         }
                   6874:     }
                   6875:     return;
                   6876: }
                   6877: 
                   6878: sub course_group_datechecker {
                   6879:     my ($dates,$now,$status) = @_;
                   6880:     my ($start,$end) = split(/\./,$dates);
                   6881:     if (!$start && !$end) {
                   6882:         return 'ok';
                   6883:     }
                   6884:     if (grep/^active$/,@{$status}) {
                   6885:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   6886:             return 'ok';
                   6887:         }
                   6888:     }
                   6889:     if (grep/^previous$/,@{$status}) {
                   6890:         if ($end > $now ) {
                   6891:             return 'ok';
                   6892:         }
                   6893:     }
                   6894:     if (grep/^future$/,@{$status}) {
                   6895:         if ($start > $now) {
                   6896:             return 'ok';
                   6897:         }
                   6898:     }
                   6899:     return; 
                   6900: }
                   6901: 
                   6902: sub parse_portfolio_url {
                   6903:     my ($url) = @_;
                   6904: 
                   6905:     my ($type,$udom,$unum,$group,$file_name);
                   6906:     
1.823     albertel 6907:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 6908: 	$type = 1;
                   6909:         $udom = $1;
                   6910:         $unum = $2;
                   6911:         $file_name = $3;
1.823     albertel 6912:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 6913: 	$type = 2;
                   6914:         $udom = $1;
                   6915:         $unum = $2;
                   6916:         $group = $3;
                   6917:         $file_name = $3.'/'.$4;
                   6918:     }
                   6919:     if (wantarray) {
                   6920: 	return ($type,$udom,$unum,$file_name,$group);
                   6921:     }
                   6922:     return $type;
                   6923: }
                   6924: 
                   6925: sub is_portfolio_url {
                   6926:     my ($url) = @_;
                   6927:     return scalar(&parse_portfolio_url($url));
                   6928: }
                   6929: 
1.798     raeburn  6930: sub is_portfolio_file {
                   6931:     my ($file) = @_;
1.820     raeburn  6932:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  6933:         return 1;
                   6934:     }
                   6935:     return;
                   6936: }
                   6937: 
1.976     raeburn  6938: sub usertools_access {
1.1084    raeburn  6939:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  6940:     my ($access,%tools);
                   6941:     if ($context eq '') {
                   6942:         $context = 'tools';
                   6943:     }
                   6944:     if ($context eq 'requestcourses') {
                   6945:         %tools = (
                   6946:                       official   => 1,
                   6947:                       unofficial => 1,
1.1006    raeburn  6948:                       community  => 1,
1.1246    raeburn  6949:                       textbook   => 1,
1.1305    raeburn  6950:                       placement  => 1,
1.985     raeburn  6951:                  );
1.1183    raeburn  6952:     } elsif ($context eq 'requestauthor') {
                   6953:         %tools = (
                   6954:                       requestauthor => 1,
                   6955:                  );
1.985     raeburn  6956:     } else {
                   6957:         %tools = (
                   6958:                       aboutme   => 1,
                   6959:                       blog      => 1,
1.1177    raeburn  6960:                       webdav    => 1,
1.985     raeburn  6961:                       portfolio => 1,
                   6962:                  );
                   6963:     }
1.976     raeburn  6964:     return if (!defined($tools{$tool}));
                   6965: 
1.1242    raeburn  6966:     if (($udom eq '') || ($uname eq '')) {
1.976     raeburn  6967:         $udom = $env{'user.domain'};
                   6968:         $uname = $env{'user.name'};
                   6969:     }
                   6970: 
1.978     raeburn  6971:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   6972:         if ($action ne 'reload') {
1.985     raeburn  6973:             if ($context eq 'requestcourses') {
                   6974:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  6975:             } elsif ($context eq 'requestauthor') {
                   6976:                 return $env{'environment.canrequest.author'};
1.985     raeburn  6977:             } else {
                   6978:                 return $env{'environment.availabletools.'.$tool};
                   6979:             }
                   6980:         }
1.976     raeburn  6981:     }
                   6982: 
1.1183    raeburn  6983:     my ($toolstatus,$inststatus,$envkey);
                   6984:     if ($context eq 'requestauthor') {
                   6985:         $envkey = $context; 
                   6986:     } else {
                   6987:         $envkey = $context.'.'.$tool;
                   6988:     }
1.976     raeburn  6989: 
1.985     raeburn  6990:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   6991:          ($action ne 'reload')) {
1.1183    raeburn  6992:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  6993:         $inststatus = $env{'environment.inststatus'};
                   6994:     } else {
1.1084    raeburn  6995:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  6996:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  6997:             $inststatus = $userenvref->{'inststatus'};
                   6998:         } else {
1.1183    raeburn  6999:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   7000:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  7001:             $inststatus = $userenv{'inststatus'};
                   7002:         }
1.976     raeburn  7003:     }
                   7004: 
                   7005:     if ($toolstatus ne '') {
                   7006:         if ($toolstatus) {
                   7007:             $access = 1;
                   7008:         } else {
                   7009:             $access = 0;
                   7010:         }
                   7011:         return $access;
                   7012:     }
                   7013: 
1.1084    raeburn  7014:     my ($is_adv,%domdef);
                   7015:     if (ref($is_advref) eq 'HASH') {
                   7016:         $is_adv = $is_advref->{'is_adv'};
                   7017:     } else {
                   7018:         $is_adv = &is_advanced_user($udom,$uname);
                   7019:     }
                   7020:     if (ref($domdefref) eq 'HASH') {
                   7021:         %domdef = %{$domdefref};
                   7022:     } else {
                   7023:         %domdef = &get_domain_defaults($udom);
                   7024:     }
1.976     raeburn  7025:     if (ref($domdef{$tool}) eq 'HASH') {
                   7026:         if ($is_adv) {
                   7027:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   7028:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   7029:                     $access = 1;
                   7030:                 } else {
                   7031:                     $access = 0;
                   7032:                 }
                   7033:                 return $access;
                   7034:             }
                   7035:         }
                   7036:         if ($inststatus ne '') {
                   7037:             my ($hasaccess,$hasnoaccess);
                   7038:             foreach my $affiliation (split(/:/,$inststatus)) {
                   7039:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   7040:                     if ($domdef{$tool}{$affiliation}) {
                   7041:                         $hasaccess = 1;
                   7042:                     } else {
                   7043:                         $hasnoaccess = 1;
                   7044:                     }
                   7045:                 }
                   7046:             }
                   7047:             if ($hasaccess || $hasnoaccess) {
                   7048:                 if ($hasaccess) {
                   7049:                     $access = 1;
                   7050:                 } elsif ($hasnoaccess) {
                   7051:                     $access = 0; 
                   7052:                 }
                   7053:                 return $access;
                   7054:             }
                   7055:         } else {
                   7056:             if ($domdef{$tool}{'default'} ne '') {
                   7057:                 if ($domdef{$tool}{'default'}) {
                   7058:                     $access = 1;
                   7059:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   7060:                     $access = 0;
                   7061:                 }
                   7062:                 return $access;
                   7063:             }
                   7064:         }
                   7065:     } else {
1.1177    raeburn  7066:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  7067:             $access = 1;
                   7068:         } else {
                   7069:             $access = 0;
                   7070:         }
1.976     raeburn  7071:         return $access;
                   7072:     }
                   7073: }
                   7074: 
1.1050    raeburn  7075: sub is_course_owner {
                   7076:     my ($cdom,$cnum,$udom,$uname) = @_;
                   7077:     if (($udom eq '') || ($uname eq '')) {
                   7078:         $udom = $env{'user.domain'};
                   7079:         $uname = $env{'user.name'};
                   7080:     }
                   7081:     unless (($udom eq '') || ($uname eq '')) {
                   7082:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   7083:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   7084:                 return 1;
                   7085:             } else {
                   7086:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   7087:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   7088:                     return 1;
                   7089:                 }
                   7090:             }
                   7091:         }
                   7092:     }
                   7093:     return;
                   7094: }
                   7095: 
1.976     raeburn  7096: sub is_advanced_user {
                   7097:     my ($udom,$uname) = @_;
1.1085    raeburn  7098:     if ($udom ne '' && $uname ne '') {
                   7099:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  7100:             if (wantarray) {
                   7101:                 return ($env{'user.adv'},$env{'user.author'});
                   7102:             } else {
                   7103:                 return $env{'user.adv'};
                   7104:             }
1.1085    raeburn  7105:         }
                   7106:     }
1.976     raeburn  7107:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   7108:     my %allroles;
1.1128    raeburn  7109:     my ($is_adv,$is_author);
1.976     raeburn  7110:     foreach my $role (keys(%roleshash)) {
                   7111:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   7112:         my $area = '/'.$tdomain.'/'.$trest;
                   7113:         if ($sec ne '') {
                   7114:             $area .= '/'.$sec;
                   7115:         }
                   7116:         if (($area ne '') && ($trole ne '')) {
                   7117:             my $spec=$trole.'.'.$area;
                   7118:             if ($trole =~ /^cr\//) {
                   7119:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   7120:             } elsif ($trole ne 'gr') {
                   7121:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   7122:             }
1.1128    raeburn  7123:             if ($trole eq 'au') {
                   7124:                 $is_author = 1;
                   7125:             }
1.976     raeburn  7126:         }
                   7127:     }
                   7128:     foreach my $role (keys(%allroles)) {
                   7129:         last if ($is_adv);
                   7130:         foreach my $item (split(/:/,$allroles{$role})) {
                   7131:             if ($item ne '') {
                   7132:                 my ($privilege,$restrictions)=split(/&/,$item);
                   7133:                 if ($privilege eq 'adv') {
                   7134:                     $is_adv = 1;
                   7135:                     last;
                   7136:                 }
                   7137:             }
                   7138:         }
                   7139:     }
1.1128    raeburn  7140:     if (wantarray) {
                   7141:         return ($is_adv,$is_author);
                   7142:     }
1.976     raeburn  7143:     return $is_adv;
                   7144: }
1.798     raeburn  7145: 
1.1035    raeburn  7146: sub check_can_request {
1.1036    raeburn  7147:     my ($dom,$can_request,$request_domains) = @_;
1.1035    raeburn  7148:     my $canreq = 0;
                   7149:     my ($types,$typename) = &Apache::loncommon::course_types();
                   7150:     my @options = ('approval','validate','autolimit');
                   7151:     my $optregex = join('|',@options);
                   7152:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   7153:         foreach my $type (@{$types}) {
                   7154:             if (&usertools_access($env{'user.name'},
                   7155:                                   $env{'user.domain'},
                   7156:                                   $type,undef,'requestcourses')) {
                   7157:                 $canreq ++;
1.1036    raeburn  7158:                 if (ref($request_domains) eq 'HASH') {
                   7159:                     push(@{$request_domains->{$type}},$env{'user.domain'});
                   7160:                 }
1.1035    raeburn  7161:                 if ($dom eq $env{'user.domain'}) {
                   7162:                     $can_request->{$type} = 1;
                   7163:                 }
                   7164:             }
                   7165:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                   7166:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   7167:                 if (@curr > 0) {
1.1036    raeburn  7168:                     foreach my $item (@curr) {
                   7169:                         if (ref($request_domains) eq 'HASH') {
                   7170:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   7171:                             if ($otherdom ne '') {
                   7172:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   7173:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   7174:                                         push(@{$request_domains->{$type}},$otherdom);
                   7175:                                     }
                   7176:                                 } else {
                   7177:                                     push(@{$request_domains->{$type}},$otherdom);
                   7178:                                 }
                   7179:                             }
                   7180:                         }
                   7181:                     }
                   7182:                     unless($dom eq $env{'user.domain'}) {
                   7183:                         $canreq ++;
1.1035    raeburn  7184:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   7185:                             $can_request->{$type} = 1;
                   7186:                         }
                   7187:                     }
                   7188:                 }
                   7189:             }
                   7190:         }
                   7191:     }
                   7192:     return $canreq;
                   7193: }
                   7194: 
1.341     www      7195: # ---------------------------------------------- Custom access rule evaluation
                   7196: 
                   7197: sub customaccess {
                   7198:     my ($priv,$uri)=@_;
1.807     albertel 7199:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      7200:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 7201:     $udom = &LONCAPA::clean_domain($udom);
                   7202:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      7203:     my $access=0;
1.800     albertel 7204:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 7205: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   7206: 	if ($type eq 'user') {
                   7207: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 7208: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 7209: 		if ($tdom) {
                   7210: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   7211: 		}
1.896     albertel 7212: 		if ($tuname) {
                   7213: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 7214: 		}
                   7215: 		$access=($effect eq 'allow');
                   7216: 		last;
                   7217: 	    }
                   7218: 	} else {
                   7219: 	    if ($role) {
                   7220: 		if ($role ne $urole) { next; }
                   7221: 	    }
                   7222: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   7223: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   7224: 		if ($tdom) {
                   7225: 		    if ($tdom ne $udom) { next; }
                   7226: 		}
                   7227: 		if ($tcrs) {
                   7228: 		    if ($tcrs ne $ucrs) { next; }
                   7229: 		}
                   7230: 		if ($tsec) {
                   7231: 		    if ($tsec ne $usec) { next; }
                   7232: 		}
                   7233: 		$access=($effect eq 'allow');
                   7234: 		last;
                   7235: 	    }
                   7236: 	    if ($realm eq '' && $role eq '') {
                   7237: 		$access=($effect eq 'allow');
                   7238: 	    }
1.402     bowersj2 7239: 	}
1.341     www      7240:     }
                   7241:     return $access;
                   7242: }
                   7243: 
1.103     harris41 7244: # ------------------------------------------------- Check for a user privilege
1.12      www      7245: 
                   7246: sub allowed {
1.1281    raeburn  7247:     my ($priv,$uri,$symb,$role,$clientip,$noblockcheck)=@_;
1.705     albertel 7248:     my $ver_orguri=$uri;
1.439     www      7249:     $uri=&deversion($uri);
1.152     www      7250:     my $orguri=$uri;
1.52      www      7251:     $uri=&declutter($uri);
1.809     raeburn  7252: 
1.810     raeburn  7253:     if ($priv eq 'evb') {
                   7254: # Evade communication block restrictions for specified role in a course
                   7255:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   7256:             return $1;
                   7257:         } else {
                   7258:             return;
                   7259:         }
                   7260:     }
                   7261: 
1.620     albertel 7262:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      7263: # Free bre access to adm and meta resources
1.1343    raeburn  7264:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard|ext\.tool)$})) 
1.769     albertel 7265: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   7266: 	&& ($priv eq 'bre')) {
1.14      www      7267: 	return 'F';
1.159     www      7268:     }
                   7269: 
1.545     banghart 7270: # Free bre access to user's own portfolio contents
1.714     raeburn  7271:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  7272:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  7273: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  7274:         my %setters;
                   7275:         my ($startblock,$endblock) = 
                   7276:             &Apache::loncommon::blockcheck(\%setters,'port');
                   7277:         if ($startblock && $endblock) {
                   7278:             return 'B';
                   7279:         } else {
                   7280:             return 'F';
                   7281:         }
1.545     banghart 7282:     }
                   7283: 
1.762     raeburn  7284: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  7285:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   7286:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   7287:         if (exists($env{'request.course.id'})) {
                   7288:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7289:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7290:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   7291:                 my $courseprivid=$env{'request.course.id'};
                   7292:                 $courseprivid=~s/\_/\//;
                   7293:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   7294:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   7295:                     return $1; 
1.762     raeburn  7296:                 } else {
                   7297:                     if ($env{'request.course.sec'}) {
                   7298:                         $courseprivid.='/'.$env{'request.course.sec'};
                   7299:                     }
                   7300:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   7301:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   7302:                         return $2;
                   7303:                     }
1.714     raeburn  7304:                 }
                   7305:             }
                   7306:         }
                   7307:     }
                   7308: 
1.159     www      7309: # Free bre to public access
                   7310: 
                   7311:     if ($priv eq 'bre') {
1.238     www      7312:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 7313: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      7314:            return 'F'; 
                   7315:         }
1.238     www      7316:         if ($copyright eq 'priv') {
                   7317:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 7318: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      7319: 		return '';
                   7320:             }
                   7321:         }
                   7322:         if ($copyright eq 'domain') {
                   7323:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 7324: 	    unless (($env{'user.domain'} eq $1) ||
                   7325:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      7326: 		return '';
                   7327:             }
1.262     matthew  7328:         }
1.620     albertel 7329:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  7330:             # Library role, so allow browsing of resources in this domain.
                   7331:             return 'F';
1.238     www      7332:         }
1.341     www      7333:         if ($copyright eq 'custom') {
                   7334: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   7335:         }
1.14      www      7336:     }
1.264     matthew  7337:     # Domain coordinator is trying to create a course
1.620     albertel 7338:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  7339:         # uri is the requested domain in this case.
                   7340:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  7341:         # a role of dc for the domain in question.
1.620     albertel 7342:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  7343:     }
1.29      www      7344: 
1.52      www      7345:     my $thisallowed='';
                   7346:     my $statecond=0;
                   7347:     my $courseprivid='';
                   7348: 
1.1039    raeburn  7349:     my $ownaccess;
1.1043    raeburn  7350:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  7351:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   7352:         if ($uri eq '') {
                   7353:             $ownaccess = 1;
                   7354:         } else {
                   7355:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   7356:                 my $udom = $env{'user.domain'};
                   7357:                 my $uname = $env{'user.name'};
                   7358:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   7359:                     $ownaccess = 1;
1.1040    raeburn  7360:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  7361:                     unless ($uri =~ m{\.\./}) {
                   7362:                         $ownaccess = 1;
                   7363:                     }
                   7364:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   7365:                     my $now = time;
                   7366:                     if ($uri =~ m{^([^/]+)/?$}) {
                   7367:                         my $adom = $1;
                   7368:                         foreach my $key (keys(%env)) {
1.1042    raeburn  7369:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  7370:                                 my ($start,$end) = split('.',$env{$key});
                   7371:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   7372:                                     $ownaccess = 1;
                   7373:                                     last;
                   7374:                                 }
                   7375:                             }
                   7376:                         }
                   7377:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   7378:                         my $adom = $1;
                   7379:                         my $aname = $2;
1.1042    raeburn  7380:                         foreach my $role ('ca','aa') { 
                   7381:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   7382:                                 my ($start,$end) =
                   7383:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   7384:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   7385:                                     $ownaccess = 1;
                   7386:                                     last;
                   7387:                                 }
1.1039    raeburn  7388:                             }
                   7389:                         }
                   7390:                     }
                   7391:                 }
                   7392:             }
                   7393:         }
                   7394:     }
                   7395: 
1.52      www      7396: # Course
                   7397: 
1.620     albertel 7398:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  7399:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  7400:             $thisallowed.=$1;
                   7401:         }
1.52      www      7402:     }
1.29      www      7403: 
1.52      www      7404: # Domain
                   7405: 
1.620     albertel 7406:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 7407:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  7408:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  7409:             $thisallowed.=$1;
                   7410:         }
1.12      www      7411:     }
1.52      www      7412: 
1.1141    raeburn  7413: # User who is not author or co-author might still be able to edit
                   7414: # resource of an author in the domain (e.g., if Domain Coordinator).
                   7415:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   7416:         (&allowed('mdc',$env{'request.course.id'}))) {
                   7417:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   7418:             $thisallowed.=$1;
                   7419:         }
                   7420:     }
                   7421: 
1.52      www      7422: # Course: uri itself is a course
1.66      www      7423:     my $courseuri=$uri;
                   7424:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      7425:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      7426: 
1.620     albertel 7427:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 7428:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  7429:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  7430:             $thisallowed.=$1;
                   7431:         }
1.12      www      7432:     }
1.29      www      7433: 
1.665     albertel 7434: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 7435: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 7436:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 7437: 	$thisallowed='';
1.671     raeburn  7438:         my ($match)=&is_on_map($uri);
                   7439:         if ($match) {
                   7440:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   7441:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1281    raeburn  7442:                 my $value = $1;
                   7443:                 if ($noblockcheck) {
                   7444:                     $thisallowed.=$value;
1.1162    raeburn  7445:                 } else {
1.1281    raeburn  7446:                     my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   7447:                     if (@blockers > 0) {
                   7448:                         $thisallowed = 'B';
                   7449:                     } else {
                   7450:                         $thisallowed.=$value;
                   7451:                     }
1.1162    raeburn  7452:                 }
1.671     raeburn  7453:             }
                   7454:         } else {
1.705     albertel 7455:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  7456:             if ($refuri) {
                   7457:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  7458:                     $thisallowed='F';
1.671     raeburn  7459:                 } else {
                   7460:                     $refuri=&declutter($refuri);
                   7461:                     my ($match) = &is_on_map($refuri);
                   7462:                     if ($match) {
1.1281    raeburn  7463:                         if ($noblockcheck) {
                   7464:                             $thisallowed='F';
1.1162    raeburn  7465:                         } else {
1.1281    raeburn  7466:                             my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   7467:                             if (@blockers > 0) {
                   7468:                                 $thisallowed = 'B';
                   7469:                             } else {
                   7470:                                 $thisallowed='F';
                   7471:                             }
1.1162    raeburn  7472:                         }
1.671     raeburn  7473:                     }
1.669     raeburn  7474:                 }
1.671     raeburn  7475:             }
                   7476:         }
1.314     www      7477:     }
1.492     albertel 7478: 
1.766     albertel 7479:     if ($priv eq 'bre'
                   7480: 	&& $thisallowed ne 'F' 
                   7481: 	&& $thisallowed ne '2'
                   7482: 	&& &is_portfolio_url($uri)) {
1.1270    raeburn  7483: 	$thisallowed = &portfolio_access($uri,$clientip);
1.766     albertel 7484:     }
1.1250    raeburn  7485: 
1.52      www      7486: # Full access at system, domain or course-wide level? Exit.
1.29      www      7487:     if ($thisallowed=~/F/) {
                   7488: 	return 'F';
                   7489:     }
                   7490: 
1.52      www      7491: # If this is generating or modifying users, exit with special codes
1.29      www      7492: 
1.643     www      7493:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   7494: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 7495: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      7496: # no author name given, so this just checks on the general right to make a co-author in this domain
                   7497: 	    unless ($auname) { return $thisallowed; }
                   7498: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 7499: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   7500: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   7501: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   7502: 	}
1.52      www      7503: 	return $thisallowed;
                   7504:     }
                   7505: #
1.103     harris41 7506: # Gathered so far: system, domain and course wide privileges
1.52      www      7507: #
                   7508: # Course: See if uri or referer is an individual resource that is part of 
                   7509: # the course
                   7510: 
1.620     albertel 7511:     if ($env{'request.course.id'}) {
1.232     www      7512: 
1.620     albertel 7513:        $courseprivid=$env{'request.course.id'};
                   7514:        if ($env{'request.course.sec'}) {
                   7515:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      7516:        }
                   7517:        $courseprivid=~s/\_/\//;
                   7518:        my $checkreferer=1;
1.232     www      7519:        my ($match,$cond)=&is_on_map($uri);
                   7520:        if ($match) {
                   7521:            $statecond=$cond;
1.620     albertel 7522:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 7523:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  7524:                my $value = $1;
                   7525:                if ($priv eq 'bre') {
1.1281    raeburn  7526:                    if ($noblockcheck) {
                   7527:                        $thisallowed.=$value;
1.1162    raeburn  7528:                    } else {
1.1281    raeburn  7529:                        my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   7530:                        if (@blockers > 0) {
                   7531:                            $thisallowed = 'B';
                   7532:                        } else {
                   7533:                            $thisallowed.=$value;
                   7534:                        }
1.1162    raeburn  7535:                    }
                   7536:                } else {
                   7537:                    $thisallowed.=$value;
                   7538:                }
1.52      www      7539:                $checkreferer=0;
                   7540:            }
1.29      www      7541:        }
1.83      www      7542:        
1.148     www      7543:        if ($checkreferer) {
1.620     albertel 7544: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      7545:             unless ($refuri) {
1.800     albertel 7546:                 foreach my $key (keys(%env)) {
                   7547: 		    if ($key=~/^httpref\..*\*/) {
                   7548: 			my $pattern=$key;
1.156     www      7549:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      7550:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   7551:                         $pattern=~s/\//\\\//g;
1.152     www      7552:                         if ($orguri=~/$pattern/) {
1.800     albertel 7553: 			    $refuri=$env{$key};
1.148     www      7554:                         }
                   7555:                     }
1.191     harris41 7556:                 }
1.148     www      7557:             }
1.232     www      7558: 
1.148     www      7559:          if ($refuri) { 
1.152     www      7560: 	  $refuri=&declutter($refuri);
1.232     www      7561:           my ($match,$cond)=&is_on_map($refuri);
                   7562:             if ($match) {
                   7563:               my $refstatecond=$cond;
1.620     albertel 7564:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 7565:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  7566:                   my $value = $1;
                   7567:                   if ($priv eq 'bre') {
1.1281    raeburn  7568:                       if ($noblockcheck) {
                   7569:                           $thisallowed.=$value;
1.1162    raeburn  7570:                       } else {
1.1281    raeburn  7571:                           my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   7572:                           if (@blockers > 0) {
                   7573:                               $thisallowed = 'B';
                   7574:                           } else {
                   7575:                               $thisallowed.=$value;
                   7576:                           }
1.1162    raeburn  7577:                       }
                   7578:                   } else {
                   7579:                       $thisallowed.=$value;
                   7580:                   }
1.53      www      7581:                   $uri=$refuri;
                   7582:                   $statecond=$refstatecond;
1.52      www      7583:               }
                   7584:           }
1.148     www      7585:         }
1.29      www      7586:        }
1.52      www      7587:    }
1.29      www      7588: 
1.52      www      7589: #
1.103     harris41 7590: # Gathered now: all privileges that could apply, and condition number
1.52      www      7591: # 
                   7592: #
                   7593: # Full or no access?
                   7594: #
1.29      www      7595: 
1.52      www      7596:     if ($thisallowed=~/F/) {
                   7597: 	return 'F';
                   7598:     }
1.29      www      7599: 
1.52      www      7600:     unless ($thisallowed) {
                   7601:         return '';
                   7602:     }
1.29      www      7603: 
1.52      www      7604: # Restrictions exist, deal with them
                   7605: #
                   7606: #   C:according to course preferences
                   7607: #   R:according to resource settings
                   7608: #   L:unless locked
                   7609: #   X:according to user session state
                   7610: #
                   7611: 
                   7612: # Possibly locked functionality, check all courses
1.54      www      7613: # Locks might take effect only after 10 minutes cache expiration for other
                   7614: # courses, and 2 minutes for current course
1.52      www      7615: 
                   7616:     my $envkey;
                   7617:     if ($thisallowed=~/L/) {
1.1000    raeburn  7618:         foreach $envkey (keys(%env)) {
1.54      www      7619:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   7620:                my $courseid=$2;
                   7621:                my $roleid=$1.'.'.$2;
1.92      www      7622:                $courseid=~s/^\///;
1.54      www      7623:                my $expiretime=600;
1.620     albertel 7624:                if ($env{'request.role'} eq $roleid) {
1.54      www      7625: 		  $expiretime=120;
                   7626:                }
                   7627: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   7628:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 7629:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 7630: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      7631:                }
1.620     albertel 7632:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   7633:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   7634: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   7635:                        &log($env{'user.domain'},$env{'user.name'},
                   7636:                             $env{'user.home'},
1.57      www      7637:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      7638:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 7639:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      7640: 		       return '';
                   7641:                    }
                   7642:                }
1.620     albertel 7643:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   7644:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   7645: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   7646:                        &log($env{'user.domain'},$env{'user.name'},
                   7647:                             $env{'user.home'},
1.57      www      7648:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      7649:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 7650:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      7651: 		       return '';
                   7652:                    }
                   7653:                }
                   7654: 	   }
1.29      www      7655:        }
1.52      www      7656:     }
                   7657:    
                   7658: #
                   7659: # Rest of the restrictions depend on selected course
                   7660: #
                   7661: 
1.620     albertel 7662:     unless ($env{'request.course.id'}) {
1.766     albertel 7663: 	if ($thisallowed eq 'A') {
                   7664: 	    return 'A';
1.814     raeburn  7665:         } elsif ($thisallowed eq 'B') {
                   7666:             return 'B';
1.766     albertel 7667: 	} else {
                   7668: 	    return '1';
                   7669: 	}
1.52      www      7670:     }
1.29      www      7671: 
1.52      www      7672: #
                   7673: # Now user is definitely in a course
                   7674: #
1.53      www      7675: 
                   7676: 
                   7677: # Course preferences
                   7678: 
                   7679:    if ($thisallowed=~/C/) {
1.620     albertel 7680:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   7681:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   7682:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 7683: 	   =~/\Q$rolecode\E/) {
1.1304    raeburn  7684: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 7685: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   7686: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   7687: 			$env{'request.course.id'});
                   7688: 	   }
1.237     www      7689:            return '';
                   7690:        }
                   7691: 
1.620     albertel 7692:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 7693: 	   =~/\Q$unamedom\E/) {
1.1304    raeburn  7694: 	   if (($priv ne 'pch') && ($priv ne 'plc') && ($priv ne 'pac')) {
1.689     albertel 7695: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   7696: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   7697: 			$env{'request.course.id'});
                   7698: 	   }
1.54      www      7699:            return '';
                   7700:        }
1.53      www      7701:    }
                   7702: 
                   7703: # Resource preferences
                   7704: 
                   7705:    if ($thisallowed=~/R/) {
1.620     albertel 7706:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 7707:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  7708: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 7709: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   7710: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   7711: 	   }
                   7712: 	   return '';
1.54      www      7713:        }
1.53      www      7714:    }
1.30      www      7715: 
1.246     www      7716: # Restricted by state or randomout?
1.30      www      7717: 
1.52      www      7718:    if ($thisallowed=~/X/) {
1.620     albertel 7719:       if ($env{'acc.randomout'}) {
1.579     albertel 7720: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 7721:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      7722:             return ''; 
                   7723:          }
1.247     www      7724:       }
                   7725:       if (&condval($statecond)) {
1.52      www      7726: 	 return '2';
                   7727:       } else {
                   7728:          return '';
                   7729:       }
                   7730:    }
1.30      www      7731: 
1.766     albertel 7732:     if ($thisallowed eq 'A') {
                   7733: 	return 'A';
1.814     raeburn  7734:     } elsif ($thisallowed eq 'B') {
                   7735:         return 'B';
1.766     albertel 7736:     }
1.52      www      7737:    return 'F';
1.232     www      7738: }
1.1162    raeburn  7739: 
1.1192    raeburn  7740: # ------------------------------------------- Check construction space access
                   7741: 
                   7742: sub constructaccess {
                   7743:     my ($url,$setpriv)=@_;
                   7744: 
                   7745: # We do not allow editing of previous versions of files
                   7746:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   7747: 
                   7748: # Get username and domain from URL
                   7749:     my ($ownername,$ownerdomain,$ownerhome);
                   7750: 
                   7751:     ($ownerdomain,$ownername) =
1.1325    raeburn  7752:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)(?:/daxepage|/daxeopen)?/priv/($match_domain)/($match_username)(?:/|$)});
1.1192    raeburn  7753: 
                   7754: # The URL does not really point to any authorspace, forget it
                   7755:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   7756: 
                   7757: # Now we need to see if the user has access to the authorspace of
                   7758: # $ownername at $ownerdomain
                   7759: 
                   7760:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   7761: # Real author for this?
                   7762:        $ownerhome = $env{'user.home'};
                   7763:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   7764:           return ($ownername,$ownerdomain,$ownerhome);
                   7765:        }
                   7766:     } else {
                   7767: # Co-author for this?
                   7768:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   7769:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   7770:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   7771:             return ($ownername,$ownerdomain,$ownerhome);
                   7772:         }
1.1312    raeburn  7773:         if ($env{'request.course.id'}) {
                   7774:             if (($ownername eq $env{'course.'.$env{'request.course.id'}.'.num'}) &&
                   7775:                 ($ownerdomain eq $env{'course.'.$env{'request.course.id'}.'.domain'})) {
                   7776:                 if (&allowed('mdc',$env{'request.course.id'})) {
                   7777:                     $ownerhome = $env{'course.'.$env{'request.course.id'}.'.home'};
                   7778:                     return ($ownername,$ownerdomain,$ownerhome);
                   7779:                 }
                   7780:             }
                   7781:         }
1.1192    raeburn  7782:     }
                   7783: 
                   7784: # We don't have any access right now. If we are not possibly going to do anything about this,
                   7785: # we might as well leave
                   7786:    unless ($setpriv) { return ''; }
                   7787: 
                   7788: # Backdoor access?
                   7789:     my $allowed=&allowed('eco',$ownerdomain);
                   7790: # Nope
                   7791:     unless ($allowed) { return ''; }
                   7792: # Looks like we may have access, but could be locked by the owner of the construction space
                   7793:     if ($allowed eq 'U') {
                   7794:         my %blocked=&get('environment',['domcoord.author'],
                   7795:                          $ownerdomain,$ownername);
                   7796: # Is blocked by owner
                   7797:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   7798:     }
                   7799:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   7800: # Grant temporary access
                   7801:         my $then=$env{'user.login.time'};
1.1209    raeburn  7802:         my $update=$env{'user.update.time'};
1.1192    raeburn  7803:         if (!$update) { $update = $then; }
                   7804:         my $refresh=$env{'user.refresh.time'};
                   7805:         if (!$refresh) { $refresh = $update; }
                   7806:         my $now = time;
                   7807:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   7808:                            $now,'ca','constructaccess');
                   7809:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   7810:         return($ownername,$ownerdomain,$ownerhome);
                   7811:     }
                   7812: # No business here
                   7813:     return '';
                   7814: }
                   7815: 
1.1282    raeburn  7816: # ----------------------------------------------------------- Content Blocking
                   7817: 
                   7818: {
                   7819: # Caches for faster Course Contents display where content blocking
                   7820: # is in operation (i.e., interval param set) for timed quiz.
                   7821: #
                   7822: # User for whom data are being temporarily cached.
                   7823: my $cacheduser='';
                   7824: # Cached blockers for this user (a hash of blocking items). 
                   7825: my %cachedblockers=();
                   7826: # When the data were last cached.
                   7827: my $cachedlast='';
                   7828: 
                   7829: sub load_all_blockers {
                   7830:     my ($uname,$udom,$blocks)=@_;
                   7831:     if (($uname ne '') && ($udom ne '')) { 
                   7832:         if (($cacheduser eq $uname.':'.$udom) &&
                   7833:             (abs($cachedlast-time)<5)) {
                   7834:             return;
                   7835:         }
                   7836:     }
                   7837:     $cachedlast=time;
                   7838:     $cacheduser=$uname.':'.$udom;
                   7839:     %cachedblockers = &get_commblock_resources($blocks);
                   7840: }
                   7841: 
1.1162    raeburn  7842: sub get_comm_blocks {
                   7843:     my ($cdom,$cnum) = @_;
                   7844:     if ($cdom eq '' || $cnum eq '') {
                   7845:         return unless ($env{'request.course.id'});
                   7846:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   7847:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   7848:     }
                   7849:     my %commblocks;
                   7850:     my $hashid=$cdom.'_'.$cnum;
                   7851:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   7852:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   7853:         %commblocks = %{$blocksref};
                   7854:     } else {
                   7855:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   7856:         my $cachetime = 600;
                   7857:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   7858:     }
                   7859:     return %commblocks;
                   7860: }
                   7861: 
1.1282    raeburn  7862: sub get_commblock_resources {
                   7863:     my ($blocks) = @_;
                   7864:     my %blockers = ();
                   7865:     return %blockers unless ($env{'request.course.id'});
                   7866:     return %blockers if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
1.1162    raeburn  7867:     my %commblocks;
                   7868:     if (ref($blocks) eq 'HASH') {
                   7869:         %commblocks = %{$blocks};
                   7870:     } else {
                   7871:         %commblocks = &get_comm_blocks();
                   7872:     }
1.1282    raeburn  7873:     return %blockers unless (keys(%commblocks) > 0); 
                   7874:     my $navmap = Apache::lonnavmaps::navmap->new();
                   7875:     return %blockers unless (ref($navmap));
1.1162    raeburn  7876:     my $now = time;
                   7877:     foreach my $block (keys(%commblocks)) {
                   7878:         if ($block =~ /^(\d+)____(\d+)$/) {
                   7879:             my ($start,$end) = ($1,$2);
                   7880:             if ($start <= $now && $end >= $now) {
                   7881:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   7882:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   7883:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
1.1282    raeburn  7884:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   7885:                                 $blockers{$block}{maps} = $commblocks{$block}{'blocks'}{'docs'}{'maps'}; 
1.1162    raeburn  7886:                             }
                   7887:                         }
                   7888:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
1.1282    raeburn  7889:                             if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   7890:                                 $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1162    raeburn  7891:                             }
                   7892:                         }
                   7893:                     }
                   7894:                 }
                   7895:             }
                   7896:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   7897:             my $item = $1;
1.1163    raeburn  7898:             my @to_test;
1.1162    raeburn  7899:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   7900:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
1.1282    raeburn  7901:                     my @interval;
                   7902:                     my $type = 'map';
                   7903:                     if ($item eq 'course') {
                   7904:                         $type = 'course';
                   7905:                         @interval=&EXT("resource.0.interval");
                   7906:                     } else {
                   7907:                         if ($item =~ /___\d+___/) {
                   7908:                             $type = 'resource';
                   7909:                             @interval=&EXT("resource.0.interval",$item);
                   7910:                             if (ref($navmap)) {                        
                   7911:                                 my $res = $navmap->getBySymb($item); 
                   7912:                                 push(@to_test,$res);
                   7913:                             }
1.1162    raeburn  7914:                         } else {
1.1282    raeburn  7915:                             my $mapsymb = &symbread($item,1);
                   7916:                             if ($mapsymb) {
                   7917:                                 if (ref($navmap)) {
                   7918:                                     my $mapres = $navmap->getBySymb($mapsymb);
                   7919:                                     @to_test = $mapres->retrieveResources($mapres,undef,0,0,0,1);
                   7920:                                     foreach my $res (@to_test) {
                   7921:                                         my $symb = $res->symb();
                   7922:                                         next if ($symb eq $mapsymb);
                   7923:                                         if ($symb ne '') {
                   7924:                                             @interval=&EXT("resource.0.interval",$symb);
                   7925:                                             if ($interval[1] eq 'map') {
                   7926:                                                 last;
1.1162    raeburn  7927:                                             }
                   7928:                                         }
                   7929:                                     }
                   7930:                                 }
                   7931:                             }
                   7932:                         }
1.1282    raeburn  7933:                     }
1.1310    raeburn  7934:                     if ($interval[0] =~ /^(\d+)/) {
1.1308    raeburn  7935:                         my $timelimit = $1; 
1.1282    raeburn  7936:                         my $first_access;
                   7937:                         if ($type eq 'resource') {
                   7938:                             $first_access=&get_first_access($interval[1],$item);
                   7939:                         } elsif ($type eq 'map') {
                   7940:                             $first_access=&get_first_access($interval[1],undef,$item);
                   7941:                         } else {
                   7942:                             $first_access=&get_first_access($interval[1]);
                   7943:                         }
                   7944:                         if ($first_access) {
1.1292    raeburn  7945:                             my $timesup = $first_access+$timelimit;
1.1282    raeburn  7946:                             if ($timesup > $now) {
                   7947:                                 my $activeblock;
                   7948:                                 foreach my $res (@to_test) {
1.1283    raeburn  7949:                                     if ($res->answerable()) {
1.1282    raeburn  7950:                                         $activeblock = 1;
                   7951:                                         last;
                   7952:                                     }
                   7953:                                 }
                   7954:                                 if ($activeblock) {
                   7955:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   7956:                                          if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'maps'}})) {
                   7957:                                              $blockers{$block}{'maps'} = $commblocks{$block}{'blocks'}{'docs'}{'maps'};
                   7958:                                          }
                   7959:                                     }
                   7960:                                     if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   7961:                                         if (keys(%{$commblocks{$block}{'blocks'}{'docs'}{'resources'}})) {
                   7962:                                             $blockers{$block}{'resources'} = $commblocks{$block}{'blocks'}{'docs'}{'resources'};
1.1163    raeburn  7963:                                         }
1.1162    raeburn  7964:                                     }
                   7965:                                 }
                   7966:                             }
                   7967:                         }
                   7968:                     }
                   7969:                 }
                   7970:             }
                   7971:         }
                   7972:     }
1.1282    raeburn  7973:     return %blockers;
1.1162    raeburn  7974: }
                   7975: 
1.1282    raeburn  7976: sub has_comm_blocking {
                   7977:     my ($priv,$symb,$uri,$blocks) = @_;
                   7978:     my @blockers;
                   7979:     return unless ($env{'request.course.id'});
                   7980:     return unless ($priv eq 'bre');
                   7981:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
                   7982:     return if ($env{'request.state'} eq 'construct');
                   7983:     &load_all_blockers($env{'user.name'},$env{'user.domain'},$blocks);
                   7984:     return unless (keys(%cachedblockers) > 0);
                   7985:     my (%possibles,@symbs);
                   7986:     if (!$symb) {
                   7987:         $symb = &symbread($uri,1,1,1,\%possibles);
1.1162    raeburn  7988:     }
1.1282    raeburn  7989:     if ($symb) {
                   7990:         @symbs = ($symb);
                   7991:     } elsif (keys(%possibles)) { 
                   7992:         @symbs = keys(%possibles);
                   7993:     }
                   7994:     my $noblock;
                   7995:     foreach my $symb (@symbs) {
                   7996:         last if ($noblock);
                   7997:         my ($map,$resid,$resurl)=&decode_symb($symb);
                   7998:         foreach my $block (keys(%cachedblockers)) {
                   7999:             if ($block =~ /^firstaccess____(.+)$/) {
                   8000:                 my $item = $1;
                   8001:                 if (($item eq $map) || ($item eq $symb)) {
                   8002:                     $noblock = 1;
                   8003:                     last;
                   8004:                 }
                   8005:             }
                   8006:             if (ref($cachedblockers{$block}) eq 'HASH') {
                   8007:                 if (ref($cachedblockers{$block}{'resources'}) eq 'HASH') {
                   8008:                     if ($cachedblockers{$block}{'resources'}{$symb}) {
                   8009:                         unless (grep(/^\Q$block\E$/,@blockers)) {
                   8010:                             push(@blockers,$block);
                   8011:                         }
                   8012:                     }
                   8013:                 }
1.1162    raeburn  8014:             }
1.1282    raeburn  8015:             if (ref($cachedblockers{$block}{'maps'}) eq 'HASH') {
                   8016:                 if ($cachedblockers{$block}{'maps'}{$map}) {
                   8017:                     unless (grep(/^\Q$block\E$/,@blockers)) {
                   8018:                         push(@blockers,$block);
                   8019:                     }
                   8020:                 }
1.1162    raeburn  8021:             }
                   8022:         }
                   8023:     }
1.1282    raeburn  8024:     return if ($noblock);
                   8025:     return @blockers;
                   8026: }
1.1162    raeburn  8027: }
                   8028: 
1.1282    raeburn  8029: # -------------------------------- Deversion and split uri into path an filename   
                   8030: 
1.1133    foxr     8031: #
1.1282    raeburn  8032: #   Removes the version from a URI and
1.1133    foxr     8033: #   splits it in to its filename and path to the filename.
                   8034: #   Seems like File::Basename could have done this more clearly.
                   8035: #   Parameters:
                   8036: #      $uri   - input URI
                   8037: #   Returns:
                   8038: #     Two element list consisting of 
                   8039: #     $pathname  - the URI up to and excluding the trailing /
                   8040: #     $filename  - The part of the URI following the last /
                   8041: #  NOTE:
                   8042: #    Another realization of this is simply:
                   8043: #    use File::Basename;
                   8044: #    ...
                   8045: #    $uri = shift;
                   8046: #    $filename = basename($uri);
                   8047: #    $path     = dirname($uri);
                   8048: #    return ($filename, $path);
                   8049: #
                   8050: #     The implementation below is probably faster however.
                   8051: #
1.710     albertel 8052: sub split_uri_for_cond {
                   8053:     my $uri=&deversion(&declutter(shift));
                   8054:     my @uriparts=split(/\//,$uri);
                   8055:     my $filename=pop(@uriparts);
                   8056:     my $pathname=join('/',@uriparts);
                   8057:     return ($pathname,$filename);
                   8058: }
1.232     www      8059: # --------------------------------------------------- Is a resource on the map?
                   8060: 
                   8061: sub is_on_map {
1.710     albertel 8062:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 8063:     #Trying to find the conditional for the file
1.620     albertel 8064:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 8065: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      8066:     if ($match) {
1.289     bowersj2 8067: 	return (1,$1);
                   8068:     } else {
1.434     www      8069: 	return (0,0);
1.289     bowersj2 8070:     }
1.12      www      8071: }
                   8072: 
1.427     www      8073: # --------------------------------------------------------- Get symb from alias
                   8074: 
                   8075: sub get_symb_from_alias {
                   8076:     my $symb=shift;
                   8077:     my ($map,$resid,$url)=&decode_symb($symb);
                   8078: # Already is a symb
                   8079:     if ($url) { return $symb; }
                   8080: # Must be an alias
                   8081:     my $aliassymb='';
                   8082:     my %bighash;
1.620     albertel 8083:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      8084:                             &GDBM_READER(),0640)) {
                   8085:         my $rid=$bighash{'mapalias_'.$symb};
                   8086: 	if ($rid) {
                   8087: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 8088: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   8089: 				    $resid,$bighash{'src_'.$rid});
1.427     www      8090: 	}
                   8091:         untie %bighash;
                   8092:     }
                   8093:     return $aliassymb;
                   8094: }
                   8095: 
1.12      www      8096: # ----------------------------------------------------------------- Define Role
                   8097: 
                   8098: sub definerole {
                   8099:   if (allowed('mcr','/')) {
1.1326    raeburn  8100:     my ($rolename,$sysrole,$domrole,$courole,$uname,$udom)=@_;
1.800     albertel 8101:     foreach my $role (split(':',$sysrole)) {
                   8102: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8103:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   8104:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   8105: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      8106:                return "refused:s:$crole&$cqual"; 
                   8107:             }
                   8108:         }
1.191     harris41 8109:     }
1.800     albertel 8110:     foreach my $role (split(':',$domrole)) {
                   8111: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8112:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   8113:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   8114: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      8115:                return "refused:d:$crole&$cqual"; 
                   8116:             }
                   8117:         }
1.191     harris41 8118:     }
1.800     albertel 8119:     foreach my $role (split(':',$courole)) {
                   8120: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 8121:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   8122:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   8123: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      8124:                return "refused:c:$crole&$cqual"; 
                   8125:             }
                   8126:         }
1.191     harris41 8127:     }
1.1326    raeburn  8128:     my $uhome;
                   8129:     if (($uname ne '') && ($udom ne '')) {
                   8130:         $uhome = &homeserver($uname,$udom);
                   8131:         return $uhome if ($uhome eq 'no_host');
                   8132:     } else {
                   8133:         $uname = $env{'user.name'};
                   8134:         $udom = $env{'user.domain'};
                   8135:         $uhome = $env{'user.home'};
                   8136:     }
1.620     albertel 8137:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.1326    raeburn  8138:                 "$udom:$uname:rolesdef_$rolename=".
1.21      www      8139:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.1326    raeburn  8140:     return reply($command,$uhome);
1.12      www      8141:   } else {
                   8142:     return 'refused';
                   8143:   }
1.105     harris41 8144: }
                   8145: 
                   8146: # ---------------- Make a metadata query against the network of library servers
                   8147: 
                   8148: sub metadata_query {
1.1237    raeburn  8149:     my ($query,$custom,$customshow,$server_array,$domains_hash)=@_;
1.120     harris41 8150:     my %rhash;
1.845     albertel 8151:     my %libserv = &all_library();
1.244     matthew  8152:     my @server_list = (defined($server_array) ? @$server_array
                   8153:                                               : keys(%libserv) );
                   8154:     for my $server (@server_list) {
1.1237    raeburn  8155:         my $domains = ''; 
                   8156:         if (ref($domains_hash) eq 'HASH') {
                   8157:             $domains = $domains_hash->{$server}; 
                   8158:         }
1.118     harris41 8159: 	unless ($custom or $customshow) {
1.1237    raeburn  8160: 	    my $reply=&reply("querysend:".&escape($query).':::'.&escape($domains),$server);
1.118     harris41 8161: 	    $rhash{$server}=$reply;
                   8162: 	}
                   8163: 	else {
                   8164: 	    my $reply=&reply("querysend:".&escape($query).':'.
1.1237    raeburn  8165: 			     &escape($custom).':'.&escape($customshow).':'.&escape($domains),
1.118     harris41 8166: 			     $server);
                   8167: 	    $rhash{$server}=$reply;
                   8168: 	}
1.112     harris41 8169:     }
1.118     harris41 8170:     return \%rhash;
1.240     www      8171: }
                   8172: 
                   8173: # ----------------------------------------- Send log queries and wait for reply
                   8174: 
                   8175: sub log_query {
                   8176:     my ($uname,$udom,$query,%filters)=@_;
                   8177:     my $uhome=&homeserver($uname,$udom);
                   8178:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 8179:     my $uhost=&hostname($uhome);
1.800     albertel 8180:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      8181:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   8182:                        $uhome);
1.479     albertel 8183:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      8184:     return get_query_reply($queryid);
                   8185: }
                   8186: 
1.818     raeburn  8187: # -------------------------- Update MySQL table for portfolio file
                   8188: 
                   8189: sub update_portfolio_table {
1.821     raeburn  8190:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  8191:     if ($group ne '') {
                   8192:         $file_name =~s /^\Q$group\E//;
                   8193:     }
1.818     raeburn  8194:     my $homeserver = &homeserver($uname,$udom);
                   8195:     my $queryid=
1.821     raeburn  8196:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   8197:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  8198:     my $reply = &get_query_reply($queryid);
                   8199:     return $reply;
                   8200: }
                   8201: 
1.899     raeburn  8202: # -------------------------- Update MySQL allusers table
                   8203: 
                   8204: sub update_allusers_table {
                   8205:     my ($uname,$udom,$names) = @_;
                   8206:     my $homeserver = &homeserver($uname,$udom);
                   8207:     my $queryid=
                   8208:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   8209:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   8210:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   8211:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   8212:                'generation='.&escape($names->{'generation'}).'%%'.
                   8213:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   8214:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  8215:     return;
1.899     raeburn  8216: }
                   8217: 
1.508     raeburn  8218: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  8219: 
                   8220: sub fetch_enrollment_query {
1.511     raeburn  8221:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.1317    raeburn  8222:     my ($homeserver,$sleep,$loopmax);
1.547     raeburn  8223:     my $maxtries = 1;
1.508     raeburn  8224:     if ($context eq 'automated') {
                   8225:         $homeserver = $perlvar{'lonHostID'};
1.1317    raeburn  8226:         $sleep = 2;
                   8227:         $loopmax = 100;
1.547     raeburn  8228:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  8229:     } else {
                   8230:         $homeserver = &homeserver($cnum,$dom);
                   8231:     }
1.838     albertel 8232:     my $host=&hostname($homeserver);
1.506     raeburn  8233:     my $cmd = '';
1.1000    raeburn  8234:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 8235:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  8236:     }
                   8237:     $cmd =~ s/%%$//;
                   8238:     $cmd = &escape($cmd);
                   8239:     my $query = 'fetchenrollment';
1.620     albertel 8240:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  8241:     unless ($queryid=~/^\Q$host\E\_/) { 
                   8242:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   8243:         return 'error: '.$queryid;
                   8244:     }
1.1317    raeburn  8245:     my $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  8246:     my $tries = 1;
                   8247:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
1.1317    raeburn  8248:         $reply = &get_query_reply($queryid,$sleep,$loopmax);
1.547     raeburn  8249:         $tries ++;
                   8250:     }
1.526     raeburn  8251:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 8252:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  8253:     } else {
1.901     albertel 8254:         my @responses = split(/:/,$reply);
1.1322    raeburn  8255:         if (grep { $_ eq $homeserver } &current_machine_ids()) {
1.800     albertel 8256:             foreach my $line (@responses) {
                   8257:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  8258:                 $$replyref{$key} = $value;
                   8259:             }
                   8260:         } else {
1.1117    foxr     8261:             my $pathname = LONCAPA::tempdir();
1.800     albertel 8262:             foreach my $line (@responses) {
                   8263:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  8264:                 $$replyref{$key} = $value;
                   8265:                 if ($value > 0) {
1.800     albertel 8266:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   8267:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  8268:                         my $destname = $pathname.'/'.$filename;
                   8269:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  8270:                         if ($xml_classlist =~ /^error/) {
                   8271:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   8272:                         } else {
1.506     raeburn  8273:                             if ( open(FILE,">$destname") ) {
                   8274:                                 print FILE &unescape($xml_classlist);
                   8275:                                 close(FILE);
1.526     raeburn  8276:                             } else {
                   8277:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  8278:                             }
                   8279:                         }
                   8280:                     }
                   8281:                 }
                   8282:             }
                   8283:         }
                   8284:         return 'ok';
                   8285:     }
                   8286:     return 'error';
                   8287: }
                   8288: 
1.242     www      8289: sub get_query_reply {
1.1317    raeburn  8290:     my ($queryid,$sleep,$loopmax) = @_;;
                   8291:     if (($sleep eq '') || ($sleep !~ /^\d+\.?\d*$/)) {
                   8292:         $sleep = 0.2;
                   8293:     }
                   8294:     if (($loopmax eq '') || ($loopmax =~ /\D/)) {
                   8295:         $loopmax = 100;
                   8296:     }
1.1117    foxr     8297:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      8298:     my $reply='';
1.1317    raeburn  8299:     for (1..$loopmax) {
                   8300: 	sleep($sleep);
1.240     www      8301:         if (-e $replyfile.'.end') {
1.448     albertel 8302: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 8303: 		$reply = join('',<$fh>);
                   8304: 		close($fh);
1.240     www      8305: 	   } else { return 'error: reply_file_error'; }
1.242     www      8306:            return &unescape($reply);
                   8307: 	}
1.240     www      8308:     }
1.242     www      8309:     return 'timeout:'.$queryid;
1.240     www      8310: }
                   8311: 
                   8312: sub courselog_query {
1.241     www      8313: #
                   8314: # possible filters:
                   8315: # url: url or symb
                   8316: # username
                   8317: # domain
                   8318: # action: view, submit, grade
                   8319: # start: timestamp
                   8320: # end: timestamp
                   8321: #
1.240     www      8322:     my (%filters)=@_;
1.620     albertel 8323:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      8324:     if ($filters{'url'}) {
                   8325: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   8326:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   8327:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   8328:     }
1.620     albertel 8329:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   8330:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      8331:     return &log_query($cname,$cdom,'courselog',%filters);
                   8332: }
                   8333: 
                   8334: sub userlog_query {
1.858     raeburn  8335: #
                   8336: # possible filters:
                   8337: # action: log check role
                   8338: # start: timestamp
                   8339: # end: timestamp
                   8340: #
1.240     www      8341:     my ($uname,$udom,%filters)=@_;
                   8342:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      8343: }
                   8344: 
1.506     raeburn  8345: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   8346: 
                   8347: sub auto_run {
1.508     raeburn  8348:     my ($cnum,$cdom) = @_;
1.876     raeburn  8349:     my $response = 0;
                   8350:     my $settings;
                   8351:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   8352:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   8353:         $settings = $domconfig{'autoenroll'};
                   8354:         if ($settings->{'run'} eq '1') {
                   8355:             $response = 1;
                   8356:         }
                   8357:     } else {
1.934     raeburn  8358:         my $homeserver;
                   8359:         if (&is_course($cdom,$cnum)) {
                   8360:             $homeserver = &homeserver($cnum,$cdom);
                   8361:         } else {
                   8362:             $homeserver = &domain($cdom,'primary');
                   8363:         }
                   8364:         if ($homeserver ne 'no_host') {
                   8365:             $response = &reply('autorun:'.$cdom,$homeserver);
                   8366:         }
1.876     raeburn  8367:     }
1.506     raeburn  8368:     return $response;
                   8369: }
1.776     albertel 8370: 
1.506     raeburn  8371: sub auto_get_sections {
1.508     raeburn  8372:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  8373:     my $homeserver;
                   8374:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   8375:         $homeserver = &homeserver($cnum,$cdom);
                   8376:     }
                   8377:     if (!defined($homeserver)) { 
                   8378:         if ($cdom =~ /^$match_domain$/) {
                   8379:             $homeserver = &domain($cdom,'primary');
                   8380:         }
                   8381:     }
                   8382:     my @secs;
                   8383:     if (defined($homeserver)) {
                   8384:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   8385:         unless ($response eq 'refused') {
                   8386:             @secs = split(/:/,$response);
                   8387:         }
1.506     raeburn  8388:     }
                   8389:     return @secs;
                   8390: }
1.776     albertel 8391: 
1.506     raeburn  8392: sub auto_new_course {
1.1099    raeburn  8393:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  8394:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  8395:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  8396:     return $response;
                   8397: }
1.776     albertel 8398: 
1.506     raeburn  8399: sub auto_validate_courseID {
1.508     raeburn  8400:     my ($cnum,$cdom,$inst_course_id) = @_;
                   8401:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  8402:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  8403:     return $response;
                   8404: }
1.776     albertel 8405: 
1.1007    raeburn  8406: sub auto_validate_instcode {
1.1020    raeburn  8407:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  8408:     my ($homeserver,$response);
                   8409:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   8410:         $homeserver = &homeserver($cnum,$cdom);
                   8411:     }
                   8412:     if (!defined($homeserver)) {
                   8413:         if ($cdom =~ /^$match_domain$/) {
                   8414:             $homeserver = &domain($cdom,'primary');
                   8415:         }
                   8416:     }
1.1065    raeburn  8417:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   8418:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  8419:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   8420:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  8421: }
                   8422: 
1.506     raeburn  8423: sub auto_create_password {
1.873     raeburn  8424:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   8425:     my ($homeserver,$response);
1.506     raeburn  8426:     my $create_passwd = 0;
                   8427:     my $authchk = '';
1.873     raeburn  8428:     if ($udom =~ /^$match_domain$/) {
                   8429:         $homeserver = &domain($udom,'primary');
                   8430:     }
                   8431:     if ($homeserver eq '') {
                   8432:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   8433:             $homeserver = &homeserver($cnum,$cdom);
                   8434:         }
                   8435:     }
                   8436:     if ($homeserver eq '') {
                   8437:         $authchk = 'nodomain';
1.506     raeburn  8438:     } else {
1.873     raeburn  8439:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   8440:         if ($response eq 'refused') {
                   8441:             $authchk = 'refused';
                   8442:         } else {
1.901     albertel 8443:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  8444:         }
1.506     raeburn  8445:     }
                   8446:     return ($authparam,$create_passwd,$authchk);
                   8447: }
                   8448: 
1.706     raeburn  8449: sub auto_photo_permission {
                   8450:     my ($cnum,$cdom,$students) = @_;
                   8451:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 8452:     my ($outcome,$perm_reqd,$conditions) = 
                   8453: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 8454:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   8455: 	return (undef,undef);
                   8456:     }
1.706     raeburn  8457:     return ($outcome,$perm_reqd,$conditions);
                   8458: }
                   8459: 
                   8460: sub auto_checkphotos {
                   8461:     my ($uname,$udom,$pid) = @_;
                   8462:     my $homeserver = &homeserver($uname,$udom);
                   8463:     my ($result,$resulttype);
                   8464:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 8465: 				   &escape($uname).':'.&escape($pid),
                   8466: 				   $homeserver));
1.709     albertel 8467:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   8468: 	return (undef,undef);
                   8469:     }
1.706     raeburn  8470:     if ($outcome) {
                   8471:         ($result,$resulttype) = split(/:/,$outcome);
                   8472:     } 
                   8473:     return ($result,$resulttype);
                   8474: }
                   8475: 
                   8476: sub auto_photochoice {
                   8477:     my ($cnum,$cdom) = @_;
                   8478:     my $homeserver = &homeserver($cnum,$cdom);
                   8479:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 8480: 						       &escape($cdom),
                   8481: 						       $homeserver)));
1.709     albertel 8482:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   8483: 	return (undef,undef);
                   8484:     }
1.706     raeburn  8485:     return ($update,$comment);
                   8486: }
                   8487: 
                   8488: sub auto_photoupdate {
                   8489:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   8490:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 8491:     my $host=&hostname($homeserver);
1.706     raeburn  8492:     my $cmd = '';
                   8493:     my $maxtries = 1;
1.800     albertel 8494:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   8495:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  8496:     }
                   8497:     $cmd =~ s/%%$//;
                   8498:     $cmd = &escape($cmd);
                   8499:     my $query = 'institutionalphotos';
                   8500:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   8501:     unless ($queryid=~/^\Q$host\E\_/) {
                   8502:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   8503:         return 'error: '.$queryid;
                   8504:     }
                   8505:     my $reply = &get_query_reply($queryid);
                   8506:     my $tries = 1;
                   8507:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   8508:         $reply = &get_query_reply($queryid);
                   8509:         $tries ++;
                   8510:     }
                   8511:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   8512:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   8513:     } else {
                   8514:         my @responses = split(/:/,$reply);
                   8515:         my $outcome = shift(@responses); 
                   8516:         foreach my $item (@responses) {
                   8517:             my ($key,$value) = split(/=/,$item);
                   8518:             $$photo{$key} = $value;
                   8519:         }
                   8520:         return $outcome;
                   8521:     }
                   8522:     return 'error';
                   8523: }
                   8524: 
1.521     raeburn  8525: sub auto_instcode_format {
1.793     albertel 8526:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   8527: 	$cat_order) = @_;
1.521     raeburn  8528:     my $courses = '';
1.772     raeburn  8529:     my @homeservers;
1.521     raeburn  8530:     if ($caller eq 'global') {
1.841     albertel 8531: 	my %servers = &get_servers($codedom,'library');
                   8532: 	foreach my $tryserver (keys(%servers)) {
                   8533: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   8534: 		push(@homeservers,$tryserver);
                   8535: 	    }
1.584     raeburn  8536:         }
1.1022    raeburn  8537:     } elsif ($caller eq 'requests') {
                   8538:         if ($codedom =~ /^$match_domain$/) {
                   8539:             my $chome = &domain($codedom,'primary');
                   8540:             unless ($chome eq 'no_host') {
                   8541:                 push(@homeservers,$chome);
                   8542:             }
                   8543:         }
1.521     raeburn  8544:     } else {
1.772     raeburn  8545:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  8546:     }
1.793     albertel 8547:     foreach my $code (keys(%{$instcodes})) {
                   8548:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  8549:     }
                   8550:     chop($courses);
1.772     raeburn  8551:     my $ok_response = 0;
                   8552:     my $response;
                   8553:     while (@homeservers > 0 && $ok_response == 0) {
                   8554:         my $server = shift(@homeservers); 
                   8555:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   8556:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   8557:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 8558: 		split(/:/,$response);
1.772     raeburn  8559:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   8560:             push(@{$codetitles},&str2array($codetitles_str));
                   8561:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   8562:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   8563:             $ok_response = 1;
                   8564:         }
                   8565:     }
                   8566:     if ($ok_response) {
1.521     raeburn  8567:         return 'ok';
1.772     raeburn  8568:     } else {
                   8569:         return $response;
1.521     raeburn  8570:     }
                   8571: }
                   8572: 
1.792     raeburn  8573: sub auto_instcode_defaults {
                   8574:     my ($domain,$returnhash,$code_order) = @_;
                   8575:     my @homeservers;
1.841     albertel 8576: 
                   8577:     my %servers = &get_servers($domain,'library');
                   8578:     foreach my $tryserver (keys(%servers)) {
                   8579: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   8580: 	    push(@homeservers,$tryserver);
                   8581: 	}
1.792     raeburn  8582:     }
1.841     albertel 8583: 
1.792     raeburn  8584:     my $response;
1.841     albertel 8585:     foreach my $server (@homeservers) {
1.792     raeburn  8586:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 8587:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   8588: 	
                   8589: 	foreach my $pair (split(/\&/,$response)) {
                   8590: 	    my ($name,$value)=split(/\=/,$pair);
                   8591: 	    if ($name eq 'code_order') {
                   8592: 		@{$code_order} = split(/\&/,&unescape($value));
                   8593: 	    } else {
                   8594: 		$returnhash->{&unescape($name)}=&unescape($value);
                   8595: 	    }
                   8596: 	}
                   8597: 	return 'ok';
1.792     raeburn  8598:     }
1.841     albertel 8599: 
                   8600:     return $response;
1.1003    raeburn  8601: }
                   8602: 
                   8603: sub auto_possible_instcodes {
1.1007    raeburn  8604:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   8605:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   8606:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   8607:         return;
                   8608:     }
1.1003    raeburn  8609:     my (@homeservers,$uhome);
                   8610:     if (defined(&domain($domain,'primary'))) {
                   8611:         $uhome=&domain($domain,'primary');
                   8612:         push(@homeservers,&domain($domain,'primary'));
                   8613:     } else {
                   8614:         my %servers = &get_servers($domain,'library');
                   8615:         foreach my $tryserver (keys(%servers)) {
                   8616:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   8617:                 push(@homeservers,$tryserver);
                   8618:             }
                   8619:         }
                   8620:     }
                   8621:     my $response;
                   8622:     foreach my $server (@homeservers) {
                   8623:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   8624:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  8625:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   8626:             split(':',$response);
                   8627:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   8628:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  8629:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  8630:             my ($name,$value)=split('=',$item);
                   8631:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  8632:         }
                   8633:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  8634:             my ($name,$value)=split('=',$item);
                   8635:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  8636:         }
                   8637:         return 'ok';
                   8638:     }
                   8639:     return $response;
                   8640: }
1.792     raeburn  8641: 
1.1010    raeburn  8642: sub auto_courserequest_checks {
                   8643:     my ($dom) = @_;
1.1020    raeburn  8644:     my ($homeserver,%validations);
                   8645:     if ($dom =~ /^$match_domain$/) {
                   8646:         $homeserver = &domain($dom,'primary');
                   8647:     }
                   8648:     unless ($homeserver eq 'no_host') {
                   8649:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   8650:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   8651:             my @items = split(/&/,$response);
                   8652:             foreach my $item (@items) {
                   8653:                 my ($key,$value) = split('=',$item);
                   8654:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   8655:             }
                   8656:         }
                   8657:     }
1.1010    raeburn  8658:     return %validations; 
                   8659: }
                   8660: 
1.1020    raeburn  8661: sub auto_courserequest_validation {
1.1255    raeburn  8662:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist,$custominfo) = @_;
1.1020    raeburn  8663:     my ($homeserver,$response);
                   8664:     if ($dom =~ /^$match_domain$/) {
                   8665:         $homeserver = &domain($dom,'primary');
                   8666:     }
1.1255    raeburn  8667:     unless ($homeserver eq 'no_host') {
                   8668:         my $customdata;
                   8669:         if (ref($custominfo) eq 'HASH') {
                   8670:             $customdata = &freeze_escape($custominfo);
                   8671:         }
1.1020    raeburn  8672:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  8673:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1255    raeburn  8674:                                     ':'.&escape($instcode).':'.&escape($instseclist).':'.
                   8675:                                     $customdata,$homeserver));
1.1020    raeburn  8676:     }
                   8677:     return $response;
                   8678: }
                   8679: 
1.777     albertel 8680: sub auto_validate_class_sec {
1.918     raeburn  8681:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  8682:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  8683:     my $ownerlist;
                   8684:     if (ref($owners) eq 'ARRAY') {
                   8685:         $ownerlist = join(',',@{$owners});
                   8686:     } else {
                   8687:         $ownerlist = $owners;
                   8688:     }
1.773     raeburn  8689:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  8690:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  8691:     return $response;
                   8692: }
                   8693: 
1.1248    raeburn  8694: sub auto_crsreq_update {
                   8695:     my ($cdom,$cnum,$crstype,$action,$ownername,$ownerdomain,$fullname,$title,
1.1257    raeburn  8696:         $code,$accessstart,$accessend,$inbound) = @_;
1.1248    raeburn  8697:     my ($homeserver,%crsreqresponse);
                   8698:     if ($cdom =~ /^$match_domain$/) {
                   8699:         $homeserver = &domain($cdom,'primary');
                   8700:     }
                   8701:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   8702:         my $info;
                   8703:         if (ref($inbound) eq 'HASH') {
                   8704:             $info = &freeze_escape($inbound);
                   8705:         }
                   8706:         my $response=&reply('autocrsrequpdate:'.$cdom.':'.$cnum.':'.&escape($crstype).
                   8707:                             ':'.&escape($action).':'.&escape($ownername).':'.
                   8708:                             &escape($ownerdomain).':'.&escape($fullname).':'.
1.1257    raeburn  8709:                             &escape($title).':'.&escape($code).':'.
                   8710:                             &escape($accessstart).':'.&escape($accessend).':'.$info,
                   8711:                             $homeserver);
1.1248    raeburn  8712:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   8713:             my @items = split(/&/,$response);
                   8714:             foreach my $item (@items) {
                   8715:                 my ($key,$value) = split('=',$item);
                   8716:                 $crsreqresponse{&unescape($key)} = &thaw_unescape($value);
                   8717:             }
                   8718:         }
                   8719:     }
                   8720:     return \%crsreqresponse;
                   8721: }
                   8722: 
1.1305    raeburn  8723: sub auto_export_grades {
1.1309    raeburn  8724:     my ($cdom,$cnum,$inforef,$gradesref) = @_;
                   8725:     my ($homeserver,%exportresponse);
                   8726:     if ($cdom =~ /^$match_domain$/) {
                   8727:         $homeserver = &domain($cdom,'primary');
                   8728:     }
                   8729:     unless (($homeserver eq 'no_host') || ($homeserver eq '')) {
                   8730:         my $info;
                   8731:         if (ref($inforef) eq 'HASH') {
                   8732:             $info = &freeze_escape($inforef);
                   8733:         }
                   8734:         if (ref($gradesref) eq 'HASH') {
                   8735:             my $grades = &freeze_escape($gradesref);
                   8736:             my $response=&reply('encrypt:autoexportgrades:'.$cdom.':'.$cnum.':'.
                   8737:                                 $info.':'.$grades,$homeserver);
                   8738:             unless ($response =~ /(con_lost|error|no_such_host|refused|unknown_command)/) {
                   8739:                 my @items = split(/&/,$response);
                   8740:                 foreach my $item (@items) {
                   8741:                     my ($key,$value) = split('=',$item);
                   8742:                     $exportresponse{&unescape($key)} = &thaw_unescape($value);
                   8743:                 }
                   8744:             }
                   8745:         }
                   8746:     }
                   8747:     return \%exportresponse;
1.1305    raeburn  8748: }
                   8749: 
1.1287    raeburn  8750: sub check_instcode_cloning {
                   8751:     my ($codedefaults,$code_order,$cloner,$clonefromcode,$clonetocode) = @_;
                   8752:     unless ((ref($codedefaults) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   8753:         return;
                   8754:     }
                   8755:     my $canclone;
                   8756:     if (@{$code_order} > 0) {
                   8757:         my $instcoderegexp ='^';
                   8758:         my @clonecodes = split(/\&/,$cloner);
                   8759:         foreach my $item (@{$code_order}) {
                   8760:             if (grep(/^\Q$item\E=/,@clonecodes)) {
                   8761:                 foreach my $pair (@clonecodes) {
                   8762:                     my ($key,$val) = split(/\=/,$pair,2);
                   8763:                     $val = &unescape($val);
                   8764:                     if ($key eq $item) {
                   8765:                         $instcoderegexp .= '('.$val.')';
                   8766:                         last;
                   8767:                     }
                   8768:                 }
                   8769:             } else {
                   8770:                 $instcoderegexp .= $codedefaults->{$item};
                   8771:             }
                   8772:         }
                   8773:         $instcoderegexp .= '$';
                   8774:         my (@from,@to);
                   8775:         eval {
                   8776:                (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   8777:                (@to) = ($clonetocode =~ /$instcoderegexp/);
                   8778:         };
                   8779:         if ((@from > 0) && (@to > 0)) {
                   8780:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   8781:             if (!@diffs) {
                   8782:                 $canclone = 1;
                   8783:             }
                   8784:         }
                   8785:     }
                   8786:     return $canclone;
                   8787: }
                   8788: 
                   8789: sub default_instcode_cloning {
                   8790:     my ($clonedom,$domdefclone,$clonefromcode,$clonetocode,$codedefaultsref,$codeorderref) = @_;
                   8791:     my (%codedefaults,@code_order,$canclone);
                   8792:     if ((ref($codedefaultsref) eq 'HASH') && (ref($codeorderref) eq 'ARRAY')) {
                   8793:         %codedefaults = %{$codedefaultsref};
                   8794:         @code_order = @{$codeorderref};
                   8795:     } elsif ($clonedom) {
                   8796:         &auto_instcode_defaults($clonedom,\%codedefaults,\@code_order);
                   8797:     }
                   8798:     if (($domdefclone) && (@code_order)) {
                   8799:         my @clonecodes = split(/\+/,$domdefclone);
                   8800:         my $instcoderegexp ='^';
                   8801:         foreach my $item (@code_order) {
                   8802:             if (grep(/^\Q$item\E$/,@clonecodes)) {
                   8803:                 $instcoderegexp .= '('.$codedefaults{$item}.')';
                   8804:             } else {
                   8805:                 $instcoderegexp .= $codedefaults{$item};
                   8806:             }
                   8807:         }
                   8808:         $instcoderegexp .= '$';
                   8809:         my (@from,@to);
                   8810:         eval {
                   8811:             (@from) = ($clonefromcode =~ /$instcoderegexp/);
                   8812:             (@to) = ($clonetocode =~ /$instcoderegexp/);
                   8813:         };
                   8814:         if ((@from > 0) && (@to > 0)) {
                   8815:             my @diffs = &Apache::loncommon::compare_arrays(\@from,\@to);
                   8816:             if (!@diffs) {
                   8817:                 $canclone = 1;
                   8818:             }
                   8819:         }
                   8820:     }
                   8821:     return $canclone;
                   8822: }
                   8823: 
1.679     raeburn  8824: # ------------------------------------------------------- Course Group routines
                   8825: 
                   8826: sub get_coursegroups {
1.809     raeburn  8827:     my ($cdom,$cnum,$group,$namespace) = @_;
                   8828:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  8829: }
                   8830: 
1.679     raeburn  8831: sub modify_coursegroup {
                   8832:     my ($cdom,$cnum,$groupsettings) = @_;
                   8833:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   8834: }
                   8835: 
1.809     raeburn  8836: sub toggle_coursegroup_status {
                   8837:     my ($cdom,$cnum,$group,$action) = @_;
                   8838:     my ($from_namespace,$to_namespace);
                   8839:     if ($action eq 'delete') {
                   8840:         $from_namespace = 'coursegroups';
                   8841:         $to_namespace = 'deleted_groups';
                   8842:     } else {
                   8843:         $from_namespace = 'deleted_groups';
                   8844:         $to_namespace = 'coursegroups';
                   8845:     }
                   8846:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  8847:     if (my $tmp = &error(%curr_group)) {
                   8848:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   8849:         return ('read error',$tmp);
                   8850:     } else {
                   8851:         my %savedsettings = %curr_group; 
1.809     raeburn  8852:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  8853:         my $deloutcome;
                   8854:         if ($result eq 'ok') {
1.809     raeburn  8855:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  8856:         } else {
                   8857:             return ('write error',$result);
                   8858:         }
                   8859:         if ($deloutcome eq 'ok') {
                   8860:             return 'ok';
                   8861:         } else {
                   8862:             return ('delete error',$deloutcome);
                   8863:         }
                   8864:     }
                   8865: }
                   8866: 
1.679     raeburn  8867: sub modify_group_roles {
1.957     raeburn  8868:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  8869:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   8870:     my $role = 'gr/'.&escape($userprivs);
                   8871:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  8872:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  8873:     if ($result eq 'ok') {
                   8874:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   8875:     }
1.679     raeburn  8876:     return $result;
                   8877: }
                   8878: 
                   8879: sub modify_coursegroup_membership {
                   8880:     my ($cdom,$cnum,$membership) = @_;
                   8881:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   8882:     return $result;
                   8883: }
                   8884: 
1.682     raeburn  8885: sub get_active_groups {
                   8886:     my ($udom,$uname,$cdom,$cnum) = @_;
                   8887:     my $now = time;
                   8888:     my %groups = ();
                   8889:     foreach my $key (keys(%env)) {
1.811     albertel 8890:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  8891:             my ($start,$end) = split(/\./,$env{$key});
                   8892:             if (($end!=0) && ($end<$now)) { next; }
                   8893:             if (($start!=0) && ($start>$now)) { next; }
                   8894:             if ($1 eq $cdom && $2 eq $cnum) {
                   8895:                 $groups{$3} = $env{$key} ;
                   8896:             }
                   8897:         }
                   8898:     }
                   8899:     return %groups;
                   8900: }
                   8901: 
1.683     raeburn  8902: sub get_group_membership {
                   8903:     my ($cdom,$cnum,$group) = @_;
                   8904:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   8905: }
                   8906: 
                   8907: sub get_users_groups {
                   8908:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  8909:     my @usersgroups;
1.683     raeburn  8910:     my $cachetime=1800;
                   8911: 
                   8912:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  8913:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   8914:     if (defined($cached)) {
1.734     albertel 8915:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  8916:     } else {  
                   8917:         $grouplist = '';
1.816     raeburn  8918:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  8919:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  8920:         my $access_end = $env{'course.'.$courseid.
                   8921:                               '.default_enrollment_end_date'};
                   8922:         my $now = time;
                   8923:         foreach my $key (keys(%roleshash)) {
                   8924:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   8925:                 my $group = $1;
                   8926:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   8927:                     my $start = $2;
                   8928:                     my $end = $1;
                   8929:                     if ($start == -1) { next; } # deleted from group
                   8930:                     if (($start!=0) && ($start>$now)) { next; }
                   8931:                     if (($end!=0) && ($end<$now)) {
                   8932:                         if ($access_end && $access_end < $now) {
                   8933:                             if ($access_end - $end < 86400) {
                   8934:                                 push(@usersgroups,$group);
1.733     raeburn  8935:                             }
                   8936:                         }
1.817     raeburn  8937:                         next;
1.733     raeburn  8938:                     }
1.817     raeburn  8939:                     push(@usersgroups,$group);
1.683     raeburn  8940:                 }
                   8941:             }
                   8942:         }
1.817     raeburn  8943:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   8944:         $grouplist = join(':',@usersgroups);
                   8945:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  8946:     }
1.733     raeburn  8947:     return @usersgroups;
1.683     raeburn  8948: }
                   8949: 
                   8950: sub devalidate_getgroups_cache {
                   8951:     my ($udom,$uname,$cdom,$cnum)=@_;
                   8952:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 8953: 
1.683     raeburn  8954:     my $hashid="$udom:$uname:$courseid";
                   8955:     &devalidate_cache_new('getgroups',$hashid);
                   8956: }
                   8957: 
1.12      www      8958: # ------------------------------------------------------------------ Plain Text
                   8959: 
                   8960: sub plaintext {
1.988     raeburn  8961:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  8962:     if ($short =~ m{^cr/}) {
1.758     albertel 8963: 	return (split('/',$short))[-1];
                   8964:     }
1.742     raeburn  8965:     if (!defined($cid)) {
                   8966:         $cid = $env{'request.course.id'};
                   8967:     }
                   8968:     my %rolenames = (
1.1008    raeburn  8969:                       Course    => 'std',
                   8970:                       Community => 'alt1',
1.1305    raeburn  8971:                       Placement => 'std',
1.742     raeburn  8972:                     );
1.1037    raeburn  8973:     if ($cid ne '') {
                   8974:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   8975:             unless ($forcedefault) {
                   8976:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   8977:                 &Apache::lonlocal::mt_escape(\$roletext);
                   8978:                 return &Apache::lonlocal::mt($roletext);
                   8979:             }
                   8980:         }
                   8981:     }
                   8982:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   8983:         (defined($rolenames{$type})) && 
                   8984:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  8985:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  8986:     } elsif ($cid ne '') {
                   8987:         my $crstype = $env{'course.'.$cid.'.type'};
                   8988:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   8989:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   8990:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   8991:         }
1.742     raeburn  8992:     }
1.1037    raeburn  8993:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      8994: }
                   8995: 
                   8996: # ----------------------------------------------------------------- Assign Role
                   8997: 
                   8998: sub assignrole {
1.957     raeburn  8999:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   9000:         $context)=@_;
1.21      www      9001:     my $mrole;
                   9002:     if ($role =~ /^cr\//) {
1.393     www      9003:         my $cwosec=$url;
1.811     albertel 9004:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      9005: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  9006:            my $refused = 1;
                   9007:            if ($context eq 'requestcourses') {
                   9008:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   9009:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   9010:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   9011:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   9012:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9013:                            if ($crsenv{'internal.courseowner'} eq
                   9014:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   9015:                                $refused = '';
                   9016:                            }
                   9017:                        }
                   9018:                    }
                   9019:                }
                   9020:            }
                   9021:            if ($refused) {
                   9022:                &logthis('Refused custom assignrole: '.
                   9023:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   9024:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   9025:                return 'refused';
                   9026:            }
1.104     www      9027:         }
1.21      www      9028:         $mrole='cr';
1.678     raeburn  9029:     } elsif ($role =~ /^gr\//) {
                   9030:         my $cwogrp=$url;
1.811     albertel 9031:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  9032:         unless (&allowed('mdg',$cwogrp)) {
                   9033:             &logthis('Refused group assignrole: '.
                   9034:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   9035:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   9036:             return 'refused';
                   9037:         }
                   9038:         $mrole='gr';
1.21      www      9039:     } else {
1.82      www      9040:         my $cwosec=$url;
1.811     albertel 9041:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  9042:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   9043:             my $refused;
                   9044:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   9045:                 if (!(&allowed('c'.$role,$url))) {
                   9046:                     $refused = 1;
                   9047:                 }
                   9048:             } else {
                   9049:                 $refused = 1;
                   9050:             }
1.947     raeburn  9051:             if ($refused) {
1.1045    raeburn  9052:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   9053:                 if (!$selfenroll && $context eq 'course') {
                   9054:                     my %crsenv;
                   9055:                     if ($role eq 'cc' || $role eq 'co') {
                   9056:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9057:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   9058:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   9059:                                 if ($crsenv{'internal.courseowner'} eq 
                   9060:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   9061:                                     $refused = '';
                   9062:                                 }
                   9063:                             }
                   9064:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   9065:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   9066:                                 if ($crsenv{'internal.courseowner'} eq 
                   9067:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   9068:                                     $refused = '';
                   9069:                                 }
                   9070:                             }
                   9071:                         }
                   9072:                     }
                   9073:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947     raeburn  9074:                     $refused = '';
1.1017    raeburn  9075:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  9076:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  9077:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  9078:                         my $wrongcc;
                   9079:                         if ($cnum =~ /^$match_community$/) {
                   9080:                             $wrongcc = 1 if ($role eq 'cc');
                   9081:                         } else {
                   9082:                             $wrongcc = 1 if ($role eq 'co');
                   9083:                         }
                   9084:                         unless ($wrongcc) {
                   9085:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   9086:                             if ($crsenv{'internal.courseowner'} eq 
                   9087:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   9088:                                 $refused = '';
                   9089:                             }
1.1017    raeburn  9090:                         }
                   9091:                     }
1.1183    raeburn  9092:                 } elsif ($context eq 'requestauthor') {
                   9093:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   9094:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   9095:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   9096:                             $refused = '';
                   9097:                         } else {
                   9098:                             my %domdefaults = &get_domain_defaults($udom);
                   9099:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   9100:                                 my $checkbystatus;
                   9101:                                 if ($env{'user.adv'}) { 
                   9102:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   9103:                                     if ($disposition eq 'automatic') {
                   9104:                                         $refused = '';
                   9105:                                     } elsif ($disposition eq '') {
                   9106:                                         $checkbystatus = 1;
                   9107:                                     } 
                   9108:                                 } else {
                   9109:                                     $checkbystatus = 1;
                   9110:                                 }
                   9111:                                 if ($checkbystatus) {
                   9112:                                     if ($env{'environment.inststatus'}) {
                   9113:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   9114:                                         foreach my $type (@inststatuses) {
                   9115:                                             if (($type ne '') &&
                   9116:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   9117:                                                 $refused = '';
                   9118:                                             }
                   9119:                                         }
                   9120:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   9121:                                         $refused = '';
                   9122:                                     }
                   9123:                                 }
                   9124:                             }
                   9125:                         }
                   9126:                     }
1.1017    raeburn  9127:                 }
                   9128:                 if ($refused) {
1.947     raeburn  9129:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   9130:                              ' '.$role.' '.$end.' '.$start.' by '.
                   9131: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   9132:                     return 'refused';
                   9133:                 }
1.932     raeburn  9134:             }
1.1131    raeburn  9135:         } elsif ($role eq 'au') {
                   9136:             if ($url ne '/'.$udom.'/') {
                   9137:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   9138:                          ' to assign author role for '.$uname.':'.$udom.
                   9139:                          ' in domain: '.$url.' refused (wrong domain).');
                   9140:                 return 'refused';
                   9141:             }
1.104     www      9142:         }
1.21      www      9143:         $mrole=$role;
                   9144:     }
1.620     albertel 9145:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      9146:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      9147:     if ($end) { $command.='_'.$end; }
1.21      www      9148:     if ($start) {
                   9149: 	if ($end) { 
1.81      www      9150:            $command.='_'.$start; 
1.21      www      9151:         } else {
1.81      www      9152:            $command.='_0_'.$start;
1.21      www      9153:         }
                   9154:     }
1.739     raeburn  9155:     my $origstart = $start;
                   9156:     my $origend = $end;
1.957     raeburn  9157:     my $delflag;
1.357     www      9158: # actually delete
                   9159:     if ($deleteflag) {
1.373     www      9160: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      9161: # modify command to delete the role
1.620     albertel 9162:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      9163:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 9164: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      9165: # set start and finish to negative values for userrolelog
                   9166:            $start=-1;
                   9167:            $end=-1;
1.957     raeburn  9168:            $delflag = 1;
1.357     www      9169:         }
                   9170:     }
                   9171: # send command
1.349     www      9172:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      9173: # log new user role if status is ok
1.349     www      9174:     if ($answer eq 'ok') {
1.663     raeburn  9175: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  9176:         if (($role eq 'cc') || ($role eq 'in') ||
                   9177:             ($role eq 'ep') || ($role eq 'ad') ||
                   9178:             ($role eq 'ta') || ($role eq 'st') ||
                   9179:             ($role=~/^cr/) || ($role eq 'gr') ||
                   9180:             ($role eq 'co')) {
1.1200    raeburn  9181: # for course roles, perform group memberships changes triggered by role change.
                   9182:             unless ($role =~ /^gr/) {
                   9183:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   9184:                                                  $origstart,$selfenroll,$context);
                   9185:             }
1.1184    raeburn  9186:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9187:                            $selfenroll,$context);
                   9188:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
1.1334    raeburn  9189:                  ($role eq 'au') || ($role eq 'dc') || ($role eq 'dh') ||
                   9190:                  ($role eq 'da')) {
1.1184    raeburn  9191:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9192:                            $context);
                   9193:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
                   9194:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   9195:                              $context); 
                   9196:         }
1.1053    raeburn  9197:         if ($role eq 'cc') {
                   9198:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   9199:         }
1.349     www      9200:     }
                   9201:     return $answer;
1.169     harris41 9202: }
                   9203: 
1.1053    raeburn  9204: sub autoupdate_coowners {
                   9205:     my ($url,$end,$start,$uname,$udom) = @_;
                   9206:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   9207:     if (($cdom ne '') && ($cnum ne '')) {
                   9208:         my $now = time;
                   9209:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   9210:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   9211:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   9212:             my $instcode = $coursehash{'internal.coursecode'};
                   9213:             if ($instcode ne '') {
1.1056    raeburn  9214:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   9215:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  9216:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  9217:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   9218:                         if ($result eq 'valid') {
                   9219:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  9220:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   9221:                                     push(@newcoowners,$coowner);
                   9222:                                 }
                   9223:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   9224:                                     push(@newcoowners,$uname.':'.$udom);
                   9225:                                 }
                   9226:                                 @newcoowners = sort(@newcoowners);
                   9227:                             } else {
                   9228:                                 push(@newcoowners,$uname.':'.$udom);
                   9229:                             }
                   9230:                         } else {
                   9231:                             if ($coursehash{'internal.co-owners'}) {
                   9232:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   9233:                                     unless ($coowner eq $uname.':'.$udom) {
                   9234:                                         push(@newcoowners,$coowner);
                   9235:                                     }
                   9236:                                 }
                   9237:                                 unless (@newcoowners > 0) {
                   9238:                                     $delcoowners = 1;
                   9239:                                     $coowners = '';
                   9240:                                 }
                   9241:                             }
                   9242:                         }
                   9243:                         if (@newcoowners || $delcoowners) {
                   9244:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   9245:                                             $delcoowners,@newcoowners);
                   9246:                         }
                   9247:                     }
                   9248:                 }
                   9249:             }
                   9250:         }
                   9251:     }
                   9252: }
                   9253: 
                   9254: sub store_coowners {
                   9255:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   9256:     my $cid = $cdom.'_'.$cnum;
                   9257:     my ($coowners,$delresult,$putresult);
                   9258:     if (@newcoowners) {
                   9259:         $coowners = join(',',@newcoowners);
                   9260:         my %coownershash = (
                   9261:                             'internal.co-owners' => $coowners,
                   9262:                            );
                   9263:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   9264:         if ($putresult eq 'ok') {
                   9265:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   9266:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   9267:             }
                   9268:         }
                   9269:     }
                   9270:     if ($delcoowners) {
                   9271:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   9272:         if ($delresult eq 'ok') {
                   9273:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   9274:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   9275:             }
                   9276:         }
                   9277:     }
                   9278:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   9279:         my %crsinfo =
                   9280:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   9281:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   9282:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   9283:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   9284:         }
                   9285:     }
                   9286: }
                   9287: 
1.169     harris41 9288: # -------------------------------------------------- Modify user authentication
1.197     www      9289: # Overrides without validation
                   9290: 
1.169     harris41 9291: sub modifyuserauth {
                   9292:     my ($udom,$uname,$umode,$upass)=@_;
                   9293:     my $uhome=&homeserver($uname,$udom);
1.197     www      9294:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   9295:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 9296:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   9297:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 9298:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   9299: 		     &escape($upass),$uhome);
1.620     albertel 9300:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      9301:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   9302:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   9303:     &log($udom,,$uname,$uhome,
1.620     albertel 9304:         'Authentication changed by '.$env{'user.domain'}.', '.
                   9305:                                      $env{'user.name'}.', '.$umode.
1.197     www      9306:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 9307:     unless ($reply eq 'ok') {
1.197     www      9308:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 9309: 	return 'error: '.$reply;
                   9310:     }   
1.170     harris41 9311:     return 'ok';
1.80      www      9312: }
                   9313: 
1.81      www      9314: # --------------------------------------------------------------- Modify a user
1.80      www      9315: 
1.81      www      9316: sub modifyuser {
1.206     matthew  9317:     my ($udom,    $uname, $uid,
                   9318:         $umode,   $upass, $first,
                   9319:         $middle,  $last,  $gene,
1.1058    raeburn  9320:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 9321:     $udom= &LONCAPA::clean_domain($udom);
                   9322:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  9323:     my $showcandelete = 'none';
                   9324:     if (ref($candelete) eq 'ARRAY') {
                   9325:         if (@{$candelete} > 0) {
                   9326:             $showcandelete = join(', ',@{$candelete});
                   9327:         }
                   9328:     }
1.81      www      9329:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      9330:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  9331: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  9332:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   9333:                                      ' desiredhome not specified'). 
1.620     albertel 9334:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   9335:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 9336:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  9337:     my $newuser;
                   9338:     if ($uhome eq 'no_host') {
                   9339:         $newuser = 1;
                   9340:     }
1.80      www      9341: # ----------------------------------------------------------------- Create User
1.406     albertel 9342:     if (($uhome eq 'no_host') && 
                   9343: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      9344:         my $unhome='';
1.844     albertel 9345:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  9346:             $unhome = $desiredhome;
1.620     albertel 9347: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   9348: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  9349:         } else { # load balancing routine for determining $unhome
1.81      www      9350:             my $loadm=10000000;
1.841     albertel 9351: 	    my %servers = &get_servers($udom,'library');
                   9352: 	    foreach my $tryserver (keys(%servers)) {
                   9353: 		my $answer=reply('load',$tryserver);
                   9354: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   9355: 		    $loadm=$answer;
                   9356: 		    $unhome=$tryserver;
                   9357: 		}
1.80      www      9358: 	    }
                   9359:         }
                   9360:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  9361: 	    return 'error: unable to find a home server for '.$uname.
                   9362:                    ' in domain '.$udom;
1.80      www      9363:         }
                   9364:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   9365:                          &escape($upass),$unhome);
                   9366: 	unless ($reply eq 'ok') {
                   9367:             return 'error: '.$reply;
                   9368:         }   
1.230     stredwic 9369:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      9370:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  9371: 	    return 'error: unable verify users home machine.';
1.80      www      9372:         }
1.209     matthew  9373:     }   # End of creation of new user
1.80      www      9374: # ---------------------------------------------------------------------- Add ID
                   9375:     if ($uid) {
                   9376:        $uid=~tr/A-Z/a-z/;
                   9377:        my %uidhash=&idrget($udom,$uname);
1.196     www      9378:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   9379:          && (!$forceid)) {
1.80      www      9380: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  9381: 	      return 'error: user id "'.$uid.'" does not match '.
                   9382:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      9383:           }
                   9384:        } else {
1.1300    raeburn  9385: 	  &idput($udom,{$uname => $uid},$uhome,'ids');
1.80      www      9386:        }
                   9387:     }
                   9388: # -------------------------------------------------------------- Add names, etc
1.313     matthew  9389:     my @tmp=&get('environment',
1.899     raeburn  9390: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  9391:                     'permanentemail','inststatus'],
1.134     albertel 9392: 		   $udom,$uname);
1.1075    raeburn  9393:     my (%names,%oldnames);
1.313     matthew  9394:     if ($tmp[0] =~ m/^error:.*/) { 
                   9395:         %names=(); 
                   9396:     } else {
                   9397:         %names = @tmp;
1.1075    raeburn  9398:         %oldnames = %names;
1.313     matthew  9399:     }
1.388     www      9400: #
1.1058    raeburn  9401: # If name, email and/or uid are blank (e.g., because an uploaded file
                   9402: # of users did not contain them), do not overwrite existing values
                   9403: # unless field is in $candelete array ref.  
                   9404: #
                   9405: 
                   9406:     my @fields = ('firstname','middlename','lastname','generation',
                   9407:                   'permanentemail','id');
                   9408:     my %newvalues;
                   9409:     if (ref($candelete) eq 'ARRAY') {
                   9410:         foreach my $field (@fields) {
                   9411:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   9412:                 if ($field eq 'firstname') {
                   9413:                     $names{$field} = $first;
                   9414:                 } elsif ($field eq 'middlename') {
                   9415:                     $names{$field} = $middle;
                   9416:                 } elsif ($field eq 'lastname') {
                   9417:                     $names{$field} = $last;
                   9418:                 } elsif ($field eq 'generation') { 
                   9419:                     $names{$field} = $gene;
                   9420:                 } elsif ($field eq 'permanentemail') {
                   9421:                     $names{$field} = $email;
                   9422:                 } elsif ($field eq 'id') {
                   9423:                     $names{$field}  = $uid;
                   9424:                 }
                   9425:             }
                   9426:         }
                   9427:     }
1.388     www      9428:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  9429:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      9430:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  9431:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      9432:     if ($email) {
                   9433:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  9434:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      9435:     }
1.899     raeburn  9436:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  9437:     if (defined($inststatus)) {
                   9438:         $names{'inststatus'} = '';
                   9439:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   9440:         if (ref($usertypes) eq 'HASH') {
                   9441:             my @okstatuses; 
                   9442:             foreach my $item (split(/:/,$inststatus)) {
                   9443:                 if (defined($usertypes->{$item})) {
                   9444:                     push(@okstatuses,$item);  
                   9445:                 }
                   9446:             }
                   9447:             if (@okstatuses) {
                   9448:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   9449:             }
                   9450:         }
                   9451:     }
1.1075    raeburn  9452:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  9453:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  9454:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  9455:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   9456:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   9457:     } else {
                   9458:         $logmsg .= ' during self creation';
                   9459:     }
1.1075    raeburn  9460:     my $changed;
                   9461:     if ($newuser) {
                   9462:         $changed = 1;
                   9463:     } else {
                   9464:         foreach my $field (@fields) {
                   9465:             if ($names{$field} ne $oldnames{$field}) {
                   9466:                 $changed = 1;
                   9467:                 last;
                   9468:             }
                   9469:         }
                   9470:     }
                   9471:     unless ($changed) {
                   9472:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   9473:         &logthis($logmsg);
                   9474:         return 'ok';
                   9475:     }
                   9476:     my $reply = &put('environment', \%names, $udom,$uname);
                   9477:     if ($reply ne 'ok') { 
                   9478:         return 'error: '.$reply;
                   9479:     }
1.1087    raeburn  9480:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   9481:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   9482:     }
1.1075    raeburn  9483:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   9484:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   9485:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  9486:     &logthis($logmsg);
1.134     albertel 9487:     return 'ok';
1.80      www      9488: }
                   9489: 
1.81      www      9490: # -------------------------------------------------------------- Modify student
1.80      www      9491: 
1.81      www      9492: sub modifystudent {
                   9493:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  9494:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1314    raeburn  9495:         $selfenroll,$context,$inststatus,$credits,$instsec)=@_;
1.455     albertel 9496:     if (!$cid) {
1.620     albertel 9497: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 9498: 	    return 'not_in_class';
                   9499: 	}
1.80      www      9500:     }
                   9501: # --------------------------------------------------------------- Make the user
1.81      www      9502:     my $reply=&modifyuser
1.209     matthew  9503: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  9504:          $desiredhome,$email,$inststatus);
1.80      www      9505:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  9506:     # This will cause &modify_student_enrollment to get the uid from the
1.1235    raeburn  9507:     # student's environment
1.297     matthew  9508:     $uid = undef if (!$forceid);
1.455     albertel 9509:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  9510:                                         $gene,$usec,$end,$start,$type,$locktype,
1.1314    raeburn  9511:                                         $cid,$selfenroll,$context,$credits,$instsec);
1.297     matthew  9512:     return $reply;
                   9513: }
                   9514: 
                   9515: sub modify_student_enrollment {
1.1216    raeburn  9516:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
1.1314    raeburn  9517:         $locktype,$cid,$selfenroll,$context,$credits,$instsec) = @_;
1.455     albertel 9518:     my ($cdom,$cnum,$chome);
                   9519:     if (!$cid) {
1.620     albertel 9520: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 9521: 	    return 'not_in_class';
                   9522: 	}
1.620     albertel 9523: 	$cdom=$env{'course.'.$cid.'.domain'};
                   9524: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 9525:     } else {
                   9526: 	($cdom,$cnum)=split(/_/,$cid);
                   9527:     }
1.620     albertel 9528:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 9529:     if (!$chome) {
1.457     raeburn  9530: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  9531:     }
1.455     albertel 9532:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  9533:     # Make sure the user exists
1.81      www      9534:     my $uhome=&homeserver($uname,$udom);
                   9535:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   9536: 	return 'error: no such user';
                   9537:     }
1.297     matthew  9538:     # Get student data if we were not given enough information
                   9539:     if (!defined($first)  || $first  eq '' || 
                   9540:         !defined($last)   || $last   eq '' || 
                   9541:         !defined($uid)    || $uid    eq '' || 
                   9542:         !defined($middle) || $middle eq '' || 
                   9543:         !defined($gene)   || $gene   eq '') {
1.294     matthew  9544:         # They did not supply us with enough data to enroll the student, so
                   9545:         # we need to pick up more information.
1.297     matthew  9546:         my %tmp = &get('environment',
1.294     matthew  9547:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  9548:                        ,$udom,$uname);
                   9549: 
1.800     albertel 9550:         #foreach my $key (keys(%tmp)) {
                   9551:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 9552:         #}
1.294     matthew  9553:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   9554:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   9555:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  9556:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  9557:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   9558:     }
1.556     albertel 9559:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  9560:     my $user = "$uname:$udom";
                   9561:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 9562:     my $reply=cput('classlist',
1.1148    raeburn  9563: 		   {$user => 
1.1314    raeburn  9564: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits,$instsec) },
1.487     albertel 9565: 		   $cdom,$cnum);
1.1148    raeburn  9566:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   9567:         &devalidate_getsection_cache($udom,$uname,$cid);
                   9568:     } else { 
1.81      www      9569: 	return 'error: '.$reply;
                   9570:     }
1.297     matthew  9571:     # Add student role to user
1.83      www      9572:     my $uurl='/'.$cid;
1.81      www      9573:     $uurl=~s/\_/\//g;
                   9574:     if ($usec) {
                   9575: 	$uurl.='/'.$usec;
                   9576:     }
1.1148    raeburn  9577:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   9578:                              $selfenroll,$context);
                   9579:     if ($result ne 'ok') {
                   9580:         if ($old_entry{$user} ne '') {
                   9581:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   9582:         } else {
                   9583:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   9584:         }
                   9585:     }
                   9586:     return $result; 
1.21      www      9587: }
                   9588: 
1.556     albertel 9589: sub format_name {
                   9590:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   9591:     my $name;
                   9592:     if ($first ne 'lastname') {
                   9593: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   9594:     } else {
                   9595: 	if ($lastname=~/\S/) {
                   9596: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   9597: 	    $name=~s/\s+,/,/;
                   9598: 	} else {
                   9599: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   9600: 	}
                   9601:     }
                   9602:     $name=~s/^\s+//;
                   9603:     $name=~s/\s+$//;
                   9604:     $name=~s/\s+/ /g;
                   9605:     return $name;
                   9606: }
                   9607: 
1.84      www      9608: # ------------------------------------------------- Write to course preferences
                   9609: 
                   9610: sub writecoursepref {
                   9611:     my ($courseid,%prefs)=@_;
                   9612:     $courseid=~s/^\///;
                   9613:     $courseid=~s/\_/\//g;
                   9614:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   9615:     my $chome=homeserver($cnum,$cdomain);
                   9616:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   9617: 	return 'error: no such course';
                   9618:     }
                   9619:     my $cstring='';
1.800     albertel 9620:     foreach my $pref (keys(%prefs)) {
                   9621: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 9622:     }
1.84      www      9623:     $cstring=~s/\&$//;
                   9624:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   9625: }
                   9626: 
                   9627: # ---------------------------------------------------------- Make/modify course
                   9628: 
                   9629: sub createcourse {
1.741     raeburn  9630:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  9631:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      9632:     $url=&declutter($url);
                   9633:     my $cid='';
1.1028    raeburn  9634:     if ($context eq 'requestcourses') {
                   9635:         my $can_create = 0;
                   9636:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   9637:         if ($udom eq $ownerdom) {
                   9638:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   9639:                                   $context)) {
                   9640:                 $can_create = 1;
                   9641:             }
                   9642:         } else {
                   9643:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   9644:                                            $category);
                   9645:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   9646:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   9647:                 if (@curr > 0) {
                   9648:                     my @options = qw(approval validate autolimit);
                   9649:                     my $optregex = join('|',@options);
                   9650:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   9651:                         $can_create = 1;
                   9652:                     }
                   9653:                 }
                   9654:             }
                   9655:         }
                   9656:         if ($can_create) {
                   9657:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   9658:                 unless (&allowed('ccc',$udom)) {
                   9659:                     return 'refused'; 
                   9660:                 }
1.1017    raeburn  9661:             }
                   9662:         } else {
                   9663:             return 'refused';
                   9664:         }
1.1028    raeburn  9665:     } elsif (!&allowed('ccc',$udom)) {
                   9666:         return 'refused';
1.84      www      9667:     }
1.1011    raeburn  9668: # --------------------------------------------------------------- Get Unique ID
                   9669:     my $uname;
                   9670:     if ($cnum =~ /^$match_courseid$/) {
                   9671:         my $chome=&homeserver($cnum,$udom,'true');
                   9672:         if (($chome eq '') || ($chome eq 'no_host')) {
                   9673:             $uname = $cnum;
                   9674:         } else {
1.1038    raeburn  9675:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  9676:         }
                   9677:     } else {
1.1038    raeburn  9678:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  9679:     }
                   9680:     return $uname if ($uname =~ /^error/);
                   9681: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  9682:     if (!defined($course_server)) {
                   9683:         if (defined(&domain($udom,'primary'))) {
                   9684:             $course_server = &domain($udom,'primary');
                   9685:         } else {
                   9686:             $course_server = $env{'user.home'}; 
                   9687:         }
                   9688:     }
                   9689:     my %host_servers =
                   9690:         &Apache::lonnet::get_servers($udom,'library');
                   9691:     unless ($host_servers{$course_server}) {
                   9692:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  9693:     }
1.84      www      9694: # ------------------------------------------------------------- Make the course
                   9695:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  9696:                       $course_server);
1.84      www      9697:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  9698:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      9699:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   9700: 	return 'error: no such course';
                   9701:     }
1.271     www      9702: # ----------------------------------------------------------------- Course made
1.516     raeburn  9703: # log existence
1.1029    raeburn  9704:     my $now = time;
1.918     raeburn  9705:     my $newcourse = {
                   9706:                     $udom.'_'.$uname => {
1.921     raeburn  9707:                                      description => $description,
                   9708:                                      inst_code   => $inst_code,
                   9709:                                      owner       => $course_owner,
                   9710:                                      type        => $crstype,
1.1029    raeburn  9711:                                      creator     => $env{'user.name'}.':'.
                   9712:                                                     $env{'user.domain'},
                   9713:                                      created     => $now,
                   9714:                                      context     => $context,
1.918     raeburn  9715:                                                 },
                   9716:                     };
1.921     raeburn  9717:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      9718: # set toplevel url
1.271     www      9719:     my $topurl=$url;
                   9720:     unless ($nonstandard) {
                   9721: # ------------------------------------------ For standard courses, make top url
                   9722:         my $mapurl=&clutter($url);
1.278     www      9723:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 9724:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      9725: <map>
                   9726: <resource id="1" type="start"></resource>
                   9727: <resource id="2" src="$mapurl"></resource>
                   9728: <resource id="3" type="finish"></resource>
                   9729: <link index="1" from="1" to="2"></link>
                   9730: <link index="2" from="2" to="3"></link>
                   9731: </map>
                   9732: ENDINITMAP
                   9733:         $topurl=&declutter(
1.638     albertel 9734:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      9735:                           );
                   9736:     }
                   9737: # ----------------------------------------------------------- Write preferences
1.84      www      9738:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  9739:                      ('description'              => $description,
                   9740:                       'url'                      => $topurl,
                   9741:                       'internal.creator'         => $env{'user.name'}.':'.
                   9742:                                                     $env{'user.domain'},
                   9743:                       'internal.created'         => $now,
                   9744:                       'internal.creationcontext' => $context)
                   9745:                     );
1.84      www      9746:     return '/'.$udom.'/'.$uname;
                   9747: }
                   9748: 
1.1011    raeburn  9749: # ------------------------------------------------------------------- Create ID
                   9750: sub generate_coursenum {
1.1038    raeburn  9751:     my ($udom,$crstype) = @_;
1.1011    raeburn  9752:     my $domdesc = &domain($udom);
                   9753:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  9754:     my $first;
                   9755:     if ($crstype eq 'Community') {
                   9756:         $first = '0';
                   9757:     } else {
                   9758:         $first = int(1+rand(9)); 
                   9759:     } 
                   9760:     my $uname=$first.
1.1011    raeburn  9761:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   9762:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   9763:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   9764: # ----------------------------------------------- Make sure that does not exist
                   9765:     my $uhome=&homeserver($uname,$udom,'true');
                   9766:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  9767:         if ($crstype eq 'Community') {
                   9768:             $first = '0';
                   9769:         } else {
                   9770:             $first = int(1+rand(9));
                   9771:         }
                   9772:         $uname=$first.
1.1011    raeburn  9773:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   9774:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   9775:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   9776:         $uhome=&homeserver($uname,$udom,'true');
                   9777:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   9778:             return 'error: unable to generate unique course-ID';
                   9779:         }
                   9780:     }
                   9781:     return $uname;
                   9782: }
                   9783: 
1.813     albertel 9784: sub is_course {
1.1167    droeschl 9785:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   9786:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   9787: 
                   9788:     return unless $cdom and $cnum;
                   9789: 
                   9790:     my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
                   9791:         '.');
                   9792: 
1.1219    raeburn  9793:     return unless(exists($courses{$cdom.'_'.$cnum}));
1.1167    droeschl 9794:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 9795: }
                   9796: 
1.1015    raeburn  9797: sub store_userdata {
                   9798:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  9799:     my $result;
1.1016    raeburn  9800:     if ($datakey ne '') {
1.1013    raeburn  9801:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  9802:             if ($udom eq '' || $uname eq '') {
                   9803:                 $udom = $env{'user.domain'};
                   9804:                 $uname = $env{'user.name'};
                   9805:             }
                   9806:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  9807:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   9808:                 $result = 'error: no_host';
                   9809:             } else {
                   9810:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   9811:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   9812: 
                   9813:                 my $namevalue='';
                   9814:                 foreach my $key (keys(%{$storehash})) {
                   9815:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   9816:                 }
                   9817:                 $namevalue=~s/\&$//;
1.1224    raeburn  9818:                 unless ($namespace eq 'courserequests') {
                   9819:                     $datakey = &escape($datakey);
                   9820:                 }
1.1105    raeburn  9821:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   9822:                                   $namevalue,$uhome);
1.1013    raeburn  9823:             }
                   9824:         } else {
                   9825:             $result = 'error: data to store was not a hash reference'; 
                   9826:         }
                   9827:     } else {
                   9828:         $result= 'error: invalid requestkey'; 
                   9829:     }
                   9830:     return $result;
                   9831: }
                   9832: 
1.21      www      9833: # ---------------------------------------------------------- Assign Custom Role
                   9834: 
                   9835: sub assigncustomrole {
1.957     raeburn  9836:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      9837:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  9838:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      9839: }
                   9840: 
                   9841: # ----------------------------------------------------------------- Revoke Role
                   9842: 
                   9843: sub revokerole {
1.957     raeburn  9844:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      9845:     my $now=time;
1.965     raeburn  9846:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      9847: }
                   9848: 
                   9849: # ---------------------------------------------------------- Revoke Custom Role
                   9850: 
                   9851: sub revokecustomrole {
1.957     raeburn  9852:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      9853:     my $now=time;
1.357     www      9854:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  9855:            $deleteflag,$selfenroll,$context);
1.17      www      9856: }
                   9857: 
1.533     banghart 9858: # ------------------------------------------------------------ Disk usage
1.535     albertel 9859: sub diskusage {
1.955     raeburn  9860:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   9861:     $directorypath =~ s/\/$//;
                   9862:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   9863:                        .&escape($getpropath).':'.&escape($uname).':'
                   9864:                        .&escape($udom),homeserver($uname,$udom));
                   9865:     if ($listing eq 'unknown_cmd') {
                   9866:         if ($getpropath) {
                   9867:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   9868:         }
                   9869:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   9870:     }
1.514     albertel 9871:     return $listing;
1.512     banghart 9872: }
                   9873: 
1.566     banghart 9874: sub is_locked {
1.1096    raeburn  9875:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 9876:     my @check;
                   9877:     my $is_locked;
1.1093    raeburn  9878:     push (@check,$file_name);
1.613     albertel 9879:     my %locked = &get('file_permissions',\@check,
1.620     albertel 9880: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 9881:     my ($tmp)=keys(%locked);
                   9882:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  9883:     
1.566     banghart 9884:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  9885:         $is_locked = 'false';
                   9886:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  9887:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  9888:                $is_locked = 'true';
1.1096    raeburn  9889:                if (ref($which) eq 'ARRAY') {
                   9890:                    push(@{$which},$entry);
                   9891:                } else {
                   9892:                    last;
                   9893:                }
1.745     raeburn  9894:            }
                   9895:        }
1.566     banghart 9896:     } else {
                   9897:         $is_locked = 'false';
                   9898:     }
1.1093    raeburn  9899:     return $is_locked;
1.566     banghart 9900: }
                   9901: 
1.759     albertel 9902: sub declutter_portfile {
                   9903:     my ($file) = @_;
1.833     albertel 9904:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 9905:     return $file;
                   9906: }
                   9907: 
1.559     banghart 9908: # ------------------------------------------------------------- Mark as Read Only
                   9909: 
                   9910: sub mark_as_readonly {
                   9911:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 9912:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 9913:     my ($tmp)=keys(%current_permissions);
                   9914:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 9915:     foreach my $file (@{$files}) {
1.759     albertel 9916: 	$file = &declutter_portfile($file);
1.561     banghart 9917:         push(@{$current_permissions{$file}},$what);
1.559     banghart 9918:     }
1.613     albertel 9919:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 9920:     return;
                   9921: }
                   9922: 
1.572     banghart 9923: # ------------------------------------------------------------Save Selected Files
                   9924: 
                   9925: sub save_selected_files {
                   9926:     my ($user, $path, @files) = @_;
                   9927:     my $filename = $user."savedfiles";
1.573     banghart 9928:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 9929:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 9930:     foreach my $file (@files) {
1.620     albertel 9931:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 9932:     }
                   9933:     foreach my $file (@other_files) {
1.574     banghart 9934:         print (OUT $file."\n");
1.572     banghart 9935:     }
1.574     banghart 9936:     close (OUT);
1.572     banghart 9937:     return 'ok';
                   9938: }
                   9939: 
1.574     banghart 9940: sub clear_selected_files {
                   9941:     my ($user) = @_;
                   9942:     my $filename = $user."savedfiles";
1.1117    foxr     9943:     open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574     banghart 9944:     print (OUT undef);
                   9945:     close (OUT);
                   9946:     return ("ok");    
                   9947: }
                   9948: 
1.572     banghart 9949: sub files_in_path {
                   9950:     my ($user, $path) = @_;
                   9951:     my $filename = $user."savedfiles";
                   9952:     my %return_files;
1.1117    foxr     9953:     open (IN, '<'.LONCAPA::tempdir().$filename);
1.573     banghart 9954:     while (my $line_in = <IN>) {
1.574     banghart 9955:         chomp ($line_in);
                   9956:         my @paths_and_file = split (m!/!, $line_in);
                   9957:         my $file_part = pop (@paths_and_file);
                   9958:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 9959:         $path_part.='/';
                   9960:         my $path_and_file = $path_part.$file_part;
                   9961:         if ($path_part eq $path) {
                   9962:             $return_files{$file_part}= 'selected';
                   9963:         }
                   9964:     }
1.574     banghart 9965:     close (IN);
                   9966:     return (\%return_files);
1.572     banghart 9967: }
                   9968: 
                   9969: # called in portfolio select mode, to show files selected NOT in current directory
                   9970: sub files_not_in_path {
                   9971:     my ($user, $path) = @_;
                   9972:     my $filename = $user."savedfiles";
                   9973:     my @return_files;
                   9974:     my $path_part;
1.1117    foxr     9975:     open(IN, '<'.LONCAPA::.$filename);
1.800     albertel 9976:     while (my $line = <IN>) {
1.572     banghart 9977:         #ok, I know it's clunky, but I want it to work
1.800     albertel 9978:         my @paths_and_file = split(m|/|, $line);
                   9979:         my $file_part = pop(@paths_and_file);
                   9980:         chomp($file_part);
                   9981:         my $path_part = join('/', @paths_and_file);
1.572     banghart 9982:         $path_part .= '/';
                   9983:         my $path_and_file = $path_part.$file_part;
                   9984:         if ($path_part ne $path) {
1.800     albertel 9985:             push(@return_files, ($path_and_file));
1.572     banghart 9986:         }
                   9987:     }
1.800     albertel 9988:     close(OUT);
1.574     banghart 9989:     return (@return_files);
1.572     banghart 9990: }
                   9991: 
1.1273    raeburn  9992: #------------------------------Submitted/Handedback Portfolio Files Versioning
                   9993:  
                   9994: sub portfiles_versioning {
                   9995:     my ($symb,$domain,$stu_name,$portfiles,$versioned_portfiles) = @_;
                   9996:     my $portfolio_root = '/userfiles/portfolio';
                   9997:     return unless ((ref($portfiles) eq 'ARRAY') && (ref($versioned_portfiles) eq 'ARRAY'));
                   9998:     foreach my $file (@{$portfiles}) {
                   9999:         &unmark_as_readonly($domain,$stu_name,[$symb,$env{'request.course.id'}],$file);
                   10000:         my ($directory,$answer_file) =($file =~ /^(.*?)([^\/]*)$/);
                   10001:         my ($answer_name,$answer_ver,$answer_ext) = &file_name_version_ext($answer_file);
                   10002:         my $getpropath = 1;
                   10003:         my ($dir_list,$listerror) = &dirlist($portfolio_root.$directory,$domain,
                   10004:                                              $stu_name,$getpropath);
                   10005:         my $version = &get_next_version($answer_name,$answer_ext,$dir_list);
                   10006:         my $new_answer = 
                   10007:             &version_selected_portfile($domain,$stu_name,$directory,$answer_file,$version);
                   10008:         if ($new_answer ne 'problem getting file') {
                   10009:             push(@{$versioned_portfiles}, $directory.$new_answer);
                   10010:             &mark_as_readonly($domain,$stu_name,[$directory.$new_answer],
                   10011:                               [$symb,$env{'request.course.id'},'graded']);
                   10012:         }
                   10013:     }
                   10014: }
                   10015: 
                   10016: sub get_next_version {
                   10017:     my ($answer_name, $answer_ext, $dir_list) = @_;
                   10018:     my $version;
                   10019:     if (ref($dir_list) eq 'ARRAY') {
                   10020:         foreach my $row (@{$dir_list}) {
                   10021:             my ($file) = split(/\&/,$row,2);
                   10022:             my ($file_name,$file_version,$file_ext) =
                   10023:                 &file_name_version_ext($file);
                   10024:             if (($file_name eq $answer_name) &&
                   10025:                 ($file_ext eq $answer_ext)) {
                   10026:                      # gets here if filename and extension match,
                   10027:                      # regardless of version
                   10028:                 if ($file_version ne '') {
                   10029:                     # a versioned file is found  so save it for later
                   10030:                     if ($file_version > $version) {
                   10031:                         $version = $file_version;
                   10032:                     }
                   10033:                 }
                   10034:             }
                   10035:         }
                   10036:     }
                   10037:     $version ++;
                   10038:     return($version);
                   10039: }
                   10040: 
                   10041: sub version_selected_portfile {
                   10042:     my ($domain,$stu_name,$directory,$file_name,$version) = @_;
                   10043:     my ($answer_name,$answer_ver,$answer_ext) =
                   10044:         &file_name_version_ext($file_name);
                   10045:     my $new_answer;
                   10046:     $env{'form.copy'} =
                   10047:         &getfile("/uploaded/$domain/$stu_name/portfolio$directory$file_name");
                   10048:     if($env{'form.copy'} eq '-1') {
                   10049:         $new_answer = 'problem getting file';
                   10050:     } else {
                   10051:         $new_answer = $answer_name.'.'.$version.'.'.$answer_ext;
                   10052:         my $copy_result = 
                   10053:             &finishuserfileupload($stu_name,$domain,'copy',
                   10054:                                   '/portfolio'.$directory.$new_answer);
                   10055:     }
                   10056:     undef($env{'form.copy'});
                   10057:     return ($new_answer);
                   10058: }
                   10059: 
                   10060: sub file_name_version_ext {
                   10061:     my ($file)=@_;
                   10062:     my @file_parts = split(/\./, $file);
                   10063:     my ($name,$version,$ext);
                   10064:     if (@file_parts > 1) {
                   10065:         $ext=pop(@file_parts);
                   10066:         if (@file_parts > 1 && $file_parts[-1] =~ /^\d+$/) {
                   10067:             $version=pop(@file_parts);
                   10068:         }
                   10069:         $name=join('.',@file_parts);
                   10070:     } else {
                   10071:         $name=join('.',@file_parts);
                   10072:     }
                   10073:     return($name,$version,$ext);
                   10074: }
                   10075: 
1.745     raeburn  10076: #----------------------------------------------Get portfolio file permissions
1.629     banghart 10077: 
1.745     raeburn  10078: sub get_portfile_permissions {
                   10079:     my ($domain,$user) = @_;
1.613     albertel 10080:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 10081:     my ($tmp)=keys(%current_permissions);
                   10082:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  10083:     return \%current_permissions;
                   10084: }
                   10085: 
                   10086: #---------------------------------------------Get portfolio file access controls
                   10087: 
1.749     raeburn  10088: sub get_access_controls {
1.745     raeburn  10089:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 10090:     my %access;
                   10091:     my $real_file = $file;
                   10092:     $file =~ s/\.meta$//;
1.745     raeburn  10093:     if (defined($file)) {
1.749     raeburn  10094:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   10095:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 10096:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  10097:             }
                   10098:         }
1.745     raeburn  10099:     } else {
1.749     raeburn  10100:         foreach my $key (keys(%{$current_permissions})) {
                   10101:             if ($key =~ /\0accesscontrol$/) {
                   10102:                 if (defined($group)) {
                   10103:                     if ($key !~ m-^\Q$group\E/-) {
                   10104:                         next;
                   10105:                     }
                   10106:                 }
                   10107:                 my ($fullpath) = split(/\0/,$key);
                   10108:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   10109:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   10110:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   10111:                     }
                   10112:                 }
                   10113:             }
                   10114:         }
                   10115:     }
                   10116:     return %access;
                   10117: }
                   10118: 
                   10119: sub modify_access_controls {
                   10120:     my ($file_name,$changes,$domain,$user)=@_;
                   10121:     my ($outcome,$deloutcome);
                   10122:     my %store_permissions;
                   10123:     my %new_values;
                   10124:     my %new_control;
                   10125:     my %translation;
                   10126:     my @deletions = ();
                   10127:     my $now = time;
                   10128:     if (exists($$changes{'activate'})) {
                   10129:         if (ref($$changes{'activate'}) eq 'HASH') {
                   10130:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   10131:             my $numnew = scalar(@newitems);
                   10132:             for (my $i=0; $i<$numnew; $i++) {
                   10133:                 my $newkey = $newitems[$i];
                   10134:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  10135:                 if ($newkey =~ /^\d+:/) { 
                   10136:                     $newkey =~ s/^(\d+)/$newid/;
                   10137:                     $translation{$1} = $newid;
                   10138:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   10139:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   10140:                     $translation{$1} = $newid;
                   10141:                 }
1.749     raeburn  10142:                 $new_values{$file_name."\0".$newkey} = 
                   10143:                                           $$changes{'activate'}{$newitems[$i]};
                   10144:                 $new_control{$newkey} = $now;
                   10145:             }
                   10146:         }
                   10147:     }
                   10148:     my %todelete;
                   10149:     my %changed_items;
                   10150:     foreach my $action ('delete','update') {
                   10151:         if (exists($$changes{$action})) {
                   10152:             if (ref($$changes{$action}) eq 'HASH') {
                   10153:                 foreach my $key (keys(%{$$changes{$action}})) {
                   10154:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   10155:                     if ($action eq 'delete') { 
                   10156:                         $todelete{$itemnum} = 1;
                   10157:                     } else {
                   10158:                         $changed_items{$itemnum} = $key;
                   10159:                     }
                   10160:                 }
1.745     raeburn  10161:             }
                   10162:         }
1.749     raeburn  10163:     }
                   10164:     # get lock on access controls for file.
                   10165:     my $lockhash = {
                   10166:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   10167:                                                        ':'.$env{'user.domain'},
                   10168:                    }; 
                   10169:     my $tries = 0;
                   10170:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   10171:    
1.1288    damieng  10172:     while (($gotlock ne 'ok') && $tries < 10) {
1.749     raeburn  10173:         $tries ++;
1.1289    damieng  10174:         sleep(0.1);
1.749     raeburn  10175:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   10176:     }
                   10177:     if ($gotlock eq 'ok') {
                   10178:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   10179:         my ($tmp)=keys(%curr_permissions);
                   10180:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   10181:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   10182:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   10183:             if (ref($curr_controls) eq 'HASH') {
                   10184:                 foreach my $control_item (keys(%{$curr_controls})) {
                   10185:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   10186:                     if (defined($todelete{$itemnum})) {
                   10187:                         push(@deletions,$file_name."\0".$control_item);
                   10188:                     } else {
                   10189:                         if (defined($changed_items{$itemnum})) {
                   10190:                             $new_control{$changed_items{$itemnum}} = $now;
                   10191:                             push(@deletions,$file_name."\0".$control_item);
                   10192:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   10193:                         } else {
                   10194:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   10195:                         }
                   10196:                     }
1.745     raeburn  10197:                 }
                   10198:             }
                   10199:         }
1.970     raeburn  10200:         my ($group);
                   10201:         if (&is_course($domain,$user)) {
                   10202:             ($group,my $file) = split(/\//,$file_name,2);
                   10203:         }
1.749     raeburn  10204:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   10205:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   10206:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   10207:         #  remove lock
                   10208:         my @del_lock = ($file_name."\0".'locked_access_records');
                   10209:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  10210:         my $sqlresult =
1.970     raeburn  10211:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  10212:                                     $group);
1.749     raeburn  10213:     } else {
                   10214:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  10215:     }
1.749     raeburn  10216:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  10217: }
                   10218: 
1.827     raeburn  10219: sub make_public_indefinitely {
1.1271    raeburn  10220:     my (@requrl) = @_;
                   10221:     return &automated_portfile_access('public',\@requrl);
                   10222: }
                   10223: 
                   10224: sub automated_portfile_access {
                   10225:     my ($accesstype,$addsref,$delsref,$info) = @_;
1.1273    raeburn  10226:     unless (($accesstype eq 'public') || ($accesstype eq 'ip')) {
                   10227:         return 'invalid';
                   10228:     }
1.1271    raeburn  10229:     my %urls;
                   10230:     if (ref($addsref) eq 'ARRAY') {
                   10231:         foreach my $requrl (@{$addsref}) {
                   10232:             if (&is_portfolio_url($requrl)) {
                   10233:                 unless (exists($urls{$requrl})) {
                   10234:                     $urls{$requrl} = 'add';
                   10235:                 }
                   10236:             }
                   10237:         }
                   10238:     }
                   10239:     if (ref($delsref) eq 'ARRAY') {
                   10240:         foreach my $requrl (@{$delsref}) { 
                   10241:             if (&is_portfolio_url($requrl)) {
                   10242:                 unless (exists($urls{$requrl})) {
                   10243:                     $urls{$requrl} = 'delete'; 
                   10244:                 }
                   10245:             }
                   10246:         }
                   10247:     }
                   10248:     unless (keys(%urls)) {
                   10249:         return 'invalid';
                   10250:     }
                   10251:     my $ip;
                   10252:     if ($accesstype eq 'ip') {
                   10253:         if (ref($info) eq 'HASH') {
                   10254:             if ($info->{'ip'} ne '') {
                   10255:                 $ip = $info->{'ip'};
                   10256:             }
                   10257:         }
                   10258:         if ($ip eq '') {
                   10259:             return 'invalid';
                   10260:         }
                   10261:     }
                   10262:     my $errors;
1.827     raeburn  10263:     my $now = time;
1.1271    raeburn  10264:     my %current_perms;
                   10265:     foreach my $requrl (sort(keys(%urls))) {
                   10266:         my $action;
                   10267:         if ($urls{$requrl} eq 'add') {
                   10268:             $action = 'activate';
                   10269:         } else {
                   10270:             $action = 'none';
                   10271:         }
                   10272:         my $aclnum = 0;
1.827     raeburn  10273:         my (undef,$udom,$unum,$file_name,$group) =
                   10274:             &parse_portfolio_url($requrl);
1.1271    raeburn  10275:         unless (exists($current_perms{$unum.':'.$udom})) {
                   10276:             $current_perms{$unum.':'.$udom} = &get_portfile_permissions($udom,$unum);
                   10277:         }
                   10278:         my %access_controls = &get_access_controls($current_perms{$unum.':'.$udom},
1.827     raeburn  10279:                                                    $group,$file_name);
                   10280:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   10281:             my ($num,$scope,$end,$start) = 
                   10282:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
1.1271    raeburn  10283:             if ($scope eq $accesstype) {
                   10284:                 if (($start <= $now) && ($end == 0)) {
                   10285:                     if ($accesstype eq 'ip') {
                   10286:                         if (ref($access_controls{$file_name}{$key}) eq 'HASH') {
                   10287:                             if (ref($access_controls{$file_name}{$key}{'ip'}) eq 'ARRAY') {
                   10288:                                 if (grep(/^\Q$ip\E$/,@{$access_controls{$file_name}{$key}{'ip'}})) {
                   10289:                                     if ($urls{$requrl} eq 'add') {
                   10290:                                         $action = 'none';
                   10291:                                         last;
                   10292:                                     } else {
                   10293:                                         $action = 'delete';
                   10294:                                         $aclnum = $num;
                   10295:                                         last;
                   10296:                                     }
                   10297:                                 }
                   10298:                             }
                   10299:                         }
                   10300:                     } elsif ($accesstype eq 'public') {
                   10301:                         if ($urls{$requrl} eq 'add') {
                   10302:                             $action = 'none';
                   10303:                             last;
                   10304:                         } else {
                   10305:                             $action = 'delete';
                   10306:                             $aclnum = $num;
                   10307:                             last;
                   10308:                         }
                   10309:                     }
                   10310:                 } elsif ($accesstype eq 'public') {
1.827     raeburn  10311:                     $action = 'update';
                   10312:                     $aclnum = $num;
1.1271    raeburn  10313:                     last;
1.827     raeburn  10314:                 }
                   10315:             }
                   10316:         }
                   10317:         if ($action eq 'none') {
1.1271    raeburn  10318:             next;
1.827     raeburn  10319:         } else {
                   10320:             my %changes;
                   10321:             my $newend = 0;
                   10322:             my $newstart = $now;
1.1271    raeburn  10323:             my $newkey = $aclnum.':'.$accesstype.'_'.$newend.'_'.$newstart;
1.827     raeburn  10324:             $changes{$action}{$newkey} = {
1.1271    raeburn  10325:                 type => $accesstype,
1.827     raeburn  10326:                 time => {
                   10327:                     start => $newstart,
                   10328:                     end   => $newend,
                   10329:                 },
                   10330:             };
1.1271    raeburn  10331:             if ($accesstype eq 'ip') {
                   10332:                 $changes{$action}{$newkey}{'ip'} = [$ip];
                   10333:             }
1.827     raeburn  10334:             my ($outcome,$deloutcome,$new_values,$translation) =
                   10335:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
1.1271    raeburn  10336:             unless ($outcome eq 'ok') {
                   10337:                 $errors .= $outcome.' ';
                   10338:             }
1.827     raeburn  10339:         }
1.1271    raeburn  10340:     }
                   10341:     if ($errors) {
                   10342:         $errors =~ s/\s$//;
                   10343:         return $errors;
1.827     raeburn  10344:     } else {
1.1271    raeburn  10345:         return 'ok';
1.827     raeburn  10346:     }
                   10347: }
                   10348: 
1.745     raeburn  10349: #------------------------------------------------------Get Marked as Read Only
                   10350: 
                   10351: sub get_marked_as_readonly {
                   10352:     my ($domain,$user,$what,$group) = @_;
                   10353:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 10354:     my @readonly_files;
1.629     banghart 10355:     my $cmp1=$what;
                   10356:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  10357:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   10358:         if (defined($group)) {
                   10359:             if ($file_name !~ m-^\Q$group\E/-) {
                   10360:                 next;
                   10361:             }
                   10362:         }
1.561     banghart 10363:         if (ref($value) eq "ARRAY"){
                   10364:             foreach my $stored_what (@{$value}) {
1.629     banghart 10365:                 my $cmp2=$stored_what;
1.759     albertel 10366:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  10367:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  10368:                 }
1.629     banghart 10369:                 if ($cmp1 eq $cmp2) {
1.561     banghart 10370:                     push(@readonly_files, $file_name);
1.745     raeburn  10371:                     last;
1.563     banghart 10372:                 } elsif (!defined($what)) {
                   10373:                     push(@readonly_files, $file_name);
1.745     raeburn  10374:                     last;
1.561     banghart 10375:                 }
                   10376:             }
1.745     raeburn  10377:         }
1.561     banghart 10378:     }
                   10379:     return @readonly_files;
                   10380: }
1.577     banghart 10381: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 10382: 
1.577     banghart 10383: sub get_marked_as_readonly_hash {
1.745     raeburn  10384:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 10385:     my %readonly_files;
1.745     raeburn  10386:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   10387:         if (defined($group)) {
                   10388:             if ($file_name !~ m-^\Q$group\E/-) {
                   10389:                 next;
                   10390:             }
                   10391:         }
1.577     banghart 10392:         if (ref($value) eq "ARRAY"){
                   10393:             foreach my $stored_what (@{$value}) {
1.745     raeburn  10394:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 10395:                     foreach my $lock_descriptor(@{$stored_what}) {
                   10396:                         if ($lock_descriptor eq 'graded') {
                   10397:                             $readonly_files{$file_name} = 'graded';
                   10398:                         } elsif ($lock_descriptor eq 'handback') {
                   10399:                             $readonly_files{$file_name} = 'handback';
                   10400:                         } else {
                   10401:                             if (!exists($readonly_files{$file_name})) {
                   10402:                                 $readonly_files{$file_name} = 'locked';
                   10403:                             }
                   10404:                         }
1.745     raeburn  10405:                     }
1.750     banghart 10406:                 } 
1.577     banghart 10407:             }
                   10408:         } 
                   10409:     }
                   10410:     return %readonly_files;
                   10411: }
1.559     banghart 10412: # ------------------------------------------------------------ Unmark as Read Only
                   10413: 
                   10414: sub unmark_as_readonly {
1.629     banghart 10415:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   10416:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  10417:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 10418:     $file_name = &declutter_portfile($file_name);
1.634     albertel 10419:     my $symb_crs = $what;
                   10420:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  10421:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 10422:     my ($tmp)=keys(%current_permissions);
                   10423:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  10424:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 10425:     foreach my $file (@readonly_files) {
1.759     albertel 10426: 	my $clean_file = &declutter_portfile($file);
                   10427: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 10428: 	my $current_locks = $current_permissions{$file};
1.563     banghart 10429:         my @new_locks;
                   10430:         my @del_keys;
                   10431:         if (ref($current_locks) eq "ARRAY"){
                   10432:             foreach my $locker (@{$current_locks}) {
1.632     albertel 10433:                 my $compare=$locker;
1.749     raeburn  10434:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  10435:                     $compare=join('',@{$locker});
1.746     raeburn  10436:                     if ($compare ne $symb_crs) {
                   10437:                         push(@new_locks, $locker);
                   10438:                     }
1.563     banghart 10439:                 }
                   10440:             }
1.650     albertel 10441:             if (scalar(@new_locks) > 0) {
1.563     banghart 10442:                 $current_permissions{$file} = \@new_locks;
                   10443:             } else {
                   10444:                 push(@del_keys, $file);
1.613     albertel 10445:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 10446:                 delete($current_permissions{$file});
1.563     banghart 10447:             }
                   10448:         }
1.561     banghart 10449:     }
1.613     albertel 10450:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 10451:     return;
                   10452: }
1.512     banghart 10453: 
1.17      www      10454: # ------------------------------------------------------------ Directory lister
                   10455: 
                   10456: sub dirlist {
1.955     raeburn  10457:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      10458:     $uri=~s/^\///;
                   10459:     $uri=~s/\/$//;
1.253     stredwic 10460:     my ($udom, $uname);
1.955     raeburn  10461:     if ($getuserdir) {
1.253     stredwic 10462:         $udom = $userdomain;
                   10463:         $uname = $username;
1.955     raeburn  10464:     } else {
                   10465:         (undef,$udom,$uname)=split(/\//,$uri);
                   10466:         if(defined($userdomain)) {
                   10467:             $udom = $userdomain;
                   10468:         }
                   10469:         if(defined($username)) {
                   10470:             $uname = $username;
                   10471:         }
1.253     stredwic 10472:     }
1.955     raeburn  10473:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 10474: 
1.955     raeburn  10475:     $dirRoot = $perlvar{'lonDocRoot'};
                   10476:     if (defined($getpropath)) {
                   10477:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 10478:         $dirRoot =~ s/\/$//;
1.955     raeburn  10479:     } elsif (defined($getuserdir)) {
                   10480:         my $subdir=$uname.'__';
                   10481:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   10482:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   10483:                    ."/$udom/$subdir/$uname";
                   10484:     } elsif (defined($alternateRoot)) {
                   10485:         $dirRoot = $alternateRoot;
1.751     banghart 10486:     }
1.253     stredwic 10487: 
                   10488:     if($udom) {
                   10489:         if($uname) {
1.1135    raeburn  10490:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  10491:             if ($uhome eq 'no_host') {
                   10492:                 return ([],'no_host');
                   10493:             }
1.955     raeburn  10494:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  10495:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  10496:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  10497:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  10498:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  10499:             } else {
                   10500:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   10501:             }
1.605     matthew  10502:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  10503:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  10504:                 @listing_results = split(/:/,$listing);
                   10505:             } else {
                   10506:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   10507:             }
1.1135    raeburn  10508:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  10509:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   10510:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   10511:                 return ([],$listing);
                   10512:             } else {
                   10513:                 return (\@listing_results);
1.1135    raeburn  10514:             }
1.955     raeburn  10515:         } elsif(!$alternateRoot) {
1.1136    raeburn  10516:             my (%allusers,%listerror);
1.841     albertel 10517: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  10518:  	    foreach my $tryserver (keys(%servers)) {
                   10519:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   10520:                                   &escape($udom),$tryserver);
                   10521:                 if ($listing eq 'unknown_cmd') {
                   10522: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   10523: 				      $udom, $tryserver);
                   10524:                 } else {
                   10525:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   10526:                 }
1.841     albertel 10527: 		if ($listing eq 'unknown_cmd') {
                   10528: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   10529: 				      $udom, $tryserver);
                   10530: 		    @listing_results = split(/:/,$listing);
                   10531: 		} else {
                   10532: 		    @listing_results =
                   10533: 			map { &unescape($_); } split(/:/,$listing);
                   10534: 		}
1.1136    raeburn  10535:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   10536:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   10537:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   10538:                     $listerror{$tryserver} = $listing;
                   10539:                 } else {
1.841     albertel 10540: 		    foreach my $line (@listing_results) {
                   10541: 			my ($entry) = split(/&/,$line,2);
                   10542: 			$allusers{$entry} = 1;
                   10543: 		    }
                   10544: 		}
1.253     stredwic 10545:             }
1.1136    raeburn  10546:             my @alluserslist=();
1.800     albertel 10547:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  10548:                 push(@alluserslist,$user.'&user');
1.253     stredwic 10549:             }
1.1318    droeschl 10550: 
                   10551:             if (!%listerror) {
                   10552:                 # no errors
                   10553:                 return (\@alluserslist);
                   10554:             } elsif (scalar(keys(%servers)) == 1) {
                   10555:                 # one library server, one error 
                   10556:                 my ($key) = keys(%listerror);
                   10557:                 return (\@alluserslist, $listerror{$key});
                   10558:             } elsif ( grep { $_ eq 'con_lost' } values(%listerror) ) {
                   10559:                 # con_lost indicates that we might miss data from at least one
                   10560:                 # library server
                   10561:                 return (\@alluserslist, 'con_lost');
                   10562:             } else {
                   10563:                 # multiple library servers and no con_lost -> data should be
                   10564:                 # complete. 
                   10565:                 return (\@alluserslist);
                   10566:             }
                   10567: 
1.253     stredwic 10568:         } else {
1.1136    raeburn  10569:             return ([],'missing username');
1.253     stredwic 10570:         }
1.955     raeburn  10571:     } elsif(!defined($getpropath)) {
1.1136    raeburn  10572:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   10573:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   10574:         return (\@all_domains);
1.955     raeburn  10575:     } else {
1.1136    raeburn  10576:         return ([],'missing domain');
1.275     stredwic 10577:     }
                   10578: }
                   10579: 
                   10580: # --------------------------------------------- GetFileTimestamp
                   10581: # This function utilizes dirlist and returns the date stamp for
                   10582: # when it was last modified.  It will also return an error of -1
                   10583: # if an error occurs
                   10584: 
                   10585: sub GetFileTimestamp {
1.955     raeburn  10586:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 10587:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   10588:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  10589:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   10590:                                     undef,$getuserdir);
                   10591:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   10592:         return -1;
                   10593:     }
                   10594:     if (ref($fileref) eq 'ARRAY') {
                   10595:         my @stats = split('&',$fileref->[0]);
1.375     matthew  10596:         # @stats contains first the filename, then the stat output
                   10597:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 10598:     } else {
                   10599:         return -1;
1.253     stredwic 10600:     }
1.26      www      10601: }
                   10602: 
1.712     albertel 10603: sub stat_file {
                   10604:     my ($uri) = @_;
1.787     albertel 10605:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 10606: 
1.955     raeburn  10607:     my ($udom,$uname,$file);
1.712     albertel 10608:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   10609: 	($udom,$uname,$file) =
1.811     albertel 10610: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 10611: 	$file = 'userfiles/'.$file;
                   10612:     }
                   10613:     if ($uri =~ m-^/res/-) {
                   10614: 	($udom,$uname) = 
1.807     albertel 10615: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 10616: 	$file = $uri;
                   10617:     }
                   10618: 
                   10619:     if (!$udom || !$uname || !$file) {
                   10620: 	# unable to handle the uri
                   10621: 	return ();
                   10622:     }
1.956     raeburn  10623:     my $getpropath;
                   10624:     if ($file =~ /^userfiles\//) {
                   10625:         $getpropath = 1;
                   10626:     }
1.1136    raeburn  10627:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   10628:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   10629:         return ();
                   10630:     } else {
                   10631:         if (ref($listref) eq 'ARRAY') {
                   10632:             my @stats = split('&',$listref->[0]);
                   10633: 	    shift(@stats); #filename is first
                   10634: 	    return @stats;
                   10635:         }
1.712     albertel 10636:     }
                   10637:     return ();
                   10638: }
                   10639: 
1.1313    raeburn  10640: # --------------------------------------------------------- recursedirs
                   10641: # Recursive function to traverse either a specific user's Authoring Space
                   10642: # or corresponding Published Resource Space, and populate the hash ref:
                   10643: # $dirhashref with URLs of all directories, and if $filehashref hash
                   10644: # ref arg is provided, the URLs of any files, excluding versioned, .meta,
                   10645: # or .rights files in resource space, and .meta, .save, .log, and .bak
                   10646: # files in Authoring Space.
                   10647: #
                   10648: # Inputs:
                   10649: #
                   10650: # $is_home - true if current server is home server for user's space
                   10651: # $context - either: priv, or res respectively for Authoring or Resource Space.
                   10652: # $docroot - Document root (i.e., /home/httpd/html
                   10653: # $toppath - Top level directory (i.e., /res/$dom/$uname or /priv/$dom/$uname
                   10654: # $relpath - Current path (relative to top level).
                   10655: # $dirhashref - reference to hash to populate with URLs of directories (Required)
                   10656: # $filehashref - reference to hash to populate with URLs of files (Optional)
                   10657: #
                   10658: # Returns: nothing
                   10659: #
                   10660: # Side Effects: populates $dirhashref, and $filehashref (if provided).
                   10661: #
                   10662: # Currently used by interface/londocs.pm to create linked select boxes for
                   10663: # directory and filename to import a Course "Author" resource into a course, and
                   10664: # also to create linked select boxes for Authoring Space and Directory to choose
                   10665: # save location for creation of a new "standard" problem from the Course Editor.
                   10666: #
                   10667: 
                   10668: sub recursedirs {
                   10669:     my ($is_home,$context,$docroot,$toppath,$relpath,$dirhashref,$filehashref) = @_;
                   10670:     return unless (ref($dirhashref) eq 'HASH');
                   10671:     my $currpath = $docroot.$toppath;
                   10672:     if ($relpath) {
                   10673:         $currpath .= "/$relpath";
                   10674:     }
                   10675:     my $savefile;
                   10676:     if (ref($filehashref)) {
                   10677:         $savefile = 1;
                   10678:     }
                   10679:     if ($is_home) {
                   10680:         if (opendir(my $dirh,$currpath)) {
                   10681:             foreach my $item (sort { lc($a) cmp lc($b) } grep(!/^\.+$/,readdir($dirh))) {
                   10682:                 next if ($item eq '');
                   10683:                 if (-d "$currpath/$item") {
                   10684:                     my $newpath;
                   10685:                     if ($relpath) {
                   10686:                         $newpath = "$relpath/$item";
                   10687:                     } else {
                   10688:                         $newpath = $item;
                   10689:                     }
                   10690:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   10691:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   10692:                 } elsif ($savefile) {
                   10693:                     if ($context eq 'priv') {
                   10694:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   10695:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   10696:                         }
                   10697:                     } else {
                   10698:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/) || ($item =~ /\.rights$/)) {
                   10699:                             $filehashref->{&Apache::lonlocal::js_escape($relpath)}{$item} = 1;
                   10700:                         }
                   10701:                     }
                   10702:                 }
                   10703:             }
                   10704:             closedir($dirh);
                   10705:         }
                   10706:     } else {
                   10707:         my ($dirlistref,$listerror) =
                   10708:             &dirlist($toppath.$relpath);
                   10709:         my @dir_lines;
                   10710:         my $dirptr=16384;
                   10711:         if (ref($dirlistref) eq 'ARRAY') {
                   10712:             foreach my $dir_line (sort
                   10713:                               {
                   10714:                                   my ($afile)=split('&',$a,2);
                   10715:                                   my ($bfile)=split('&',$b,2);
                   10716:                                   return (lc($afile) cmp lc($bfile));
                   10717:                               } (@{$dirlistref})) {
                   10718:                 my ($item,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef) =
                   10719:                     split(/\&/,$dir_line,16);
                   10720:                 $item =~ s/\s+$//;
                   10721:                 next if (($item =~ /^\.\.?$/) || ($obs));
                   10722:                 if ($dirptr&$testdir) {
                   10723:                     my $newpath;
                   10724:                     if ($relpath) {
                   10725:                         $newpath = "$relpath/$item";
                   10726:                     } else {
                   10727:                         $relpath = '/';
                   10728:                         $newpath = $item;
                   10729:                     }
                   10730:                     $dirhashref->{&Apache::lonlocal::js_escape($newpath)} = 1;
                   10731:                     &recursedirs($is_home,$context,$docroot,$toppath,$newpath,$dirhashref,$filehashref);
                   10732:                 } elsif ($savefile) {
                   10733:                     if ($context eq 'priv') {
                   10734:                         unless ($item =~ /\.(meta|save|log|bak|DS_Store)$/) {
                   10735:                             $filehashref->{$relpath}{$item} = 1;
                   10736:                         }
                   10737:                     } else {
                   10738:                         unless (($item =~ /\.meta$/) || ($item =~ /\.\d+\.\w+$/)) {
                   10739:                             $filehashref->{$relpath}{$item} = 1;
                   10740:                         }
                   10741:                     }
                   10742:                 }
                   10743:             }
                   10744:         }
                   10745:     }
                   10746:     return;
                   10747: }
                   10748: 
1.26      www      10749: # -------------------------------------------------------- Value of a Condition
                   10750: 
1.713     albertel 10751: # gets the value of a specific preevaluated condition
                   10752: #    stored in the string  $env{user.state.<cid>}
                   10753: # or looks up a condition reference in the bighash and if if hasn't
                   10754: # already been evaluated recurses into docondval to get the value of
                   10755: # the condition, then memoizing it to 
                   10756: #   $env{user.state.<cid>.<condition>}
1.40      www      10757: sub directcondval {
                   10758:     my $number=shift;
1.620     albertel 10759:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 10760: 	&Apache::lonuserstate::evalstate();
                   10761:     }
1.713     albertel 10762:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   10763: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   10764:     } elsif ($number =~ /^_/) {
                   10765: 	my $sub_condition;
                   10766: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   10767: 		&GDBM_READER(),0640)) {
                   10768: 	    $sub_condition=$bighash{'conditions'.$number};
                   10769: 	    untie(%bighash);
                   10770: 	}
                   10771: 	my $value = &docondval($sub_condition);
1.949     raeburn  10772: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 10773: 	return $value;
                   10774:     }
1.620     albertel 10775:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   10776:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      10777:     } else {
                   10778:        return 2;
                   10779:     }
                   10780: }
                   10781: 
1.713     albertel 10782: # get the collection of conditions for this resource
1.26      www      10783: sub condval {
                   10784:     my $condidx=shift;
1.54      www      10785:     my $allpathcond='';
1.713     albertel 10786:     foreach my $cond (split(/\|/,$condidx)) {
                   10787: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   10788: 	    $allpathcond.=
                   10789: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   10790: 	}
1.191     harris41 10791:     }
1.54      www      10792:     $allpathcond=~s/\|$//;
1.713     albertel 10793:     return &docondval($allpathcond);
                   10794: }
                   10795: 
                   10796: #evaluates an expression of conditions
                   10797: sub docondval {
                   10798:     my ($allpathcond) = @_;
                   10799:     my $result=0;
                   10800:     if ($env{'request.course.id'}
                   10801: 	&& defined($allpathcond)) {
                   10802: 	my $operand='|';
                   10803: 	my @stack;
                   10804: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   10805: 	    if ($chunk eq '(') {
                   10806: 		push @stack,($operand,$result);
                   10807: 	    } elsif ($chunk eq ')') {
                   10808: 		my $before=pop @stack;
                   10809: 		if (pop @stack eq '&') {
                   10810: 		    $result=$result>$before?$before:$result;
                   10811: 		} else {
                   10812: 		    $result=$result>$before?$result:$before;
                   10813: 		}
                   10814: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   10815: 		$operand=$chunk;
                   10816: 	    } else {
                   10817: 		my $new=directcondval($chunk);
                   10818: 		if ($operand eq '&') {
                   10819: 		    $result=$result>$new?$new:$result;
                   10820: 		} else {
                   10821: 		    $result=$result>$new?$result:$new;
                   10822: 		}
                   10823: 	    }
                   10824: 	}
1.26      www      10825:     }
                   10826:     return $result;
1.421     albertel 10827: }
                   10828: 
                   10829: # ---------------------------------------------------- Devalidate courseresdata
                   10830: 
                   10831: sub devalidatecourseresdata {
                   10832:     my ($coursenum,$coursedomain)=@_;
                   10833:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 10834:     &devalidate_cache_new('courseres',$hashid);
1.28      www      10835: }
                   10836: 
1.763     www      10837: 
1.200     www      10838: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     10839: #
                   10840: #  Parameters:
                   10841: #      $coursenum    - Number of the course.
                   10842: #      $coursedomain - Domain at which the course was created.
                   10843: #  Returns:
                   10844: #     A hash of the course parameters along (I think) with timestamps
                   10845: #     and version info.
1.877     foxr     10846: 
1.624     albertel 10847: sub get_courseresdata {
                   10848:     my ($coursenum,$coursedomain)=@_;
1.200     www      10849:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   10850:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 10851:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 10852:     my %dumpreply;
1.417     albertel 10853:     unless (defined($cached)) {
1.624     albertel 10854: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 10855: 	$result=\%dumpreply;
1.251     albertel 10856: 	my ($tmp) = keys(%dumpreply);
                   10857: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 10858: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 10859: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   10860: 	    return $tmp;
1.416     albertel 10861: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 10862: 	    $result=undef;
1.599     albertel 10863: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 10864: 	}
                   10865:     }
1.624     albertel 10866:     return $result;
                   10867: }
                   10868: 
1.633     albertel 10869: sub devalidateuserresdata {
                   10870:     my ($uname,$udom)=@_;
                   10871:     my $hashid="$udom:$uname";
                   10872:     &devalidate_cache_new('userres',$hashid);
                   10873: }
                   10874: 
1.624     albertel 10875: sub get_userresdata {
                   10876:     my ($uname,$udom)=@_;
                   10877:     #most student don\'t have any data set, check if there is some data
                   10878:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   10879: 
                   10880:     my $hashid="$udom:$uname";
                   10881:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   10882:     if (!defined($cached)) {
                   10883: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   10884: 	$result=\%resourcedata;
                   10885: 	&do_cache_new('userres',$hashid,$result,600);
                   10886:     }
                   10887:     my ($tmp)=keys(%$result);
                   10888:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   10889: 	return $result;
                   10890:     }
                   10891:     #error 2 occurs when the .db doesn't exist
                   10892:     if ($tmp!~/error: 2 /) {
1.1294    raeburn  10893:         if ((!defined($cached)) || ($tmp ne 'con_lost')) {
                   10894: 	    &logthis("<font color=\"blue\">WARNING:".
                   10895: 		     " Trying to get resource data for ".
                   10896: 		     $uname." at ".$udom.": ".
                   10897: 		     $tmp."</font>");
                   10898:         }
1.624     albertel 10899:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 10900: 	#&EXT_cache_set($udom,$uname);
                   10901: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 10902: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 10903:     }
                   10904:     return $tmp;
                   10905: }
1.879     foxr     10906: #----------------------------------------------- resdata - return resource data
                   10907: #  Purpose:
                   10908: #    Return resource data for either users or for a course.
                   10909: #  Parameters:
                   10910: #     $name      - Course/user name.
                   10911: #     $domain    - Name of the domain the user/course is registered on.
1.1311    raeburn  10912: #     $type      - Type of thing $name is (must be 'course' or 'user')
1.1301    raeburn  10913: #     $mapp      - decluttered URL of enclosing map  
                   10914: #     $recursed  - Ref to scalar -- set to 1, if nested maps have been recursed.
                   10915: #     $recurseup - Ref to array of map URLs, starting with map containing
                   10916: #                  $mapp up through hierarchy of nested maps to top level map.  
                   10917: #     $courseid  - CourseID (first part of param identifier).
                   10918: #     $modifier  - Middle part of param identifier.
                   10919: #     $what      - Last part of param identifier.
1.879     foxr     10920: #     @which     - Array of names of resources desired.
                   10921: #  Returns:
                   10922: #     The value of the first reasource in @which that is found in the
                   10923: #     resource hash.
                   10924: #  Exceptional Conditions:
                   10925: #     If the $type passed in is not valid (not the string 'course' or 
                   10926: #     'user', an undefined  reference is returned.
                   10927: #     If none of the resources are found, an undef is returned
1.624     albertel 10928: sub resdata {
1.1301    raeburn  10929:     my ($name,$domain,$type,$mapp,$recursed,$recurseup,$courseid,
                   10930:         $modifier,$what,@which)=@_;
1.624     albertel 10931:     my $result;
                   10932:     if ($type eq 'course') {
                   10933: 	$result=&get_courseresdata($name,$domain);
                   10934:     } elsif ($type eq 'user') {
                   10935: 	$result=&get_userresdata($name,$domain);
                   10936:     }
                   10937:     if (!ref($result)) { return $result; }    
1.251     albertel 10938:     foreach my $item (@which) {
1.1301    raeburn  10939:         if ($item->[1] eq 'course') {
                   10940:             if ((ref($recurseup) eq 'ARRAY') && (ref($recursed) eq 'SCALAR')) {
                   10941:                 unless ($$recursed) {
1.1302    raeburn  10942:                     @{$recurseup} = &get_map_hierarchy($mapp,$courseid);
1.1301    raeburn  10943:                     $$recursed = 1;
                   10944:                 }
                   10945:                 foreach my $item (@${recurseup}) {
                   10946:                     my $norecursechk=$courseid.$modifier.$item.'___(all).'.$what;
                   10947:                     last if (defined($result->{$norecursechk}));
                   10948:                     my $recursechk=$courseid.$modifier.$item.'___(rec).'.$what;
                   10949:                     if (defined($result->{$recursechk})) { return [$result->{$recursechk},'map']; }
                   10950:                 }
                   10951:             }
                   10952:         }
                   10953:         if (defined($result->{$item->[0]})) {
1.927     albertel 10954: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 10955: 	}
1.250     albertel 10956:     }
1.291     albertel 10957:     return undef;
1.200     www      10958: }
                   10959: 
1.1298    raeburn  10960: sub get_domain_ltitools {
                   10961:     my ($cdom) = @_;
                   10962:     my %ltitools;
                   10963:     my ($result,$cached)=&is_cached_new('ltitools',$cdom);
                   10964:     if (defined($cached)) {
                   10965:         if (ref($result) eq 'HASH') {
                   10966:             %ltitools = %{$result};
                   10967:         }
                   10968:     } else {
                   10969:         my %domconfig = &get_dom('configuration',['ltitools'],$cdom);
                   10970:         if (ref($domconfig{'ltitools'}) eq 'HASH') {
                   10971:             %ltitools = %{$domconfig{'ltitools'}};
1.1344    raeburn  10972:             my %encdomconfig = &get_dom('encconfig',['ltitools'],$cdom);
                   10973:             if (ref($encdomconfig{'ltitools'}) eq 'HASH') {
                   10974:                 foreach my $id (keys(%ltitools)) {
                   10975:                     if (ref($encdomconfig{'ltitools'}{$id}) eq 'HASH') {
                   10976:                         foreach my $item ('key','secret') {
                   10977:                             $ltitools{$id}{$item} = $encdomconfig{'ltitools'}{$id}{$item};
                   10978:                         }
                   10979:                     }
                   10980:                 }
                   10981:             }
1.1298    raeburn  10982:         }
                   10983:         my $cachetime = 24*60*60;
                   10984:         &do_cache_new('ltitools',$cdom,\%ltitools,$cachetime);
                   10985:     }
                   10986:     return %ltitools;
                   10987: }
                   10988: 
1.1236    raeburn  10989: sub get_numsuppfiles {
                   10990:     my ($cnum,$cdom,$ignorecache)=@_;
                   10991:     my $hashid=$cnum.':'.$cdom;
                   10992:     my ($suppcount,$cached);
                   10993:     unless ($ignorecache) {
                   10994:         ($suppcount,$cached) = &is_cached_new('suppcount',$hashid);
                   10995:     }
                   10996:     unless (defined($cached)) {
                   10997:         my $chome=&homeserver($cnum,$cdom);
                   10998:         unless ($chome eq 'no_host') {
                   10999:             ($suppcount,my $errors) = (0,0);
                   11000:             my $suppmap = 'supplemental.sequence';
                   11001:             ($suppcount,$errors) = 
                   11002:                 &Apache::loncommon::recurse_supplemental($cnum,$cdom,$suppmap,$suppcount,$errors);
                   11003:         }
                   11004:         &do_cache_new('suppcount',$hashid,$suppcount,600);
                   11005:     }
                   11006:     return $suppcount;
                   11007: }
                   11008: 
1.379     matthew  11009: #
                   11010: # EXT resource caching routines
                   11011: #
                   11012: 
1.1302    raeburn  11013: {
                   11014: # Cache (5 seconds) of map hierarchy for speedup of navmaps display
                   11015: #
                   11016: # The course for which we cache
                   11017: my $cachedmapkey='';
                   11018: # The cached recursive maps for this course
                   11019: my %cachedmaps=();
                   11020: # When this was last done
                   11021: my $cachedmaptime='';
                   11022: 
1.379     matthew  11023: sub clear_EXT_cache_status {
1.383     albertel 11024:     &delenv('cache.EXT.');
1.379     matthew  11025: }
                   11026: 
                   11027: sub EXT_cache_status {
                   11028:     my ($target_domain,$target_user) = @_;
1.383     albertel 11029:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 11030:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  11031:         # We know already the user has no data
                   11032:         return 1;
                   11033:     } else {
                   11034:         return 0;
                   11035:     }
                   11036: }
                   11037: 
                   11038: sub EXT_cache_set {
                   11039:     my ($target_domain,$target_user) = @_;
1.383     albertel 11040:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  11041:     #&appenv({$cachename => time});
1.379     matthew  11042: }
                   11043: 
1.28      www      11044: # --------------------------------------------------------- Value of a Variable
1.58      www      11045: sub EXT {
1.715     albertel 11046: 
1.1228    raeburn  11047:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68      www      11048:     unless ($varname) { return ''; }
1.218     albertel 11049:     #get real user name/domain, courseid and symb
                   11050:     my $courseid;
1.359     albertel 11051:     my $publicuser;
1.427     www      11052:     if ($symbparm) {
                   11053: 	$symbparm=&get_symb_from_alias($symbparm);
                   11054:     }
1.218     albertel 11055:     if (!($uname && $udom)) {
1.790     albertel 11056:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 11057:       if (!$symbparm) {	$symbparm=$cursymb; }
                   11058:     } else {
1.620     albertel 11059: 	$courseid=$env{'request.course.id'};
1.218     albertel 11060:     }
1.48      www      11061:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   11062:     my $rest;
1.320     albertel 11063:     if (defined($therest[0])) {
1.48      www      11064:        $rest=join('.',@therest);
                   11065:     } else {
                   11066:        $rest='';
                   11067:     }
1.320     albertel 11068: 
1.57      www      11069:     my $qualifierrest=$qualifier;
                   11070:     if ($rest) { $qualifierrest.='.'.$rest; }
                   11071:     my $spacequalifierrest=$space;
                   11072:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      11073:     if ($realm eq 'user') {
1.48      www      11074: # --------------------------------------------------------------- user.resource
                   11075: 	if ($space eq 'resource') {
1.651     albertel 11076: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   11077: 		  || defined($Apache::lonhomework::parsing_a_task))
                   11078: 		 &&
1.744     albertel 11079: 		 ($symbparm eq &symbread()) ) {	
                   11080: 		# if we are in the middle of processing the resource the
                   11081: 		# get the value we are planning on committing
                   11082:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   11083:                     return $Apache::lonhomework::results{$qualifierrest};
                   11084:                 } else {
                   11085:                     return $Apache::lonhomework::history{$qualifierrest};
                   11086:                 }
1.335     albertel 11087: 	    } else {
1.359     albertel 11088: 		my %restored;
1.620     albertel 11089: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 11090: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   11091: 		} else {
                   11092: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   11093: 		}
1.335     albertel 11094: 		return $restored{$qualifierrest};
                   11095: 	    }
1.48      www      11096: # ----------------------------------------------------------------- user.access
                   11097:         } elsif ($space eq 'access') {
1.218     albertel 11098: 	    # FIXME - not supporting calls for a specific user
1.48      www      11099:             return &allowed($qualifier,$rest);
                   11100: # ------------------------------------------ user.preferences, user.environment
                   11101:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 11102: 	    if (($uname eq $env{'user.name'}) &&
                   11103: 		($udom eq $env{'user.domain'})) {
                   11104: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 11105: 	    } else {
1.359     albertel 11106: 		my %returnhash;
                   11107: 		if (!$publicuser) {
                   11108: 		    %returnhash=&userenvironment($udom,$uname,
                   11109: 						 $qualifierrest);
                   11110: 		}
1.218     albertel 11111: 		return $returnhash{$qualifierrest};
                   11112: 	    }
1.48      www      11113: # ----------------------------------------------------------------- user.course
                   11114:         } elsif ($space eq 'course') {
1.218     albertel 11115: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 11116:             return $env{join('.',('request.course',$qualifier))};
1.48      www      11117: # ------------------------------------------------------------------- user.role
                   11118:         } elsif ($space eq 'role') {
1.218     albertel 11119: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 11120:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      11121:             if ($qualifier eq 'value') {
                   11122: 		return $role;
                   11123:             } elsif ($qualifier eq 'extent') {
                   11124:                 return $where;
                   11125:             }
                   11126: # ----------------------------------------------------------------- user.domain
                   11127:         } elsif ($space eq 'domain') {
1.218     albertel 11128:             return $udom;
1.48      www      11129: # ------------------------------------------------------------------- user.name
                   11130:         } elsif ($space eq 'name') {
1.218     albertel 11131:             return $uname;
1.48      www      11132: # ---------------------------------------------------- Any other user namespace
1.29      www      11133:         } else {
1.359     albertel 11134: 	    my %reply;
                   11135: 	    if (!$publicuser) {
                   11136: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   11137: 	    }
                   11138: 	    return $reply{$qualifierrest};
1.48      www      11139:         }
1.236     www      11140:     } elsif ($realm eq 'query') {
                   11141: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 11142:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   11143: 						[$spacequalifierrest]);
1.620     albertel 11144: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      11145:    } elsif ($realm eq 'request') {
1.48      www      11146: # ------------------------------------------------------------- request.browser
                   11147:         if ($space eq 'browser') {
1.1145    bisitz   11148:             return $env{'browser.'.$qualifier};
1.57      www      11149: # ------------------------------------------------------------ request.filename
                   11150:         } else {
1.620     albertel 11151:             return $env{'request.'.$spacequalifierrest};
1.29      www      11152:         }
1.28      www      11153:     } elsif ($realm eq 'course') {
1.48      www      11154: # ---------------------------------------------------------- course.description
1.620     albertel 11155:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      11156:     } elsif ($realm eq 'resource') {
1.165     www      11157: 
1.620     albertel 11158: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 11159: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   11160: 	}
1.693     albertel 11161: 
1.1232    raeburn  11162:         if ($qualifier eq '') {
                   11163: 	    if ($space eq 'title') {
                   11164: 	        if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   11165: 	        return &gettitle($symbparm);
                   11166: 	    }
1.693     albertel 11167: 	
1.1232    raeburn  11168: 	    if ($space eq 'map') {
                   11169: 	        my ($map) = &decode_symb($symbparm);
                   11170: 	        return &symbread($map);
                   11171: 	    }
                   11172:             if ($space eq 'maptitle') {
                   11173:                 my ($map) = &decode_symb($symbparm);
                   11174:                 return &gettitle($map);
                   11175:             }
                   11176: 	    if ($space eq 'filename') {
                   11177: 	        if ($symbparm) {
                   11178: 		    return &clutter((&decode_symb($symbparm))[2]);
                   11179: 	        }
                   11180: 	        return &hreflocation('',$env{'request.filename'});
1.905     albertel 11181: 	    }
1.1232    raeburn  11182: 
1.1233    raeburn  11183:             if ((defined($courseid)) && ($courseid eq $env{'request.course.id'}) && $symbparm) {
                   11184:                 if ($space eq 'visibleparts') {
                   11185:                     my $navmap = Apache::lonnavmaps::navmap->new();
                   11186:                     my $item;
                   11187:                     if (ref($navmap)) {
                   11188:                         my $res = $navmap->getBySymb($symbparm);
                   11189:                         my $parts = $res->parts();
                   11190:                         if (ref($parts) eq 'ARRAY') {
                   11191:                             $item = join(',',@{$parts});
                   11192:                         }
                   11193:                         undef($navmap);
1.1232    raeburn  11194:                     }
1.1233    raeburn  11195:                     return $item;
1.1232    raeburn  11196:                 }
                   11197:             }
                   11198:         }
1.693     albertel 11199: 
1.1301    raeburn  11200: 	my ($section, $group, @groups, @recurseup, $recursed);
                   11201: 	my ($courselevelm,$courseleveli,$courselevel,$mapp);
1.1228    raeburn  11202:         if (($courseid eq '') && ($cid)) {
                   11203:             $courseid = $cid;
                   11204:         }
                   11205: 	if (($symbparm && $courseid) && 
                   11206: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      11207: 
1.218     albertel 11208: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      11209: 
1.60      www      11210: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 11211: 	    my $symbp=$symbparm;
1.1301    raeburn  11212: 	    $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 11213: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
1.1301    raeburn  11214:             my $recurseparm=$mapp.'___(rec).'.$spacequalifierrest;
1.218     albertel 11215: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
1.620     albertel 11216: 	    if (($env{'user.name'} eq $uname) &&
                   11217: 		($env{'user.domain'} eq $udom)) {
                   11218: 		$section=$env{'request.course.sec'};
1.733     raeburn  11219:                 @groups = split(/:/,$env{'request.course.groups'});  
                   11220:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 11221: 	    } else {
1.539     albertel 11222: 		if (! defined($usection)) {
1.551     albertel 11223: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 11224: 		} else {
                   11225: 		    $section = $usection;
                   11226: 		}
1.733     raeburn  11227:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 11228: 	    }
                   11229: 
                   11230: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   11231: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
1.1301    raeburn  11232:             my $secleveli=$courseid.'.['.$section.'].'.$recurseparm;
1.218     albertel 11233: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   11234: 
1.593     albertel 11235: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 11236: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.1301    raeburn  11237:             $courseleveli=$courseid.'.'.$recurseparm;
1.593     albertel 11238: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      11239: 
1.60      www      11240: # ----------------------------------------------------------- first, check user
1.624     albertel 11241: 
1.1301    raeburn  11242: 	    my $userreply=&resdata($uname,$udom,'user',$mapp,\$recursed,
                   11243:                                    \@recurseup,$courseid,'.',$spacequalifierrest, 
1.927     albertel 11244: 				       ([$courselevelr,'resource'],
                   11245: 					[$courselevelm,'map'     ],
1.1301    raeburn  11246:                                         [$courseleveli,'map'     ],
1.927     albertel 11247: 					[$courselevel, 'course'  ]));
1.931     albertel 11248: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      11249: 
1.594     albertel 11250: # ------------------------------------------------ second, check some of course
1.684     raeburn  11251:             my $coursereply;
1.691     raeburn  11252:             if (@groups > 0) {
                   11253:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
1.1301    raeburn  11254:                                        $recurseparm,$mapparm,$spacequalifierrest,
                   11255:                                        $mapp,\$recursed,\@recurseup);
                   11256:                 if (defined($coursereply)) { return &get_reply($coursereply); } 
1.684     raeburn  11257:             }
1.96      www      11258: 
1.684     raeburn  11259: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 11260: 				  $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  11261: 				  'course',$mapp,\$recursed,\@recurseup,
                   11262:                                   $courseid,'.['.$section.'].',$spacequalifierrest,
1.927     albertel 11263: 				  ([$seclevelr,   'resource'],
                   11264: 				   [$seclevelm,   'map'     ],
1.1301    raeburn  11265:                                    [$secleveli,   'map'     ],
1.927     albertel 11266: 				   [$seclevel,    'course'  ],
                   11267: 				   [$courselevelr,'resource']));
                   11268: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      11269: 
1.60      www      11270: # ------------------------------------------------------ third, check map parms
1.218     albertel 11271: 	    my %parmhash=();
                   11272: 	    my $thisparm='';
                   11273: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 11274: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 11275: 		    &GDBM_READER(),0640)) {
1.218     albertel 11276: 		$thisparm=$parmhash{$symbparm};
                   11277: 		untie(%parmhash);
                   11278: 	    }
1.927     albertel 11279: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 11280: 	}
1.594     albertel 11281: # ------------------------------------------ fourth, look in resource metadata
1.1301    raeburn  11282:  
                   11283:         my $what = $spacequalifierrest;
                   11284: 	$what=~s/\./\_/;
1.282     albertel 11285: 	my $filename;
                   11286: 	if (!$symbparm) { $symbparm=&symbread(); }
                   11287: 	if ($symbparm) {
1.409     www      11288: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 11289: 	} else {
1.620     albertel 11290: 	    $filename=$env{'request.filename'};
1.282     albertel 11291: 	}
1.1301    raeburn  11292: 	my $metadata=&metadata($filename,$what);
1.927     albertel 11293: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.1301    raeburn  11294: 	$metadata=&metadata($filename,'parameter_'.$what);
1.927     albertel 11295: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      11296: 
1.1301    raeburn  11297: # ----------------------------------------------- fifth, look in rest of course
1.593     albertel 11298: 	if ($symbparm && defined($courseid) && 
1.620     albertel 11299: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 11300: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   11301: 				     $env{'course.'.$courseid.'.domain'},
1.1301    raeburn  11302: 				     'course',$mapp,\$recursed,\@recurseup,
                   11303:                                      $courseid,'.',$spacequalifierrest,
1.927     albertel 11304: 				     ([$courselevelm,'map'   ],
1.1301    raeburn  11305:                                       [$courseleveli,'map'   ],
1.927     albertel 11306: 				      [$courselevel, 'course']));
                   11307: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 11308: 	}
1.145     www      11309: # ------------------------------------------------------------------ Cascade up
1.218     albertel 11310: 	unless ($space eq '0') {
1.336     albertel 11311: 	    my @parts=split(/_/,$space);
                   11312: 	    my $id=pop(@parts);
                   11313: 	    my $part=join('_',@parts);
                   11314: 	    if ($part eq '') { $part='0'; }
1.927     albertel 11315: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 11316: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  11317: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 11318: 	}
1.395     albertel 11319: 	if ($recurse) { return undef; }
                   11320: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 11321: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      11322: # ---------------------------------------------------- Any other user namespace
                   11323:     } elsif ($realm eq 'environment') {
                   11324: # ----------------------------------------------------------------- environment
1.620     albertel 11325: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   11326: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 11327: 	} else {
1.770     albertel 11328: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   11329: 		return '';
                   11330: 	    }
1.219     albertel 11331: 	    my %returnhash=&userenvironment($udom,$uname,
                   11332: 					    $spacequalifierrest);
                   11333: 	    return $returnhash{$spacequalifierrest};
                   11334: 	}
1.28      www      11335:     } elsif ($realm eq 'system') {
1.48      www      11336: # ----------------------------------------------------------------- system.time
                   11337: 	if ($space eq 'time') {
                   11338: 	    return time;
                   11339:         }
1.696     albertel 11340:     } elsif ($realm eq 'server') {
                   11341: # ----------------------------------------------------------------- system.time
                   11342: 	if ($space eq 'name') {
                   11343: 	    return $ENV{'SERVER_NAME'};
                   11344:         }
1.28      www      11345:     }
1.48      www      11346:     return '';
1.61      www      11347: }
                   11348: 
1.927     albertel 11349: sub get_reply {
                   11350:     my ($reply_value) = @_;
1.940     raeburn  11351:     if (ref($reply_value) eq 'ARRAY') {
                   11352:         if (wantarray) {
                   11353: 	    return @$reply_value;
                   11354:         }
                   11355:         return $reply_value->[0];
                   11356:     } else {
                   11357:         return $reply_value;
1.927     albertel 11358:     }
                   11359: }
                   11360: 
1.691     raeburn  11361: sub check_group_parms {
1.1301    raeburn  11362:     my ($courseid,$groups,$symbparm,$recurseparm,$mapparm,$what,$mapp,
                   11363:         $recursed,$recurseupref) = @_;
                   11364:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$recurseparm,'map'],
                   11365:                   [$what,'course']);
                   11366:     my $coursereply;
1.691     raeburn  11367:     foreach my $group (@{$groups}) {
1.1301    raeburn  11368:         my @groupitems = ();
1.691     raeburn  11369:         foreach my $level (@levels) {
1.927     albertel 11370:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   11371:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  11372:         }
1.1301    raeburn  11373:         my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   11374:                                    $env{'course.'.$courseid.'.domain'},
                   11375:                                    'course',$mapp,$recursed,$recurseupref,
                   11376:                                    $courseid,'.['.$group.'].',$what,
                   11377:                                    @groupitems);
                   11378:         last if (defined($coursereply));
1.691     raeburn  11379:     }
                   11380:     return $coursereply;
                   11381: }
                   11382: 
1.1301    raeburn  11383: sub get_map_hierarchy {
1.1302    raeburn  11384:     my ($mapname,$courseid) = @_;
                   11385:     my @recurseup = ();
1.1301    raeburn  11386:     if ($mapname) {
1.1302    raeburn  11387:         if (($cachedmapkey eq $courseid) &&
                   11388:             (abs($cachedmaptime-time)<5)) {
                   11389:             if (ref($cachedmaps{$mapname}) eq 'ARRAY') {
                   11390:                 return @{$cachedmaps{$mapname}};
                   11391:             }
                   11392:         }
1.1301    raeburn  11393:         my $navmap = Apache::lonnavmaps::navmap->new();
                   11394:         if (ref($navmap)) {
                   11395:             @recurseup = $navmap->recurseup_maps($mapname);
                   11396:             undef($navmap);
1.1302    raeburn  11397:             $cachedmaps{$mapname} = \@recurseup;
                   11398:             $cachedmaptime=time;
                   11399:             $cachedmapkey=$courseid;
1.1301    raeburn  11400:         }
                   11401:     }
                   11402:     return @recurseup;
                   11403: }
                   11404: 
1.1302    raeburn  11405: }
                   11406: 
1.691     raeburn  11407: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  11408:     my ($courseid,@groups) = @_;
                   11409:     @groups = sort(@groups);
1.691     raeburn  11410:     return @groups;
                   11411: }
                   11412: 
1.395     albertel 11413: sub packages_tab_default {
                   11414:     my ($uri,$varname)=@_;
                   11415:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 11416: 
                   11417:     my (@extension,@specifics,$do_default);
                   11418:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 11419: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 11420: 	if ($pack_type eq 'default') {
                   11421: 	    $do_default=1;
                   11422: 	} elsif ($pack_type eq 'extension') {
                   11423: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 11424: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 11425: 	    # only look at packages defaults for packages that this id is
1.738     albertel 11426: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   11427: 	}
                   11428:     }
                   11429:     # first look for a package that matches the requested part id
                   11430:     foreach my $package (@specifics) {
                   11431: 	my (undef,$pack_type,$pack_part)=@{$package};
                   11432: 	next if ($pack_part ne $part);
                   11433: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   11434: 	    return $packagetab{"$pack_type&$name&default"};
                   11435: 	}
                   11436:     }
                   11437:     # look for any possible matching non extension_ package
                   11438:     foreach my $package (@specifics) {
                   11439: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 11440: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   11441: 	    return $packagetab{"$pack_type&$name&default"};
                   11442: 	}
1.585     albertel 11443: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 11444: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   11445: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 11446: 	}
                   11447:     }
1.738     albertel 11448:     # look for any posible extension_ match
                   11449:     foreach my $package (@extension) {
                   11450: 	my ($package,$pack_type)=@{$package};
                   11451: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   11452: 	    return $packagetab{"$pack_type&$name&default"};
                   11453: 	}
                   11454: 	if (defined($packagetab{$package."&$name&default"})) {
                   11455: 	    return $packagetab{$package."&$name&default"};
                   11456: 	}
                   11457:     }
                   11458:     # look for a global default setting
                   11459:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   11460: 	return $packagetab{"default&$name&default"};
                   11461:     }
1.395     albertel 11462:     return undef;
                   11463: }
                   11464: 
1.334     albertel 11465: sub add_prefix_and_part {
                   11466:     my ($prefix,$part)=@_;
                   11467:     my $keyroot;
                   11468:     if (defined($prefix) && $prefix !~ /^__/) {
                   11469: 	# prefix that has a part already
                   11470: 	$keyroot=$prefix;
                   11471:     } elsif (defined($prefix)) {
                   11472: 	# prefix that is missing a part
                   11473: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   11474:     } else {
                   11475: 	# no prefix at all
                   11476: 	if (defined($part)) { $keyroot='_'.$part; }
                   11477:     }
                   11478:     return $keyroot;
                   11479: }
                   11480: 
1.71      www      11481: # ---------------------------------------------------------------- Get metadata
                   11482: 
1.599     albertel 11483: my %metaentry;
1.1070    www      11484: my %importedpartids;
1.71      www      11485: sub metadata {
1.176     www      11486:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      11487:     $uri=&declutter($uri);
1.288     albertel 11488:     # if it is a non metadata possible uri return quickly
1.529     albertel 11489:     if (($uri eq '') || 
                   11490: 	(($uri =~ m|^/*adm/|) && 
1.1343    raeburn  11491: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard|ext\.tool)$})) ||
1.1108    raeburn  11492:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 11493: 	return undef;
                   11494:     }
1.1261    raeburn  11495:     if (($uri =~ /^priv/ || $uri=~m{^home/httpd/html/priv}) 
1.924     albertel 11496: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 11497: 	return undef;
1.288     albertel 11498:     }
1.73      www      11499:     my $filename=$uri;
                   11500:     $uri=~s/\.meta$//;
1.172     www      11501: #
                   11502: # Is the metadata already cached?
1.177     www      11503: # Look at timestamp of caching
1.172     www      11504: # Everything is cached by the main uri, libraries are never directly cached
                   11505: #
1.428     albertel 11506:     if (!defined($liburi)) {
1.599     albertel 11507: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 11508: 	if (defined($cached)) { return $result->{':'.$what}; }
                   11509:     }
                   11510:     {
1.1069    www      11511: # Imported parts would go here
1.1070    www      11512:         my %importedids=();
                   11513:         my @origfileimportpartids=();
1.1069    www      11514:         my $importedparts=0;
1.172     www      11515: #
                   11516: # Is this a recursive call for a library?
                   11517: #
1.599     albertel 11518: #	if (! exists($metacache{$uri})) {
                   11519: #	    $metacache{$uri}={};
                   11520: #	}
1.924     albertel 11521: 	my $cachetime = 60*60;
1.171     www      11522:         if ($liburi) {
                   11523: 	    $liburi=&declutter($liburi);
                   11524:             $filename=$liburi;
1.401     bowersj2 11525:         } else {
1.599     albertel 11526: 	    &devalidate_cache_new('meta',$uri);
                   11527: 	    undef(%metaentry);
1.401     bowersj2 11528: 	}
1.140     www      11529:         my %metathesekeys=();
1.73      www      11530:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 11531: 	my $metastring;
1.1140    www      11532: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 11533: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 11534: 	    $metastring = 
1.929     albertel 11535: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 11536: 					  ('grade_target' => 'meta'));
                   11537: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  11538: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   11539:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 11540: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 11541: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 11542: 	    $metastring=&getfile($file);
1.489     albertel 11543: 	}
1.208     albertel 11544:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      11545:         my $token;
1.140     www      11546:         undef %metathesekeys;
1.71      www      11547:         while ($token=$parser->get_token) {
1.339     albertel 11548: 	    if ($token->[0] eq 'S') {
                   11549: 		if (defined($token->[2]->{'package'})) {
1.172     www      11550: #
                   11551: # This is a package - get package info
                   11552: #
1.339     albertel 11553: 		    my $package=$token->[2]->{'package'};
                   11554: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   11555: 		    if (defined($token->[2]->{'id'})) { 
                   11556: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   11557: 		    }
1.599     albertel 11558: 		    if ($metaentry{':packages'}) {
                   11559: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 11560: 		    } else {
1.599     albertel 11561: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 11562: 		    }
1.736     albertel 11563: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 11564: 			my $part=$keyroot;
                   11565: 			$part=~s/^\_//;
1.736     albertel 11566: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   11567: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   11568: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 11569: 			    # ignore package.tab specified default values
                   11570:                             # here &package_tab_default() will fetch those
                   11571: 			    if ($subp eq 'default') { next; }
1.736     albertel 11572: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 11573: 			    my $unikey;
                   11574: 			    if ($pack =~ /_0$/) {
                   11575: 				$unikey='parameter_0_'.$name;
                   11576: 				$part=0;
                   11577: 			    } else {
                   11578: 				$unikey='parameter'.$keyroot.'_'.$name;
                   11579: 			    }
1.339     albertel 11580: 			    if ($subp eq 'display') {
                   11581: 				$value.=' [Part: '.$part.']';
                   11582: 			    }
1.599     albertel 11583: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 11584: 			    $metathesekeys{$unikey}=1;
1.599     albertel 11585: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   11586: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 11587: 			    }
1.599     albertel 11588: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   11589: 				$metaentry{':'.$unikey}=
                   11590: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 11591: 			    }
1.339     albertel 11592: 			}
                   11593: 		    }
                   11594: 		} else {
1.172     www      11595: #
                   11596: # This is not a package - some other kind of start tag
1.339     albertel 11597: #
                   11598: 		    my $entry=$token->[1];
1.1068    www      11599: 		    my $unikey='';
1.175     www      11600: 
1.339     albertel 11601: 		    if ($entry eq 'import') {
1.175     www      11602: #
                   11603: # Importing a library here
1.339     albertel 11604: #
1.1067    www      11605:                         my $location=$parser->get_text('/import');
                   11606:                         my $dir=$filename;
                   11607:                         $dir=~s|[^/]*$||;
                   11608:                         $location=&filelocation($dir,$location);
1.1069    www      11609:                        
1.1068    www      11610:                         my $importmode=$token->[2]->{'importmode'};
                   11611:                         if ($importmode eq 'problem') {
1.1069    www      11612: # Import as problem/response
1.1068    www      11613:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   11614:                         } elsif ($importmode eq 'part') {
                   11615: # Import as part(s)
1.1069    www      11616:                            $importedparts=1;
                   11617: # We need to get the original file and the imported file to get the part order correct
                   11618: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
                   11619: # Load and inspect original file
1.1070    www      11620:                            if ($#origfileimportpartids<0) {
                   11621:                               undef(%importedpartids);
                   11622:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   11623:                               my $origfile=&getfile($origfilelocation);
                   11624:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   11625:                            }
                   11626: 
1.1069    www      11627: # Load and inspect imported file
                   11628:                            my $impfile=&getfile($location);
                   11629:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   11630:                            if ($#impfilepartids>=0) {
                   11631: # This problem had parts
1.1070    www      11632:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      11633:                            } else {
                   11634: # Importing by turning a single problem into a problem part
                   11635: # It gets the import-tags ID as part-ID
                   11636:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      11637:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      11638:                            }
1.1068    www      11639:                         } else {
                   11640: # Normal import
                   11641:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   11642:                            if (defined($token->[2]->{'id'})) {
                   11643:                               $unikey.='_'.$token->[2]->{'id'};
                   11644:                            }
1.1067    www      11645:                         }
                   11646: 
1.339     albertel 11647: 			if ($depthcount<20) {
1.736     albertel 11648: 			    my $metadata = 
                   11649: 				&metadata($uri,'keys', $location,$unikey,
                   11650: 					  $depthcount+1);
                   11651: 			    foreach my $meta (split(',',$metadata)) {
                   11652: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   11653: 				$metathesekeys{$meta}=1;
1.339     albertel 11654: 			    }
1.1068    www      11655: 			
                   11656:                         }
1.1067    www      11657: 		    } else {
                   11658: #
                   11659: # Not importing, some other kind of non-package, non-library start tag
                   11660: # 
                   11661:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   11662:                         if (defined($token->[2]->{'id'})) {
                   11663:                             $unikey.='_'.$token->[2]->{'id'};
                   11664:                         }
1.339     albertel 11665: 			if (defined($token->[2]->{'name'})) { 
                   11666: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   11667: 			}
                   11668: 			$metathesekeys{$unikey}=1;
1.736     albertel 11669: 			foreach my $param (@{$token->[3]}) {
                   11670: 			    $metaentry{':'.$unikey.'.'.$param} =
                   11671: 				$token->[2]->{$param};
1.339     albertel 11672: 			}
                   11673: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 11674: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 11675: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   11676: 		 # only ws inside the tag, and not in default, so use default
                   11677: 		 # as value
1.599     albertel 11678: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 11679: 			} elsif ( $internaltext =~ /\S/ ) {
                   11680: 		  # something interesting inside the tag
                   11681: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 11682: 			} else {
1.908     albertel 11683: 		  # no interesting values, don't set a default
1.339     albertel 11684: 			}
1.172     www      11685: # end of not-a-package not-a-library import
1.339     albertel 11686: 		    }
1.172     www      11687: # end of not-a-package start tag
1.339     albertel 11688: 		}
1.172     www      11689: # the next is the end of "start tag"
1.339     albertel 11690: 	    }
                   11691: 	}
1.483     albertel 11692: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 11693: 	$extension = lc($extension);
                   11694: 	if ($extension eq 'htm') { $extension='html'; }
                   11695: 
1.737     albertel 11696: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 11697: 	    #no specific packages #how's our extension
                   11698: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 11699: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 11700: 					 \%metathesekeys);
                   11701: 	}
1.883     albertel 11702: 
                   11703: 	if (!exists($metaentry{':packages'})
                   11704: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 11705: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 11706: 		#no specific packages well let's get default then
                   11707: 		if ($key!~/^default&/) { next; }
1.488     albertel 11708: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 11709: 					     \%metathesekeys);
                   11710: 	    }
                   11711: 	}
1.338     www      11712: # are there custom rights to evaluate
1.599     albertel 11713: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 11714: 
1.338     www      11715:     #
                   11716:     # Importing a rights file here
1.339     albertel 11717:     #
                   11718: 	    unless ($depthcount) {
1.599     albertel 11719: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 11720: 		my $dir=$filename;
                   11721: 		$dir=~s|[^/]*$||;
                   11722: 		$location=&filelocation($dir,$location);
1.736     albertel 11723: 		my $rights_metadata =
                   11724: 		    &metadata($uri,'keys',$location,'_rights',
                   11725: 			      $depthcount+1);
                   11726: 		foreach my $rights (split(',',$rights_metadata)) {
                   11727: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   11728: 		    $metathesekeys{$rights}=1;
1.339     albertel 11729: 		}
                   11730: 	    }
                   11731: 	}
1.737     albertel 11732: 	# uniqifiy package listing
                   11733: 	my %seen;
                   11734: 	my @uniq_packages =
                   11735: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   11736: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   11737: 
1.1070    www      11738:         if ($importedparts) {
                   11739: # We had imported parts and need to rebuild partorder
                   11740:            $metaentry{':partorder'}='';
                   11741:            $metathesekeys{'partorder'}=1;
                   11742:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
                   11743:                if ($origfileimportpartids[$index] eq 'part') {
                   11744: # original part, part of the problem
                   11745:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
                   11746:                } else {
                   11747: # we have imported parts at this position
                   11748:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
                   11749:                }
                   11750:            }
                   11751:            $metaentry{':partorder'}=~s/^\,//;
                   11752:         }
                   11753: 
1.737     albertel 11754: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 11755: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
1.1274    raeburn  11756: 	$metaentry{':allpossiblekeys'}=join(',',keys(%metathesekeys));
1.924     albertel 11757: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      11758: # this is the end of "was not already recently cached
1.71      www      11759:     }
1.599     albertel 11760:     return $metaentry{':'.$what};
1.261     albertel 11761: }
                   11762: 
1.488     albertel 11763: sub metadata_create_package_def {
1.483     albertel 11764:     my ($uri,$key,$package,$metathesekeys)=@_;
                   11765:     my ($pack,$name,$subp)=split(/\&/,$key);
                   11766:     if ($subp eq 'default') { next; }
                   11767:     
1.599     albertel 11768:     if (defined($metaentry{':packages'})) {
                   11769: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 11770:     } else {
1.599     albertel 11771: 	$metaentry{':packages'}=$package;
1.483     albertel 11772:     }
                   11773:     my $value=$packagetab{$key};
                   11774:     my $unikey;
                   11775:     $unikey='parameter_0_'.$name;
1.599     albertel 11776:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 11777:     $$metathesekeys{$unikey}=1;
1.599     albertel 11778:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   11779: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 11780:     }
1.599     albertel 11781:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   11782: 	$metaentry{':'.$unikey}=
                   11783: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 11784:     }
                   11785: }
                   11786: 
1.261     albertel 11787: sub metadata_generate_part0 {
                   11788:     my ($metadata,$metacache,$uri) = @_;
                   11789:     my %allnames;
1.737     albertel 11790:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 11791: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 11792: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   11793: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 11794: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 11795: 	    $allnames{$name}=$part;
                   11796: 	  }
                   11797: 	}
                   11798:     }
                   11799:     foreach my $name (keys(%allnames)) {
                   11800:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 11801:       my $key=":parameter_0_$name";
1.261     albertel 11802:       $$metacache{"$key.part"}='0';
                   11803:       $$metacache{"$key.name"}=$name;
1.428     albertel 11804:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 11805: 					   $allnames{$name}.'_'.$name.
                   11806: 					   '.type'};
1.428     albertel 11807:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 11808: 			     '.display'};
1.644     www      11809:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 11810:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 11811:       $$metacache{"$key.display"}=$olddis;
                   11812:     }
1.71      www      11813: }
                   11814: 
1.764     albertel 11815: # ------------------------------------------------------ Devalidate title cache
                   11816: 
                   11817: sub devalidate_title_cache {
                   11818:     my ($url)=@_;
                   11819:     if (!$env{'request.course.id'}) { return; }
                   11820:     my $symb=&symbread($url);
                   11821:     if (!$symb) { return; }
                   11822:     my $key=$env{'request.course.id'}."\0".$symb;
                   11823:     &devalidate_cache_new('title',$key);
                   11824: }
                   11825: 
1.1014    droeschl 11826: # ------------------------------------------------- Get the title of a course
                   11827: 
                   11828: sub current_course_title {
                   11829:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   11830: }
1.301     www      11831: # ------------------------------------------------- Get the title of a resource
                   11832: 
                   11833: sub gettitle {
                   11834:     my $urlsymb=shift;
                   11835:     my $symb=&symbread($urlsymb);
1.534     albertel 11836:     if ($symb) {
1.620     albertel 11837: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 11838: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 11839: 	if (defined($cached)) { 
                   11840: 	    return $result;
                   11841: 	}
1.534     albertel 11842: 	my ($map,$resid,$url)=&decode_symb($symb);
                   11843: 	my $title='';
1.907     albertel 11844: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   11845: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   11846: 	} else {
                   11847: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   11848: 		    &GDBM_READER(),0640)) {
                   11849: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   11850: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   11851: 		untie(%bighash);
                   11852: 	    }
1.534     albertel 11853: 	}
                   11854: 	$title=~s/\&colon\;/\:/gs;
                   11855: 	if ($title) {
1.1159    www      11856: # Remember both $symb and $title for dynamic metadata
                   11857:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      11858:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      11859: # Cache this title and then return it
1.599     albertel 11860: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 11861: 	}
                   11862: 	$urlsymb=$url;
                   11863:     }
                   11864:     my $title=&metadata($urlsymb,'title');
                   11865:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   11866:     return $title;
1.301     www      11867: }
1.613     albertel 11868: 
1.614     albertel 11869: sub get_slot {
                   11870:     my ($which,$cnum,$cdom)=@_;
                   11871:     if (!$cnum || !$cdom) {
1.790     albertel 11872: 	(undef,my $courseid)=&whichuser();
1.620     albertel 11873: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   11874: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 11875:     }
1.703     albertel 11876:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   11877:     my %slotinfo;
                   11878:     if (exists($remembered{$key})) {
                   11879: 	$slotinfo{$which} = $remembered{$key};
                   11880:     } else {
                   11881: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   11882: 	&Apache::lonhomework::showhash(%slotinfo);
                   11883: 	my ($tmp)=keys(%slotinfo);
                   11884: 	if ($tmp=~/^error:/) { return (); }
                   11885: 	$remembered{$key} = $slotinfo{$which};
                   11886:     }
1.616     albertel 11887:     if (ref($slotinfo{$which}) eq 'HASH') {
                   11888: 	return %{$slotinfo{$which}};
                   11889:     }
                   11890:     return $slotinfo{$which};
1.614     albertel 11891: }
1.1150    raeburn  11892: 
                   11893: sub get_reservable_slots {
                   11894:     my ($cnum,$cdom,$uname,$udom) = @_;
                   11895:     my $now = time;
                   11896:     my $reservable_info;
                   11897:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   11898:     if (exists($remembered{$key})) {
                   11899:         $reservable_info = $remembered{$key};
                   11900:     } else {
                   11901:         my %resv;
                   11902:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   11903:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   11904:         $reservable_info = \%resv;
                   11905:         $remembered{$key} = $reservable_info;
                   11906:     }
                   11907:     return $reservable_info;
                   11908: }
                   11909: 
                   11910: sub get_course_slots {
                   11911:     my ($cnum,$cdom) = @_;
                   11912:     my $hashid=$cnum.':'.$cdom;
                   11913:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   11914:     if (defined($cached)) {
                   11915:         if (ref($result) eq 'HASH') {
                   11916:             return %{$result};
                   11917:         }
                   11918:     } else {
                   11919:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   11920:         my ($tmp) = keys(%slots);
                   11921:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  11922:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  11923:             return %slots;
                   11924:         }
                   11925:     }
                   11926:     return;
                   11927: }
                   11928: 
                   11929: sub devalidate_slots_cache {
                   11930:     my ($cnum,$cdom)=@_;
                   11931:     my $hashid=$cnum.':'.$cdom;
                   11932:     &devalidate_cache_new('allslots',$hashid);
                   11933: }
                   11934: 
1.1181    raeburn  11935: sub get_coursechange {
                   11936:     my ($cdom,$cnum) = @_;
                   11937:     if ($cdom eq '' || $cnum eq '') {
                   11938:         return unless ($env{'request.course.id'});
                   11939:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   11940:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   11941:     }
                   11942:     my $hashid=$cdom.'_'.$cnum;
                   11943:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   11944:     if ((defined($cached)) && ($change ne '')) {
                   11945:         return $change;
                   11946:     } else {
                   11947:         my %crshash;
                   11948:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   11949:         if ($crshash{'internal.contentchange'} eq '') {
                   11950:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   11951:             if ($change eq '') {
                   11952:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   11953:                 $change = $crshash{'internal.created'};
                   11954:             }
                   11955:         } else {
                   11956:             $change = $crshash{'internal.contentchange'};
                   11957:         }
                   11958:         my $cachetime = 600;
                   11959:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   11960:     }
                   11961:     return $change;
                   11962: }
                   11963: 
                   11964: sub devalidate_coursechange_cache {
                   11965:     my ($cnum,$cdom)=@_;
                   11966:     my $hashid=$cnum.':'.$cdom;
                   11967:     &devalidate_cache_new('crschange',$hashid);
                   11968: }
                   11969: 
1.31      www      11970: # ------------------------------------------------- Update symbolic store links
                   11971: 
                   11972: sub symblist {
                   11973:     my ($mapname,%newhash)=@_;
1.438     www      11974:     $mapname=&deversion(&declutter($mapname));
1.31      www      11975:     my %hash;
1.620     albertel 11976:     if (($env{'request.course.fn'}) && (%newhash)) {
                   11977:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 11978:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  11979: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 11980: 		next if ($url eq 'last_known'
                   11981: 			 && $env{'form.no_update_last_known'});
                   11982: 		$hash{declutter($url)}=&encode_symb($mapname,
                   11983: 						    $newhash{$url}->[1],
                   11984: 						    $newhash{$url}->[0]);
1.191     harris41 11985:             }
1.31      www      11986:             if (untie(%hash)) {
                   11987: 		return 'ok';
                   11988:             }
                   11989:         }
                   11990:     }
                   11991:     return 'error';
1.212     www      11992: }
                   11993: 
                   11994: # --------------------------------------------------------------- Verify a symb
                   11995: 
                   11996: sub symbverify {
1.1190    raeburn  11997:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      11998:     my $thisfn=$thisurl;
1.439     www      11999:     $thisfn=&declutter($thisfn);
1.215     www      12000: # direct jump to resource in page or to a sequence - will construct own symbs
                   12001:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   12002: # check URL part
1.409     www      12003:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      12004: 
1.431     www      12005:     unless ($url eq $thisfn) { return 0; }
1.213     www      12006: 
1.216     www      12007:     $symb=&symbclean($symb);
1.510     www      12008:     $thisurl=&deversion($thisurl);
1.439     www      12009:     $thisfn=&deversion($thisfn);
1.213     www      12010: 
                   12011:     my %bighash;
                   12012:     my $okay=0;
1.431     www      12013: 
1.620     albertel 12014:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 12015:                             &GDBM_READER(),0640)) {
1.1204    raeburn  12016:         my $noclutter;
1.1032    raeburn  12017:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   12018:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  12019:             if ($map =~ m{^uploaded/.+\.page$}) {
                   12020:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   12021:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   12022:                 $noclutter = 1;
                   12023:             }
                   12024:         }
                   12025:         my $ids;
                   12026:         if ($noclutter) {
                   12027:             $ids=$bighash{'ids_'.$thisurl};
                   12028:         } else {
                   12029:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  12030:         }
1.1102    raeburn  12031:         unless ($ids) {
                   12032:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   12033:             $ids=$bighash{$idkey};
1.216     www      12034:         }
                   12035:         if ($ids) {
                   12036: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  12037:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  12038:                 $symb =~ s/\?.+$//;
1.1202    raeburn  12039:             }
1.800     albertel 12040: 	    foreach my $id (split(/\,/,$ids)) {
                   12041: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      12042:                if (
                   12043:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  12044:    eq $symb) {
                   12045:                    if (ref($encstate)) {
                   12046:                        $$encstate = $bighash{'encrypted_'.$id};
                   12047:                    }
1.620     albertel 12048: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  12049: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   12050:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  12051: 		       $okay=1;
1.1202    raeburn  12052:                        last;
1.582     albertel 12053: 		   }
                   12054: 	       }
1.216     www      12055: 	   }
                   12056:         }
1.213     www      12057: 	untie(%bighash);
                   12058:     }
                   12059:     return $okay;
1.31      www      12060: }
                   12061: 
1.210     www      12062: # --------------------------------------------------------------- Clean-up symb
                   12063: 
                   12064: sub symbclean {
                   12065:     my $symb=shift;
1.568     albertel 12066:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      12067: # remove version from map
                   12068:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      12069: 
1.210     www      12070: # remove version from URL
                   12071:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      12072: 
1.507     www      12073: # remove wrapper
                   12074: 
1.510     www      12075:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 12076:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      12077:     return $symb;
1.409     www      12078: }
                   12079: 
                   12080: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 12081: 
                   12082: sub encode_symb {
                   12083:     my ($map,$resid,$url)=@_;
                   12084:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   12085: }
1.409     www      12086: 
                   12087: sub decode_symb {
1.568     albertel 12088:     my $symb=shift;
                   12089:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   12090:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      12091:     return (&fixversion($map),$resid,&fixversion($url));
                   12092: }
                   12093: 
                   12094: sub fixversion {
                   12095:     my $fn=shift;
1.609     banghart 12096:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      12097:     my %bighash;
                   12098:     my $uri=&clutter($fn);
1.620     albertel 12099:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      12100: # is this cached?
1.599     albertel 12101:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      12102:     if (defined($cached)) { return $result; }
                   12103: # unfortunately not cached, or expired
1.620     albertel 12104:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      12105: 	    &GDBM_READER(),0640)) {
                   12106:  	if ($bighash{'version_'.$uri}) {
                   12107:  	    my $version=$bighash{'version_'.$uri};
1.444     www      12108:  	    unless (($version eq 'mostrecent') || 
                   12109: 		    ($version==&getversion($uri))) {
1.440     www      12110:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   12111:  	    }
                   12112:  	}
                   12113:  	untie %bighash;
1.413     www      12114:     }
1.599     albertel 12115:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      12116: }
                   12117: 
                   12118: sub deversion {
                   12119:     my $url=shift;
                   12120:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   12121:     return $url;
1.210     www      12122: }
                   12123: 
1.31      www      12124: # ------------------------------------------------------ Return symb list entry
                   12125: 
                   12126: sub symbread {
1.1282    raeburn  12127:     my ($thisfn,$donotrecurse,$ignorecachednull,$checkforblock,$possibles)=@_;
1.1265    raeburn  12128:     my $cache_str='request.symbread.cached.'.$thisfn;
1.1282    raeburn  12129:     if (defined($env{$cache_str})) {
                   12130:         if ($ignorecachednull) {
                   12131:             return $env{$cache_str} unless ($env{$cache_str} eq '');
                   12132:         } else {
                   12133:             return $env{$cache_str};
                   12134:         }
                   12135:     }
1.242     www      12136: # no filename provided? try from environment
1.1265    raeburn  12137:     unless ($thisfn) {
1.620     albertel 12138:         if ($env{'request.symb'}) {
                   12139: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 12140: 	}
1.620     albertel 12141: 	$thisfn=$env{'request.filename'};
1.44      www      12142:     }
1.569     albertel 12143:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      12144: # is that filename actually a symb? Verify, clean, and return
                   12145:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 12146: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 12147: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 12148: 	}
1.242     www      12149:     }
1.44      www      12150:     $thisfn=declutter($thisfn);
1.31      www      12151:     my %hash;
1.37      www      12152:     my %bighash;
                   12153:     my $syval='';
1.620     albertel 12154:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  12155:         my $targetfn = $thisfn;
1.609     banghart 12156:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  12157:             $targetfn = 'adm/wrapper/'.$thisfn;
                   12158:         }
1.687     albertel 12159: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   12160: 	    $targetfn=$1;
                   12161: 	}
1.620     albertel 12162:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 12163:                       &GDBM_READER(),0640)) {
1.481     raeburn  12164: 	    $syval=$hash{$targetfn};
1.37      www      12165:             untie(%hash);
                   12166:         }
                   12167: # ---------------------------------------------------------- There was an entry
                   12168:         if ($syval) {
1.601     albertel 12169: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 12170: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  12171: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 12172: 		    #return $env{$cache_str}='';
1.601     albertel 12173: 		#}    
                   12174: 		#$syval.=$1;
                   12175: 	    #}
1.37      www      12176:         } else {
                   12177: # ------------------------------------------------------- Was not in symb table
1.620     albertel 12178:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 12179:                             &GDBM_READER(),0640)) {
1.37      www      12180: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      12181:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      12182:               unless ($ids) { 
                   12183:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      12184:               }
                   12185:               unless ($ids) {
                   12186: # alias?
                   12187: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      12188:               }
1.37      www      12189:               if ($ids) {
                   12190: # ------------------------------------------------------------------- Has ID(s)
                   12191:                  my @possibilities=split(/\,/,$ids);
1.39      www      12192:                  if ($#possibilities==0) {
                   12193: # ----------------------------------------------- There is only one possibility
1.37      www      12194: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 12195: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   12196: 						    $resid,$thisfn);
1.1282    raeburn  12197:                      if (ref($possibles) eq 'HASH') {
                   12198:                          $possibles->{$syval} = 1;    
                   12199:                      }
                   12200:                      if ($checkforblock) {
                   12201:                          my @blockers = &has_comm_blocking('bre',$syval,$bighash{'src_'.$ids});
                   12202:                          if (@blockers) {
                   12203:                              $syval = '';
                   12204:                              return;
                   12205:                          }
                   12206:                      }
                   12207:                  } elsif ((!$donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) { 
1.39      www      12208: # ------------------------------------------ There is more than one possibility
                   12209:                      my $realpossible=0;
1.800     albertel 12210:                      foreach my $id (@possibilities) {
                   12211: 			 my $file=$bighash{'src_'.$id};
1.1282    raeburn  12212:                          my $canaccess;
                   12213:                          if (($donotrecurse) || ($checkforblock) || (ref($possibles) eq 'HASH')) {
                   12214:                              $canaccess = 1;
                   12215:                          } else { 
                   12216:                              $canaccess = &allowed('bre',$file);
                   12217:                          }
                   12218:                          if ($canaccess) {
                   12219:          		     my ($mapid,$resid)=split(/\./,$id);
                   12220:                              if ($bighash{'map_type_'.$mapid} ne 'page') {
                   12221:                                  my $poss_syval=&encode_symb($bighash{'map_id_'.$mapid},
                   12222: 						             $resid,$thisfn);
                   12223:                                  if (ref($possibles) eq 'HASH') {
                   12224:                                      $possibles->{$syval} = 1;
                   12225:                                  }
                   12226:                                  if ($checkforblock) {
                   12227:                                      my @blockers = &has_comm_blocking('bre',$poss_syval,$file);
                   12228:                                      unless (@blockers > 0) {
                   12229:                                          $syval = $poss_syval;
                   12230:                                          $realpossible++;
                   12231:                                      }
                   12232:                                  } else {
                   12233:                                      $syval = $poss_syval;
                   12234:                                      $realpossible++;
                   12235:                                  }
                   12236:                              }
1.39      www      12237: 			 }
1.191     harris41 12238:                      }
1.39      www      12239: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      12240:                  } else {
                   12241:                      $syval='';
1.37      www      12242:                  }
                   12243: 	      }
1.1282    raeburn  12244:               untie(%bighash);
1.481     raeburn  12245:            }
1.31      www      12246:         }
1.62      www      12247:         if ($syval) {
1.620     albertel 12248: 	    return $env{$cache_str}=$syval;
1.62      www      12249:         }
1.31      www      12250:     }
1.949     raeburn  12251:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 12252:     return $env{$cache_str}='';
1.31      www      12253: }
                   12254: 
                   12255: # ---------------------------------------------------------- Return random seed
                   12256: 
1.32      www      12257: sub numval {
                   12258:     my $txt=shift;
                   12259:     $txt=~tr/A-J/0-9/;
                   12260:     $txt=~tr/a-j/0-9/;
                   12261:     $txt=~tr/K-T/0-9/;
                   12262:     $txt=~tr/k-t/0-9/;
                   12263:     $txt=~tr/U-Z/0-5/;
                   12264:     $txt=~tr/u-z/0-5/;
                   12265:     $txt=~s/\D//g;
1.564     albertel 12266:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      12267:     return int($txt);
1.368     albertel 12268: }
                   12269: 
1.484     albertel 12270: sub numval2 {
                   12271:     my $txt=shift;
                   12272:     $txt=~tr/A-J/0-9/;
                   12273:     $txt=~tr/a-j/0-9/;
                   12274:     $txt=~tr/K-T/0-9/;
                   12275:     $txt=~tr/k-t/0-9/;
                   12276:     $txt=~tr/U-Z/0-5/;
                   12277:     $txt=~tr/u-z/0-5/;
                   12278:     $txt=~s/\D//g;
                   12279:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   12280:     my $total;
                   12281:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 12282:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 12283:     return int($total);
                   12284: }
                   12285: 
1.575     albertel 12286: sub numval3 {
                   12287:     use integer;
                   12288:     my $txt=shift;
                   12289:     $txt=~tr/A-J/0-9/;
                   12290:     $txt=~tr/a-j/0-9/;
                   12291:     $txt=~tr/K-T/0-9/;
                   12292:     $txt=~tr/k-t/0-9/;
                   12293:     $txt=~tr/U-Z/0-5/;
                   12294:     $txt=~tr/u-z/0-5/;
                   12295:     $txt=~s/\D//g;
                   12296:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   12297:     my $total;
                   12298:     foreach my $val (@txts) { $total+=$val; }
                   12299:     if ($_64bit) { $total=(($total<<32)>>32); }
                   12300:     return $total;
                   12301: }
                   12302: 
1.675     albertel 12303: sub digest {
                   12304:     my ($data)=@_;
                   12305:     my $digest=&Digest::MD5::md5($data);
                   12306:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   12307:     my ($e,$f);
                   12308:     {
                   12309:         use integer;
                   12310:         $e=($a+$b);
                   12311:         $f=($c+$d);
                   12312:         if ($_64bit) {
                   12313:             $e=(($e<<32)>>32);
                   12314:             $f=(($f<<32)>>32);
                   12315:         }
                   12316:     }
                   12317:     if (wantarray) {
                   12318: 	return ($e,$f);
                   12319:     } else {
                   12320: 	my $g;
                   12321: 	{
                   12322: 	    use integer;
                   12323: 	    $g=($e+$f);
                   12324: 	    if ($_64bit) {
                   12325: 		$g=(($g<<32)>>32);
                   12326: 	    }
                   12327: 	}
                   12328: 	return $g;
                   12329:     }
                   12330: }
                   12331: 
1.368     albertel 12332: sub latest_rnd_algorithm_id {
1.675     albertel 12333:     return '64bit5';
1.366     albertel 12334: }
1.32      www      12335: 
1.503     albertel 12336: sub get_rand_alg {
                   12337:     my ($courseid)=@_;
1.790     albertel 12338:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 12339:     if ($courseid) {
1.620     albertel 12340: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 12341:     }
                   12342:     return &latest_rnd_algorithm_id();
                   12343: }
                   12344: 
1.562     albertel 12345: sub validCODE {
                   12346:     my ($CODE)=@_;
                   12347:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   12348:     return 0;
                   12349: }
                   12350: 
1.491     albertel 12351: sub getCODE {
1.620     albertel 12352:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 12353:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   12354: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   12355: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 12356: 	return $Apache::lonhomework::history{'resource.CODE'};
                   12357:     }
                   12358:     return undef;
                   12359: }
1.1133    foxr     12360: #
                   12361: #  Determines the random seed for a specific context:
                   12362: #
                   12363: # parameters:
                   12364: #   symb      - in course context the symb for the seed.
                   12365: #   course_id - The course id of the form domain_coursenum.
                   12366: #   domain    - Domain for the user.
                   12367: #   course    - Course for the user.
                   12368: #   cenv      - environment of the course.
                   12369: #
                   12370: # NOTE:
                   12371: #   All parameters are picked out of the environment if missing
                   12372: #   or not defined.
                   12373: #   If a symb cannot be determined the current time is used instead.
                   12374: #
                   12375: #  For a given well defined symb, courside, domain, username,
                   12376: #  and course environment, the seed is reproducible.
                   12377: #
1.31      www      12378: sub rndseed {
1.1133    foxr     12379:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 12380:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 12381:     if (!defined($symb)) {
1.366     albertel 12382: 	unless ($symb=$wsymb) { return time; }
                   12383:     }
1.1146    foxr     12384:     if (!defined $courseid) { 
                   12385: 	$courseid=$wcourseid; 
                   12386:     }
                   12387:     if (!defined $domain) { $domain=$wdomain; }
                   12388:     if (!defined $username) { $username=$wusername }
1.1133    foxr     12389: 
                   12390:     my $which;
                   12391:     if (defined($cenv->{'rndseed'})) {
                   12392: 	$which = $cenv->{'rndseed'};
                   12393:     } else {
                   12394: 	$which =&get_rand_alg($courseid);
                   12395:     }
1.491     albertel 12396:     if (defined(&getCODE())) {
1.1133    foxr     12397: 
1.675     albertel 12398: 	if ($which eq '64bit5') {
                   12399: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   12400: 	} elsif ($which eq '64bit4') {
1.575     albertel 12401: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   12402: 	} else {
                   12403: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   12404: 	}
1.675     albertel 12405:     } elsif ($which eq '64bit5') {
                   12406: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 12407:     } elsif ($which eq '64bit4') {
                   12408: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 12409:     } elsif ($which eq '64bit3') {
                   12410: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 12411:     } elsif ($which eq '64bit2') {
                   12412: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 12413:     } elsif ($which eq '64bit') {
                   12414: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   12415:     }
                   12416:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   12417: }
                   12418: 
                   12419: sub rndseed_32bit {
                   12420:     my ($symb,$courseid,$domain,$username)=@_;
                   12421:     {
                   12422: 	use integer;
                   12423: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   12424: 	my $symbseed=numval($symb) << 22;
                   12425: 	my $namechck=unpack("%32C*",$username) << 17;
                   12426: 	my $nameseed=numval($username) << 12;
                   12427: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   12428: 	my $courseseed=unpack("%32C*",$courseid);
                   12429: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 12430: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   12431: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 12432: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 12433: 	return $num;
                   12434:     }
                   12435: }
                   12436: 
                   12437: sub rndseed_64bit {
                   12438:     my ($symb,$courseid,$domain,$username)=@_;
                   12439:     {
                   12440: 	use integer;
                   12441: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   12442: 	my $symbseed=numval($symb) << 10;
                   12443: 	my $namechck=unpack("%32S*",$username);
                   12444: 	
                   12445: 	my $nameseed=numval($username) << 21;
                   12446: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   12447: 	my $courseseed=unpack("%32S*",$courseid);
                   12448: 	
                   12449: 	my $num1=$symbchck+$symbseed+$namechck;
                   12450: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 12451: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   12452: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 12453: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 12454: 	return "$num1,$num2";
1.155     albertel 12455:     }
1.366     albertel 12456: }
                   12457: 
1.443     albertel 12458: sub rndseed_64bit2 {
                   12459:     my ($symb,$courseid,$domain,$username)=@_;
                   12460:     {
                   12461: 	use integer;
                   12462: 	# strings need to be an even # of cahracters long, it it is odd the
                   12463:         # last characters gets thrown away
                   12464: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   12465: 	my $symbseed=numval($symb) << 10;
                   12466: 	my $namechck=unpack("%32S*",$username.' ');
                   12467: 	
                   12468: 	my $nameseed=numval($username) << 21;
1.501     albertel 12469: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   12470: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   12471: 	
                   12472: 	my $num1=$symbchck+$symbseed+$namechck;
                   12473: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 12474: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   12475: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 12476: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 12477: 	return "$num1,$num2";
                   12478:     }
                   12479: }
                   12480: 
                   12481: sub rndseed_64bit3 {
                   12482:     my ($symb,$courseid,$domain,$username)=@_;
                   12483:     {
                   12484: 	use integer;
                   12485: 	# strings need to be an even # of cahracters long, it it is odd the
                   12486:         # last characters gets thrown away
                   12487: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   12488: 	my $symbseed=numval2($symb) << 10;
                   12489: 	my $namechck=unpack("%32S*",$username.' ');
                   12490: 	
                   12491: 	my $nameseed=numval2($username) << 21;
1.443     albertel 12492: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   12493: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   12494: 	
                   12495: 	my $num1=$symbchck+$symbseed+$namechck;
                   12496: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 12497: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   12498: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 12499: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      12500: 	
1.503     albertel 12501: 	return "$num1:$num2";
1.443     albertel 12502:     }
                   12503: }
                   12504: 
1.575     albertel 12505: sub rndseed_64bit4 {
                   12506:     my ($symb,$courseid,$domain,$username)=@_;
                   12507:     {
                   12508: 	use integer;
                   12509: 	# strings need to be an even # of cahracters long, it it is odd the
                   12510:         # last characters gets thrown away
                   12511: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   12512: 	my $symbseed=numval3($symb) << 10;
                   12513: 	my $namechck=unpack("%32S*",$username.' ');
                   12514: 	
                   12515: 	my $nameseed=numval3($username) << 21;
                   12516: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   12517: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   12518: 	
                   12519: 	my $num1=$symbchck+$symbseed+$namechck;
                   12520: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 12521: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   12522: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 12523: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      12524: 	
1.575     albertel 12525: 	return "$num1:$num2";
                   12526:     }
                   12527: }
                   12528: 
1.675     albertel 12529: sub rndseed_64bit5 {
                   12530:     my ($symb,$courseid,$domain,$username)=@_;
                   12531:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   12532:     return "$num1:$num2";
                   12533: }
                   12534: 
1.366     albertel 12535: sub rndseed_CODE_64bit {
                   12536:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 12537:     {
1.366     albertel 12538: 	use integer;
1.443     albertel 12539: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 12540: 	my $symbseed=numval2($symb);
1.491     albertel 12541: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   12542: 	my $CODEseed=numval(&getCODE());
1.443     albertel 12543: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 12544: 	my $num1=$symbseed+$CODEchck;
                   12545: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 12546: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   12547: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 12548: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   12549: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 12550: 	return "$num1:$num2";
1.366     albertel 12551:     }
                   12552: }
                   12553: 
1.575     albertel 12554: sub rndseed_CODE_64bit4 {
                   12555:     my ($symb,$courseid,$domain,$username)=@_;
                   12556:     {
                   12557: 	use integer;
                   12558: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   12559: 	my $symbseed=numval3($symb);
                   12560: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   12561: 	my $CODEseed=numval3(&getCODE());
                   12562: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   12563: 	my $num1=$symbseed+$CODEchck;
                   12564: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 12565: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   12566: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 12567: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   12568: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   12569: 	return "$num1:$num2";
                   12570:     }
                   12571: }
                   12572: 
1.675     albertel 12573: sub rndseed_CODE_64bit5 {
                   12574:     my ($symb,$courseid,$domain,$username)=@_;
                   12575:     my $code = &getCODE();
                   12576:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   12577:     return "$num1:$num2";
                   12578: }
                   12579: 
1.366     albertel 12580: sub setup_random_from_rndseed {
                   12581:     my ($rndseed)=@_;
1.503     albertel 12582:     if ($rndseed =~/([,:])/) {
1.1262    raeburn  12583:         my ($num1,$num2) = map { abs($_); } (split(/[,:]/,$rndseed));
                   12584:         if ((!$num1) || (!$num2) || ($num1 > 2147483562) || ($num2 > 2147483398)) {
                   12585:             &Math::Random::random_set_seed_from_phrase($rndseed);
                   12586:         } else {
                   12587:             &Math::Random::random_set_seed($num1,$num2);
                   12588:         }
1.366     albertel 12589:     } else {
                   12590: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 12591:     }
1.36      albertel 12592: }
                   12593: 
1.474     albertel 12594: sub latest_receipt_algorithm_id {
1.835     albertel 12595:     return 'receipt3';
1.474     albertel 12596: }
                   12597: 
1.480     www      12598: sub recunique {
                   12599:     my $fucourseid=shift;
                   12600:     my $unique;
1.835     albertel 12601:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   12602: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 12603: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      12604:     } else {
                   12605: 	$unique=$perlvar{'lonReceipt'};
                   12606:     }
                   12607:     return unpack("%32C*",$unique);
                   12608: }
                   12609: 
                   12610: sub recprefix {
                   12611:     my $fucourseid=shift;
                   12612:     my $prefix;
1.835     albertel 12613:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   12614: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 12615: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      12616:     } else {
                   12617: 	$prefix=$perlvar{'lonHostID'};
                   12618:     }
                   12619:     return unpack("%32C*",$prefix);
                   12620: }
                   12621: 
1.76      www      12622: sub ireceipt {
1.474     albertel 12623:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 12624: 
                   12625:     my $return =&recprefix($fucourseid).'-';
                   12626: 
                   12627:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   12628: 	$env{'request.state'} eq 'construct') {
                   12629: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   12630: 	return $return;
                   12631:     }
                   12632: 
1.76      www      12633:     my $cuname=unpack("%32C*",$funame);
                   12634:     my $cudom=unpack("%32C*",$fudom);
                   12635:     my $cucourseid=unpack("%32C*",$fucourseid);
                   12636:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      12637:     my $cunique=&recunique($fucourseid);
1.474     albertel 12638:     my $cpart=unpack("%32S*",$part);
1.835     albertel 12639:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   12640: 
1.790     albertel 12641: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 12642: 			       
                   12643: 	$return.= ($cunique%$cuname+
                   12644: 		   $cunique%$cudom+
                   12645: 		   $cusymb%$cuname+
                   12646: 		   $cusymb%$cudom+
                   12647: 		   $cucourseid%$cuname+
                   12648: 		   $cucourseid%$cudom+
                   12649: 		   $cpart%$cuname+
                   12650: 		   $cpart%$cudom);
                   12651:     } else {
                   12652: 	$return.= ($cunique%$cuname+
                   12653: 		   $cunique%$cudom+
                   12654: 		   $cusymb%$cuname+
                   12655: 		   $cusymb%$cudom+
                   12656: 		   $cucourseid%$cuname+
                   12657: 		   $cucourseid%$cudom);
                   12658:     }
                   12659:     return $return;
1.76      www      12660: }
                   12661: 
                   12662: sub receipt {
1.474     albertel 12663:     my ($part)=@_;
1.790     albertel 12664:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 12665:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      12666: }
1.260     ng       12667: 
1.790     albertel 12668: sub whichuser {
                   12669:     my ($passedsymb)=@_;
                   12670:     my ($symb,$courseid,$domain,$name,$publicuser);
                   12671:     if (defined($env{'form.grade_symb'})) {
                   12672: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   12673: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   12674: 	if (!$allowed &&
                   12675: 	    exists($env{'request.course.sec'}) &&
                   12676: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   12677: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   12678: 			      '/'.$env{'request.course.sec'});
                   12679: 	}
                   12680: 	if ($allowed) {
                   12681: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   12682: 	    $courseid=$tmp_courseid;
                   12683: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   12684: 	    ($name)=&get_env_multiple('form.grade_username');
                   12685: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   12686: 	}
                   12687:     }
                   12688:     if (!$passedsymb) {
                   12689: 	$symb=&symbread();
                   12690:     } else {
                   12691: 	$symb=$passedsymb;
                   12692:     }
                   12693:     $courseid=$env{'request.course.id'};
                   12694:     $domain=$env{'user.domain'};
                   12695:     $name=$env{'user.name'};
                   12696:     if ($name eq 'public' && $domain eq 'public') {
                   12697: 	if (!defined($env{'form.username'})) {
                   12698: 	    $env{'form.username'}.=time.rand(10000000);
                   12699: 	}
                   12700: 	$name.=$env{'form.username'};
                   12701:     }
                   12702:     return ($symb,$courseid,$domain,$name,$publicuser);
                   12703: 
                   12704: }
                   12705: 
1.36      albertel 12706: # ------------------------------------------------------------ Serves up a file
1.472     albertel 12707: # returns either the contents of the file or 
                   12708: # -1 if the file doesn't exist
1.481     raeburn  12709: #
                   12710: # if the target is a file that was uploaded via DOCS, 
                   12711: # a check will be made to see if a current copy exists on the local server,
                   12712: # if it does this will be served, otherwise a copy will be retrieved from
                   12713: # the home server for the course and stored in /home/httpd/html/userfiles on
                   12714: # the local server.   
1.472     albertel 12715: 
1.36      albertel 12716: sub getfile {
1.538     albertel 12717:     my ($file) = @_;
1.609     banghart 12718:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 12719:     &repcopy($file);
                   12720:     return &readfile($file);
                   12721: }
                   12722: 
                   12723: sub repcopy_userfile {
                   12724:     my ($file)=@_;
1.1142    raeburn  12725:     my $londocroot = $perlvar{'lonDocRoot'};
                   12726:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  12727:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 12728:     my ($cdom,$cnum,$filename) = 
1.811     albertel 12729: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 12730:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   12731:     if (-e "$file") {
1.828     www      12732: # we already have a local copy, check it out
1.538     albertel 12733: 	my @fileinfo = stat($file);
1.828     www      12734: 	my $rtncode;
                   12735: 	my $info;
1.538     albertel 12736: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 12737: 	if ($lwpresp ne 'ok') {
1.828     www      12738: # there is no such file anymore, even though we had a local copy
1.482     albertel 12739: 	    if ($rtncode eq '404') {
1.538     albertel 12740: 		unlink($file);
1.482     albertel 12741: 	    }
                   12742: 	    return -1;
                   12743: 	}
                   12744: 	if ($info < $fileinfo[9]) {
1.828     www      12745: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  12746: 	    return 'ok';
1.828     www      12747: 	} else {
                   12748: # the file is outdated, get rid of it
                   12749: 	    unlink($file);
1.482     albertel 12750: 	}
1.828     www      12751:     }
                   12752: # one way or the other, at this point, we don't have the file
                   12753: # construct the correct path for the file
                   12754:     my @parts = ($cdom,$cnum); 
                   12755:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   12756: 	push @parts, split(/\//,$1);
                   12757:     }
                   12758:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   12759:     foreach my $part (@parts) {
                   12760: 	$path .= '/'.$part;
                   12761: 	if (!-e $path) {
                   12762: 	    mkdir($path,0770);
1.482     albertel 12763: 	}
                   12764:     }
1.828     www      12765: # now the path exists for sure
                   12766: # get a user agent
                   12767:     my $transferfile=$file.'.in.transfer';
                   12768: # FIXME: this should flock
                   12769:     if (-e $transferfile) { return 'ok'; }
                   12770:     my $request;
                   12771:     $uri=~s/^\///;
1.980     raeburn  12772:     my $homeserver = &homeserver($cnum,$cdom);
                   12773:     my $protocol = $protocol{$homeserver};
                   12774:     $protocol = 'http' if ($protocol ne 'https');
                   12775:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.1345  ! raeburn  12776:     my $response = &LONCAPA::LWPReq::makerequest($homeserver,$request,$transferfile,\%perlvar,'',0,1);
1.828     www      12777: # did it work?
                   12778:     if ($response->is_error()) {
                   12779: 	unlink($transferfile);
                   12780: 	&logthis("Userfile repcopy failed for $uri");
                   12781: 	return -1;
                   12782:     }
                   12783: # worked, rename the transfer file
                   12784:     rename($transferfile,$file);
1.607     raeburn  12785:     return 'ok';
1.481     raeburn  12786: }
                   12787: 
1.517     albertel 12788: sub tokenwrapper {
                   12789:     my $uri=shift;
1.980     raeburn  12790:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 12791:     $uri=~s|^/||;
1.620     albertel 12792:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 12793:     my $token=$1;
1.552     albertel 12794:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   12795:     if ($udom && $uname && $file) {
                   12796: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  12797:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  12798:         my $homeserver = &homeserver($uname,$udom);
                   12799:         my $protocol = $protocol{$homeserver};
                   12800:         $protocol = 'http' if ($protocol ne 'https');
                   12801:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517     albertel 12802:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   12803:                                '&tokenissued='.$perlvar{'lonHostID'};
                   12804:     } else {
                   12805:         return '/adm/notfound.html';
                   12806:     }
                   12807: }
                   12808: 
1.828     www      12809: # call with reqtype HEAD: get last modification time
                   12810: # call with reqtype GET: get the file contents
                   12811: # Do not call this with reqtype GET for large files! It loads everything into memory
                   12812: #
1.481     raeburn  12813: sub getuploaded {
                   12814:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   12815:     $uri=~s/^\///;
1.980     raeburn  12816:     my $homeserver = &homeserver($cnum,$cdom);
                   12817:     my $protocol = $protocol{$homeserver};
                   12818:     $protocol = 'http' if ($protocol ne 'https');
                   12819:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481     raeburn  12820:     my $request=new HTTP::Request($reqtype,$uri);
1.1345  ! raeburn  12821:     my $response=&LONCAPA::LWPReq::makerequest($homeserver,$request,'',\%perlvar,'',0,1);
1.481     raeburn  12822:     $$rtncode = $response->code;
1.482     albertel 12823:     if (! $response->is_success()) {
                   12824: 	return 'failed';
                   12825:     }      
                   12826:     if ($reqtype eq 'HEAD') {
1.486     www      12827: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 12828:     } elsif ($reqtype eq 'GET') {
                   12829: 	$$info = $response->content;
1.472     albertel 12830:     }
1.482     albertel 12831:     return 'ok';
1.36      albertel 12832: }
                   12833: 
1.481     raeburn  12834: sub readfile {
                   12835:     my $file = shift;
                   12836:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   12837:     my $fh;
                   12838:     open($fh,"<$file");
                   12839:     my $a='';
1.800     albertel 12840:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  12841:     return $a;
                   12842: }
                   12843: 
1.36      albertel 12844: sub filelocation {
1.590     banghart 12845:     my ($dir,$file) = @_;
                   12846:     my $location;
                   12847:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 12848: 
                   12849:     if ($file =~ m-^/adm/-) {
                   12850: 	$file=~s-^/adm/wrapper/-/-;
                   12851: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   12852:     }
1.882     albertel 12853: 
1.1139    www      12854:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  12855:         $location = $file;
1.609     banghart 12856:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 12857:         my ($udom,$uname,$filename)=
1.811     albertel 12858:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 12859:         my $home=&homeserver($uname,$udom);
                   12860:         my $is_me=0;
                   12861:         my @ids=&current_machine_ids();
                   12862:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   12863:         if ($is_me) {
1.1117    foxr     12864:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 12865:         } else {
                   12866:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   12867:   	      $udom.'/'.$uname.'/'.$filename;
                   12868:         }
1.882     albertel 12869:     } elsif ($file =~ m-^/adm/-) {
                   12870: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 12871:     } else {
                   12872:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      12873:         $file=~s:^/(res|priv)/:/:;
                   12874:         my $space=$1;
1.590     banghart 12875:         if ( !( $file =~ m:^/:) ) {
                   12876:             $location = $dir. '/'.$file;
                   12877:         } else {
1.1142    raeburn  12878:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 12879:         }
1.59      albertel 12880:     }
1.590     banghart 12881:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 12882:     while ($location=~m{/\.\./}) {
                   12883: 	if ($location =~ m{/[^/]+/\.\./}) {
                   12884: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   12885: 	} else {
                   12886: 	    $location=~ s{/\.\./}{/}g;
                   12887: 	}
                   12888:     } #remove dir/..
1.590     banghart 12889:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   12890:     return $location;
1.46      www      12891: }
1.36      albertel 12892: 
1.46      www      12893: sub hreflocation {
                   12894:     my ($dir,$file)=@_;
1.980     raeburn  12895:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 12896: 	$file=filelocation($dir,$file);
1.700     albertel 12897:     } elsif ($file=~m-^/adm/-) {
                   12898: 	$file=~s-^/adm/wrapper/-/-;
                   12899: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 12900:     }
                   12901:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   12902: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   12903:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  12904: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   12905: 	        {/uploaded/$1/$2/}x;
1.46      www      12906:     }
1.913     albertel 12907:     if ($file=~ m{^/userfiles/}) {
                   12908: 	$file =~ s{^/userfiles/}{/uploaded/};
                   12909:     }
1.462     albertel 12910:     return $file;
1.465     albertel 12911: }
                   12912: 
1.1139    www      12913: 
                   12914: 
                   12915: 
                   12916: 
1.465     albertel 12917: sub current_machine_domains {
1.853     albertel 12918:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   12919: }
                   12920: 
                   12921: sub machine_domains {
                   12922:     my ($hostname) = @_;
1.465     albertel 12923:     my @domains;
1.838     albertel 12924:     my %hostname = &all_hostnames();
1.465     albertel 12925:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  12926: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 12927: 	if ($hostname eq $name) {
1.844     albertel 12928: 	    push(@domains,&host_domain($id));
1.465     albertel 12929: 	}
                   12930:     }
                   12931:     return @domains;
                   12932: }
                   12933: 
                   12934: sub current_machine_ids {
1.853     albertel 12935:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   12936: }
                   12937: 
                   12938: sub machine_ids {
                   12939:     my ($hostname) = @_;
                   12940:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 12941:     my @ids;
1.888     albertel 12942:     my %name_to_host = &all_names();
1.889     albertel 12943:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   12944: 	return @{ $name_to_host{$hostname} };
                   12945:     }
                   12946:     return;
1.31      www      12947: }
                   12948: 
1.824     raeburn  12949: sub additional_machine_domains {
                   12950:     my @domains;
                   12951:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   12952:     while( my $line = <$fh>) {
                   12953:         $line =~ s/\s//g;
                   12954:         push(@domains,$line);
                   12955:     }
                   12956:     return @domains;
                   12957: }
                   12958: 
                   12959: sub default_login_domain {
                   12960:     my $domain = $perlvar{'lonDefDomain'};
                   12961:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   12962:     foreach my $posdom (&current_machine_domains(),
                   12963:                         &additional_machine_domains()) {
                   12964:         if (lc($posdom) eq lc($testdomain)) {
                   12965:             $domain=$posdom;
                   12966:             last;
                   12967:         }
                   12968:     }
                   12969:     return $domain;
                   12970: }
                   12971: 
1.31      www      12972: # ------------------------------------------------------------- Declutters URLs
                   12973: 
                   12974: sub declutter {
                   12975:     my $thisfn=shift;
1.569     albertel 12976:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.1261    raeburn  12977:     unless ($thisfn=~m{^/home/httpd/html/priv/}) {
                   12978:         $thisfn=~s{^/home/httpd/html}{};
                   12979:     }
1.31      www      12980:     $thisfn=~s/^\///;
1.697     albertel 12981:     $thisfn=~s|^adm/wrapper/||;
                   12982:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      12983:     $thisfn=~s/^res\///;
1.1172    bisitz   12984:     $thisfn=~s/^priv\///;
1.1032    raeburn  12985:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   12986:         $thisfn=~s/\?.+$//;
                   12987:     }
1.268     www      12988:     return $thisfn;
                   12989: }
                   12990: 
                   12991: # ------------------------------------------------------------- Clutter up URLs
                   12992: 
                   12993: sub clutter {
                   12994:     my $thisfn='/'.&declutter(shift);
1.887     albertel 12995:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 12996: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      12997:        $thisfn='/res'.$thisfn; 
                   12998:     }
1.1031    raeburn  12999:     if ($thisfn !~m|^/adm|) {
                   13000: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 13001: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 13002: 	} else {
                   13003: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   13004: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 13005: 	    if ($embstyle eq 'ssi'
                   13006: 		|| ($embstyle eq 'hdn')
                   13007: 		|| ($embstyle eq 'rat')
                   13008: 		|| ($embstyle eq 'prv')
                   13009: 		|| ($embstyle eq 'ign')) {
                   13010: 		#do nothing with these
                   13011: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 13012: 		|| ($embstyle eq 'emb')
                   13013: 		|| ($embstyle eq 'wrp')) {
                   13014: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 13015: 	    } elsif ($embstyle eq 'unk'
                   13016: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 13017: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 13018: 	    } else {
1.718     www      13019: #		&logthis("Got a blank emb style");
1.695     albertel 13020: 	    }
1.694     albertel 13021: 	}
1.1343    raeburn  13022:     } elsif ($thisfn =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
1.1298    raeburn  13023:         $thisfn='/adm/wrapper'.$thisfn;
1.694     albertel 13024:     }
1.31      www      13025:     return $thisfn;
1.12      www      13026: }
                   13027: 
1.787     albertel 13028: sub clutter_with_no_wrapper {
                   13029:     my $uri = &clutter(shift);
                   13030:     if ($uri =~ m-^/adm/-) {
                   13031: 	$uri =~ s-^/adm/wrapper/-/-;
                   13032: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   13033:     }
                   13034:     return $uri;
                   13035: }
                   13036: 
1.557     albertel 13037: sub freeze_escape {
                   13038:     my ($value)=@_;
                   13039:     if (ref($value)) {
                   13040: 	$value=&nfreeze($value);
                   13041: 	return '__FROZEN__'.&escape($value);
                   13042:     }
                   13043:     return &escape($value);
                   13044: }
                   13045: 
1.11      www      13046: 
1.557     albertel 13047: sub thaw_unescape {
                   13048:     my ($value)=@_;
                   13049:     if ($value =~ /^__FROZEN__/) {
                   13050: 	substr($value,0,10,undef);
                   13051: 	$value=&unescape($value);
                   13052: 	return &thaw($value);
                   13053:     }
                   13054:     return &unescape($value);
                   13055: }
                   13056: 
1.436     albertel 13057: sub correct_line_ends {
                   13058:     my ($result)=@_;
                   13059:     $$result =~s/\r\n/\n/mg;
                   13060:     $$result =~s/\r/\n/mg;
1.415     albertel 13061: }
1.1       albertel 13062: # ================================================================ Main Program
                   13063: 
1.184     www      13064: sub goodbye {
1.204     albertel 13065:    &logthis("Starting Shut down");
1.443     albertel 13066: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 13067:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 13068: #converted
1.599     albertel 13069: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 13070:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   13071: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   13072: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 13073: #1.1 only
1.870     albertel 13074: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   13075: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   13076: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   13077: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   13078:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 13079:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   13080:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      13081:    &flushcourselogs();
                   13082:    &logthis("Shutting down");
                   13083: }
                   13084: 
1.852     albertel 13085: sub get_dns {
1.1210    raeburn  13086:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 13087:     if (!$ignore_cache) {
                   13088: 	my ($content,$cached)=
                   13089: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   13090: 	if ($cached) {
1.1210    raeburn  13091: 	    &$func($content,$hashref);
1.869     albertel 13092: 	    return;
                   13093: 	}
                   13094:     }
                   13095: 
                   13096:     my %alldns;
1.852     albertel 13097:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   13098:     foreach my $dns (<$config>) {
                   13099: 	next if ($dns !~ /^\^(\S*)/x);
1.979     raeburn  13100:         my $line = $1;
                   13101:         my ($host,$protocol) = split(/:/,$line);
                   13102:         if ($protocol ne 'https') {
                   13103:             $protocol = 'http';
                   13104:         }
                   13105: 	$alldns{$host} = $protocol;
1.869     albertel 13106:     }
                   13107:     while (%alldns) {
1.1263    raeburn  13108: 	my ($dns) = sort { $b cmp $a } keys(%alldns);
1.979     raeburn  13109: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.1345  ! raeburn  13110:         my $response = &LONCAPA::LWPReq::makerequest('',$request,'',\%perlvar,30,0);
1.979     raeburn  13111:         delete($alldns{$dns});
1.852     albertel 13112: 	next if ($response->is_error());
                   13113: 	my @content = split("\n",$response->content);
1.1210    raeburn  13114: 	unless ($nocache) {
1.1219    raeburn  13115: 	    &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1210    raeburn  13116: 	}
                   13117: 	&$func(\@content,$hashref);
1.869     albertel 13118: 	return;
1.852     albertel 13119:     }
                   13120:     close($config);
1.871     albertel 13121:     my $which = (split('/',$url))[3];
                   13122:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   13123:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 13124:     my @content = <$config>;
1.1210    raeburn  13125:     &$func(\@content,$hashref);
                   13126:     return;
                   13127: }
                   13128: 
                   13129: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  13130: sub parse_dns_checksums_tab {
1.1210    raeburn  13131:     my ($lines,$hashref) = @_;
1.1264    raeburn  13132:     my $lonhost = $perlvar{'lonHostID'};
                   13133:     my $machine_dom = &Apache::lonnet::host_domain($lonhost);
1.1210    raeburn  13134:     my $loncaparev = &get_server_loncaparev($machine_dom);
1.1264    raeburn  13135:     my $distro = (split(/\:/,&get_server_distarch($lonhost)))[0];
                   13136:     my $webconfdir = '/etc/httpd/conf';
                   13137:     if ($distro =~ /^(ubuntu|debian)(\d+)$/) {
                   13138:         $webconfdir = '/etc/apache2';
                   13139:     } elsif ($distro =~ /^sles(\d+)$/) {
                   13140:         if ($1 >= 10) {
                   13141:             $webconfdir = '/etc/apache2';
                   13142:         }
                   13143:     } elsif ($distro =~ /^suse(\d+\.\d+)$/) {
                   13144:         if ($1 >= 10.0) {
                   13145:             $webconfdir = '/etc/apache2';
                   13146:         }
                   13147:     }
1.1210    raeburn  13148:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   13149:     my (%chksum,%revnum);
                   13150:     if (ref($lines) eq 'ARRAY') {
                   13151:         chomp(@{$lines});
1.1238    raeburn  13152:         my $version = shift(@{$lines});
                   13153:         if ($version eq $release) {  
1.1210    raeburn  13154:             foreach my $line (@{$lines}) {
1.1238    raeburn  13155:                 my ($file,$version,$shasum) = split(/,/,$line);
1.1264    raeburn  13156:                 if ($file =~ m{^/etc/httpd/conf}) {
                   13157:                     if ($webconfdir eq '/etc/apache2') {
                   13158:                         $file =~ s{^\Q/etc/httpd/conf/\E}{$webconfdir/};
                   13159:                     }
                   13160:                 }
1.1238    raeburn  13161:                 $chksum{$file} = $shasum;
                   13162:                 $revnum{$file} = $version;
1.1210    raeburn  13163:             }
                   13164:             if (ref($hashref) eq 'HASH') {
                   13165:                 %{$hashref} = (
                   13166:                                 sums     => \%chksum,
                   13167:                                 versions => \%revnum,
                   13168:                               );
                   13169:             }
                   13170:         }
                   13171:     }
1.869     albertel 13172:     return;
1.852     albertel 13173: }
1.1210    raeburn  13174: 
                   13175: sub fetch_dns_checksums {
1.1238    raeburn  13176:     my %checksums;
                   13177:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
1.1260    raeburn  13178:     my $loncaparev = &get_server_loncaparev($machine_dom,$perlvar{'lonHostID'});
1.1238    raeburn  13179:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   13180:     &get_dns("/adm/dns/checksums/$release",\&parse_dns_checksums_tab,1,1,
1.1211    raeburn  13181:              \%checksums);
1.1210    raeburn  13182:     return \%checksums;
                   13183: }
                   13184: 
1.327     albertel 13185: # ------------------------------------------------------------ Read domain file
                   13186: {
1.852     albertel 13187:     my $loaded;
1.846     albertel 13188:     my %domain;
                   13189: 
1.852     albertel 13190:     sub parse_domain_tab {
                   13191: 	my ($lines) = @_;
                   13192: 	foreach my $line (@$lines) {
                   13193: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      13194: 
1.846     albertel 13195: 	    chomp($line);
1.852     albertel 13196: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 13197: 	    my %this_domain;
                   13198: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   13199: 			       'lang_def', 'city', 'longi', 'lati',
                   13200: 			       'primary') {
                   13201: 		$this_domain{$field} = shift(@elements);
                   13202: 	    }
                   13203: 	    $domain{$name} = \%this_domain;
1.852     albertel 13204: 	}
                   13205:     }
1.864     albertel 13206: 
                   13207:     sub reset_domain_info {
                   13208: 	undef($loaded);
                   13209: 	undef(%domain);
                   13210:     }
                   13211: 
1.852     albertel 13212:     sub load_domain_tab {
1.1293    raeburn  13213: 	my ($ignore_cache,$nocache) = @_;
                   13214: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache,$nocache);
1.852     albertel 13215: 	my $fh;
                   13216: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   13217: 	    my @lines = <$fh>;
                   13218: 	    &parse_domain_tab(\@lines);
1.448     albertel 13219: 	}
1.852     albertel 13220: 	close($fh);
                   13221: 	$loaded = 1;
1.327     albertel 13222:     }
1.846     albertel 13223: 
                   13224:     sub domain {
1.852     albertel 13225: 	&load_domain_tab() if (!$loaded);
                   13226: 
1.846     albertel 13227: 	my ($name,$what) = @_;
                   13228: 	return if ( !exists($domain{$name}) );
                   13229: 
                   13230: 	if (!$what) {
                   13231: 	    return $domain{$name}{'description'};
                   13232: 	}
                   13233: 	return $domain{$name}{$what};
                   13234:     }
1.974     raeburn  13235: 
                   13236:     sub domain_info {
                   13237:         &load_domain_tab() if (!$loaded);
                   13238:         return %domain;
                   13239:     }
                   13240: 
1.327     albertel 13241: }
                   13242: 
                   13243: 
1.1       albertel 13244: # ------------------------------------------------------------- Read hosts file
                   13245: {
1.838     albertel 13246:     my %hostname;
1.844     albertel 13247:     my %hostdom;
1.845     albertel 13248:     my %libserv;
1.852     albertel 13249:     my $loaded;
1.888     albertel 13250:     my %name_to_host;
1.1074    raeburn  13251:     my %internetdom;
1.1107    raeburn  13252:     my %LC_dns_serv;
1.852     albertel 13253: 
                   13254:     sub parse_hosts_tab {
                   13255: 	my ($file) = @_;
                   13256: 	foreach my $configline (@$file) {
                   13257: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  13258:             chomp($configline);
                   13259: 	    if ($configline =~ /^\^/) {
                   13260:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   13261:                     $LC_dns_serv{$1} = 1;
                   13262:                 }
                   13263:                 next;
                   13264:             }
1.1074    raeburn  13265: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 13266: 	    $name=~s/\s//g;
                   13267: 	    if ($id && $domain && $role && $name) {
                   13268: 		$hostname{$id}=$name;
1.888     albertel 13269: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 13270: 		$hostdom{$id}=$domain;
                   13271: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  13272:                 if (defined($protocol)) {
                   13273:                     if ($protocol eq 'https') {
                   13274:                         $protocol{$id} = $protocol;
                   13275:                     } else {
                   13276:                         $protocol{$id} = 'http'; 
                   13277:                     }
1.968     raeburn  13278:                 } else {
1.969     raeburn  13279:                     $protocol{$id} = 'http';
1.968     raeburn  13280:                 }
1.1074    raeburn  13281:                 if (defined($intdom)) {
                   13282:                     $internetdom{$id} = $intdom;
                   13283:                 }
1.852     albertel 13284: 	    }
                   13285: 	}
                   13286:     }
1.864     albertel 13287:     
                   13288:     sub reset_hosts_info {
1.897     albertel 13289: 	&purge_remembered();
1.864     albertel 13290: 	&reset_domain_info();
                   13291: 	&reset_hosts_ip_info();
1.1339    raeburn  13292:         undef(%internetdom);
1.892     albertel 13293: 	undef(%name_to_host);
1.864     albertel 13294: 	undef(%hostname);
                   13295: 	undef(%hostdom);
                   13296: 	undef(%libserv);
                   13297: 	undef($loaded);
                   13298:     }
1.1       albertel 13299: 
1.852     albertel 13300:     sub load_hosts_tab {
1.1293    raeburn  13301: 	my ($ignore_cache,$nocache) = @_;
                   13302: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache,$nocache);
1.852     albertel 13303: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   13304: 	my @config = <$config>;
                   13305: 	&parse_hosts_tab(\@config);
                   13306: 	close($config);
                   13307: 	$loaded=1;
1.1       albertel 13308:     }
1.852     albertel 13309: 
1.838     albertel 13310:     sub hostname {
1.852     albertel 13311: 	&load_hosts_tab() if (!$loaded);
                   13312: 
1.838     albertel 13313: 	my ($lonid) = @_;
                   13314: 	return $hostname{$lonid};
                   13315:     }
1.845     albertel 13316: 
1.838     albertel 13317:     sub all_hostnames {
1.852     albertel 13318: 	&load_hosts_tab() if (!$loaded);
                   13319: 
1.838     albertel 13320: 	return %hostname;
                   13321:     }
1.845     albertel 13322: 
1.888     albertel 13323:     sub all_names {
1.1293    raeburn  13324:         my ($ignore_cache,$nocache) = @_;
                   13325: 	&load_hosts_tab($ignore_cache,$nocache) if (!$loaded);
1.888     albertel 13326: 
                   13327: 	return %name_to_host;
                   13328:     }
                   13329: 
1.974     raeburn  13330:     sub all_host_domain {
                   13331:         &load_hosts_tab() if (!$loaded);
                   13332:         return %hostdom;
                   13333:     }
                   13334: 
1.1339    raeburn  13335:     sub all_host_intdom {
                   13336:         &load_hosts_tab() if (!$loaded);
                   13337:         return %internetdom;
                   13338:     }
                   13339: 
1.845     albertel 13340:     sub is_library {
1.852     albertel 13341: 	&load_hosts_tab() if (!$loaded);
                   13342: 
1.845     albertel 13343: 	return exists($libserv{$_[0]});
                   13344:     }
                   13345: 
                   13346:     sub all_library {
1.852     albertel 13347: 	&load_hosts_tab() if (!$loaded);
                   13348: 
1.845     albertel 13349: 	return %libserv;
                   13350:     }
                   13351: 
1.1062    droeschl 13352:     sub unique_library {
                   13353: 	#2x reverse removes all hostnames that appear more than once
                   13354:         my %unique = reverse &all_library();
                   13355:         return reverse %unique;
                   13356:     }
                   13357: 
1.841     albertel 13358:     sub get_servers {
1.852     albertel 13359: 	&load_hosts_tab() if (!$loaded);
                   13360: 
1.841     albertel 13361: 	my ($domain,$type) = @_;
                   13362: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   13363: 	                                          : %hostname;
                   13364: 	my %result;
1.842     albertel 13365: 	if (ref($domain) eq 'ARRAY') {
                   13366: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 13367: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 13368: 		    $result{$host} = $hostname;
                   13369: 		}
                   13370: 	    }
                   13371: 	} else {
                   13372: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   13373: 		if ($hostdom{$host} eq $domain) {
                   13374: 		    $result{$host} = $hostname;
                   13375: 		}
1.841     albertel 13376: 	    }
                   13377: 	}
                   13378: 	return %result;
                   13379:     }
1.845     albertel 13380: 
1.1062    droeschl 13381:     sub get_unique_servers {
                   13382:         my %unique = reverse &get_servers(@_);
                   13383: 	return reverse %unique;
                   13384:     }
                   13385: 
1.844     albertel 13386:     sub host_domain {
1.852     albertel 13387: 	&load_hosts_tab() if (!$loaded);
                   13388: 
1.844     albertel 13389: 	my ($lonid) = @_;
                   13390: 	return $hostdom{$lonid};
                   13391:     }
                   13392: 
1.841     albertel 13393:     sub all_domains {
1.852     albertel 13394: 	&load_hosts_tab() if (!$loaded);
                   13395: 
1.841     albertel 13396: 	my %seen;
                   13397: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   13398: 	return @uniq;
                   13399:     }
1.1074    raeburn  13400: 
                   13401:     sub internet_dom {
                   13402:         &load_hosts_tab() if (!$loaded);
                   13403: 
                   13404:         my ($lonid) = @_;
                   13405:         return $internetdom{$lonid};
                   13406:     }
1.1107    raeburn  13407: 
                   13408:     sub is_LC_dns {
                   13409:         &load_hosts_tab() if (!$loaded);
                   13410: 
                   13411:         my ($hostname) = @_;
                   13412:         return exists($LC_dns_serv{$hostname});
                   13413:     }
                   13414: 
1.1       albertel 13415: }
                   13416: 
1.847     albertel 13417: { 
                   13418:     my %iphost;
1.856     albertel 13419:     my %name_to_ip;
                   13420:     my %lonid_to_ip;
1.869     albertel 13421: 
1.847     albertel 13422:     sub get_hosts_from_ip {
                   13423: 	my ($ip) = @_;
                   13424: 	my %iphosts = &get_iphost();
                   13425: 	if (ref($iphosts{$ip})) {
                   13426: 	    return @{$iphosts{$ip}};
                   13427: 	}
                   13428: 	return;
1.839     albertel 13429:     }
1.864     albertel 13430:     
                   13431:     sub reset_hosts_ip_info {
                   13432: 	undef(%iphost);
                   13433: 	undef(%name_to_ip);
                   13434: 	undef(%lonid_to_ip);
                   13435:     }
1.856     albertel 13436: 
                   13437:     sub get_host_ip {
                   13438: 	my ($lonid) = @_;
                   13439: 	if (exists($lonid_to_ip{$lonid})) {
                   13440: 	    return $lonid_to_ip{$lonid};
                   13441: 	}
                   13442: 	my $name=&hostname($lonid);
                   13443:    	my $ip = gethostbyname($name);
                   13444: 	return if (!$ip || length($ip) ne 4);
                   13445: 	$ip=inet_ntoa($ip);
                   13446: 	$name_to_ip{$name}   = $ip;
                   13447: 	$lonid_to_ip{$lonid} = $ip;
                   13448: 	return $ip;
                   13449:     }
1.847     albertel 13450:     
                   13451:     sub get_iphost {
1.1293    raeburn  13452: 	my ($ignore_cache,$nocache) = @_;
1.894     albertel 13453: 
1.869     albertel 13454: 	if (!$ignore_cache) {
                   13455: 	    if (%iphost) {
                   13456: 		return %iphost;
                   13457: 	    }
                   13458: 	    my ($ip_info,$cached)=
                   13459: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   13460: 	    if ($cached) {
                   13461: 		%iphost      = %{$ip_info->[0]};
                   13462: 		%name_to_ip  = %{$ip_info->[1]};
                   13463: 		%lonid_to_ip = %{$ip_info->[2]};
                   13464: 		return %iphost;
                   13465: 	    }
                   13466: 	}
1.894     albertel 13467: 
                   13468: 	# get yesterday's info for fallback
                   13469: 	my %old_name_to_ip;
                   13470: 	my ($ip_info,$cached)=
                   13471: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   13472: 	if ($cached) {
                   13473: 	    %old_name_to_ip = %{$ip_info->[1]};
                   13474: 	}
                   13475: 
1.1293    raeburn  13476: 	my %name_to_host = &all_names($ignore_cache,$nocache);
1.888     albertel 13477: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 13478: 	    my $ip;
                   13479: 	    if (!exists($name_to_ip{$name})) {
                   13480: 		$ip = gethostbyname($name);
                   13481: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 13482: 		    if (defined($old_name_to_ip{$name})) {
                   13483: 			$ip = $old_name_to_ip{$name};
                   13484: 			&logthis("Can't find $name defaulting to old $ip");
                   13485: 		    } else {
                   13486: 			&logthis("Name $name no IP found");
                   13487: 			next;
                   13488: 		    }
                   13489: 		} else {
                   13490: 		    $ip=inet_ntoa($ip);
1.847     albertel 13491: 		}
                   13492: 		$name_to_ip{$name} = $ip;
                   13493: 	    } else {
                   13494: 		$ip = $name_to_ip{$name};
1.653     albertel 13495: 	    }
1.888     albertel 13496: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   13497: 		$lonid_to_ip{$id} = $ip;
                   13498: 	    }
                   13499: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 13500: 	}
1.1293    raeburn  13501:         unless ($nocache) {
                   13502: 	    &do_cache_new('iphost','iphost',
                   13503: 		          [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   13504: 		          48*60*60);
                   13505:         }
1.869     albertel 13506: 
1.847     albertel 13507: 	return %iphost;
1.598     albertel 13508:     }
                   13509: 
1.992     raeburn  13510:     #
                   13511:     #  Given a DNS returns the loncapa host name for that DNS 
                   13512:     # 
                   13513:     sub host_from_dns {
                   13514:         my ($dns) = @_;
                   13515:         my @hosts;
                   13516:         my $ip;
                   13517: 
1.993     raeburn  13518:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  13519:             $ip = $name_to_ip{$dns};
                   13520:         }
                   13521:         if (!$ip) {
                   13522:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   13523:             if (length($ip) == 4) { 
                   13524: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   13525:             }
                   13526:         }
                   13527:         if ($ip) {
                   13528: 	    @hosts = get_hosts_from_ip($ip);
                   13529: 	    return $hosts[0];
                   13530:         }
                   13531:         return undef;
1.986     foxr     13532:     }
1.992     raeburn  13533: 
1.1074    raeburn  13534:     sub get_internet_names {
                   13535:         my ($lonid) = @_;
                   13536:         return if ($lonid eq '');
                   13537:         my ($idnref,$cached)=
                   13538:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   13539:         if ($cached) {
                   13540:             return $idnref;
                   13541:         }
                   13542:         my $ip = &get_host_ip($lonid);
                   13543:         my @hosts = &get_hosts_from_ip($ip);
                   13544:         my %iphost = &get_iphost();
                   13545:         my (@idns,%seen);
                   13546:         foreach my $id (@hosts) {
                   13547:             my $dom = &host_domain($id);
                   13548:             my $prim_id = &domain($dom,'primary');
                   13549:             my $prim_ip = &get_host_ip($prim_id);
                   13550:             next if ($seen{$prim_ip});
                   13551:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   13552:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   13553:                     my $intdom = &internet_dom($id);
                   13554:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   13555:                         push(@idns,$intdom);
                   13556:                     }
                   13557:                 }
                   13558:             }
                   13559:             $seen{$prim_ip} = 1;
                   13560:         }
1.1219    raeburn  13561:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  13562:     }
                   13563: 
1.986     foxr     13564: }
                   13565: 
1.1079    raeburn  13566: sub all_loncaparevs {
1.1249    raeburn  13567:     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);
1.1079    raeburn  13568: }
                   13569: 
1.1227    raeburn  13570: # ---------------------------------------------------------- Read loncaparev table
                   13571: {
                   13572:     sub load_loncaparevs { 
                   13573:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
                   13574:             if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   13575:                 while (my $configline=<$config>) {
                   13576:                     chomp($configline);
                   13577:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   13578:                     $loncaparevs{$hostid}=$loncaparev;
                   13579:                 }
                   13580:                 close($config);
                   13581:             }
                   13582:         }
                   13583:     }
                   13584: }
                   13585: 
                   13586: # ---------------------------------------------------------- Read serverhostID table
                   13587: {
                   13588:     sub load_serverhomeIDs {
                   13589:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   13590:             if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   13591:                 while (my $configline=<$config>) {
                   13592:                     chomp($configline);
                   13593:                     my ($name,$id)=split(/:/,$configline);
                   13594:                     $serverhomeIDs{$name}=$id;
                   13595:                 }
                   13596:                 close($config);
                   13597:             }
                   13598:         }
                   13599:     }
                   13600: }
                   13601: 
                   13602: 
1.862     albertel 13603: BEGIN {
                   13604: 
                   13605: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   13606:     unless ($readit) {
                   13607: {
                   13608:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   13609:     %perlvar = (%perlvar,%{$configvars});
                   13610: }
                   13611: 
                   13612: 
1.1       albertel 13613: # ------------------------------------------------------ Read spare server file
                   13614: {
1.448     albertel 13615:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 13616: 
                   13617:     while (my $configline=<$config>) {
                   13618:        chomp($configline);
1.284     matthew  13619:        if ($configline) {
1.784     albertel 13620: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 13621: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 13622: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 13623:        }
                   13624:     }
1.448     albertel 13625:     close($config);
1.1       albertel 13626: }
1.11      www      13627: # ------------------------------------------------------------ Read permissions
                   13628: {
1.448     albertel 13629:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      13630: 
                   13631:     while (my $configline=<$config>) {
1.448     albertel 13632: 	chomp($configline);
                   13633: 	if ($configline) {
                   13634: 	    my ($role,$perm)=split(/ /,$configline);
                   13635: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   13636: 	}
1.11      www      13637:     }
1.448     albertel 13638:     close($config);
1.11      www      13639: }
                   13640: 
                   13641: # -------------------------------------------- Read plain texts for permissions
                   13642: {
1.448     albertel 13643:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      13644: 
                   13645:     while (my $configline=<$config>) {
1.448     albertel 13646: 	chomp($configline);
                   13647: 	if ($configline) {
1.742     raeburn  13648: 	    my ($short,@plain)=split(/:/,$configline);
                   13649:             %{$prp{$short}} = ();
                   13650: 	    if (@plain > 0) {
                   13651:                 $prp{$short}{'std'} = $plain[0];
                   13652:                 for (my $i=1; $i<@plain; $i++) {
                   13653:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   13654:                 }
                   13655:             }
1.448     albertel 13656: 	}
1.135     www      13657:     }
1.448     albertel 13658:     close($config);
1.135     www      13659: }
                   13660: 
                   13661: # ---------------------------------------------------------- Read package table
                   13662: {
1.448     albertel 13663:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      13664: 
                   13665:     while (my $configline=<$config>) {
1.483     albertel 13666: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 13667: 	chomp($configline);
                   13668: 	my ($short,$plain)=split(/:/,$configline);
                   13669: 	my ($pack,$name)=split(/\&/,$short);
                   13670: 	if ($plain ne '') {
                   13671: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   13672: 	    $packagetab{$short}=$plain; 
                   13673: 	}
1.11      www      13674:     }
1.448     albertel 13675:     close($config);
1.329     matthew  13676: }
                   13677: 
1.1073    raeburn  13678: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  13679: 
                   13680: &load_loncaparevs();
1.1073    raeburn  13681: 
1.1074    raeburn  13682: # ---------------------------------------------------------- Read serverhostID table
                   13683: 
1.1227    raeburn  13684: &load_serverhomeIDs();
                   13685: 
                   13686: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  13687: {
                   13688:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   13689:     if (-e $file) {
                   13690:         my $parser = HTML::LCParser->new($file);
                   13691:         while (my $token = $parser->get_token()) {
                   13692:             if ($token->[0] eq 'S') {
                   13693:                 my $item = $token->[1];
                   13694:                 my $name = $token->[2]{'name'};
                   13695:                 my $value = $token->[2]{'value'};
1.1285    raeburn  13696:                 my $valuematch = $token->[2]{'valuematch'};
1.1303    raeburn  13697:                 my $namematch = $token->[2]{'namematch'};
                   13698:                 if ($item eq 'parameter') {
                   13699:                     if (($namematch ne '') || (($name ne '') && ($value ne '' || $valuematch ne ''))) {
                   13700:                         my $release = $parser->get_text();
                   13701:                         $release =~ s/(^\s*|\s*$ )//gx;
                   13702:                         $needsrelease{$item.':'.$name.':'.$value.':'.$valuematch.':'.$namematch} = $release;
                   13703:                     }
                   13704:                 } elsif ($item ne '' && $name ne '') {
1.1079    raeburn  13705:                     my $release = $parser->get_text();
                   13706:                     $release =~ s/(^\s*|\s*$ )//gx;
1.1303    raeburn  13707:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
1.1079    raeburn  13708:                 }
                   13709:             }
                   13710:         }
                   13711:     }
1.1073    raeburn  13712: }
                   13713: 
1.1138    raeburn  13714: # ---------------------------------------------------------- Read managers table
                   13715: {
                   13716:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
                   13717:         if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
                   13718:             while (my $configline=<$config>) {
                   13719:                 chomp($configline);
                   13720:                 next if ($configline =~ /^\#/);
                   13721:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   13722:                     $managerstab{$configline} = 1;
                   13723:                 }
                   13724:             }
                   13725:             close($config);
                   13726:         }
                   13727:     }
                   13728: }
                   13729: 
1.329     matthew  13730: # ------------- set up temporary directory
                   13731: {
1.1117    foxr     13732:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  13733: 
1.11      www      13734: }
                   13735: 
1.794     albertel 13736: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   13737: 				'compress_threshold'=> 20_000,
                   13738:  			        });
1.185     www      13739: 
1.281     www      13740: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      13741: $dumpcount=0;
1.958     www      13742: $locknum=0;
1.22      www      13743: 
1.163     harris41 13744: &logtouch();
1.672     albertel 13745: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      13746: $readit=1;
1.564     albertel 13747:     {
                   13748: 	use integer;
                   13749: 	my $test=(2**32)+1;
1.568     albertel 13750: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 13751: 	&logthis(" Detected 64bit platform ($_64bit)");
                   13752:     }
1.195     www      13753: }
1.1       albertel 13754: }
1.179     www      13755: 
1.1       albertel 13756: 1;
1.191     harris41 13757: __END__
                   13758: 
1.243     albertel 13759: =pod
                   13760: 
1.191     harris41 13761: =head1 NAME
                   13762: 
1.243     albertel 13763: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 13764: 
                   13765: =head1 SYNOPSIS
                   13766: 
1.243     albertel 13767: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 13768: 
                   13769:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   13770: 
1.243     albertel 13771: Common parameters:
                   13772: 
                   13773: =over 4
                   13774: 
                   13775: =item *
                   13776: 
                   13777: $uname : an internal username (if $cname expecting a course Id specifically)
                   13778: 
                   13779: =item *
                   13780: 
                   13781: $udom : a domain (if $cdom expecting a course's domain specifically)
                   13782: 
                   13783: =item *
                   13784: 
                   13785: $symb : a resource instance identifier
                   13786: 
                   13787: =item *
                   13788: 
                   13789: $namespace : the name of a .db file that contains the data needed or
                   13790: being set.
                   13791: 
                   13792: =back
                   13793: 
1.394     bowersj2 13794: =head1 OVERVIEW
1.191     harris41 13795: 
1.394     bowersj2 13796: lonnet provides subroutines which interact with the
                   13797: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   13798: about classes, users, and resources.
1.243     albertel 13799: 
                   13800: For many of these objects you can also use this to store data about
                   13801: them or modify them in various ways.
1.191     harris41 13802: 
1.394     bowersj2 13803: =head2 Symbs
1.191     harris41 13804: 
1.394     bowersj2 13805: To identify a specific instance of a resource, LON-CAPA uses symbols
                   13806: or "symbs"X<symb>. These identifiers are built from the URL of the
                   13807: map, the resource number of the resource in the map, and the URL of
                   13808: the resource itself. The latter is somewhat redundant, but might help
                   13809: if maps change.
                   13810: 
                   13811: An example is
                   13812: 
                   13813:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   13814: 
                   13815: The respective map entry is
                   13816: 
                   13817:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   13818:   title="Problem 2">
                   13819:  </resource>
                   13820: 
                   13821: Symbs are used by the random number generator, as well as to store and
                   13822: restore data specific to a certain instance of for example a problem.
                   13823: 
                   13824: =head2 Storing And Retrieving Data
                   13825: 
                   13826: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   13827: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   13828: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   13829: is is the non-critical message twin of cstore. These functions are for
                   13830: handlers to store a perl hash to a user's permanent data space in an
                   13831: easy manner, and to retrieve it again on another call. It is expected
                   13832: that a handler would use this once at the beginning to retrieve data,
                   13833: and then again once at the end to send only the new data back.
                   13834: 
                   13835: The data is stored in the user's data directory on the user's
                   13836: homeserver under the ID of the course.
                   13837: 
                   13838: The hash that is returned by restore will have all of the previous
                   13839: value for all of the elements of the hash.
                   13840: 
                   13841: Example:
                   13842: 
                   13843:  #creating a hash
                   13844:  my %hash;
                   13845:  $hash{'foo'}='bar';
                   13846: 
                   13847:  #storing it
                   13848:  &Apache::lonnet::cstore(\%hash);
                   13849: 
                   13850:  #changing a value
                   13851:  $hash{'foo'}='notbar';
                   13852: 
                   13853:  #adding a new value
                   13854:  $hash{'bar'}='foo';
                   13855:  &Apache::lonnet::cstore(\%hash);
                   13856: 
                   13857:  #retrieving the hash
                   13858:  my %history=&Apache::lonnet::restore();
                   13859: 
                   13860:  #print the hash
                   13861:  foreach my $key (sort(keys(%history))) {
                   13862:    print("\%history{$key} = $history{$key}");
                   13863:  }
                   13864: 
                   13865: Will print out:
1.191     harris41 13866: 
1.394     bowersj2 13867:  %history{1:foo} = bar
                   13868:  %history{1:keys} = foo:timestamp
                   13869:  %history{1:timestamp} = 990455579
                   13870:  %history{2:bar} = foo
                   13871:  %history{2:foo} = notbar
                   13872:  %history{2:keys} = foo:bar:timestamp
                   13873:  %history{2:timestamp} = 990455580
                   13874:  %history{bar} = foo
                   13875:  %history{foo} = notbar
                   13876:  %history{timestamp} = 990455580
                   13877:  %history{version} = 2
                   13878: 
                   13879: Note that the special hash entries C<keys>, C<version> and
                   13880: C<timestamp> were added to the hash. C<version> will be equal to the
                   13881: total number of versions of the data that have been stored. The
                   13882: C<timestamp> attribute will be the UNIX time the hash was
                   13883: stored. C<keys> is available in every historical section to list which
                   13884: keys were added or changed at a specific historical revision of a
                   13885: hash.
                   13886: 
                   13887: B<Warning>: do not store the hash that restore returns directly. This
                   13888: will cause a mess since it will restore the historical keys as if the
                   13889: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 13890: 
1.394     bowersj2 13891: Calling convention:
1.191     harris41 13892: 
1.1225    raeburn  13893:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
1.1269    raeburn  13894:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$laststore);
1.191     harris41 13895: 
1.394     bowersj2 13896: For more detailed information, see lonnet specific documentation.
1.191     harris41 13897: 
1.394     bowersj2 13898: =head1 RETURN MESSAGES
1.191     harris41 13899: 
1.394     bowersj2 13900: =over 4
1.191     harris41 13901: 
1.394     bowersj2 13902: =item * B<con_lost>: unable to contact remote host
1.191     harris41 13903: 
1.394     bowersj2 13904: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   13905: when the connection is brought back up
1.191     harris41 13906: 
1.394     bowersj2 13907: =item * B<con_failed>: unable to contact remote host and unable to save message
                   13908: for later delivery
1.191     harris41 13909: 
1.967     bisitz   13910: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 13911: 
1.394     bowersj2 13912: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 13913: that was requested
1.191     harris41 13914: 
1.243     albertel 13915: =back
1.191     harris41 13916: 
1.243     albertel 13917: =head1 PUBLIC SUBROUTINES
1.191     harris41 13918: 
1.243     albertel 13919: =head2 Session Environment Functions
1.191     harris41 13920: 
1.243     albertel 13921: =over 4
1.191     harris41 13922: 
1.394     bowersj2 13923: =item * 
                   13924: X<appenv()>
1.949     raeburn  13925: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 13926: the user envirnoment file, and will be restored for each access this
1.620     albertel 13927: user makes during this session, also modifies the %env for the current
1.949     raeburn  13928: process. Optional rolesarrayref - if defined contains a reference to an array
                   13929: of roles which are exempt from the restriction on modifying user.role entries 
                   13930: in the user's environment.db and in %env.    
1.191     harris41 13931: 
                   13932: =item *
1.394     bowersj2 13933: X<delenv()>
1.987     raeburn  13934: B<delenv($delthis,$regexp)>: removes all items from the session
                   13935: environment file that begin with $delthis. If the 
                   13936: optional second arg - $regexp - is true, $delthis is treated as a 
                   13937: regular expression, otherwise \Q$delthis\E is used. 
                   13938: The values are also deleted from the current processes %env.
1.191     harris41 13939: 
1.795     albertel 13940: =item * get_env_multiple($name) 
                   13941: 
                   13942: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   13943: values may be defined and end up as an array ref.
                   13944: 
                   13945: returns an array of values
                   13946: 
1.243     albertel 13947: =back
                   13948: 
                   13949: =head2 User Information
1.191     harris41 13950: 
1.243     albertel 13951: =over 4
1.191     harris41 13952: 
                   13953: =item *
1.394     bowersj2 13954: X<queryauthenticate()>
                   13955: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 13956: authentication scheme
                   13957: 
                   13958: =item *
1.394     bowersj2 13959: X<authenticate()>
1.1073    raeburn  13960: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 13961: authenticate user from domain's lib servers (first use the current
                   13962: one). C<$upass> should be the users password.
1.1073    raeburn  13963: $checkdefauth is optional (value is 1 if a check should be made to
                   13964:    authenticate user using default authentication method, and allow
                   13965:    account creation if username does not have account in the domain).
                   13966: $clientcancheckhost is optional (value is 1 if checking whether the
                   13967:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 13968: 
                   13969: =item *
1.394     bowersj2 13970: X<homeserver()>
                   13971: B<homeserver($uname,$udom)>: find the server which has
                   13972: the user's directory and files (there must be only one), this caches
                   13973: the answer, and also caches if there is a borken connection.
1.191     harris41 13974: 
                   13975: =item *
1.394     bowersj2 13976: X<idget()>
1.1300    raeburn  13977: B<idget($udom,$idsref,$namespace)>: find the usernames behind either 
                   13978: a list of student/employee IDs or clicker IDs
                   13979: (student/employee IDs are a unique resource in a domain, there must be 
                   13980: only 1 ID per username, and only 1 username per ID in a specific domain).
                   13981: clickerIDs are not necessarily unique, as students might share clickers.
                   13982: (returns hash: id=>name,id=>name)
1.191     harris41 13983: 
                   13984: =item *
1.394     bowersj2 13985: X<idrget()>
                   13986: B<idrget($udom,@unames)>: find the IDs behind a list of
                   13987: usernames (returns hash: name=>id,name=>id)
1.191     harris41 13988: 
                   13989: =item *
1.394     bowersj2 13990: X<idput()>
1.1300    raeburn  13991: B<idput($udom,$idsref,$uhome,$namespace)>: store away a list of 
                   13992: names and associated student/employee IDs or clicker IDs.
                   13993: 
                   13994: =item *
                   13995: X<iddel()>
                   13996: B<iddel($udom,$idshashref,$uhome,$namespace)>: delete unwanted 
                   13997: student/employee ID or clicker ID username look-ups from domain.
                   13998: The homeserver ($uhome) and namespace ($namespace) are optional.
                   13999: If no $uhome is provided, it will be determined usig &homeserver()
                   14000: for each user.  If no $namespace is provided, the default is ids.
                   14001: 
                   14002: =item *
                   14003: X<updateclickers()>
                   14004: B<updateclickers($udom,$action,$idshashref,$uhome,$critical)>: update 
                   14005: clicker ID-to-username look-ups in clickers.db on library server.
                   14006: Permitted actions are add or del (i.e., add or delete). The 
                   14007: clickers.db contains clickerID as keys (escaped), and each corresponding
                   14008: value is an escaped comma-separated list of usernames (for whom the
                   14009: library server is the homeserver), who registered that particular ID.
                   14010: If $critical is true, the update will be sent via &critical, otherwise
                   14011: &reply() will be used.
1.191     harris41 14012: 
                   14013: =item *
1.394     bowersj2 14014: X<rolesinit()>
1.1169    droeschl 14015: B<rolesinit($udom,$username)>: get user privileges.
                   14016: returns user role, first access and timer interval hashes
1.243     albertel 14017: 
                   14018: =item *
1.1171    droeschl 14019: X<privileged()>
                   14020: B<privileged($username,$domain)>: returns a true if user has a
                   14021: privileged and active role (i.e. su or dc), false otherwise.
                   14022: 
                   14023: =item *
1.551     albertel 14024: X<getsection()>
                   14025: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 14026: course $cname, return section name/number or '' for "not in course"
                   14027: and '-1' for "no section"
                   14028: 
                   14029: =item *
1.394     bowersj2 14030: X<userenvironment()>
                   14031: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 14032: passed in @what from the requested user's environment, returns a hash
                   14033: 
1.858     raeburn  14034: =item * 
                   14035: X<userlog_query()>
1.859     albertel 14036: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   14037: activity.log file. %filters defines filters applied when parsing the
                   14038: log file. These can be start or end timestamps, or the type of action
                   14039: - log to look for Login or Logout events, check for Checkin or
                   14040: Checkout, role for role selection. The response is in the form
                   14041: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   14042: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  14043: 
1.243     albertel 14044: =back
                   14045: 
                   14046: =head2 User Roles
                   14047: 
                   14048: =over 4
                   14049: 
                   14050: =item *
                   14051: 
1.1284    raeburn  14052: allowed($priv,$uri,$symb,$role,$clientip,$noblockcheck) : check for a user privilege; 
                   14053: returns codes for allowed actions.
                   14054: 
                   14055: The first argument is required, all others are optional.
                   14056: 
                   14057: $priv is the privilege being checked.
                   14058: $uri contains additional information about what is being checked for access (e.g.,
                   14059: URL, course ID etc.). 
                   14060: $symb is the unique resource instance identifier in a course; if needed,
                   14061: but not provided, it will be retrieved via a call to &symbread(). 
                   14062: $role is the role for which a priv is being checked (only used if priv is evb). 
                   14063: $clientip is the user's IP address (only used when checking for access to portfolio 
                   14064: files).
                   14065: $noblockcheck, if true, skips calls to &has_comm_blocking() for the bre priv. This 
                   14066: prevents recursive calls to &allowed.
                   14067: 
1.243     albertel 14068:  F: full access
                   14069:  U,I,K: authentication modes (cxx only)
                   14070:  '': forbidden
                   14071:  1: user needs to choose course
                   14072:  2: browse allowed
1.766     albertel 14073:  A: passphrase authentication needed
1.1284    raeburn  14074:  B: access temporarily blocked because of a blocking event in a course.
1.243     albertel 14075: 
                   14076: =item *
                   14077: 
1.1192    raeburn  14078: constructaccess($url,$setpriv) : check for access to construction space URL
                   14079: 
                   14080: See if the owner domain and name in the URL match those in the
                   14081: expected environment.  If so, return three element list
                   14082: ($ownername,$ownerdomain,$ownerhome).
                   14083: 
                   14084: Otherwise return the null string.
                   14085: 
                   14086: If second argument 'setpriv' is true, it assigns the privileges,
                   14087: and returns the same three element list, unless the owner has
                   14088: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   14089: in which case the null string is returned.
                   14090: 
                   14091: =item *
                   14092: 
1.1326    raeburn  14093: definerole($rolename,$sysrole,$domrole,$courole,$uname,$udom) : define role;
                   14094: define a custom role rolename set privileges in format of lonTabs/roles.tab
                   14095: for system, domain, and course level. $uname and $udom are optional (current
                   14096: user's username and domain will be used when either of $uname or $udom are absent.
1.243     albertel 14097: 
                   14098: =item *
                   14099: 
1.988     raeburn  14100: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   14101: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  14102: $type is Course (default) or Community.
1.988     raeburn  14103: If $forcedefault evaluates to true, text returned will be default 
                   14104: text for $type. Otherwise, if this is a course, the text returned 
                   14105: will be a custom name for the role (if defined in the course's 
                   14106: environment).  If no custom name is defined the default is returned.
                   14107:    
1.832     raeburn  14108: =item *
                   14109: 
1.1219    raeburn  14110: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  14111: All arguments are optional. Returns a hash of a roles, either for
                   14112: co-author/assistant author roles for a user's Construction Space
1.906     albertel 14113: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  14114: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   14115: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   14116: For each key, value is set to colon-separated start and end times for
                   14117: the role.  If no username and domain are specified, will default to
1.934     raeburn  14118: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  14119: of role statuses (active, future or previous), roles 
                   14120: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   14121: to restrict the list of roles reported. If no array ref is 
                   14122: provided for types, will default to return only active roles.
1.834     albertel 14123: 
1.1195    raeburn  14124: =item *
                   14125: 
                   14126: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  14127: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   14128: 
                   14129: Additional optional arguments are: $type (if role checking is to be restricted 
                   14130: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  14131: available roles) or future (roles available in the future), and
                   14132: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  14133: have active Domain Coordinator role in course's domain or in additional
                   14134: domains (specified in 'Domains to check for privileged users' in course
                   14135: environment -- set via:  Course Settings -> Classlists and staff listing).
                   14136: 
                   14137: =item *
                   14138: 
                   14139: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   14140: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   14141: $possdomains and $possroles are optional array refs -- to domains to check and
                   14142: roles to check.  If $possdomains is not specified, a dump will be done of the
                   14143: users' roles.db to check for a dc or su role in any domain. This can be
                   14144: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   14145: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   14146: this then allows &privileged_by_domain() to be used, which caches the identity
                   14147: of privileged users, eliminating the need for repeated calls to &dump().
                   14148: 
                   14149: =item *
                   14150: 
                   14151: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   14152: where the outer hash keys are domains specified in the $possdomains array ref,
                   14153: next inner hash keys are privileged roles specified in the $roles array ref,
                   14154: and the innermost hash contains key = value pairs for username:domain = end:start
                   14155: for active or future "privileged" users with that role in that domain. To avoid
                   14156: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   14157: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  14158: 
1.243     albertel 14159: =back
                   14160: 
                   14161: =head2 User Modification
                   14162: 
                   14163: =over 4
                   14164: 
                   14165: =item *
                   14166: 
1.957     raeburn  14167: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 14168: user for the level given by URL.  Optional start and end dates (leave empty
                   14169: string or zero for "no date")
1.191     harris41 14170: 
                   14171: =item *
                   14172: 
1.243     albertel 14173: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   14174: change a users, password, possible return values are: ok,
                   14175: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   14176: refused
1.191     harris41 14177: 
                   14178: =item *
                   14179: 
1.243     albertel 14180: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 14181: 
                   14182: =item *
                   14183: 
1.1058    raeburn  14184: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   14185:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   14186: 
                   14187: will update user information (firstname,middlename,lastname,generation,
                   14188: permanentemail), and if forceid is true, student/employee ID also.
                   14189: A user's institutional affiliation(s) can also be updated.
                   14190: User information fields will not be overwritten with empty entries 
                   14191: unless the field is included in the $candelete array reference.
                   14192: This array is included when a single user is modified via "Manage Users",
                   14193: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 14194: 
                   14195: =item *
                   14196: 
1.286     matthew  14197: modifystudent
                   14198: 
1.957     raeburn  14199: modify a student's enrollment and identification information.
1.1235    raeburn  14200: The course id is resolved based on the current user's environment.  
                   14201: This means the invoking user must be a course coordinator or otherwise
1.286     matthew  14202: associated with a course.
                   14203: 
1.297     matthew  14204: This call is essentially a wrapper for lonnet::modifyuser and
                   14205: lonnet::modify_student_enrollment
1.286     matthew  14206: 
                   14207: Inputs: 
                   14208: 
                   14209: =over 4
                   14210: 
1.957     raeburn  14211: =item B<$udom> Student's loncapa domain
1.286     matthew  14212: 
1.957     raeburn  14213: =item B<$uname> Student's loncapa login name
1.286     matthew  14214: 
1.964     bisitz   14215: =item B<$uid> Student/Employee ID
1.286     matthew  14216: 
1.957     raeburn  14217: =item B<$umode> Student's authentication mode
1.286     matthew  14218: 
1.957     raeburn  14219: =item B<$upass> Student's password
1.286     matthew  14220: 
1.957     raeburn  14221: =item B<$first> Student's first name
1.286     matthew  14222: 
1.957     raeburn  14223: =item B<$middle> Student's middle name
1.286     matthew  14224: 
1.957     raeburn  14225: =item B<$last> Student's last name
1.286     matthew  14226: 
1.957     raeburn  14227: =item B<$gene> Student's generation
1.286     matthew  14228: 
1.957     raeburn  14229: =item B<$usec> Student's section in course
1.286     matthew  14230: 
                   14231: =item B<$end> Unix time of the roles expiration
                   14232: 
                   14233: =item B<$start> Unix time of the roles start date
                   14234: 
                   14235: =item B<$forceid> If defined, allow $uid to be changed
                   14236: 
                   14237: =item B<$desiredhome> server to use as home server for student
                   14238: 
1.957     raeburn  14239: =item B<$email> Student's permanent e-mail address
                   14240: 
                   14241: =item B<$type> Type of enrollment (auto or manual)
                   14242: 
1.963     raeburn  14243: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   14244: 
                   14245: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  14246: 
1.963     raeburn  14247: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  14248: 
1.963     raeburn  14249: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  14250: 
1.1216    raeburn  14251: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   14252: 
                   14253: =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.
1.957     raeburn  14254: 
1.286     matthew  14255: =back
1.297     matthew  14256: 
                   14257: =item *
                   14258: 
                   14259: modify_student_enrollment
                   14260: 
1.1235    raeburn  14261: Change a student's enrollment status in a class.  The environment variable
1.297     matthew  14262: 'role.request.course' must be defined for this function to proceed.
                   14263: 
                   14264: Inputs:
                   14265: 
                   14266: =over 4
                   14267: 
1.1235    raeburn  14268: =item $udom, student's domain
1.297     matthew  14269: 
1.1235    raeburn  14270: =item $uname, student's name
1.297     matthew  14271: 
1.1235    raeburn  14272: =item $uid, student's user id
1.297     matthew  14273: 
1.1235    raeburn  14274: =item $first, student's first name
1.297     matthew  14275: 
                   14276: =item $middle
                   14277: 
                   14278: =item $last
                   14279: 
                   14280: =item $gene
                   14281: 
                   14282: =item $usec
                   14283: 
                   14284: =item $end
                   14285: 
                   14286: =item $start
                   14287: 
1.957     raeburn  14288: =item $type
                   14289: 
                   14290: =item $locktype
                   14291: 
                   14292: =item $cid
                   14293: 
                   14294: =item $selfenroll
                   14295: 
                   14296: =item $context
                   14297: 
1.1216    raeburn  14298: =item $credits, number of credits student will earn from this class
                   14299: 
1.1314    raeburn  14300: =item $instsec, institutional course section code for student
                   14301: 
1.297     matthew  14302: =back
                   14303: 
1.191     harris41 14304: 
                   14305: =item *
                   14306: 
1.243     albertel 14307: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   14308: custom role; give a custom role to a user for the level given by URL.  Specify
                   14309: name and domain of role author, and role name
1.191     harris41 14310: 
                   14311: =item *
                   14312: 
1.243     albertel 14313: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 14314: 
                   14315: =item *
                   14316: 
1.243     albertel 14317: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   14318: 
                   14319: =back
                   14320: 
                   14321: =head2 Course Infomation
                   14322: 
                   14323: =over 4
1.191     harris41 14324: 
                   14325: =item *
                   14326: 
1.1118    foxr     14327: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 14328: specified course id, including all environment settings for the
                   14329: course, the description of the course will be in the hash under the
                   14330: key 'description'
1.191     harris41 14331: 
1.1118    foxr     14332: $options is an optional parameter that if supplied is a hash reference that controls
                   14333: what how this function works.  It has the following key/values:
                   14334: 
                   14335: =over 4
                   14336: 
                   14337: =item freshen_cache
                   14338: 
                   14339: If defined, and the environment cache for the course is valid, it is 
                   14340: returned in the returned hash.
                   14341: 
                   14342: =item one_time
                   14343: 
                   14344: If defined, the last cache time is set to _now_
                   14345: 
                   14346: =item user
                   14347: 
                   14348: If defined, the supplied username is used instead of the current user.
                   14349: 
                   14350: 
                   14351: =back
                   14352: 
1.191     harris41 14353: =item *
                   14354: 
1.624     albertel 14355: resdata($name,$domain,$type,@which) : request for current parameter
                   14356: setting for a specific $type, where $type is either 'course' or 'user',
                   14357: @what should be a list of parameters to ask about. This routine caches
1.1235    raeburn  14358: answers for 10 minutes.
1.243     albertel 14359: 
1.877     foxr     14360: =item *
                   14361: 
                   14362: get_courseresdata($courseid, $domain) : dump the entire course resource
                   14363: data base, returning a hash that is keyed by the resource name and has
                   14364: values that are the resource value.  I believe that the timestamps and
                   14365: versions are also returned.
                   14366: 
1.1236    raeburn  14367: get_numsuppfiles($cnum,$cdom) : retrieve number of files in a course's
                   14368: supplemental content area. This routine caches the number of files for 
                   14369: 10 minutes.
                   14370: 
1.243     albertel 14371: =back
                   14372: 
                   14373: =head2 Course Modification
                   14374: 
                   14375: =over 4
1.191     harris41 14376: 
                   14377: =item *
                   14378: 
1.243     albertel 14379: writecoursepref($courseid,%prefs) : write preferences (environment
                   14380: database) for a course
1.191     harris41 14381: 
                   14382: =item *
                   14383: 
1.1011    raeburn  14384: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   14385: 
                   14386: =item *
                   14387: 
1.1038    raeburn  14388: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 14389: 
1.1167    droeschl 14390: =item *
                   14391: 
                   14392: is_course($courseid), is_course($cdom, $cnum)
                   14393: 
                   14394: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   14395: two component version $cdom, $cnum. It checks if the specified course exists.
                   14396: 
                   14397: Returns:
                   14398:     undef if the course doesn't exist, otherwise
                   14399:     in scalar context the combined courseid.
                   14400:     in list context the two components of the course identifier, domain and 
                   14401:     courseid.    
                   14402: 
1.243     albertel 14403: =back
                   14404: 
                   14405: =head2 Resource Subroutines
                   14406: 
                   14407: =over 4
1.191     harris41 14408: 
                   14409: =item *
                   14410: 
1.243     albertel 14411: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 14412: 
                   14413: =item *
                   14414: 
1.243     albertel 14415: repcopy($filename) : subscribes to the requested file, and attempts to
                   14416: replicate from the owning library server, Might return
1.607     raeburn  14417: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   14418: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 14419: resource. Expects the local filesystem pathname
                   14420: (/home/httpd/html/res/....)
                   14421: 
                   14422: =back
                   14423: 
                   14424: =head2 Resource Information
                   14425: 
                   14426: =over 4
1.191     harris41 14427: 
                   14428: =item *
                   14429: 
1.1228    raeburn  14430: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   14431: and returns the value of a variety of different possible values,
                   14432: $varname should be a request string, and the other parameters can be
                   14433: used to specify who and what one is asking about. Ordinarily, $cid 
                   14434: does not need to be specified, as it is retrived from 
                   14435: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   14436: within lonuserstate::loadmap() when initializing a course, before
                   14437: $env{'request.course.id'} has been set, so it needs to be provided
                   14438: in that one case.
1.243     albertel 14439: 
                   14440: Possible values for $varname are environment.lastname (or other item
                   14441: from the envirnment hash), user.name (or someother aspect about the
                   14442: user), resource.0.maxtries (or some other part and parameter of a
                   14443: resource)
1.204     albertel 14444: 
                   14445: =item *
                   14446: 
1.243     albertel 14447: directcondval($number) : get current value of a condition; reads from a state
                   14448: string
1.204     albertel 14449: 
                   14450: =item *
                   14451: 
1.243     albertel 14452: condval($condidx) : value of condition index based on state
1.204     albertel 14453: 
                   14454: =item *
                   14455: 
1.243     albertel 14456: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   14457: resource's metadata, $what should be either a specific key, or either
                   14458: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   14459: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   14460: 
                   14461: this function automatically caches all requests
1.191     harris41 14462: 
                   14463: =item *
                   14464: 
1.243     albertel 14465: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   14466: network of library servers; returns file handle of where SQL and regex results
                   14467: will be stored for query
1.191     harris41 14468: 
                   14469: =item *
                   14470: 
1.1282    raeburn  14471: symbread($filename,$donotrecurse,$ignorecachednull,$checkforblock,$possibles) : 
                   14472: return symbolic list entry (all arguments optional). 
                   14473: 
                   14474: Args: filename is the filename (including path) for the file for which a symb 
                   14475: is required; donotrecurse, if true will prevent calls to allowed() being made 
                   14476: to check access status if more than one resource was found in the bighash 
                   14477: (see rev. 1.249) to avoid an infinite loop if an ambiguous resource is part of 
                   14478: a randompick); ignorecachednull, if true will prevent a symb of '' being 
                   14479: returned if $env{$cache_str} is defined as ''; checkforblock if true will
                   14480: cause possible symbs to be checked to determine if they are subject to content
                   14481: blocking, if so they will not be included as possible symbs; possibles is a
                   14482: ref to a hash, which, as a side effect, will be populated with all possible 
                   14483: symbs (content blocking not tested).
                   14484:  
1.243     albertel 14485: returns the data handle
1.191     harris41 14486: 
                   14487: =item *
                   14488: 
1.1190    raeburn  14489: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   14490: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 14491: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  14492: on failure, user must be in a course, as it assumes the existence of
                   14493: the course initial hash, and uses $env('request.course.id'}.  The third
                   14494: arg is an optional reference to a scalar.  If this arg is passed in the 
                   14495: call to symbverify, it will be set to 1 if the symb has been set to be 
                   14496: encrypted; otherwise it will be null.  
1.191     harris41 14497: 
                   14498: =item *
                   14499: 
1.243     albertel 14500: symbclean($symb) : removes versions numbers from a symb, returns the
                   14501: cleaned symb
1.191     harris41 14502: 
                   14503: =item *
                   14504: 
1.243     albertel 14505: is_on_map($uri) : checks if the $uri is somewhere on the current
                   14506: course map, user must be in a course for it to work.
1.191     harris41 14507: 
                   14508: =item *
                   14509: 
1.243     albertel 14510: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 14511: 
                   14512: =item *
                   14513: 
1.243     albertel 14514: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   14515: a random seed, all arguments are optional, if they aren't sent it uses the
                   14516: environment to derive them. Note: if symb isn't sent and it can't get one
                   14517: from &symbread it will use the current time as its return value
1.191     harris41 14518: 
                   14519: =item *
                   14520: 
1.243     albertel 14521: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   14522: unfakeable, receipt
1.191     harris41 14523: 
                   14524: =item *
                   14525: 
1.620     albertel 14526: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 14527: 
                   14528: =item *
                   14529: 
1.243     albertel 14530: countacc($url) : count the number of accesses to a given URL
1.191     harris41 14531: 
                   14532: =item *
                   14533: 
1.243     albertel 14534: 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
1.191     harris41 14535: 
                   14536: =item *
                   14537: 
1.243     albertel 14538: 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)
1.191     harris41 14539: 
                   14540: =item *
                   14541: 
1.243     albertel 14542: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 14543: 
                   14544: =item *
                   14545: 
1.243     albertel 14546: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   14547: forcing spreadsheet to reevaluate the resource scores next time.
                   14548: 
1.1195    raeburn  14549: =item * 
                   14550: 
1.1196    raeburn  14551: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   14552: when viewing in course context.
1.1195    raeburn  14553: 
1.1196    raeburn  14554:  input: six args -- filename (decluttered), course number, course domain,
                   14555:                     url, symb (if registered) and group (if this is a 
                   14556:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  14557: 
1.1196    raeburn  14558:  output: array of five scalars --
1.1195    raeburn  14559:          $cfile -- url for file editing if editable on current server
                   14560:          $home -- homeserver of resource (i.e., for author if published,
                   14561:                                           or course if uploaded.).
                   14562:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  14563:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   14564:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  14565: 
                   14566: =item *
                   14567: 
                   14568: is_course_upload($file,$cnum,$cdom)
                   14569: 
                   14570: Used in course context to determine if current file was uploaded to 
                   14571: the course (i.e., would be found in /userfiles/docs on the course's 
                   14572: homeserver.
                   14573: 
                   14574:   input: 3 args -- filename (decluttered), course number and course domain.
                   14575:   output: boolean -- 1 if file was uploaded.
                   14576: 
1.243     albertel 14577: =back
                   14578: 
                   14579: =head2 Storing/Retreiving Data
                   14580: 
                   14581: =over 4
1.191     harris41 14582: 
                   14583: =item *
                   14584: 
1.1269    raeburn  14585: store($storehash,$symb,$namespace,$udom,$uname,$laststore) : stores hash
                   14586: permanently for this url; hashref needs to be given and should be a \%hashname;
                   14587: the remaining args aren't required and if they aren't passed or are '' they will
                   14588: be derived from the env (with the exception of $laststore, which is an 
                   14589: optional arg used when a user's submission is stored in grading).
                   14590: $laststore is $version=$timestamp, where $version is the most recent version
                   14591: number retrieved for the corresponding $symb in the $namespace db file, and
                   14592: $timestamp is the timestamp for that transaction (UNIX time).
                   14593: $laststore is currently only passed when cstore() is called by 
                   14594: structuretags::finalize_storage().
1.191     harris41 14595: 
                   14596: =item *
                   14597: 
1.1269    raeburn  14598: cstore($storehash,$symb,$namespace,$udom,$uname,$laststore) : same as store
                   14599: but uses critical subroutine
1.191     harris41 14600: 
                   14601: =item *
                   14602: 
1.243     albertel 14603: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   14604: all args are optional
1.191     harris41 14605: 
                   14606: =item *
                   14607: 
1.717     albertel 14608: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   14609: dumps the complete (or key matching regexp) namespace into a hash
                   14610: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   14611: normally &store()ed into
                   14612: 
                   14613: $range should be either an integer '100' (give me the first 100
                   14614:                                            matching records)
                   14615:               or be  two integers sperated by a - with no spaces
                   14616:                  '30-50' (give me the 30th through the 50th matching
                   14617:                           records)
                   14618: 
                   14619: 
                   14620: =item *
                   14621: 
1.1269    raeburn  14622: putstore($namespace,$symb,$version,$storehash,$udomain,$uname,$tolog) :
1.717     albertel 14623: replaces a &store() version of data with a replacement set of data
                   14624: for a particular resource in a namespace passed in the $storehash hash 
1.1269    raeburn  14625: reference. If $tolog is true, the transaction is logged in the courselog
                   14626: with an action=PUTSTORE.
1.717     albertel 14627: 
                   14628: =item *
                   14629: 
1.243     albertel 14630: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   14631: works very similar to store/cstore, but all data is stored in a
                   14632: temporary location and can be reset using tmpreset, $storehash should
                   14633: be a hash reference, returns nothing on success
1.191     harris41 14634: 
                   14635: =item *
                   14636: 
1.243     albertel 14637: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   14638: similar to restore, but all data is stored in a temporary location and
                   14639: can be reset using tmpreset. Returns a hash of values on success,
                   14640: error string otherwise.
1.191     harris41 14641: 
                   14642: =item *
                   14643: 
1.243     albertel 14644: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   14645: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 14646: 
                   14647: =item *
                   14648: 
1.243     albertel 14649: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   14650: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 14651: 
                   14652: =item *
                   14653: 
1.243     albertel 14654: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   14655: namesp ($udom and $uname are optional)
1.191     harris41 14656: 
                   14657: =item *
                   14658: 
1.702     albertel 14659: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 14660: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 14661: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  14662: 
1.702     albertel 14663: $range should be either an integer '100' (give me the first 100
                   14664:                                            matching records)
                   14665:               or be  two integers sperated by a - with no spaces
                   14666:                  '30-50' (give me the 30th through the 50th matching
                   14667:                           records)
1.449     matthew  14668: =item *
                   14669: 
                   14670: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   14671: $store can be a scalar, an array reference, or if the amount to be 
                   14672: incremented is > 1, a hash reference.
                   14673: 
                   14674: ($udom and $uname are optional)
1.191     harris41 14675: 
                   14676: =item *
                   14677: 
1.243     albertel 14678: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   14679: ($udom and $uname are optional)
1.191     harris41 14680: 
                   14681: =item *
                   14682: 
1.243     albertel 14683: cput($namespace,$storehash,$udom,$uname) : critical put
                   14684: ($udom and $uname are optional)
1.191     harris41 14685: 
                   14686: =item *
                   14687: 
1.748     albertel 14688: newput($namespace,$storehash,$udom,$uname) :
                   14689: 
                   14690: Attempts to store the items in the $storehash, but only if they don't
                   14691: currently exist, if this succeeds you can be certain that you have 
                   14692: successfully created a new key value pair in the $namespace db.
                   14693: 
                   14694: 
                   14695: Args:
                   14696:  $namespace: name of database to store values to
                   14697:  $storehash: hashref to store to the db
                   14698:  $udom: (optional) domain of user containing the db
                   14699:  $uname: (optional) name of user caontaining the db
                   14700: 
                   14701: Returns:
                   14702:  'ok' -> succeeded in storing all keys of $storehash
                   14703:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   14704:                         least <key> already existed in the db (other
                   14705:                         requested keys may also already exist)
1.967     bisitz   14706:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 14707:  'con_lost' -> unable to contact request server
                   14708:  'refused' -> action was not allowed by remote machine
                   14709: 
                   14710: 
                   14711: =item *
                   14712: 
1.243     albertel 14713: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   14714: reference filled in from namesp (encrypts the return communication)
                   14715: ($udom and $uname are optional)
1.191     harris41 14716: 
                   14717: =item *
                   14718: 
1.243     albertel 14719: log($udom,$name,$home,$message) : write to permanent log for user; use
                   14720: critical subroutine
                   14721: 
1.806     raeburn  14722: =item *
                   14723: 
1.860     raeburn  14724: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   14725: array reference filled in from namespace found in domain level on either
                   14726: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  14727: 
                   14728: =item *
                   14729: 
1.860     raeburn  14730: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   14731: domain level either on specified domain server ($uhome) or primary domain 
                   14732: server ($udom and $uhome are optional)
1.806     raeburn  14733: 
1.943     raeburn  14734: =item * 
                   14735: 
1.1240    raeburn  14736: get_domain_defaults($target_domain,$ignore_cache) : returns hash with defaults 
                   14737: for: authentication, language, quotas, timezone, date locale, and portal URL in
                   14738: the target domain.
                   14739: 
                   14740: May also include additional key => value pairs for the following groups:
                   14741: 
                   14742: =over
                   14743: 
                   14744: =item
                   14745: disk quotas (MB allocated by default to portfolios and authoring spaces).
                   14746: 
                   14747: =over
                   14748: 
                   14749: =item defaultquota, authorquota
                   14750: 
                   14751: =back
                   14752: 
                   14753: =item
                   14754: tools (availability of aboutme page, blog, webDAV access for authoring spaces,
                   14755: portfolio for users).
                   14756: 
                   14757: =over
                   14758: 
                   14759: =item
                   14760: aboutme, blog, webdav, portfolio
                   14761: 
                   14762: =back
                   14763: 
                   14764: =item
                   14765: requestcourses: ability to request courses, and how requests are processed.
                   14766: 
                   14767: =over
                   14768: 
                   14769: =item
1.1305    raeburn  14770: official, unofficial, community, textbook, placement
1.1240    raeburn  14771: 
                   14772: =back
                   14773: 
                   14774: =item
                   14775: inststatus: types of institutional affiliation, and order in which they are displayed.
                   14776: 
                   14777: =over
                   14778: 
                   14779: =item
1.1256    raeburn  14780: inststatustypes, inststatusorder, inststatusguest
1.1240    raeburn  14781: 
                   14782: =back
                   14783: 
                   14784: =item
                   14785: coursedefaults: can PDF forms can be created, default credits for courses, default quotas (MB)
                   14786: for course's uploaded content.
                   14787: 
                   14788: =over
                   14789: 
                   14790: =item
1.1246    raeburn  14791: canuse_pdfforms, officialcredits, unofficialcredits, textbookcredits, officialquota, unofficialquota, 
1.1305    raeburn  14792: communityquota, textbookquota, placementquota
1.1240    raeburn  14793: 
                   14794: =back
                   14795: 
                   14796: =item
                   14797: usersessions: set options for hosting of your users in other domains, and hosting of users from other domains
                   14798: on your servers.
                   14799: 
                   14800: =over
                   14801: 
                   14802: =item 
                   14803: remotesessions, hostedsessions
                   14804: 
                   14805: =back
                   14806: 
                   14807: =back
                   14808: 
                   14809: In cases where a domain coordinator has never used the "Set Domain Configuration"
                   14810: utility to create a configuration.db file on a domain's primary library server 
                   14811: only the following domain defaults: auth_def, auth_arg_def, lang_def
                   14812: -- corresponding values are authentication type (internal, krb4, krb5,
                   14813: or localauth), initial password or a kerberos realm, language (e.g., en-us) -- 
                   14814: will be available. Values are retrieved from cache (if current), unless the
                   14815: optional $ignore_cache arg is true, or from domain's configuration.db (if available),
                   14816: or lastly from values in lonTabs/dns_domain,tab, or lonTabs/domain.tab.
                   14817: 
                   14818: Typical usage:
1.943     raeburn  14819: 
1.1240    raeburn  14820: %domdefaults = &get_domain_defaults($target_domain);
1.943     raeburn  14821: 
1.243     albertel 14822: =back
                   14823: 
                   14824: =head2 Network Status Functions
                   14825: 
                   14826: =over 4
1.191     harris41 14827: 
                   14828: =item *
                   14829: 
1.1137    raeburn  14830: dirlist() : return directory list based on URI (first arg).
                   14831: 
                   14832: Inputs: 1 required, 5 optional.
                   14833: 
                   14834: =over
                   14835: 
                   14836: =item 
                   14837: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   14838: 
                   14839: =item
                   14840: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   14841: 
                   14842: =item
                   14843: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   14844: 
                   14845: =item
                   14846: $getpropath - boolean: 1 if prepend path using &propath(). 
                   14847: 
                   14848: =item
                   14849: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   14850: 
                   14851: =item 
                   14852: $alternateRoot - path to prepend in place of path from $uri.
                   14853: 
                   14854: =back
                   14855: 
                   14856: Returns: Array of up to two items.
                   14857: 
                   14858: =over
                   14859: 
                   14860: a reference to an array of files/subdirectories
                   14861: 
                   14862: =over
                   14863: 
                   14864: Each element in the array of files/subdirectories is a & separated list of
                   14865: item name and the result of running stat on the item.  If dirlist was requested
                   14866: for a file instead of a directory, the item name will be ''. For a directory 
                   14867: listing, if the item is a metadata file, the element will end &N&M 
                   14868: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   14869: default copyright set (1).  
                   14870: 
                   14871: =back
                   14872: 
                   14873: a scalar containing error condition (if encountered).
                   14874: 
                   14875: =over
                   14876: 
                   14877: =item 
                   14878: no_host (no homeserver identified for $username:$domain).
                   14879: 
                   14880: =item 
                   14881: no_such_host (server contacted for listing not identified as valid host).
                   14882: 
                   14883: =item 
                   14884: con_lost (connection to remote server failed).
                   14885: 
                   14886: =item 
                   14887: refused (invalid $username:$domain received on lond side).
                   14888: 
                   14889: =item 
                   14890: no_such_dir (directory at specified path on lond side does not exist). 
                   14891: 
                   14892: =item 
                   14893: empty (directory at specified path on lond side is empty).
                   14894: 
                   14895: =over
                   14896: 
                   14897: This is currently not encountered because the &ls3, &ls2, 
                   14898: &ls (_handler) routines on the lond side do not filter out
                   14899: . and .. from a directory listing. 
                   14900: 
                   14901: =back
                   14902: 
                   14903: =back
                   14904: 
                   14905: =back
1.191     harris41 14906: 
                   14907: =item *
                   14908: 
1.243     albertel 14909: spareserver() : find server with least workload from spare.tab
                   14910: 
1.986     foxr     14911: 
                   14912: =item *
                   14913: 
                   14914: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   14915: if there is no corresponding loncapa host.
                   14916: 
1.243     albertel 14917: =back
                   14918: 
1.986     foxr     14919: 
1.243     albertel 14920: =head2 Apache Request
                   14921: 
                   14922: =over 4
1.191     harris41 14923: 
                   14924: =item *
                   14925: 
1.243     albertel 14926: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   14927: localhost, posts hash
                   14928: 
                   14929: =back
                   14930: 
                   14931: =head2 Data to String to Data
                   14932: 
                   14933: =over 4
1.191     harris41 14934: 
                   14935: =item *
                   14936: 
1.243     albertel 14937: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   14938: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 14939: 
                   14940: =item *
                   14941: 
1.243     albertel 14942: hashref2str($hashref) : convert a hashref into a string complete with
                   14943: escaping and '=' and '&' separators, supports elements that are
                   14944: arrayrefs and hashrefs
1.191     harris41 14945: 
                   14946: =item *
                   14947: 
1.243     albertel 14948: arrayref2str($arrayref) : convert an arrayref into a string complete
                   14949: with escaping and '&' separators, supports elements that are arrayrefs
                   14950: and hashrefs
1.191     harris41 14951: 
                   14952: =item *
                   14953: 
1.243     albertel 14954: str2hash($string) : convert string to hash using unescaping and
                   14955: splitting on '=' and '&', supports elements that are arrayrefs and
                   14956: hashrefs
1.191     harris41 14957: 
                   14958: =item *
                   14959: 
1.243     albertel 14960: str2array($string) : convert string to hash using unescaping and
                   14961: splitting on '&', supports elements that are arrayrefs and hashrefs
                   14962: 
                   14963: =back
                   14964: 
                   14965: =head2 Logging Routines
                   14966: 
                   14967: 
                   14968: These routines allow one to make log messages in the lonnet.log and
                   14969: lonnet.perm logfiles.
1.191     harris41 14970: 
1.1119    foxr     14971: =over 4
                   14972: 
1.191     harris41 14973: =item *
                   14974: 
1.243     albertel 14975: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 14976: 
                   14977: =item *
                   14978: 
1.243     albertel 14979: logthis() : append message to the normal lonnet.log file, it gets
                   14980: preiodically rolled over and deleted.
1.191     harris41 14981: 
                   14982: =item *
                   14983: 
1.243     albertel 14984: logperm() : append a permanent message to lonnet.perm.log, this log
                   14985: file never gets deleted by any automated portion of the system, only
                   14986: messages of critical importance should go in here.
                   14987: 
1.1119    foxr     14988: 
1.243     albertel 14989: =back
                   14990: 
                   14991: =head2 General File Helper Routines
                   14992: 
                   14993: =over 4
1.191     harris41 14994: 
                   14995: =item *
                   14996: 
1.481     raeburn  14997: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   14998: (a) files in /uploaded
                   14999:   (i) If a local copy of the file exists - 
                   15000:       compares modification date of local copy with last-modified date for 
                   15001:       definitive version stored on home server for course. If local copy is 
                   15002:       stale, requests a new version from the home server and stores it. 
                   15003:       If the original has been removed from the home server, then local copy 
                   15004:       is unlinked.
                   15005:   (ii) If local copy does not exist -
                   15006:       requests the file from the home server and stores it. 
                   15007:   
                   15008:   If $caller is 'uploadrep':  
                   15009:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   15010:     for request for files originally uploaded via DOCS. 
                   15011:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   15012:   
                   15013:   Otherwise:
                   15014:      This indicates a call from the content generation phase of the request.
                   15015:      -  returns the entire contents of the file or -1.
                   15016:      
                   15017: (b) files in /res
                   15018:    - returns the entire contents of a file or -1; 
                   15019:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 15020: 
1.712     albertel 15021: 
                   15022: =item *
                   15023: 
                   15024: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   15025:                   reference
                   15026: 
                   15027: returns either a stat() list of data about the file or an empty list
                   15028: if the file doesn't exist or couldn't find out about it (connection
                   15029: problems or user unknown)
                   15030: 
1.191     harris41 15031: =item *
                   15032: 
1.243     albertel 15033: filelocation($dir,$file) : returns file system location of a file
                   15034: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   15035: directory that relative $file lookups are to looked in ($dir of /a/dir
                   15036: and a file of ../bob will become /a/bob)
1.191     harris41 15037: 
                   15038: =item *
                   15039: 
                   15040: hreflocation($dir,$file) : returns file system location or a URL; same as
                   15041: filelocation except for hrefs
                   15042: 
                   15043: =item *
                   15044: 
1.1261    raeburn  15045: declutter() : declutters URLs -- remove beginning slashes, 'res' etc.
                   15046: also removes beginning /home/httpd/html unless /priv/ follows it.
1.191     harris41 15047: 
1.243     albertel 15048: =back
                   15049: 
1.608     albertel 15050: =head2 Usererfile file routines (/uploaded*)
                   15051: 
                   15052: =over 4
                   15053: 
                   15054: =item *
                   15055: 
                   15056: userfileupload(): main rotine for putting a file in a user or course's
                   15057:                   filespace, arguments are,
                   15058: 
1.620     albertel 15059:  formname - required - this is the name of the element in $env where the
1.608     albertel 15060:            filename, and the contents of the file to create/modifed exist
1.620     albertel 15061:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   15062:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  15063:  context - if coursedoc, store the file in the course of the active role
                   15064:              of the current user; 
                   15065:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   15066:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 15067:  subdir - required - subdirectory to put the file in under ../userfiles/
                   15068:          if undefined, it will be placed in "unknown"
                   15069: 
                   15070:  (This routine calls clean_filename() to remove any dangerous
                   15071:  characters from the filename, and then calls finuserfileupload() to
                   15072:  complete the transaction)
                   15073: 
                   15074:  returns either the url of the uploaded file (/uploaded/....) if successful
                   15075:  and /adm/notfound.html if unsuccessful
                   15076: 
                   15077: =item *
                   15078: 
                   15079: clean_filename(): routine for cleaing a filename up for storage in
                   15080:                  userfile space, argument is:
                   15081: 
                   15082:  filename - proposed filename
                   15083: 
                   15084: returns: the new clean filename
                   15085: 
                   15086: =item *
                   15087: 
1.1090    raeburn  15088: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 15089: userspace, probably shouldn't be called directly
                   15090: 
                   15091:   docuname: username or courseid of destination for the file
                   15092:   docudom: domain of user/course of destination for the file
                   15093:   formname: same as for userfileupload()
1.1090    raeburn  15094:   fname: filename (including subdirectories) for the file
                   15095:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
                   15096:   allfiles: reference to hash used to store objects found by parser
                   15097:   codebase: reference to hash used for codebases of java objects found by parser
                   15098:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   15099:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   15100:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   15101:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   15102:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   15103:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  15104:   mimetype: reference to scalar to accommodate mime type determined
                   15105:             from File::MMagic if $parser = parse.
1.608     albertel 15106: 
                   15107:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  15108:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   15109:  was 'overwrite').
                   15110:  
1.608     albertel 15111: 
                   15112: =item *
                   15113: 
                   15114: renameuserfile(): renames an existing userfile to a new name
                   15115: 
                   15116:   Args:
                   15117:    docuname: username or courseid of destination for the file
                   15118:    docudom: domain of user/course of destination for the file
                   15119:    old: current file name (including any subdirs under userfiles)
                   15120:    new: desired file name (including any subdirs under userfiles)
                   15121: 
                   15122: =item *
                   15123: 
                   15124: mkdiruserfile(): creates a directory is a userfiles dir
                   15125: 
                   15126:   Args:
                   15127:    docuname: username or courseid of destination for the file
                   15128:    docudom: domain of user/course of destination for the file
                   15129:    dir: dir to create (including any subdirs under userfiles)
                   15130: 
                   15131: =item *
                   15132: 
                   15133: removeuserfile(): removes a file that exists in userfiles
                   15134: 
                   15135:   Args:
                   15136:    docuname: username or courseid of destination for the file
                   15137:    docudom: domain of user/course of destination for the file
                   15138:    fname: filname to delete (including any subdirs under userfiles)
                   15139: 
                   15140: =item *
                   15141: 
                   15142: removeuploadedurl(): convience function for removeuserfile()
                   15143: 
                   15144:   Args:
                   15145:    url:  a full /uploaded/... url to delete
                   15146: 
1.747     albertel 15147: =item * 
                   15148: 
                   15149: get_portfile_permissions():
                   15150:   Args:
                   15151:     domain: domain of user or course contain the portfolio files
                   15152:     user: name of user or num of course contain the portfolio files
                   15153:   Returns:
                   15154:     hashref of a dump of the proper file_permissions.db
                   15155:    
                   15156: 
                   15157: =item * 
                   15158: 
                   15159: get_access_controls():
                   15160: 
                   15161: Args:
                   15162:   current_permissions: the hash ref returned from get_portfile_permissions()
                   15163:   group: (optional) the group you want the files associated with
                   15164:   file: (optional) the file you want access info on
                   15165: 
                   15166: Returns:
1.749     raeburn  15167:     a hash (keys are file names) of hashes containing
                   15168:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   15169:         values are XML containing access control settings (see below) 
1.747     albertel 15170: 
                   15171: Internal notes:
                   15172: 
1.749     raeburn  15173:  access controls are stored in file_permissions.db as key=value pairs.
                   15174:     key -> path to file/file_name\0uniqueID:scope_end_start
                   15175:         where scope -> public,guest,course,group,domains or users.
                   15176:               end -> UNIX time for end of access (0 -> no end date)
                   15177:               start -> UNIX time for start of access
                   15178: 
                   15179:     value -> XML description of access control
                   15180:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   15181:             <start></start>
                   15182:             <end></end>
                   15183: 
                   15184:             <password></password>  for scope type = guest
                   15185: 
                   15186:             <domain></domain>     for scope type = course or group
                   15187:             <number></number>
                   15188:             <roles id="">
                   15189:              <role></role>
                   15190:              <access></access>
                   15191:              <section></section>
                   15192:              <group></group>
                   15193:             </roles>
                   15194: 
                   15195:             <dom></dom>         for scope type = domains
                   15196: 
                   15197:             <users>             for scope type = users
                   15198:              <user>
                   15199:               <uname></uname>
                   15200:               <udom></udom>
                   15201:              </user>
                   15202:             </users>
                   15203:            </scope> 
                   15204:               
                   15205:  Access data is also aggregated for each file in an additional key=value pair:
                   15206:  key -> path to file/file_name\0accesscontrol 
                   15207:  value -> reference to hash
                   15208:           hash contains key = value pairs
                   15209:           where key = uniqueID:scope_end_start
                   15210:                 value = UNIX time record was last updated
                   15211: 
                   15212:           Used to improve speed of look-ups of access controls for each file.  
                   15213:  
                   15214:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   15215: 
1.1198    raeburn  15216: =item *
                   15217: 
1.749     raeburn  15218: modify_access_controls():
                   15219: 
                   15220: Modifies access controls for a portfolio file
                   15221: Args
                   15222: 1. file name
                   15223: 2. reference to hash of required changes,
                   15224: 3. domain
                   15225: 4. username
                   15226:   where domain,username are the domain of the portfolio owner 
                   15227:   (either a user or a course) 
                   15228: 
                   15229: Returns:
                   15230: 1. result of additions or updates ('ok' or 'error', with error message). 
                   15231: 2. result of deletions ('ok' or 'error', with error message).
                   15232: 3. reference to hash of any new or updated access controls.
                   15233: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   15234:    key = integer (inbound ID)
1.1198    raeburn  15235:    value = uniqueID
                   15236: 
                   15237: =item *
                   15238: 
                   15239: get_timebased_id():
                   15240: 
                   15241: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   15242: course via the Course Editor (e.g., folders, composite pages, 
                   15243: group bulletin boards).
                   15244: 
                   15245: Args: (first three required; six others optional)
                   15246: 
                   15247: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   15248:    docssequence, or name of group
                   15249: 
                   15250: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   15251:    e.g., num, boardids
                   15252: 
                   15253: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   15254:    file will be named nohist_namespace.db
                   15255: 
                   15256: 4. cdom: domain of course; default is current course domain from %env
                   15257: 
                   15258: 5. cnum: course number; default is current course number from %env
                   15259: 
                   15260: 6. idtype: set to concat if an additional digit is to be appended to the 
                   15261:    unix timestamp to form the suffix, if the plain timestamp is already
                   15262:    in use.  Default is to not do this, but simply increment the unix 
                   15263:    timestamp by 1 until a unique key is obtained.
                   15264: 
                   15265: 7. who: holder of locking key; defaults to user:domain for user.
                   15266: 
                   15267: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   15268:    retrying); default is 3.
                   15269: 
                   15270: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   15271: 
                   15272: Returns:
                   15273: 
                   15274: 1. suffix obtained (numeric)
                   15275: 
                   15276: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   15277: 
                   15278: 3. error: contains (localized) error message if an error occurred.
                   15279: 
1.747     albertel 15280: 
1.608     albertel 15281: =back
                   15282: 
1.243     albertel 15283: =head2 HTTP Helper Routines
                   15284: 
                   15285: =over 4
                   15286: 
1.191     harris41 15287: =item *
                   15288: 
                   15289: escape() : unpack non-word characters into CGI-compatible hex codes
                   15290: 
                   15291: =item *
                   15292: 
                   15293: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   15294: 
1.243     albertel 15295: =back
                   15296: 
                   15297: =head1 PRIVATE SUBROUTINES
                   15298: 
                   15299: =head2 Underlying communication routines (Shouldn't call)
                   15300: 
                   15301: =over 4
                   15302: 
                   15303: =item *
                   15304: 
                   15305: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   15306: 
                   15307: =item *
                   15308: 
                   15309: reply() : uses subreply to send a message to remote machine, logs all failures
                   15310: 
                   15311: =item *
                   15312: 
                   15313: critical() : passes a critical message to another server; if cannot
                   15314: get through then place message in connection buffer directory and
                   15315: returns con_delayed, if incapable of saving message, returns
                   15316: con_failed
                   15317: 
                   15318: =item *
                   15319: 
                   15320: reconlonc() : tries to reconnect lonc client processes.
                   15321: 
                   15322: =back
                   15323: 
                   15324: =head2 Resource Access Logging
                   15325: 
                   15326: =over 4
                   15327: 
                   15328: =item *
                   15329: 
                   15330: flushcourselogs() : flush (save) buffer logs and access logs
                   15331: 
                   15332: =item *
                   15333: 
                   15334: courselog($what) : save message for course in hash
                   15335: 
                   15336: =item *
                   15337: 
                   15338: courseacclog($what) : save message for course using &courselog().  Perform
                   15339: special processing for specific resource types (problems, exams, quizzes, etc).
                   15340: 
1.191     harris41 15341: =item *
                   15342: 
                   15343: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   15344: as a PerlChildExitHandler
1.243     albertel 15345: 
                   15346: =back
                   15347: 
                   15348: =head2 Other
                   15349: 
                   15350: =over 4
                   15351: 
                   15352: =item *
                   15353: 
                   15354: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 15355: 
                   15356: =back
                   15357: 
                   15358: =cut
1.877     foxr     15359: 

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