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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1229  ! raeburn     4: # $Id: lonnet.pm,v 1.1228 2013/06/26 21:22:51 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.8       www        74: use LWP::UserAgent();
1.486     www        75: use HTTP::Date;
1.977     amueller   76: use Image::Magick;
                     77: 
1.1182    foxr       78: 
1.1173    foxr       79: use Encode;
                     80: 
1.1213    raeburn    81: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir $apache
1.1138    raeburn    82:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
                     83:             %managerstab);
1.871     albertel   84: 
                     85: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     86:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     87:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        88:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        89: 
1.1       albertel   90: use IO::Socket;
1.31      www        91: use GDBM_File;
1.208     albertel   92: use HTML::LCParser;
1.88      www        93: use Fcntl qw(:flock);
1.870     albertel   94: use Storable qw(thaw nfreeze);
1.539     albertel   95: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   96: use Cache::Memcached;
1.676     albertel   97: use Digest::MD5;
1.790     albertel   98: use Math::Random;
1.1024    raeburn    99: use File::MMagic;
1.807     albertel  100: use LONCAPA qw(:DEFAULT :match);
1.740     www       101: use LONCAPA::Configuration;
1.1160    www       102: use LONCAPA::lonmetadata;
1.1167    droeschl  103: use LONCAPA::Lond;
1.1117    foxr      104: 
1.1090    raeburn   105: use File::Copy;
1.676     albertel  106: 
1.195     www       107: my $readit;
1.550     foxr      108: my $max_connection_retries = 10;     # 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.993     raeburn   232: sub get_server_loncaparev {
1.1073    raeburn   233:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   234:     if (defined($lonhost)) {
                    235:         if (!defined(&hostname($lonhost))) {
                    236:             undef($lonhost);
                    237:         }
                    238:     }
                    239:     if (!defined($lonhost)) {
                    240:         if (defined(&domain($dom,'primary'))) {
                    241:             $lonhost=&domain($dom,'primary');
                    242:             if ($lonhost eq 'no_host') {
                    243:                 undef($lonhost);
                    244:             }
                    245:         }
                    246:     }
                    247:     if (defined($lonhost)) {
1.1073    raeburn   248:         my $cachetime = 12*3600;
                    249:         if (!$ignore_cache) {
                    250:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    251:             if (defined($cached)) {
                    252:                 return $loncaparev;
                    253:             }
                    254:         }
                    255:         my ($answer,$loncaparev);
                    256:         my @ids=&current_machine_ids();
                    257:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    258:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   259:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   260:                 $loncaparev = $1;
                    261:             }
                    262:         } else {
                    263:             $answer = &reply('serverloncaparev',$lonhost);
                    264:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    265:                 if ($caller eq 'loncron') {
                    266:                     my $ua=new LWP::UserAgent;
1.1082    raeburn   267:                     $ua->timeout(4);
1.1073    raeburn   268:                     my $protocol = $protocol{$lonhost};
                    269:                     $protocol = 'http' if ($protocol ne 'https');
                    270:                     my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
                    271:                     my $request=new HTTP::Request('GET',$url);
                    272:                     my $response=$ua->request($request);
                    273:                     unless ($response->is_error()) {
                    274:                         my $content = $response->content;
1.1081    raeburn   275:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   276:                             $loncaparev = $1;
                    277:                         }
                    278:                     }
                    279:                 } else {
                    280:                     $loncaparev = $loncaparevs{$lonhost};
                    281:                 }
1.1081    raeburn   282:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   283:                 $loncaparev = $1;
                    284:             }
1.993     raeburn   285:         }
1.1073    raeburn   286:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   287:     }
                    288: }
                    289: 
1.1074    raeburn   290: sub get_server_homeID {
                    291:     my ($hostname,$ignore_cache,$caller) = @_;
                    292:     unless ($ignore_cache) {
                    293:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    294:         if (defined($cached)) {
                    295:             return $serverhomeID;
                    296:         }
                    297:     }
                    298:     my $cachetime = 12*3600;
                    299:     my $serverhomeID;
                    300:     if ($caller eq 'loncron') { 
                    301:         my @machine_ids = &machine_ids($hostname);
                    302:         foreach my $id (@machine_ids) {
                    303:             my $response = &reply('serverhomeID',$id);
                    304:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    305:                 $serverhomeID = $response;
                    306:                 last;
                    307:             }
                    308:         }
                    309:         if ($serverhomeID eq '') {
                    310:             $serverhomeID = $machine_ids[-1];
                    311:         }
                    312:     } else {
                    313:         $serverhomeID = $serverhomeIDs{$hostname};
                    314:     }
                    315:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    316: }
                    317: 
1.1121    raeburn   318: sub get_remote_globals {
                    319:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   320:     my ($result,%returnhash,%whatneeded);
                    321:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   322:         foreach my $what (sort(keys(%{$whathash}))) {
                    323:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   324:             my ($response,$cached);
1.1121    raeburn   325:             unless ($ignore_cache) {
1.1125    raeburn   326:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   327:             }
                    328:             if (defined($cached)) {
1.1125    raeburn   329:                 $returnhash{$what} = $response;
1.1121    raeburn   330:             } else {
1.1125    raeburn   331:                 $whatneeded{$what} = 1;
1.1121    raeburn   332:             }
                    333:         }
1.1125    raeburn   334:         if (keys(%whatneeded) == 0) {
                    335:             $result = 'ok';
                    336:         } else {
1.1121    raeburn   337:             my $requested = &freeze_escape(\%whatneeded);
                    338:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   339:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    340:                 ($rep eq 'unknown_cmd')) {
                    341:                 $result = $rep;
                    342:             } else {
                    343:                 $result = 'ok';
1.1121    raeburn   344:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   345:                 foreach my $item (@pairs) {
                    346:                     my ($key,$value)=split(/=/,$item,2);
                    347:                     my $what = &unescape($key);
                    348:                     my $hashid = $lonhost.'-'.$what;
                    349:                     $returnhash{$what}=&thaw_unescape($value);
                    350:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   351:                 }
                    352:             }
                    353:         }
                    354:     }
1.1125    raeburn   355:     return ($result,\%returnhash);
1.1121    raeburn   356: }
                    357: 
1.1124    raeburn   358: sub remote_devalidate_cache {
                    359:     my ($lonhost,$name,$id) = @_;
1.1130    raeburn   360:     my $response = &reply('devalidatecache:'.&escape($name).':'.&escape($id),$lonhost);
1.1124    raeburn   361:     return $response;
                    362: }
                    363: 
1.1       albertel  364: # -------------------------------------------------- Non-critical communication
                    365: sub subreply {
                    366:     my ($cmd,$server)=@_;
1.838     albertel  367:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      368:     #
                    369:     #  With loncnew process trimming, there's a timing hole between lonc server
                    370:     #  process exit and the master server picking up the listen on the AF_UNIX
                    371:     #  socket.  In that time interval, a lock file will exist:
                    372: 
                    373:     my $lockfile=$peerfile.".lock";
                    374:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    375: 	sleep(1);
                    376:     }
                    377:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      378:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      379:     #
1.550     foxr      380:     #   We'll give the connection a few tries before abandoning it.  If
                    381:     #   connection is not possible, we'll con_lost back to the client.
                    382:     #   
                    383:     my $client;
                    384:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    385: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    386: 				      Type    => SOCK_STREAM,
                    387: 				      Timeout => 10);
1.869     albertel  388: 	if ($client) {
1.550     foxr      389: 	    last;		# Connected!
1.850     albertel  390: 	} else {
1.853     albertel  391: 	    &create_connection(&hostname($server),$server);
1.550     foxr      392: 	}
1.850     albertel  393:         sleep(1);		# Try again later if failed connection.
1.550     foxr      394:     }
                    395:     my $answer;
                    396:     if ($client) {
1.704     albertel  397: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      398: 	$answer=<$client>;
                    399: 	if (!$answer) { $answer="con_lost"; }
                    400: 	chomp($answer);
                    401:     } else {
                    402: 	$answer = 'con_lost';	# Failed connection.
                    403:     }
1.1       albertel  404:     return $answer;
                    405: }
                    406: 
                    407: sub reply {
                    408:     my ($cmd,$server)=@_;
1.838     albertel  409:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  410:     my $answer=subreply($cmd,$server);
1.65      www       411:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  412:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       413:                 " $cmd to $server returned $answer</font>");
                    414:     }
1.1       albertel  415:     return $answer;
                    416: }
                    417: 
                    418: # ----------------------------------------------------------- Send USR1 to lonc
                    419: 
                    420: sub reconlonc {
1.891     albertel  421:     my ($lonid) = @_;
                    422:     my $hostname = &hostname($lonid);
                    423:     if ($lonid) {
                    424: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    425: 	if ($hostname && -e $peerfile) {
                    426: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    427: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    428: 					     Type    => SOCK_STREAM,
                    429: 					     Timeout => 10);
                    430: 	    if ($client) {
                    431: 		print $client ("reset_retries\n");
                    432: 		my $answer=<$client>;
                    433: 		#reset just this one.
                    434: 	    }
                    435: 	}
                    436: 	return;
                    437:     }
                    438: 
1.836     www       439:     &logthis("Trying to reconnect lonc");
1.1       albertel  440:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  441:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  442: 	my $loncpid=<$fh>;
                    443:         chomp($loncpid);
                    444:         if (kill 0 => $loncpid) {
                    445: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    446:             kill USR1 => $loncpid;
                    447:             sleep 1;
1.836     www       448:          } else {
1.12      www       449: 	    &logthis(
1.672     albertel  450:                "<font color=\"blue\">WARNING:".
1.12      www       451:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  452:         }
                    453:     } else {
1.836     www       454: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  455:     }
                    456: }
                    457: 
                    458: # ------------------------------------------------------ Critical communication
1.12      www       459: 
1.1       albertel  460: sub critical {
                    461:     my ($cmd,$server)=@_;
1.838     albertel  462:     unless (&hostname($server)) {
1.672     albertel  463:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       464:                " Critical message to unknown server ($server)</font>");
                    465:         return 'no_such_host';
                    466:     }
1.1       albertel  467:     my $answer=reply($cmd,$server);
                    468:     if ($answer eq 'con_lost') {
                    469: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  470: 	my $answer=reply($cmd,$server);
1.1       albertel  471:         if ($answer eq 'con_lost') {
                    472:             my $now=time;
                    473:             my $middlename=$cmd;
1.5       www       474:             $middlename=substr($middlename,0,16);
1.1       albertel  475:             $middlename=~s/\W//g;
                    476:             my $dfilename=
1.305     www       477:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    478:             $dumpcount++;
1.1       albertel  479:             {
1.448     albertel  480: 		my $dfh;
                    481: 		if (open($dfh,">$dfilename")) {
                    482: 		    print $dfh "$cmd\n"; 
                    483: 		    close($dfh);
                    484: 		}
1.1       albertel  485:             }
                    486:             sleep 2;
                    487:             my $wcmd='';
                    488:             {
1.448     albertel  489: 		my $dfh;
                    490: 		if (open($dfh,"<$dfilename")) {
                    491: 		    $wcmd=<$dfh>; 
                    492: 		    close($dfh);
                    493: 		}
1.1       albertel  494:             }
                    495:             chomp($wcmd);
1.7       www       496:             if ($wcmd eq $cmd) {
1.672     albertel  497: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       498:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  499:                 &logperm("D:$server:$cmd");
                    500: 	        return 'con_delayed';
                    501:             } else {
1.672     albertel  502:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       503:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  504:                 &logperm("F:$server:$cmd");
                    505:                 return 'con_failed';
                    506:             }
                    507:         }
                    508:     }
                    509:     return $answer;
1.405     albertel  510: }
                    511: 
1.755     albertel  512: # ------------------------------------------- check if return value is an error
                    513: 
                    514: sub error {
                    515:     my ($result) = @_;
1.756     albertel  516:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  517: 	if ($2 == 2) { return undef; }
                    518: 	return $1;
                    519:     }
                    520:     return undef;
                    521: }
                    522: 
1.783     albertel  523: sub convert_and_load_session_env {
                    524:     my ($lonidsdir,$handle)=@_;
                    525:     my @profile;
                    526:     {
1.917     albertel  527: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    528: 	if (!$opened) {
1.915     albertel  529: 	    return 0;
                    530: 	}
1.783     albertel  531: 	flock($idf,LOCK_SH);
                    532: 	@profile=<$idf>;
                    533: 	close($idf);
                    534:     }
                    535:     my %temp_env;
                    536:     foreach my $line (@profile) {
1.786     albertel  537: 	if ($line !~ m/=/) {
                    538: 	    return 0;
                    539: 	}
1.783     albertel  540: 	chomp($line);
                    541: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    542: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    543:     }
                    544:     unlink("$lonidsdir/$handle.id");
                    545:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    546: 	    0640)) {
                    547: 	%disk_env = %temp_env;
                    548: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    549: 	untie(%disk_env);
                    550:     }
1.786     albertel  551:     return 1;
1.783     albertel  552: }
                    553: 
1.374     www       554: # ------------------------------------------- Transfer profile into environment
1.780     albertel  555: my $env_loaded;
                    556: sub transfer_profile_to_env {
1.788     albertel  557:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    558:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       559: 
1.720     albertel  560:     if (!defined($lonidsdir)) {
                    561: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    562:     }
                    563:     if (!defined($handle)) {
                    564:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    565:     }
                    566: 
1.786     albertel  567:     my $convert;
                    568:     {
1.917     albertel  569:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    570: 	if (!$opened) {
1.915     albertel  571: 	    return;
                    572: 	}
1.786     albertel  573: 	flock($idf,LOCK_SH);
                    574: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    575: 		&GDBM_READER(),0640)) {
                    576: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    577: 	    untie(%disk_env);
                    578: 	} else {
                    579: 	    $convert = 1;
                    580: 	}
                    581:     }
                    582:     if ($convert) {
                    583: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    584: 	    &logthis("Failed to load session, or convert session.");
                    585: 	}
1.374     www       586:     }
1.783     albertel  587: 
1.786     albertel  588:     my %remove;
1.783     albertel  589:     while ( my $envname = each(%env) ) {
1.433     matthew   590:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    591:             if ($time < time-300) {
1.783     albertel  592:                 $remove{$key}++;
1.433     matthew   593:             }
                    594:         }
                    595:     }
1.783     albertel  596: 
1.619     albertel  597:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  598:     $env_loaded=1;
1.783     albertel  599:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   600:         &delenv($expired_key);
1.374     www       601:     }
1.1       albertel  602: }
                    603: 
1.916     albertel  604: # ---------------------------------------------------- Check for valid session 
                    605: sub check_for_valid_session {
1.1155    raeburn   606:     my ($r,$name) = @_;
1.916     albertel  607:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
1.1155    raeburn   608:     if ($name eq '') {
                    609:         $name = 'lonID';
                    610:     }
                    611:     my $lonid=$cookies{$name};
1.916     albertel  612:     return undef if (!$lonid);
                    613: 
                    614:     my $handle=&LONCAPA::clean_handle($lonid->value);
1.1155    raeburn   615:     my $lonidsdir;
                    616:     if ($name eq 'lonDAV') {
                    617:         $lonidsdir=$r->dir_config('lonDAVsessDir');
                    618:     } else {
                    619:         $lonidsdir=$r->dir_config('lonIDsDir');
                    620:     }
1.916     albertel  621:     return undef if (!-e "$lonidsdir/$handle.id");
                    622: 
1.917     albertel  623:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    624:     return undef if (!$opened);
1.916     albertel  625: 
                    626:     flock($idf,LOCK_SH);
                    627:     my %disk_env;
                    628:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    629: 	    &GDBM_READER(),0640)) {
                    630: 	return undef;	
                    631:     }
                    632: 
                    633:     if (!defined($disk_env{'user.name'})
                    634: 	|| !defined($disk_env{'user.domain'})) {
                    635: 	return undef;
                    636:     }
1.1213    raeburn   637:     if (($r->user() eq '') && ($apache >= 2.4)) {
1.1212    raeburn   638:         if ($disk_env{'user.domain'} eq $r->dir_config('lonDefDomain')) {
                    639:             $r->user($disk_env{'user.name'});
                    640:         } else {
                    641:             $r->user($disk_env{'user.name'}.':'.$disk_env{'user.domain'});
                    642:         }
                    643:     }
1.916     albertel  644:     return $handle;
                    645: }
                    646: 
1.830     albertel  647: sub timed_flock {
                    648:     my ($file,$lock_type) = @_;
                    649:     my $failed=0;
                    650:     eval {
                    651: 	local $SIG{__DIE__}='DEFAULT';
                    652: 	local $SIG{ALRM}=sub {
                    653: 	    $failed=1;
                    654: 	    die("failed lock");
                    655: 	};
                    656: 	alarm(13);
                    657: 	flock($file,$lock_type);
                    658: 	alarm(0);
                    659:     };
                    660:     if ($failed) {
                    661: 	return undef;
                    662:     } else {
                    663: 	return 1;
                    664:     }
                    665: }
                    666: 
1.5       www       667: # ---------------------------------------------------------- Append Environment
                    668: 
                    669: sub appenv {
1.949     raeburn   670:     my ($newenv,$roles) = @_;
                    671:     if (ref($newenv) eq 'HASH') {
                    672:         foreach my $key (keys(%{$newenv})) {
                    673:             my $refused = 0;
                    674: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    675:                 $refused = 1;
                    676:                 if (ref($roles) eq 'ARRAY') {
                    677:                     my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
                    678:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    679:                         $refused = 0;
                    680:                     }
                    681:                 }
                    682:             }
                    683:             if ($refused) {
                    684:                 &logthis("<font color=\"blue\">WARNING: ".
                    685:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    686:                          .'</font>');
                    687: 	        delete($newenv->{$key});
                    688:             } else {
                    689:                 $env{$key}=$newenv->{$key};
                    690:             }
                    691:         }
                    692:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    693:         if ($opened
                    694: 	    && &timed_flock($env_file,LOCK_EX)
                    695: 	    &&
                    696: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    697: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    698: 	    while (my ($key,$value) = each(%{$newenv})) {
                    699: 	        $disk_env{$key} = $value;
                    700: 	    }
                    701: 	    untie(%disk_env);
1.35      www       702:         }
1.191     harris41  703:     }
1.56      www       704:     return 'ok';
                    705: }
                    706: # ----------------------------------------------------- Delete from Environment
                    707: 
                    708: sub delenv {
1.1104    raeburn   709:     my ($delthis,$regexp,$roles) = @_;
                    710:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    711:         my $refused = 1;
                    712:         if (ref($roles) eq 'ARRAY') {
                    713:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    714:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    715:                 $refused = 0;
                    716:             }
                    717:         }
                    718:         if ($refused) {
                    719:             &logthis("<font color=\"blue\">WARNING: ".
                    720:                      "Attempt to delete from environment ".$delthis);
                    721:             return 'error';
                    722:         }
1.56      www       723:     }
1.917     albertel  724:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    725:     if ($opened
1.915     albertel  726: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  727: 	&&
                    728: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    729: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  730: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   731: 	    if ($regexp) {
                    732:                 if ($key=~/^$delthis/) {
                    733:                     delete($env{$key});
                    734:                     delete($disk_env{$key});
                    735:                 } 
                    736:             } else {
                    737:                 if ($key=~/^\Q$delthis\E/) {
                    738: 		    delete($env{$key});
                    739: 		    delete($disk_env{$key});
                    740: 	        }
                    741:             }
1.448     albertel  742: 	}
1.783     albertel  743: 	untie(%disk_env);
1.5       www       744:     }
                    745:     return 'ok';
1.369     albertel  746: }
                    747: 
1.790     albertel  748: sub get_env_multiple {
                    749:     my ($name) = @_;
                    750:     my @values;
                    751:     if (defined($env{$name})) {
                    752:         # exists is it an array
                    753:         if (ref($env{$name})) {
                    754:             @values=@{ $env{$name} };
                    755:         } else {
                    756:             $values[0]=$env{$name};
                    757:         }
                    758:     }
                    759:     return(@values);
                    760: }
                    761: 
1.958     www       762: # ------------------------------------------------------------------- Locking
                    763: 
                    764: sub set_lock {
                    765:     my ($text)=@_;
                    766:     $locknum++;
                    767:     my $id=$$.'-'.$locknum;
                    768:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    769:              'session.lock.'.$id => $text});
                    770:     return $id;
                    771: }
                    772: 
                    773: sub get_locks {
                    774:     my $num=0;
                    775:     my %texts=();
                    776:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    777:        if ($lock=~/\w/) {
                    778:           $num++;
                    779:           $texts{$lock}=$env{'session.lock.'.$lock};
                    780:        }
                    781:    }
                    782:    return ($num,%texts);
                    783: }
                    784: 
                    785: sub remove_lock {
                    786:     my ($id)=@_;
                    787:     my $newlocks='';
                    788:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    789:        if (($lock=~/\w/) && ($lock ne $id)) {
                    790:           $newlocks.=','.$lock;
                    791:        }
                    792:     }
                    793:     &appenv({'session.locks' => $newlocks});
                    794:     &delenv('session.lock.'.$id);
                    795: }
                    796: 
                    797: sub remove_all_locks {
                    798:     my $activelocks=$env{'session.locks'};
                    799:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    800:        if ($lock=~/\w/) {
                    801:           &remove_lock($lock);
                    802:        }
                    803:     }
                    804: }
                    805: 
                    806: 
1.369     albertel  807: # ------------------------------------------ Find out current server userload
                    808: sub userload {
                    809:     my $numusers=0;
                    810:     {
                    811: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    812: 	my $filename;
                    813: 	my $curtime=time;
                    814: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  815: 	    next if ($filename eq '.' || $filename eq '..');
                    816: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.404     albertel  817: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  818: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  819: 	}
                    820: 	closedir(LONIDS);
                    821:     }
                    822:     my $userloadpercent=0;
                    823:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    824:     if ($maxuserload) {
1.371     albertel  825: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  826:     }
1.372     albertel  827:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  828:     return $userloadpercent;
1.283     www       829: }
                    830: 
1.1       albertel  831: # ------------------------------ Find server with least workload from spare.tab
1.11      www       832: 
1.1       albertel  833: sub spareserver {
1.1083    raeburn   834:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  835:     my $spare_server;
1.370     albertel  836:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  837:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    838:                                                      :  $userloadpercent;
1.1083    raeburn   839:     my ($uint_dom,$remotesessions);
                    840:     if (($udom ne '') && (&domain($udom) ne '')) {
                    841:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    842:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    843:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    844:         $remotesessions = $udomdefaults{'remotesessions'};
                    845:     }
1.1123    raeburn   846:     my $spareshash = &this_host_spares($udom);
                    847:     if (ref($spareshash) eq 'HASH') {
                    848:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    849:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                    850:                 if ($uint_dom) {
                    851:                     next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    852:                                                  $try_server));
                    853:                 }
                    854: 	        ($spare_server, $lowest_load) =
                    855: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                    856:             }
1.1083    raeburn   857:         }
1.784     albertel  858: 
1.1123    raeburn   859:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    860: 
                    861:         if (!$found_server) {
                    862:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                    863: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
                    864:                     if ($uint_dom) {
                    865:                         next unless (&spare_can_host($udom,$uint_dom,
                    866:                                                      $remotesessions,$try_server));
                    867:                     }
                    868: 	            ($spare_server, $lowest_load) =
                    869: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                    870:                 }
                    871: 	    }
                    872:         }
1.784     albertel  873:     }
                    874: 
                    875:     if (!$want_server_name) {
1.968     raeburn   876:         my $protocol = 'http';
                    877:         if ($protocol{$spare_server} eq 'https') {
                    878:             $protocol = $protocol{$spare_server};
                    879:         }
1.1001    raeburn   880:         if (defined($spare_server)) {
                    881:             my $hostname = &hostname($spare_server);
1.1083    raeburn   882:             if (defined($hostname)) {
1.1001    raeburn   883: 	        $spare_server = $protocol.'://'.$hostname;
                    884:             }
                    885:         }
1.784     albertel  886:     }
                    887:     return $spare_server;
                    888: }
                    889: 
                    890: sub compare_server_load {
                    891:     my ($try_server, $spare_server, $lowest_load) = @_;
                    892: 
                    893:     my $loadans     = &reply('load',    $try_server);
                    894:     my $userloadans = &reply('userload',$try_server);
                    895: 
                    896:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn   897: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel  898:     }
                    899: 
                    900:     my $load;
                    901:     if ($loadans =~ /\d/) {
                    902: 	if ($userloadans =~ /\d/) {
                    903: 	    #both are numbers, pick the bigger one
                    904: 	    $load = ($loadans > $userloadans) ? $loadans 
                    905: 		                              : $userloadans;
1.411     albertel  906: 	} else {
1.784     albertel  907: 	    $load = $loadans;
1.411     albertel  908: 	}
1.784     albertel  909:     } else {
                    910: 	$load = $userloadans;
                    911:     }
                    912: 
                    913:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    914: 	$spare_server = $try_server;
                    915: 	$lowest_load  = $load;
1.370     albertel  916:     }
1.784     albertel  917:     return ($spare_server,$lowest_load);
1.202     matthew   918: }
1.914     albertel  919: 
                    920: # --------------------------- ask offload servers if user already has a session
                    921: sub find_existing_session {
                    922:     my ($udom,$uname) = @_;
1.1123    raeburn   923:     my $spareshash = &this_host_spares($udom);
                    924:     if (ref($spareshash) eq 'HASH') {
                    925:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    926:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                    927:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    928:             }
                    929:         }
                    930:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                    931:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                    932:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    933:             }
                    934:         }
1.914     albertel  935:     }
                    936:     return;
                    937: }
                    938: 
                    939: # -------------------------------- ask if server already has a session for user
                    940: sub has_user_session {
                    941:     my ($lonid,$udom,$uname) = @_;
                    942:     my $result = &reply(join(':','userhassession',
                    943: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                    944:     return 1 if ($result eq 'ok');
                    945: 
                    946:     return 0;
                    947: }
                    948: 
1.1076    raeburn   949: # --------- determine least loaded server in a user's domain which allows login
                    950: 
                    951: sub choose_server {
1.1115    raeburn   952:     my ($udom,$checkloginvia) = @_;
1.1076    raeburn   953:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn   954:     my %servers = &get_servers($udom);
1.1076    raeburn   955:     my $lowest_load = 30000;
1.1151    raeburn   956:     my ($login_host,$hostname,$portal_path,$isredirect);
1.1076    raeburn   957:     foreach my $lonhost (keys(%servers)) {
1.1115    raeburn   958:         my $loginvia;
                    959:         if ($checkloginvia) {
                    960:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn   961:             if ($loginvia) {
                    962:                 my ($server,$path) = split(/:/,$loginvia);
                    963:                 ($login_host, $lowest_load) =
                    964:                     &compare_server_load($server, $login_host, $lowest_load);
                    965:                 if ($login_host eq $server) {
                    966:                     $portal_path = $path;
1.1151    raeburn   967:                     $isredirect = 1;
1.1116    raeburn   968:                 }
                    969:             } else {
                    970:                 ($login_host, $lowest_load) =
                    971:                     &compare_server_load($lonhost, $login_host, $lowest_load);
                    972:                 if ($login_host eq $lonhost) {
                    973:                     $portal_path = '';
1.1151    raeburn   974:                     $isredirect = ''; 
1.1116    raeburn   975:                 }
                    976:             }
                    977:         } else {
1.1076    raeburn   978:             ($login_host, $lowest_load) =
1.1116    raeburn   979:                 &compare_server_load($lonhost, $login_host, $lowest_load);
1.1076    raeburn   980:         }
                    981:     }
                    982:     if ($login_host ne '') {
1.1116    raeburn   983:         $hostname = &hostname($login_host);
1.1076    raeburn   984:     }
1.1151    raeburn   985:     return ($login_host,$hostname,$portal_path,$isredirect);
1.1076    raeburn   986: }
                    987: 
1.202     matthew   988: # --------------------------------------------- Try to change a user's password
                    989: 
                    990: sub changepass {
1.799     raeburn   991:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   992:     $currentpass = &escape($currentpass);
                    993:     $newpass     = &escape($newpass);
1.1030    raeburn   994:     my $lonhost = $perlvar{'lonHostID'};
                    995:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew   996: 		       $server);
                    997:     if (! $answer) {
                    998: 	&logthis("No reply on password change request to $server ".
                    999: 		 "by $uname in domain $udom.");
                   1000:     } elsif ($answer =~ "^ok") {
                   1001:         &logthis("$uname in $udom successfully changed their password ".
                   1002: 		 "on $server.");
                   1003:     } elsif ($answer =~ "^pwchange_failure") {
                   1004: 	&logthis("$uname in $udom was unable to change their password ".
                   1005: 		 "on $server.  The action was blocked by either lcpasswd ".
                   1006: 		 "or pwchange");
                   1007:     } elsif ($answer =~ "^non_authorized") {
                   1008:         &logthis("$uname in $udom did not get their password correct when ".
                   1009: 		 "attempting to change it on $server.");
                   1010:     } elsif ($answer =~ "^auth_mode_error") {
                   1011:         &logthis("$uname in $udom attempted to change their password despite ".
                   1012: 		 "not being locally or internally authenticated on $server.");
                   1013:     } elsif ($answer =~ "^unknown_user") {
                   1014:         &logthis("$uname in $udom attempted to change their password ".
                   1015: 		 "on $server but were unable to because $server is not ".
                   1016: 		 "their home server.");
                   1017:     } elsif ($answer =~ "^refused") {
                   1018: 	&logthis("$server refused to change $uname in $udom password because ".
                   1019: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn  1020:     } elsif ($answer =~ "invalid_client") {
                   1021:         &logthis("$server refused to change $uname in $udom password because ".
                   1022:                  "it was a reset by e-mail originating from an invalid server.");
1.202     matthew  1023:     }
                   1024:     return $answer;
1.1       albertel 1025: }
                   1026: 
1.169     harris41 1027: # ----------------------- Try to determine user's current authentication scheme
                   1028: 
                   1029: sub queryauthenticate {
                   1030:     my ($uname,$udom)=@_;
1.456     albertel 1031:     my $uhome=&homeserver($uname,$udom);
                   1032:     if (!$uhome) {
                   1033: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1034: 	return 'no_host';
                   1035:     }
                   1036:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1037:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1038: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1039:     }
1.456     albertel 1040:     return $answer;
1.169     harris41 1041: }
                   1042: 
1.1       albertel 1043: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1044: 
1.1       albertel 1045: sub authenticate {
1.1073    raeburn  1046:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1047:     $upass=&escape($upass);
                   1048:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1049:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1050:     my $newhome;
1.836     www      1051:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1052: # Maybe the machine was offline and only re-appeared again recently?
                   1053:         &reconlonc();
                   1054: # One more
1.952     raeburn  1055: 	$uhome=&homeserver($uname,$udom,1);
                   1056:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1057:             if (defined(&domain($udom,'primary'))) {
                   1058:                 $newhome=&domain($udom,'primary');
                   1059:             }
                   1060:             if ($newhome ne '') {
                   1061:                 $uhome = $newhome;
                   1062:             }
                   1063:         }
1.836     www      1064: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1065: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1066: 	    return 'no_host';
                   1067:         }
1.1       albertel 1068:     }
1.1073    raeburn  1069:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1070:     if ($answer eq 'authorized') {
1.952     raeburn  1071:         if ($newhome) {
                   1072:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1073:             return 'no_account_on_host'; 
                   1074:         } else {
                   1075:             &logthis("User $uname at $udom authorized by $uhome");
                   1076:             return $uhome;
                   1077:         }
1.471     albertel 1078:     }
                   1079:     if ($answer eq 'non_authorized') {
                   1080: 	&logthis("User $uname at $udom rejected by $uhome");
                   1081: 	return 'no_host'; 
1.9       www      1082:     }
1.471     albertel 1083:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1084:     return 'no_host';
                   1085: }
                   1086: 
1.1073    raeburn  1087: sub can_host_session {
1.1074    raeburn  1088:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1089:     my $canhost = 1;
1.1074    raeburn  1090:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn  1091:     if (ref($remotesessions) eq 'HASH') {
                   1092:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1093:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1094:                 $canhost = 0;
                   1095:             } else {
                   1096:                 $canhost = 1;
                   1097:             }
                   1098:         }
                   1099:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1100:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1101:                 $canhost = 1;
                   1102:             } else {
                   1103:                 $canhost = 0;
                   1104:             }
                   1105:         }
                   1106:         if ($canhost) {
                   1107:             if ($remotesessions->{'version'} ne '') {
                   1108:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1109:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1110:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1111:                         my $major = $1;
                   1112:                         my $minor = $2;
                   1113:                         if (($major < $reqmajor ) ||
                   1114:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1115:                             $canhost = 0;
                   1116:                         }
                   1117:                     } else {
                   1118:                         $canhost = 0;
                   1119:                     }
                   1120:                 }
                   1121:             }
                   1122:         }
                   1123:     }
                   1124:     if ($canhost) {
                   1125:         if (ref($hostedsessions) eq 'HASH') {
1.1120    raeburn  1126:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1127:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073    raeburn  1128:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1129:                 if (($uint_dom ne '') && 
                   1130:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1131:                     $canhost = 0;
                   1132:                 } else {
                   1133:                     $canhost = 1;
                   1134:                 }
                   1135:             }
                   1136:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1137:                 if (($uint_dom ne '') && 
                   1138:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1139:                     $canhost = 1;
                   1140:                 } else {
                   1141:                     $canhost = 0;
                   1142:                 }
                   1143:             }
                   1144:         }
                   1145:     }
                   1146:     return $canhost;
                   1147: }
                   1148: 
1.1083    raeburn  1149: sub spare_can_host {
                   1150:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1151:     my $canhost=1;
                   1152:     my @intdoms;
                   1153:     my $internet_names = &Apache::lonnet::get_internet_names($try_server);
                   1154:     if (ref($internet_names) eq 'ARRAY') {
                   1155:         @intdoms = @{$internet_names};
                   1156:     }
                   1157:     unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1158:         my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
                   1159:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   1160:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                   1161:         my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
                   1162:         $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1163:                                      $remotesessions,
                   1164:                                      $defdomdefaults{'hostedsessions'});
                   1165:     }
                   1166:     return $canhost;
                   1167: }
                   1168: 
1.1123    raeburn  1169: sub this_host_spares {
                   1170:     my ($dom) = @_;
1.1126    raeburn  1171:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1172:     my @hosts = &current_machine_ids();
                   1173:     foreach my $lonhost (@hosts) {
                   1174:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1175:             $dom_in_use = $dom;
                   1176:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1177:             last;
                   1178:         }
                   1179:     }
1.1126    raeburn  1180:     if ($dom_in_use ne '') {
                   1181:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1182:     }
                   1183:     if (ref($result) ne 'HASH') {
                   1184:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1185:         $dom_in_use = &host_domain($lonhost_in_use);
                   1186:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1187:         if (ref($result) ne 'HASH') {
                   1188:             $result = \%spareid;
                   1189:         }
                   1190:     }
                   1191:     return $result;
                   1192: }
                   1193: 
                   1194: sub spares_for_offload  {
                   1195:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1196:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1197:     if (defined($cached)) {
                   1198:         return $result;
                   1199:     } else {
1.1126    raeburn  1200:         my $cachetime = 60*60*24;
                   1201:         my %domconfig =
                   1202:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
                   1203:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1204:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1205:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1206:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1207:                 }
                   1208:             }
                   1209:         }
                   1210:     }
1.1126    raeburn  1211:     return;
1.1123    raeburn  1212: }
                   1213: 
1.1129    raeburn  1214: sub get_lonbalancer_config {
                   1215:     my ($servers) = @_;
                   1216:     my ($currbalancer,$currtargets);
                   1217:     if (ref($servers) eq 'HASH') {
                   1218:         foreach my $server (keys(%{$servers})) {
                   1219:             my %what = (
                   1220:                          spareid => 1,
                   1221:                          perlvar => 1,
                   1222:                        );
                   1223:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1224:             if ($result eq 'ok') {
                   1225:                 if (ref($returnhash) eq 'HASH') {
                   1226:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1227:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1228:                             $currbalancer = $server;
                   1229:                             $currtargets = {};
                   1230:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1231:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1232:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1233:                                 }
                   1234:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1235:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1236:                                 }
                   1237:                             }
                   1238:                             last;
                   1239:                         }
                   1240:                     }
                   1241:                 }
                   1242:             }
                   1243:         }
                   1244:     }
                   1245:     return ($currbalancer,$currtargets);
                   1246: }
                   1247: 
                   1248: sub check_loadbalancing {
                   1249:     my ($uname,$udom) = @_;
1.1191    raeburn  1250:     my ($is_balancer,$currtargets,$currrules,$dom_in_use,$homeintdom,
                   1251:         $rule_in_effect,$offloadto,$otherserver);
1.1129    raeburn  1252:     my $lonhost = $perlvar{'lonHostID'};
1.1175    raeburn  1253:     my @hosts = &current_machine_ids();
1.1129    raeburn  1254:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1255:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
                   1256:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                   1257:     my $serverhomedom = &host_domain($lonhost);
                   1258: 
                   1259:     my $cachetime = 60*60*24;
                   1260: 
                   1261:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1262:         $dom_in_use = $udom;
                   1263:         $homeintdom = 1;
                   1264:     } else {
                   1265:         $dom_in_use = $serverhomedom;
                   1266:     }
                   1267:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1268:     unless (defined($cached)) {
                   1269:         my %domconfig =
                   1270:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
                   1271:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1272:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1129    raeburn  1273:         }
                   1274:     }
                   1275:     if (ref($result) eq 'HASH') {
1.1191    raeburn  1276:         ($is_balancer,$currtargets,$currrules) = 
                   1277:             &check_balancer_result($result,@hosts);
1.1129    raeburn  1278:         if ($is_balancer) {
                   1279:             if (ref($currrules) eq 'HASH') {
                   1280:                 if ($homeintdom) {
                   1281:                     if ($uname ne '') {
                   1282:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1283:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1284:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1285:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1286:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1287:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1288:                             }
                   1289:                         }
                   1290:                         if ($rule_in_effect eq '') {
                   1291:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1292:                             if ($userenv{'inststatus'} ne '') {
                   1293:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1294:                                 my ($othertitle,$usertypes,$types) =
                   1295:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1296:                                 if (ref($types) eq 'ARRAY') {
                   1297:                                     foreach my $type (@{$types}) {
                   1298:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1299:                                             if (exists($currrules->{$type})) {
                   1300:                                                 $rule_in_effect = $currrules->{$type};
                   1301:                                             }
                   1302:                                         }
                   1303:                                     }
                   1304:                                 }
                   1305:                             } else {
                   1306:                                 if (exists($currrules->{'default'})) {
                   1307:                                     $rule_in_effect = $currrules->{'default'};
                   1308:                                 }
                   1309:                             }
                   1310:                         }
                   1311:                     } else {
                   1312:                         if (exists($currrules->{'default'})) {
                   1313:                             $rule_in_effect = $currrules->{'default'};
                   1314:                         }
                   1315:                     }
                   1316:                 } else {
                   1317:                     if ($currrules->{'_LC_external'} ne '') {
                   1318:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1319:                     }
                   1320:                 }
                   1321:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1322:                                                        $uname,$udom);
                   1323:             }
                   1324:         }
                   1325:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
                   1326:         my ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
                   1327:         unless (defined($cached)) {
                   1328:             my %domconfig =
                   1329:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
                   1330:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1331:                 $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1129    raeburn  1332:             }
                   1333:         }
                   1334:         if (ref($result) eq 'HASH') {
1.1191    raeburn  1335:             ($is_balancer,$currtargets,$currrules) = 
                   1336:                 &check_balancer_result($result,@hosts);
                   1337:             if ($is_balancer) {
1.1129    raeburn  1338:                 if (ref($currrules) eq 'HASH') {
                   1339:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1340:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1341:                     }
                   1342:                 }
                   1343:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1344:                                                        $uname,$udom);
                   1345:             }
                   1346:         } else {
                   1347:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1348:                 $is_balancer = 1;
                   1349:                 $offloadto = &this_host_spares($dom_in_use);
                   1350:             }
                   1351:         }
                   1352:     } else {
                   1353:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1354:             $is_balancer = 1;
                   1355:             $offloadto = &this_host_spares($dom_in_use);
                   1356:         }
                   1357:     }
1.1176    raeburn  1358:     if ($is_balancer) {
                   1359:         my $lowest_load = 30000;
                   1360:         if (ref($offloadto) eq 'HASH') {
                   1361:             if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1362:                 foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1363:                     ($otherserver,$lowest_load) =
                   1364:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1365:                 }
1.1129    raeburn  1366:             }
1.1176    raeburn  1367:             my $found_server = ($otherserver ne '' && $lowest_load < 100);
1.1129    raeburn  1368: 
1.1176    raeburn  1369:             if (!$found_server) {
                   1370:                 if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1371:                     foreach my $try_server (@{$offloadto->{'default'}}) {
                   1372:                         ($otherserver,$lowest_load) =
                   1373:                             &compare_server_load($try_server,$otherserver,$lowest_load);
                   1374:                     }
                   1375:                 }
                   1376:             }
                   1377:         } elsif (ref($offloadto) eq 'ARRAY') {
                   1378:             if (@{$offloadto} == 1) {
                   1379:                 $otherserver = $offloadto->[0];
                   1380:             } elsif (@{$offloadto} > 1) {
                   1381:                 foreach my $try_server (@{$offloadto}) {
1.1129    raeburn  1382:                     ($otherserver,$lowest_load) =
                   1383:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1384:                 }
                   1385:             }
                   1386:         }
1.1176    raeburn  1387:         if (($otherserver ne '') && (grep(/^\Q$otherserver\E$/,@hosts))) {
                   1388:             $is_balancer = 0;
                   1389:             if ($uname ne '' && $udom ne '') {
                   1390:                 if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
                   1391:                     
                   1392:                     &appenv({'user.loadbalexempt'     => $lonhost,  
                   1393:                              'user.loadbalcheck.time' => time});
                   1394:                 }
1.1129    raeburn  1395:             }
                   1396:         }
                   1397:     }
                   1398:     return ($is_balancer,$otherserver);
                   1399: }
                   1400: 
1.1191    raeburn  1401: sub check_balancer_result {
                   1402:     my ($result,@hosts) = @_;
                   1403:     my ($is_balancer,$currtargets,$currrules);
                   1404:     if (ref($result) eq 'HASH') {
                   1405:         if ($result->{'lonhost'} ne '') {
                   1406:             my $currbalancer = $result->{'lonhost'};
                   1407:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1408:                 $is_balancer = 1;
                   1409:                 $currtargets = $result->{'targets'};
                   1410:                 $currrules = $result->{'rules'};
                   1411:             }
                   1412:         } else {
                   1413:             foreach my $key (keys(%{$result})) {
                   1414:                 if (($key ne '') && (grep(/^\Q$key\E$/,@hosts)) &&
                   1415:                     (ref($result->{$key}) eq 'HASH')) {
                   1416:                     $is_balancer = 1;
                   1417:                     $currrules = $result->{$key}{'rules'};
                   1418:                     $currtargets = $result->{$key}{'targets'};
                   1419:                     last;
                   1420:                 }
                   1421:             }
                   1422:         }
                   1423:     }
                   1424:     return ($is_balancer,$currtargets,$currrules);
                   1425: }
                   1426: 
1.1129    raeburn  1427: sub get_loadbalancer_targets {
                   1428:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1429:     my $offloadto;
1.1175    raeburn  1430:     if ($rule_in_effect eq 'none') {
                   1431:         return [$perlvar{'lonHostID'}];
                   1432:     } elsif ($rule_in_effect eq '') {
1.1129    raeburn  1433:         $offloadto = $currtargets;
                   1434:     } else {
                   1435:         if ($rule_in_effect eq 'homeserver') {
                   1436:             my $homeserver = &homeserver($uname,$udom);
                   1437:             if ($homeserver ne 'no_host') {
                   1438:                 $offloadto = [$homeserver];
                   1439:             }
                   1440:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1441:             my %domconfig =
                   1442:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1443:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1444:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1445:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1446:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1447:                     }
                   1448:                 }
                   1449:             } else {
1.1178    raeburn  1450:                 my %servers = &internet_dom_servers($udom);
1.1129    raeburn  1451:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1452:                 if (&hostname($remotebalancer) ne '') {
                   1453:                     $offloadto = [$remotebalancer];
                   1454:                 }
                   1455:             }
                   1456:         } elsif (&hostname($rule_in_effect) ne '') {
                   1457:             $offloadto = [$rule_in_effect];
                   1458:         }
                   1459:     }
                   1460:     return $offloadto;
                   1461: }
                   1462: 
1.1127    raeburn  1463: sub internet_dom_servers {
                   1464:     my ($dom) = @_;
                   1465:     my (%uniqservers,%servers);
                   1466:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1467:     my @machinedoms = &machine_domains($primaryserver);
                   1468:     foreach my $mdom (@machinedoms) {
                   1469:         my %currservers = %servers;
                   1470:         my %server = &get_servers($mdom);
                   1471:         %servers = (%currservers,%server);
                   1472:     }
                   1473:     my %by_hostname;
                   1474:     foreach my $id (keys(%servers)) {
                   1475:         push(@{$by_hostname{$servers{$id}}},$id);
                   1476:     }
                   1477:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1478:         if (@{$by_hostname{$hostname}} > 1) {
                   1479:             my $match = 0;
                   1480:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1481:                 if (&host_domain($id) eq $dom) {
                   1482:                     $uniqservers{$id} = $hostname;
                   1483:                     $match = 1;
                   1484:                 }
                   1485:             }
                   1486:             unless ($match) {
                   1487:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1488:             }
                   1489:         } else {
                   1490:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1491:         }
                   1492:     }
                   1493:     return %uniqservers;
                   1494: }
                   1495: 
1.1       albertel 1496: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1497: 
1.599     albertel 1498: my %homecache;
1.1       albertel 1499: sub homeserver {
1.230     stredwic 1500:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1501:     my $index="$uname:$udom";
1.426     albertel 1502: 
1.599     albertel 1503:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1504: 
                   1505:     my %servers = &get_servers($udom,'library');
                   1506:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1507:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1508: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1509: 
                   1510: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1511: 	if ($answer eq 'found') {
                   1512: 	    delete($badServerCache{$tryserver}); 
                   1513: 	    return $homecache{$index}=$tryserver;
                   1514: 	} elsif ($answer eq 'no_host') {
                   1515: 	    $badServerCache{$tryserver}=1;
                   1516: 	}
1.1       albertel 1517:     }    
                   1518:     return 'no_host';
1.70      www      1519: }
                   1520: 
                   1521: # ------------------------------------- Find the usernames behind a list of IDs
                   1522: 
                   1523: sub idget {
                   1524:     my ($udom,@ids)=@_;
                   1525:     my %returnhash=();
                   1526:     
1.841     albertel 1527:     my %servers = &get_servers($udom,'library');
                   1528:     foreach my $tryserver (keys(%servers)) {
                   1529: 	my $idlist=join('&',@ids);
                   1530: 	$idlist=~tr/A-Z/a-z/; 
                   1531: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1532: 	my @answer=();
                   1533: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1534: 	    @answer=split(/\&/,$reply);
                   1535: 	}                    ;
                   1536: 	my $i;
                   1537: 	for ($i=0;$i<=$#ids;$i++) {
                   1538: 	    if ($answer[$i]) {
                   1539: 		$returnhash{$ids[$i]}=$answer[$i];
                   1540: 	    } 
                   1541: 	}
                   1542:     } 
1.70      www      1543:     return %returnhash;
                   1544: }
                   1545: 
                   1546: # ------------------------------------- Find the IDs behind a list of usernames
                   1547: 
                   1548: sub idrget {
                   1549:     my ($udom,@unames)=@_;
                   1550:     my %returnhash=();
1.800     albertel 1551:     foreach my $uname (@unames) {
                   1552:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1553:     }
1.70      www      1554:     return %returnhash;
                   1555: }
                   1556: 
                   1557: # ------------------------------- Store away a list of names and associated IDs
                   1558: 
                   1559: sub idput {
                   1560:     my ($udom,%ids)=@_;
                   1561:     my %servers=();
1.800     albertel 1562:     foreach my $uname (keys(%ids)) {
                   1563: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                   1564:         my $uhom=&homeserver($uname,$udom);
1.70      www      1565:         if ($uhom ne 'no_host') {
1.800     albertel 1566:             my $id=&escape($ids{$uname});
1.70      www      1567:             $id=~tr/A-Z/a-z/;
1.800     albertel 1568:             my $esc_unam=&escape($uname);
1.70      www      1569: 	    if ($servers{$uhom}) {
1.800     albertel 1570: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www      1571:             } else {
1.800     albertel 1572:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www      1573:             }
                   1574:         }
1.191     harris41 1575:     }
1.800     albertel 1576:     foreach my $server (keys(%servers)) {
                   1577:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41 1578:     }
1.344     www      1579: }
                   1580: 
1.1023    raeburn  1581: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  1582: sub dump_dom {
1.1165    droeschl 1583:     my ($namespace, $udom, $regexp) = @_;
                   1584: 
                   1585:     $udom ||= $env{'user.domain'};
                   1586: 
                   1587:     return () unless $udom;
                   1588: 
                   1589:     return &dump($namespace, $udom, &get_domainconfiguser($udom), $regexp);
1.1012    raeburn  1590: }
                   1591: 
1.1023    raeburn  1592: # ------------------------------------------ get items from domain db files   
1.806     raeburn  1593: 
                   1594: sub get_dom {
1.860     raeburn  1595:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn  1596:     my $items='';
                   1597:     foreach my $item (@$storearr) {
                   1598:         $items.=&escape($item).'&';
                   1599:     }
                   1600:     $items=~s/\&$//;
1.860     raeburn  1601:     if (!$udom) {
                   1602:         $udom=$env{'user.domain'};
                   1603:         if (defined(&domain($udom,'primary'))) {
                   1604:             $uhome=&domain($udom,'primary');
                   1605:         } else {
1.874     albertel 1606:             undef($uhome);
1.860     raeburn  1607:         }
                   1608:     } else {
                   1609:         if (!$uhome) {
                   1610:             if (defined(&domain($udom,'primary'))) {
                   1611:                 $uhome=&domain($udom,'primary');
                   1612:             }
                   1613:         }
                   1614:     }
                   1615:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1616:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn  1617:         my %returnhash;
1.875     albertel 1618:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  1619:             return %returnhash;
                   1620:         }
1.806     raeburn  1621:         my @pairs=split(/\&/,$rep);
                   1622:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   1623:             return @pairs;
                   1624:         }
                   1625:         my $i=0;
                   1626:         foreach my $item (@$storearr) {
                   1627:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   1628:             $i++;
                   1629:         }
                   1630:         return %returnhash;
                   1631:     } else {
1.880     banghart 1632:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  1633:     }
                   1634: }
                   1635: 
                   1636: # -------------------------------------------- put items in domain db files 
                   1637: 
                   1638: sub put_dom {
1.860     raeburn  1639:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   1640:     if (!$udom) {
                   1641:         $udom=$env{'user.domain'};
                   1642:         if (defined(&domain($udom,'primary'))) {
                   1643:             $uhome=&domain($udom,'primary');
                   1644:         } else {
1.874     albertel 1645:             undef($uhome);
1.860     raeburn  1646:         }
                   1647:     } else {
                   1648:         if (!$uhome) {
                   1649:             if (defined(&domain($udom,'primary'))) {
                   1650:                 $uhome=&domain($udom,'primary');
                   1651:             }
                   1652:         }
                   1653:     } 
                   1654:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1655:         my $items='';
                   1656:         foreach my $item (keys(%$storehash)) {
                   1657:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   1658:         }
                   1659:         $items=~s/\&$//;
                   1660:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                   1661:     } else {
1.860     raeburn  1662:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  1663:     }
                   1664: }
                   1665: 
1.1023    raeburn  1666: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  1667: sub newput_dom {
1.1023    raeburn  1668:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  1669:     my $result;
                   1670:     if (!$udom) {
                   1671:         $udom=$env{'user.domain'};
                   1672:     }
1.1023    raeburn  1673:     if ($udom) {
                   1674:         my $uname = &get_domainconfiguser($udom);
                   1675:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  1676:     }
                   1677:     return $result;
                   1678: }
                   1679: 
1.1023    raeburn  1680: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  1681: sub del_dom {
1.1023    raeburn  1682:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  1683:     if (ref($storearr) eq 'ARRAY') {
                   1684:         if (!$udom) {
                   1685:             $udom=$env{'user.domain'};
                   1686:         }
1.1023    raeburn  1687:         if ($udom) {
                   1688:             my $uname = &get_domainconfiguser($udom); 
                   1689:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  1690:         }
                   1691:     }
                   1692: }
                   1693: 
1.1023    raeburn  1694: # ----------------------------------construct domainconfig user for a domain 
                   1695: sub get_domainconfiguser {
                   1696:     my ($udom) = @_;
                   1697:     return $udom.'-domainconfig';
                   1698: }
                   1699: 
1.837     raeburn  1700: sub retrieve_inst_usertypes {
                   1701:     my ($udom) = @_;
                   1702:     my (%returnhash,@order);
1.989     raeburn  1703:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   1704:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   1705:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
                   1706:         %returnhash = %{$domdefs{'inststatustypes'}};
                   1707:         @order = @{$domdefs{'inststatusorder'}};
                   1708:     } else {
                   1709:         if (defined(&domain($udom,'primary'))) {
                   1710:             my $uhome=&domain($udom,'primary');
                   1711:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   1712:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
                   1713:                 &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
                   1714:                 return (\%returnhash,\@order);
                   1715:             }
                   1716:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   1717:             my @pairs=split(/\&/,$hashitems);
                   1718:             foreach my $item (@pairs) {
                   1719:                 my ($key,$value)=split(/=/,$item,2);
                   1720:                 $key = &unescape($key);
                   1721:                 next if ($key =~ /^error: 2 /);
                   1722:                 $returnhash{$key}=&thaw_unescape($value);
                   1723:             }
                   1724:             my @esc_order = split(/\&/,$orderitems);
                   1725:             foreach my $item (@esc_order) {
                   1726:                 push(@order,&unescape($item));
                   1727:             }
                   1728:         } else {
                   1729:             &logthis("get_dom failed - no primary domain server for $udom");
1.837     raeburn  1730:         }
                   1731:     }
                   1732:     return (\%returnhash,\@order);
                   1733: }
                   1734: 
1.868     raeburn  1735: sub is_domainimage {
                   1736:     my ($url) = @_;
                   1737:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                   1738:         if (&domain($1) ne '') {
                   1739:             return '1';
                   1740:         }
                   1741:     }
                   1742:     return;
                   1743: }
                   1744: 
1.899     raeburn  1745: sub inst_directory_query {
                   1746:     my ($srch) = @_;
                   1747:     my $udom = $srch->{'srchdomain'};
                   1748:     my %results;
                   1749:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  1750:     my $outcome;
1.899     raeburn  1751:     if ($homeserver ne '') {
1.904     albertel 1752: 	my $queryid=&reply("querysend:instdirsearch:".
                   1753: 			   &escape($srch->{'srchby'}).':'.
                   1754: 			   &escape($srch->{'srchterm'}).':'.
                   1755: 			   &escape($srch->{'srchtype'}),$homeserver);
                   1756: 	my $host=&hostname($homeserver);
                   1757: 	if ($queryid !~/^\Q$host\E\_/) {
                   1758: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1759: 	    return;
                   1760: 	}
                   1761: 	my $response = &get_query_reply($queryid);
                   1762: 	my $maxtries = 5;
                   1763: 	my $tries = 1;
                   1764: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1765: 	    $response = &get_query_reply($queryid);
                   1766: 	    $tries ++;
                   1767: 	}
                   1768: 
                   1769:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  1770:             if ($response eq 'unavailable') {
                   1771:                 $outcome = $response;
                   1772:             } else {
                   1773:                 $outcome = 'ok';
                   1774:                 my @matches = split(/\n/,$response);
                   1775:                 foreach my $match (@matches) {
                   1776:                     my ($key,$value) = split(/=/,$match);
                   1777:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   1778:                 }
1.899     raeburn  1779:             }
                   1780:         }
                   1781:     }
1.909     raeburn  1782:     return ($outcome,%results);
1.899     raeburn  1783: }
                   1784: 
                   1785: sub usersearch {
                   1786:     my ($srch) = @_;
                   1787:     my $dom = $srch->{'srchdomain'};
                   1788:     my %results;
                   1789:     my %libserv = &all_library();
                   1790:     my $query = 'usersearch';
                   1791:     foreach my $tryserver (keys(%libserv)) {
                   1792:         if (&host_domain($tryserver) eq $dom) {
                   1793:             my $host=&hostname($tryserver);
                   1794:             my $queryid=
1.911     raeburn  1795:                 &reply("querysend:".&escape($query).':'.
                   1796:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  1797:                        &escape($srch->{'srchtype'}).':'.
                   1798:                        &escape($srch->{'srchterm'}),$tryserver);
                   1799:             if ($queryid !~/^\Q$host\E\_/) {
                   1800:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  1801:                 next;
1.899     raeburn  1802:             }
                   1803:             my $reply = &get_query_reply($queryid);
                   1804:             my $maxtries = 1;
                   1805:             my $tries = 1;
                   1806:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   1807:                 $reply = &get_query_reply($queryid);
                   1808:                 $tries ++;
                   1809:             }
                   1810:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   1811:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   1812:             } else {
1.911     raeburn  1813:                 my @matches;
                   1814:                 if ($reply =~ /\n/) {
                   1815:                     @matches = split(/\n/,$reply);
                   1816:                 } else {
                   1817:                     @matches = split(/\&/,$reply);
                   1818:                 }
1.899     raeburn  1819:                 foreach my $match (@matches) {
                   1820:                     my ($uname,$udom,%userhash);
1.911     raeburn  1821:                     foreach my $entry (split(/:/,$match)) {
                   1822:                         my ($key,$value) =
                   1823:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  1824:                         $userhash{$key} = $value;
                   1825:                         if ($key eq 'username') {
                   1826:                             $uname = $value;
                   1827:                         } elsif ($key eq 'domain') {
                   1828:                             $udom = $value;
1.911     raeburn  1829:                         }
1.899     raeburn  1830:                     }
                   1831:                     $results{$uname.':'.$udom} = \%userhash;
                   1832:                 }
                   1833:             }
                   1834:         }
                   1835:     }
                   1836:     return %results;
                   1837: }
                   1838: 
1.912     raeburn  1839: sub get_instuser {
                   1840:     my ($udom,$uname,$id) = @_;
                   1841:     my $homeserver = &domain($udom,'primary');
                   1842:     my ($outcome,%results);
                   1843:     if ($homeserver ne '') {
                   1844:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   1845:                            &escape($id).':'.&escape($udom),$homeserver);
                   1846:         my $host=&hostname($homeserver);
                   1847:         if ($queryid !~/^\Q$host\E\_/) {
                   1848:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1849:             return;
                   1850:         }
                   1851:         my $response = &get_query_reply($queryid);
                   1852:         my $maxtries = 5;
                   1853:         my $tries = 1;
                   1854:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1855:             $response = &get_query_reply($queryid);
                   1856:             $tries ++;
                   1857:         }
                   1858:         if (!&error($response) && $response ne 'refused') {
                   1859:             if ($response eq 'unavailable') {
                   1860:                 $outcome = $response;
                   1861:             } else {
                   1862:                 $outcome = 'ok';
                   1863:                 my @matches = split(/\n/,$response);
                   1864:                 foreach my $match (@matches) {
                   1865:                     my ($key,$value) = split(/=/,$match);
                   1866:                     $results{&unescape($key)} = &thaw_unescape($value);
                   1867:                 }
                   1868:             }
                   1869:         }
                   1870:     }
                   1871:     my %userinfo;
                   1872:     if (ref($results{$uname}) eq 'HASH') {
                   1873:         %userinfo = %{$results{$uname}};
                   1874:     } 
                   1875:     return ($outcome,%userinfo);
                   1876: }
                   1877: 
                   1878: sub inst_rulecheck {
1.923     raeburn  1879:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  1880:     my %returnhash;
                   1881:     if ($udom ne '') {
                   1882:         if (ref($rules) eq 'ARRAY') {
                   1883:             @{$rules} = map {&escape($_);} (@{$rules});
                   1884:             my $rulestr = join(':',@{$rules});
                   1885:             my $homeserver=&domain($udom,'primary');
                   1886:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1887:                 my $response;
                   1888:                 if ($item eq 'username') {                
                   1889:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   1890:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  1891:                                               $homeserver));
1.923     raeburn  1892:                 } elsif ($item eq 'id') {
                   1893:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   1894:                                               ':'.&escape($id).':'.$rulestr,
                   1895:                                               $homeserver));
1.945     raeburn  1896:                 } elsif ($item eq 'selfcreate') {
                   1897:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  1898:                                                &escape($udom).':'.&escape($uname).
                   1899:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  1900:                 }
1.912     raeburn  1901:                 if ($response ne 'refused') {
                   1902:                     my @pairs=split(/\&/,$response);
                   1903:                     foreach my $item (@pairs) {
                   1904:                         my ($key,$value)=split(/=/,$item,2);
                   1905:                         $key = &unescape($key);
                   1906:                         next if ($key =~ /^error: 2 /);
                   1907:                         $returnhash{$key}=&thaw_unescape($value);
                   1908:                     }
                   1909:                 }
                   1910:             }
                   1911:         }
                   1912:     }
                   1913:     return %returnhash;
                   1914: }
                   1915: 
                   1916: sub inst_userrules {
1.923     raeburn  1917:     my ($udom,$check) = @_;
1.912     raeburn  1918:     my (%ruleshash,@ruleorder);
                   1919:     if ($udom ne '') {
                   1920:         my $homeserver=&domain($udom,'primary');
                   1921:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1922:             my $response;
                   1923:             if ($check eq 'id') {
                   1924:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  1925:                                  $homeserver);
1.943     raeburn  1926:             } elsif ($check eq 'email') {
                   1927:                 $response=&reply('instemailrules:'.&escape($udom),
                   1928:                                  $homeserver);
1.923     raeburn  1929:             } else {
                   1930:                 $response=&reply('instuserrules:'.&escape($udom),
                   1931:                                  $homeserver);
                   1932:             }
1.912     raeburn  1933:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  1934:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  1935:                 ($response ne 'no_such_host')) {
                   1936:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   1937:                 my @pairs=split(/\&/,$hashitems);
                   1938:                 foreach my $item (@pairs) {
                   1939:                     my ($key,$value)=split(/=/,$item,2);
                   1940:                     $key = &unescape($key);
                   1941:                     next if ($key =~ /^error: 2 /);
                   1942:                     $ruleshash{$key}=&thaw_unescape($value);
                   1943:                 }
                   1944:                 my @esc_order = split(/\&/,$orderitems);
                   1945:                 foreach my $item (@esc_order) {
                   1946:                     push(@ruleorder,&unescape($item));
                   1947:                 }
                   1948:             }
                   1949:         }
                   1950:     }
                   1951:     return (\%ruleshash,\@ruleorder);
                   1952: }
                   1953: 
1.976     raeburn  1954: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  1955: 
                   1956: sub get_domain_defaults {
                   1957:     my ($domain) = @_;
                   1958:     my $cachetime = 60*60*24;
                   1959:     my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   1960:     if (defined($cached)) {
                   1961:         if (ref($result) eq 'HASH') {
                   1962:             return %{$result};
                   1963:         }
                   1964:     }
                   1965:     my %domdefaults;
                   1966:     my %domconfig =
1.989     raeburn  1967:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  1968:                                   'requestcourses','inststatus',
1.1183    raeburn  1969:                                   'coursedefaults','usersessions',
                   1970:                                   'requestauthor'],$domain);
1.943     raeburn  1971:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   1972:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   1973:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   1974:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  1975:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  1976:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  1977:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943     raeburn  1978:     } else {
                   1979:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   1980:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   1981:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   1982:     }
1.976     raeburn  1983:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   1984:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   1985:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   1986:         } else {
                   1987:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
1.1229  ! raeburn  1988:         }
1.1177    raeburn  1989:         my @usertools = ('aboutme','blog','webdav','portfolio');
1.976     raeburn  1990:         foreach my $item (@usertools) {
                   1991:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   1992:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   1993:             }
                   1994:         }
1.1229  ! raeburn  1995:         if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
        !          1996:             $domdefaults{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
        !          1997:         }
1.976     raeburn  1998:     }
1.985     raeburn  1999:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006    raeburn  2000:         foreach my $item ('official','unofficial','community') {
1.985     raeburn  2001:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   2002:         }
                   2003:     }
1.1183    raeburn  2004:     if (ref($domconfig{'requestauthor'}) eq 'HASH') {
                   2005:         $domdefaults{'requestauthor'} = $domconfig{'requestauthor'};
                   2006:     }
1.989     raeburn  2007:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
                   2008:         foreach my $item ('inststatustypes','inststatusorder') {
                   2009:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   2010:         }
                   2011:     }
1.1047    raeburn  2012:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
1.1229  ! raeburn  2013:         foreach my $item ('canuse_pdfforms','uploadquota') {
1.1047    raeburn  2014:             $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
                   2015:         }
1.1216    raeburn  2016:         if (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   2017:             $domdefaults{'officialcredits'} = $domconfig{'coursedefaults'}{'coursecredits'}{'official'};
                   2018:             $domdefaults{'unofficialcredits'} = $domconfig{'coursedefaults'}{'coursecredits'}{'unofficial'};
                   2019:         }
1.1047    raeburn  2020:     }
1.1073    raeburn  2021:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   2022:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   2023:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   2024:         }
                   2025:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   2026:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   2027:         }
                   2028:     }
1.1219    raeburn  2029:     &do_cache_new('domdefaults',$domain,\%domdefaults,$cachetime);
1.943     raeburn  2030:     return %domdefaults;
                   2031: }
                   2032: 
1.344     www      2033: # --------------------------------------------------- Assign a key to a student
                   2034: 
                   2035: sub assign_access_key {
1.364     www      2036: #
                   2037: # a valid key looks like uname:udom#comments
                   2038: # comments are being appended
                   2039: #
1.498     www      2040:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   2041:     $kdom=
1.620     albertel 2042:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      2043:     $knum=
1.620     albertel 2044:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      2045:     $cdom=
1.620     albertel 2046:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2047:     $cnum=
1.620     albertel 2048:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2049:     $udom=$env{'user.name'} unless (defined($udom));
                   2050:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      2051:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      2052:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 2053:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      2054:                                                   # assigned to this person
                   2055:                                                   # - this should not happen,
1.345     www      2056:                                                   # unless something went wrong
                   2057:                                                   # the first time around
                   2058: # ready to assign
1.364     www      2059:         $logentry=$1.'; '.$logentry;
1.496     www      2060:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      2061:                                                  $kdom,$knum) eq 'ok') {
1.345     www      2062: # key now belongs to user
1.346     www      2063: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      2064:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  2065:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      2066:                 return 'ok';
                   2067:             } else {
                   2068:                 return 
                   2069:   'error: Count not permanently assign key, will need to be re-entered later.';
                   2070: 	    }
                   2071:         } else {
                   2072:             return 'error: Could not assign key, try again later.';
                   2073:         }
1.364     www      2074:     } elsif (!$existing{$ckey}) {
1.345     www      2075: # the key does not exist
                   2076: 	return 'error: The key does not exist';
                   2077:     } else {
                   2078: # the key is somebody else's
                   2079: 	return 'error: The key is already in use';
                   2080:     }
1.344     www      2081: }
                   2082: 
1.364     www      2083: # ------------------------------------------ put an additional comment on a key
                   2084: 
                   2085: sub comment_access_key {
                   2086: #
                   2087: # a valid key looks like uname:udom#comments
                   2088: # comments are being appended
                   2089: #
                   2090:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   2091:     $cdom=
1.620     albertel 2092:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      2093:     $cnum=
1.620     albertel 2094:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      2095:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   2096:     if ($existing{$ckey}) {
                   2097:         $existing{$ckey}.='; '.$logentry;
                   2098: # ready to assign
1.367     www      2099:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      2100:                                                  $cdom,$cnum) eq 'ok') {
                   2101: 	    return 'ok';
                   2102:         } else {
                   2103: 	    return 'error: Count not store comment.';
                   2104:         }
                   2105:     } else {
                   2106: # the key does not exist
                   2107: 	return 'error: The key does not exist';
                   2108:     }
                   2109: }
                   2110: 
1.344     www      2111: # ------------------------------------------------------ Generate a set of keys
                   2112: 
                   2113: sub generate_access_keys {
1.364     www      2114:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      2115:     $cdom=
1.620     albertel 2116:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2117:     $cnum=
1.620     albertel 2118:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      2119:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      2120:     unless (($cdom) && ($cnum)) { return 0; }
                   2121:     if ($number>10000) { return 0; }
                   2122:     sleep(2); # make sure don't get same seed twice
                   2123:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   2124:     my $total=0;
                   2125:     for (my $i=1;$i<=$number;$i++) {
                   2126:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   2127:                   sprintf("%lx",int(100000*rand)).'-'.
                   2128:                   sprintf("%lx",int(100000*rand));
                   2129:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   2130:        $newkey=~s/0/h/g; # and also 0 and O
                   2131:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   2132:        if ($existing{$newkey}) {
                   2133:            $i--;
                   2134:        } else {
1.364     www      2135: 	  if (&put('accesskeys',
                   2136:               { $newkey => '# generated '.localtime().
1.620     albertel 2137:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      2138:                            '; '.$logentry },
                   2139: 		   $cdom,$cnum) eq 'ok') {
1.344     www      2140:               $total++;
                   2141: 	  }
                   2142:        }
                   2143:     }
1.620     albertel 2144:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      2145:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   2146:     return $total;
                   2147: }
                   2148: 
                   2149: # ------------------------------------------------------- Validate an accesskey
                   2150: 
                   2151: sub validate_access_key {
                   2152:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   2153:     $cdom=
1.620     albertel 2154:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2155:     $cnum=
1.620     albertel 2156:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2157:     $udom=$env{'user.domain'} unless (defined($udom));
                   2158:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      2159:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 2160:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      2161: }
                   2162: 
                   2163: # ------------------------------------- Find the section of student in a course
1.652     albertel 2164: sub devalidate_getsection_cache {
                   2165:     my ($udom,$unam,$courseid)=@_;
                   2166:     my $hashid="$udom:$unam:$courseid";
                   2167:     &devalidate_cache_new('getsection',$hashid);
                   2168: }
1.298     matthew  2169: 
1.815     albertel 2170: sub courseid_to_courseurl {
                   2171:     my ($courseid) = @_;
                   2172:     #already url style courseid
                   2173:     return $courseid if ($courseid =~ m{^/});
                   2174: 
                   2175:     if (exists($env{'course.'.$courseid.'.num'})) {
                   2176: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   2177: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   2178: 	return "/$cdom/$cnum";
                   2179:     }
                   2180: 
                   2181:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   2182:     if (exists($courseinfo{'num'})) {
                   2183: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   2184:     }
                   2185: 
                   2186:     return undef;
                   2187: }
                   2188: 
1.298     matthew  2189: sub getsection {
                   2190:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 2191:     my $cachetime=1800;
1.551     albertel 2192: 
                   2193:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 2194:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 2195:     if (defined($cached)) { return $result; }
                   2196: 
1.298     matthew  2197:     my %Pending; 
                   2198:     my %Expired;
                   2199:     #
                   2200:     # Each role can either have not started yet (pending), be active, 
                   2201:     #    or have expired.
                   2202:     #
                   2203:     # If there is an active role, we are done.
                   2204:     #
                   2205:     # If there is more than one role which has not started yet, 
                   2206:     #     choose the one which will start sooner
                   2207:     # If there is one role which has not started yet, return it.
                   2208:     #
                   2209:     # If there is more than one expired role, choose the one which ended last.
                   2210:     # If there is a role which has expired, return it.
                   2211:     #
1.815     albertel 2212:     $courseid = &courseid_to_courseurl($courseid);
1.1166    raeburn  2213:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
1.817     raeburn  2214:     foreach my $key (keys(%roleshash)) {
1.479     albertel 2215:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  2216:         my $section=$1;
                   2217:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  2218:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  2219:         my $now=time;
1.548     albertel 2220:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  2221:             $Expired{$end}=$section;
                   2222:             next;
                   2223:         }
1.548     albertel 2224:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  2225:             $Pending{$start}=$section;
                   2226:             next;
                   2227:         }
1.599     albertel 2228:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  2229:     }
                   2230:     #
                   2231:     # Presumedly there will be few matching roles from the above
                   2232:     # loop and the sorting time will be negligible.
                   2233:     if (scalar(keys(%Pending))) {
                   2234:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 2235:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  2236:     } 
                   2237:     if (scalar(keys(%Expired))) {
                   2238:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   2239:         my $time = pop(@sorted);
1.599     albertel 2240:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  2241:     }
1.599     albertel 2242:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  2243: }
1.70      www      2244: 
1.599     albertel 2245: sub save_cache {
                   2246:     &purge_remembered();
1.722     albertel 2247:     #&Apache::loncommon::validate_page();
1.620     albertel 2248:     undef(%env);
1.780     albertel 2249:     undef($env_loaded);
1.599     albertel 2250: }
1.452     albertel 2251: 
1.599     albertel 2252: my $to_remember=-1;
                   2253: my %remembered;
                   2254: my %accessed;
                   2255: my $kicks=0;
                   2256: my $hits=0;
1.849     albertel 2257: sub make_key {
                   2258:     my ($name,$id) = @_;
1.872     albertel 2259:     if (length($id) > 65 
                   2260: 	&& length(&escape($id)) > 200) {
                   2261: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   2262:     }
1.849     albertel 2263:     return &escape($name.':'.$id);
                   2264: }
                   2265: 
1.599     albertel 2266: sub devalidate_cache_new {
                   2267:     my ($name,$id,$debug) = @_;
                   2268:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 2269:     $id=&make_key($name,$id);
1.599     albertel 2270:     $memcache->delete($id);
                   2271:     delete($remembered{$id});
                   2272:     delete($accessed{$id});
                   2273: }
                   2274: 
                   2275: sub is_cached_new {
                   2276:     my ($name,$id,$debug) = @_;
1.849     albertel 2277:     $id=&make_key($name,$id);
1.599     albertel 2278:     if (exists($remembered{$id})) {
1.1133    foxr     2279: 	if ($debug) { &Apache::lonnet::logthis("Early return $id of $remembered{$id} "); }
1.599     albertel 2280: 	$accessed{$id}=[&gettimeofday()];
                   2281: 	$hits++;
                   2282: 	return ($remembered{$id},1);
                   2283:     }
                   2284:     my $value = $memcache->get($id);
                   2285:     if (!(defined($value))) {
                   2286: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 2287: 	return (undef,undef);
1.416     albertel 2288:     }
1.599     albertel 2289:     if ($value eq '__undef__') {
                   2290: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   2291: 	$value=undef;
                   2292:     }
                   2293:     &make_room($id,$value,$debug);
                   2294:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   2295:     return ($value,1);
                   2296: }
                   2297: 
                   2298: sub do_cache_new {
                   2299:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 2300:     $id=&make_key($name,$id);
1.599     albertel 2301:     my $setvalue=$value;
                   2302:     if (!defined($setvalue)) {
                   2303: 	$setvalue='__undef__';
                   2304:     }
1.623     albertel 2305:     if (!defined($time) ) {
                   2306: 	$time=600;
                   2307:     }
1.599     albertel 2308:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 2309:     my $result = $memcache->set($id,$setvalue,$time);
                   2310:     if (! $result) {
1.872     albertel 2311: 	&logthis("caching of id -> $id  failed");
1.910     albertel 2312: 	$memcache->disconnect_all();
1.872     albertel 2313:     }
1.600     albertel 2314:     # need to make a copy of $value
1.919     albertel 2315:     &make_room($id,$value,$debug);
1.599     albertel 2316:     return $value;
                   2317: }
                   2318: 
                   2319: sub make_room {
                   2320:     my ($id,$value,$debug)=@_;
1.919     albertel 2321: 
                   2322:     $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
                   2323:                                     : $value;
1.599     albertel 2324:     if ($to_remember<0) { return; }
                   2325:     $accessed{$id}=[&gettimeofday()];
                   2326:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   2327:     my $to_kick;
                   2328:     my $max_time=0;
                   2329:     foreach my $other (keys(%accessed)) {
                   2330: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   2331: 	    $to_kick=$other;
                   2332: 	    $max_time=&tv_interval($accessed{$other});
                   2333: 	}
                   2334:     }
                   2335:     delete($remembered{$to_kick});
                   2336:     delete($accessed{$to_kick});
                   2337:     $kicks++;
                   2338:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 2339:     return;
                   2340: }
                   2341: 
1.599     albertel 2342: sub purge_remembered {
1.604     albertel 2343:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   2344:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 2345:     undef(%remembered);
                   2346:     undef(%accessed);
1.428     albertel 2347: }
1.70      www      2348: # ------------------------------------- Read an entry from a user's environment
                   2349: 
                   2350: sub userenvironment {
                   2351:     my ($udom,$unam,@what)=@_;
1.976     raeburn  2352:     my $items;
                   2353:     foreach my $item (@what) {
                   2354:         $items.=&escape($item).'&';
                   2355:     }
                   2356:     $items=~s/\&$//;
1.70      www      2357:     my %returnhash=();
1.1009    raeburn  2358:     my $uhome = &homeserver($unam,$udom);
                   2359:     unless ($uhome eq 'no_host') {
                   2360:         my @answer=split(/\&/, 
                   2361:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  2362:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   2363:             return %returnhash;
                   2364:         }
1.1009    raeburn  2365:         my $i;
                   2366:         for ($i=0;$i<=$#what;$i++) {
                   2367: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   2368:         }
1.70      www      2369:     }
                   2370:     return %returnhash;
1.1       albertel 2371: }
                   2372: 
1.617     albertel 2373: # ---------------------------------------------------------- Get a studentphoto
                   2374: sub studentphoto {
                   2375:     my ($udom,$unam,$ext) = @_;
                   2376:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  2377:     if (defined($env{'request.course.id'})) {
1.708     raeburn  2378:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  2379:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   2380:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   2381:             } else {
                   2382:                 my ($result,$perm_reqd)=
1.707     albertel 2383: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2384:                 if ($result eq 'ok') {
                   2385:                     if (!($perm_reqd eq 'yes')) {
                   2386:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   2387:                     }
                   2388:                 }
                   2389:             }
                   2390:         }
                   2391:     } else {
                   2392:         my ($result,$perm_reqd) = 
1.707     albertel 2393: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2394:         if ($result eq 'ok') {
                   2395:             if (!($perm_reqd eq 'yes')) {
                   2396:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   2397:             }
                   2398:         }
                   2399:     }
                   2400:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   2401: }
                   2402: 
                   2403: sub retrievestudentphoto {
                   2404:     my ($udom,$unam,$ext,$type) = @_;
                   2405:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   2406:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   2407:     if ($ret eq 'ok') {
                   2408:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   2409:         if ($type eq 'thumbnail') {
                   2410:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   2411:         }
                   2412:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   2413:         return $tokenurl;
                   2414:     } else {
                   2415:         if ($type eq 'thumbnail') {
                   2416:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   2417:         } else { 
                   2418:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   2419:         }
1.617     albertel 2420:     }
                   2421: }
                   2422: 
1.263     www      2423: # -------------------------------------------------------------------- New chat
                   2424: 
                   2425: sub chatsend {
1.724     raeburn  2426:     my ($newentry,$anon,$group)=@_;
1.620     albertel 2427:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2428:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2429:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      2430:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 2431: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  2432: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      2433: }
                   2434: 
                   2435: # ------------------------------------------ Find current version of a resource
                   2436: 
                   2437: sub getversion {
                   2438:     my $fname=&clutter(shift);
1.1189    raeburn  2439:     unless ($fname=~m{^(/adm/wrapper|)/res/}) { return -1; }
1.292     www      2440:     return &currentversion(&filelocation('',$fname));
                   2441: }
                   2442: 
                   2443: sub currentversion {
                   2444:     my $fname=shift;
                   2445:     my $author=$fname;
                   2446:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2447:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      2448:     my $home=&homeserver($uname,$udom);
1.292     www      2449:     if ($home eq 'no_host') { 
                   2450:         return -1; 
                   2451:     }
1.1112    www      2452:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      2453:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2454: 	return -1;
                   2455:     }
1.1112    www      2456:     return $answer;
1.263     www      2457: }
                   2458: 
1.1111    www      2459: #
                   2460: # Return special version number of resource if set by override, empty otherwise
                   2461: #
                   2462: sub usedversion {
                   2463:     my $fname=shift;
                   2464:     unless ($fname) { $fname=$env{'request.uri'}; }
                   2465:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   2466:     if ($urlversion) { return $urlversion; }
                   2467:     return '';
                   2468: }
                   2469: 
1.1       albertel 2470: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      2471: 
1.1       albertel 2472: sub subscribe {
                   2473:     my $fname=shift;
1.761     raeburn  2474:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 2475:     $fname=~s/[\n\r]//g;
1.1       albertel 2476:     my $author=$fname;
                   2477:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2478:     my ($udom,$uname)=split(/\//,$author);
                   2479:     my $home=homeserver($uname,$udom);
1.335     albertel 2480:     if ($home eq 'no_host') {
                   2481:         return 'not_found';
1.1       albertel 2482:     }
                   2483:     my $answer=reply("sub:$fname",$home);
1.64      www      2484:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2485: 	$answer.=' by '.$home;
                   2486:     }
1.1       albertel 2487:     return $answer;
                   2488: }
                   2489:     
1.8       www      2490: # -------------------------------------------------------------- Replicate file
                   2491: 
                   2492: sub repcopy {
                   2493:     my $filename=shift;
1.23      www      2494:     $filename=~s/\/+/\//g;
1.1142    raeburn  2495:     my $londocroot = $perlvar{'lonDocRoot'};
                   2496:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
1.1164    raeburn  2497:     if ($filename=~m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.1142    raeburn  2498:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   2499: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 2500: 	return &repcopy_userfile($filename);
                   2501:     }
1.532     albertel 2502:     $filename=~s/[\n\r]//g;
1.8       www      2503:     my $transname="$filename.in.transfer";
1.828     www      2504: # FIXME: this should flock
1.607     raeburn  2505:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      2506:     my $remoteurl=subscribe($filename);
1.64      www      2507:     if ($remoteurl =~ /^con_lost by/) {
                   2508: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2509:            return 'unavailable';
1.8       www      2510:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 2511: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  2512: 	   return 'not_found';
1.64      www      2513:     } elsif ($remoteurl =~ /^rejected by/) {
                   2514: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2515:            return 'forbidden';
1.20      www      2516:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  2517:            return 'ok';
1.8       www      2518:     } else {
1.290     www      2519:         my $author=$filename;
                   2520:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2521:         my ($udom,$uname)=split(/\//,$author);
                   2522:         my $home=homeserver($uname,$udom);
                   2523:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      2524:            my @parts=split(/\//,$filename);
                   2525:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  2526:            if ($path ne "$londocroot/res") {
1.8       www      2527:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  2528: 	       return 'bad_request';
1.8       www      2529:            }
                   2530:            my $count;
                   2531:            for ($count=5;$count<$#parts;$count++) {
                   2532:                $path.="/$parts[$count]";
                   2533:                if ((-e $path)!=1) {
                   2534: 		   mkdir($path,0777);
                   2535:                }
                   2536:            }
                   2537:            my $ua=new LWP::UserAgent;
                   2538:            my $request=new HTTP::Request('GET',"$remoteurl");
                   2539:            my $response=$ua->request($request,$transname);
                   2540:            if ($response->is_error()) {
                   2541: 	       unlink($transname);
                   2542:                my $message=$response->status_line;
1.672     albertel 2543:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      2544:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  2545:                return 'unavailable';
1.8       www      2546:            } else {
1.16      www      2547: 	       if ($remoteurl!~/\.meta$/) {
                   2548:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   2549:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   2550:                   if ($mresponse->is_error()) {
                   2551: 		      unlink($filename.'.meta');
                   2552:                       &logthis(
1.672     albertel 2553:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      2554:                   }
                   2555: 	       }
1.8       www      2556:                rename($transname,$filename);
1.607     raeburn  2557:                return 'ok';
1.8       www      2558:            }
1.290     www      2559:        }
1.8       www      2560:     }
1.330     www      2561: }
                   2562: 
                   2563: # ------------------------------------------------ Get server side include body
                   2564: sub ssi_body {
1.381     albertel 2565:     my ($filelink,%form)=@_;
1.606     matthew  2566:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   2567:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   2568:     }
1.953     www      2569:     my $output='';
                   2570:     my $response;
1.980     raeburn  2571:     if ($filelink=~/^https?\:/) {
1.954     raeburn  2572:        ($output,$response)=&externalssi($filelink);
1.953     www      2573:     } else {
1.1004    droeschl 2574:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   2575:        $filelink .= 'inhibitmenu=yes';
1.953     www      2576:        ($output,$response)=&ssi($filelink,%form);
                   2577:     }
1.778     albertel 2578:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 2579:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 2580:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      2581:     if (wantarray) {
                   2582:         return ($output, $response);
                   2583:     } else {
                   2584:         return $output;
                   2585:     }
1.8       www      2586: }
                   2587: 
1.15      www      2588: # --------------------------------------------------------- Server Side Include
                   2589: 
1.782     albertel 2590: sub absolute_url {
                   2591:     my ($host_name) = @_;
                   2592:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   2593:     if ($host_name eq '') {
                   2594: 	$host_name = $ENV{'SERVER_NAME'};
                   2595:     }
                   2596:     return $protocol.$host_name;
                   2597: }
                   2598: 
1.942     foxr     2599: #
                   2600: #   Server side include.
                   2601: # Parameters:
                   2602: #  fn     Possibly encrypted resource name/id.
                   2603: #  form   Hash that describes how the rendering should be done
                   2604: #         and other things.
1.944     foxr     2605: # Returns:
1.950     raeburn  2606: #   Scalar context: The content of the response.
                   2607: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     2608: #     
1.15      www      2609: sub ssi {
                   2610: 
1.944     foxr     2611:     my ($fn,%form)=@_;
1.15      www      2612:     my $ua=new LWP::UserAgent;
1.23      www      2613:     my $request;
1.711     albertel 2614: 
                   2615:     $form{'no_update_last_known'}=1;
1.895     albertel 2616:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      2617:     if (%form) {
1.782     albertel 2618:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000    raeburn  2619:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23      www      2620:     } else {
1.782     albertel 2621:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      2622:     }
                   2623: 
1.15      www      2624:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
1.1173    foxr     2625:     my $response= $ua->request($request);
1.1182    foxr     2626:     my $content = $response->content;
                   2627: 
                   2628: 
1.944     foxr     2629:     if (wantarray) {
1.1173    foxr     2630: 	return ($content, $response);
1.944     foxr     2631:     } else {
1.1173    foxr     2632: 	return $content;
1.942     foxr     2633:     }
1.324     www      2634: }
                   2635: 
                   2636: sub externalssi {
                   2637:     my ($url)=@_;
                   2638:     my $ua=new LWP::UserAgent;
                   2639:     my $request=new HTTP::Request('GET',$url);
                   2640:     my $response=$ua->request($request);
1.954     raeburn  2641:     if (wantarray) {
                   2642:         return ($response->content, $response);
                   2643:     } else {
                   2644:         return $response->content;
                   2645:     }
1.15      www      2646: }
1.254     www      2647: 
1.492     albertel 2648: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   2649: 
                   2650: sub allowuploaded {
                   2651:     my ($srcurl,$url)=@_;
                   2652:     $url=&clutter(&declutter($url));
                   2653:     my $dir=$url;
                   2654:     $dir=~s/\/[^\/]+$//;
                   2655:     my %httpref=();
                   2656:     my $httpurl=&hreflocation('',$url);
                   2657:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  2658:     &Apache::lonnet::appenv(\%httpref);
1.254     www      2659: }
1.477     raeburn  2660: 
1.1193    raeburn  2661: #
                   2662: # Determine if the current user should be able to edit a particular resource,
                   2663: # when viewing in course context.
                   2664: # (a) When viewing resource used to determine if "Edit" item is included in 
                   2665: #     Functions.
                   2666: # (b) When displaying folder contents in course editor, used to determine if
                   2667: #     "Edit" link will be displayed alongside resource.
                   2668: #
1.1196    raeburn  2669: #  input: six args -- filename (decluttered), course number, course domain,
                   2670: #                   url, symb (if registered) and group (if this is a group
                   2671: #                   item -- e.g., bulletin board, group page etc.).
                   2672: #  output: array of five scalars -- 
1.1193    raeburn  2673: #          $cfile -- url for file editing if editable on current server
                   2674: #          $home -- homeserver of resource (i.e., for author if published,
                   2675: #                                           or course if uploaded.).
                   2676: #          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  2677: #          $forceedit -- 1 if icon/link should be to go to edit mode 
                   2678: #          $forceview -- 1 if icon/link should be to go to view mode
1.1193    raeburn  2679: #
                   2680: 
                   2681: sub can_edit_resource {
1.1194    raeburn  2682:     my ($file,$cnum,$cdom,$resurl,$symb,$group) = @_;
                   2683:     my ($cfile,$home,$switchserver,$forceedit,$forceview,$uploaded,$incourse);
                   2684: #
                   2685: # For aboutme pages user can only edit his/her own.
                   2686: #
1.1199    raeburn  2687:     if ($resurl =~ m{^/?adm/($match_domain)/($match_username)/aboutme$}) {
1.1194    raeburn  2688:         my ($sdom,$sname) = ($1,$2);
                   2689:         if (($sdom eq $env{'user.domain'}) && ($sname eq $env{'user.name'})) {
                   2690:             $home = $env{'user.home'};
                   2691:             $cfile = $resurl;
                   2692:             if ($env{'form.forceedit'}) {
                   2693:                 $forceview = 1;
                   2694:             } else {
                   2695:                 $forceedit = 1;
                   2696:             }
                   2697:             return ($cfile,$home,$switchserver,$forceedit,$forceview);
                   2698:         } else {
                   2699:             return;
                   2700:         }
                   2701:     }
                   2702: 
                   2703:     if ($env{'request.course.id'}) {
                   2704:         my $crsedit = &Apache::lonnet::allowed('mdc',$env{'request.course.id'});
                   2705:         if ($group ne '') {
                   2706: # if this is a group homepage or group bulletin board, check group privs
                   2707:             my $allowed = 0;
1.1197    raeburn  2708:             if ($resurl =~ m{^/?adm/$cdom/$cnum/$group/smppg$}) {
                   2709:                 if ((&allowed('mdg',$env{'request.course.id'}.
1.1198    raeburn  2710:                               ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
1.1194    raeburn  2711:                         (&allowed('mgh',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
                   2712:                     $allowed = 1;
                   2713:                 }
1.1197    raeburn  2714:             } elsif ($resurl =~ m{^/?adm/$cdom/$cnum/\d+/bulletinboard$}) {
                   2715:                 if ((&allowed('mdg',$env{'request.course.id'}.($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''))) ||
                   2716:                         (&allowed('cgb',$env{'request.course.id'}.'/'.$group)) || $crsedit) {
1.1194    raeburn  2717:                     $allowed = 1;
                   2718:                 }
                   2719:             }
                   2720:             if ($allowed) {
                   2721:                 $home=&homeserver($cnum,$cdom);
                   2722:                 if ($env{'form.forceedit'}) {
                   2723:                     $forceview = 1;
                   2724:                 } else {
                   2725:                     $forceedit = 1;
                   2726:                 }
                   2727:                 $cfile = $resurl;
                   2728:             } else {
                   2729:                 return;
                   2730:             }
                   2731:         } else {
1.1208    raeburn  2732:             if ($resurl =~ m{^/?adm/viewclasslist$}) {
                   2733:                 unless (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) {
                   2734:                     return;
                   2735:                 }
                   2736:             } elsif (!$crsedit) {
1.1194    raeburn  2737: #
                   2738: # No edit allowed where CC has switched to student role.
                   2739: #
                   2740:                 return;
                   2741:             }
                   2742:         }
                   2743:     }
                   2744: 
1.1193    raeburn  2745:     if ($file ne '') {
                   2746:         if (($cnum =~ /$match_courseid/) && ($cdom =~ /$match_domain/)) {
1.1194    raeburn  2747:             if (&is_course_upload($file,$cnum,$cdom)) {
                   2748:                 $uploaded = 1;
                   2749:                 $incourse = 1;
1.1193    raeburn  2750:                 if ($file =~/\.(htm|html|css|js|txt)$/) {
                   2751:                     $cfile = &hreflocation('',$file);
1.1201    raeburn  2752:                     if ($env{'form.forceedit'}) {
                   2753:                         $forceview = 1;
                   2754:                     } else {
                   2755:                         $forceedit = 1;
                   2756:                     }
1.1194    raeburn  2757:                 }
                   2758:             } elsif ($resurl =~ m{^/public/$cdom/$cnum/syllabus}) {
                   2759:                 $incourse = 1;
                   2760:                 if ($env{'form.forceedit'}) {
                   2761:                     $forceview = 1;
                   2762:                 } else {
                   2763:                     $forceedit = 1;
                   2764:                 }
                   2765:                 $cfile = $resurl;
                   2766:             } elsif (($resurl ne '') && (&is_on_map($resurl))) { 
                   2767:                 if ($resurl =~ m{^/adm/$match_domain/$match_username/\d+/smppg|bulletinboard$}) {
                   2768:                     $incourse = 1;
                   2769:                     if ($env{'form.forceedit'}) {
                   2770:                         $forceview = 1;
                   2771:                     } else {
                   2772:                         $forceedit = 1;
                   2773:                     }
                   2774:                     $cfile = $resurl;
1.1199    raeburn  2775:                 } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem') {
1.1194    raeburn  2776:                     $incourse = 1;
                   2777:                     $cfile = $resurl.'/smpedit';
1.1199    raeburn  2778:                 } elsif ($resurl =~ m{^/adm/wrapper/ext/}) {
1.1194    raeburn  2779:                     $incourse = 1;
1.1199    raeburn  2780:                     if ($env{'form.forceedit'}) {
                   2781:                         $forceview = 1;
                   2782:                     } else {
                   2783:                         $forceedit = 1;
                   2784:                     }
                   2785:                     $cfile = $resurl;
1.1208    raeburn  2786:                 } elsif ($resurl =~ m{^/?adm/viewclasslist$}) {
                   2787:                     $incourse = 1;
                   2788:                     if ($env{'form.forceedit'}) {
                   2789:                         $forceview = 1;
                   2790:                     } else {
                   2791:                         $forceedit = 1;
                   2792:                     }
                   2793:                     $cfile = ($resurl =~ m{^/} ? $resurl : "/$resurl");
1.1194    raeburn  2794:                 }
                   2795:             } elsif ($resurl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
                   2796:                 my $template = '/res/lib/templates/simpleproblem.problem';
                   2797:                 if (&is_on_map($template)) { 
                   2798:                     $incourse = 1;
                   2799:                     $forceview = 1;
                   2800:                     $cfile = $template;
1.1193    raeburn  2801:                 }
1.1199    raeburn  2802:             } elsif (($resurl =~ m{^/adm/wrapper/ext/}) && ($env{'form.folderpath'} =~ /^supplemental/)) {
                   2803:                     $incourse = 1;
                   2804:                     if ($env{'form.forceedit'}) {
                   2805:                         $forceview = 1;
                   2806:                     } else {
                   2807:                         $forceedit = 1;
                   2808:                     }
                   2809:                     $cfile = $resurl;
                   2810:             } elsif (($resurl eq '/adm/extresedit') && ($symb || $env{'form.folderpath'})) {
                   2811:                 $incourse = 1;
                   2812:                 $forceview = 1;
                   2813:                 if ($symb) {
                   2814:                     my ($map,$id,$res)=&decode_symb($symb);
                   2815:                     $env{'request.symb'} = $symb;
                   2816:                     $cfile = &clutter($res);
                   2817:                 } else {
                   2818:                     $cfile = $env{'form.suppurl'};
                   2819:                     $cfile =~ s{^http://}{};
                   2820:                     $cfile = '/adm/wrapper/ext/'.$cfile;
                   2821:                 }
1.1193    raeburn  2822:             }
                   2823:         }
1.1194    raeburn  2824:         if ($uploaded || $incourse) {
                   2825:             $home=&homeserver($cnum,$cdom);
1.1207    raeburn  2826:         } elsif ($file !~ m{/$}) {
1.1193    raeburn  2827:             $file=~s{^(priv/$match_domain/$match_username)}{/$1};
                   2828:             $file=~s{^($match_domain/$match_username)}{/priv/$1};
                   2829:             # Check that the user has permission to edit this resource
                   2830:             my $setpriv = 1;
                   2831:             my ($cfuname,$cfudom)=&constructaccess($file,$setpriv);
                   2832:             if (defined($cfudom)) {
                   2833:                 $home=&homeserver($cfuname,$cfudom);
                   2834:                 $cfile=$file;
                   2835:             }
                   2836:         }
1.1194    raeburn  2837:         if (($cfile ne '') && (!$incourse || $uploaded) && 
                   2838:             (($home ne '') && ($home ne 'no_host'))) {
1.1193    raeburn  2839:             my @ids=&current_machine_ids();
                   2840:             unless (grep(/^\Q$home\E$/,@ids)) {
                   2841:                 $switchserver=1;
                   2842:             }
                   2843:         }
                   2844:     }
1.1194    raeburn  2845:     return ($cfile,$home,$switchserver,$forceedit,$forceview);
1.1193    raeburn  2846: }
                   2847: 
                   2848: sub is_course_upload {
                   2849:     my ($file,$cnum,$cdom) = @_;
                   2850:     my $uploadpath = &LONCAPA::propath($cdom,$cnum);
                   2851:     $uploadpath =~ s{^\/}{};
1.1201    raeburn  2852:     if (($file =~ m{^\Q$uploadpath\E/userfiles/(docs|supplemental)/}) ||
                   2853:         ($file =~ m{^userfiles/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/})) {
1.1193    raeburn  2854:         return 1;
                   2855:     }
                   2856:     return;
                   2857: }
                   2858: 
1.1194    raeburn  2859: sub in_course {
1.1195    raeburn  2860:     my ($udom,$uname,$cdom,$cnum,$type,$hideprivileged) = @_;
                   2861:     if ($hideprivileged) {
                   2862:         my $skipuser;
1.1219    raeburn  2863:         my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   2864:         my @possdoms = ($cdom);  
                   2865:         if ($coursehash{'checkforpriv'}) { 
                   2866:             push(@possdoms,split(/,/,$coursehash{'checkforpriv'})); 
                   2867:         }
                   2868:         if (&privileged($uname,$udom,\@possdoms)) {
1.1195    raeburn  2869:             $skipuser = 1;
                   2870:             if ($coursehash{'nothideprivileged'}) {
                   2871:                 foreach my $item (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2872:                     my $user;
                   2873:                     if ($item =~ /:/) {
                   2874:                         $user = $item;
                   2875:                     } else {
                   2876:                         $user = join(':',split(/[\@]/,$item));
                   2877:                     }
                   2878:                     if ($user eq $uname.':'.$udom) {
                   2879:                         undef($skipuser);
                   2880:                         last;
                   2881:                     }
                   2882:                 }
                   2883:             }
                   2884:             if ($skipuser) {
                   2885:                 return 0;
                   2886:             }
                   2887:         }
                   2888:     }
1.1194    raeburn  2889:     $type ||= 'any';
                   2890:     if (!defined($cdom) || !defined($cnum)) {
                   2891:         my $cid  = $env{'request.course.id'};
                   2892:         $cdom = $env{'course.'.$cid.'.domain'};
                   2893:         $cnum = $env{'course.'.$cid.'.num'};
                   2894:     }
                   2895:     my $typesref;
1.1195    raeburn  2896:     if (($type eq 'any') || ($type eq 'all')) {
1.1194    raeburn  2897:         $typesref = ['active','previous','future'];
                   2898:     } elsif ($type eq 'previous' || $type eq 'future') {
                   2899:         $typesref = [$type];
                   2900:     }
                   2901:     my %roles = &get_my_roles($uname,$udom,'userroles',
                   2902:                               $typesref,undef,[$cdom]);
                   2903:     my ($tmp) = keys(%roles);
                   2904:     return 0 if ($tmp =~ /^(con_lost|error|no_such_host)/i);
                   2905:     my @course_roles = grep(/^\Q$cnum\E:\Q$cdom\E:/, keys(%roles));
                   2906:     if (@course_roles > 0) {
                   2907:         return 1;
                   2908:     }
                   2909:     return 0;
                   2910: }
                   2911: 
1.478     albertel 2912: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 2913: # input: action, courseID, current domain, intended
1.637     raeburn  2914: #        path to file, source of file, instruction to parse file for objects,
                   2915: #        ref to hash for embedded objects,
                   2916: #        ref to hash for codebase of java objects.
1.1095    raeburn  2917: #        reference to scalar to accommodate mime type determined
                   2918: #          from File::MMagic if $parser = parse.
1.637     raeburn  2919: #
1.485     raeburn  2920: # output: url to file (if action was uploaddoc), 
                   2921: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  2922: #
1.478     albertel 2923: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   2924: # course.
1.477     raeburn  2925: #
1.478     albertel 2926: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2927: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   2928: #          course's home server.
1.477     raeburn  2929: #
1.478     albertel 2930: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   2931: #          be copied from $source (current location) to 
                   2932: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2933: #         and will then be copied to
                   2934: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   2935: #         course's home server.
1.485     raeburn  2936: #
1.481     raeburn  2937: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 2938: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  2939: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2940: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   2941: #         in course's home server.
1.637     raeburn  2942: #
1.477     raeburn  2943: 
                   2944: sub process_coursefile {
1.1095    raeburn  2945:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   2946:         $mimetype)=@_;
1.477     raeburn  2947:     my $fetchresult;
1.638     albertel 2948:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  2949:     if ($action eq 'propagate') {
1.638     albertel 2950:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   2951: 			     $home);
1.481     raeburn  2952:     } else {
1.477     raeburn  2953:         my $fpath = '';
                   2954:         my $fname = $file;
1.478     albertel 2955:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  2956:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  2957:         my $filepath = &build_filepath($fpath);
1.481     raeburn  2958:         if ($action eq 'copy') {
                   2959:             if ($source eq '') {
                   2960:                 $fetchresult = 'no source file';
                   2961:                 return $fetchresult;
                   2962:             } else {
                   2963:                 my $destination = $filepath.'/'.$fname;
                   2964:                 rename($source,$destination);
                   2965:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2966:                                  $home);
1.481     raeburn  2967:             }
                   2968:         } elsif ($action eq 'uploaddoc') {
                   2969:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 2970:             print $fh $env{'form.'.$source};
1.481     raeburn  2971:             close($fh);
1.637     raeburn  2972:             if ($parser eq 'parse') {
1.1024    raeburn  2973:                 my $mm = new File::MMagic;
1.1095    raeburn  2974:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   2975:                 if ($type eq 'text/html') {
1.1024    raeburn  2976:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   2977:                     unless ($parse_result eq 'ok') {
                   2978:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   2979:                     }
1.637     raeburn  2980:                 }
1.1095    raeburn  2981:                 if (ref($mimetype)) {
                   2982:                     $$mimetype = $type;
                   2983:                 } 
1.637     raeburn  2984:             }
1.477     raeburn  2985:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2986:                                  $home);
1.481     raeburn  2987:             if ($fetchresult eq 'ok') {
                   2988:                 return '/uploaded/'.$fpath.'/'.$fname;
                   2989:             } else {
                   2990:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2991:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  2992:                 return '/adm/notfound.html';
                   2993:             }
1.477     raeburn  2994:         }
                   2995:     }
1.485     raeburn  2996:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  2997:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2998:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  2999:     }
                   3000:     return $fetchresult;
                   3001: }
                   3002: 
1.637     raeburn  3003: sub build_filepath {
                   3004:     my ($fpath) = @_;
                   3005:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   3006:     unless ($fpath eq '') {
                   3007:         my @parts=split('/',$fpath);
                   3008:         foreach my $part (@parts) {
                   3009:             $filepath.= '/'.$part;
                   3010:             if ((-e $filepath)!=1) {
                   3011:                 mkdir($filepath,0777);
                   3012:             }
                   3013:         }
                   3014:     }
                   3015:     return $filepath;
                   3016: }
                   3017: 
                   3018: sub store_edited_file {
1.638     albertel 3019:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  3020:     my $file = $primary_url;
                   3021:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   3022:     my $fpath = '';
                   3023:     my $fname = $file;
                   3024:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   3025:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   3026:     my $filepath = &build_filepath($fpath);
                   3027:     open(my $fh,'>'.$filepath.'/'.$fname);
                   3028:     print $fh $content;
                   3029:     close($fh);
1.638     albertel 3030:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  3031:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 3032: 			  $home);
1.637     raeburn  3033:     if ($$fetchresult eq 'ok') {
                   3034:         return '/uploaded/'.$fpath.'/'.$fname;
                   3035:     } else {
1.638     albertel 3036:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   3037: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  3038:         return '/adm/notfound.html';
                   3039:     }
                   3040: }
                   3041: 
1.531     albertel 3042: sub clean_filename {
1.831     albertel 3043:     my ($fname,$args)=@_;
1.315     www      3044: # Replace Windows backslashes by forward slashes
1.257     www      3045:     $fname=~s/\\/\//g;
1.831     albertel 3046:     if (!$args->{'keep_path'}) {
                   3047:         # Get rid of everything but the actual filename
                   3048: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   3049:     }
1.315     www      3050: # Replace spaces by underscores
                   3051:     $fname=~s/\s+/\_/g;
                   3052: # Replace all other weird characters by nothing
1.831     albertel 3053:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 3054: # Replace all .\d. sequences with _\d. so they no longer look like version
                   3055: # numbers
                   3056:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 3057:     return $fname;
                   3058: }
1.1051    raeburn  3059: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   3060: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   3061: # image with the same aspect ratio as the original, but with dimensions which do 
                   3062: # not exceed $resizewidth and $resizeheight.
                   3063:  
1.984     neumanie 3064: sub resizeImage {
1.1051    raeburn  3065:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   3066:     my $ima = Image::Magick->new;
                   3067:     my $resized;
                   3068:     if (-e $img_path) {
                   3069:         $ima->Read($img_path);
                   3070:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   3071:             my $width = $ima->Get('width');
                   3072:             my $height = $ima->Get('height');
                   3073:             if ($width > $resizewidth) {
                   3074: 	        my $factor = $width/$resizewidth;
                   3075:                 my $newheight = $height/$factor;
                   3076:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   3077:                 $resized = 1;
                   3078:             }
                   3079:         }
                   3080:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   3081:             my $width = $ima->Get('width');
                   3082:             my $height = $ima->Get('height');
                   3083:             if ($height > $resizeheight) {
                   3084:                 my $factor = $height/$resizeheight;
                   3085:                 my $newwidth = $width/$factor;
                   3086:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   3087:                 $resized = 1;
                   3088:             }
                   3089:         }
                   3090:         if ($resized) {
                   3091:             $ima->Write($img_path);
                   3092:         }
                   3093:     }
                   3094:     return;
1.977     amueller 3095: }
                   3096: 
1.608     albertel 3097: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 3098: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  3099: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  3100: #        $context - possible values: coursedoc, existingfile, overwrite, 
                   3101: #                                    canceloverwrite, or ''. 
                   3102: #                   if 'coursedoc': upload to the current course
                   3103: #                   if 'existingfile': write file to tmp/overwrites directory 
                   3104: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   3105: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 3106: #        $subdir - directory in userfile to store the file into
1.858     raeburn  3107: #        $parser - instruction to parse file for objects ($parser = parse)    
                   3108: #        $allfiles - reference to hash for embedded objects
                   3109: #        $codebase - reference to hash for codebase of java objects
                   3110: #        $desuname - username for permanent storage of uploaded file
                   3111: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  3112: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   3113: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  3114: #        $resizewidth - width (pixels) to which to resize uploaded image
                   3115: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  3116: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  3117: #                    from File::MMagic.
1.858     raeburn  3118: # 
1.686     albertel 3119: # output: url of file in userspace, or error: <message> 
                   3120: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 3121: 
1.531     albertel 3122: sub userfileupload {
1.1090    raeburn  3123:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  3124:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 3125:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 3126:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 3127:     $fname=&clean_filename($fname);
1.1090    raeburn  3128:     # See if there is anything left
1.257     www      3129:     unless ($fname) { return 'error: no uploaded file'; }
1.1090    raeburn  3130:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   3131:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   3132:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   3133:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  3134:         my $now = time;
1.1090    raeburn  3135:         my $filepath;
1.1095    raeburn  3136:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  3137:              $filepath = 'tmp/helprequests/'.$now;
                   3138:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   3139:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   3140:                          '_'.$env{'user.domain'}.'/pending';
                   3141:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   3142:             my ($docuname,$docudom);
                   3143:             if ($destudom) {
                   3144:                 $docudom = $destudom;
                   3145:             } else {
                   3146:                 $docudom = $env{'user.domain'};
                   3147:             }
                   3148:             if ($destuname) {
                   3149:                 $docuname = $destuname;
                   3150:             } else {
                   3151:                 $docuname = $env{'user.name'};
                   3152:             }
                   3153:             if (exists($env{'form.group'})) {
                   3154:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3155:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3156:             }
                   3157:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   3158:             if ($context eq 'canceloverwrite') {
                   3159:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   3160:                 if (-e  $tempfile) {
                   3161:                     my @info = stat($tempfile);
                   3162:                     if ($info[9] eq $env{'form.timestamp'}) {
                   3163:                         unlink($tempfile);
                   3164:                     }
                   3165:                 }
                   3166:                 return;
1.523     raeburn  3167:             }
                   3168:         }
1.1090    raeburn  3169:         # Create the directory if not present
1.741     raeburn  3170:         my @parts=split(/\//,$filepath);
                   3171:         my $fullpath = $perlvar{'lonDaemons'};
                   3172:         for (my $i=0;$i<@parts;$i++) {
                   3173:             $fullpath .= '/'.$parts[$i];
                   3174:             if ((-e $fullpath)!=1) {
                   3175:                 mkdir($fullpath,0777);
                   3176:             }
                   3177:         }
                   3178:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   3179:         print $fh $env{'form.'.$formname};
                   3180:         close($fh);
1.1090    raeburn  3181:         if ($context eq 'existingfile') {
                   3182:             my @info = stat($fullpath.'/'.$fname);
                   3183:             return ($fullpath.'/'.$fname,$info[9]);
                   3184:         } else {
                   3185:             return $fullpath.'/'.$fname;
                   3186:         }
1.523     raeburn  3187:     }
1.995     raeburn  3188:     if ($subdir eq 'scantron') {
                   3189:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  3190:     } else {
1.995     raeburn  3191:         $fname="$subdir/$fname";
                   3192:     }
1.1090    raeburn  3193:     if ($context eq 'coursedoc') {
1.638     albertel 3194: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3195: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  3196:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 3197:             return &finishuserfileupload($docuname,$docudom,
                   3198: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  3199: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  3200:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  3201:         } else {
1.1218    raeburn  3202:             if ($env{'form.folder'}) {
                   3203:                 $fname=$env{'form.folder'}.'/'.$fname;
                   3204:             }
1.638     albertel 3205:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   3206: 				       $fname,$formname,$parser,
1.1095    raeburn  3207: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  3208:         }
1.719     banghart 3209:     } elsif (defined($destuname)) {
                   3210:         my $docuname=$destuname;
                   3211:         my $docudom=$destudom;
1.860     raeburn  3212: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   3213: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  3214:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  3215:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      3216:     } else {
1.638     albertel 3217:         my $docuname=$env{'user.name'};
                   3218:         my $docudom=$env{'user.domain'};
1.1220    raeburn  3219:         if ((exists($env{'form.group'})) || ($context eq 'syllabus')) {
1.714     raeburn  3220:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   3221:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   3222:         }
1.860     raeburn  3223: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   3224: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  3225:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  3226:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      3227:     }
1.271     www      3228: }
                   3229: 
                   3230: sub finishuserfileupload {
1.860     raeburn  3231:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  3232:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  3233:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      3234:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 3235:   
1.860     raeburn  3236:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 3237:     $file=$fname;
                   3238:     if ($fname=~m|/|) {
                   3239:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   3240: 	$path.=$fnamepath.'/';
                   3241:     }
1.259     www      3242:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      3243:     my $count;
                   3244:     for ($count=4;$count<=$#parts;$count++) {
                   3245:         $filepath.="/$parts[$count]";
                   3246:         if ((-e $filepath)!=1) {
                   3247: 	    mkdir($filepath,0777);
                   3248:         }
                   3249:     }
1.984     neumanie 3250: 
1.258     www      3251: # Save the file
                   3252:     {
1.701     albertel 3253: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   3254: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   3255: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   3256: 	    return '/adm/notfound.html';
                   3257: 	}
1.1090    raeburn  3258:         if ($context eq 'overwrite') {
1.1117    foxr     3259:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  3260:             my $target = $filepath.'/'.$file;
                   3261:             if (-e $source) {
                   3262:                 my @info = stat($source);
                   3263:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   3264:                     unless (&File::Copy::move($source,$target)) {
                   3265:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   3266:                         return "Moving from $source failed";
                   3267:                     }
                   3268:                 } else {
                   3269:                     return "Temporary file: $source had unexpected date/time for last modification";
                   3270:                 }
                   3271:             } else {
                   3272:                 return "Temporary file: $source missing";
                   3273:             }
                   3274:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 3275: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   3276: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   3277: 	    return '/adm/notfound.html';
                   3278: 	}
1.570     albertel 3279: 	close(FH);
1.1051    raeburn  3280:         if ($resizewidth && $resizeheight) {
                   3281:             my $mm = new File::MMagic;
                   3282:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   3283:             if ($mime_type =~ m{^image/}) {
                   3284: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   3285:             }  
1.977     amueller 3286: 	}
1.258     www      3287:     }
1.1152    raeburn  3288:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   3289:         if (ref($mimetype)) {
                   3290:             if ($$mimetype eq '') {
                   3291:                 my $mm = new File::MMagic;
                   3292:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   3293:                 $$mimetype = $type;
                   3294:             }
                   3295:         }
                   3296:     }
1.637     raeburn  3297:     if ($parser eq 'parse') {
1.1152    raeburn  3298:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  3299:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   3300:                                                        $allfiles,$codebase);
                   3301:             unless ($parse_result eq 'ok') {
                   3302:                 &logthis('Failed to parse '.$filepath.$file.
                   3303: 	   	         ' for embedded media: '.$parse_result); 
                   3304:             }
1.637     raeburn  3305:         }
                   3306:     }
1.860     raeburn  3307:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   3308:         my $input = $filepath.'/'.$file;
                   3309:         my $output = $filepath.'/'.'tn-'.$file;
                   3310:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   3311:         system("convert -sample $thumbsize $input $output");
                   3312:         if (-e $filepath.'/'.'tn-'.$file) {
                   3313:             $fetchthumb  = 1; 
                   3314:         }
                   3315:     }
1.858     raeburn  3316:  
1.259     www      3317: # Notify homeserver to grep it
                   3318: #
1.984     neumanie 3319:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 3320:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      3321:     if ($fetchresult eq 'ok') {
1.860     raeburn  3322:         if ($fetchthumb) {
                   3323:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   3324:             if ($thumbresult ne 'ok') {
                   3325:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   3326:                          $docuhome.': '.$thumbresult);
                   3327:             }
                   3328:         }
1.259     www      3329: #
1.258     www      3330: # Return the URL to it
1.494     albertel 3331:         return '/uploaded/'.$path.$file;
1.263     www      3332:     } else {
1.494     albertel 3333:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   3334: 		 ': '.$fetchresult);
1.263     www      3335:         return '/adm/notfound.html';
1.858     raeburn  3336:     }
1.493     albertel 3337: }
                   3338: 
1.637     raeburn  3339: sub extract_embedded_items {
1.961     raeburn  3340:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  3341:     my @state = ();
1.1164    raeburn  3342:     my (%lastids,%related,%shockwave,%flashvars);
1.637     raeburn  3343:     my %javafiles = (
                   3344:                       codebase => '',
                   3345:                       code => '',
                   3346:                       archive => ''
                   3347:                     );
                   3348:     my %mediafiles = (
                   3349:                       src => '',
                   3350:                       movie => '',
                   3351:                      );
1.648     raeburn  3352:     my $p;
                   3353:     if ($content) {
                   3354:         $p = HTML::LCParser->new($content);
                   3355:     } else {
1.961     raeburn  3356:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  3357:     }
1.641     albertel 3358:     while (my $t=$p->get_token()) {
1.640     albertel 3359: 	if ($t->[0] eq 'S') {
                   3360: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 3361: 	    push(@state, $tagname);
1.648     raeburn  3362:             if (lc($tagname) eq 'allow') {
                   3363:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   3364:             }
1.640     albertel 3365: 	    if (lc($tagname) eq 'img') {
                   3366: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   3367: 	    }
1.886     albertel 3368: 	    if (lc($tagname) eq 'a') {
1.1222    raeburn  3369:                 unless (($attr->{'href'} =~ /^#/) || ($attr->{'href'} eq '')) {
1.1221    raeburn  3370:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   3371:                 }
1.886     albertel 3372: 	    }
1.645     raeburn  3373:             if (lc($tagname) eq 'script') {
1.1164    raeburn  3374:                 my $src;
1.645     raeburn  3375:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   3376:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   3377:                 } else {
1.1164    raeburn  3378:                     if ($attr->{'src'} ne '') {
                   3379:                         $src = $attr->{'src'};
                   3380:                         &add_filetype($allfiles,$src,'src');
                   3381:                     }
                   3382:                 }
                   3383:                 my $text = $p->get_trimmed_text();
                   3384:                 if ($text =~ /\Qswfobject.registerObject(\E([^\)]+)\)/) {
                   3385:                     my @swfargs = split(/,/,$1);
                   3386:                     foreach my $item (@swfargs) {
                   3387:                         $item =~ s/["']//g;
                   3388:                         $item =~ s/^\s+//;
                   3389:                         $item =~ s/\s+$//;
                   3390:                     }
                   3391:                     if (($swfargs[0] ne'') && ($swfargs[2] ne '')) {
                   3392:                         if (ref($related{$swfargs[0]}) eq 'ARRAY') {
                   3393:                             push(@{$related{$swfargs[0]}},$swfargs[2]);
                   3394:                         } else {
                   3395:                             $related{$swfargs[0]} = [$swfargs[2]];
                   3396:                         }
                   3397:                     }
1.645     raeburn  3398:                 }
                   3399:             }
                   3400:             if (lc($tagname) eq 'link') {
                   3401:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   3402:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   3403:                 }
                   3404:             }
1.640     albertel 3405: 	    if (lc($tagname) eq 'object' ||
                   3406: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   3407: 		foreach my $item (keys(%javafiles)) {
                   3408: 		    $javafiles{$item} = '';
                   3409: 		}
1.1164    raeburn  3410:                 if ((lc($tagname) eq 'object') && (lc($state[-2]) ne 'object')) {
                   3411:                     $lastids{lc($tagname)} = $attr->{'id'};
                   3412:                 }
1.640     albertel 3413: 	    }
                   3414: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   3415: 		my $name = lc($attr->{'name'});
                   3416: 		foreach my $item (keys(%javafiles)) {
                   3417: 		    if ($name eq $item) {
                   3418: 			$javafiles{$item} = $attr->{'value'};
                   3419: 			last;
                   3420: 		    }
                   3421: 		}
1.1164    raeburn  3422:                 my $pathfrom;
1.640     albertel 3423: 		foreach my $item (keys(%mediafiles)) {
                   3424: 		    if ($name eq $item) {
1.1164    raeburn  3425:                         $pathfrom = $attr->{'value'};
                   3426:                         $shockwave{$lastids{lc($state[-2])}} = $pathfrom;
                   3427: 			&add_filetype($allfiles,$pathfrom,$name);
1.640     albertel 3428: 			last;
                   3429: 		    }
                   3430: 		}
1.1164    raeburn  3431:                 if ($name eq 'flashvars') {
                   3432:                     $flashvars{$lastids{lc($state[-2])}} = $attr->{'value'};
                   3433:                 }
                   3434:                 if ($pathfrom ne '') {
                   3435:                     &embedded_dependency($allfiles,\%related,$lastids{lc($state[-2])},
                   3436:                                          $pathfrom);
                   3437:                 }
1.640     albertel 3438: 	    }
                   3439: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   3440: 		foreach my $item (keys(%javafiles)) {
                   3441: 		    if ($attr->{$item}) {
                   3442: 			$javafiles{$item} = $attr->{$item};
                   3443: 			last;
                   3444: 		    }
                   3445: 		}
                   3446: 		foreach my $item (keys(%mediafiles)) {
                   3447: 		    if ($attr->{$item}) {
                   3448: 			&add_filetype($allfiles,$attr->{$item},$item);
                   3449: 			last;
                   3450: 		    }
                   3451: 		}
1.1164    raeburn  3452:                 if (lc($tagname) eq 'embed') {
                   3453:                     if (($attr->{'name'} ne '') && ($attr->{'src'} ne '')) {
                   3454:                         &embedded_dependency($allfiles,\%related,$attr->{'name'},
                   3455:                                              $attr->{'src'});
                   3456:                     }
                   3457:                 }
1.640     albertel 3458: 	    }
1.1164    raeburn  3459:             if ($t->[4] =~ m{/>$}) {
                   3460:                 pop(@state);  
                   3461:             }
1.640     albertel 3462: 	} elsif ($t->[0] eq 'E') {
                   3463: 	    my ($tagname) = ($t->[1]);
                   3464: 	    if ($javafiles{'codebase'} ne '') {
                   3465: 		$javafiles{'codebase'} .= '/';
                   3466: 	    }  
                   3467: 	    if (lc($tagname) eq 'applet' ||
                   3468: 		lc($tagname) eq 'object' ||
                   3469: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   3470: 		) {
                   3471: 		foreach my $item (keys(%javafiles)) {
                   3472: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   3473: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   3474: 			&add_filetype($allfiles,$file,$item);
                   3475: 		    }
                   3476: 		}
                   3477: 	    } 
                   3478: 	    pop @state;
                   3479: 	}
                   3480:     }
1.1164    raeburn  3481:     foreach my $id (sort(keys(%flashvars))) {
                   3482:         if ($shockwave{$id} ne '') {
                   3483:             my @pairs = split(/\&/,$flashvars{$id});
                   3484:             foreach my $pair (@pairs) {
                   3485:                 my ($key,$value) = split(/\=/,$pair);
                   3486:                 if ($key eq 'thumb') {
                   3487:                     &add_filetype($allfiles,$value,$key);
                   3488:                 } elsif ($key eq 'content') {
                   3489:                     my ($path) = ($shockwave{$id} =~ m{^(.+/)[^/]+$});
                   3490:                     my ($ext) = ($value =~ /\.([^.]+)$/);
                   3491:                     if ($ext ne '') {
                   3492:                         &add_filetype($allfiles,$path.$value,$ext);
                   3493:                     }
                   3494:                 }
                   3495:             }
                   3496:         }
                   3497:     }
1.637     raeburn  3498:     return 'ok';
                   3499: }
                   3500: 
1.639     albertel 3501: sub add_filetype {
                   3502:     my ($allfiles,$file,$type)=@_;
                   3503:     if (exists($allfiles->{$file})) {
                   3504: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   3505: 	    push(@{$allfiles->{$file}}, &escape($type));
                   3506: 	}
                   3507:     } else {
                   3508: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  3509:     }
                   3510: }
                   3511: 
1.1164    raeburn  3512: sub embedded_dependency {
                   3513:     my ($allfiles,$related,$identifier,$pathfrom) = @_;
                   3514:     if ((ref($allfiles) eq 'HASH') && (ref($related) eq 'HASH')) {
                   3515:         if (($identifier ne '') &&
                   3516:             (ref($related->{$identifier}) eq 'ARRAY') &&
                   3517:             ($pathfrom ne '')) {
                   3518:             my ($path) = ($pathfrom =~ m{^(.+/)[^/]+$});
                   3519:             foreach my $dep (@{$related->{$identifier}}) {
                   3520:                 &add_filetype($allfiles,$path.$dep,'object');
                   3521:             }
                   3522:         }
                   3523:     }
                   3524:     return;
                   3525: }
                   3526: 
1.493     albertel 3527: sub removeuploadedurl {
1.984     neumanie 3528:     my ($url)=@_;	
                   3529:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 3530:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 3531: }
                   3532: 
                   3533: sub removeuserfile {
                   3534:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 3535:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  3536:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 3537:     if ($result eq 'ok') {	
1.798     raeburn  3538:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   3539:             my $metafile = $fname.'.meta';
                   3540:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 3541: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 3542:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  3543:             my $sqlresult = 
1.823     albertel 3544:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  3545:                                         'portfolio_metadata',$group,
                   3546:                                         'delete');
1.798     raeburn  3547:         }
                   3548:     }
                   3549:     return $result;
1.257     www      3550: }
1.15      www      3551: 
1.530     albertel 3552: sub mkdiruserfile {
                   3553:     my ($docuname,$docudom,$dir)=@_;
                   3554:     my $home=&homeserver($docuname,$docudom);
                   3555:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   3556: }
                   3557: 
1.531     albertel 3558: sub renameuserfile {
                   3559:     my ($docuname,$docudom,$old,$new)=@_;
                   3560:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  3561:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   3562:                         &escape("$old").':'.&escape("$new"),$home);
                   3563:     if ($result eq 'ok') {
                   3564:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   3565:             my $oldmeta = $old.'.meta';
                   3566:             my $newmeta = $new.'.meta';
                   3567:             my $metaresult = 
                   3568:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 3569: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   3570:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  3571:             my $sqlresult = 
1.823     albertel 3572:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  3573:                                         'portfolio_metadata',$group,
                   3574:                                         'delete');
1.798     raeburn  3575:         }
                   3576:     }
                   3577:     return $result;
1.531     albertel 3578: }
                   3579: 
1.14      www      3580: # ------------------------------------------------------------------------- Log
                   3581: 
                   3582: sub log {
                   3583:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      3584:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      3585: }
                   3586: 
                   3587: # ------------------------------------------------------------------ Course Log
1.352     www      3588: #
                   3589: # This routine flushes several buffers of non-mission-critical nature
                   3590: #
1.157     www      3591: 
                   3592: sub flushcourselogs {
1.352     www      3593:     &logthis('Flushing log buffers');
                   3594: #
                   3595: # course logs
                   3596: # This is a log of all transactions in a course, which can be used
                   3597: # for data mining purposes
                   3598: #
                   3599: # It also collects the courseid database, which lists last transaction
                   3600: # times and course titles for all courseids
                   3601: #
                   3602:     my %courseidbuffer=();
1.921     raeburn  3603:     foreach my $crsid (keys(%courselogs)) {
1.352     www      3604:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      3605: 		          &escape($courselogs{$crsid}),
                   3606: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      3607: 	    delete $courselogs{$crsid};
                   3608:         } else {
                   3609:             &logthis('Failed to flush log buffer for '.$crsid);
                   3610:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 3611:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      3612:                         " exceeded maximum size, deleting.</font>");
                   3613:                delete $courselogs{$crsid};
                   3614:             }
1.352     www      3615:         }
1.920     raeburn  3616:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  3617:             'description' => $coursedescrbuf{$crsid},
                   3618:             'inst_code'    => $courseinstcodebuf{$crsid},
                   3619:             'type'        => $coursetypebuf{$crsid},
                   3620:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  3621:         };
1.191     harris41 3622:     }
1.352     www      3623: #
                   3624: # Write course id database (reverse lookup) to homeserver of courses 
                   3625: # Is used in pickcourse
                   3626: #
1.840     albertel 3627:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  3628:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  3629:                                     $courseidbuffer{$crs_home},
                   3630:                                     $crs_home,'timeonly');
1.352     www      3631:     }
                   3632: #
                   3633: # File accesses
                   3634: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   3635: #
1.449     matthew  3636:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  3637:         if ($entry =~ /___count$/) {
                   3638:             my ($dom,$name);
1.807     albertel 3639:             ($dom,$name,undef)=
1.811     albertel 3640: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  3641:             if (! defined($dom) || $dom eq '' || 
                   3642:                 ! defined($name) || $name eq '') {
1.620     albertel 3643:                 my $cid = $env{'request.course.id'};
                   3644:                 $dom  = $env{'request.'.$cid.'.domain'};
                   3645:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  3646:             }
1.450     matthew  3647:             my $value = $accesshash{$entry};
                   3648:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   3649:             my %temphash=($url => $value);
1.449     matthew  3650:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   3651:             if ($result eq 'ok') {
                   3652:                 delete $accesshash{$entry};
                   3653:             }
                   3654:         } else {
1.811     albertel 3655:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.1159    www      3656:             if (($dom eq 'uploaded') || ($dom eq 'adm')) { next; }
1.450     matthew  3657:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  3658:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   3659:                 delete $accesshash{$entry};
                   3660:             }
1.185     www      3661:         }
1.191     harris41 3662:     }
1.352     www      3663: #
                   3664: # Roles
                   3665: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   3666: #
1.800     albertel 3667:     foreach my $entry (keys(%userrolehash)) {
1.351     www      3668:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      3669: 	    split(/\:/,$entry);
                   3670:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      3671:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      3672:                 $rudom,$runame) eq 'ok') {
                   3673: 	    delete $userrolehash{$entry};
                   3674:         }
                   3675:     }
1.662     raeburn  3676: #
                   3677: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   3678: #
                   3679:     my %domrolebuffer = ();
1.1000    raeburn  3680:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 3681:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  3682:         if ($domrolebuffer{$rudom}) {
                   3683:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   3684:                       '='.&escape($domainrolehash{$entry});
                   3685:         } else {
                   3686:             $domrolebuffer{$rudom}.=&escape($entry).
                   3687:                       '='.&escape($domainrolehash{$entry});
                   3688:         }
                   3689:         delete $domainrolehash{$entry};
                   3690:     }
                   3691:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 3692: 	my %servers = &get_servers($dom,'library');
                   3693: 	foreach my $tryserver (keys(%servers)) {
                   3694: 	    unless (&reply('domroleput:'.$dom.':'.
                   3695: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   3696: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   3697: 	    }
1.662     raeburn  3698:         }
                   3699:     }
1.186     www      3700:     $dumpcount++;
1.157     www      3701: }
                   3702: 
                   3703: sub courselog {
                   3704:     my $what=shift;
1.158     www      3705:     $what=time.':'.$what;
1.620     albertel 3706:     unless ($env{'request.course.id'}) { return ''; }
                   3707:     $coursedombuf{$env{'request.course.id'}}=
                   3708:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3709:     $coursenumbuf{$env{'request.course.id'}}=
                   3710:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   3711:     $coursehombuf{$env{'request.course.id'}}=
                   3712:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   3713:     $coursedescrbuf{$env{'request.course.id'}}=
                   3714:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   3715:     $courseinstcodebuf{$env{'request.course.id'}}=
                   3716:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   3717:     $courseownerbuf{$env{'request.course.id'}}=
                   3718:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  3719:     $coursetypebuf{$env{'request.course.id'}}=
                   3720:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 3721:     if (defined $courselogs{$env{'request.course.id'}}) {
                   3722: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      3723:     } else {
1.620     albertel 3724: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      3725:     }
1.620     albertel 3726:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      3727: 	&flushcourselogs();
                   3728:     }
1.158     www      3729: }
                   3730: 
                   3731: sub courseacclog {
                   3732:     my $fnsymb=shift;
1.620     albertel 3733:     unless ($env{'request.course.id'}) { return ''; }
                   3734:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      3735:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      3736:         $what.=':POST';
1.583     matthew  3737:         # FIXME: Probably ought to escape things....
1.800     albertel 3738: 	foreach my $key (keys(%env)) {
                   3739:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  3740:                 my $formitem = $1;
                   3741:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   3742:                     $what.=':'.$formitem.'='.$env{$key};
                   3743:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
                   3744:                     $what.=':'.$formitem.'='.$env{$key};
                   3745:                 }
1.158     www      3746:             }
1.191     harris41 3747:         }
1.583     matthew  3748:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   3749:         # FIXME: We should not be depending on a form parameter that someone
                   3750:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 3751:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  3752:             $what.= ':POST';
                   3753:             # FIXME: Probably ought to escape things....
                   3754:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   3755:                                  'crsdiscuss') {
1.620     albertel 3756:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  3757:             }
                   3758:         }
1.158     www      3759:     }
                   3760:     &courselog($what);
1.149     www      3761: }
                   3762: 
1.185     www      3763: sub countacc {
                   3764:     my $url=&declutter(shift);
1.458     matthew  3765:     return if (! defined($url) || $url eq '');
1.620     albertel 3766:     unless ($env{'request.course.id'}) { return ''; }
1.1158    www      3767: #
                   3768: # Mark that this url was used in this course
                   3769: #
1.620     albertel 3770:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.1158    www      3771: #
                   3772: # Increase the access count for this resource in this child process
                   3773: #
1.281     www      3774:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  3775:     $accesshash{$key}++;
1.185     www      3776: }
1.349     www      3777: 
1.361     www      3778: sub linklog {
                   3779:     my ($from,$to)=@_;
                   3780:     $from=&declutter($from);
                   3781:     $to=&declutter($to);
                   3782:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   3783:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   3784: }
1.1160    www      3785: 
                   3786: sub statslog {
                   3787:     my ($symb,$part,$users,$av_attempts,$degdiff)=@_;
                   3788:     if ($users<2) { return; }
                   3789:     my %dynstore=&LONCAPA::lonmetadata::dynamic_metadata_storage({
                   3790:             'course'       => $env{'request.course.id'},
                   3791:             'sections'     => '"all"',
                   3792:             'num_students' => $users,
                   3793:             'part'         => $part,
                   3794:             'symb'         => $symb,
                   3795:             'mean_tries'   => $av_attempts,
                   3796:             'deg_of_diff'  => $degdiff});
                   3797:     foreach my $key (keys(%dynstore)) {
                   3798:         $accesshash{$key}=$dynstore{$key};
                   3799:     }
                   3800: }
1.361     www      3801:   
1.349     www      3802: sub userrolelog {
                   3803:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.1169    droeschl 3804:     if ( $trole =~ /^(ca|aa|in|cc|ep|cr|ta|co)/ ) {
1.350     www      3805:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3806:        $userrolehash
                   3807:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      3808:                     =$tend.':'.$tstart;
1.662     raeburn  3809:     }
1.1169    droeschl 3810:     if ($env{'request.role'} =~ /dc\./ && $trole =~ /^(au|in|cc|ep|cr|ta|co)/) {
1.898     albertel 3811:        $userrolehash
                   3812:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   3813:                     =$tend.':'.$tstart;
                   3814:     }
1.1169    droeschl 3815:     if ($trole =~ /^(dc|ad|li|au|dg|sc)/ ) {
1.662     raeburn  3816:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3817:        $domainrolehash
                   3818:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   3819:                     = $tend.':'.$tstart;
                   3820:     }
1.351     www      3821: }
                   3822: 
1.957     raeburn  3823: sub courserolelog {
                   3824:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
1.1184    raeburn  3825:     if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   3826:         my $cdom = $1;
                   3827:         my $cnum = $2;
                   3828:         my $sec = $3;
                   3829:         my $namespace = 'rolelog';
                   3830:         my %storehash = (
                   3831:                            role    => $trole,
                   3832:                            start   => $tstart,
                   3833:                            end     => $tend,
                   3834:                            selfenroll => $selfenroll,
                   3835:                            context    => $context,
                   3836:                         );
                   3837:         if ($trole eq 'gr') {
                   3838:             $namespace = 'groupslog';
                   3839:             $storehash{'group'} = $sec;
                   3840:         } else {
                   3841:             $storehash{'section'} = $sec;
                   3842:         }
1.1188    raeburn  3843:         &write_log('course',$namespace,\%storehash,$delflag,$username,
                   3844:                    $domain,$cnum,$cdom);
1.1184    raeburn  3845:         if (($trole ne 'st') || ($sec ne '')) {
                   3846:             &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
1.957     raeburn  3847:         }
                   3848:     }
                   3849:     return;
                   3850: }
                   3851: 
1.1184    raeburn  3852: sub domainrolelog {
                   3853:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   3854:     if ($area =~ m{^/($match_domain)/$}) {
                   3855:         my $cdom = $1;
                   3856:         my $domconfiguser = &Apache::lonnet::get_domainconfiguser($cdom);
                   3857:         my $namespace = 'rolelog';
                   3858:         my %storehash = (
                   3859:                            role    => $trole,
                   3860:                            start   => $tstart,
                   3861:                            end     => $tend,
                   3862:                            context => $context,
                   3863:                         );
1.1188    raeburn  3864:         &write_log('domain',$namespace,\%storehash,$delflag,$username,
                   3865:                    $domain,$domconfiguser,$cdom);
1.1184    raeburn  3866:     }
                   3867:     return;
                   3868: 
                   3869: }
                   3870: 
                   3871: sub coauthorrolelog {
                   3872:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$context)=@_;
                   3873:     if ($area =~ m{^/($match_domain)/($match_username)$}) {
                   3874:         my $audom = $1;
                   3875:         my $auname = $2;
                   3876:         my $namespace = 'rolelog';
                   3877:         my %storehash = (
                   3878:                            role    => $trole,
                   3879:                            start   => $tstart,
                   3880:                            end     => $tend,
                   3881:                            context => $context,
                   3882:                         );
1.1188    raeburn  3883:         &write_log('author',$namespace,\%storehash,$delflag,$username,
                   3884:                    $domain,$auname,$audom);
1.1184    raeburn  3885:     }
                   3886:     return;
                   3887: }
                   3888: 
1.351     www      3889: sub get_course_adv_roles {
1.948     raeburn  3890:     my ($cid,$codes) = @_;
1.620     albertel 3891:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      3892:     my %coursehash=&coursedescription($cid);
1.988     raeburn  3893:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      3894:     my %nothide=();
1.800     albertel 3895:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  3896:         if ($user !~ /:/) {
                   3897: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   3898:         } else {
                   3899:             $nothide{$user}=1;
                   3900:         }
1.470     www      3901:     }
1.1219    raeburn  3902:     my @possdoms = ($coursehash{'domain'});
                   3903:     if ($coursehash{'checkforpriv'}) {
                   3904:         push(@possdoms,split(/,/,$coursehash{'checkforpriv'}));
                   3905:     }
1.351     www      3906:     my %returnhash=();
                   3907:     my %dumphash=
                   3908:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   3909:     my $now=time;
1.997     raeburn  3910:     my %privileged;
1.1000    raeburn  3911:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 3912: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      3913:         if (($tstart) && ($tstart<0)) { next; }
                   3914:         if (($tend) && ($tend<$now)) { next; }
                   3915:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 3916:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 3917: 	if ($username eq '' || $domain eq '') { next; }
1.1219    raeburn  3918:         if ((&privileged($username,$domain,\@possdoms)) &&
1.997     raeburn  3919:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 3920: 	if ($role eq 'cr') { next; }
1.948     raeburn  3921:         if ($codes) {
                   3922:             if ($section) { $role .= ':'.$section; }
                   3923:             if ($returnhash{$role}) {
                   3924:                 $returnhash{$role}.=','.$username.':'.$domain;
                   3925:             } else {
                   3926:                 $returnhash{$role}=$username.':'.$domain;
                   3927:             }
1.351     www      3928:         } else {
1.988     raeburn  3929:             my $key=&plaintext($role,$crstype);
1.973     bisitz   3930:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  3931:             if ($returnhash{$key}) {
                   3932: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   3933:             } else {
                   3934:                 $returnhash{$key}=$username.':'.$domain;
                   3935:             }
1.351     www      3936:         }
1.948     raeburn  3937:     }
1.400     www      3938:     return %returnhash;
                   3939: }
                   3940: 
                   3941: sub get_my_roles {
1.937     raeburn  3942:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 3943:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   3944:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  3945:     my (%dumphash,%nothide);
1.1086    raeburn  3946:     if ($context eq 'userroles') {
1.1166    raeburn  3947:         %dumphash = &dump('roles',$udom,$uname);
1.858     raeburn  3948:     } else {
1.1219    raeburn  3949:         %dumphash = &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  3950:         if ($hidepriv) {
                   3951:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   3952:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3953:                 if ($user !~ /:/) {
                   3954:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   3955:                 } else {
                   3956:                     $nothide{$user} = 1;
                   3957:                 }
                   3958:             }
                   3959:         }
1.858     raeburn  3960:     }
1.400     www      3961:     my %returnhash=();
                   3962:     my $now=time;
1.999     raeburn  3963:     my %privileged;
1.800     albertel 3964:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  3965:         my ($role,$tend,$tstart);
                   3966:         if ($context eq 'userroles') {
1.1149    raeburn  3967:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  3968: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   3969:         } else {
                   3970:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   3971:         }
1.400     www      3972:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  3973:         my $status = 'active';
1.939     raeburn  3974:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  3975:             $status = 'previous';
                   3976:         } 
                   3977:         if (($tstart) && ($now<$tstart)) {
                   3978:             $status = 'future';
                   3979:         }
                   3980:         if (ref($types) eq 'ARRAY') {
                   3981:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   3982:                 next;
                   3983:             } 
                   3984:         } else {
                   3985:             if ($status ne 'active') {
                   3986:                 next;
                   3987:             }
                   3988:         }
1.867     raeburn  3989:         my ($rolecode,$username,$domain,$section,$area);
                   3990:         if ($context eq 'userroles') {
1.1186    raeburn  3991:             ($area,$rolecode) = ($entry =~ /^(.+)_([^_]+)$/);
1.867     raeburn  3992:             (undef,$domain,$username,$section) = split(/\//,$area);
                   3993:         } else {
                   3994:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   3995:         }
1.832     raeburn  3996:         if (ref($roledoms) eq 'ARRAY') {
                   3997:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   3998:                 next;
                   3999:             }
                   4000:         }
                   4001:         if (ref($roles) eq 'ARRAY') {
                   4002:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  4003:                 if ($role =~ /^cr\//) {
                   4004:                     if (!grep(/^cr$/,@{$roles})) {
                   4005:                         next;
                   4006:                     }
1.1104    raeburn  4007:                 } elsif ($role =~ /^gr\//) {
                   4008:                     if (!grep(/^gr$/,@{$roles})) {
                   4009:                         next;
                   4010:                     }
1.922     raeburn  4011:                 } else {
                   4012:                     next;
                   4013:                 }
1.832     raeburn  4014:             }
1.867     raeburn  4015:         }
1.937     raeburn  4016:         if ($hidepriv) {
1.1219    raeburn  4017:             my @privroles = ('dc','su');
1.999     raeburn  4018:             if ($context eq 'userroles') {
1.1219    raeburn  4019:                 next if (grep(/^\Q$role\E$/,@privroles));
1.999     raeburn  4020:             } else {
1.1219    raeburn  4021:                 my $possdoms = [$domain];
                   4022:                 if (ref($roledoms) eq 'ARRAY') {
                   4023:                    push(@{$possdoms},@{$roledoms}); 
1.999     raeburn  4024:                 }
1.1219    raeburn  4025:                 if (&privileged($username,$domain,$possdoms,\@privroles)) {
1.999     raeburn  4026:                     if (!$nothide{$username.':'.$domain}) {
                   4027:                         next;
                   4028:                     }
                   4029:                 }
1.937     raeburn  4030:             }
                   4031:         }
1.933     raeburn  4032:         if ($withsec) {
                   4033:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   4034:                 $tstart.':'.$tend;
                   4035:         } else {
                   4036:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   4037:         }
1.832     raeburn  4038:     }
1.373     www      4039:     return %returnhash;
1.399     www      4040: }
                   4041: 
                   4042: # ----------------------------------------------------- Frontpage Announcements
                   4043: #
                   4044: #
                   4045: 
                   4046: sub postannounce {
                   4047:     my ($server,$text)=@_;
1.844     albertel 4048:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      4049:     unless ($text=~/\w/) { $text=''; }
                   4050:     return &reply('setannounce:'.&escape($text),$server);
                   4051: }
                   4052: 
                   4053: sub getannounce {
1.448     albertel 4054: 
                   4055:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      4056: 	my $announcement='';
1.800     albertel 4057: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 4058: 	close($fh);
1.399     www      4059: 	if ($announcement=~/\w/) { 
                   4060: 	    return 
                   4061:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 4062:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      4063: 	} else {
                   4064: 	    return '';
                   4065: 	}
                   4066:     } else {
                   4067: 	return '';
                   4068:     }
1.351     www      4069: }
1.353     www      4070: 
                   4071: # ---------------------------------------------------------- Course ID routines
                   4072: # Deal with domain's nohist_courseid.db files
                   4073: #
                   4074: 
                   4075: sub courseidput {
1.921     raeburn  4076:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  4077:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  4078:     my $outcome;
                   4079:     if ($caller eq 'timeonly') {
                   4080:         my $cids = '';
                   4081:         foreach my $item (keys(%$storehash)) {
                   4082:             $cids.=&escape($item).'&';
                   4083:         }
                   4084:         $cids=~s/\&$//;
                   4085:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   4086:                           $coursehome);       
                   4087:     } else {
                   4088:         my $items = '';
                   4089:         foreach my $item (keys(%$storehash)) {
                   4090:             $items.= &escape($item).'='.
                   4091:                      &freeze_escape($$storehash{$item}).'&';
                   4092:         }
                   4093:         $items=~s/\&$//;
                   4094:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   4095:                           $coursehome);
1.918     raeburn  4096:     }
                   4097:     if ($outcome eq 'unknown_cmd') {
                   4098:         my $what;
                   4099:         foreach my $cid (keys(%$storehash)) {
                   4100:             $what .= &escape($cid).'=';
1.921     raeburn  4101:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  4102:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  4103:             }
                   4104:             $what =~ s/\:$/&/;
                   4105:         }
                   4106:         $what =~ s/\&$//;  
                   4107:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   4108:     } else {
                   4109:         return $outcome;
                   4110:     }
1.353     www      4111: }
                   4112: 
                   4113: sub courseiddump {
1.921     raeburn  4114:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  4115:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  4116:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1071    raeburn  4117:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918     raeburn  4118:     my $as_hash = 1;
                   4119:     my %returnhash;
                   4120:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 4121:     my %libserv = &all_library();
                   4122:     foreach my $tryserver (keys(%libserv)) {
                   4123:         if ( (  $hostidflag == 1 
                   4124: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   4125: 	     || (!defined($hostidflag)) ) {
                   4126: 
1.918     raeburn  4127: 	    if (($domfilter eq '') ||
                   4128: 		(&host_domain($tryserver) eq $domfilter)) {
1.1180    droeschl 4129:                 my $rep;
                   4130:                 if (grep { $_ eq $tryserver } current_machine_ids()) {
                   4131:                     $rep = LONCAPA::Lond::dump_course_id_handler(
                   4132:                         join(":", (&host_domain($tryserver), $sincefilter, 
                   4133:                                 &escape($descfilter), &escape($instcodefilter), 
                   4134:                                 &escape($ownerfilter), &escape($coursefilter),
                   4135:                                 &escape($typefilter), &escape($regexp_ok), 
                   4136:                                 $as_hash, &escape($selfenrollonly), 
                   4137:                                 &escape($catfilter), $showhidden, $caller, 
                   4138:                                 &escape($cloner), &escape($cc_clone), $cloneonly, 
                   4139:                                 &escape($createdbefore), &escape($createdafter), 
                   4140:                                 &escape($creationcontext), $domcloner)));
                   4141:                 } else {
                   4142:                     $rep = &reply('courseiddump:'.&host_domain($tryserver).':'.
                   4143:                              $sincefilter.':'.&escape($descfilter).':'.
                   4144:                              &escape($instcodefilter).':'.&escape($ownerfilter).
                   4145:                              ':'.&escape($coursefilter).':'.&escape($typefilter).
                   4146:                              ':'.&escape($regexp_ok).':'.$as_hash.':'.
                   4147:                              &escape($selfenrollonly).':'.&escape($catfilter).':'.
                   4148:                              $showhidden.':'.$caller.':'.&escape($cloner).':'.
                   4149:                              &escape($cc_clone).':'.$cloneonly.':'.
                   4150:                              &escape($createdbefore).':'.&escape($createdafter).':'.
                   4151:                              &escape($creationcontext).':'.$domcloner,
                   4152:                              $tryserver);
                   4153:                 }
                   4154:                      
1.918     raeburn  4155:                 my @pairs=split(/\&/,$rep);
                   4156:                 foreach my $item (@pairs) {
                   4157:                     my ($key,$value)=split(/\=/,$item,2);
                   4158:                     $key = &unescape($key);
                   4159:                     next if ($key =~ /^error: 2 /);
                   4160:                     my $result = &thaw_unescape($value);
                   4161:                     if (ref($result) eq 'HASH') {
                   4162:                         $returnhash{$key}=$result;
                   4163:                     } else {
1.921     raeburn  4164:                         my @responses = split(/:/,$value);
                   4165:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  4166:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  4167:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  4168:                         }
1.1008    raeburn  4169:                     }
1.353     www      4170:                 }
                   4171:             }
                   4172:         }
                   4173:     }
                   4174:     return %returnhash;
                   4175: }
                   4176: 
1.1055    raeburn  4177: sub courselastaccess {
                   4178:     my ($cdom,$cnum,$hostidref) = @_;
                   4179:     my %returnhash;
                   4180:     if ($cdom && $cnum) {
                   4181:         my $chome = &homeserver($cnum,$cdom);
                   4182:         if ($chome ne 'no_host') {
                   4183:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   4184:             &extract_lastaccess(\%returnhash,$rep);
                   4185:         }
                   4186:     } else {
                   4187:         if (!$cdom) { $cdom=''; }
                   4188:         my %libserv = &all_library();
                   4189:         foreach my $tryserver (keys(%libserv)) {
                   4190:             if (ref($hostidref) eq 'ARRAY') {
                   4191:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   4192:             } 
                   4193:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   4194:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   4195:                 &extract_lastaccess(\%returnhash,$rep);
                   4196:             }
                   4197:         }
                   4198:     }
                   4199:     return %returnhash;
                   4200: }
                   4201: 
                   4202: sub extract_lastaccess {
                   4203:     my ($returnhash,$rep) = @_;
                   4204:     if (ref($returnhash) eq 'HASH') {
                   4205:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   4206:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   4207:                  $rep eq '') {
                   4208:             my @pairs=split(/\&/,$rep);
                   4209:             foreach my $item (@pairs) {
                   4210:                 my ($key,$value)=split(/\=/,$item,2);
                   4211:                 $key = &unescape($key);
                   4212:                 next if ($key =~ /^error: 2 /);
                   4213:                 $returnhash->{$key} = &thaw_unescape($value);
                   4214:             }
                   4215:         }
                   4216:     }
                   4217:     return;
                   4218: }
                   4219: 
1.658     raeburn  4220: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  4221: 
                   4222: sub dcmailput {
1.685     raeburn  4223:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  4224:     my $status = &Apache::lonnet::critical(
1.740     www      4225:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   4226:        &escape($message),$server);
1.662     raeburn  4227:     return $status;
                   4228: }
                   4229: 
1.658     raeburn  4230: sub dcmaildump {
                   4231:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  4232:     my %returnhash=();
1.846     albertel 4233: 
                   4234:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  4235:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   4236:                                                          &escape($enddate).':';
                   4237: 	my @esc_senders=map { &escape($_)} @$senders;
                   4238: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 4239: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 4240:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  4241:             if (($key) && ($value)) {
                   4242:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  4243:             }
                   4244:         }
                   4245:     }
                   4246:     return %returnhash;
                   4247: }
1.662     raeburn  4248: # ---------------------------------------------------------- Domain roles
                   4249: 
                   4250: sub get_domain_roles {
                   4251:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  4252:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  4253:         $startdate = '.';
                   4254:     }
1.1018    raeburn  4255:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  4256:         $enddate = '.';
                   4257:     }
1.922     raeburn  4258:     my $rolelist;
                   4259:     if (ref($roles) eq 'ARRAY') {
1.1219    raeburn  4260:         $rolelist = join('&',@{$roles});
1.922     raeburn  4261:     }
1.662     raeburn  4262:     my %personnel = ();
1.841     albertel 4263: 
                   4264:     my %servers = &get_servers($dom,'library');
                   4265:     foreach my $tryserver (keys(%servers)) {
                   4266: 	%{$personnel{$tryserver}}=();
                   4267: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   4268: 					    &escape($startdate).':'.
                   4269: 					    &escape($enddate).':'.
                   4270: 					    &escape($rolelist), $tryserver))) {
                   4271: 	    my ($key,$value) = split(/\=/,$line,2);
                   4272: 	    if (($key) && ($value)) {
                   4273: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   4274: 	    }
                   4275: 	}
1.662     raeburn  4276:     }
                   4277:     return %personnel;
                   4278: }
1.658     raeburn  4279: 
1.1057    www      4280: # ----------------------------------------------------------- Interval timing 
1.149     www      4281: 
1.1153    www      4282: {
                   4283: # Caches needed for speedup of navmaps
                   4284: # We don't want to cache this for very long at all (5 seconds at most)
                   4285: # 
                   4286: # The user for whom we cache
                   4287: my $cachedkey='';
                   4288: # The cached times for this user
                   4289: my %cachedtimes=();
                   4290: # When this was last done
                   4291: my $cachedtime=();
                   4292: 
                   4293: sub load_all_first_access {
1.1154    raeburn  4294:     my ($uname,$udom)=@_;
1.1156    www      4295:     if (($cachedkey eq $uname.':'.$udom) &&
1.1174    raeburn  4296:         (abs($cachedtime-time)<5) && (!$env{'form.markaccess'})) {
1.1154    raeburn  4297:         return;
                   4298:     }
                   4299:     $cachedtime=time;
                   4300:     $cachedkey=$uname.':'.$udom;
                   4301:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      4302: }
                   4303: 
1.504     albertel 4304: sub get_first_access {
1.1162    raeburn  4305:     my ($type,$argsymb,$argmap)=@_;
1.790     albertel 4306:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 4307:     if ($argsymb) { $symb=$argsymb; }
                   4308:     my ($map,$id,$res)=&decode_symb($symb);
1.1162    raeburn  4309:     if ($argmap) { $map = $argmap; }
1.926     albertel 4310:     if ($type eq 'course') {
                   4311: 	$res='course';
                   4312:     } elsif ($type eq 'map') {
1.588     albertel 4313: 	$res=&symbread($map);
                   4314:     } else {
                   4315: 	$res=$symb;
                   4316:     }
1.1153    www      4317:     &load_all_first_access($uname,$udom);
                   4318:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 4319: }
                   4320: 
                   4321: sub set_first_access {
1.1162    raeburn  4322:     my ($type,$interval)=@_;
1.790     albertel 4323:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 4324:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 4325:     if ($type eq 'course') {
                   4326: 	$res='course';
                   4327:     } elsif ($type eq 'map') {
1.588     albertel 4328: 	$res=&symbread($map);
                   4329:     } else {
                   4330: 	$res=$symb;
                   4331:     }
1.1153    www      4332:     $cachedkey='';
1.1162    raeburn  4333:     my $firstaccess=&get_first_access($type,$symb,$map);
1.505     albertel 4334:     if (!$firstaccess) {
1.1162    raeburn  4335:         my $start = time;
                   4336: 	my $putres = &put('firstaccesstimes',{"$courseid\0$res"=>$start},
                   4337:                           $udom,$uname);
                   4338:         if ($putres eq 'ok') {
                   4339:             &put('timerinterval',{"$courseid\0$res"=>$interval},
                   4340:                  $udom,$uname); 
                   4341:             &appenv(
                   4342:                      {
                   4343:                         'course.'.$courseid.'.firstaccess.'.$res   => $start,
                   4344:                         'course.'.$courseid.'.timerinterval.'.$res => $interval,
                   4345:                      }
                   4346:                   );
                   4347:         }
                   4348:         return $putres;
1.505     albertel 4349:     }
                   4350:     return 'already_set';
1.504     albertel 4351: }
1.1153    www      4352: }
1.110     www      4353: # --------------------------------------------- Set Expire Date for Spreadsheet
                   4354: 
                   4355: sub expirespread {
                   4356:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 4357:     my $cid=$env{'request.course.id'}; 
1.110     www      4358:     if ($cid) {
                   4359:        my $now=time;
                   4360:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 4361:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   4362:                             $env{'course.'.$cid.'.num'}.
1.110     www      4363: 	        	    ':nohist_expirationdates:'.
                   4364:                             &escape($key).'='.$now,
1.620     albertel 4365:                             $env{'course.'.$cid.'.home'})
1.110     www      4366:     }
                   4367:     return 'ok';
1.14      www      4368: }
                   4369: 
1.109     www      4370: # ----------------------------------------------------- Devalidate Spreadsheets
                   4371: 
                   4372: sub devalidate {
1.325     www      4373:     my ($symb,$uname,$udom)=@_;
1.620     albertel 4374:     my $cid=$env{'request.course.id'}; 
1.109     www      4375:     if ($cid) {
1.391     matthew  4376:         # delete the stored spreadsheets for
                   4377:         # - the student level sheet of this user in course's homespace
                   4378:         # - the assessment level sheet for this resource 
                   4379:         #   for this user in user's homespace
1.553     albertel 4380: 	# - current conditional state info
1.325     www      4381: 	my $key=$uname.':'.$udom.':';
1.109     www      4382:         my $status=
1.299     matthew  4383: 	    &del('nohist_calculatedsheets',
1.391     matthew  4384: 		 [$key.'studentcalc:'],
1.620     albertel 4385: 		 $env{'course.'.$cid.'.domain'},
                   4386: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 4387: 		.' '.
                   4388: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  4389: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      4390:         unless ($status eq 'ok ok') {
                   4391:            &logthis('Could not devalidate spreadsheet '.
1.325     www      4392:                     $uname.' at '.$udom.' for '.
1.109     www      4393: 		    $symb.': '.$status);
1.133     albertel 4394:         }
1.553     albertel 4395: 	&delenv('user.state.'.$cid);
1.109     www      4396:     }
                   4397: }
                   4398: 
1.265     albertel 4399: sub get_scalar {
                   4400:     my ($string,$end) = @_;
                   4401:     my $value;
                   4402:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   4403: 	$value = $1;
                   4404:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   4405: 	$value = $1;
                   4406:     }
                   4407:     return &unescape($value);
                   4408: }
                   4409: 
                   4410: sub array2str {
                   4411:   my (@array) = @_;
                   4412:   my $result=&arrayref2str(\@array);
                   4413:   $result=~s/^__ARRAY_REF__//;
                   4414:   $result=~s/__END_ARRAY_REF__$//;
                   4415:   return $result;
                   4416: }
                   4417: 
1.204     albertel 4418: sub arrayref2str {
                   4419:   my ($arrayref) = @_;
1.265     albertel 4420:   my $result='__ARRAY_REF__';
1.204     albertel 4421:   foreach my $elem (@$arrayref) {
1.265     albertel 4422:     if(ref($elem) eq 'ARRAY') {
                   4423:       $result.=&arrayref2str($elem).'&';
                   4424:     } elsif(ref($elem) eq 'HASH') {
                   4425:       $result.=&hashref2str($elem).'&';
                   4426:     } elsif(ref($elem)) {
                   4427:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 4428:     } else {
                   4429:       $result.=&escape($elem).'&';
                   4430:     }
                   4431:   }
                   4432:   $result=~s/\&$//;
1.265     albertel 4433:   $result .= '__END_ARRAY_REF__';
1.204     albertel 4434:   return $result;
                   4435: }
                   4436: 
1.168     albertel 4437: sub hash2str {
1.204     albertel 4438:   my (%hash) = @_;
                   4439:   my $result=&hashref2str(\%hash);
1.265     albertel 4440:   $result=~s/^__HASH_REF__//;
                   4441:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 4442:   return $result;
                   4443: }
                   4444: 
                   4445: sub hashref2str {
                   4446:   my ($hashref)=@_;
1.265     albertel 4447:   my $result='__HASH_REF__';
1.800     albertel 4448:   foreach my $key (sort(keys(%$hashref))) {
                   4449:     if (ref($key) eq 'ARRAY') {
                   4450:       $result.=&arrayref2str($key).'=';
                   4451:     } elsif (ref($key) eq 'HASH') {
                   4452:       $result.=&hashref2str($key).'=';
                   4453:     } elsif (ref($key)) {
1.265     albertel 4454:       $result.='=';
1.800     albertel 4455:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 4456:     } else {
1.1132    raeburn  4457: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 4458:     }
                   4459: 
1.800     albertel 4460:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   4461:       $result.=&arrayref2str($hashref->{$key}).'&';
                   4462:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   4463:       $result.=&hashref2str($hashref->{$key}).'&';
                   4464:     } elsif(ref($hashref->{$key})) {
1.265     albertel 4465:        $result.='&';
1.800     albertel 4466:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 4467:     } else {
1.800     albertel 4468:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 4469:     }
                   4470:   }
1.168     albertel 4471:   $result=~s/\&$//;
1.265     albertel 4472:   $result .= '__END_HASH_REF__';
1.168     albertel 4473:   return $result;
                   4474: }
                   4475: 
                   4476: sub str2hash {
1.265     albertel 4477:     my ($string)=@_;
                   4478:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   4479:     return %$hash;
                   4480: }
                   4481: 
                   4482: sub str2hashref {
1.168     albertel 4483:   my ($string) = @_;
1.265     albertel 4484: 
                   4485:   my %hash;
                   4486: 
                   4487:   if($string !~ /^__HASH_REF__/) {
                   4488:       if (! ($string eq '' || !defined($string))) {
                   4489: 	  $hash{'error'}='Not hash reference';
                   4490:       }
                   4491:       return (\%hash, $string);
                   4492:   }
                   4493: 
                   4494:   $string =~ s/^__HASH_REF__//;
                   4495: 
                   4496:   while($string !~ /^__END_HASH_REF__/) {
                   4497:       #key
                   4498:       my $key='';
                   4499:       if($string =~ /^__HASH_REF__/) {
                   4500:           ($key, $string)=&str2hashref($string);
                   4501:           if(defined($key->{'error'})) {
                   4502:               $hash{'error'}='Bad data';
                   4503:               return (\%hash, $string);
                   4504:           }
                   4505:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4506:           ($key, $string)=&str2arrayref($string);
                   4507:           if($key->[0] eq 'Array reference error') {
                   4508:               $hash{'error'}='Bad data';
                   4509:               return (\%hash, $string);
                   4510:           }
                   4511:       } else {
                   4512:           $string =~ s/^(.*?)=//;
1.267     albertel 4513: 	  $key=&unescape($1);
1.265     albertel 4514:       }
                   4515:       $string =~ s/^=//;
                   4516: 
                   4517:       #value
                   4518:       my $value='';
                   4519:       if($string =~ /^__HASH_REF__/) {
                   4520:           ($value, $string)=&str2hashref($string);
                   4521:           if(defined($value->{'error'})) {
                   4522:               $hash{'error'}='Bad data';
                   4523:               return (\%hash, $string);
                   4524:           }
                   4525:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4526:           ($value, $string)=&str2arrayref($string);
                   4527:           if($value->[0] eq 'Array reference error') {
                   4528:               $hash{'error'}='Bad data';
                   4529:               return (\%hash, $string);
                   4530:           }
                   4531:       } else {
                   4532: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   4533:       }
                   4534:       $string =~ s/^&//;
                   4535: 
                   4536:       $hash{$key}=$value;
1.204     albertel 4537:   }
1.265     albertel 4538: 
                   4539:   $string =~ s/^__END_HASH_REF__//;
                   4540: 
                   4541:   return (\%hash, $string);
1.204     albertel 4542: }
                   4543: 
                   4544: sub str2array {
1.265     albertel 4545:     my ($string)=@_;
                   4546:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   4547:     return @$array;
                   4548: }
                   4549: 
                   4550: sub str2arrayref {
1.204     albertel 4551:   my ($string) = @_;
1.265     albertel 4552:   my @array;
                   4553: 
                   4554:   if($string !~ /^__ARRAY_REF__/) {
                   4555:       if (! ($string eq '' || !defined($string))) {
                   4556: 	  $array[0]='Array reference error';
                   4557:       }
                   4558:       return (\@array, $string);
                   4559:   }
                   4560: 
                   4561:   $string =~ s/^__ARRAY_REF__//;
                   4562: 
                   4563:   while($string !~ /^__END_ARRAY_REF__/) {
                   4564:       my $value='';
                   4565:       if($string =~ /^__HASH_REF__/) {
                   4566:           ($value, $string)=&str2hashref($string);
                   4567:           if(defined($value->{'error'})) {
                   4568:               $array[0] ='Array reference error';
                   4569:               return (\@array, $string);
                   4570:           }
                   4571:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4572:           ($value, $string)=&str2arrayref($string);
                   4573:           if($value->[0] eq 'Array reference error') {
                   4574:               $array[0] ='Array reference error';
                   4575:               return (\@array, $string);
                   4576:           }
                   4577:       } else {
                   4578: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   4579:       }
                   4580:       $string =~ s/^&//;
                   4581: 
                   4582:       push(@array, $value);
1.191     harris41 4583:   }
1.265     albertel 4584: 
                   4585:   $string =~ s/^__END_ARRAY_REF__//;
                   4586: 
                   4587:   return (\@array, $string);
1.168     albertel 4588: }
                   4589: 
1.167     albertel 4590: # -------------------------------------------------------------------Temp Store
                   4591: 
1.168     albertel 4592: sub tmpreset {
                   4593:   my ($symb,$namespace,$domain,$stuname) = @_;
                   4594:   if (!$symb) {
                   4595:     $symb=&symbread();
1.620     albertel 4596:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4597:   }
                   4598:   $symb=escape($symb);
                   4599: 
1.620     albertel 4600:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 4601:   $namespace=~s/\//\_/g;
                   4602:   $namespace=~s/\W//g;
                   4603: 
1.620     albertel 4604:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4605:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4606:   if ($domain eq 'public' && $stuname eq 'public') {
                   4607:       $stuname=$ENV{'REMOTE_ADDR'};
                   4608:   }
1.1117    foxr     4609:   my $path=LONCAPA::tempdir();
1.168     albertel 4610:   my %hash;
                   4611:   if (tie(%hash,'GDBM_File',
                   4612: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4613: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  4614:     foreach my $key (keys(%hash)) {
1.180     albertel 4615:       if ($key=~ /:$symb/) {
1.168     albertel 4616: 	delete($hash{$key});
                   4617:       }
                   4618:     }
                   4619:   }
                   4620: }
                   4621: 
1.167     albertel 4622: sub tmpstore {
1.168     albertel 4623:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4624: 
                   4625:   if (!$symb) {
                   4626:     $symb=&symbread();
1.620     albertel 4627:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4628:   }
                   4629:   $symb=escape($symb);
                   4630: 
                   4631:   if (!$namespace) {
                   4632:     # I don't think we would ever want to store this for a course.
                   4633:     # it seems this will only be used if we don't have a course.
1.620     albertel 4634:     #$namespace=$env{'request.course.id'};
1.168     albertel 4635:     #if (!$namespace) {
1.620     albertel 4636:       $namespace=$env{'request.state'};
1.168     albertel 4637:     #}
                   4638:   }
                   4639:   $namespace=~s/\//\_/g;
                   4640:   $namespace=~s/\W//g;
1.620     albertel 4641:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4642:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4643:   if ($domain eq 'public' && $stuname eq 'public') {
                   4644:       $stuname=$ENV{'REMOTE_ADDR'};
                   4645:   }
1.168     albertel 4646:   my $now=time;
                   4647:   my %hash;
1.1117    foxr     4648:   my $path=LONCAPA::tempdir();
1.168     albertel 4649:   if (tie(%hash,'GDBM_File',
                   4650: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4651: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 4652:     $hash{"version:$symb"}++;
                   4653:     my $version=$hash{"version:$symb"};
                   4654:     my $allkeys=''; 
                   4655:     foreach my $key (keys(%$storehash)) {
                   4656:       $allkeys.=$key.':';
1.591     albertel 4657:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 4658:     }
                   4659:     $hash{"$version:$symb:timestamp"}=$now;
                   4660:     $allkeys.='timestamp';
                   4661:     $hash{"$version:keys:$symb"}=$allkeys;
                   4662:     if (untie(%hash)) {
                   4663:       return 'ok';
                   4664:     } else {
                   4665:       return "error:$!";
                   4666:     }
                   4667:   } else {
                   4668:     return "error:$!";
                   4669:   }
                   4670: }
1.167     albertel 4671: 
1.168     albertel 4672: # -----------------------------------------------------------------Temp Restore
1.167     albertel 4673: 
1.168     albertel 4674: sub tmprestore {
                   4675:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 4676: 
1.168     albertel 4677:   if (!$symb) {
                   4678:     $symb=&symbread();
1.620     albertel 4679:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4680:   }
                   4681:   $symb=escape($symb);
                   4682: 
1.620     albertel 4683:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 4684: 
1.620     albertel 4685:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4686:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4687:   if ($domain eq 'public' && $stuname eq 'public') {
                   4688:       $stuname=$ENV{'REMOTE_ADDR'};
                   4689:   }
1.168     albertel 4690:   my %returnhash;
                   4691:   $namespace=~s/\//\_/g;
                   4692:   $namespace=~s/\W//g;
                   4693:   my %hash;
1.1117    foxr     4694:   my $path=LONCAPA::tempdir();
1.168     albertel 4695:   if (tie(%hash,'GDBM_File',
                   4696: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4697: 	  &GDBM_READER(),0640)) {
1.168     albertel 4698:     my $version=$hash{"version:$symb"};
                   4699:     $returnhash{'version'}=$version;
                   4700:     my $scope;
                   4701:     for ($scope=1;$scope<=$version;$scope++) {
                   4702:       my $vkeys=$hash{"$scope:keys:$symb"};
                   4703:       my @keys=split(/:/,$vkeys);
                   4704:       my $key;
                   4705:       $returnhash{"$scope:keys"}=$vkeys;
                   4706:       foreach $key (@keys) {
1.591     albertel 4707: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   4708: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 4709:       }
                   4710:     }
1.168     albertel 4711:     if (!(untie(%hash))) {
                   4712:       return "error:$!";
                   4713:     }
                   4714:   } else {
                   4715:     return "error:$!";
                   4716:   }
                   4717:   return %returnhash;
1.167     albertel 4718: }
                   4719: 
1.9       www      4720: # ----------------------------------------------------------------------- Store
                   4721: 
                   4722: sub store {
1.124     www      4723:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4724:     my $home='';
                   4725: 
1.168     albertel 4726:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4727: 
1.213     www      4728:     $symb=&symbclean($symb);
1.122     albertel 4729:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4730: 
1.620     albertel 4731:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4732:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4733: 
                   4734:     &devalidate($symb,$stuname,$domain);
1.109     www      4735: 
                   4736:     $symb=escape($symb);
1.187     www      4737:     if (!$namespace) { 
1.620     albertel 4738:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4739:           return ''; 
                   4740:        } 
                   4741:     }
1.620     albertel 4742:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4743: 
                   4744:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4745:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   4746: 
1.12      www      4747:     my $namevalue='';
1.800     albertel 4748:     foreach my $key (keys(%$storehash)) {
                   4749:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4750:     }
1.12      www      4751:     $namevalue=~s/\&$//;
1.187     www      4752:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      4753:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      4754: }
                   4755: 
1.47      www      4756: # -------------------------------------------------------------- Critical Store
                   4757: 
                   4758: sub cstore {
1.124     www      4759:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4760:     my $home='';
                   4761: 
1.168     albertel 4762:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4763: 
1.213     www      4764:     $symb=&symbclean($symb);
1.122     albertel 4765:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4766: 
1.620     albertel 4767:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4768:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4769: 
                   4770:     &devalidate($symb,$stuname,$domain);
1.109     www      4771: 
                   4772:     $symb=escape($symb);
1.187     www      4773:     if (!$namespace) { 
1.620     albertel 4774:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4775:           return ''; 
                   4776:        } 
                   4777:     }
1.620     albertel 4778:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4779: 
                   4780:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4781:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 4782: 
1.47      www      4783:     my $namevalue='';
1.800     albertel 4784:     foreach my $key (keys(%$storehash)) {
                   4785:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4786:     }
1.47      www      4787:     $namevalue=~s/\&$//;
1.187     www      4788:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      4789:     return critical
                   4790:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      4791: }
                   4792: 
1.9       www      4793: # --------------------------------------------------------------------- Restore
                   4794: 
                   4795: sub restore {
1.124     www      4796:     my ($symb,$namespace,$domain,$stuname) = @_;
                   4797:     my $home='';
                   4798: 
1.168     albertel 4799:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4800: 
1.122     albertel 4801:     if (!$symb) {
1.1224    raeburn  4802:         return if ($namespace eq 'courserequests');
                   4803:         unless ($symb=escape(&symbread())) { return ''; }
1.122     albertel 4804:     } else {
1.1224    raeburn  4805:         unless ($namespace eq 'courserequests') {
                   4806:             $symb=&escape(&symbclean($symb));
                   4807:         }
1.122     albertel 4808:     }
1.188     www      4809:     if (!$namespace) { 
1.620     albertel 4810:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      4811:           return ''; 
                   4812:        } 
                   4813:     }
1.620     albertel 4814:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4815:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   4816:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 4817:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   4818: 
1.12      www      4819:     my %returnhash=();
1.800     albertel 4820:     foreach my $line (split(/\&/,$answer)) {
                   4821: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 4822:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 4823:     }
1.75      www      4824:     my $version;
                   4825:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 4826:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   4827:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 4828:        }
1.75      www      4829:     }
1.13      www      4830:     return %returnhash;
1.34      www      4831: }
                   4832: 
                   4833: # ---------------------------------------------------------- Course Description
1.1118    foxr     4834: #
                   4835: #  
1.34      www      4836: 
                   4837: sub coursedescription {
1.731     albertel 4838:     my ($courseid,$args)=@_;
1.34      www      4839:     $courseid=~s/^\///;
1.49      www      4840:     $courseid=~s/\_/\//g;
1.34      www      4841:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 4842:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 4843:     my $normalid=$cdomain.'_'.$cnum;
                   4844:     # need to always cache even if we get errors otherwise we keep 
                   4845:     # trying and trying and trying to get the course description.
                   4846:     my %envhash=();
                   4847:     my %returnhash=();
1.731     albertel 4848:     
                   4849:     my $expiretime=600;
                   4850:     if ($env{'request.course.id'} eq $normalid) {
                   4851: 	$expiretime=120;
                   4852:     }
                   4853: 
                   4854:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   4855:     if (!$args->{'freshen_cache'}
                   4856: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   4857: 	foreach my $key (keys(%env)) {
                   4858: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   4859: 	    my ($setting) = $1;
                   4860: 	    $returnhash{$setting} = $env{$key};
                   4861: 	}
                   4862: 	return %returnhash;
                   4863:     }
                   4864: 
1.1118    foxr     4865:     # get the data again
                   4866: 
1.731     albertel 4867:     if (!$args->{'one_time'}) {
                   4868: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   4869:     }
1.811     albertel 4870: 
1.34      www      4871:     if ($chome ne 'no_host') {
1.302     albertel 4872:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 4873:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     4874: 	   my $username = $env{'user.name'}; # Defult username
                   4875: 	   if(defined $args->{'user'}) {
                   4876: 	       $username = $args->{'user'};
                   4877: 	   }
1.129     albertel 4878:            $returnhash{'home'}= $chome;
                   4879: 	   $returnhash{'domain'} = $cdomain;
                   4880: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  4881:            if (!defined($returnhash{'type'})) {
                   4882:                $returnhash{'type'} = 'Course';
                   4883:            }
1.130     albertel 4884:            while (my ($name,$value) = each %returnhash) {
1.53      www      4885:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 4886:            }
1.270     www      4887:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     4888:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     4889: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      4890:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   4891:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   4892:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      4893:        }
                   4894:     }
1.731     albertel 4895:     if (!$args->{'one_time'}) {
1.949     raeburn  4896: 	&appenv(\%envhash);
1.731     albertel 4897:     }
1.302     albertel 4898:     return %returnhash;
1.461     www      4899: }
                   4900: 
1.1080    raeburn  4901: sub update_released_required {
                   4902:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   4903:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   4904:         $cid = $env{'request.course.id'};
                   4905:         $cdom = $env{'course.'.$cid.'.domain'};
                   4906:         $cnum = $env{'course.'.$cid.'.num'};
                   4907:         $chome = $env{'course.'.$cid.'.home'};
                   4908:     }
                   4909:     if ($needsrelease) {
                   4910:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   4911:         my $needsupdate;
                   4912:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   4913:             $needsupdate = 1;
                   4914:         } else {
                   4915:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   4916:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   4917:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   4918:                 $needsupdate = 1;
                   4919:             }
                   4920:         }
                   4921:         if ($needsupdate) {
                   4922:             my %needshash = (
                   4923:                              'internal.releaserequired' => $needsrelease,
                   4924:                             );
                   4925:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   4926:             if ($putresult eq 'ok') {
                   4927:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   4928:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   4929:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   4930:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   4931:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   4932:                 }
                   4933:             }
                   4934:         }
                   4935:     }
                   4936:     return;
                   4937: }
                   4938: 
1.461     www      4939: # -------------------------------------------------See if a user is privileged
                   4940: 
                   4941: sub privileged {
1.1219    raeburn  4942:     my ($username,$domain,$possdomains,$possroles)=@_;
1.1170    droeschl 4943:     my $now = time;
1.1219    raeburn  4944:     my $roles;
                   4945:     if (ref($possroles) eq 'ARRAY') {
                   4946:         $roles = $possroles; 
                   4947:     } else {
                   4948:         $roles = ['dc','su'];
                   4949:     }
                   4950:     if (ref($possdomains) eq 'ARRAY') {
                   4951:         my %privileged = &privileged_by_domain($possdomains,$roles);
                   4952:         foreach my $dom (@{$possdomains}) {
                   4953:             if (($username =~ /^$match_username$/) && ($domain =~ /^$match_domain$/) &&
                   4954:                 (ref($privileged{$dom}) eq 'HASH')) {
                   4955:                 foreach my $role (@{$roles}) {
                   4956:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   4957:                         if (exists($privileged{$dom}{$role}{$username.':'.$domain})) {
                   4958:                             my ($end,$start) = split(/:/,$privileged{$dom}{$role}{$username.':'.$domain});
                   4959:                             return 1 unless (($end && $end < $now) ||
                   4960:                                              ($start && $start > $now));
                   4961:                         }
                   4962:                     }
                   4963:                 }
                   4964:             }
                   4965:         }
                   4966:     } else {
                   4967:         my %rolesdump = &dump("roles", $domain, $username) or return 0;
                   4968:         my $now = time;
1.1170    droeschl 4969: 
1.1219    raeburn  4970:         for my $role (@rolesdump{grep { ! /^rolesdef_/ } keys %rolesdump}) {
1.1170    droeschl 4971:             my ($trole, $tend, $tstart) = split(/_/, $role);
1.1219    raeburn  4972:             if (grep(/^\Q$trole\E$/,@{$roles})) {
1.1170    droeschl 4973:                 return 1 unless ($tend && $tend < $now) 
1.1219    raeburn  4974:                         or ($tstart && $tstart > $now);
1.1170    droeschl 4975:             }
1.1219    raeburn  4976:         }
                   4977:     }
                   4978:     return 0;
                   4979: }
1.1170    droeschl 4980: 
1.1219    raeburn  4981: sub privileged_by_domain {
                   4982:     my ($domains,$roles) = @_;
                   4983:     my %privileged = ();
                   4984:     my $cachetime = 60*60*24;
                   4985:     my $now = time;
                   4986:     unless ((ref($domains) eq 'ARRAY') && (ref($roles) eq 'ARRAY')) {
                   4987:         return %privileged;
                   4988:     }
                   4989:     foreach my $dom (@{$domains}) {
                   4990:         next if (ref($privileged{$dom}) eq 'HASH');
                   4991:         my $needroles;
                   4992:         foreach my $role (@{$roles}) {
                   4993:             my ($result,$cached)=&is_cached_new('priv_'.$role,$dom);
                   4994:             if (defined($cached)) {
                   4995:                 if (ref($result) eq 'HASH') {
                   4996:                     $privileged{$dom}{$role} = $result;
                   4997:                 }
                   4998:             } else {
                   4999:                 $needroles = 1;
                   5000:             }
                   5001:         }
                   5002:         if ($needroles) {
                   5003:             my %dompersonnel = &get_domain_roles($dom,$roles);
                   5004:             $privileged{$dom} = {};
                   5005:             foreach my $server (keys(%dompersonnel)) {
                   5006:                 if (ref($dompersonnel{$server}) eq 'HASH') {
                   5007:                     foreach my $item (keys(%{$dompersonnel{$server}})) {
                   5008:                         my ($trole,$uname,$udom,$rest) = split(/:/,$item,4);
                   5009:                         my ($end,$start) = split(/:/,$dompersonnel{$server}{$item});
                   5010:                         next if ($end && $end < $now);
                   5011:                         $privileged{$dom}{$trole}{$uname.':'.$udom} = 
                   5012:                             $dompersonnel{$server}{$item};
                   5013:                     }
                   5014:                 }
                   5015:             }
                   5016:             if (ref($privileged{$dom}) eq 'HASH') {
                   5017:                 foreach my $role (@{$roles}) {
                   5018:                     if (ref($privileged{$dom}{$role}) eq 'HASH') {
                   5019:                         &do_cache_new('priv_'.$role,$dom,$privileged{$dom}{$role},$cachetime);
                   5020:                     } else {
                   5021:                         my %hash = ();
                   5022:                         &do_cache_new('priv_'.$role,$dom,\%hash,$cachetime);
                   5023:                     }
                   5024:                 }
                   5025:             }
                   5026:         }
                   5027:     }
                   5028:     return %privileged;
1.9       www      5029: }
1.1       albertel 5030: 
1.103     harris41 5031: # -------------------------------------------------------- Get user privileges
1.11      www      5032: 
                   5033: sub rolesinit {
1.1169    droeschl 5034:     my ($domain, $username) = @_;
                   5035:     my %userroles = ('user.login.time' => time);
                   5036:     my %rolesdump = &dump("roles", $domain, $username) or return \%userroles;
                   5037: 
                   5038:     # firstaccess and timerinterval are related to timed maps/resources. 
                   5039:     # also, blocking can be triggered by an activating timer
                   5040:     # it's saved in the user's %env.
                   5041:     my %firstaccess = &dump('firstaccesstimes', $domain, $username);
                   5042:     my %timerinterval = &dump('timerinterval', $domain, $username);
                   5043:     my (%coursetimerstarts, %firstaccchk, %firstaccenv, %coursetimerintervals,
                   5044:         %timerintchk, %timerintenv);
                   5045: 
1.1162    raeburn  5046:     foreach my $key (keys(%firstaccess)) {
1.1169    droeschl 5047:         my ($cid, $rest) = split(/\0/, $key);
1.1162    raeburn  5048:         $coursetimerstarts{$cid}{$rest} = $firstaccess{$key};
                   5049:     }
1.1169    droeschl 5050: 
1.1162    raeburn  5051:     foreach my $key (keys(%timerinterval)) {
                   5052:         my ($cid,$rest) = split(/\0/,$key);
                   5053:         $coursetimerintervals{$cid}{$rest} = $timerinterval{$key};
                   5054:     }
1.1169    droeschl 5055: 
1.11      www      5056:     my %allroles=();
1.1162    raeburn  5057:     my %allgroups=();
1.11      www      5058: 
1.1169    droeschl 5059:     for my $area (grep { ! /^rolesdef_/ } keys %rolesdump) {
                   5060:         my $role = $rolesdump{$area};
                   5061:         $area =~ s/\_\w\w$//;
                   5062: 
                   5063:         my ($trole, $tend, $tstart, $group_privs);
                   5064: 
                   5065:         if ($role =~ /^cr/) {
                   5066:         # Custom role, defined by a user 
                   5067:         # e.g., user.role.cr/msu/smith/mynewrole
                   5068:             if ($role =~ m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   5069:                 $trole = $1;
                   5070:                 ($tend, $tstart) = split('_', $2);
                   5071:             } else {
                   5072:                 $trole = $role;
                   5073:             }
                   5074:         } elsif ($role =~ m|^gr/|) {
                   5075:         # Role of member in a group, defined within a course/community
                   5076:         # e.g., user.role.gr/msu/04935610a19ee4a5fmsul1/leopards
                   5077:             ($trole, $tend, $tstart) = split(/_/, $role);
                   5078:             next if $tstart eq '-1';
                   5079:             ($trole, $group_privs) = split(/\//, $trole);
                   5080:             $group_privs = &unescape($group_privs);
                   5081:         } else {
                   5082:         # Just a normal role, defined in roles.tab
                   5083:             ($trole, $tend, $tstart) = split(/_/,$role);
                   5084:         }
                   5085: 
                   5086:         my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   5087:                  $username);
                   5088:         @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
                   5089: 
                   5090:         # role expired or not available yet?
                   5091:         $trole = '' if ($tend != 0 && $tend < $userroles{'user.login.time'}) or 
                   5092:             ($tstart != 0 && $tstart > $userroles{'user.login.time'});
                   5093: 
                   5094:         next if $area eq '' or $trole eq '';
                   5095: 
                   5096:         my $spec = "$trole.$area";
                   5097:         my ($tdummy, $tdomain, $trest) = split(/\//, $area);
                   5098: 
                   5099:         if ($trole =~ /^cr\//) {
                   5100:         # Custom role, defined by a user
                   5101:             &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   5102:         } elsif ($trole eq 'gr') {
                   5103:         # Role of a member in a group, defined within a course/community
                   5104:             &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
                   5105:             next;
                   5106:         } else {
                   5107:         # Normal role, defined in roles.tab
                   5108:             &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   5109:         }
                   5110: 
                   5111:         my $cid = $tdomain.'_'.$trest;
                   5112:         unless ($firstaccchk{$cid}) {
                   5113:             if (ref($coursetimerstarts{$cid}) eq 'HASH') {
                   5114:                 foreach my $item (keys(%{$coursetimerstarts{$cid}})) {
                   5115:                     $firstaccenv{'course.'.$cid.'.firstaccess.'.$item} = 
                   5116:                         $coursetimerstarts{$cid}{$item}; 
                   5117:                 }
                   5118:             }
                   5119:             $firstaccchk{$cid} = 1;
                   5120:         }
                   5121:         unless ($timerintchk{$cid}) {
                   5122:             if (ref($coursetimerintervals{$cid}) eq 'HASH') {
                   5123:                 foreach my $item (keys(%{$coursetimerintervals{$cid}})) {
                   5124:                     $timerintenv{'course.'.$cid.'.timerinterval.'.$item} =
                   5125:                        $coursetimerintervals{$cid}{$item};
1.1162    raeburn  5126:                 }
1.12      www      5127:             }
1.1169    droeschl 5128:             $timerintchk{$cid} = 1;
1.191     harris41 5129:         }
1.11      www      5130:     }
1.1169    droeschl 5131: 
                   5132:     @userroles{'user.author', 'user.adv'} = &set_userprivs(\%userroles,
                   5133:         \%allroles, \%allgroups);
                   5134:     $env{'user.adv'} = $userroles{'user.adv'};
                   5135: 
1.1162    raeburn  5136:     return (\%userroles,\%firstaccenv,\%timerintenv);
1.11      www      5137: }
                   5138: 
1.567     raeburn  5139: sub set_arearole {
1.1215    raeburn  5140:     my ($trole,$area,$tstart,$tend,$domain,$username,$nolog) = @_;
                   5141:     unless ($nolog) {
1.567     raeburn  5142: # log the associated role with the area
1.1215    raeburn  5143:         &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
                   5144:     }
1.743     albertel 5145:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  5146: }
                   5147: 
                   5148: sub custom_roleprivs {
                   5149:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   5150:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   5151:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 5152:     if (&hostname($homsvr) ne '') {
1.567     raeburn  5153:         my ($rdummy,$roledef)=
                   5154:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   5155:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   5156:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   5157:             if (defined($syspriv)) {
1.1043    raeburn  5158:                 if ($trest =~ /^$match_community$/) {
                   5159:                     $syspriv =~ s/bre\&S//; 
                   5160:                 }
1.567     raeburn  5161:                 $$allroles{'cm./'}.=':'.$syspriv;
                   5162:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   5163:             }
                   5164:             if ($tdomain ne '') {
                   5165:                 if (defined($dompriv)) {
                   5166:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   5167:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   5168:                 }
                   5169:                 if (($trest ne '') && (defined($coursepriv))) {
                   5170:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   5171:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   5172:                 }
                   5173:             }
                   5174:         }
                   5175:     }
                   5176: }
                   5177: 
1.678     raeburn  5178: sub group_roleprivs {
                   5179:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   5180:     my $access = 1;
                   5181:     my $now = time;
                   5182:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   5183:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   5184:     if ($access) {
1.811     albertel 5185:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  5186:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   5187:     }
                   5188: }
1.567     raeburn  5189: 
                   5190: sub standard_roleprivs {
                   5191:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   5192:     if (defined($pr{$trole.':s'})) {
                   5193:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   5194:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   5195:     }
                   5196:     if ($tdomain ne '') {
                   5197:         if (defined($pr{$trole.':d'})) {
                   5198:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   5199:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   5200:         }
                   5201:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   5202:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   5203:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   5204:         }
                   5205:     }
                   5206: }
                   5207: 
                   5208: sub set_userprivs {
1.1064    raeburn  5209:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  5210:     my $author=0;
                   5211:     my $adv=0;
1.678     raeburn  5212:     my %grouproles = ();
                   5213:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  5214:         my @groupkeys; 
1.1000    raeburn  5215:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  5216:             push(@groupkeys,$role);
                   5217:         }
                   5218:         if (ref($groups_roles) eq 'HASH') {
                   5219:             foreach my $key (keys(%{$groups_roles})) {
                   5220:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   5221:                     push(@groupkeys,$key);
                   5222:                 }
                   5223:             }
                   5224:         }
                   5225:         if (@groupkeys > 0) {
                   5226:             foreach my $role (@groupkeys) {
                   5227:                 my ($trole,$area,$sec,$extendedarea);
                   5228:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   5229:                     $trole = $1;
                   5230:                     $area = $2;
                   5231:                     $sec = $3;
                   5232:                     $extendedarea = $area.$sec;
                   5233:                     if (exists($$allgroups{$area})) {
                   5234:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   5235:                             my $spec = $trole.'.'.$extendedarea;
                   5236:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  5237:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  5238:                         }
1.678     raeburn  5239:                     }
                   5240:                 }
                   5241:             }
                   5242:         }
                   5243:     }
1.800     albertel 5244:     foreach my $group (keys(%grouproles)) {
                   5245:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  5246:     }
1.800     albertel 5247:     foreach my $role (keys(%{$allroles})) {
                   5248:         my %thesepriv;
1.941     raeburn  5249:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 5250:         foreach my $item (split(/:/,$$allroles{$role})) {
                   5251:             if ($item ne '') {
                   5252:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  5253:                 if ($restrictions eq '') {
                   5254:                     $thesepriv{$privilege}='F';
                   5255:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   5256:                     $thesepriv{$privilege}.=$restrictions;
                   5257:                 }
                   5258:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   5259:             }
                   5260:         }
                   5261:         my $thesestr='';
1.1104    raeburn  5262:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 5263: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   5264: 	}
                   5265:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  5266:     }
                   5267:     return ($author,$adv);
                   5268: }
                   5269: 
1.994     raeburn  5270: sub role_status {
1.1104    raeburn  5271:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  5272:     my @pwhere = ();
                   5273:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
                   5274:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                   5275:         unless (!defined($$role) || $$role eq '') {
                   5276:             $$where=join('.',@pwhere);
                   5277:             $$trolecode=$$role.'.'.$$where;
                   5278:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   5279:             $$tstatus='is';
1.1104    raeburn  5280:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  5281:                 $$tstatus='future';
1.1034    raeburn  5282:                 if ($$tstart<$now) {
                   5283:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  5284:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  5285:                             my (%allroles,%allgroups,$group_privs,
                   5286:                                 %groups_roles,@rolecodes);
1.1002    raeburn  5287:                             my %userroles = (
                   5288:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   5289:                             );
1.1064    raeburn  5290:                             @rolecodes = ('cm'); 
1.1002    raeburn  5291:                             my $spec=$$role.'.'.$$where;
                   5292:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   5293:                             if ($$role =~ /^cr\//) {
                   5294:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  5295:                                 push(@rolecodes,'cr');
1.1002    raeburn  5296:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  5297:                                 push(@rolecodes,$$role);
1.1002    raeburn  5298:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   5299:                                                     $env{'user.name'});
1.1064    raeburn  5300:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  5301:                                 (undef,my $group_privs) = split(/\//,$trole);
                   5302:                                 $group_privs = &unescape($group_privs);
                   5303:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  5304:                                 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  5305:                                 &get_groups_roles($tdomain,$trest,
                   5306:                                                   \%course_roles,\@rolecodes,
                   5307:                                                   \%groups_roles);
1.1002    raeburn  5308:                             } else {
1.1064    raeburn  5309:                                 push(@rolecodes,$$role);
1.1002    raeburn  5310:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   5311:                             }
1.1064    raeburn  5312:                             my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   5313:                             &appenv(\%userroles,\@rolecodes);
1.1002    raeburn  5314:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
                   5315:                         }
                   5316:                     }
1.1034    raeburn  5317:                     $$tstatus = 'is';
1.1002    raeburn  5318:                 }
1.994     raeburn  5319:             }
                   5320:             if ($$tend) {
1.1104    raeburn  5321:                 if ($$tend<$update) {
1.994     raeburn  5322:                     $$tstatus='expired';
                   5323:                 } elsif ($$tend<$now) {
                   5324:                     $$tstatus='will_not';
                   5325:                 }
                   5326:             }
                   5327:         }
                   5328:     }
                   5329: }
                   5330: 
1.1104    raeburn  5331: sub get_groups_roles {
                   5332:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   5333:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   5334:                   (ref($rolecodes) eq 'ARRAY') && 
                   5335:                   (ref($groups_roles) eq 'HASH')); 
                   5336:     if (keys(%{$cdom_courseroles}) > 0) {
                   5337:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   5338:         if ($cdom ne '' && $cnum ne '') {
                   5339:             foreach my $key (keys(%{$cdom_courseroles})) {
                   5340:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   5341:                     my $crsrole = $1;
                   5342:                     my $crssec = $2;
                   5343:                     if ($crsrole =~ /^cr/) {
                   5344:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   5345:                             push(@{$rolecodes},'cr');
                   5346:                         }
                   5347:                     } else {
                   5348:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   5349:                             push(@{$rolecodes},$crsrole);
                   5350:                         }
                   5351:                     }
                   5352:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   5353:                     if ($crssec ne '') {
                   5354:                         $rolekey .= "/$crssec";
                   5355:                     }
                   5356:                     $rolekey .= './';
                   5357:                     $groups_roles->{$rolekey} = $rolecodes;
                   5358:                 }
                   5359:             }
                   5360:         }
                   5361:     }
                   5362:     return;
                   5363: }
                   5364: 
                   5365: sub delete_env_groupprivs {
                   5366:     my ($where,$courseroles,$possroles) = @_;
                   5367:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   5368:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   5369:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   5370:         %{$courseroles->{$udom}} =
                   5371:             &get_my_roles('','','userroles',['active'],
                   5372:                           $possroles,[$udom],1);
                   5373:     }
                   5374:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   5375:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   5376:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   5377:             my $area = '/'.$cdom.'/'.$cnum;
                   5378:             my $privkey = "user.priv.$crsrole.$area";
                   5379:             if ($crssec ne '') {
                   5380:                 $privkey .= '/'.$crssec;
                   5381:             }
                   5382:             $privkey .= ".$area/$group";
                   5383:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   5384:         }
                   5385:     }
                   5386:     return;
                   5387: }
                   5388: 
1.994     raeburn  5389: sub check_adhoc_privs {
1.1104    raeburn  5390:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller) = @_;
1.994     raeburn  5391:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
1.1185    raeburn  5392:     my $setprivs;
1.994     raeburn  5393:     if ($env{$cckey}) {
                   5394:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  5395:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  5396:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1088    raeburn  5397:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.1185    raeburn  5398:             $setprivs = 1;
1.994     raeburn  5399:         }
                   5400:     } else {
1.1088    raeburn  5401:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.1185    raeburn  5402:         $setprivs = 1;
1.994     raeburn  5403:     }
1.1185    raeburn  5404:     return $setprivs;
1.994     raeburn  5405: }
                   5406: 
                   5407: sub set_adhoc_privileges {
                   5408: # role can be cc or ca
1.1088    raeburn  5409:     my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994     raeburn  5410:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   5411:     my $spec = $role.'.'.$area;
                   5412:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
1.1215    raeburn  5413:                                   $env{'user.name'},1);
1.994     raeburn  5414:     my %ccrole = ();
                   5415:     &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
                   5416:     my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
                   5417:     &appenv(\%userroles,[$role,'cm']);
                   5418:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1088    raeburn  5419:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
                   5420:         &appenv( {'request.role'        => $spec,
                   5421:                   'request.role.domain' => $dcdom,
                   5422:                   'request.course.sec'  => ''
                   5423:                  }
                   5424:                );
                   5425:         my $tadv=0;
                   5426:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   5427:         &appenv({'request.role.adv'    => $tadv});
                   5428:     }
1.994     raeburn  5429: }
                   5430: 
1.12      www      5431: # --------------------------------------------------------------- get interface
                   5432: 
                   5433: sub get {
1.131     albertel 5434:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      5435:    my $items='';
1.800     albertel 5436:    foreach my $item (@$storearr) {
                   5437:        $items.=&escape($item).'&';
1.191     harris41 5438:    }
1.12      www      5439:    $items=~s/\&$//;
1.620     albertel 5440:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5441:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 5442:    my $uhome=&homeserver($uname,$udomain);
                   5443: 
1.133     albertel 5444:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      5445:    my @pairs=split(/\&/,$rep);
1.273     albertel 5446:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   5447:      return @pairs;
                   5448:    }
1.15      www      5449:    my %returnhash=();
1.42      www      5450:    my $i=0;
1.800     albertel 5451:    foreach my $item (@$storearr) {
                   5452:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      5453:       $i++;
1.191     harris41 5454:    }
1.15      www      5455:    return %returnhash;
1.27      www      5456: }
                   5457: 
                   5458: # --------------------------------------------------------------- del interface
                   5459: 
                   5460: sub del {
1.133     albertel 5461:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      5462:    my $items='';
1.800     albertel 5463:    foreach my $item (@$storearr) {
                   5464:        $items.=&escape($item).'&';
1.191     harris41 5465:    }
1.984     neumanie 5466: 
1.27      www      5467:    $items=~s/\&$//;
1.620     albertel 5468:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5469:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 5470:    my $uhome=&homeserver($uname,$udomain);
                   5471:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      5472: }
                   5473: 
                   5474: # -------------------------------------------------------------- dump interface
                   5475: 
1.1180    droeschl 5476: sub unserialize {
                   5477:     my ($rep, $escapedkeys) = @_;
                   5478: 
                   5479:     return {} if $rep =~ /^error/;
                   5480: 
                   5481:     my %returnhash=();
                   5482: 	foreach my $item (split /\&/, $rep) {
                   5483: 	    my ($key, $value) = split(/=/, $item, 2);
                   5484: 	    $key = unescape($key) unless $escapedkeys;
                   5485: 	    next if $key =~ /^error: 2 /;
                   5486: 	    $returnhash{$key} = Apache::lonnet::thaw_unescape($value);
                   5487: 	}
                   5488:     #return %returnhash;
                   5489:     return \%returnhash;
                   5490: }        
                   5491: 
                   5492: # see Lond::dump_with_regexp
                   5493: # if $escapedkeys hash keys won't get unescaped.
1.15      www      5494: sub dump {
1.1180    droeschl 5495:     my ($namespace,$udomain,$uname,$regexp,$range,$escapedkeys)=@_;
1.755     albertel 5496:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5497:     if (!$uname) { $uname=$env{'user.name'}; }
                   5498:     my $uhome=&homeserver($uname,$udomain);
1.1167    droeschl 5499: 
1.1180    droeschl 5500:     my $reply;
                   5501:     if (grep { $_ eq $uhome } current_machine_ids()) {
                   5502:         # user is hosted on this machine
                   5503:         $reply = LONCAPA::Lond::dump_with_regexp(join(":", ($udomain,
1.1226    raeburn  5504:                     $uname, $namespace, $regexp, $range)), $perlvar{'lonVersion'});
1.1180    droeschl 5505:         return %{unserialize($reply, $escapedkeys)};
                   5506:     }
1.755     albertel 5507:     if ($regexp) {
                   5508: 	$regexp=&escape($regexp);
                   5509:     } else {
                   5510: 	$regexp='.';
                   5511:     }
1.1166    raeburn  5512:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
1.755     albertel 5513:     my @pairs=split(/\&/,$rep);
                   5514:     my %returnhash=();
1.1098    foxr     5515:     if (!($rep =~ /^error/ )) {
                   5516: 	foreach my $item (@pairs) {
                   5517: 	    my ($key,$value)=split(/=/,$item,2);
1.1180    droeschl 5518:         $key = unescape($key) unless $escapedkeys;
                   5519:         #$key = &unescape($key);
1.1098    foxr     5520: 	    next if ($key =~ /^error: 2 /);
                   5521: 	    $returnhash{$key}=&thaw_unescape($value);
                   5522: 	}
1.755     albertel 5523:     }
                   5524:     return %returnhash;
1.407     www      5525: }
                   5526: 
1.1098    foxr     5527: 
1.717     albertel 5528: # --------------------------------------------------------- dumpstore interface
                   5529: 
                   5530: sub dumpstore {
                   5531:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.1180    droeschl 5532:    # same as dump but keys must be escaped. They may contain colon separated
                   5533:    # lists of values that may themself contain colons (e.g. symbs).
                   5534:    return &dump($namespace, $udomain, $uname, $regexp, $range, 1);
1.717     albertel 5535: }
                   5536: 
1.407     www      5537: # -------------------------------------------------------------- keys interface
                   5538: 
                   5539: sub getkeys {
                   5540:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 5541:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5542:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      5543:    my $uhome=&homeserver($uname,$udomain);
                   5544:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   5545:    my @keyarray=();
1.800     albertel 5546:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  5547:       next if ($key =~ /^error: 2 /);
1.800     albertel 5548:       push(@keyarray,&unescape($key));
1.407     www      5549:    }
                   5550:    return @keyarray;
1.318     matthew  5551: }
                   5552: 
1.319     matthew  5553: # --------------------------------------------------------------- currentdump
                   5554: sub currentdump {
1.328     matthew  5555:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 5556:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   5557:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   5558:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  5559:    my $uhome = &homeserver($sname,$sdom);
1.1180    droeschl 5560:    my $rep;
                   5561: 
                   5562:    if (grep { $_ eq $uhome } current_machine_ids()) {
                   5563:        $rep = LONCAPA::Lond::dump_profile_database(join(":", ($sdom, $sname, 
                   5564:                    $courseid)));
                   5565:    } else {
                   5566:        $rep = reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
                   5567:    }
                   5568: 
1.318     matthew  5569:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  5570:    #
1.318     matthew  5571:    my %returnhash=();
1.319     matthew  5572:    #
                   5573:    if ($rep eq "unknown_cmd") { 
                   5574:        # an old lond will not know currentdump
                   5575:        # Do a dump and make it look like a currentdump
1.822     albertel 5576:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  5577:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   5578:        my %hash = @tmp;
                   5579:        @tmp=();
1.424     matthew  5580:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  5581:    } else {
                   5582:        my @pairs=split(/\&/,$rep);
1.800     albertel 5583:        foreach my $pair (@pairs) {
                   5584:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  5585:            my ($symb,$param) = split(/:/,$key);
                   5586:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 5587:                                                         &thaw_unescape($value);
1.319     matthew  5588:        }
1.191     harris41 5589:    }
1.12      www      5590:    return %returnhash;
1.424     matthew  5591: }
                   5592: 
                   5593: sub convert_dump_to_currentdump{
                   5594:     my %hash = %{shift()};
                   5595:     my %returnhash;
                   5596:     # Code ripped from lond, essentially.  The only difference
                   5597:     # here is the unescaping done by lonnet::dump().  Conceivably
                   5598:     # we might run in to problems with parameter names =~ /^v\./
                   5599:     while (my ($key,$value) = each(%hash)) {
                   5600:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 5601: 	$symb  = &unescape($symb);
                   5602: 	$param = &unescape($param);
1.424     matthew  5603:         next if ($v eq 'version' || $symb eq 'keys');
                   5604:         next if (exists($returnhash{$symb}) &&
                   5605:                  exists($returnhash{$symb}->{$param}) &&
                   5606:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   5607:         $returnhash{$symb}->{$param}=$value;
                   5608:         $returnhash{$symb}->{'v.'.$param}=$v;
                   5609:     }
                   5610:     #
                   5611:     # Remove all of the keys in the hashes which keep track of
                   5612:     # the version of the parameter.
                   5613:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   5614:         # use a foreach because we are going to delete from the hash.
                   5615:         foreach my $key (keys(%$param_hash)) {
                   5616:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   5617:         }
                   5618:     }
                   5619:     return \%returnhash;
1.12      www      5620: }
                   5621: 
1.627     albertel 5622: # ------------------------------------------------------ critical inc interface
                   5623: 
                   5624: sub cinc {
                   5625:     return &inc(@_,'critical');
                   5626: }
                   5627: 
1.449     matthew  5628: # --------------------------------------------------------------- inc interface
                   5629: 
                   5630: sub inc {
1.627     albertel 5631:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 5632:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5633:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  5634:     my $uhome=&homeserver($uname,$udomain);
                   5635:     my $items='';
                   5636:     if (! ref($store)) {
                   5637:         # got a single value, so use that instead
                   5638:         $items = &escape($store).'=&';
                   5639:     } elsif (ref($store) eq 'SCALAR') {
                   5640:         $items = &escape($$store).'=&';        
                   5641:     } elsif (ref($store) eq 'ARRAY') {
                   5642:         $items = join('=&',map {&escape($_);} @{$store});
                   5643:     } elsif (ref($store) eq 'HASH') {
                   5644:         while (my($key,$value) = each(%{$store})) {
                   5645:             $items.= &escape($key).'='.&escape($value).'&';
                   5646:         }
                   5647:     }
                   5648:     $items=~s/\&$//;
1.627     albertel 5649:     if ($critical) {
                   5650: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5651:     } else {
                   5652: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5653:     }
1.449     matthew  5654: }
                   5655: 
1.12      www      5656: # --------------------------------------------------------------- put interface
                   5657: 
                   5658: sub put {
1.134     albertel 5659:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5660:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5661:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5662:    my $uhome=&homeserver($uname,$udomain);
1.12      www      5663:    my $items='';
1.800     albertel 5664:    foreach my $item (keys(%$storehash)) {
                   5665:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5666:    }
1.12      www      5667:    $items=~s/\&$//;
1.134     albertel 5668:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      5669: }
                   5670: 
1.631     albertel 5671: # ------------------------------------------------------------ newput interface
                   5672: 
                   5673: sub newput {
                   5674:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   5675:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5676:    if (!$uname) { $uname=$env{'user.name'}; }
                   5677:    my $uhome=&homeserver($uname,$udomain);
                   5678:    my $items='';
                   5679:    foreach my $key (keys(%$storehash)) {
                   5680:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   5681:    }
                   5682:    $items=~s/\&$//;
                   5683:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   5684: }
                   5685: 
                   5686: # ---------------------------------------------------------  putstore interface
                   5687: 
1.524     raeburn  5688: sub putstore {
1.715     albertel 5689:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 5690:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5691:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  5692:    my $uhome=&homeserver($uname,$udomain);
                   5693:    my $items='';
1.715     albertel 5694:    foreach my $key (keys(%$storehash)) {
                   5695:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  5696:    }
1.715     albertel 5697:    $items=~s/\&$//;
1.716     albertel 5698:    my $esc_symb=&escape($symb);
                   5699:    my $esc_v=&escape($version);
1.715     albertel 5700:    my $reply =
1.716     albertel 5701:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 5702: 	      $uhome);
                   5703:    if ($reply eq 'unknown_cmd') {
1.716     albertel 5704:        # gfall back to way things use to be done
1.715     albertel 5705:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   5706: 			    $uname);
1.524     raeburn  5707:    }
1.715     albertel 5708:    return $reply;
                   5709: }
                   5710: 
                   5711: sub old_putstore {
1.716     albertel 5712:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   5713:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5714:     if (!$uname) { $uname=$env{'user.name'}; }
                   5715:     my $uhome=&homeserver($uname,$udomain);
                   5716:     my %newstorehash;
1.800     albertel 5717:     foreach my $item (keys(%$storehash)) {
                   5718: 	my $key = $version.':'.&escape($symb).':'.$item;
                   5719: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 5720:     }
                   5721:     my $items='';
                   5722:     my %allitems = ();
1.800     albertel 5723:     foreach my $item (keys(%newstorehash)) {
                   5724: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 5725: 	    my $key = $1.':keys:'.$2;
                   5726: 	    $allitems{$key} .= $3.':';
                   5727: 	}
1.800     albertel 5728: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 5729:     }
1.800     albertel 5730:     foreach my $item (keys(%allitems)) {
                   5731: 	$allitems{$item} =~ s/\:$//;
                   5732: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 5733:     }
                   5734:     $items=~s/\&$//;
                   5735:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  5736: }
                   5737: 
1.47      www      5738: # ------------------------------------------------------ critical put interface
                   5739: 
                   5740: sub cput {
1.134     albertel 5741:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5742:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5743:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5744:    my $uhome=&homeserver($uname,$udomain);
1.47      www      5745:    my $items='';
1.800     albertel 5746:    foreach my $item (keys(%$storehash)) {
                   5747:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5748:    }
1.47      www      5749:    $items=~s/\&$//;
1.134     albertel 5750:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5751: }
                   5752: 
                   5753: # -------------------------------------------------------------- eget interface
                   5754: 
                   5755: sub eget {
1.133     albertel 5756:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      5757:    my $items='';
1.800     albertel 5758:    foreach my $item (@$storearr) {
                   5759:        $items.=&escape($item).'&';
1.191     harris41 5760:    }
1.12      www      5761:    $items=~s/\&$//;
1.620     albertel 5762:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5763:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 5764:    my $uhome=&homeserver($uname,$udomain);
                   5765:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5766:    my @pairs=split(/\&/,$rep);
                   5767:    my %returnhash=();
1.42      www      5768:    my $i=0;
1.800     albertel 5769:    foreach my $item (@$storearr) {
                   5770:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      5771:       $i++;
1.191     harris41 5772:    }
1.12      www      5773:    return %returnhash;
                   5774: }
                   5775: 
1.667     albertel 5776: # ------------------------------------------------------------ tmpput interface
                   5777: sub tmpput {
1.802     raeburn  5778:     my ($storehash,$server,$context)=@_;
1.667     albertel 5779:     my $items='';
1.800     albertel 5780:     foreach my $item (keys(%$storehash)) {
                   5781: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 5782:     }
                   5783:     $items=~s/\&$//;
1.802     raeburn  5784:     if (defined($context)) {
                   5785:         $items .= ':'.&escape($context);
                   5786:     }
1.667     albertel 5787:     return &reply("tmpput:$items",$server);
                   5788: }
                   5789: 
                   5790: # ------------------------------------------------------------ tmpget interface
                   5791: sub tmpget {
1.688     albertel 5792:     my ($token,$server)=@_;
                   5793:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5794:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 5795:     my %returnhash;
                   5796:     foreach my $item (split(/\&/,$rep)) {
                   5797: 	my ($key,$value)=split(/=/,$item);
1.951     raeburn  5798:         next if ($key =~ /^error: 2 /);
1.667     albertel 5799: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   5800:     }
                   5801:     return %returnhash;
                   5802: }
                   5803: 
1.1113    raeburn  5804: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 5805: sub tmpdel {
                   5806:     my ($token,$server)=@_;
                   5807:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5808:     return &reply("tmpdel:$token",$server);
                   5809: }
                   5810: 
1.1198    raeburn  5811: # ------------------------------------------------------------ get_timebased_id 
                   5812: 
                   5813: sub get_timebased_id {
                   5814:     my ($prefix,$keyid,$namespace,$cdom,$cnum,$idtype,$who,$locktries,
                   5815:         $maxtries) = @_;
                   5816:     my ($newid,$error,$dellock);
                   5817:     unless (($prefix =~ /^\w+$/) && ($keyid =~ /^\w+$/) && ($namespace ne '')) {  
                   5818:         return ('','ok','invalid call to get suffix');
                   5819:     }
                   5820: 
                   5821: # set defaults for any optional args for which values were not supplied
                   5822:     if ($who eq '') {
                   5823:         $who = $env{'user.name'}.':'.$env{'user.domain'};
                   5824:     }
                   5825:     if (!$locktries) {
                   5826:         $locktries = 3;
                   5827:     }
                   5828:     if (!$maxtries) {
                   5829:         $maxtries = 10;
                   5830:     }
                   5831:     
                   5832:     if (($cdom eq '') || ($cnum eq '')) {
                   5833:         if ($env{'request.course.id'}) {
                   5834:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5835:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5836:         }
                   5837:         if (($cdom eq '') || ($cnum eq '')) {
                   5838:             return ('','ok','call to get suffix not in course context');
                   5839:         }
                   5840:     }
                   5841: 
                   5842: # construct locking item
                   5843:     my $lockhash = {
                   5844:                       $prefix."\0".'locked_'.$keyid => $who,
                   5845:                    };
                   5846:     my $tries = 0;
                   5847: 
                   5848: # attempt to get lock on nohist_$namespace file
                   5849:     my $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   5850:     while (($gotlock ne 'ok') && $tries <$locktries) {
                   5851:         $tries ++;
                   5852:         sleep 1;
                   5853:         $gotlock = &Apache::lonnet::newput('nohist_'.$namespace,$lockhash,$cdom,$cnum);
                   5854:     }
                   5855: 
                   5856: # attempt to get unique identifier, based on current timestamp
                   5857:     if ($gotlock eq 'ok') {
                   5858:         my %inuse = &Apache::lonnet::dump('nohist_'.$namespace,$cdom,$cnum,$prefix);
                   5859:         my $id = time;
                   5860:         $newid = $id;
                   5861:         my $idtries = 0;
                   5862:         while (exists($inuse{$prefix."\0".$newid}) && $idtries < $maxtries) {
                   5863:             if ($idtype eq 'concat') {
                   5864:                 $newid = $id.$idtries;
                   5865:             } else {
                   5866:                 $newid ++;
                   5867:             }
                   5868:             $idtries ++;
                   5869:         }
                   5870:         if (!exists($inuse{$prefix."\0".$newid})) {
                   5871:             my %new_item =  (
                   5872:                               $prefix."\0".$newid => $who,
                   5873:                             );
                   5874:             my $putresult = &Apache::lonnet::put('nohist_'.$namespace,\%new_item,
                   5875:                                                  $cdom,$cnum);
                   5876:             if ($putresult ne 'ok') {
                   5877:                 undef($newid);
                   5878:                 $error = 'error saving new item: '.$putresult;
                   5879:             }
                   5880:         } else {
                   5881:              $error = ('error: no unique suffix available for the new item ');
                   5882:         }
                   5883: #  remove lock
                   5884:         my @del_lock = ($prefix."\0".'locked_'.$keyid);
                   5885:         $dellock = &Apache::lonnet::del('nohist_'.$namespace,\@del_lock,$cdom,$cnum);
                   5886:     } else {
                   5887:         $error = "error: could not obtain lockfile\n";
                   5888:         $dellock = 'ok';
                   5889:     }
                   5890:     return ($newid,$dellock,$error);
                   5891: }
                   5892: 
1.765     albertel 5893: # -------------------------------------------------- portfolio access checking
                   5894: 
                   5895: sub portfolio_access {
1.766     albertel 5896:     my ($requrl) = @_;
1.765     albertel 5897:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   5898:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  5899:     if ($result) {
                   5900:         my %setters;
                   5901:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5902:             my ($startblock,$endblock) =
                   5903:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   5904:             if ($startblock && $endblock) {
                   5905:                 return 'B';
                   5906:             }
                   5907:         } else {
                   5908:             my ($startblock,$endblock) =
                   5909:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   5910:             if ($startblock && $endblock) {
                   5911:                 return 'B';
                   5912:             }
                   5913:         }
                   5914:     }
1.765     albertel 5915:     if ($result eq 'ok') {
1.766     albertel 5916:        return 'F';
1.765     albertel 5917:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 5918:        return 'A';
1.765     albertel 5919:     }
1.766     albertel 5920:     return '';
1.765     albertel 5921: }
                   5922: 
                   5923: sub get_portfolio_access {
1.767     albertel 5924:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   5925: 
                   5926:     if (!ref($access_hash)) {
                   5927: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   5928: 	my %access_controls = &get_access_controls($current_perms,$group,
                   5929: 						   $file_name);
                   5930: 	$access_hash = $access_controls{$file_name};
                   5931:     }
                   5932: 
1.765     albertel 5933:     my ($public,$guest,@domains,@users,@courses,@groups);
                   5934:     my $now = time;
                   5935:     if (ref($access_hash) eq 'HASH') {
                   5936:         foreach my $key (keys(%{$access_hash})) {
                   5937:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5938:             if ($start > $now) {
                   5939:                 next;
                   5940:             }
                   5941:             if ($end && $end<$now) {
                   5942:                 next;
                   5943:             }
                   5944:             if ($scope eq 'public') {
                   5945:                 $public = $key;
                   5946:                 last;
                   5947:             } elsif ($scope eq 'guest') {
                   5948:                 $guest = $key;
                   5949:             } elsif ($scope eq 'domains') {
                   5950:                 push(@domains,$key);
                   5951:             } elsif ($scope eq 'users') {
                   5952:                 push(@users,$key);
                   5953:             } elsif ($scope eq 'course') {
                   5954:                 push(@courses,$key);
                   5955:             } elsif ($scope eq 'group') {
                   5956:                 push(@groups,$key);
                   5957:             }
                   5958:         }
                   5959:         if ($public) {
                   5960:             return 'ok';
                   5961:         }
                   5962:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5963:             if ($guest) {
                   5964:                 return $guest;
                   5965:             }
                   5966:         } else {
                   5967:             if (@domains > 0) {
                   5968:                 foreach my $domkey (@domains) {
                   5969:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   5970:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   5971:                             return 'ok';
                   5972:                         }
                   5973:                     }
                   5974:                 }
                   5975:             }
                   5976:             if (@users > 0) {
                   5977:                 foreach my $userkey (@users) {
1.865     raeburn  5978:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   5979:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   5980:                             if (ref($item) eq 'HASH') {
                   5981:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   5982:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   5983:                                     return 'ok';
                   5984:                                 }
                   5985:                             }
                   5986:                         }
                   5987:                     } 
1.765     albertel 5988:                 }
                   5989:             }
                   5990:             my %roleshash;
                   5991:             my @courses_and_groups = @courses;
                   5992:             push(@courses_and_groups,@groups); 
                   5993:             if (@courses_and_groups > 0) {
                   5994:                 my (%allgroups,%allroles); 
                   5995:                 my ($start,$end,$role,$sec,$group);
                   5996:                 foreach my $envkey (%env) {
1.1060    raeburn  5997:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 5998:                         my $cid = $2.'_'.$3; 
                   5999:                         if ($1 eq 'gr') {
                   6000:                             $group = $4;
                   6001:                             $allgroups{$cid}{$group} = $env{$envkey};
                   6002:                         } else {
                   6003:                             if ($4 eq '') {
                   6004:                                 $sec = 'none';
                   6005:                             } else {
                   6006:                                 $sec = $4;
                   6007:                             }
                   6008:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   6009:                         }
1.811     albertel 6010:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 6011:                         my $cid = $2.'_'.$3;
                   6012:                         if ($4 eq '') {
                   6013:                             $sec = 'none';
                   6014:                         } else {
                   6015:                             $sec = $4;
                   6016:                         }
                   6017:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   6018:                     }
                   6019:                 }
                   6020:                 if (keys(%allroles) == 0) {
                   6021:                     return;
                   6022:                 }
                   6023:                 foreach my $key (@courses_and_groups) {
                   6024:                     my %content = %{$$access_hash{$key}};
                   6025:                     my $cnum = $content{'number'};
                   6026:                     my $cdom = $content{'domain'};
                   6027:                     my $cid = $cdom.'_'.$cnum;
                   6028:                     if (!exists($allroles{$cid})) {
                   6029:                         next;
                   6030:                     }    
                   6031:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   6032:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   6033:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   6034:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   6035:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   6036:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   6037:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   6038:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   6039:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   6040:                                         if (grep/^all$/,@sections) {
                   6041:                                             return 'ok';
                   6042:                                         } else {
                   6043:                                             if (grep/^$sec$/,@sections) {
                   6044:                                                 return 'ok';
                   6045:                                             }
                   6046:                                         }
                   6047:                                     }
                   6048:                                 }
                   6049:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   6050:                                     if (grep/^none$/,@groups) {
                   6051:                                         return 'ok';
                   6052:                                     }
                   6053:                                 } else {
                   6054:                                     if (grep/^all$/,@groups) {
                   6055:                                         return 'ok';
                   6056:                                     } 
                   6057:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   6058:                                         if (grep/^$group$/,@groups) {
                   6059:                                             return 'ok';
                   6060:                                         }
                   6061:                                     }
                   6062:                                 } 
                   6063:                             }
                   6064:                         }
                   6065:                     }
                   6066:                 }
                   6067:             }
                   6068:             if ($guest) {
                   6069:                 return $guest;
                   6070:             }
                   6071:         }
                   6072:     }
                   6073:     return;
                   6074: }
                   6075: 
                   6076: sub course_group_datechecker {
                   6077:     my ($dates,$now,$status) = @_;
                   6078:     my ($start,$end) = split(/\./,$dates);
                   6079:     if (!$start && !$end) {
                   6080:         return 'ok';
                   6081:     }
                   6082:     if (grep/^active$/,@{$status}) {
                   6083:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   6084:             return 'ok';
                   6085:         }
                   6086:     }
                   6087:     if (grep/^previous$/,@{$status}) {
                   6088:         if ($end > $now ) {
                   6089:             return 'ok';
                   6090:         }
                   6091:     }
                   6092:     if (grep/^future$/,@{$status}) {
                   6093:         if ($start > $now) {
                   6094:             return 'ok';
                   6095:         }
                   6096:     }
                   6097:     return; 
                   6098: }
                   6099: 
                   6100: sub parse_portfolio_url {
                   6101:     my ($url) = @_;
                   6102: 
                   6103:     my ($type,$udom,$unum,$group,$file_name);
                   6104:     
1.823     albertel 6105:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 6106: 	$type = 1;
                   6107:         $udom = $1;
                   6108:         $unum = $2;
                   6109:         $file_name = $3;
1.823     albertel 6110:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 6111: 	$type = 2;
                   6112:         $udom = $1;
                   6113:         $unum = $2;
                   6114:         $group = $3;
                   6115:         $file_name = $3.'/'.$4;
                   6116:     }
                   6117:     if (wantarray) {
                   6118: 	return ($type,$udom,$unum,$file_name,$group);
                   6119:     }
                   6120:     return $type;
                   6121: }
                   6122: 
                   6123: sub is_portfolio_url {
                   6124:     my ($url) = @_;
                   6125:     return scalar(&parse_portfolio_url($url));
                   6126: }
                   6127: 
1.798     raeburn  6128: sub is_portfolio_file {
                   6129:     my ($file) = @_;
1.820     raeburn  6130:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  6131:         return 1;
                   6132:     }
                   6133:     return;
                   6134: }
                   6135: 
1.976     raeburn  6136: sub usertools_access {
1.1084    raeburn  6137:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  6138:     my ($access,%tools);
                   6139:     if ($context eq '') {
                   6140:         $context = 'tools';
                   6141:     }
                   6142:     if ($context eq 'requestcourses') {
                   6143:         %tools = (
                   6144:                       official   => 1,
                   6145:                       unofficial => 1,
1.1006    raeburn  6146:                       community  => 1,
1.985     raeburn  6147:                  );
1.1183    raeburn  6148:     } elsif ($context eq 'requestauthor') {
                   6149:         %tools = (
                   6150:                       requestauthor => 1,
                   6151:                  );
1.985     raeburn  6152:     } else {
                   6153:         %tools = (
                   6154:                       aboutme   => 1,
                   6155:                       blog      => 1,
1.1177    raeburn  6156:                       webdav    => 1,
1.985     raeburn  6157:                       portfolio => 1,
                   6158:                  );
                   6159:     }
1.976     raeburn  6160:     return if (!defined($tools{$tool}));
                   6161: 
                   6162:     if ((!defined($udom)) || (!defined($uname))) {
                   6163:         $udom = $env{'user.domain'};
                   6164:         $uname = $env{'user.name'};
                   6165:     }
                   6166: 
1.978     raeburn  6167:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   6168:         if ($action ne 'reload') {
1.985     raeburn  6169:             if ($context eq 'requestcourses') {
                   6170:                 return $env{'environment.canrequest.'.$tool};
1.1183    raeburn  6171:             } elsif ($context eq 'requestauthor') {
                   6172:                 return $env{'environment.canrequest.author'};
1.985     raeburn  6173:             } else {
                   6174:                 return $env{'environment.availabletools.'.$tool};
                   6175:             }
                   6176:         }
1.976     raeburn  6177:     }
                   6178: 
1.1183    raeburn  6179:     my ($toolstatus,$inststatus,$envkey);
                   6180:     if ($context eq 'requestauthor') {
                   6181:         $envkey = $context; 
                   6182:     } else {
                   6183:         $envkey = $context.'.'.$tool;
                   6184:     }
1.976     raeburn  6185: 
1.985     raeburn  6186:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   6187:          ($action ne 'reload')) {
1.1183    raeburn  6188:         $toolstatus = $env{'environment.'.$envkey};
1.976     raeburn  6189:         $inststatus = $env{'environment.inststatus'};
                   6190:     } else {
1.1084    raeburn  6191:         if (ref($userenvref) eq 'HASH') {
1.1183    raeburn  6192:             $toolstatus = $userenvref->{$envkey};
1.1084    raeburn  6193:             $inststatus = $userenvref->{'inststatus'};
                   6194:         } else {
1.1183    raeburn  6195:             my %userenv = &userenvironment($udom,$uname,$envkey,'inststatus');
                   6196:             $toolstatus = $userenv{$envkey};
1.1084    raeburn  6197:             $inststatus = $userenv{'inststatus'};
                   6198:         }
1.976     raeburn  6199:     }
                   6200: 
                   6201:     if ($toolstatus ne '') {
                   6202:         if ($toolstatus) {
                   6203:             $access = 1;
                   6204:         } else {
                   6205:             $access = 0;
                   6206:         }
                   6207:         return $access;
                   6208:     }
                   6209: 
1.1084    raeburn  6210:     my ($is_adv,%domdef);
                   6211:     if (ref($is_advref) eq 'HASH') {
                   6212:         $is_adv = $is_advref->{'is_adv'};
                   6213:     } else {
                   6214:         $is_adv = &is_advanced_user($udom,$uname);
                   6215:     }
                   6216:     if (ref($domdefref) eq 'HASH') {
                   6217:         %domdef = %{$domdefref};
                   6218:     } else {
                   6219:         %domdef = &get_domain_defaults($udom);
                   6220:     }
1.976     raeburn  6221:     if (ref($domdef{$tool}) eq 'HASH') {
                   6222:         if ($is_adv) {
                   6223:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   6224:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   6225:                     $access = 1;
                   6226:                 } else {
                   6227:                     $access = 0;
                   6228:                 }
                   6229:                 return $access;
                   6230:             }
                   6231:         }
                   6232:         if ($inststatus ne '') {
                   6233:             my ($hasaccess,$hasnoaccess);
                   6234:             foreach my $affiliation (split(/:/,$inststatus)) {
                   6235:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   6236:                     if ($domdef{$tool}{$affiliation}) {
                   6237:                         $hasaccess = 1;
                   6238:                     } else {
                   6239:                         $hasnoaccess = 1;
                   6240:                     }
                   6241:                 }
                   6242:             }
                   6243:             if ($hasaccess || $hasnoaccess) {
                   6244:                 if ($hasaccess) {
                   6245:                     $access = 1;
                   6246:                 } elsif ($hasnoaccess) {
                   6247:                     $access = 0; 
                   6248:                 }
                   6249:                 return $access;
                   6250:             }
                   6251:         } else {
                   6252:             if ($domdef{$tool}{'default'} ne '') {
                   6253:                 if ($domdef{$tool}{'default'}) {
                   6254:                     $access = 1;
                   6255:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   6256:                     $access = 0;
                   6257:                 }
                   6258:                 return $access;
                   6259:             }
                   6260:         }
                   6261:     } else {
1.1177    raeburn  6262:         if (($context eq 'tools') && ($tool ne 'webdav')) {
1.985     raeburn  6263:             $access = 1;
                   6264:         } else {
                   6265:             $access = 0;
                   6266:         }
1.976     raeburn  6267:         return $access;
                   6268:     }
                   6269: }
                   6270: 
1.1050    raeburn  6271: sub is_course_owner {
                   6272:     my ($cdom,$cnum,$udom,$uname) = @_;
                   6273:     if (($udom eq '') || ($uname eq '')) {
                   6274:         $udom = $env{'user.domain'};
                   6275:         $uname = $env{'user.name'};
                   6276:     }
                   6277:     unless (($udom eq '') || ($uname eq '')) {
                   6278:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   6279:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   6280:                 return 1;
                   6281:             } else {
                   6282:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   6283:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   6284:                     return 1;
                   6285:                 }
                   6286:             }
                   6287:         }
                   6288:     }
                   6289:     return;
                   6290: }
                   6291: 
1.976     raeburn  6292: sub is_advanced_user {
                   6293:     my ($udom,$uname) = @_;
1.1085    raeburn  6294:     if ($udom ne '' && $uname ne '') {
                   6295:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  6296:             if (wantarray) {
                   6297:                 return ($env{'user.adv'},$env{'user.author'});
                   6298:             } else {
                   6299:                 return $env{'user.adv'};
                   6300:             }
1.1085    raeburn  6301:         }
                   6302:     }
1.976     raeburn  6303:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   6304:     my %allroles;
1.1128    raeburn  6305:     my ($is_adv,$is_author);
1.976     raeburn  6306:     foreach my $role (keys(%roleshash)) {
                   6307:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   6308:         my $area = '/'.$tdomain.'/'.$trest;
                   6309:         if ($sec ne '') {
                   6310:             $area .= '/'.$sec;
                   6311:         }
                   6312:         if (($area ne '') && ($trole ne '')) {
                   6313:             my $spec=$trole.'.'.$area;
                   6314:             if ($trole =~ /^cr\//) {
                   6315:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   6316:             } elsif ($trole ne 'gr') {
                   6317:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   6318:             }
1.1128    raeburn  6319:             if ($trole eq 'au') {
                   6320:                 $is_author = 1;
                   6321:             }
1.976     raeburn  6322:         }
                   6323:     }
                   6324:     foreach my $role (keys(%allroles)) {
                   6325:         last if ($is_adv);
                   6326:         foreach my $item (split(/:/,$allroles{$role})) {
                   6327:             if ($item ne '') {
                   6328:                 my ($privilege,$restrictions)=split(/&/,$item);
                   6329:                 if ($privilege eq 'adv') {
                   6330:                     $is_adv = 1;
                   6331:                     last;
                   6332:                 }
                   6333:             }
                   6334:         }
                   6335:     }
1.1128    raeburn  6336:     if (wantarray) {
                   6337:         return ($is_adv,$is_author);
                   6338:     }
1.976     raeburn  6339:     return $is_adv;
                   6340: }
1.798     raeburn  6341: 
1.1035    raeburn  6342: sub check_can_request {
1.1036    raeburn  6343:     my ($dom,$can_request,$request_domains) = @_;
1.1035    raeburn  6344:     my $canreq = 0;
                   6345:     my ($types,$typename) = &Apache::loncommon::course_types();
                   6346:     my @options = ('approval','validate','autolimit');
                   6347:     my $optregex = join('|',@options);
                   6348:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   6349:         foreach my $type (@{$types}) {
                   6350:             if (&usertools_access($env{'user.name'},
                   6351:                                   $env{'user.domain'},
                   6352:                                   $type,undef,'requestcourses')) {
                   6353:                 $canreq ++;
1.1036    raeburn  6354:                 if (ref($request_domains) eq 'HASH') {
                   6355:                     push(@{$request_domains->{$type}},$env{'user.domain'});
                   6356:                 }
1.1035    raeburn  6357:                 if ($dom eq $env{'user.domain'}) {
                   6358:                     $can_request->{$type} = 1;
                   6359:                 }
                   6360:             }
                   6361:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                   6362:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   6363:                 if (@curr > 0) {
1.1036    raeburn  6364:                     foreach my $item (@curr) {
                   6365:                         if (ref($request_domains) eq 'HASH') {
                   6366:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   6367:                             if ($otherdom ne '') {
                   6368:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   6369:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   6370:                                         push(@{$request_domains->{$type}},$otherdom);
                   6371:                                     }
                   6372:                                 } else {
                   6373:                                     push(@{$request_domains->{$type}},$otherdom);
                   6374:                                 }
                   6375:                             }
                   6376:                         }
                   6377:                     }
                   6378:                     unless($dom eq $env{'user.domain'}) {
                   6379:                         $canreq ++;
1.1035    raeburn  6380:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   6381:                             $can_request->{$type} = 1;
                   6382:                         }
                   6383:                     }
                   6384:                 }
                   6385:             }
                   6386:         }
                   6387:     }
                   6388:     return $canreq;
                   6389: }
                   6390: 
1.341     www      6391: # ---------------------------------------------- Custom access rule evaluation
                   6392: 
                   6393: sub customaccess {
                   6394:     my ($priv,$uri)=@_;
1.807     albertel 6395:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      6396:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 6397:     $udom = &LONCAPA::clean_domain($udom);
                   6398:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      6399:     my $access=0;
1.800     albertel 6400:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 6401: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   6402: 	if ($type eq 'user') {
                   6403: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 6404: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 6405: 		if ($tdom) {
                   6406: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   6407: 		}
1.896     albertel 6408: 		if ($tuname) {
                   6409: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 6410: 		}
                   6411: 		$access=($effect eq 'allow');
                   6412: 		last;
                   6413: 	    }
                   6414: 	} else {
                   6415: 	    if ($role) {
                   6416: 		if ($role ne $urole) { next; }
                   6417: 	    }
                   6418: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   6419: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   6420: 		if ($tdom) {
                   6421: 		    if ($tdom ne $udom) { next; }
                   6422: 		}
                   6423: 		if ($tcrs) {
                   6424: 		    if ($tcrs ne $ucrs) { next; }
                   6425: 		}
                   6426: 		if ($tsec) {
                   6427: 		    if ($tsec ne $usec) { next; }
                   6428: 		}
                   6429: 		$access=($effect eq 'allow');
                   6430: 		last;
                   6431: 	    }
                   6432: 	    if ($realm eq '' && $role eq '') {
                   6433: 		$access=($effect eq 'allow');
                   6434: 	    }
1.402     bowersj2 6435: 	}
1.341     www      6436:     }
                   6437:     return $access;
                   6438: }
                   6439: 
1.103     harris41 6440: # ------------------------------------------------- Check for a user privilege
1.12      www      6441: 
                   6442: sub allowed {
1.810     raeburn  6443:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 6444:     my $ver_orguri=$uri;
1.439     www      6445:     $uri=&deversion($uri);
1.152     www      6446:     my $orguri=$uri;
1.52      www      6447:     $uri=&declutter($uri);
1.809     raeburn  6448: 
1.810     raeburn  6449:     if ($priv eq 'evb') {
                   6450: # Evade communication block restrictions for specified role in a course
                   6451:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   6452:             return $1;
                   6453:         } else {
                   6454:             return;
                   6455:         }
                   6456:     }
                   6457: 
1.620     albertel 6458:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      6459: # Free bre access to adm and meta resources
1.775     albertel 6460:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 6461: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   6462: 	&& ($priv eq 'bre')) {
1.14      www      6463: 	return 'F';
1.159     www      6464:     }
                   6465: 
1.545     banghart 6466: # Free bre access to user's own portfolio contents
1.714     raeburn  6467:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  6468:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  6469: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  6470:         my %setters;
                   6471:         my ($startblock,$endblock) = 
                   6472:             &Apache::loncommon::blockcheck(\%setters,'port');
                   6473:         if ($startblock && $endblock) {
                   6474:             return 'B';
                   6475:         } else {
                   6476:             return 'F';
                   6477:         }
1.545     banghart 6478:     }
                   6479: 
1.762     raeburn  6480: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  6481:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   6482:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   6483:         if (exists($env{'request.course.id'})) {
                   6484:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6485:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6486:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   6487:                 my $courseprivid=$env{'request.course.id'};
                   6488:                 $courseprivid=~s/\_/\//;
                   6489:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   6490:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   6491:                     return $1; 
1.762     raeburn  6492:                 } else {
                   6493:                     if ($env{'request.course.sec'}) {
                   6494:                         $courseprivid.='/'.$env{'request.course.sec'};
                   6495:                     }
                   6496:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   6497:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   6498:                         return $2;
                   6499:                     }
1.714     raeburn  6500:                 }
                   6501:             }
                   6502:         }
                   6503:     }
                   6504: 
1.159     www      6505: # Free bre to public access
                   6506: 
                   6507:     if ($priv eq 'bre') {
1.238     www      6508:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 6509: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      6510:            return 'F'; 
                   6511:         }
1.238     www      6512:         if ($copyright eq 'priv') {
                   6513:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 6514: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      6515: 		return '';
                   6516:             }
                   6517:         }
                   6518:         if ($copyright eq 'domain') {
                   6519:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 6520: 	    unless (($env{'user.domain'} eq $1) ||
                   6521:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      6522: 		return '';
                   6523:             }
1.262     matthew  6524:         }
1.620     albertel 6525:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  6526:             # Library role, so allow browsing of resources in this domain.
                   6527:             return 'F';
1.238     www      6528:         }
1.341     www      6529:         if ($copyright eq 'custom') {
                   6530: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   6531:         }
1.14      www      6532:     }
1.264     matthew  6533:     # Domain coordinator is trying to create a course
1.620     albertel 6534:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  6535:         # uri is the requested domain in this case.
                   6536:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  6537:         # a role of dc for the domain in question.
1.620     albertel 6538:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  6539:     }
1.29      www      6540: 
1.52      www      6541:     my $thisallowed='';
                   6542:     my $statecond=0;
                   6543:     my $courseprivid='';
                   6544: 
1.1039    raeburn  6545:     my $ownaccess;
1.1043    raeburn  6546:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  6547:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   6548:         if ($uri eq '') {
                   6549:             $ownaccess = 1;
                   6550:         } else {
                   6551:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   6552:                 my $udom = $env{'user.domain'};
                   6553:                 my $uname = $env{'user.name'};
                   6554:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   6555:                     $ownaccess = 1;
1.1040    raeburn  6556:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  6557:                     unless ($uri =~ m{\.\./}) {
                   6558:                         $ownaccess = 1;
                   6559:                     }
                   6560:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   6561:                     my $now = time;
                   6562:                     if ($uri =~ m{^([^/]+)/?$}) {
                   6563:                         my $adom = $1;
                   6564:                         foreach my $key (keys(%env)) {
1.1042    raeburn  6565:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  6566:                                 my ($start,$end) = split('.',$env{$key});
                   6567:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   6568:                                     $ownaccess = 1;
                   6569:                                     last;
                   6570:                                 }
                   6571:                             }
                   6572:                         }
                   6573:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   6574:                         my $adom = $1;
                   6575:                         my $aname = $2;
1.1042    raeburn  6576:                         foreach my $role ('ca','aa') { 
                   6577:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   6578:                                 my ($start,$end) =
                   6579:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   6580:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   6581:                                     $ownaccess = 1;
                   6582:                                     last;
                   6583:                                 }
1.1039    raeburn  6584:                             }
                   6585:                         }
                   6586:                     }
                   6587:                 }
                   6588:             }
                   6589:         }
                   6590:     }
                   6591: 
1.52      www      6592: # Course
                   6593: 
1.620     albertel 6594:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  6595:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  6596:             $thisallowed.=$1;
                   6597:         }
1.52      www      6598:     }
1.29      www      6599: 
1.52      www      6600: # Domain
                   6601: 
1.620     albertel 6602:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 6603:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  6604:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  6605:             $thisallowed.=$1;
                   6606:         }
1.12      www      6607:     }
1.52      www      6608: 
1.1141    raeburn  6609: # User who is not author or co-author might still be able to edit
                   6610: # resource of an author in the domain (e.g., if Domain Coordinator).
                   6611:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   6612:         (&allowed('mdc',$env{'request.course.id'}))) {
                   6613:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   6614:             $thisallowed.=$1;
                   6615:         }
                   6616:     }
                   6617: 
1.52      www      6618: # Course: uri itself is a course
1.66      www      6619:     my $courseuri=$uri;
                   6620:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      6621:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      6622: 
1.620     albertel 6623:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 6624:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  6625:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  6626:             $thisallowed.=$1;
                   6627:         }
1.12      www      6628:     }
1.29      www      6629: 
1.665     albertel 6630: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 6631: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 6632:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 6633: 	$thisallowed='';
1.671     raeburn  6634:         my ($match)=&is_on_map($uri);
                   6635:         if ($match) {
                   6636:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   6637:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  6638:                 my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   6639:                 if (@blockers > 0) {
                   6640:                     $thisallowed = 'B';
                   6641:                 } else {
                   6642:                     $thisallowed.=$1;
                   6643:                 }
1.671     raeburn  6644:             }
                   6645:         } else {
1.705     albertel 6646:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  6647:             if ($refuri) {
                   6648:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  6649:                     $thisallowed='F';
1.671     raeburn  6650:                 } else {
                   6651:                     $refuri=&declutter($refuri);
                   6652:                     my ($match) = &is_on_map($refuri);
                   6653:                     if ($match) {
1.1162    raeburn  6654:                         my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   6655:                         if (@blockers > 0) {
                   6656:                             $thisallowed = 'B';
                   6657:                         } else {
                   6658:                             $thisallowed='F';
                   6659:                         }
1.671     raeburn  6660:                     }
1.669     raeburn  6661:                 }
1.671     raeburn  6662:             }
                   6663:         }
1.314     www      6664:     }
1.492     albertel 6665: 
1.766     albertel 6666:     if ($priv eq 'bre'
                   6667: 	&& $thisallowed ne 'F' 
                   6668: 	&& $thisallowed ne '2'
                   6669: 	&& &is_portfolio_url($uri)) {
                   6670: 	$thisallowed = &portfolio_access($uri);
                   6671:     }
                   6672:     
1.52      www      6673: # Full access at system, domain or course-wide level? Exit.
1.29      www      6674:     if ($thisallowed=~/F/) {
                   6675: 	return 'F';
                   6676:     }
                   6677: 
1.52      www      6678: # If this is generating or modifying users, exit with special codes
1.29      www      6679: 
1.643     www      6680:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   6681: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 6682: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      6683: # no author name given, so this just checks on the general right to make a co-author in this domain
                   6684: 	    unless ($auname) { return $thisallowed; }
                   6685: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 6686: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   6687: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   6688: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   6689: 	}
1.52      www      6690: 	return $thisallowed;
                   6691:     }
                   6692: #
1.103     harris41 6693: # Gathered so far: system, domain and course wide privileges
1.52      www      6694: #
                   6695: # Course: See if uri or referer is an individual resource that is part of 
                   6696: # the course
                   6697: 
1.620     albertel 6698:     if ($env{'request.course.id'}) {
1.232     www      6699: 
1.620     albertel 6700:        $courseprivid=$env{'request.course.id'};
                   6701:        if ($env{'request.course.sec'}) {
                   6702:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      6703:        }
                   6704:        $courseprivid=~s/\_/\//;
                   6705:        my $checkreferer=1;
1.232     www      6706:        my ($match,$cond)=&is_on_map($uri);
                   6707:        if ($match) {
                   6708:            $statecond=$cond;
1.620     albertel 6709:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6710:                =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  6711:                my $value = $1;
                   6712:                if ($priv eq 'bre') {
                   6713:                    my @blockers = &has_comm_blocking($priv,$symb,$uri);
                   6714:                    if (@blockers > 0) {
                   6715:                        $thisallowed = 'B';
                   6716:                    } else {
                   6717:                        $thisallowed.=$value;
                   6718:                    }
                   6719:                } else {
                   6720:                    $thisallowed.=$value;
                   6721:                }
1.52      www      6722:                $checkreferer=0;
                   6723:            }
1.29      www      6724:        }
1.83      www      6725:        
1.148     www      6726:        if ($checkreferer) {
1.620     albertel 6727: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      6728:             unless ($refuri) {
1.800     albertel 6729:                 foreach my $key (keys(%env)) {
                   6730: 		    if ($key=~/^httpref\..*\*/) {
                   6731: 			my $pattern=$key;
1.156     www      6732:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      6733:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   6734:                         $pattern=~s/\//\\\//g;
1.152     www      6735:                         if ($orguri=~/$pattern/) {
1.800     albertel 6736: 			    $refuri=$env{$key};
1.148     www      6737:                         }
                   6738:                     }
1.191     harris41 6739:                 }
1.148     www      6740:             }
1.232     www      6741: 
1.148     www      6742:          if ($refuri) { 
1.152     www      6743: 	  $refuri=&declutter($refuri);
1.232     www      6744:           my ($match,$cond)=&is_on_map($refuri);
                   6745:             if ($match) {
                   6746:               my $refstatecond=$cond;
1.620     albertel 6747:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6748:                   =~/\Q$priv\E\&([^\:]*)/) {
1.1162    raeburn  6749:                   my $value = $1;
                   6750:                   if ($priv eq 'bre') {
                   6751:                       my @blockers = &has_comm_blocking($priv,$symb,$refuri);
                   6752:                       if (@blockers > 0) {
                   6753:                           $thisallowed = 'B';
                   6754:                       } else {
                   6755:                           $thisallowed.=$value;
                   6756:                       }
                   6757:                   } else {
                   6758:                       $thisallowed.=$value;
                   6759:                   }
1.53      www      6760:                   $uri=$refuri;
                   6761:                   $statecond=$refstatecond;
1.52      www      6762:               }
                   6763:           }
1.148     www      6764:         }
1.29      www      6765:        }
1.52      www      6766:    }
1.29      www      6767: 
1.52      www      6768: #
1.103     harris41 6769: # Gathered now: all privileges that could apply, and condition number
1.52      www      6770: # 
                   6771: #
                   6772: # Full or no access?
                   6773: #
1.29      www      6774: 
1.52      www      6775:     if ($thisallowed=~/F/) {
                   6776: 	return 'F';
                   6777:     }
1.29      www      6778: 
1.52      www      6779:     unless ($thisallowed) {
                   6780:         return '';
                   6781:     }
1.29      www      6782: 
1.52      www      6783: # Restrictions exist, deal with them
                   6784: #
                   6785: #   C:according to course preferences
                   6786: #   R:according to resource settings
                   6787: #   L:unless locked
                   6788: #   X:according to user session state
                   6789: #
                   6790: 
                   6791: # Possibly locked functionality, check all courses
1.54      www      6792: # Locks might take effect only after 10 minutes cache expiration for other
                   6793: # courses, and 2 minutes for current course
1.52      www      6794: 
                   6795:     my $envkey;
                   6796:     if ($thisallowed=~/L/) {
1.1000    raeburn  6797:         foreach $envkey (keys(%env)) {
1.54      www      6798:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   6799:                my $courseid=$2;
                   6800:                my $roleid=$1.'.'.$2;
1.92      www      6801:                $courseid=~s/^\///;
1.54      www      6802:                my $expiretime=600;
1.620     albertel 6803:                if ($env{'request.role'} eq $roleid) {
1.54      www      6804: 		  $expiretime=120;
                   6805:                }
                   6806: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   6807:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 6808:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 6809: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      6810:                }
1.620     albertel 6811:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6812:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   6813: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   6814:                        &log($env{'user.domain'},$env{'user.name'},
                   6815:                             $env{'user.home'},
1.57      www      6816:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      6817:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6818:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6819: 		       return '';
                   6820:                    }
                   6821:                }
1.620     albertel 6822:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6823:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   6824: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   6825:                        &log($env{'user.domain'},$env{'user.name'},
                   6826:                             $env{'user.home'},
1.57      www      6827:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      6828:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6829:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6830: 		       return '';
                   6831:                    }
                   6832:                }
                   6833: 	   }
1.29      www      6834:        }
1.52      www      6835:     }
                   6836:    
                   6837: #
                   6838: # Rest of the restrictions depend on selected course
                   6839: #
                   6840: 
1.620     albertel 6841:     unless ($env{'request.course.id'}) {
1.766     albertel 6842: 	if ($thisallowed eq 'A') {
                   6843: 	    return 'A';
1.814     raeburn  6844:         } elsif ($thisallowed eq 'B') {
                   6845:             return 'B';
1.766     albertel 6846: 	} else {
                   6847: 	    return '1';
                   6848: 	}
1.52      www      6849:     }
1.29      www      6850: 
1.52      www      6851: #
                   6852: # Now user is definitely in a course
                   6853: #
1.53      www      6854: 
                   6855: 
                   6856: # Course preferences
                   6857: 
                   6858:    if ($thisallowed=~/C/) {
1.620     albertel 6859:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   6860:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   6861:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 6862: 	   =~/\Q$rolecode\E/) {
1.1103    raeburn  6863: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6864: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6865: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   6866: 			$env{'request.course.id'});
                   6867: 	   }
1.237     www      6868:            return '';
                   6869:        }
                   6870: 
1.620     albertel 6871:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 6872: 	   =~/\Q$unamedom\E/) {
1.1103    raeburn  6873: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6874: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   6875: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   6876: 			$env{'request.course.id'});
                   6877: 	   }
1.54      www      6878:            return '';
                   6879:        }
1.53      www      6880:    }
                   6881: 
                   6882: # Resource preferences
                   6883: 
                   6884:    if ($thisallowed=~/R/) {
1.620     albertel 6885:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 6886:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  6887: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6888: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6889: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   6890: 	   }
                   6891: 	   return '';
1.54      www      6892:        }
1.53      www      6893:    }
1.30      www      6894: 
1.246     www      6895: # Restricted by state or randomout?
1.30      www      6896: 
1.52      www      6897:    if ($thisallowed=~/X/) {
1.620     albertel 6898:       if ($env{'acc.randomout'}) {
1.579     albertel 6899: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 6900:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      6901:             return ''; 
                   6902:          }
1.247     www      6903:       }
                   6904:       if (&condval($statecond)) {
1.52      www      6905: 	 return '2';
                   6906:       } else {
                   6907:          return '';
                   6908:       }
                   6909:    }
1.30      www      6910: 
1.766     albertel 6911:     if ($thisallowed eq 'A') {
                   6912: 	return 'A';
1.814     raeburn  6913:     } elsif ($thisallowed eq 'B') {
                   6914:         return 'B';
1.766     albertel 6915:     }
1.52      www      6916:    return 'F';
1.232     www      6917: }
1.1162    raeburn  6918: 
1.1192    raeburn  6919: # ------------------------------------------- Check construction space access
                   6920: 
                   6921: sub constructaccess {
                   6922:     my ($url,$setpriv)=@_;
                   6923: 
                   6924: # We do not allow editing of previous versions of files
                   6925:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
                   6926: 
                   6927: # Get username and domain from URL
                   6928:     my ($ownername,$ownerdomain,$ownerhome);
                   6929: 
                   6930:     ($ownerdomain,$ownername) =
                   6931:         ($url=~ m{^(?:\Q$perlvar{'lonDocRoot'}\E|)/priv/($match_domain)/($match_username)/});
                   6932: 
                   6933: # The URL does not really point to any authorspace, forget it
                   6934:     unless (($ownername) && ($ownerdomain)) { return ''; }
                   6935: 
                   6936: # Now we need to see if the user has access to the authorspace of
                   6937: # $ownername at $ownerdomain
                   6938: 
                   6939:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
                   6940: # Real author for this?
                   6941:        $ownerhome = $env{'user.home'};
                   6942:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
                   6943:           return ($ownername,$ownerdomain,$ownerhome);
                   6944:        }
                   6945:     } else {
                   6946: # Co-author for this?
                   6947:         if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
                   6948:             exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
                   6949:             $ownerhome = &homeserver($ownername,$ownerdomain);
                   6950:             return ($ownername,$ownerdomain,$ownerhome);
                   6951:         }
                   6952:     }
                   6953: 
                   6954: # We don't have any access right now. If we are not possibly going to do anything about this,
                   6955: # we might as well leave
                   6956:    unless ($setpriv) { return ''; }
                   6957: 
                   6958: # Backdoor access?
                   6959:     my $allowed=&allowed('eco',$ownerdomain);
                   6960: # Nope
                   6961:     unless ($allowed) { return ''; }
                   6962: # Looks like we may have access, but could be locked by the owner of the construction space
                   6963:     if ($allowed eq 'U') {
                   6964:         my %blocked=&get('environment',['domcoord.author'],
                   6965:                          $ownerdomain,$ownername);
                   6966: # Is blocked by owner
                   6967:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
                   6968:     }
                   6969:     if (($allowed eq 'F') || ($allowed eq 'U')) {
                   6970: # Grant temporary access
                   6971:         my $then=$env{'user.login.time'};
1.1209    raeburn  6972:         my $update=$env{'user.update.time'};
1.1192    raeburn  6973:         if (!$update) { $update = $then; }
                   6974:         my $refresh=$env{'user.refresh.time'};
                   6975:         if (!$refresh) { $refresh = $update; }
                   6976:         my $now = time;
                   6977:         &check_adhoc_privs($ownerdomain,$ownername,$update,$refresh,
                   6978:                            $now,'ca','constructaccess');
                   6979:         $ownerhome = &homeserver($ownername,$ownerdomain);
                   6980:         return($ownername,$ownerdomain,$ownerhome);
                   6981:     }
                   6982: # No business here
                   6983:     return '';
                   6984: }
                   6985: 
1.1162    raeburn  6986: sub get_comm_blocks {
                   6987:     my ($cdom,$cnum) = @_;
                   6988:     if ($cdom eq '' || $cnum eq '') {
                   6989:         return unless ($env{'request.course.id'});
                   6990:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   6991:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   6992:     }
                   6993:     my %commblocks;
                   6994:     my $hashid=$cdom.'_'.$cnum;
                   6995:     my ($blocksref,$cached)=&is_cached_new('comm_block',$hashid);
                   6996:     if ((defined($cached)) && (ref($blocksref) eq 'HASH')) {
                   6997:         %commblocks = %{$blocksref};
                   6998:     } else {
                   6999:         %commblocks = &Apache::lonnet::dump('comm_block',$cdom,$cnum);
                   7000:         my $cachetime = 600;
                   7001:         &do_cache_new('comm_block',$hashid,\%commblocks,$cachetime);
                   7002:     }
                   7003:     return %commblocks;
                   7004: }
                   7005: 
                   7006: sub has_comm_blocking {
                   7007:     my ($priv,$symb,$uri,$blocks) = @_;
                   7008:     return unless ($env{'request.course.id'});
                   7009:     return unless ($priv eq 'bre');
                   7010:     return if ($env{'user.priv.'.$env{'request.role'}} =~/evb\&([^\:]*)/);
                   7011:     my %commblocks;
                   7012:     if (ref($blocks) eq 'HASH') {
                   7013:         %commblocks = %{$blocks};
                   7014:     } else {
                   7015:         %commblocks = &get_comm_blocks();
                   7016:     }
                   7017:     return unless (keys(%commblocks) > 0);
                   7018:     if (!$symb) { $symb=&symbread($uri,1); }
                   7019:     my ($map,$resid,undef)=&decode_symb($symb);
                   7020:     my %tocheck = (
                   7021:                     maps      => $map,
                   7022:                     resources => $symb,
                   7023:                   );
                   7024:     my @blockers;
                   7025:     my $now = time;
1.1163    raeburn  7026:     my $navmap = Apache::lonnavmaps::navmap->new();
1.1162    raeburn  7027:     foreach my $block (keys(%commblocks)) {
                   7028:         if ($block =~ /^(\d+)____(\d+)$/) {
                   7029:             my ($start,$end) = ($1,$2);
                   7030:             if ($start <= $now && $end >= $now) {
                   7031:                 if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   7032:                     if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   7033:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'maps'}) eq 'HASH') {
                   7034:                             if ($commblocks{$block}{'blocks'}{'docs'}{'maps'}{$map}) {
                   7035:                                 unless (grep(/^\Q$block\E$/,@blockers)) {
                   7036:                                     push(@blockers,$block);
                   7037:                                 }
                   7038:                             }
                   7039:                         }
                   7040:                         if (ref($commblocks{$block}{'blocks'}{'docs'}{'resources'}) eq 'HASH') {
                   7041:                             if ($commblocks{$block}{'blocks'}{'docs'}{'resources'}{$symb}) {
                   7042:                                 unless (grep(/^\Q$block\E$/,@blockers)) {  
                   7043:                                     push(@blockers,$block);
                   7044:                                 }
                   7045:                             }
                   7046:                         }
                   7047:                     }
                   7048:                 }
                   7049:             }
                   7050:         } elsif ($block =~ /^firstaccess____(.+)$/) {
                   7051:             my $item = $1;
1.1163    raeburn  7052:             my @to_test;
1.1162    raeburn  7053:             if (ref($commblocks{$block}{'blocks'}) eq 'HASH') {
                   7054:                 if (ref($commblocks{$block}{'blocks'}{'docs'}) eq 'HASH') {
                   7055:                     my $check_interval;
                   7056:                     if (&check_docs_block($commblocks{$block}{'blocks'}{'docs'},\%tocheck)) {
                   7057:                         my @interval;
                   7058:                         my $type = 'map';
                   7059:                         if ($item eq 'course') {
                   7060:                             $type = 'course';
                   7061:                             @interval=&EXT("resource.0.interval");
                   7062:                         } else {
                   7063:                             if ($item =~ /___\d+___/) {
                   7064:                                 $type = 'resource';
                   7065:                                 @interval=&EXT("resource.0.interval",$item);
1.1163    raeburn  7066:                                 if (ref($navmap)) {                        
                   7067:                                     my $res = $navmap->getBySymb($item); 
                   7068:                                     push(@to_test,$res);
                   7069:                                 }
1.1162    raeburn  7070:                             } else {
                   7071:                                 my $mapsymb = &symbread($item,1);
                   7072:                                 if ($mapsymb) {
                   7073:                                     if (ref($navmap)) {
                   7074:                                         my $mapres = $navmap->getBySymb($mapsymb);
1.1163    raeburn  7075:                                         @to_test = $mapres->retrieveResources($mapres,undef,0,1);
                   7076:                                         foreach my $res (@to_test) {
1.1162    raeburn  7077:                                             my $symb = $res->symb();
                   7078:                                             next if ($symb eq $mapsymb);
                   7079:                                             if ($symb ne '') {
                   7080:                                                 @interval=&EXT("resource.0.interval",$symb);
                   7081:                                                 last;
                   7082:                                             }
                   7083:                                         }
                   7084:                                     }
                   7085:                                 }
                   7086:                             }
                   7087:                         }
                   7088:                         if ($interval[0] =~ /\d+/) {
                   7089:                             my $first_access;
                   7090:                             if ($type eq 'resource') {
                   7091:                                 $first_access=&get_first_access($interval[1],$item);
                   7092:                             } elsif ($type eq 'map') {
                   7093:                                 $first_access=&get_first_access($interval[1],undef,$item);
                   7094:                             } else {
                   7095:                                 $first_access=&get_first_access($interval[1]);
                   7096:                             }
                   7097:                             if ($first_access) {
                   7098:                                 my $timesup = $first_access+$interval[0];
                   7099:                                 if ($timesup > $now) {
1.1163    raeburn  7100:                                     foreach my $res (@to_test) {
                   7101:                                         if ($res->is_problem()) {
                   7102:                                             if ($res->completable()) {
                   7103:                                                 unless (grep(/^\Q$block\E$/,@blockers)) {
                   7104:                                                     push(@blockers,$block);
                   7105:                                                 }
                   7106:                                                 last;
                   7107:                                             }
                   7108:                                         }
1.1162    raeburn  7109:                                     }
                   7110:                                 }
                   7111:                             }
                   7112:                         }
                   7113:                     }
                   7114:                 }
                   7115:             }
                   7116:         }
                   7117:     }
                   7118:     return @blockers;
                   7119: }
                   7120: 
                   7121: sub check_docs_block {
                   7122:     my ($docsblock,$tocheck) =@_;
                   7123:     if ((ref($docsblock) ne 'HASH') || (ref($tocheck) ne 'HASH')) {
                   7124:         return;
                   7125:     }
                   7126:     if (ref($docsblock->{'maps'}) eq 'HASH') {
                   7127:         if ($tocheck->{'maps'}) {
                   7128:             if ($docsblock->{'maps'}{$tocheck->{'maps'}}) {
                   7129:                 return 1;
                   7130:             }
                   7131:         }
                   7132:     }
                   7133:     if (ref($docsblock->{'resources'}) eq 'HASH') {
                   7134:         if ($tocheck->{'resources'}) {
                   7135:             if ($docsblock->{'resources'}{$tocheck->{'resources'}}) {
                   7136:                 return 1;
                   7137:             }
                   7138:         }
                   7139:     }
                   7140:     return;
                   7141: }
                   7142: 
1.1133    foxr     7143: #
                   7144: #   Removes the versino from a URI and
                   7145: #   splits it in to its filename and path to the filename.
                   7146: #   Seems like File::Basename could have done this more clearly.
                   7147: #   Parameters:
                   7148: #      $uri   - input URI
                   7149: #   Returns:
                   7150: #     Two element list consisting of 
                   7151: #     $pathname  - the URI up to and excluding the trailing /
                   7152: #     $filename  - The part of the URI following the last /
                   7153: #  NOTE:
                   7154: #    Another realization of this is simply:
                   7155: #    use File::Basename;
                   7156: #    ...
                   7157: #    $uri = shift;
                   7158: #    $filename = basename($uri);
                   7159: #    $path     = dirname($uri);
                   7160: #    return ($filename, $path);
                   7161: #
                   7162: #     The implementation below is probably faster however.
                   7163: #
1.710     albertel 7164: sub split_uri_for_cond {
                   7165:     my $uri=&deversion(&declutter(shift));
                   7166:     my @uriparts=split(/\//,$uri);
                   7167:     my $filename=pop(@uriparts);
                   7168:     my $pathname=join('/',@uriparts);
                   7169:     return ($pathname,$filename);
                   7170: }
1.232     www      7171: # --------------------------------------------------- Is a resource on the map?
                   7172: 
                   7173: sub is_on_map {
1.710     albertel 7174:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 7175:     #Trying to find the conditional for the file
1.620     albertel 7176:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 7177: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      7178:     if ($match) {
1.289     bowersj2 7179: 	return (1,$1);
                   7180:     } else {
1.434     www      7181: 	return (0,0);
1.289     bowersj2 7182:     }
1.12      www      7183: }
                   7184: 
1.427     www      7185: # --------------------------------------------------------- Get symb from alias
                   7186: 
                   7187: sub get_symb_from_alias {
                   7188:     my $symb=shift;
                   7189:     my ($map,$resid,$url)=&decode_symb($symb);
                   7190: # Already is a symb
                   7191:     if ($url) { return $symb; }
                   7192: # Must be an alias
                   7193:     my $aliassymb='';
                   7194:     my %bighash;
1.620     albertel 7195:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      7196:                             &GDBM_READER(),0640)) {
                   7197:         my $rid=$bighash{'mapalias_'.$symb};
                   7198: 	if ($rid) {
                   7199: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 7200: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   7201: 				    $resid,$bighash{'src_'.$rid});
1.427     www      7202: 	}
                   7203:         untie %bighash;
                   7204:     }
                   7205:     return $aliassymb;
                   7206: }
                   7207: 
1.12      www      7208: # ----------------------------------------------------------------- Define Role
                   7209: 
                   7210: sub definerole {
                   7211:   if (allowed('mcr','/')) {
                   7212:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 7213:     foreach my $role (split(':',$sysrole)) {
                   7214: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 7215:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   7216:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   7217: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      7218:                return "refused:s:$crole&$cqual"; 
                   7219:             }
                   7220:         }
1.191     harris41 7221:     }
1.800     albertel 7222:     foreach my $role (split(':',$domrole)) {
                   7223: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 7224:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   7225:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   7226: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      7227:                return "refused:d:$crole&$cqual"; 
                   7228:             }
                   7229:         }
1.191     harris41 7230:     }
1.800     albertel 7231:     foreach my $role (split(':',$courole)) {
                   7232: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 7233:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   7234:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   7235: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      7236:                return "refused:c:$crole&$cqual"; 
                   7237:             }
                   7238:         }
1.191     harris41 7239:     }
1.620     albertel 7240:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   7241:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      7242: 	        "rolesdef_$rolename=".
                   7243:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 7244:     return reply($command,$env{'user.home'});
1.12      www      7245:   } else {
                   7246:     return 'refused';
                   7247:   }
1.105     harris41 7248: }
                   7249: 
                   7250: # ---------------- Make a metadata query against the network of library servers
                   7251: 
                   7252: sub metadata_query {
1.244     matthew  7253:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 7254:     my %rhash;
1.845     albertel 7255:     my %libserv = &all_library();
1.244     matthew  7256:     my @server_list = (defined($server_array) ? @$server_array
                   7257:                                               : keys(%libserv) );
                   7258:     for my $server (@server_list) {
1.118     harris41 7259: 	unless ($custom or $customshow) {
                   7260: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   7261: 	    $rhash{$server}=$reply;
                   7262: 	}
                   7263: 	else {
                   7264: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   7265: 			     &escape($custom).':'.&escape($customshow),
                   7266: 			     $server);
                   7267: 	    $rhash{$server}=$reply;
                   7268: 	}
1.112     harris41 7269:     }
1.118     harris41 7270:     return \%rhash;
1.240     www      7271: }
                   7272: 
                   7273: # ----------------------------------------- Send log queries and wait for reply
                   7274: 
                   7275: sub log_query {
                   7276:     my ($uname,$udom,$query,%filters)=@_;
                   7277:     my $uhome=&homeserver($uname,$udom);
                   7278:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 7279:     my $uhost=&hostname($uhome);
1.800     albertel 7280:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      7281:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   7282:                        $uhome);
1.479     albertel 7283:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      7284:     return get_query_reply($queryid);
                   7285: }
                   7286: 
1.818     raeburn  7287: # -------------------------- Update MySQL table for portfolio file
                   7288: 
                   7289: sub update_portfolio_table {
1.821     raeburn  7290:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  7291:     if ($group ne '') {
                   7292:         $file_name =~s /^\Q$group\E//;
                   7293:     }
1.818     raeburn  7294:     my $homeserver = &homeserver($uname,$udom);
                   7295:     my $queryid=
1.821     raeburn  7296:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   7297:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  7298:     my $reply = &get_query_reply($queryid);
                   7299:     return $reply;
                   7300: }
                   7301: 
1.899     raeburn  7302: # -------------------------- Update MySQL allusers table
                   7303: 
                   7304: sub update_allusers_table {
                   7305:     my ($uname,$udom,$names) = @_;
                   7306:     my $homeserver = &homeserver($uname,$udom);
                   7307:     my $queryid=
                   7308:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   7309:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   7310:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   7311:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   7312:                'generation='.&escape($names->{'generation'}).'%%'.
                   7313:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   7314:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  7315:     return;
1.899     raeburn  7316: }
                   7317: 
1.508     raeburn  7318: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  7319: 
                   7320: sub fetch_enrollment_query {
1.511     raeburn  7321:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  7322:     my $homeserver;
1.547     raeburn  7323:     my $maxtries = 1;
1.508     raeburn  7324:     if ($context eq 'automated') {
                   7325:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  7326:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  7327:     } else {
                   7328:         $homeserver = &homeserver($cnum,$dom);
                   7329:     }
1.838     albertel 7330:     my $host=&hostname($homeserver);
1.506     raeburn  7331:     my $cmd = '';
1.1000    raeburn  7332:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 7333:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  7334:     }
                   7335:     $cmd =~ s/%%$//;
                   7336:     $cmd = &escape($cmd);
                   7337:     my $query = 'fetchenrollment';
1.620     albertel 7338:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  7339:     unless ($queryid=~/^\Q$host\E\_/) { 
                   7340:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   7341:         return 'error: '.$queryid;
                   7342:     }
1.506     raeburn  7343:     my $reply = &get_query_reply($queryid);
1.547     raeburn  7344:     my $tries = 1;
                   7345:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   7346:         $reply = &get_query_reply($queryid);
                   7347:         $tries ++;
                   7348:     }
1.526     raeburn  7349:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 7350:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  7351:     } else {
1.901     albertel 7352:         my @responses = split(/:/,$reply);
1.515     raeburn  7353:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 7354:             foreach my $line (@responses) {
                   7355:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  7356:                 $$replyref{$key} = $value;
                   7357:             }
                   7358:         } else {
1.1117    foxr     7359:             my $pathname = LONCAPA::tempdir();
1.800     albertel 7360:             foreach my $line (@responses) {
                   7361:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  7362:                 $$replyref{$key} = $value;
                   7363:                 if ($value > 0) {
1.800     albertel 7364:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   7365:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  7366:                         my $destname = $pathname.'/'.$filename;
                   7367:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  7368:                         if ($xml_classlist =~ /^error/) {
                   7369:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   7370:                         } else {
1.506     raeburn  7371:                             if ( open(FILE,">$destname") ) {
                   7372:                                 print FILE &unescape($xml_classlist);
                   7373:                                 close(FILE);
1.526     raeburn  7374:                             } else {
                   7375:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  7376:                             }
                   7377:                         }
                   7378:                     }
                   7379:                 }
                   7380:             }
                   7381:         }
                   7382:         return 'ok';
                   7383:     }
                   7384:     return 'error';
                   7385: }
                   7386: 
1.242     www      7387: sub get_query_reply {
                   7388:     my $queryid=shift;
1.1117    foxr     7389:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      7390:     my $reply='';
                   7391:     for (1..100) {
                   7392: 	sleep 2;
                   7393:         if (-e $replyfile.'.end') {
1.448     albertel 7394: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 7395: 		$reply = join('',<$fh>);
                   7396: 		close($fh);
1.240     www      7397: 	   } else { return 'error: reply_file_error'; }
1.242     www      7398:            return &unescape($reply);
                   7399: 	}
1.240     www      7400:     }
1.242     www      7401:     return 'timeout:'.$queryid;
1.240     www      7402: }
                   7403: 
                   7404: sub courselog_query {
1.241     www      7405: #
                   7406: # possible filters:
                   7407: # url: url or symb
                   7408: # username
                   7409: # domain
                   7410: # action: view, submit, grade
                   7411: # start: timestamp
                   7412: # end: timestamp
                   7413: #
1.240     www      7414:     my (%filters)=@_;
1.620     albertel 7415:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      7416:     if ($filters{'url'}) {
                   7417: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   7418:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   7419:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   7420:     }
1.620     albertel 7421:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   7422:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      7423:     return &log_query($cname,$cdom,'courselog',%filters);
                   7424: }
                   7425: 
                   7426: sub userlog_query {
1.858     raeburn  7427: #
                   7428: # possible filters:
                   7429: # action: log check role
                   7430: # start: timestamp
                   7431: # end: timestamp
                   7432: #
1.240     www      7433:     my ($uname,$udom,%filters)=@_;
                   7434:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      7435: }
                   7436: 
1.506     raeburn  7437: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   7438: 
                   7439: sub auto_run {
1.508     raeburn  7440:     my ($cnum,$cdom) = @_;
1.876     raeburn  7441:     my $response = 0;
                   7442:     my $settings;
                   7443:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   7444:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   7445:         $settings = $domconfig{'autoenroll'};
                   7446:         if ($settings->{'run'} eq '1') {
                   7447:             $response = 1;
                   7448:         }
                   7449:     } else {
1.934     raeburn  7450:         my $homeserver;
                   7451:         if (&is_course($cdom,$cnum)) {
                   7452:             $homeserver = &homeserver($cnum,$cdom);
                   7453:         } else {
                   7454:             $homeserver = &domain($cdom,'primary');
                   7455:         }
                   7456:         if ($homeserver ne 'no_host') {
                   7457:             $response = &reply('autorun:'.$cdom,$homeserver);
                   7458:         }
1.876     raeburn  7459:     }
1.506     raeburn  7460:     return $response;
                   7461: }
1.776     albertel 7462: 
1.506     raeburn  7463: sub auto_get_sections {
1.508     raeburn  7464:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  7465:     my $homeserver;
                   7466:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   7467:         $homeserver = &homeserver($cnum,$cdom);
                   7468:     }
                   7469:     if (!defined($homeserver)) { 
                   7470:         if ($cdom =~ /^$match_domain$/) {
                   7471:             $homeserver = &domain($cdom,'primary');
                   7472:         }
                   7473:     }
                   7474:     my @secs;
                   7475:     if (defined($homeserver)) {
                   7476:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   7477:         unless ($response eq 'refused') {
                   7478:             @secs = split(/:/,$response);
                   7479:         }
1.506     raeburn  7480:     }
                   7481:     return @secs;
                   7482: }
1.776     albertel 7483: 
1.506     raeburn  7484: sub auto_new_course {
1.1099    raeburn  7485:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  7486:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  7487:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  7488:     return $response;
                   7489: }
1.776     albertel 7490: 
1.506     raeburn  7491: sub auto_validate_courseID {
1.508     raeburn  7492:     my ($cnum,$cdom,$inst_course_id) = @_;
                   7493:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  7494:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  7495:     return $response;
                   7496: }
1.776     albertel 7497: 
1.1007    raeburn  7498: sub auto_validate_instcode {
1.1020    raeburn  7499:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  7500:     my ($homeserver,$response);
                   7501:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   7502:         $homeserver = &homeserver($cnum,$cdom);
                   7503:     }
                   7504:     if (!defined($homeserver)) {
                   7505:         if ($cdom =~ /^$match_domain$/) {
                   7506:             $homeserver = &domain($cdom,'primary');
                   7507:         }
                   7508:     }
1.1065    raeburn  7509:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   7510:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1216    raeburn  7511:     my ($outcome,$description,$defaultcredits) = map { &unescape($_); } split('&',$response,3);
                   7512:     return ($outcome,$description,$defaultcredits);
1.1007    raeburn  7513: }
                   7514: 
1.506     raeburn  7515: sub auto_create_password {
1.873     raeburn  7516:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   7517:     my ($homeserver,$response);
1.506     raeburn  7518:     my $create_passwd = 0;
                   7519:     my $authchk = '';
1.873     raeburn  7520:     if ($udom =~ /^$match_domain$/) {
                   7521:         $homeserver = &domain($udom,'primary');
                   7522:     }
                   7523:     if ($homeserver eq '') {
                   7524:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   7525:             $homeserver = &homeserver($cnum,$cdom);
                   7526:         }
                   7527:     }
                   7528:     if ($homeserver eq '') {
                   7529:         $authchk = 'nodomain';
1.506     raeburn  7530:     } else {
1.873     raeburn  7531:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   7532:         if ($response eq 'refused') {
                   7533:             $authchk = 'refused';
                   7534:         } else {
1.901     albertel 7535:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  7536:         }
1.506     raeburn  7537:     }
                   7538:     return ($authparam,$create_passwd,$authchk);
                   7539: }
                   7540: 
1.706     raeburn  7541: sub auto_photo_permission {
                   7542:     my ($cnum,$cdom,$students) = @_;
                   7543:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 7544:     my ($outcome,$perm_reqd,$conditions) = 
                   7545: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 7546:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   7547: 	return (undef,undef);
                   7548:     }
1.706     raeburn  7549:     return ($outcome,$perm_reqd,$conditions);
                   7550: }
                   7551: 
                   7552: sub auto_checkphotos {
                   7553:     my ($uname,$udom,$pid) = @_;
                   7554:     my $homeserver = &homeserver($uname,$udom);
                   7555:     my ($result,$resulttype);
                   7556:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 7557: 				   &escape($uname).':'.&escape($pid),
                   7558: 				   $homeserver));
1.709     albertel 7559:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   7560: 	return (undef,undef);
                   7561:     }
1.706     raeburn  7562:     if ($outcome) {
                   7563:         ($result,$resulttype) = split(/:/,$outcome);
                   7564:     } 
                   7565:     return ($result,$resulttype);
                   7566: }
                   7567: 
                   7568: sub auto_photochoice {
                   7569:     my ($cnum,$cdom) = @_;
                   7570:     my $homeserver = &homeserver($cnum,$cdom);
                   7571:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 7572: 						       &escape($cdom),
                   7573: 						       $homeserver)));
1.709     albertel 7574:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   7575: 	return (undef,undef);
                   7576:     }
1.706     raeburn  7577:     return ($update,$comment);
                   7578: }
                   7579: 
                   7580: sub auto_photoupdate {
                   7581:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   7582:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 7583:     my $host=&hostname($homeserver);
1.706     raeburn  7584:     my $cmd = '';
                   7585:     my $maxtries = 1;
1.800     albertel 7586:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   7587:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  7588:     }
                   7589:     $cmd =~ s/%%$//;
                   7590:     $cmd = &escape($cmd);
                   7591:     my $query = 'institutionalphotos';
                   7592:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   7593:     unless ($queryid=~/^\Q$host\E\_/) {
                   7594:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   7595:         return 'error: '.$queryid;
                   7596:     }
                   7597:     my $reply = &get_query_reply($queryid);
                   7598:     my $tries = 1;
                   7599:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   7600:         $reply = &get_query_reply($queryid);
                   7601:         $tries ++;
                   7602:     }
                   7603:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   7604:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   7605:     } else {
                   7606:         my @responses = split(/:/,$reply);
                   7607:         my $outcome = shift(@responses); 
                   7608:         foreach my $item (@responses) {
                   7609:             my ($key,$value) = split(/=/,$item);
                   7610:             $$photo{$key} = $value;
                   7611:         }
                   7612:         return $outcome;
                   7613:     }
                   7614:     return 'error';
                   7615: }
                   7616: 
1.521     raeburn  7617: sub auto_instcode_format {
1.793     albertel 7618:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   7619: 	$cat_order) = @_;
1.521     raeburn  7620:     my $courses = '';
1.772     raeburn  7621:     my @homeservers;
1.521     raeburn  7622:     if ($caller eq 'global') {
1.841     albertel 7623: 	my %servers = &get_servers($codedom,'library');
                   7624: 	foreach my $tryserver (keys(%servers)) {
                   7625: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   7626: 		push(@homeservers,$tryserver);
                   7627: 	    }
1.584     raeburn  7628:         }
1.1022    raeburn  7629:     } elsif ($caller eq 'requests') {
                   7630:         if ($codedom =~ /^$match_domain$/) {
                   7631:             my $chome = &domain($codedom,'primary');
                   7632:             unless ($chome eq 'no_host') {
                   7633:                 push(@homeservers,$chome);
                   7634:             }
                   7635:         }
1.521     raeburn  7636:     } else {
1.772     raeburn  7637:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  7638:     }
1.793     albertel 7639:     foreach my $code (keys(%{$instcodes})) {
                   7640:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  7641:     }
                   7642:     chop($courses);
1.772     raeburn  7643:     my $ok_response = 0;
                   7644:     my $response;
                   7645:     while (@homeservers > 0 && $ok_response == 0) {
                   7646:         my $server = shift(@homeservers); 
                   7647:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   7648:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   7649:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 7650: 		split(/:/,$response);
1.772     raeburn  7651:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   7652:             push(@{$codetitles},&str2array($codetitles_str));
                   7653:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   7654:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   7655:             $ok_response = 1;
                   7656:         }
                   7657:     }
                   7658:     if ($ok_response) {
1.521     raeburn  7659:         return 'ok';
1.772     raeburn  7660:     } else {
                   7661:         return $response;
1.521     raeburn  7662:     }
                   7663: }
                   7664: 
1.792     raeburn  7665: sub auto_instcode_defaults {
                   7666:     my ($domain,$returnhash,$code_order) = @_;
                   7667:     my @homeservers;
1.841     albertel 7668: 
                   7669:     my %servers = &get_servers($domain,'library');
                   7670:     foreach my $tryserver (keys(%servers)) {
                   7671: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   7672: 	    push(@homeservers,$tryserver);
                   7673: 	}
1.792     raeburn  7674:     }
1.841     albertel 7675: 
1.792     raeburn  7676:     my $response;
1.841     albertel 7677:     foreach my $server (@homeservers) {
1.792     raeburn  7678:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 7679:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   7680: 	
                   7681: 	foreach my $pair (split(/\&/,$response)) {
                   7682: 	    my ($name,$value)=split(/\=/,$pair);
                   7683: 	    if ($name eq 'code_order') {
                   7684: 		@{$code_order} = split(/\&/,&unescape($value));
                   7685: 	    } else {
                   7686: 		$returnhash->{&unescape($name)}=&unescape($value);
                   7687: 	    }
                   7688: 	}
                   7689: 	return 'ok';
1.792     raeburn  7690:     }
1.841     albertel 7691: 
                   7692:     return $response;
1.1003    raeburn  7693: }
                   7694: 
                   7695: sub auto_possible_instcodes {
1.1007    raeburn  7696:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   7697:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   7698:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   7699:         return;
                   7700:     }
1.1003    raeburn  7701:     my (@homeservers,$uhome);
                   7702:     if (defined(&domain($domain,'primary'))) {
                   7703:         $uhome=&domain($domain,'primary');
                   7704:         push(@homeservers,&domain($domain,'primary'));
                   7705:     } else {
                   7706:         my %servers = &get_servers($domain,'library');
                   7707:         foreach my $tryserver (keys(%servers)) {
                   7708:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   7709:                 push(@homeservers,$tryserver);
                   7710:             }
                   7711:         }
                   7712:     }
                   7713:     my $response;
                   7714:     foreach my $server (@homeservers) {
                   7715:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   7716:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  7717:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   7718:             split(':',$response);
                   7719:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   7720:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  7721:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  7722:             my ($name,$value)=split('=',$item);
                   7723:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  7724:         }
                   7725:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  7726:             my ($name,$value)=split('=',$item);
                   7727:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  7728:         }
                   7729:         return 'ok';
                   7730:     }
                   7731:     return $response;
                   7732: }
1.792     raeburn  7733: 
1.1010    raeburn  7734: sub auto_courserequest_checks {
                   7735:     my ($dom) = @_;
1.1020    raeburn  7736:     my ($homeserver,%validations);
                   7737:     if ($dom =~ /^$match_domain$/) {
                   7738:         $homeserver = &domain($dom,'primary');
                   7739:     }
                   7740:     unless ($homeserver eq 'no_host') {
                   7741:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   7742:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   7743:             my @items = split(/&/,$response);
                   7744:             foreach my $item (@items) {
                   7745:                 my ($key,$value) = split('=',$item);
                   7746:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   7747:             }
                   7748:         }
                   7749:     }
1.1010    raeburn  7750:     return %validations; 
                   7751: }
                   7752: 
1.1020    raeburn  7753: sub auto_courserequest_validation {
                   7754:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
                   7755:     my ($homeserver,$response);
                   7756:     if ($dom =~ /^$match_domain$/) {
                   7757:         $homeserver = &domain($dom,'primary');
                   7758:     }
                   7759:     unless ($homeserver eq 'no_host') {  
1.1021    raeburn  7760:           
1.1020    raeburn  7761:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  7762:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020    raeburn  7763:                                     ':'.&escape($instcode).':'.&escape($instseclist),
                   7764:                                     $homeserver));
                   7765:     }
                   7766:     return $response;
                   7767: }
                   7768: 
1.777     albertel 7769: sub auto_validate_class_sec {
1.918     raeburn  7770:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  7771:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  7772:     my $ownerlist;
                   7773:     if (ref($owners) eq 'ARRAY') {
                   7774:         $ownerlist = join(',',@{$owners});
                   7775:     } else {
                   7776:         $ownerlist = $owners;
                   7777:     }
1.773     raeburn  7778:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  7779:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  7780:     return $response;
                   7781: }
                   7782: 
1.679     raeburn  7783: # ------------------------------------------------------- Course Group routines
                   7784: 
                   7785: sub get_coursegroups {
1.809     raeburn  7786:     my ($cdom,$cnum,$group,$namespace) = @_;
                   7787:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  7788: }
                   7789: 
1.679     raeburn  7790: sub modify_coursegroup {
                   7791:     my ($cdom,$cnum,$groupsettings) = @_;
                   7792:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   7793: }
                   7794: 
1.809     raeburn  7795: sub toggle_coursegroup_status {
                   7796:     my ($cdom,$cnum,$group,$action) = @_;
                   7797:     my ($from_namespace,$to_namespace);
                   7798:     if ($action eq 'delete') {
                   7799:         $from_namespace = 'coursegroups';
                   7800:         $to_namespace = 'deleted_groups';
                   7801:     } else {
                   7802:         $from_namespace = 'deleted_groups';
                   7803:         $to_namespace = 'coursegroups';
                   7804:     }
                   7805:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  7806:     if (my $tmp = &error(%curr_group)) {
                   7807:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   7808:         return ('read error',$tmp);
                   7809:     } else {
                   7810:         my %savedsettings = %curr_group; 
1.809     raeburn  7811:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  7812:         my $deloutcome;
                   7813:         if ($result eq 'ok') {
1.809     raeburn  7814:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  7815:         } else {
                   7816:             return ('write error',$result);
                   7817:         }
                   7818:         if ($deloutcome eq 'ok') {
                   7819:             return 'ok';
                   7820:         } else {
                   7821:             return ('delete error',$deloutcome);
                   7822:         }
                   7823:     }
                   7824: }
                   7825: 
1.679     raeburn  7826: sub modify_group_roles {
1.957     raeburn  7827:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  7828:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   7829:     my $role = 'gr/'.&escape($userprivs);
                   7830:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  7831:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  7832:     if ($result eq 'ok') {
                   7833:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   7834:     }
1.679     raeburn  7835:     return $result;
                   7836: }
                   7837: 
                   7838: sub modify_coursegroup_membership {
                   7839:     my ($cdom,$cnum,$membership) = @_;
                   7840:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   7841:     return $result;
                   7842: }
                   7843: 
1.682     raeburn  7844: sub get_active_groups {
                   7845:     my ($udom,$uname,$cdom,$cnum) = @_;
                   7846:     my $now = time;
                   7847:     my %groups = ();
                   7848:     foreach my $key (keys(%env)) {
1.811     albertel 7849:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  7850:             my ($start,$end) = split(/\./,$env{$key});
                   7851:             if (($end!=0) && ($end<$now)) { next; }
                   7852:             if (($start!=0) && ($start>$now)) { next; }
                   7853:             if ($1 eq $cdom && $2 eq $cnum) {
                   7854:                 $groups{$3} = $env{$key} ;
                   7855:             }
                   7856:         }
                   7857:     }
                   7858:     return %groups;
                   7859: }
                   7860: 
1.683     raeburn  7861: sub get_group_membership {
                   7862:     my ($cdom,$cnum,$group) = @_;
                   7863:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   7864: }
                   7865: 
                   7866: sub get_users_groups {
                   7867:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  7868:     my @usersgroups;
1.683     raeburn  7869:     my $cachetime=1800;
                   7870: 
                   7871:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  7872:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   7873:     if (defined($cached)) {
1.734     albertel 7874:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  7875:     } else {  
                   7876:         $grouplist = '';
1.816     raeburn  7877:         my $courseurl = &courseid_to_courseurl($courseid);
1.1166    raeburn  7878:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  7879:         my $access_end = $env{'course.'.$courseid.
                   7880:                               '.default_enrollment_end_date'};
                   7881:         my $now = time;
                   7882:         foreach my $key (keys(%roleshash)) {
                   7883:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   7884:                 my $group = $1;
                   7885:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   7886:                     my $start = $2;
                   7887:                     my $end = $1;
                   7888:                     if ($start == -1) { next; } # deleted from group
                   7889:                     if (($start!=0) && ($start>$now)) { next; }
                   7890:                     if (($end!=0) && ($end<$now)) {
                   7891:                         if ($access_end && $access_end < $now) {
                   7892:                             if ($access_end - $end < 86400) {
                   7893:                                 push(@usersgroups,$group);
1.733     raeburn  7894:                             }
                   7895:                         }
1.817     raeburn  7896:                         next;
1.733     raeburn  7897:                     }
1.817     raeburn  7898:                     push(@usersgroups,$group);
1.683     raeburn  7899:                 }
                   7900:             }
                   7901:         }
1.817     raeburn  7902:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   7903:         $grouplist = join(':',@usersgroups);
                   7904:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  7905:     }
1.733     raeburn  7906:     return @usersgroups;
1.683     raeburn  7907: }
                   7908: 
                   7909: sub devalidate_getgroups_cache {
                   7910:     my ($udom,$uname,$cdom,$cnum)=@_;
                   7911:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 7912: 
1.683     raeburn  7913:     my $hashid="$udom:$uname:$courseid";
                   7914:     &devalidate_cache_new('getgroups',$hashid);
                   7915: }
                   7916: 
1.12      www      7917: # ------------------------------------------------------------------ Plain Text
                   7918: 
                   7919: sub plaintext {
1.988     raeburn  7920:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  7921:     if ($short =~ m{^cr/}) {
1.758     albertel 7922: 	return (split('/',$short))[-1];
                   7923:     }
1.742     raeburn  7924:     if (!defined($cid)) {
                   7925:         $cid = $env{'request.course.id'};
                   7926:     }
                   7927:     my %rolenames = (
1.1008    raeburn  7928:                       Course    => 'std',
                   7929:                       Community => 'alt1',
1.742     raeburn  7930:                     );
1.1037    raeburn  7931:     if ($cid ne '') {
                   7932:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   7933:             unless ($forcedefault) {
                   7934:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   7935:                 &Apache::lonlocal::mt_escape(\$roletext);
                   7936:                 return &Apache::lonlocal::mt($roletext);
                   7937:             }
                   7938:         }
                   7939:     }
                   7940:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   7941:         (defined($rolenames{$type})) && 
                   7942:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  7943:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  7944:     } elsif ($cid ne '') {
                   7945:         my $crstype = $env{'course.'.$cid.'.type'};
                   7946:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   7947:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   7948:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   7949:         }
1.742     raeburn  7950:     }
1.1037    raeburn  7951:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      7952: }
                   7953: 
                   7954: # ----------------------------------------------------------------- Assign Role
                   7955: 
                   7956: sub assignrole {
1.957     raeburn  7957:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   7958:         $context)=@_;
1.21      www      7959:     my $mrole;
                   7960:     if ($role =~ /^cr\//) {
1.393     www      7961:         my $cwosec=$url;
1.811     albertel 7962:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      7963: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  7964:            my $refused = 1;
                   7965:            if ($context eq 'requestcourses') {
                   7966:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   7967:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   7968:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   7969:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   7970:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7971:                            if ($crsenv{'internal.courseowner'} eq
                   7972:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   7973:                                $refused = '';
                   7974:                            }
                   7975:                        }
                   7976:                    }
                   7977:                }
                   7978:            }
                   7979:            if ($refused) {
                   7980:                &logthis('Refused custom assignrole: '.
                   7981:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   7982:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   7983:                return 'refused';
                   7984:            }
1.104     www      7985:         }
1.21      www      7986:         $mrole='cr';
1.678     raeburn  7987:     } elsif ($role =~ /^gr\//) {
                   7988:         my $cwogrp=$url;
1.811     albertel 7989:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  7990:         unless (&allowed('mdg',$cwogrp)) {
                   7991:             &logthis('Refused group assignrole: '.
                   7992:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   7993:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   7994:             return 'refused';
                   7995:         }
                   7996:         $mrole='gr';
1.21      www      7997:     } else {
1.82      www      7998:         my $cwosec=$url;
1.811     albertel 7999:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  8000:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   8001:             my $refused;
                   8002:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   8003:                 if (!(&allowed('c'.$role,$url))) {
                   8004:                     $refused = 1;
                   8005:                 }
                   8006:             } else {
                   8007:                 $refused = 1;
                   8008:             }
1.947     raeburn  8009:             if ($refused) {
1.1045    raeburn  8010:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   8011:                 if (!$selfenroll && $context eq 'course') {
                   8012:                     my %crsenv;
                   8013:                     if ($role eq 'cc' || $role eq 'co') {
                   8014:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   8015:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   8016:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   8017:                                 if ($crsenv{'internal.courseowner'} eq 
                   8018:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   8019:                                     $refused = '';
                   8020:                                 }
                   8021:                             }
                   8022:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   8023:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   8024:                                 if ($crsenv{'internal.courseowner'} eq 
                   8025:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   8026:                                     $refused = '';
                   8027:                                 }
                   8028:                             }
                   8029:                         }
                   8030:                     }
                   8031:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947     raeburn  8032:                     $refused = '';
1.1017    raeburn  8033:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  8034:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  8035:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  8036:                         my $wrongcc;
                   8037:                         if ($cnum =~ /^$match_community$/) {
                   8038:                             $wrongcc = 1 if ($role eq 'cc');
                   8039:                         } else {
                   8040:                             $wrongcc = 1 if ($role eq 'co');
                   8041:                         }
                   8042:                         unless ($wrongcc) {
                   8043:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   8044:                             if ($crsenv{'internal.courseowner'} eq 
                   8045:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   8046:                                 $refused = '';
                   8047:                             }
1.1017    raeburn  8048:                         }
                   8049:                     }
1.1183    raeburn  8050:                 } elsif ($context eq 'requestauthor') {
                   8051:                     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) && 
                   8052:                         ($url eq '/'.$udom.'/') && ($role eq 'au')) {
                   8053:                         if ($env{'environment.requestauthor'} eq 'automatic') {
                   8054:                             $refused = '';
                   8055:                         } else {
                   8056:                             my %domdefaults = &get_domain_defaults($udom);
                   8057:                             if (ref($domdefaults{'requestauthor'}) eq 'HASH') {
                   8058:                                 my $checkbystatus;
                   8059:                                 if ($env{'user.adv'}) { 
                   8060:                                     my $disposition = $domdefaults{'requestauthor'}{'_LC_adv'};
                   8061:                                     if ($disposition eq 'automatic') {
                   8062:                                         $refused = '';
                   8063:                                     } elsif ($disposition eq '') {
                   8064:                                         $checkbystatus = 1;
                   8065:                                     } 
                   8066:                                 } else {
                   8067:                                     $checkbystatus = 1;
                   8068:                                 }
                   8069:                                 if ($checkbystatus) {
                   8070:                                     if ($env{'environment.inststatus'}) {
                   8071:                                         my @inststatuses = split(/,/,$env{'environment.inststatus'});
                   8072:                                         foreach my $type (@inststatuses) {
                   8073:                                             if (($type ne '') &&
                   8074:                                                 ($domdefaults{'requestauthor'}{$type} eq 'automatic')) {
                   8075:                                                 $refused = '';
                   8076:                                             }
                   8077:                                         }
                   8078:                                     } elsif ($domdefaults{'requestauthor'}{'default'} eq 'automatic') {
                   8079:                                         $refused = '';
                   8080:                                     }
                   8081:                                 }
                   8082:                             }
                   8083:                         }
                   8084:                     }
1.1017    raeburn  8085:                 }
                   8086:                 if ($refused) {
1.947     raeburn  8087:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   8088:                              ' '.$role.' '.$end.' '.$start.' by '.
                   8089: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   8090:                     return 'refused';
                   8091:                 }
1.932     raeburn  8092:             }
1.1131    raeburn  8093:         } elsif ($role eq 'au') {
                   8094:             if ($url ne '/'.$udom.'/') {
                   8095:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   8096:                          ' to assign author role for '.$uname.':'.$udom.
                   8097:                          ' in domain: '.$url.' refused (wrong domain).');
                   8098:                 return 'refused';
                   8099:             }
1.104     www      8100:         }
1.21      www      8101:         $mrole=$role;
                   8102:     }
1.620     albertel 8103:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      8104:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      8105:     if ($end) { $command.='_'.$end; }
1.21      www      8106:     if ($start) {
                   8107: 	if ($end) { 
1.81      www      8108:            $command.='_'.$start; 
1.21      www      8109:         } else {
1.81      www      8110:            $command.='_0_'.$start;
1.21      www      8111:         }
                   8112:     }
1.739     raeburn  8113:     my $origstart = $start;
                   8114:     my $origend = $end;
1.957     raeburn  8115:     my $delflag;
1.357     www      8116: # actually delete
                   8117:     if ($deleteflag) {
1.373     www      8118: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      8119: # modify command to delete the role
1.620     albertel 8120:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      8121:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 8122: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      8123: # set start and finish to negative values for userrolelog
                   8124:            $start=-1;
                   8125:            $end=-1;
1.957     raeburn  8126:            $delflag = 1;
1.357     www      8127:         }
                   8128:     }
                   8129: # send command
1.349     www      8130:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      8131: # log new user role if status is ok
1.349     www      8132:     if ($answer eq 'ok') {
1.663     raeburn  8133: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.1184    raeburn  8134:         if (($role eq 'cc') || ($role eq 'in') ||
                   8135:             ($role eq 'ep') || ($role eq 'ad') ||
                   8136:             ($role eq 'ta') || ($role eq 'st') ||
                   8137:             ($role=~/^cr/) || ($role eq 'gr') ||
                   8138:             ($role eq 'co')) {
1.1200    raeburn  8139: # for course roles, perform group memberships changes triggered by role change.
                   8140:             unless ($role =~ /^gr/) {
                   8141:                 &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   8142:                                                  $origstart,$selfenroll,$context);
                   8143:             }
1.1184    raeburn  8144:             &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   8145:                            $selfenroll,$context);
                   8146:         } elsif (($role eq 'li') || ($role eq 'dg') || ($role eq 'sc') ||
                   8147:                  ($role eq 'au') || ($role eq 'dc')) {
                   8148:             &domainrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   8149:                            $context);
                   8150:         } elsif (($role eq 'ca') || ($role eq 'aa')) {
                   8151:             &coauthorrolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,
                   8152:                              $context); 
                   8153:         }
1.1053    raeburn  8154:         if ($role eq 'cc') {
                   8155:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   8156:         }
1.349     www      8157:     }
                   8158:     return $answer;
1.169     harris41 8159: }
                   8160: 
1.1053    raeburn  8161: sub autoupdate_coowners {
                   8162:     my ($url,$end,$start,$uname,$udom) = @_;
                   8163:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   8164:     if (($cdom ne '') && ($cnum ne '')) {
                   8165:         my $now = time;
                   8166:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   8167:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   8168:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   8169:             my $instcode = $coursehash{'internal.coursecode'};
                   8170:             if ($instcode ne '') {
1.1056    raeburn  8171:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   8172:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  8173:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  8174:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   8175:                         if ($result eq 'valid') {
                   8176:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  8177:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   8178:                                     push(@newcoowners,$coowner);
                   8179:                                 }
                   8180:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   8181:                                     push(@newcoowners,$uname.':'.$udom);
                   8182:                                 }
                   8183:                                 @newcoowners = sort(@newcoowners);
                   8184:                             } else {
                   8185:                                 push(@newcoowners,$uname.':'.$udom);
                   8186:                             }
                   8187:                         } else {
                   8188:                             if ($coursehash{'internal.co-owners'}) {
                   8189:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   8190:                                     unless ($coowner eq $uname.':'.$udom) {
                   8191:                                         push(@newcoowners,$coowner);
                   8192:                                     }
                   8193:                                 }
                   8194:                                 unless (@newcoowners > 0) {
                   8195:                                     $delcoowners = 1;
                   8196:                                     $coowners = '';
                   8197:                                 }
                   8198:                             }
                   8199:                         }
                   8200:                         if (@newcoowners || $delcoowners) {
                   8201:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   8202:                                             $delcoowners,@newcoowners);
                   8203:                         }
                   8204:                     }
                   8205:                 }
                   8206:             }
                   8207:         }
                   8208:     }
                   8209: }
                   8210: 
                   8211: sub store_coowners {
                   8212:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   8213:     my $cid = $cdom.'_'.$cnum;
                   8214:     my ($coowners,$delresult,$putresult);
                   8215:     if (@newcoowners) {
                   8216:         $coowners = join(',',@newcoowners);
                   8217:         my %coownershash = (
                   8218:                             'internal.co-owners' => $coowners,
                   8219:                            );
                   8220:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   8221:         if ($putresult eq 'ok') {
                   8222:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   8223:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   8224:             }
                   8225:         }
                   8226:     }
                   8227:     if ($delcoowners) {
                   8228:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   8229:         if ($delresult eq 'ok') {
                   8230:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   8231:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   8232:             }
                   8233:         }
                   8234:     }
                   8235:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   8236:         my %crsinfo =
                   8237:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   8238:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   8239:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   8240:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   8241:         }
                   8242:     }
                   8243: }
                   8244: 
1.169     harris41 8245: # -------------------------------------------------- Modify user authentication
1.197     www      8246: # Overrides without validation
                   8247: 
1.169     harris41 8248: sub modifyuserauth {
                   8249:     my ($udom,$uname,$umode,$upass)=@_;
                   8250:     my $uhome=&homeserver($uname,$udom);
1.197     www      8251:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   8252:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 8253:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   8254:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 8255:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   8256: 		     &escape($upass),$uhome);
1.620     albertel 8257:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      8258:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   8259:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   8260:     &log($udom,,$uname,$uhome,
1.620     albertel 8261:         'Authentication changed by '.$env{'user.domain'}.', '.
                   8262:                                      $env{'user.name'}.', '.$umode.
1.197     www      8263:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 8264:     unless ($reply eq 'ok') {
1.197     www      8265:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 8266: 	return 'error: '.$reply;
                   8267:     }   
1.170     harris41 8268:     return 'ok';
1.80      www      8269: }
                   8270: 
1.81      www      8271: # --------------------------------------------------------------- Modify a user
1.80      www      8272: 
1.81      www      8273: sub modifyuser {
1.206     matthew  8274:     my ($udom,    $uname, $uid,
                   8275:         $umode,   $upass, $first,
                   8276:         $middle,  $last,  $gene,
1.1058    raeburn  8277:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 8278:     $udom= &LONCAPA::clean_domain($udom);
                   8279:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  8280:     my $showcandelete = 'none';
                   8281:     if (ref($candelete) eq 'ARRAY') {
                   8282:         if (@{$candelete} > 0) {
                   8283:             $showcandelete = join(', ',@{$candelete});
                   8284:         }
                   8285:     }
1.81      www      8286:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      8287:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  8288: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  8289:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   8290:                                      ' desiredhome not specified'). 
1.620     albertel 8291:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   8292:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 8293:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  8294:     my $newuser;
                   8295:     if ($uhome eq 'no_host') {
                   8296:         $newuser = 1;
                   8297:     }
1.80      www      8298: # ----------------------------------------------------------------- Create User
1.406     albertel 8299:     if (($uhome eq 'no_host') && 
                   8300: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      8301:         my $unhome='';
1.844     albertel 8302:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  8303:             $unhome = $desiredhome;
1.620     albertel 8304: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   8305: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  8306:         } else { # load balancing routine for determining $unhome
1.81      www      8307:             my $loadm=10000000;
1.841     albertel 8308: 	    my %servers = &get_servers($udom,'library');
                   8309: 	    foreach my $tryserver (keys(%servers)) {
                   8310: 		my $answer=reply('load',$tryserver);
                   8311: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   8312: 		    $loadm=$answer;
                   8313: 		    $unhome=$tryserver;
                   8314: 		}
1.80      www      8315: 	    }
                   8316:         }
                   8317:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  8318: 	    return 'error: unable to find a home server for '.$uname.
                   8319:                    ' in domain '.$udom;
1.80      www      8320:         }
                   8321:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   8322:                          &escape($upass),$unhome);
                   8323: 	unless ($reply eq 'ok') {
                   8324:             return 'error: '.$reply;
                   8325:         }   
1.230     stredwic 8326:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      8327:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  8328: 	    return 'error: unable verify users home machine.';
1.80      www      8329:         }
1.209     matthew  8330:     }   # End of creation of new user
1.80      www      8331: # ---------------------------------------------------------------------- Add ID
                   8332:     if ($uid) {
                   8333:        $uid=~tr/A-Z/a-z/;
                   8334:        my %uidhash=&idrget($udom,$uname);
1.196     www      8335:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   8336:          && (!$forceid)) {
1.80      www      8337: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  8338: 	      return 'error: user id "'.$uid.'" does not match '.
                   8339:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      8340:           }
                   8341:        } else {
                   8342: 	  &idput($udom,($uname => $uid));
                   8343:        }
                   8344:     }
                   8345: # -------------------------------------------------------------- Add names, etc
1.313     matthew  8346:     my @tmp=&get('environment',
1.899     raeburn  8347: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  8348:                     'permanentemail','inststatus'],
1.134     albertel 8349: 		   $udom,$uname);
1.1075    raeburn  8350:     my (%names,%oldnames);
1.313     matthew  8351:     if ($tmp[0] =~ m/^error:.*/) { 
                   8352:         %names=(); 
                   8353:     } else {
                   8354:         %names = @tmp;
1.1075    raeburn  8355:         %oldnames = %names;
1.313     matthew  8356:     }
1.388     www      8357: #
1.1058    raeburn  8358: # If name, email and/or uid are blank (e.g., because an uploaded file
                   8359: # of users did not contain them), do not overwrite existing values
                   8360: # unless field is in $candelete array ref.  
                   8361: #
                   8362: 
                   8363:     my @fields = ('firstname','middlename','lastname','generation',
                   8364:                   'permanentemail','id');
                   8365:     my %newvalues;
                   8366:     if (ref($candelete) eq 'ARRAY') {
                   8367:         foreach my $field (@fields) {
                   8368:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   8369:                 if ($field eq 'firstname') {
                   8370:                     $names{$field} = $first;
                   8371:                 } elsif ($field eq 'middlename') {
                   8372:                     $names{$field} = $middle;
                   8373:                 } elsif ($field eq 'lastname') {
                   8374:                     $names{$field} = $last;
                   8375:                 } elsif ($field eq 'generation') { 
                   8376:                     $names{$field} = $gene;
                   8377:                 } elsif ($field eq 'permanentemail') {
                   8378:                     $names{$field} = $email;
                   8379:                 } elsif ($field eq 'id') {
                   8380:                     $names{$field}  = $uid;
                   8381:                 }
                   8382:             }
                   8383:         }
                   8384:     }
1.388     www      8385:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  8386:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      8387:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  8388:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      8389:     if ($email) {
                   8390:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  8391:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      8392:     }
1.899     raeburn  8393:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  8394:     if (defined($inststatus)) {
                   8395:         $names{'inststatus'} = '';
                   8396:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   8397:         if (ref($usertypes) eq 'HASH') {
                   8398:             my @okstatuses; 
                   8399:             foreach my $item (split(/:/,$inststatus)) {
                   8400:                 if (defined($usertypes->{$item})) {
                   8401:                     push(@okstatuses,$item);  
                   8402:                 }
                   8403:             }
                   8404:             if (@okstatuses) {
                   8405:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   8406:             }
                   8407:         }
                   8408:     }
1.1075    raeburn  8409:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  8410:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  8411:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  8412:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   8413:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   8414:     } else {
                   8415:         $logmsg .= ' during self creation';
                   8416:     }
1.1075    raeburn  8417:     my $changed;
                   8418:     if ($newuser) {
                   8419:         $changed = 1;
                   8420:     } else {
                   8421:         foreach my $field (@fields) {
                   8422:             if ($names{$field} ne $oldnames{$field}) {
                   8423:                 $changed = 1;
                   8424:                 last;
                   8425:             }
                   8426:         }
                   8427:     }
                   8428:     unless ($changed) {
                   8429:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   8430:         &logthis($logmsg);
                   8431:         return 'ok';
                   8432:     }
                   8433:     my $reply = &put('environment', \%names, $udom,$uname);
                   8434:     if ($reply ne 'ok') { 
                   8435:         return 'error: '.$reply;
                   8436:     }
1.1087    raeburn  8437:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   8438:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   8439:     }
1.1075    raeburn  8440:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   8441:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   8442:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  8443:     &logthis($logmsg);
1.134     albertel 8444:     return 'ok';
1.80      www      8445: }
                   8446: 
1.81      www      8447: # -------------------------------------------------------------- Modify student
1.80      www      8448: 
1.81      www      8449: sub modifystudent {
                   8450:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  8451:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.1216    raeburn  8452:         $selfenroll,$context,$inststatus,$credits)=@_;
1.455     albertel 8453:     if (!$cid) {
1.620     albertel 8454: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 8455: 	    return 'not_in_class';
                   8456: 	}
1.80      www      8457:     }
                   8458: # --------------------------------------------------------------- Make the user
1.81      www      8459:     my $reply=&modifyuser
1.209     matthew  8460: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  8461:          $desiredhome,$email,$inststatus);
1.80      www      8462:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  8463:     # This will cause &modify_student_enrollment to get the uid from the
                   8464:     # students environment
                   8465:     $uid = undef if (!$forceid);
1.455     albertel 8466:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.1216    raeburn  8467:                                         $gene,$usec,$end,$start,$type,$locktype,
                   8468:                                         $cid,$selfenroll,$context,$credits);
1.297     matthew  8469:     return $reply;
                   8470: }
                   8471: 
                   8472: sub modify_student_enrollment {
1.1216    raeburn  8473:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,
                   8474:         $locktype,$cid,$selfenroll,$context,$credits) = @_;
1.455     albertel 8475:     my ($cdom,$cnum,$chome);
                   8476:     if (!$cid) {
1.620     albertel 8477: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 8478: 	    return 'not_in_class';
                   8479: 	}
1.620     albertel 8480: 	$cdom=$env{'course.'.$cid.'.domain'};
                   8481: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 8482:     } else {
                   8483: 	($cdom,$cnum)=split(/_/,$cid);
                   8484:     }
1.620     albertel 8485:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 8486:     if (!$chome) {
1.457     raeburn  8487: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  8488:     }
1.455     albertel 8489:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  8490:     # Make sure the user exists
1.81      www      8491:     my $uhome=&homeserver($uname,$udom);
                   8492:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   8493: 	return 'error: no such user';
                   8494:     }
1.297     matthew  8495:     # Get student data if we were not given enough information
                   8496:     if (!defined($first)  || $first  eq '' || 
                   8497:         !defined($last)   || $last   eq '' || 
                   8498:         !defined($uid)    || $uid    eq '' || 
                   8499:         !defined($middle) || $middle eq '' || 
                   8500:         !defined($gene)   || $gene   eq '') {
1.294     matthew  8501:         # They did not supply us with enough data to enroll the student, so
                   8502:         # we need to pick up more information.
1.297     matthew  8503:         my %tmp = &get('environment',
1.294     matthew  8504:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  8505:                        ,$udom,$uname);
                   8506: 
1.800     albertel 8507:         #foreach my $key (keys(%tmp)) {
                   8508:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 8509:         #}
1.294     matthew  8510:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   8511:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   8512:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  8513:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  8514:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   8515:     }
1.556     albertel 8516:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  8517:     my $user = "$uname:$udom";
                   8518:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 8519:     my $reply=cput('classlist',
1.1148    raeburn  8520: 		   {$user => 
1.1216    raeburn  8521: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype,$credits) },
1.487     albertel 8522: 		   $cdom,$cnum);
1.1148    raeburn  8523:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   8524:         &devalidate_getsection_cache($udom,$uname,$cid);
                   8525:     } else { 
1.81      www      8526: 	return 'error: '.$reply;
                   8527:     }
1.297     matthew  8528:     # Add student role to user
1.83      www      8529:     my $uurl='/'.$cid;
1.81      www      8530:     $uurl=~s/\_/\//g;
                   8531:     if ($usec) {
                   8532: 	$uurl.='/'.$usec;
                   8533:     }
1.1148    raeburn  8534:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   8535:                              $selfenroll,$context);
                   8536:     if ($result ne 'ok') {
                   8537:         if ($old_entry{$user} ne '') {
                   8538:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   8539:         } else {
                   8540:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   8541:         }
                   8542:     }
                   8543:     return $result; 
1.21      www      8544: }
                   8545: 
1.556     albertel 8546: sub format_name {
                   8547:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   8548:     my $name;
                   8549:     if ($first ne 'lastname') {
                   8550: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   8551:     } else {
                   8552: 	if ($lastname=~/\S/) {
                   8553: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   8554: 	    $name=~s/\s+,/,/;
                   8555: 	} else {
                   8556: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   8557: 	}
                   8558:     }
                   8559:     $name=~s/^\s+//;
                   8560:     $name=~s/\s+$//;
                   8561:     $name=~s/\s+/ /g;
                   8562:     return $name;
                   8563: }
                   8564: 
1.84      www      8565: # ------------------------------------------------- Write to course preferences
                   8566: 
                   8567: sub writecoursepref {
                   8568:     my ($courseid,%prefs)=@_;
                   8569:     $courseid=~s/^\///;
                   8570:     $courseid=~s/\_/\//g;
                   8571:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   8572:     my $chome=homeserver($cnum,$cdomain);
                   8573:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   8574: 	return 'error: no such course';
                   8575:     }
                   8576:     my $cstring='';
1.800     albertel 8577:     foreach my $pref (keys(%prefs)) {
                   8578: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 8579:     }
1.84      www      8580:     $cstring=~s/\&$//;
                   8581:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   8582: }
                   8583: 
                   8584: # ---------------------------------------------------------- Make/modify course
                   8585: 
                   8586: sub createcourse {
1.741     raeburn  8587:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  8588:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      8589:     $url=&declutter($url);
                   8590:     my $cid='';
1.1028    raeburn  8591:     if ($context eq 'requestcourses') {
                   8592:         my $can_create = 0;
                   8593:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   8594:         if ($udom eq $ownerdom) {
                   8595:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   8596:                                   $context)) {
                   8597:                 $can_create = 1;
                   8598:             }
                   8599:         } else {
                   8600:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   8601:                                            $category);
                   8602:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   8603:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   8604:                 if (@curr > 0) {
                   8605:                     my @options = qw(approval validate autolimit);
                   8606:                     my $optregex = join('|',@options);
                   8607:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   8608:                         $can_create = 1;
                   8609:                     }
                   8610:                 }
                   8611:             }
                   8612:         }
                   8613:         if ($can_create) {
                   8614:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   8615:                 unless (&allowed('ccc',$udom)) {
                   8616:                     return 'refused'; 
                   8617:                 }
1.1017    raeburn  8618:             }
                   8619:         } else {
                   8620:             return 'refused';
                   8621:         }
1.1028    raeburn  8622:     } elsif (!&allowed('ccc',$udom)) {
                   8623:         return 'refused';
1.84      www      8624:     }
1.1011    raeburn  8625: # --------------------------------------------------------------- Get Unique ID
                   8626:     my $uname;
                   8627:     if ($cnum =~ /^$match_courseid$/) {
                   8628:         my $chome=&homeserver($cnum,$udom,'true');
                   8629:         if (($chome eq '') || ($chome eq 'no_host')) {
                   8630:             $uname = $cnum;
                   8631:         } else {
1.1038    raeburn  8632:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  8633:         }
                   8634:     } else {
1.1038    raeburn  8635:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  8636:     }
                   8637:     return $uname if ($uname =~ /^error/);
                   8638: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  8639:     if (!defined($course_server)) {
                   8640:         if (defined(&domain($udom,'primary'))) {
                   8641:             $course_server = &domain($udom,'primary');
                   8642:         } else {
                   8643:             $course_server = $env{'user.home'}; 
                   8644:         }
                   8645:     }
                   8646:     my %host_servers =
                   8647:         &Apache::lonnet::get_servers($udom,'library');
                   8648:     unless ($host_servers{$course_server}) {
                   8649:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  8650:     }
1.84      www      8651: # ------------------------------------------------------------- Make the course
                   8652:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  8653:                       $course_server);
1.84      www      8654:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  8655:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      8656:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   8657: 	return 'error: no such course';
                   8658:     }
1.271     www      8659: # ----------------------------------------------------------------- Course made
1.516     raeburn  8660: # log existence
1.1029    raeburn  8661:     my $now = time;
1.918     raeburn  8662:     my $newcourse = {
                   8663:                     $udom.'_'.$uname => {
1.921     raeburn  8664:                                      description => $description,
                   8665:                                      inst_code   => $inst_code,
                   8666:                                      owner       => $course_owner,
                   8667:                                      type        => $crstype,
1.1029    raeburn  8668:                                      creator     => $env{'user.name'}.':'.
                   8669:                                                     $env{'user.domain'},
                   8670:                                      created     => $now,
                   8671:                                      context     => $context,
1.918     raeburn  8672:                                                 },
                   8673:                     };
1.921     raeburn  8674:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      8675: # set toplevel url
1.271     www      8676:     my $topurl=$url;
                   8677:     unless ($nonstandard) {
                   8678: # ------------------------------------------ For standard courses, make top url
                   8679:         my $mapurl=&clutter($url);
1.278     www      8680:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 8681:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      8682: <map>
                   8683: <resource id="1" type="start"></resource>
                   8684: <resource id="2" src="$mapurl"></resource>
                   8685: <resource id="3" type="finish"></resource>
                   8686: <link index="1" from="1" to="2"></link>
                   8687: <link index="2" from="2" to="3"></link>
                   8688: </map>
                   8689: ENDINITMAP
                   8690:         $topurl=&declutter(
1.638     albertel 8691:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      8692:                           );
                   8693:     }
                   8694: # ----------------------------------------------------------- Write preferences
1.84      www      8695:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  8696:                      ('description'              => $description,
                   8697:                       'url'                      => $topurl,
                   8698:                       'internal.creator'         => $env{'user.name'}.':'.
                   8699:                                                     $env{'user.domain'},
                   8700:                       'internal.created'         => $now,
                   8701:                       'internal.creationcontext' => $context)
                   8702:                     );
1.84      www      8703:     return '/'.$udom.'/'.$uname;
                   8704: }
                   8705: 
1.1011    raeburn  8706: # ------------------------------------------------------------------- Create ID
                   8707: sub generate_coursenum {
1.1038    raeburn  8708:     my ($udom,$crstype) = @_;
1.1011    raeburn  8709:     my $domdesc = &domain($udom);
                   8710:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  8711:     my $first;
                   8712:     if ($crstype eq 'Community') {
                   8713:         $first = '0';
                   8714:     } else {
                   8715:         $first = int(1+rand(9)); 
                   8716:     } 
                   8717:     my $uname=$first.
1.1011    raeburn  8718:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   8719:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   8720:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   8721: # ----------------------------------------------- Make sure that does not exist
                   8722:     my $uhome=&homeserver($uname,$udom,'true');
                   8723:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  8724:         if ($crstype eq 'Community') {
                   8725:             $first = '0';
                   8726:         } else {
                   8727:             $first = int(1+rand(9));
                   8728:         }
                   8729:         $uname=$first.
1.1011    raeburn  8730:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   8731:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   8732:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   8733:         $uhome=&homeserver($uname,$udom,'true');
                   8734:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   8735:             return 'error: unable to generate unique course-ID';
                   8736:         }
                   8737:     }
                   8738:     return $uname;
                   8739: }
                   8740: 
1.813     albertel 8741: sub is_course {
1.1167    droeschl 8742:     my ($cdom, $cnum) = scalar(@_) == 1 ? 
                   8743:          ($_[0] =~ /^($match_domain)_($match_courseid)$/)  :  @_;
                   8744: 
                   8745:     return unless $cdom and $cnum;
                   8746: 
                   8747:     my %courses = &courseiddump($cdom, '.', 1, '.', '.', $cnum, undef, undef,
                   8748:         '.');
                   8749: 
1.1219    raeburn  8750:     return unless(exists($courses{$cdom.'_'.$cnum}));
1.1167    droeschl 8751:     return wantarray ? ($cdom, $cnum) : $cdom.'_'.$cnum;
1.813     albertel 8752: }
                   8753: 
1.1015    raeburn  8754: sub store_userdata {
                   8755:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  8756:     my $result;
1.1016    raeburn  8757:     if ($datakey ne '') {
1.1013    raeburn  8758:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  8759:             if ($udom eq '' || $uname eq '') {
                   8760:                 $udom = $env{'user.domain'};
                   8761:                 $uname = $env{'user.name'};
                   8762:             }
                   8763:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  8764:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   8765:                 $result = 'error: no_host';
                   8766:             } else {
                   8767:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   8768:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   8769: 
                   8770:                 my $namevalue='';
                   8771:                 foreach my $key (keys(%{$storehash})) {
                   8772:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   8773:                 }
                   8774:                 $namevalue=~s/\&$//;
1.1224    raeburn  8775:                 unless ($namespace eq 'courserequests') {
                   8776:                     $datakey = &escape($datakey);
                   8777:                 }
1.1105    raeburn  8778:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   8779:                                   $namevalue,$uhome);
1.1013    raeburn  8780:             }
                   8781:         } else {
                   8782:             $result = 'error: data to store was not a hash reference'; 
                   8783:         }
                   8784:     } else {
                   8785:         $result= 'error: invalid requestkey'; 
                   8786:     }
                   8787:     return $result;
                   8788: }
                   8789: 
1.21      www      8790: # ---------------------------------------------------------- Assign Custom Role
                   8791: 
                   8792: sub assigncustomrole {
1.957     raeburn  8793:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      8794:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  8795:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      8796: }
                   8797: 
                   8798: # ----------------------------------------------------------------- Revoke Role
                   8799: 
                   8800: sub revokerole {
1.957     raeburn  8801:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      8802:     my $now=time;
1.965     raeburn  8803:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      8804: }
                   8805: 
                   8806: # ---------------------------------------------------------- Revoke Custom Role
                   8807: 
                   8808: sub revokecustomrole {
1.957     raeburn  8809:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      8810:     my $now=time;
1.357     www      8811:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  8812:            $deleteflag,$selfenroll,$context);
1.17      www      8813: }
                   8814: 
1.533     banghart 8815: # ------------------------------------------------------------ Disk usage
1.535     albertel 8816: sub diskusage {
1.955     raeburn  8817:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   8818:     $directorypath =~ s/\/$//;
                   8819:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   8820:                        .&escape($getpropath).':'.&escape($uname).':'
                   8821:                        .&escape($udom),homeserver($uname,$udom));
                   8822:     if ($listing eq 'unknown_cmd') {
                   8823:         if ($getpropath) {
                   8824:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   8825:         }
                   8826:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   8827:     }
1.514     albertel 8828:     return $listing;
1.512     banghart 8829: }
                   8830: 
1.566     banghart 8831: sub is_locked {
1.1096    raeburn  8832:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 8833:     my @check;
                   8834:     my $is_locked;
1.1093    raeburn  8835:     push (@check,$file_name);
1.613     albertel 8836:     my %locked = &get('file_permissions',\@check,
1.620     albertel 8837: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 8838:     my ($tmp)=keys(%locked);
                   8839:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  8840:     
1.566     banghart 8841:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  8842:         $is_locked = 'false';
                   8843:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  8844:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  8845:                $is_locked = 'true';
1.1096    raeburn  8846:                if (ref($which) eq 'ARRAY') {
                   8847:                    push(@{$which},$entry);
                   8848:                } else {
                   8849:                    last;
                   8850:                }
1.745     raeburn  8851:            }
                   8852:        }
1.566     banghart 8853:     } else {
                   8854:         $is_locked = 'false';
                   8855:     }
1.1093    raeburn  8856:     return $is_locked;
1.566     banghart 8857: }
                   8858: 
1.759     albertel 8859: sub declutter_portfile {
                   8860:     my ($file) = @_;
1.833     albertel 8861:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 8862:     return $file;
                   8863: }
                   8864: 
1.559     banghart 8865: # ------------------------------------------------------------- Mark as Read Only
                   8866: 
                   8867: sub mark_as_readonly {
                   8868:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 8869:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 8870:     my ($tmp)=keys(%current_permissions);
                   8871:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 8872:     foreach my $file (@{$files}) {
1.759     albertel 8873: 	$file = &declutter_portfile($file);
1.561     banghart 8874:         push(@{$current_permissions{$file}},$what);
1.559     banghart 8875:     }
1.613     albertel 8876:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 8877:     return;
                   8878: }
                   8879: 
1.572     banghart 8880: # ------------------------------------------------------------Save Selected Files
                   8881: 
                   8882: sub save_selected_files {
                   8883:     my ($user, $path, @files) = @_;
                   8884:     my $filename = $user."savedfiles";
1.573     banghart 8885:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 8886:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 8887:     foreach my $file (@files) {
1.620     albertel 8888:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 8889:     }
                   8890:     foreach my $file (@other_files) {
1.574     banghart 8891:         print (OUT $file."\n");
1.572     banghart 8892:     }
1.574     banghart 8893:     close (OUT);
1.572     banghart 8894:     return 'ok';
                   8895: }
                   8896: 
1.574     banghart 8897: sub clear_selected_files {
                   8898:     my ($user) = @_;
                   8899:     my $filename = $user."savedfiles";
1.1117    foxr     8900:     open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574     banghart 8901:     print (OUT undef);
                   8902:     close (OUT);
                   8903:     return ("ok");    
                   8904: }
                   8905: 
1.572     banghart 8906: sub files_in_path {
                   8907:     my ($user, $path) = @_;
                   8908:     my $filename = $user."savedfiles";
                   8909:     my %return_files;
1.1117    foxr     8910:     open (IN, '<'.LONCAPA::tempdir().$filename);
1.573     banghart 8911:     while (my $line_in = <IN>) {
1.574     banghart 8912:         chomp ($line_in);
                   8913:         my @paths_and_file = split (m!/!, $line_in);
                   8914:         my $file_part = pop (@paths_and_file);
                   8915:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 8916:         $path_part.='/';
                   8917:         my $path_and_file = $path_part.$file_part;
                   8918:         if ($path_part eq $path) {
                   8919:             $return_files{$file_part}= 'selected';
                   8920:         }
                   8921:     }
1.574     banghart 8922:     close (IN);
                   8923:     return (\%return_files);
1.572     banghart 8924: }
                   8925: 
                   8926: # called in portfolio select mode, to show files selected NOT in current directory
                   8927: sub files_not_in_path {
                   8928:     my ($user, $path) = @_;
                   8929:     my $filename = $user."savedfiles";
                   8930:     my @return_files;
                   8931:     my $path_part;
1.1117    foxr     8932:     open(IN, '<'.LONCAPA::.$filename);
1.800     albertel 8933:     while (my $line = <IN>) {
1.572     banghart 8934:         #ok, I know it's clunky, but I want it to work
1.800     albertel 8935:         my @paths_and_file = split(m|/|, $line);
                   8936:         my $file_part = pop(@paths_and_file);
                   8937:         chomp($file_part);
                   8938:         my $path_part = join('/', @paths_and_file);
1.572     banghart 8939:         $path_part .= '/';
                   8940:         my $path_and_file = $path_part.$file_part;
                   8941:         if ($path_part ne $path) {
1.800     albertel 8942:             push(@return_files, ($path_and_file));
1.572     banghart 8943:         }
                   8944:     }
1.800     albertel 8945:     close(OUT);
1.574     banghart 8946:     return (@return_files);
1.572     banghart 8947: }
                   8948: 
1.745     raeburn  8949: #----------------------------------------------Get portfolio file permissions
1.629     banghart 8950: 
1.745     raeburn  8951: sub get_portfile_permissions {
                   8952:     my ($domain,$user) = @_;
1.613     albertel 8953:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 8954:     my ($tmp)=keys(%current_permissions);
                   8955:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  8956:     return \%current_permissions;
                   8957: }
                   8958: 
                   8959: #---------------------------------------------Get portfolio file access controls
                   8960: 
1.749     raeburn  8961: sub get_access_controls {
1.745     raeburn  8962:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 8963:     my %access;
                   8964:     my $real_file = $file;
                   8965:     $file =~ s/\.meta$//;
1.745     raeburn  8966:     if (defined($file)) {
1.749     raeburn  8967:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   8968:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 8969:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  8970:             }
                   8971:         }
1.745     raeburn  8972:     } else {
1.749     raeburn  8973:         foreach my $key (keys(%{$current_permissions})) {
                   8974:             if ($key =~ /\0accesscontrol$/) {
                   8975:                 if (defined($group)) {
                   8976:                     if ($key !~ m-^\Q$group\E/-) {
                   8977:                         next;
                   8978:                     }
                   8979:                 }
                   8980:                 my ($fullpath) = split(/\0/,$key);
                   8981:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   8982:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   8983:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   8984:                     }
                   8985:                 }
                   8986:             }
                   8987:         }
                   8988:     }
                   8989:     return %access;
                   8990: }
                   8991: 
                   8992: sub modify_access_controls {
                   8993:     my ($file_name,$changes,$domain,$user)=@_;
                   8994:     my ($outcome,$deloutcome);
                   8995:     my %store_permissions;
                   8996:     my %new_values;
                   8997:     my %new_control;
                   8998:     my %translation;
                   8999:     my @deletions = ();
                   9000:     my $now = time;
                   9001:     if (exists($$changes{'activate'})) {
                   9002:         if (ref($$changes{'activate'}) eq 'HASH') {
                   9003:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   9004:             my $numnew = scalar(@newitems);
                   9005:             for (my $i=0; $i<$numnew; $i++) {
                   9006:                 my $newkey = $newitems[$i];
                   9007:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  9008:                 if ($newkey =~ /^\d+:/) { 
                   9009:                     $newkey =~ s/^(\d+)/$newid/;
                   9010:                     $translation{$1} = $newid;
                   9011:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   9012:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   9013:                     $translation{$1} = $newid;
                   9014:                 }
1.749     raeburn  9015:                 $new_values{$file_name."\0".$newkey} = 
                   9016:                                           $$changes{'activate'}{$newitems[$i]};
                   9017:                 $new_control{$newkey} = $now;
                   9018:             }
                   9019:         }
                   9020:     }
                   9021:     my %todelete;
                   9022:     my %changed_items;
                   9023:     foreach my $action ('delete','update') {
                   9024:         if (exists($$changes{$action})) {
                   9025:             if (ref($$changes{$action}) eq 'HASH') {
                   9026:                 foreach my $key (keys(%{$$changes{$action}})) {
                   9027:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   9028:                     if ($action eq 'delete') { 
                   9029:                         $todelete{$itemnum} = 1;
                   9030:                     } else {
                   9031:                         $changed_items{$itemnum} = $key;
                   9032:                     }
                   9033:                 }
1.745     raeburn  9034:             }
                   9035:         }
1.749     raeburn  9036:     }
                   9037:     # get lock on access controls for file.
                   9038:     my $lockhash = {
                   9039:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   9040:                                                        ':'.$env{'user.domain'},
                   9041:                    }; 
                   9042:     my $tries = 0;
                   9043:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   9044:    
                   9045:     while (($gotlock ne 'ok') && $tries <3) {
                   9046:         $tries ++;
                   9047:         sleep 1;
                   9048:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   9049:     }
                   9050:     if ($gotlock eq 'ok') {
                   9051:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   9052:         my ($tmp)=keys(%curr_permissions);
                   9053:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   9054:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   9055:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   9056:             if (ref($curr_controls) eq 'HASH') {
                   9057:                 foreach my $control_item (keys(%{$curr_controls})) {
                   9058:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   9059:                     if (defined($todelete{$itemnum})) {
                   9060:                         push(@deletions,$file_name."\0".$control_item);
                   9061:                     } else {
                   9062:                         if (defined($changed_items{$itemnum})) {
                   9063:                             $new_control{$changed_items{$itemnum}} = $now;
                   9064:                             push(@deletions,$file_name."\0".$control_item);
                   9065:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   9066:                         } else {
                   9067:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   9068:                         }
                   9069:                     }
1.745     raeburn  9070:                 }
                   9071:             }
                   9072:         }
1.970     raeburn  9073:         my ($group);
                   9074:         if (&is_course($domain,$user)) {
                   9075:             ($group,my $file) = split(/\//,$file_name,2);
                   9076:         }
1.749     raeburn  9077:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   9078:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   9079:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   9080:         #  remove lock
                   9081:         my @del_lock = ($file_name."\0".'locked_access_records');
                   9082:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  9083:         my $sqlresult =
1.970     raeburn  9084:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  9085:                                     $group);
1.749     raeburn  9086:     } else {
                   9087:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  9088:     }
1.749     raeburn  9089:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  9090: }
                   9091: 
1.827     raeburn  9092: sub make_public_indefinitely {
                   9093:     my ($requrl) = @_;
                   9094:     my $now = time;
                   9095:     my $action = 'activate';
                   9096:     my $aclnum = 0;
                   9097:     if (&is_portfolio_url($requrl)) {
                   9098:         my (undef,$udom,$unum,$file_name,$group) =
                   9099:             &parse_portfolio_url($requrl);
                   9100:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   9101:         my %access_controls = &get_access_controls($current_perms,
                   9102:                                                    $group,$file_name);
                   9103:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   9104:             my ($num,$scope,$end,$start) = 
                   9105:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   9106:             if ($scope eq 'public') {
                   9107:                 if ($start <= $now && $end == 0) {
                   9108:                     $action = 'none';
                   9109:                 } else {
                   9110:                     $action = 'update';
                   9111:                     $aclnum = $num;
                   9112:                 }
                   9113:                 last;
                   9114:             }
                   9115:         }
                   9116:         if ($action eq 'none') {
                   9117:              return 'ok';
                   9118:         } else {
                   9119:             my %changes;
                   9120:             my $newend = 0;
                   9121:             my $newstart = $now;
                   9122:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   9123:             $changes{$action}{$newkey} = {
                   9124:                 type => 'public',
                   9125:                 time => {
                   9126:                     start => $newstart,
                   9127:                     end   => $newend,
                   9128:                 },
                   9129:             };
                   9130:             my ($outcome,$deloutcome,$new_values,$translation) =
                   9131:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   9132:             return $outcome;
                   9133:         }
                   9134:     } else {
                   9135:         return 'invalid';
                   9136:     }
                   9137: }
                   9138: 
1.745     raeburn  9139: #------------------------------------------------------Get Marked as Read Only
                   9140: 
                   9141: sub get_marked_as_readonly {
                   9142:     my ($domain,$user,$what,$group) = @_;
                   9143:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 9144:     my @readonly_files;
1.629     banghart 9145:     my $cmp1=$what;
                   9146:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  9147:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   9148:         if (defined($group)) {
                   9149:             if ($file_name !~ m-^\Q$group\E/-) {
                   9150:                 next;
                   9151:             }
                   9152:         }
1.561     banghart 9153:         if (ref($value) eq "ARRAY"){
                   9154:             foreach my $stored_what (@{$value}) {
1.629     banghart 9155:                 my $cmp2=$stored_what;
1.759     albertel 9156:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  9157:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  9158:                 }
1.629     banghart 9159:                 if ($cmp1 eq $cmp2) {
1.561     banghart 9160:                     push(@readonly_files, $file_name);
1.745     raeburn  9161:                     last;
1.563     banghart 9162:                 } elsif (!defined($what)) {
                   9163:                     push(@readonly_files, $file_name);
1.745     raeburn  9164:                     last;
1.561     banghart 9165:                 }
                   9166:             }
1.745     raeburn  9167:         }
1.561     banghart 9168:     }
                   9169:     return @readonly_files;
                   9170: }
1.577     banghart 9171: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 9172: 
1.577     banghart 9173: sub get_marked_as_readonly_hash {
1.745     raeburn  9174:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 9175:     my %readonly_files;
1.745     raeburn  9176:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   9177:         if (defined($group)) {
                   9178:             if ($file_name !~ m-^\Q$group\E/-) {
                   9179:                 next;
                   9180:             }
                   9181:         }
1.577     banghart 9182:         if (ref($value) eq "ARRAY"){
                   9183:             foreach my $stored_what (@{$value}) {
1.745     raeburn  9184:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 9185:                     foreach my $lock_descriptor(@{$stored_what}) {
                   9186:                         if ($lock_descriptor eq 'graded') {
                   9187:                             $readonly_files{$file_name} = 'graded';
                   9188:                         } elsif ($lock_descriptor eq 'handback') {
                   9189:                             $readonly_files{$file_name} = 'handback';
                   9190:                         } else {
                   9191:                             if (!exists($readonly_files{$file_name})) {
                   9192:                                 $readonly_files{$file_name} = 'locked';
                   9193:                             }
                   9194:                         }
1.745     raeburn  9195:                     }
1.750     banghart 9196:                 } 
1.577     banghart 9197:             }
                   9198:         } 
                   9199:     }
                   9200:     return %readonly_files;
                   9201: }
1.559     banghart 9202: # ------------------------------------------------------------ Unmark as Read Only
                   9203: 
                   9204: sub unmark_as_readonly {
1.629     banghart 9205:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   9206:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  9207:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 9208:     $file_name = &declutter_portfile($file_name);
1.634     albertel 9209:     my $symb_crs = $what;
                   9210:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  9211:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 9212:     my ($tmp)=keys(%current_permissions);
                   9213:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  9214:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 9215:     foreach my $file (@readonly_files) {
1.759     albertel 9216: 	my $clean_file = &declutter_portfile($file);
                   9217: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 9218: 	my $current_locks = $current_permissions{$file};
1.563     banghart 9219:         my @new_locks;
                   9220:         my @del_keys;
                   9221:         if (ref($current_locks) eq "ARRAY"){
                   9222:             foreach my $locker (@{$current_locks}) {
1.632     albertel 9223:                 my $compare=$locker;
1.749     raeburn  9224:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  9225:                     $compare=join('',@{$locker});
1.746     raeburn  9226:                     if ($compare ne $symb_crs) {
                   9227:                         push(@new_locks, $locker);
                   9228:                     }
1.563     banghart 9229:                 }
                   9230:             }
1.650     albertel 9231:             if (scalar(@new_locks) > 0) {
1.563     banghart 9232:                 $current_permissions{$file} = \@new_locks;
                   9233:             } else {
                   9234:                 push(@del_keys, $file);
1.613     albertel 9235:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 9236:                 delete($current_permissions{$file});
1.563     banghart 9237:             }
                   9238:         }
1.561     banghart 9239:     }
1.613     albertel 9240:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 9241:     return;
                   9242: }
1.512     banghart 9243: 
1.17      www      9244: # ------------------------------------------------------------ Directory lister
                   9245: 
                   9246: sub dirlist {
1.955     raeburn  9247:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      9248:     $uri=~s/^\///;
                   9249:     $uri=~s/\/$//;
1.253     stredwic 9250:     my ($udom, $uname);
1.955     raeburn  9251:     if ($getuserdir) {
1.253     stredwic 9252:         $udom = $userdomain;
                   9253:         $uname = $username;
1.955     raeburn  9254:     } else {
                   9255:         (undef,$udom,$uname)=split(/\//,$uri);
                   9256:         if(defined($userdomain)) {
                   9257:             $udom = $userdomain;
                   9258:         }
                   9259:         if(defined($username)) {
                   9260:             $uname = $username;
                   9261:         }
1.253     stredwic 9262:     }
1.955     raeburn  9263:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 9264: 
1.955     raeburn  9265:     $dirRoot = $perlvar{'lonDocRoot'};
                   9266:     if (defined($getpropath)) {
                   9267:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 9268:         $dirRoot =~ s/\/$//;
1.955     raeburn  9269:     } elsif (defined($getuserdir)) {
                   9270:         my $subdir=$uname.'__';
                   9271:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   9272:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   9273:                    ."/$udom/$subdir/$uname";
                   9274:     } elsif (defined($alternateRoot)) {
                   9275:         $dirRoot = $alternateRoot;
1.751     banghart 9276:     }
1.253     stredwic 9277: 
                   9278:     if($udom) {
                   9279:         if($uname) {
1.1135    raeburn  9280:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  9281:             if ($uhome eq 'no_host') {
                   9282:                 return ([],'no_host');
                   9283:             }
1.955     raeburn  9284:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  9285:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  9286:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  9287:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  9288:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  9289:             } else {
                   9290:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   9291:             }
1.605     matthew  9292:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  9293:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  9294:                 @listing_results = split(/:/,$listing);
                   9295:             } else {
                   9296:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   9297:             }
1.1135    raeburn  9298:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  9299:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   9300:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   9301:                 return ([],$listing);
                   9302:             } else {
                   9303:                 return (\@listing_results);
1.1135    raeburn  9304:             }
1.955     raeburn  9305:         } elsif(!$alternateRoot) {
1.1136    raeburn  9306:             my (%allusers,%listerror);
1.841     albertel 9307: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  9308:  	    foreach my $tryserver (keys(%servers)) {
                   9309:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   9310:                                   &escape($udom),$tryserver);
                   9311:                 if ($listing eq 'unknown_cmd') {
                   9312: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   9313: 				      $udom, $tryserver);
                   9314:                 } else {
                   9315:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   9316:                 }
1.841     albertel 9317: 		if ($listing eq 'unknown_cmd') {
                   9318: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   9319: 				      $udom, $tryserver);
                   9320: 		    @listing_results = split(/:/,$listing);
                   9321: 		} else {
                   9322: 		    @listing_results =
                   9323: 			map { &unescape($_); } split(/:/,$listing);
                   9324: 		}
1.1136    raeburn  9325:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   9326:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   9327:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   9328:                     $listerror{$tryserver} = $listing;
                   9329:                 } else {
1.841     albertel 9330: 		    foreach my $line (@listing_results) {
                   9331: 			my ($entry) = split(/&/,$line,2);
                   9332: 			$allusers{$entry} = 1;
                   9333: 		    }
                   9334: 		}
1.253     stredwic 9335:             }
1.1136    raeburn  9336:             my @alluserslist=();
1.800     albertel 9337:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  9338:                 push(@alluserslist,$user.'&user');
1.253     stredwic 9339:             }
1.1136    raeburn  9340:             return (\@alluserslist);
1.253     stredwic 9341:         } else {
1.1136    raeburn  9342:             return ([],'missing username');
1.253     stredwic 9343:         }
1.955     raeburn  9344:     } elsif(!defined($getpropath)) {
1.1136    raeburn  9345:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   9346:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   9347:         return (\@all_domains);
1.955     raeburn  9348:     } else {
1.1136    raeburn  9349:         return ([],'missing domain');
1.275     stredwic 9350:     }
                   9351: }
                   9352: 
                   9353: # --------------------------------------------- GetFileTimestamp
                   9354: # This function utilizes dirlist and returns the date stamp for
                   9355: # when it was last modified.  It will also return an error of -1
                   9356: # if an error occurs
                   9357: 
                   9358: sub GetFileTimestamp {
1.955     raeburn  9359:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 9360:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   9361:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  9362:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   9363:                                     undef,$getuserdir);
                   9364:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   9365:         return -1;
                   9366:     }
                   9367:     if (ref($fileref) eq 'ARRAY') {
                   9368:         my @stats = split('&',$fileref->[0]);
1.375     matthew  9369:         # @stats contains first the filename, then the stat output
                   9370:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 9371:     } else {
                   9372:         return -1;
1.253     stredwic 9373:     }
1.26      www      9374: }
                   9375: 
1.712     albertel 9376: sub stat_file {
                   9377:     my ($uri) = @_;
1.787     albertel 9378:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 9379: 
1.955     raeburn  9380:     my ($udom,$uname,$file);
1.712     albertel 9381:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   9382: 	($udom,$uname,$file) =
1.811     albertel 9383: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 9384: 	$file = 'userfiles/'.$file;
                   9385:     }
                   9386:     if ($uri =~ m-^/res/-) {
                   9387: 	($udom,$uname) = 
1.807     albertel 9388: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 9389: 	$file = $uri;
                   9390:     }
                   9391: 
                   9392:     if (!$udom || !$uname || !$file) {
                   9393: 	# unable to handle the uri
                   9394: 	return ();
                   9395:     }
1.956     raeburn  9396:     my $getpropath;
                   9397:     if ($file =~ /^userfiles\//) {
                   9398:         $getpropath = 1;
                   9399:     }
1.1136    raeburn  9400:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   9401:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   9402:         return ();
                   9403:     } else {
                   9404:         if (ref($listref) eq 'ARRAY') {
                   9405:             my @stats = split('&',$listref->[0]);
                   9406: 	    shift(@stats); #filename is first
                   9407: 	    return @stats;
                   9408:         }
1.712     albertel 9409:     }
                   9410:     return ();
                   9411: }
                   9412: 
1.26      www      9413: # -------------------------------------------------------- Value of a Condition
                   9414: 
1.713     albertel 9415: # gets the value of a specific preevaluated condition
                   9416: #    stored in the string  $env{user.state.<cid>}
                   9417: # or looks up a condition reference in the bighash and if if hasn't
                   9418: # already been evaluated recurses into docondval to get the value of
                   9419: # the condition, then memoizing it to 
                   9420: #   $env{user.state.<cid>.<condition>}
1.40      www      9421: sub directcondval {
                   9422:     my $number=shift;
1.620     albertel 9423:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 9424: 	&Apache::lonuserstate::evalstate();
                   9425:     }
1.713     albertel 9426:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   9427: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   9428:     } elsif ($number =~ /^_/) {
                   9429: 	my $sub_condition;
                   9430: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   9431: 		&GDBM_READER(),0640)) {
                   9432: 	    $sub_condition=$bighash{'conditions'.$number};
                   9433: 	    untie(%bighash);
                   9434: 	}
                   9435: 	my $value = &docondval($sub_condition);
1.949     raeburn  9436: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 9437: 	return $value;
                   9438:     }
1.620     albertel 9439:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   9440:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      9441:     } else {
                   9442:        return 2;
                   9443:     }
                   9444: }
                   9445: 
1.713     albertel 9446: # get the collection of conditions for this resource
1.26      www      9447: sub condval {
                   9448:     my $condidx=shift;
1.54      www      9449:     my $allpathcond='';
1.713     albertel 9450:     foreach my $cond (split(/\|/,$condidx)) {
                   9451: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   9452: 	    $allpathcond.=
                   9453: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   9454: 	}
1.191     harris41 9455:     }
1.54      www      9456:     $allpathcond=~s/\|$//;
1.713     albertel 9457:     return &docondval($allpathcond);
                   9458: }
                   9459: 
                   9460: #evaluates an expression of conditions
                   9461: sub docondval {
                   9462:     my ($allpathcond) = @_;
                   9463:     my $result=0;
                   9464:     if ($env{'request.course.id'}
                   9465: 	&& defined($allpathcond)) {
                   9466: 	my $operand='|';
                   9467: 	my @stack;
                   9468: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   9469: 	    if ($chunk eq '(') {
                   9470: 		push @stack,($operand,$result);
                   9471: 	    } elsif ($chunk eq ')') {
                   9472: 		my $before=pop @stack;
                   9473: 		if (pop @stack eq '&') {
                   9474: 		    $result=$result>$before?$before:$result;
                   9475: 		} else {
                   9476: 		    $result=$result>$before?$result:$before;
                   9477: 		}
                   9478: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   9479: 		$operand=$chunk;
                   9480: 	    } else {
                   9481: 		my $new=directcondval($chunk);
                   9482: 		if ($operand eq '&') {
                   9483: 		    $result=$result>$new?$new:$result;
                   9484: 		} else {
                   9485: 		    $result=$result>$new?$result:$new;
                   9486: 		}
                   9487: 	    }
                   9488: 	}
1.26      www      9489:     }
                   9490:     return $result;
1.421     albertel 9491: }
                   9492: 
                   9493: # ---------------------------------------------------- Devalidate courseresdata
                   9494: 
                   9495: sub devalidatecourseresdata {
                   9496:     my ($coursenum,$coursedomain)=@_;
                   9497:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 9498:     &devalidate_cache_new('courseres',$hashid);
1.28      www      9499: }
                   9500: 
1.763     www      9501: 
1.200     www      9502: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     9503: #
                   9504: #  Parameters:
                   9505: #      $coursenum    - Number of the course.
                   9506: #      $coursedomain - Domain at which the course was created.
                   9507: #  Returns:
                   9508: #     A hash of the course parameters along (I think) with timestamps
                   9509: #     and version info.
1.877     foxr     9510: 
1.624     albertel 9511: sub get_courseresdata {
                   9512:     my ($coursenum,$coursedomain)=@_;
1.200     www      9513:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   9514:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 9515:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 9516:     my %dumpreply;
1.417     albertel 9517:     unless (defined($cached)) {
1.624     albertel 9518: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 9519: 	$result=\%dumpreply;
1.251     albertel 9520: 	my ($tmp) = keys(%dumpreply);
                   9521: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 9522: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 9523: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   9524: 	    return $tmp;
1.416     albertel 9525: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 9526: 	    $result=undef;
1.599     albertel 9527: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 9528: 	}
                   9529:     }
1.624     albertel 9530:     return $result;
                   9531: }
                   9532: 
1.633     albertel 9533: sub devalidateuserresdata {
                   9534:     my ($uname,$udom)=@_;
                   9535:     my $hashid="$udom:$uname";
                   9536:     &devalidate_cache_new('userres',$hashid);
                   9537: }
                   9538: 
1.624     albertel 9539: sub get_userresdata {
                   9540:     my ($uname,$udom)=@_;
                   9541:     #most student don\'t have any data set, check if there is some data
                   9542:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   9543: 
                   9544:     my $hashid="$udom:$uname";
                   9545:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   9546:     if (!defined($cached)) {
                   9547: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   9548: 	$result=\%resourcedata;
                   9549: 	&do_cache_new('userres',$hashid,$result,600);
                   9550:     }
                   9551:     my ($tmp)=keys(%$result);
                   9552:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   9553: 	return $result;
                   9554:     }
                   9555:     #error 2 occurs when the .db doesn't exist
                   9556:     if ($tmp!~/error: 2 /) {
1.672     albertel 9557: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 9558: 		 " Trying to get resource data for ".
                   9559: 		 $uname." at ".$udom.": ".
                   9560: 		 $tmp."</font>");
                   9561:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 9562: 	#&EXT_cache_set($udom,$uname);
                   9563: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 9564: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 9565:     }
                   9566:     return $tmp;
                   9567: }
1.879     foxr     9568: #----------------------------------------------- resdata - return resource data
                   9569: #  Purpose:
                   9570: #    Return resource data for either users or for a course.
                   9571: #  Parameters:
                   9572: #     $name      - Course/user name.
                   9573: #     $domain    - Name of the domain the user/course is registered on.
                   9574: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   9575: #     @which     - Array of names of resources desired.
                   9576: #  Returns:
                   9577: #     The value of the first reasource in @which that is found in the
                   9578: #     resource hash.
                   9579: #  Exceptional Conditions:
                   9580: #     If the $type passed in is not valid (not the string 'course' or 
                   9581: #     'user', an undefined  reference is returned.
                   9582: #     If none of the resources are found, an undef is returned
1.624     albertel 9583: sub resdata {
                   9584:     my ($name,$domain,$type,@which)=@_;
                   9585:     my $result;
                   9586:     if ($type eq 'course') {
                   9587: 	$result=&get_courseresdata($name,$domain);
                   9588:     } elsif ($type eq 'user') {
                   9589: 	$result=&get_userresdata($name,$domain);
                   9590:     }
                   9591:     if (!ref($result)) { return $result; }    
1.251     albertel 9592:     foreach my $item (@which) {
1.927     albertel 9593: 	if (defined($result->{$item->[0]})) {
                   9594: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 9595: 	}
1.250     albertel 9596:     }
1.291     albertel 9597:     return undef;
1.200     www      9598: }
                   9599: 
1.379     matthew  9600: #
                   9601: # EXT resource caching routines
                   9602: #
                   9603: 
                   9604: sub clear_EXT_cache_status {
1.383     albertel 9605:     &delenv('cache.EXT.');
1.379     matthew  9606: }
                   9607: 
                   9608: sub EXT_cache_status {
                   9609:     my ($target_domain,$target_user) = @_;
1.383     albertel 9610:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 9611:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  9612:         # We know already the user has no data
                   9613:         return 1;
                   9614:     } else {
                   9615:         return 0;
                   9616:     }
                   9617: }
                   9618: 
                   9619: sub EXT_cache_set {
                   9620:     my ($target_domain,$target_user) = @_;
1.383     albertel 9621:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  9622:     #&appenv({$cachename => time});
1.379     matthew  9623: }
                   9624: 
1.28      www      9625: # --------------------------------------------------------- Value of a Variable
1.58      www      9626: sub EXT {
1.715     albertel 9627: 
1.1228    raeburn  9628:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse,$cid)=@_;
1.68      www      9629:     unless ($varname) { return ''; }
1.218     albertel 9630:     #get real user name/domain, courseid and symb
                   9631:     my $courseid;
1.359     albertel 9632:     my $publicuser;
1.427     www      9633:     if ($symbparm) {
                   9634: 	$symbparm=&get_symb_from_alias($symbparm);
                   9635:     }
1.218     albertel 9636:     if (!($uname && $udom)) {
1.790     albertel 9637:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 9638:       if (!$symbparm) {	$symbparm=$cursymb; }
                   9639:     } else {
1.620     albertel 9640: 	$courseid=$env{'request.course.id'};
1.218     albertel 9641:     }
1.48      www      9642:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   9643:     my $rest;
1.320     albertel 9644:     if (defined($therest[0])) {
1.48      www      9645:        $rest=join('.',@therest);
                   9646:     } else {
                   9647:        $rest='';
                   9648:     }
1.320     albertel 9649: 
1.57      www      9650:     my $qualifierrest=$qualifier;
                   9651:     if ($rest) { $qualifierrest.='.'.$rest; }
                   9652:     my $spacequalifierrest=$space;
                   9653:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      9654:     if ($realm eq 'user') {
1.48      www      9655: # --------------------------------------------------------------- user.resource
                   9656: 	if ($space eq 'resource') {
1.651     albertel 9657: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   9658: 		  || defined($Apache::lonhomework::parsing_a_task))
                   9659: 		 &&
1.744     albertel 9660: 		 ($symbparm eq &symbread()) ) {	
                   9661: 		# if we are in the middle of processing the resource the
                   9662: 		# get the value we are planning on committing
                   9663:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   9664:                     return $Apache::lonhomework::results{$qualifierrest};
                   9665:                 } else {
                   9666:                     return $Apache::lonhomework::history{$qualifierrest};
                   9667:                 }
1.335     albertel 9668: 	    } else {
1.359     albertel 9669: 		my %restored;
1.620     albertel 9670: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 9671: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   9672: 		} else {
                   9673: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   9674: 		}
1.335     albertel 9675: 		return $restored{$qualifierrest};
                   9676: 	    }
1.48      www      9677: # ----------------------------------------------------------------- user.access
                   9678:         } elsif ($space eq 'access') {
1.218     albertel 9679: 	    # FIXME - not supporting calls for a specific user
1.48      www      9680:             return &allowed($qualifier,$rest);
                   9681: # ------------------------------------------ user.preferences, user.environment
                   9682:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 9683: 	    if (($uname eq $env{'user.name'}) &&
                   9684: 		($udom eq $env{'user.domain'})) {
                   9685: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 9686: 	    } else {
1.359     albertel 9687: 		my %returnhash;
                   9688: 		if (!$publicuser) {
                   9689: 		    %returnhash=&userenvironment($udom,$uname,
                   9690: 						 $qualifierrest);
                   9691: 		}
1.218     albertel 9692: 		return $returnhash{$qualifierrest};
                   9693: 	    }
1.48      www      9694: # ----------------------------------------------------------------- user.course
                   9695:         } elsif ($space eq 'course') {
1.218     albertel 9696: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 9697:             return $env{join('.',('request.course',$qualifier))};
1.48      www      9698: # ------------------------------------------------------------------- user.role
                   9699:         } elsif ($space eq 'role') {
1.218     albertel 9700: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 9701:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      9702:             if ($qualifier eq 'value') {
                   9703: 		return $role;
                   9704:             } elsif ($qualifier eq 'extent') {
                   9705:                 return $where;
                   9706:             }
                   9707: # ----------------------------------------------------------------- user.domain
                   9708:         } elsif ($space eq 'domain') {
1.218     albertel 9709:             return $udom;
1.48      www      9710: # ------------------------------------------------------------------- user.name
                   9711:         } elsif ($space eq 'name') {
1.218     albertel 9712:             return $uname;
1.48      www      9713: # ---------------------------------------------------- Any other user namespace
1.29      www      9714:         } else {
1.359     albertel 9715: 	    my %reply;
                   9716: 	    if (!$publicuser) {
                   9717: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   9718: 	    }
                   9719: 	    return $reply{$qualifierrest};
1.48      www      9720:         }
1.236     www      9721:     } elsif ($realm eq 'query') {
                   9722: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 9723:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   9724: 						[$spacequalifierrest]);
1.620     albertel 9725: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      9726:    } elsif ($realm eq 'request') {
1.48      www      9727: # ------------------------------------------------------------- request.browser
                   9728:         if ($space eq 'browser') {
1.1145    bisitz   9729:             return $env{'browser.'.$qualifier};
1.57      www      9730: # ------------------------------------------------------------ request.filename
                   9731:         } else {
1.620     albertel 9732:             return $env{'request.'.$spacequalifierrest};
1.29      www      9733:         }
1.28      www      9734:     } elsif ($realm eq 'course') {
1.48      www      9735: # ---------------------------------------------------------- course.description
1.620     albertel 9736:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      9737:     } elsif ($realm eq 'resource') {
1.165     www      9738: 
1.620     albertel 9739: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 9740: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   9741: 	}
1.693     albertel 9742: 
                   9743: 	if ($space eq 'title') {
                   9744: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   9745: 	    return &gettitle($symbparm);
                   9746: 	}
                   9747: 	
                   9748: 	if ($space eq 'map') {
                   9749: 	    my ($map) = &decode_symb($symbparm);
                   9750: 	    return &symbread($map);
                   9751: 	}
1.905     albertel 9752: 	if ($space eq 'filename') {
                   9753: 	    if ($symbparm) {
                   9754: 		return &clutter((&decode_symb($symbparm))[2]);
                   9755: 	    }
                   9756: 	    return &hreflocation('',$env{'request.filename'});
                   9757: 	}
1.693     albertel 9758: 
                   9759: 	my ($section, $group, @groups);
1.593     albertel 9760: 	my ($courselevelm,$courselevel);
1.1228    raeburn  9761:         if (($courseid eq '') && ($cid)) {
                   9762:             $courseid = $cid;
                   9763:         }
                   9764: 	if (($symbparm && $courseid) && 
                   9765: 	    (($courseid eq $env{'request.course.id'}) || ($courseid eq $cid)))  {
1.165     www      9766: 
1.218     albertel 9767: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      9768: 
1.60      www      9769: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 9770: 	    my $symbp=$symbparm;
1.735     albertel 9771: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 9772: 
                   9773: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   9774: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   9775: 
1.620     albertel 9776: 	    if (($env{'user.name'} eq $uname) &&
                   9777: 		($env{'user.domain'} eq $udom)) {
                   9778: 		$section=$env{'request.course.sec'};
1.733     raeburn  9779:                 @groups = split(/:/,$env{'request.course.groups'});  
                   9780:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 9781: 	    } else {
1.539     albertel 9782: 		if (! defined($usection)) {
1.551     albertel 9783: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 9784: 		} else {
                   9785: 		    $section = $usection;
                   9786: 		}
1.733     raeburn  9787:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 9788: 	    }
                   9789: 
                   9790: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   9791: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   9792: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   9793: 
1.593     albertel 9794: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 9795: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 9796: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      9797: 
1.60      www      9798: # ----------------------------------------------------------- first, check user
1.624     albertel 9799: 
                   9800: 	    my $userreply=&resdata($uname,$udom,'user',
1.927     albertel 9801: 				       ([$courselevelr,'resource'],
                   9802: 					[$courselevelm,'map'     ],
                   9803: 					[$courselevel, 'course'  ]));
1.931     albertel 9804: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      9805: 
1.594     albertel 9806: # ------------------------------------------------ second, check some of course
1.684     raeburn  9807:             my $coursereply;
1.691     raeburn  9808:             if (@groups > 0) {
                   9809:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   9810:                                        $mapparm,$spacequalifierrest);
1.927     albertel 9811:                 if (defined($coursereply)) { return &get_reply($coursereply); }
1.684     raeburn  9812:             }
1.96      www      9813: 
1.684     raeburn  9814: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 9815: 				  $env{'course.'.$courseid.'.domain'},
                   9816: 				  'course',
                   9817: 				  ([$seclevelr,   'resource'],
                   9818: 				   [$seclevelm,   'map'     ],
                   9819: 				   [$seclevel,    'course'  ],
                   9820: 				   [$courselevelr,'resource']));
                   9821: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      9822: 
1.60      www      9823: # ------------------------------------------------------ third, check map parms
1.218     albertel 9824: 	    my %parmhash=();
                   9825: 	    my $thisparm='';
                   9826: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 9827: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 9828: 		    &GDBM_READER(),0640)) {
1.218     albertel 9829: 		$thisparm=$parmhash{$symbparm};
                   9830: 		untie(%parmhash);
                   9831: 	    }
1.927     albertel 9832: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 9833: 	}
1.594     albertel 9834: # ------------------------------------------ fourth, look in resource metadata
1.71      www      9835: 
1.218     albertel 9836: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 9837: 	my $filename;
                   9838: 	if (!$symbparm) { $symbparm=&symbread(); }
                   9839: 	if ($symbparm) {
1.409     www      9840: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 9841: 	} else {
1.620     albertel 9842: 	    $filename=$env{'request.filename'};
1.282     albertel 9843: 	}
                   9844: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.927     albertel 9845: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282     albertel 9846: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927     albertel 9847: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      9848: 
1.927     albertel 9849: # ---------------------------------------------- fourth, look in rest of course
1.593     albertel 9850: 	if ($symbparm && defined($courseid) && 
1.620     albertel 9851: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 9852: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   9853: 				     $env{'course.'.$courseid.'.domain'},
                   9854: 				     'course',
1.927     albertel 9855: 				     ([$courselevelm,'map'   ],
                   9856: 				      [$courselevel, 'course']));
                   9857: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 9858: 	}
1.145     www      9859: # ------------------------------------------------------------------ Cascade up
1.218     albertel 9860: 	unless ($space eq '0') {
1.336     albertel 9861: 	    my @parts=split(/_/,$space);
                   9862: 	    my $id=pop(@parts);
                   9863: 	    my $part=join('_',@parts);
                   9864: 	    if ($part eq '') { $part='0'; }
1.927     albertel 9865: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 9866: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  9867: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 9868: 	}
1.395     albertel 9869: 	if ($recurse) { return undef; }
                   9870: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 9871: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      9872: # ---------------------------------------------------- Any other user namespace
                   9873:     } elsif ($realm eq 'environment') {
                   9874: # ----------------------------------------------------------------- environment
1.620     albertel 9875: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   9876: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 9877: 	} else {
1.770     albertel 9878: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   9879: 		return '';
                   9880: 	    }
1.219     albertel 9881: 	    my %returnhash=&userenvironment($udom,$uname,
                   9882: 					    $spacequalifierrest);
                   9883: 	    return $returnhash{$spacequalifierrest};
                   9884: 	}
1.28      www      9885:     } elsif ($realm eq 'system') {
1.48      www      9886: # ----------------------------------------------------------------- system.time
                   9887: 	if ($space eq 'time') {
                   9888: 	    return time;
                   9889:         }
1.696     albertel 9890:     } elsif ($realm eq 'server') {
                   9891: # ----------------------------------------------------------------- system.time
                   9892: 	if ($space eq 'name') {
                   9893: 	    return $ENV{'SERVER_NAME'};
                   9894:         }
1.28      www      9895:     }
1.48      www      9896:     return '';
1.61      www      9897: }
                   9898: 
1.927     albertel 9899: sub get_reply {
                   9900:     my ($reply_value) = @_;
1.940     raeburn  9901:     if (ref($reply_value) eq 'ARRAY') {
                   9902:         if (wantarray) {
                   9903: 	    return @$reply_value;
                   9904:         }
                   9905:         return $reply_value->[0];
                   9906:     } else {
                   9907:         return $reply_value;
1.927     albertel 9908:     }
                   9909: }
                   9910: 
1.691     raeburn  9911: sub check_group_parms {
                   9912:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   9913:     my @groupitems = ();
                   9914:     my $resultitem;
1.927     albertel 9915:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691     raeburn  9916:     foreach my $group (@{$groups}) {
                   9917:         foreach my $level (@levels) {
1.927     albertel 9918:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   9919:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  9920:         }
                   9921:     }
                   9922:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   9923:                             $env{'course.'.$courseid.'.domain'},
                   9924:                                      'course',@groupitems);
                   9925:     return $coursereply;
                   9926: }
                   9927: 
                   9928: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  9929:     my ($courseid,@groups) = @_;
                   9930:     @groups = sort(@groups);
1.691     raeburn  9931:     return @groups;
                   9932: }
                   9933: 
1.395     albertel 9934: sub packages_tab_default {
                   9935:     my ($uri,$varname)=@_;
                   9936:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 9937: 
                   9938:     my (@extension,@specifics,$do_default);
                   9939:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 9940: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 9941: 	if ($pack_type eq 'default') {
                   9942: 	    $do_default=1;
                   9943: 	} elsif ($pack_type eq 'extension') {
                   9944: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 9945: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 9946: 	    # only look at packages defaults for packages that this id is
1.738     albertel 9947: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   9948: 	}
                   9949:     }
                   9950:     # first look for a package that matches the requested part id
                   9951:     foreach my $package (@specifics) {
                   9952: 	my (undef,$pack_type,$pack_part)=@{$package};
                   9953: 	next if ($pack_part ne $part);
                   9954: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   9955: 	    return $packagetab{"$pack_type&$name&default"};
                   9956: 	}
                   9957:     }
                   9958:     # look for any possible matching non extension_ package
                   9959:     foreach my $package (@specifics) {
                   9960: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 9961: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   9962: 	    return $packagetab{"$pack_type&$name&default"};
                   9963: 	}
1.585     albertel 9964: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 9965: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   9966: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 9967: 	}
                   9968:     }
1.738     albertel 9969:     # look for any posible extension_ match
                   9970:     foreach my $package (@extension) {
                   9971: 	my ($package,$pack_type)=@{$package};
                   9972: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   9973: 	    return $packagetab{"$pack_type&$name&default"};
                   9974: 	}
                   9975: 	if (defined($packagetab{$package."&$name&default"})) {
                   9976: 	    return $packagetab{$package."&$name&default"};
                   9977: 	}
                   9978:     }
                   9979:     # look for a global default setting
                   9980:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   9981: 	return $packagetab{"default&$name&default"};
                   9982:     }
1.395     albertel 9983:     return undef;
                   9984: }
                   9985: 
1.334     albertel 9986: sub add_prefix_and_part {
                   9987:     my ($prefix,$part)=@_;
                   9988:     my $keyroot;
                   9989:     if (defined($prefix) && $prefix !~ /^__/) {
                   9990: 	# prefix that has a part already
                   9991: 	$keyroot=$prefix;
                   9992:     } elsif (defined($prefix)) {
                   9993: 	# prefix that is missing a part
                   9994: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   9995:     } else {
                   9996: 	# no prefix at all
                   9997: 	if (defined($part)) { $keyroot='_'.$part; }
                   9998:     }
                   9999:     return $keyroot;
                   10000: }
                   10001: 
1.71      www      10002: # ---------------------------------------------------------------- Get metadata
                   10003: 
1.599     albertel 10004: my %metaentry;
1.1070    www      10005: my %importedpartids;
1.71      www      10006: sub metadata {
1.176     www      10007:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      10008:     $uri=&declutter($uri);
1.288     albertel 10009:     # if it is a non metadata possible uri return quickly
1.529     albertel 10010:     if (($uri eq '') || 
                   10011: 	(($uri =~ m|^/*adm/|) && 
1.1217    raeburn  10012: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m{/(smppg|bulletinboard)$})) ||
1.1108    raeburn  10013:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 10014: 	return undef;
                   10015:     }
1.1140    www      10016:     if (($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) 
1.924     albertel 10017: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 10018: 	return undef;
1.288     albertel 10019:     }
1.73      www      10020:     my $filename=$uri;
                   10021:     $uri=~s/\.meta$//;
1.172     www      10022: #
                   10023: # Is the metadata already cached?
1.177     www      10024: # Look at timestamp of caching
1.172     www      10025: # Everything is cached by the main uri, libraries are never directly cached
                   10026: #
1.428     albertel 10027:     if (!defined($liburi)) {
1.599     albertel 10028: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 10029: 	if (defined($cached)) { return $result->{':'.$what}; }
                   10030:     }
                   10031:     {
1.1069    www      10032: # Imported parts would go here
1.1070    www      10033:         my %importedids=();
                   10034:         my @origfileimportpartids=();
1.1069    www      10035:         my $importedparts=0;
1.172     www      10036: #
                   10037: # Is this a recursive call for a library?
                   10038: #
1.599     albertel 10039: #	if (! exists($metacache{$uri})) {
                   10040: #	    $metacache{$uri}={};
                   10041: #	}
1.924     albertel 10042: 	my $cachetime = 60*60;
1.171     www      10043:         if ($liburi) {
                   10044: 	    $liburi=&declutter($liburi);
                   10045:             $filename=$liburi;
1.401     bowersj2 10046:         } else {
1.599     albertel 10047: 	    &devalidate_cache_new('meta',$uri);
                   10048: 	    undef(%metaentry);
1.401     bowersj2 10049: 	}
1.140     www      10050:         my %metathesekeys=();
1.73      www      10051:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 10052: 	my $metastring;
1.1140    www      10053: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 10054: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 10055: 	    $metastring = 
1.929     albertel 10056: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 10057: 					  ('grade_target' => 'meta'));
                   10058: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  10059: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   10060:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 10061: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 10062: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 10063: 	    $metastring=&getfile($file);
1.489     albertel 10064: 	}
1.208     albertel 10065:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      10066:         my $token;
1.140     www      10067:         undef %metathesekeys;
1.71      www      10068:         while ($token=$parser->get_token) {
1.339     albertel 10069: 	    if ($token->[0] eq 'S') {
                   10070: 		if (defined($token->[2]->{'package'})) {
1.172     www      10071: #
                   10072: # This is a package - get package info
                   10073: #
1.339     albertel 10074: 		    my $package=$token->[2]->{'package'};
                   10075: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   10076: 		    if (defined($token->[2]->{'id'})) { 
                   10077: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   10078: 		    }
1.599     albertel 10079: 		    if ($metaentry{':packages'}) {
                   10080: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 10081: 		    } else {
1.599     albertel 10082: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 10083: 		    }
1.736     albertel 10084: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 10085: 			my $part=$keyroot;
                   10086: 			$part=~s/^\_//;
1.736     albertel 10087: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   10088: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   10089: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 10090: 			    # ignore package.tab specified default values
                   10091:                             # here &package_tab_default() will fetch those
                   10092: 			    if ($subp eq 'default') { next; }
1.736     albertel 10093: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 10094: 			    my $unikey;
                   10095: 			    if ($pack =~ /_0$/) {
                   10096: 				$unikey='parameter_0_'.$name;
                   10097: 				$part=0;
                   10098: 			    } else {
                   10099: 				$unikey='parameter'.$keyroot.'_'.$name;
                   10100: 			    }
1.339     albertel 10101: 			    if ($subp eq 'display') {
                   10102: 				$value.=' [Part: '.$part.']';
                   10103: 			    }
1.599     albertel 10104: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 10105: 			    $metathesekeys{$unikey}=1;
1.599     albertel 10106: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   10107: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 10108: 			    }
1.599     albertel 10109: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   10110: 				$metaentry{':'.$unikey}=
                   10111: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 10112: 			    }
1.339     albertel 10113: 			}
                   10114: 		    }
                   10115: 		} else {
1.172     www      10116: #
                   10117: # This is not a package - some other kind of start tag
1.339     albertel 10118: #
                   10119: 		    my $entry=$token->[1];
1.1068    www      10120: 		    my $unikey='';
1.175     www      10121: 
1.339     albertel 10122: 		    if ($entry eq 'import') {
1.175     www      10123: #
                   10124: # Importing a library here
1.339     albertel 10125: #
1.1067    www      10126:                         my $location=$parser->get_text('/import');
                   10127:                         my $dir=$filename;
                   10128:                         $dir=~s|[^/]*$||;
                   10129:                         $location=&filelocation($dir,$location);
1.1069    www      10130:                        
1.1068    www      10131:                         my $importmode=$token->[2]->{'importmode'};
                   10132:                         if ($importmode eq 'problem') {
1.1069    www      10133: # Import as problem/response
1.1068    www      10134:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   10135:                         } elsif ($importmode eq 'part') {
                   10136: # Import as part(s)
1.1069    www      10137:                            $importedparts=1;
                   10138: # We need to get the original file and the imported file to get the part order correct
                   10139: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
                   10140: # Load and inspect original file
1.1070    www      10141:                            if ($#origfileimportpartids<0) {
                   10142:                               undef(%importedpartids);
                   10143:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   10144:                               my $origfile=&getfile($origfilelocation);
                   10145:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   10146:                            }
                   10147: 
1.1069    www      10148: # Load and inspect imported file
                   10149:                            my $impfile=&getfile($location);
                   10150:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   10151:                            if ($#impfilepartids>=0) {
                   10152: # This problem had parts
1.1070    www      10153:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      10154:                            } else {
                   10155: # Importing by turning a single problem into a problem part
                   10156: # It gets the import-tags ID as part-ID
                   10157:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      10158:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      10159:                            }
1.1068    www      10160:                         } else {
                   10161: # Normal import
                   10162:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   10163:                            if (defined($token->[2]->{'id'})) {
                   10164:                               $unikey.='_'.$token->[2]->{'id'};
                   10165:                            }
1.1067    www      10166:                         }
                   10167: 
1.339     albertel 10168: 			if ($depthcount<20) {
1.736     albertel 10169: 			    my $metadata = 
                   10170: 				&metadata($uri,'keys', $location,$unikey,
                   10171: 					  $depthcount+1);
                   10172: 			    foreach my $meta (split(',',$metadata)) {
                   10173: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   10174: 				$metathesekeys{$meta}=1;
1.339     albertel 10175: 			    }
1.1068    www      10176: 			
                   10177:                         }
1.1067    www      10178: 		    } else {
                   10179: #
                   10180: # Not importing, some other kind of non-package, non-library start tag
                   10181: # 
                   10182:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   10183:                         if (defined($token->[2]->{'id'})) {
                   10184:                             $unikey.='_'.$token->[2]->{'id'};
                   10185:                         }
1.339     albertel 10186: 			if (defined($token->[2]->{'name'})) { 
                   10187: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   10188: 			}
                   10189: 			$metathesekeys{$unikey}=1;
1.736     albertel 10190: 			foreach my $param (@{$token->[3]}) {
                   10191: 			    $metaentry{':'.$unikey.'.'.$param} =
                   10192: 				$token->[2]->{$param};
1.339     albertel 10193: 			}
                   10194: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 10195: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 10196: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   10197: 		 # only ws inside the tag, and not in default, so use default
                   10198: 		 # as value
1.599     albertel 10199: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 10200: 			} elsif ( $internaltext =~ /\S/ ) {
                   10201: 		  # something interesting inside the tag
                   10202: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 10203: 			} else {
1.908     albertel 10204: 		  # no interesting values, don't set a default
1.339     albertel 10205: 			}
1.172     www      10206: # end of not-a-package not-a-library import
1.339     albertel 10207: 		    }
1.172     www      10208: # end of not-a-package start tag
1.339     albertel 10209: 		}
1.172     www      10210: # the next is the end of "start tag"
1.339     albertel 10211: 	    }
                   10212: 	}
1.483     albertel 10213: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 10214: 	$extension = lc($extension);
                   10215: 	if ($extension eq 'htm') { $extension='html'; }
                   10216: 
1.737     albertel 10217: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 10218: 	    #no specific packages #how's our extension
                   10219: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 10220: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 10221: 					 \%metathesekeys);
                   10222: 	}
1.883     albertel 10223: 
                   10224: 	if (!exists($metaentry{':packages'})
                   10225: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 10226: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 10227: 		#no specific packages well let's get default then
                   10228: 		if ($key!~/^default&/) { next; }
1.488     albertel 10229: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 10230: 					     \%metathesekeys);
                   10231: 	    }
                   10232: 	}
1.338     www      10233: # are there custom rights to evaluate
1.599     albertel 10234: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 10235: 
1.338     www      10236:     #
                   10237:     # Importing a rights file here
1.339     albertel 10238:     #
                   10239: 	    unless ($depthcount) {
1.599     albertel 10240: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 10241: 		my $dir=$filename;
                   10242: 		$dir=~s|[^/]*$||;
                   10243: 		$location=&filelocation($dir,$location);
1.736     albertel 10244: 		my $rights_metadata =
                   10245: 		    &metadata($uri,'keys',$location,'_rights',
                   10246: 			      $depthcount+1);
                   10247: 		foreach my $rights (split(',',$rights_metadata)) {
                   10248: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   10249: 		    $metathesekeys{$rights}=1;
1.339     albertel 10250: 		}
                   10251: 	    }
                   10252: 	}
1.737     albertel 10253: 	# uniqifiy package listing
                   10254: 	my %seen;
                   10255: 	my @uniq_packages =
                   10256: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   10257: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   10258: 
1.1070    www      10259:         if ($importedparts) {
                   10260: # We had imported parts and need to rebuild partorder
                   10261:            $metaentry{':partorder'}='';
                   10262:            $metathesekeys{'partorder'}=1;
                   10263:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
                   10264:                if ($origfileimportpartids[$index] eq 'part') {
                   10265: # original part, part of the problem
                   10266:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
                   10267:                } else {
                   10268: # we have imported parts at this position
                   10269:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
                   10270:                }
                   10271:            }
                   10272:            $metaentry{':partorder'}=~s/^\,//;
                   10273:         }
                   10274: 
1.737     albertel 10275: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 10276: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   10277: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924     albertel 10278: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      10279: # this is the end of "was not already recently cached
1.71      www      10280:     }
1.599     albertel 10281:     return $metaentry{':'.$what};
1.261     albertel 10282: }
                   10283: 
1.488     albertel 10284: sub metadata_create_package_def {
1.483     albertel 10285:     my ($uri,$key,$package,$metathesekeys)=@_;
                   10286:     my ($pack,$name,$subp)=split(/\&/,$key);
                   10287:     if ($subp eq 'default') { next; }
                   10288:     
1.599     albertel 10289:     if (defined($metaentry{':packages'})) {
                   10290: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 10291:     } else {
1.599     albertel 10292: 	$metaentry{':packages'}=$package;
1.483     albertel 10293:     }
                   10294:     my $value=$packagetab{$key};
                   10295:     my $unikey;
                   10296:     $unikey='parameter_0_'.$name;
1.599     albertel 10297:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 10298:     $$metathesekeys{$unikey}=1;
1.599     albertel 10299:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   10300: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 10301:     }
1.599     albertel 10302:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   10303: 	$metaentry{':'.$unikey}=
                   10304: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 10305:     }
                   10306: }
                   10307: 
1.261     albertel 10308: sub metadata_generate_part0 {
                   10309:     my ($metadata,$metacache,$uri) = @_;
                   10310:     my %allnames;
1.737     albertel 10311:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 10312: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 10313: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   10314: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 10315: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 10316: 	    $allnames{$name}=$part;
                   10317: 	  }
                   10318: 	}
                   10319:     }
                   10320:     foreach my $name (keys(%allnames)) {
                   10321:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 10322:       my $key=":parameter_0_$name";
1.261     albertel 10323:       $$metacache{"$key.part"}='0';
                   10324:       $$metacache{"$key.name"}=$name;
1.428     albertel 10325:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 10326: 					   $allnames{$name}.'_'.$name.
                   10327: 					   '.type'};
1.428     albertel 10328:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 10329: 			     '.display'};
1.644     www      10330:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 10331:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 10332:       $$metacache{"$key.display"}=$olddis;
                   10333:     }
1.71      www      10334: }
                   10335: 
1.764     albertel 10336: # ------------------------------------------------------ Devalidate title cache
                   10337: 
                   10338: sub devalidate_title_cache {
                   10339:     my ($url)=@_;
                   10340:     if (!$env{'request.course.id'}) { return; }
                   10341:     my $symb=&symbread($url);
                   10342:     if (!$symb) { return; }
                   10343:     my $key=$env{'request.course.id'}."\0".$symb;
                   10344:     &devalidate_cache_new('title',$key);
                   10345: }
                   10346: 
1.1014    droeschl 10347: # ------------------------------------------------- Get the title of a course
                   10348: 
                   10349: sub current_course_title {
                   10350:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   10351: }
1.301     www      10352: # ------------------------------------------------- Get the title of a resource
                   10353: 
                   10354: sub gettitle {
                   10355:     my $urlsymb=shift;
                   10356:     my $symb=&symbread($urlsymb);
1.534     albertel 10357:     if ($symb) {
1.620     albertel 10358: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 10359: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 10360: 	if (defined($cached)) { 
                   10361: 	    return $result;
                   10362: 	}
1.534     albertel 10363: 	my ($map,$resid,$url)=&decode_symb($symb);
                   10364: 	my $title='';
1.907     albertel 10365: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   10366: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   10367: 	} else {
                   10368: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   10369: 		    &GDBM_READER(),0640)) {
                   10370: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   10371: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   10372: 		untie(%bighash);
                   10373: 	    }
1.534     albertel 10374: 	}
                   10375: 	$title=~s/\&colon\;/\:/gs;
                   10376: 	if ($title) {
1.1159    www      10377: # Remember both $symb and $title for dynamic metadata
                   10378:             $accesshash{$symb.'___crstitle'}=$title;
1.1161    www      10379:             $accesshash{&declutter($map).'___'.&declutter($url).'___usage'}=time;
1.1159    www      10380: # Cache this title and then return it
1.599     albertel 10381: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 10382: 	}
                   10383: 	$urlsymb=$url;
                   10384:     }
                   10385:     my $title=&metadata($urlsymb,'title');
                   10386:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   10387:     return $title;
1.301     www      10388: }
1.613     albertel 10389: 
1.614     albertel 10390: sub get_slot {
                   10391:     my ($which,$cnum,$cdom)=@_;
                   10392:     if (!$cnum || !$cdom) {
1.790     albertel 10393: 	(undef,my $courseid)=&whichuser();
1.620     albertel 10394: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   10395: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 10396:     }
1.703     albertel 10397:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   10398:     my %slotinfo;
                   10399:     if (exists($remembered{$key})) {
                   10400: 	$slotinfo{$which} = $remembered{$key};
                   10401:     } else {
                   10402: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   10403: 	&Apache::lonhomework::showhash(%slotinfo);
                   10404: 	my ($tmp)=keys(%slotinfo);
                   10405: 	if ($tmp=~/^error:/) { return (); }
                   10406: 	$remembered{$key} = $slotinfo{$which};
                   10407:     }
1.616     albertel 10408:     if (ref($slotinfo{$which}) eq 'HASH') {
                   10409: 	return %{$slotinfo{$which}};
                   10410:     }
                   10411:     return $slotinfo{$which};
1.614     albertel 10412: }
1.1150    raeburn  10413: 
                   10414: sub get_reservable_slots {
                   10415:     my ($cnum,$cdom,$uname,$udom) = @_;
                   10416:     my $now = time;
                   10417:     my $reservable_info;
                   10418:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   10419:     if (exists($remembered{$key})) {
                   10420:         $reservable_info = $remembered{$key};
                   10421:     } else {
                   10422:         my %resv;
                   10423:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   10424:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   10425:         $reservable_info = \%resv;
                   10426:         $remembered{$key} = $reservable_info;
                   10427:     }
                   10428:     return $reservable_info;
                   10429: }
                   10430: 
                   10431: sub get_course_slots {
                   10432:     my ($cnum,$cdom) = @_;
                   10433:     my $hashid=$cnum.':'.$cdom;
                   10434:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   10435:     if (defined($cached)) {
                   10436:         if (ref($result) eq 'HASH') {
                   10437:             return %{$result};
                   10438:         }
                   10439:     } else {
                   10440:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   10441:         my ($tmp) = keys(%slots);
                   10442:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.1219    raeburn  10443:             &do_cache_new('allslots',$hashid,\%slots,600);
1.1150    raeburn  10444:             return %slots;
                   10445:         }
                   10446:     }
                   10447:     return;
                   10448: }
                   10449: 
                   10450: sub devalidate_slots_cache {
                   10451:     my ($cnum,$cdom)=@_;
                   10452:     my $hashid=$cnum.':'.$cdom;
                   10453:     &devalidate_cache_new('allslots',$hashid);
                   10454: }
                   10455: 
1.1181    raeburn  10456: sub get_coursechange {
                   10457:     my ($cdom,$cnum) = @_;
                   10458:     if ($cdom eq '' || $cnum eq '') {
                   10459:         return unless ($env{'request.course.id'});
                   10460:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   10461:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   10462:     }
                   10463:     my $hashid=$cdom.'_'.$cnum;
                   10464:     my ($change,$cached)=&is_cached_new('crschange',$hashid);
                   10465:     if ((defined($cached)) && ($change ne '')) {
                   10466:         return $change;
                   10467:     } else {
                   10468:         my %crshash;
                   10469:         %crshash = &get('environment',['internal.contentchange'],$cdom,$cnum);
                   10470:         if ($crshash{'internal.contentchange'} eq '') {
                   10471:             $change = $env{'course.'.$cdom.'_'.$cnum.'.internal.created'};
                   10472:             if ($change eq '') {
                   10473:                 %crshash = &get('environment',['internal.created'],$cdom,$cnum);
                   10474:                 $change = $crshash{'internal.created'};
                   10475:             }
                   10476:         } else {
                   10477:             $change = $crshash{'internal.contentchange'};
                   10478:         }
                   10479:         my $cachetime = 600;
                   10480:         &do_cache_new('crschange',$hashid,$change,$cachetime);
                   10481:     }
                   10482:     return $change;
                   10483: }
                   10484: 
                   10485: sub devalidate_coursechange_cache {
                   10486:     my ($cnum,$cdom)=@_;
                   10487:     my $hashid=$cnum.':'.$cdom;
                   10488:     &devalidate_cache_new('crschange',$hashid);
                   10489: }
                   10490: 
1.31      www      10491: # ------------------------------------------------- Update symbolic store links
                   10492: 
                   10493: sub symblist {
                   10494:     my ($mapname,%newhash)=@_;
1.438     www      10495:     $mapname=&deversion(&declutter($mapname));
1.31      www      10496:     my %hash;
1.620     albertel 10497:     if (($env{'request.course.fn'}) && (%newhash)) {
                   10498:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 10499:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  10500: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 10501: 		next if ($url eq 'last_known'
                   10502: 			 && $env{'form.no_update_last_known'});
                   10503: 		$hash{declutter($url)}=&encode_symb($mapname,
                   10504: 						    $newhash{$url}->[1],
                   10505: 						    $newhash{$url}->[0]);
1.191     harris41 10506:             }
1.31      www      10507:             if (untie(%hash)) {
                   10508: 		return 'ok';
                   10509:             }
                   10510:         }
                   10511:     }
                   10512:     return 'error';
1.212     www      10513: }
                   10514: 
                   10515: # --------------------------------------------------------------- Verify a symb
                   10516: 
                   10517: sub symbverify {
1.1190    raeburn  10518:     my ($symb,$thisurl,$encstate)=@_;
1.510     www      10519:     my $thisfn=$thisurl;
1.439     www      10520:     $thisfn=&declutter($thisfn);
1.215     www      10521: # direct jump to resource in page or to a sequence - will construct own symbs
                   10522:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   10523: # check URL part
1.409     www      10524:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      10525: 
1.431     www      10526:     unless ($url eq $thisfn) { return 0; }
1.213     www      10527: 
1.216     www      10528:     $symb=&symbclean($symb);
1.510     www      10529:     $thisurl=&deversion($thisurl);
1.439     www      10530:     $thisfn=&deversion($thisfn);
1.213     www      10531: 
                   10532:     my %bighash;
                   10533:     my $okay=0;
1.431     www      10534: 
1.620     albertel 10535:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 10536:                             &GDBM_READER(),0640)) {
1.1204    raeburn  10537:         my $noclutter;
1.1032    raeburn  10538:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   10539:             $thisurl =~ s/\?.+$//;
1.1204    raeburn  10540:             if ($map =~ m{^uploaded/.+\.page$}) {
                   10541:                 $thisurl =~ s{^(/adm/wrapper|)/ext/}{http://};
                   10542:                 $thisurl =~ s{^\Qhttp://https://\E}{https://};
                   10543:                 $noclutter = 1;
                   10544:             }
                   10545:         }
                   10546:         my $ids;
                   10547:         if ($noclutter) {
                   10548:             $ids=$bighash{'ids_'.$thisurl};
                   10549:         } else {
                   10550:             $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1032    raeburn  10551:         }
1.1102    raeburn  10552:         unless ($ids) {
                   10553:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   10554:             $ids=$bighash{$idkey};
1.216     www      10555:         }
                   10556:         if ($ids) {
                   10557: # ------------------------------------------------------------------- Has ID(s)
1.1202    raeburn  10558:             if ($thisfn =~ m{^/adm/wrapper/ext/}) {
1.1203    raeburn  10559:                 $symb =~ s/\?.+$//;
1.1202    raeburn  10560:             }
1.800     albertel 10561: 	    foreach my $id (split(/\,/,$ids)) {
                   10562: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      10563:                if (
                   10564:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
1.1190    raeburn  10565:    eq $symb) {
                   10566:                    if (ref($encstate)) {
                   10567:                        $$encstate = $bighash{'encrypted_'.$id};
                   10568:                    }
1.620     albertel 10569: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  10570: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   10571:                        ($thisurl eq '/adm/navmaps')) {
1.1190    raeburn  10572: 		       $okay=1;
1.1202    raeburn  10573:                        last;
1.582     albertel 10574: 		   }
                   10575: 	       }
1.216     www      10576: 	   }
                   10577:         }
1.213     www      10578: 	untie(%bighash);
                   10579:     }
                   10580:     return $okay;
1.31      www      10581: }
                   10582: 
1.210     www      10583: # --------------------------------------------------------------- Clean-up symb
                   10584: 
                   10585: sub symbclean {
                   10586:     my $symb=shift;
1.568     albertel 10587:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      10588: # remove version from map
                   10589:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      10590: 
1.210     www      10591: # remove version from URL
                   10592:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      10593: 
1.507     www      10594: # remove wrapper
                   10595: 
1.510     www      10596:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 10597:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      10598:     return $symb;
1.409     www      10599: }
                   10600: 
                   10601: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 10602: 
                   10603: sub encode_symb {
                   10604:     my ($map,$resid,$url)=@_;
                   10605:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   10606: }
1.409     www      10607: 
                   10608: sub decode_symb {
1.568     albertel 10609:     my $symb=shift;
                   10610:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   10611:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      10612:     return (&fixversion($map),$resid,&fixversion($url));
                   10613: }
                   10614: 
                   10615: sub fixversion {
                   10616:     my $fn=shift;
1.609     banghart 10617:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      10618:     my %bighash;
                   10619:     my $uri=&clutter($fn);
1.620     albertel 10620:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      10621: # is this cached?
1.599     albertel 10622:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      10623:     if (defined($cached)) { return $result; }
                   10624: # unfortunately not cached, or expired
1.620     albertel 10625:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      10626: 	    &GDBM_READER(),0640)) {
                   10627:  	if ($bighash{'version_'.$uri}) {
                   10628:  	    my $version=$bighash{'version_'.$uri};
1.444     www      10629:  	    unless (($version eq 'mostrecent') || 
                   10630: 		    ($version==&getversion($uri))) {
1.440     www      10631:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   10632:  	    }
                   10633:  	}
                   10634:  	untie %bighash;
1.413     www      10635:     }
1.599     albertel 10636:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      10637: }
                   10638: 
                   10639: sub deversion {
                   10640:     my $url=shift;
                   10641:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   10642:     return $url;
1.210     www      10643: }
                   10644: 
1.31      www      10645: # ------------------------------------------------------ Return symb list entry
                   10646: 
                   10647: sub symbread {
1.249     www      10648:     my ($thisfn,$donotrecurse)=@_;
1.1206    raeburn  10649:     my $cache_str;
                   10650:     if ($thisfn ne '') {
                   10651:         $cache_str='request.symbread.cached.'.$thisfn;
                   10652:         if ($env{$cache_str} ne '') {
1.1179    raeburn  10653:             return $env{$cache_str};
                   10654:         }
1.1206    raeburn  10655:     } else {
1.242     www      10656: # no filename provided? try from environment
1.620     albertel 10657:         if ($env{'request.symb'}) {
                   10658: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 10659: 	}
1.620     albertel 10660: 	$thisfn=$env{'request.filename'};
1.44      www      10661:     }
1.569     albertel 10662:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      10663: # is that filename actually a symb? Verify, clean, and return
                   10664:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 10665: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 10666: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 10667: 	}
1.242     www      10668:     }
1.44      www      10669:     $thisfn=declutter($thisfn);
1.31      www      10670:     my %hash;
1.37      www      10671:     my %bighash;
                   10672:     my $syval='';
1.620     albertel 10673:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  10674:         my $targetfn = $thisfn;
1.609     banghart 10675:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  10676:             $targetfn = 'adm/wrapper/'.$thisfn;
                   10677:         }
1.687     albertel 10678: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   10679: 	    $targetfn=$1;
                   10680: 	}
1.620     albertel 10681:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 10682:                       &GDBM_READER(),0640)) {
1.481     raeburn  10683: 	    $syval=$hash{$targetfn};
1.37      www      10684:             untie(%hash);
                   10685:         }
                   10686: # ---------------------------------------------------------- There was an entry
                   10687:         if ($syval) {
1.601     albertel 10688: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 10689: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  10690: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 10691: 		    #return $env{$cache_str}='';
1.601     albertel 10692: 		#}    
                   10693: 		#$syval.=$1;
                   10694: 	    #}
1.37      www      10695:         } else {
                   10696: # ------------------------------------------------------- Was not in symb table
1.620     albertel 10697:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 10698:                             &GDBM_READER(),0640)) {
1.37      www      10699: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      10700:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      10701:               unless ($ids) { 
                   10702:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      10703:               }
                   10704:               unless ($ids) {
                   10705: # alias?
                   10706: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      10707:               }
1.37      www      10708:               if ($ids) {
                   10709: # ------------------------------------------------------------------- Has ID(s)
                   10710:                  my @possibilities=split(/\,/,$ids);
1.39      www      10711:                  if ($#possibilities==0) {
                   10712: # ----------------------------------------------- There is only one possibility
1.37      www      10713: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 10714: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   10715: 						    $resid,$thisfn);
1.249     www      10716:                  } elsif (!$donotrecurse) {
1.39      www      10717: # ------------------------------------------ There is more than one possibility
                   10718:                      my $realpossible=0;
1.800     albertel 10719:                      foreach my $id (@possibilities) {
                   10720: 			 my $file=$bighash{'src_'.$id};
1.39      www      10721:                          if (&allowed('bre',$file)) {
1.800     albertel 10722:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      10723:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   10724: 				$realpossible++;
1.626     albertel 10725:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   10726: 						    $resid,$thisfn);
1.39      www      10727:                             }
                   10728: 			 }
1.191     harris41 10729:                      }
1.39      www      10730: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      10731:                  } else {
                   10732:                      $syval='';
1.37      www      10733:                  }
                   10734: 	      }
                   10735:               untie(%bighash)
1.481     raeburn  10736:            }
1.31      www      10737:         }
1.62      www      10738:         if ($syval) {
1.620     albertel 10739: 	    return $env{$cache_str}=$syval;
1.62      www      10740:         }
1.31      www      10741:     }
1.949     raeburn  10742:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 10743:     return $env{$cache_str}='';
1.31      www      10744: }
                   10745: 
                   10746: # ---------------------------------------------------------- Return random seed
                   10747: 
1.32      www      10748: sub numval {
                   10749:     my $txt=shift;
                   10750:     $txt=~tr/A-J/0-9/;
                   10751:     $txt=~tr/a-j/0-9/;
                   10752:     $txt=~tr/K-T/0-9/;
                   10753:     $txt=~tr/k-t/0-9/;
                   10754:     $txt=~tr/U-Z/0-5/;
                   10755:     $txt=~tr/u-z/0-5/;
                   10756:     $txt=~s/\D//g;
1.564     albertel 10757:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      10758:     return int($txt);
1.368     albertel 10759: }
                   10760: 
1.484     albertel 10761: sub numval2 {
                   10762:     my $txt=shift;
                   10763:     $txt=~tr/A-J/0-9/;
                   10764:     $txt=~tr/a-j/0-9/;
                   10765:     $txt=~tr/K-T/0-9/;
                   10766:     $txt=~tr/k-t/0-9/;
                   10767:     $txt=~tr/U-Z/0-5/;
                   10768:     $txt=~tr/u-z/0-5/;
                   10769:     $txt=~s/\D//g;
                   10770:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   10771:     my $total;
                   10772:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 10773:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 10774:     return int($total);
                   10775: }
                   10776: 
1.575     albertel 10777: sub numval3 {
                   10778:     use integer;
                   10779:     my $txt=shift;
                   10780:     $txt=~tr/A-J/0-9/;
                   10781:     $txt=~tr/a-j/0-9/;
                   10782:     $txt=~tr/K-T/0-9/;
                   10783:     $txt=~tr/k-t/0-9/;
                   10784:     $txt=~tr/U-Z/0-5/;
                   10785:     $txt=~tr/u-z/0-5/;
                   10786:     $txt=~s/\D//g;
                   10787:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   10788:     my $total;
                   10789:     foreach my $val (@txts) { $total+=$val; }
                   10790:     if ($_64bit) { $total=(($total<<32)>>32); }
                   10791:     return $total;
                   10792: }
                   10793: 
1.675     albertel 10794: sub digest {
                   10795:     my ($data)=@_;
                   10796:     my $digest=&Digest::MD5::md5($data);
                   10797:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   10798:     my ($e,$f);
                   10799:     {
                   10800:         use integer;
                   10801:         $e=($a+$b);
                   10802:         $f=($c+$d);
                   10803:         if ($_64bit) {
                   10804:             $e=(($e<<32)>>32);
                   10805:             $f=(($f<<32)>>32);
                   10806:         }
                   10807:     }
                   10808:     if (wantarray) {
                   10809: 	return ($e,$f);
                   10810:     } else {
                   10811: 	my $g;
                   10812: 	{
                   10813: 	    use integer;
                   10814: 	    $g=($e+$f);
                   10815: 	    if ($_64bit) {
                   10816: 		$g=(($g<<32)>>32);
                   10817: 	    }
                   10818: 	}
                   10819: 	return $g;
                   10820:     }
                   10821: }
                   10822: 
1.368     albertel 10823: sub latest_rnd_algorithm_id {
1.675     albertel 10824:     return '64bit5';
1.366     albertel 10825: }
1.32      www      10826: 
1.503     albertel 10827: sub get_rand_alg {
                   10828:     my ($courseid)=@_;
1.790     albertel 10829:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 10830:     if ($courseid) {
1.620     albertel 10831: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 10832:     }
                   10833:     return &latest_rnd_algorithm_id();
                   10834: }
                   10835: 
1.562     albertel 10836: sub validCODE {
                   10837:     my ($CODE)=@_;
                   10838:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   10839:     return 0;
                   10840: }
                   10841: 
1.491     albertel 10842: sub getCODE {
1.620     albertel 10843:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 10844:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   10845: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   10846: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 10847: 	return $Apache::lonhomework::history{'resource.CODE'};
                   10848:     }
                   10849:     return undef;
                   10850: }
1.1133    foxr     10851: #
                   10852: #  Determines the random seed for a specific context:
                   10853: #
                   10854: # parameters:
                   10855: #   symb      - in course context the symb for the seed.
                   10856: #   course_id - The course id of the form domain_coursenum.
                   10857: #   domain    - Domain for the user.
                   10858: #   course    - Course for the user.
                   10859: #   cenv      - environment of the course.
                   10860: #
                   10861: # NOTE:
                   10862: #   All parameters are picked out of the environment if missing
                   10863: #   or not defined.
                   10864: #   If a symb cannot be determined the current time is used instead.
                   10865: #
                   10866: #  For a given well defined symb, courside, domain, username,
                   10867: #  and course environment, the seed is reproducible.
                   10868: #
1.31      www      10869: sub rndseed {
1.1133    foxr     10870:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 10871:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 10872:     if (!defined($symb)) {
1.366     albertel 10873: 	unless ($symb=$wsymb) { return time; }
                   10874:     }
1.1146    foxr     10875:     if (!defined $courseid) { 
                   10876: 	$courseid=$wcourseid; 
                   10877:     }
                   10878:     if (!defined $domain) { $domain=$wdomain; }
                   10879:     if (!defined $username) { $username=$wusername }
1.1133    foxr     10880: 
                   10881:     my $which;
                   10882:     if (defined($cenv->{'rndseed'})) {
                   10883: 	$which = $cenv->{'rndseed'};
                   10884:     } else {
                   10885: 	$which =&get_rand_alg($courseid);
                   10886:     }
1.491     albertel 10887:     if (defined(&getCODE())) {
1.1133    foxr     10888: 
1.675     albertel 10889: 	if ($which eq '64bit5') {
                   10890: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   10891: 	} elsif ($which eq '64bit4') {
1.575     albertel 10892: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   10893: 	} else {
                   10894: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   10895: 	}
1.675     albertel 10896:     } elsif ($which eq '64bit5') {
                   10897: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 10898:     } elsif ($which eq '64bit4') {
                   10899: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 10900:     } elsif ($which eq '64bit3') {
                   10901: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 10902:     } elsif ($which eq '64bit2') {
                   10903: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 10904:     } elsif ($which eq '64bit') {
                   10905: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   10906:     }
                   10907:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   10908: }
                   10909: 
                   10910: sub rndseed_32bit {
                   10911:     my ($symb,$courseid,$domain,$username)=@_;
                   10912:     {
                   10913: 	use integer;
                   10914: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   10915: 	my $symbseed=numval($symb) << 22;
                   10916: 	my $namechck=unpack("%32C*",$username) << 17;
                   10917: 	my $nameseed=numval($username) << 12;
                   10918: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   10919: 	my $courseseed=unpack("%32C*",$courseid);
                   10920: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 10921: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   10922: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 10923: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 10924: 	return $num;
                   10925:     }
                   10926: }
                   10927: 
                   10928: sub rndseed_64bit {
                   10929:     my ($symb,$courseid,$domain,$username)=@_;
                   10930:     {
                   10931: 	use integer;
                   10932: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   10933: 	my $symbseed=numval($symb) << 10;
                   10934: 	my $namechck=unpack("%32S*",$username);
                   10935: 	
                   10936: 	my $nameseed=numval($username) << 21;
                   10937: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   10938: 	my $courseseed=unpack("%32S*",$courseid);
                   10939: 	
                   10940: 	my $num1=$symbchck+$symbseed+$namechck;
                   10941: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 10942: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   10943: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 10944: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 10945: 	return "$num1,$num2";
1.155     albertel 10946:     }
1.366     albertel 10947: }
                   10948: 
1.443     albertel 10949: sub rndseed_64bit2 {
                   10950:     my ($symb,$courseid,$domain,$username)=@_;
                   10951:     {
                   10952: 	use integer;
                   10953: 	# strings need to be an even # of cahracters long, it it is odd the
                   10954:         # last characters gets thrown away
                   10955: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   10956: 	my $symbseed=numval($symb) << 10;
                   10957: 	my $namechck=unpack("%32S*",$username.' ');
                   10958: 	
                   10959: 	my $nameseed=numval($username) << 21;
1.501     albertel 10960: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   10961: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   10962: 	
                   10963: 	my $num1=$symbchck+$symbseed+$namechck;
                   10964: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 10965: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   10966: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 10967: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 10968: 	return "$num1,$num2";
                   10969:     }
                   10970: }
                   10971: 
                   10972: sub rndseed_64bit3 {
                   10973:     my ($symb,$courseid,$domain,$username)=@_;
                   10974:     {
                   10975: 	use integer;
                   10976: 	# strings need to be an even # of cahracters long, it it is odd the
                   10977:         # last characters gets thrown away
                   10978: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   10979: 	my $symbseed=numval2($symb) << 10;
                   10980: 	my $namechck=unpack("%32S*",$username.' ');
                   10981: 	
                   10982: 	my $nameseed=numval2($username) << 21;
1.443     albertel 10983: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   10984: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   10985: 	
                   10986: 	my $num1=$symbchck+$symbseed+$namechck;
                   10987: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 10988: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   10989: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 10990: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      10991: 	
1.503     albertel 10992: 	return "$num1:$num2";
1.443     albertel 10993:     }
                   10994: }
                   10995: 
1.575     albertel 10996: sub rndseed_64bit4 {
                   10997:     my ($symb,$courseid,$domain,$username)=@_;
                   10998:     {
                   10999: 	use integer;
                   11000: 	# strings need to be an even # of cahracters long, it it is odd the
                   11001:         # last characters gets thrown away
                   11002: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   11003: 	my $symbseed=numval3($symb) << 10;
                   11004: 	my $namechck=unpack("%32S*",$username.' ');
                   11005: 	
                   11006: 	my $nameseed=numval3($username) << 21;
                   11007: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   11008: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   11009: 	
                   11010: 	my $num1=$symbchck+$symbseed+$namechck;
                   11011: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 11012: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   11013: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 11014: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      11015: 	
1.575     albertel 11016: 	return "$num1:$num2";
                   11017:     }
                   11018: }
                   11019: 
1.675     albertel 11020: sub rndseed_64bit5 {
                   11021:     my ($symb,$courseid,$domain,$username)=@_;
                   11022:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   11023:     return "$num1:$num2";
                   11024: }
                   11025: 
1.366     albertel 11026: sub rndseed_CODE_64bit {
                   11027:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 11028:     {
1.366     albertel 11029: 	use integer;
1.443     albertel 11030: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 11031: 	my $symbseed=numval2($symb);
1.491     albertel 11032: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   11033: 	my $CODEseed=numval(&getCODE());
1.443     albertel 11034: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 11035: 	my $num1=$symbseed+$CODEchck;
                   11036: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 11037: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   11038: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 11039: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   11040: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 11041: 	return "$num1:$num2";
1.366     albertel 11042:     }
                   11043: }
                   11044: 
1.575     albertel 11045: sub rndseed_CODE_64bit4 {
                   11046:     my ($symb,$courseid,$domain,$username)=@_;
                   11047:     {
                   11048: 	use integer;
                   11049: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   11050: 	my $symbseed=numval3($symb);
                   11051: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   11052: 	my $CODEseed=numval3(&getCODE());
                   11053: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   11054: 	my $num1=$symbseed+$CODEchck;
                   11055: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 11056: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   11057: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 11058: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   11059: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   11060: 	return "$num1:$num2";
                   11061:     }
                   11062: }
                   11063: 
1.675     albertel 11064: sub rndseed_CODE_64bit5 {
                   11065:     my ($symb,$courseid,$domain,$username)=@_;
                   11066:     my $code = &getCODE();
                   11067:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   11068:     return "$num1:$num2";
                   11069: }
                   11070: 
1.366     albertel 11071: sub setup_random_from_rndseed {
                   11072:     my ($rndseed)=@_;
1.503     albertel 11073:     if ($rndseed =~/([,:])/) {
                   11074: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 11075: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   11076:     } else {
                   11077: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 11078:     }
1.36      albertel 11079: }
                   11080: 
1.474     albertel 11081: sub latest_receipt_algorithm_id {
1.835     albertel 11082:     return 'receipt3';
1.474     albertel 11083: }
                   11084: 
1.480     www      11085: sub recunique {
                   11086:     my $fucourseid=shift;
                   11087:     my $unique;
1.835     albertel 11088:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   11089: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 11090: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      11091:     } else {
                   11092: 	$unique=$perlvar{'lonReceipt'};
                   11093:     }
                   11094:     return unpack("%32C*",$unique);
                   11095: }
                   11096: 
                   11097: sub recprefix {
                   11098:     my $fucourseid=shift;
                   11099:     my $prefix;
1.835     albertel 11100:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   11101: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 11102: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      11103:     } else {
                   11104: 	$prefix=$perlvar{'lonHostID'};
                   11105:     }
                   11106:     return unpack("%32C*",$prefix);
                   11107: }
                   11108: 
1.76      www      11109: sub ireceipt {
1.474     albertel 11110:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 11111: 
                   11112:     my $return =&recprefix($fucourseid).'-';
                   11113: 
                   11114:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   11115: 	$env{'request.state'} eq 'construct') {
                   11116: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   11117: 	return $return;
                   11118:     }
                   11119: 
1.76      www      11120:     my $cuname=unpack("%32C*",$funame);
                   11121:     my $cudom=unpack("%32C*",$fudom);
                   11122:     my $cucourseid=unpack("%32C*",$fucourseid);
                   11123:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      11124:     my $cunique=&recunique($fucourseid);
1.474     albertel 11125:     my $cpart=unpack("%32S*",$part);
1.835     albertel 11126:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   11127: 
1.790     albertel 11128: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 11129: 			       
                   11130: 	$return.= ($cunique%$cuname+
                   11131: 		   $cunique%$cudom+
                   11132: 		   $cusymb%$cuname+
                   11133: 		   $cusymb%$cudom+
                   11134: 		   $cucourseid%$cuname+
                   11135: 		   $cucourseid%$cudom+
                   11136: 		   $cpart%$cuname+
                   11137: 		   $cpart%$cudom);
                   11138:     } else {
                   11139: 	$return.= ($cunique%$cuname+
                   11140: 		   $cunique%$cudom+
                   11141: 		   $cusymb%$cuname+
                   11142: 		   $cusymb%$cudom+
                   11143: 		   $cucourseid%$cuname+
                   11144: 		   $cucourseid%$cudom);
                   11145:     }
                   11146:     return $return;
1.76      www      11147: }
                   11148: 
                   11149: sub receipt {
1.474     albertel 11150:     my ($part)=@_;
1.790     albertel 11151:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 11152:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      11153: }
1.260     ng       11154: 
1.790     albertel 11155: sub whichuser {
                   11156:     my ($passedsymb)=@_;
                   11157:     my ($symb,$courseid,$domain,$name,$publicuser);
                   11158:     if (defined($env{'form.grade_symb'})) {
                   11159: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   11160: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   11161: 	if (!$allowed &&
                   11162: 	    exists($env{'request.course.sec'}) &&
                   11163: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   11164: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   11165: 			      '/'.$env{'request.course.sec'});
                   11166: 	}
                   11167: 	if ($allowed) {
                   11168: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   11169: 	    $courseid=$tmp_courseid;
                   11170: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   11171: 	    ($name)=&get_env_multiple('form.grade_username');
                   11172: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   11173: 	}
                   11174:     }
                   11175:     if (!$passedsymb) {
                   11176: 	$symb=&symbread();
                   11177:     } else {
                   11178: 	$symb=$passedsymb;
                   11179:     }
                   11180:     $courseid=$env{'request.course.id'};
                   11181:     $domain=$env{'user.domain'};
                   11182:     $name=$env{'user.name'};
                   11183:     if ($name eq 'public' && $domain eq 'public') {
                   11184: 	if (!defined($env{'form.username'})) {
                   11185: 	    $env{'form.username'}.=time.rand(10000000);
                   11186: 	}
                   11187: 	$name.=$env{'form.username'};
                   11188:     }
                   11189:     return ($symb,$courseid,$domain,$name,$publicuser);
                   11190: 
                   11191: }
                   11192: 
1.36      albertel 11193: # ------------------------------------------------------------ Serves up a file
1.472     albertel 11194: # returns either the contents of the file or 
                   11195: # -1 if the file doesn't exist
1.481     raeburn  11196: #
                   11197: # if the target is a file that was uploaded via DOCS, 
                   11198: # a check will be made to see if a current copy exists on the local server,
                   11199: # if it does this will be served, otherwise a copy will be retrieved from
                   11200: # the home server for the course and stored in /home/httpd/html/userfiles on
                   11201: # the local server.   
1.472     albertel 11202: 
1.36      albertel 11203: sub getfile {
1.538     albertel 11204:     my ($file) = @_;
1.609     banghart 11205:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 11206:     &repcopy($file);
                   11207:     return &readfile($file);
                   11208: }
                   11209: 
                   11210: sub repcopy_userfile {
                   11211:     my ($file)=@_;
1.1142    raeburn  11212:     my $londocroot = $perlvar{'lonDocRoot'};
                   11213:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
1.1164    raeburn  11214:     if ($file =~ m{^\Q/home/httpd/lonUsers/\E}) { return 'ok'; }
1.538     albertel 11215:     my ($cdom,$cnum,$filename) = 
1.811     albertel 11216: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 11217:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   11218:     if (-e "$file") {
1.828     www      11219: # we already have a local copy, check it out
1.538     albertel 11220: 	my @fileinfo = stat($file);
1.828     www      11221: 	my $rtncode;
                   11222: 	my $info;
1.538     albertel 11223: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 11224: 	if ($lwpresp ne 'ok') {
1.828     www      11225: # there is no such file anymore, even though we had a local copy
1.482     albertel 11226: 	    if ($rtncode eq '404') {
1.538     albertel 11227: 		unlink($file);
1.482     albertel 11228: 	    }
                   11229: 	    return -1;
                   11230: 	}
                   11231: 	if ($info < $fileinfo[9]) {
1.828     www      11232: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  11233: 	    return 'ok';
1.828     www      11234: 	} else {
                   11235: # the file is outdated, get rid of it
                   11236: 	    unlink($file);
1.482     albertel 11237: 	}
1.828     www      11238:     }
                   11239: # one way or the other, at this point, we don't have the file
                   11240: # construct the correct path for the file
                   11241:     my @parts = ($cdom,$cnum); 
                   11242:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   11243: 	push @parts, split(/\//,$1);
                   11244:     }
                   11245:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   11246:     foreach my $part (@parts) {
                   11247: 	$path .= '/'.$part;
                   11248: 	if (!-e $path) {
                   11249: 	    mkdir($path,0770);
1.482     albertel 11250: 	}
                   11251:     }
1.828     www      11252: # now the path exists for sure
                   11253: # get a user agent
                   11254:     my $ua=new LWP::UserAgent;
                   11255:     my $transferfile=$file.'.in.transfer';
                   11256: # FIXME: this should flock
                   11257:     if (-e $transferfile) { return 'ok'; }
                   11258:     my $request;
                   11259:     $uri=~s/^\///;
1.980     raeburn  11260:     my $homeserver = &homeserver($cnum,$cdom);
                   11261:     my $protocol = $protocol{$homeserver};
                   11262:     $protocol = 'http' if ($protocol ne 'https');
                   11263:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828     www      11264:     my $response=$ua->request($request,$transferfile);
                   11265: # did it work?
                   11266:     if ($response->is_error()) {
                   11267: 	unlink($transferfile);
                   11268: 	&logthis("Userfile repcopy failed for $uri");
                   11269: 	return -1;
                   11270:     }
                   11271: # worked, rename the transfer file
                   11272:     rename($transferfile,$file);
1.607     raeburn  11273:     return 'ok';
1.481     raeburn  11274: }
                   11275: 
1.517     albertel 11276: sub tokenwrapper {
                   11277:     my $uri=shift;
1.980     raeburn  11278:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 11279:     $uri=~s|^/||;
1.620     albertel 11280:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 11281:     my $token=$1;
1.552     albertel 11282:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   11283:     if ($udom && $uname && $file) {
                   11284: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  11285:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  11286:         my $homeserver = &homeserver($uname,$udom);
                   11287:         my $protocol = $protocol{$homeserver};
                   11288:         $protocol = 'http' if ($protocol ne 'https');
                   11289:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517     albertel 11290:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   11291:                                '&tokenissued='.$perlvar{'lonHostID'};
                   11292:     } else {
                   11293:         return '/adm/notfound.html';
                   11294:     }
                   11295: }
                   11296: 
1.828     www      11297: # call with reqtype HEAD: get last modification time
                   11298: # call with reqtype GET: get the file contents
                   11299: # Do not call this with reqtype GET for large files! It loads everything into memory
                   11300: #
1.481     raeburn  11301: sub getuploaded {
                   11302:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   11303:     $uri=~s/^\///;
1.980     raeburn  11304:     my $homeserver = &homeserver($cnum,$cdom);
                   11305:     my $protocol = $protocol{$homeserver};
                   11306:     $protocol = 'http' if ($protocol ne 'https');
                   11307:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481     raeburn  11308:     my $ua=new LWP::UserAgent;
                   11309:     my $request=new HTTP::Request($reqtype,$uri);
                   11310:     my $response=$ua->request($request);
                   11311:     $$rtncode = $response->code;
1.482     albertel 11312:     if (! $response->is_success()) {
                   11313: 	return 'failed';
                   11314:     }      
                   11315:     if ($reqtype eq 'HEAD') {
1.486     www      11316: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 11317:     } elsif ($reqtype eq 'GET') {
                   11318: 	$$info = $response->content;
1.472     albertel 11319:     }
1.482     albertel 11320:     return 'ok';
1.36      albertel 11321: }
                   11322: 
1.481     raeburn  11323: sub readfile {
                   11324:     my $file = shift;
                   11325:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   11326:     my $fh;
                   11327:     open($fh,"<$file");
                   11328:     my $a='';
1.800     albertel 11329:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  11330:     return $a;
                   11331: }
                   11332: 
1.36      albertel 11333: sub filelocation {
1.590     banghart 11334:     my ($dir,$file) = @_;
                   11335:     my $location;
                   11336:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 11337: 
                   11338:     if ($file =~ m-^/adm/-) {
                   11339: 	$file=~s-^/adm/wrapper/-/-;
                   11340: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   11341:     }
1.882     albertel 11342: 
1.1139    www      11343:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  11344:         $location = $file;
1.609     banghart 11345:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 11346:         my ($udom,$uname,$filename)=
1.811     albertel 11347:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 11348:         my $home=&homeserver($uname,$udom);
                   11349:         my $is_me=0;
                   11350:         my @ids=&current_machine_ids();
                   11351:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   11352:         if ($is_me) {
1.1117    foxr     11353:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 11354:         } else {
                   11355:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   11356:   	      $udom.'/'.$uname.'/'.$filename;
                   11357:         }
1.882     albertel 11358:     } elsif ($file =~ m-^/adm/-) {
                   11359: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 11360:     } else {
                   11361:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      11362:         $file=~s:^/(res|priv)/:/:;
                   11363:         my $space=$1;
1.590     banghart 11364:         if ( !( $file =~ m:^/:) ) {
                   11365:             $location = $dir. '/'.$file;
                   11366:         } else {
1.1142    raeburn  11367:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 11368:         }
1.59      albertel 11369:     }
1.590     banghart 11370:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 11371:     while ($location=~m{/\.\./}) {
                   11372: 	if ($location =~ m{/[^/]+/\.\./}) {
                   11373: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   11374: 	} else {
                   11375: 	    $location=~ s{/\.\./}{/}g;
                   11376: 	}
                   11377:     } #remove dir/..
1.590     banghart 11378:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   11379:     return $location;
1.46      www      11380: }
1.36      albertel 11381: 
1.46      www      11382: sub hreflocation {
                   11383:     my ($dir,$file)=@_;
1.980     raeburn  11384:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 11385: 	$file=filelocation($dir,$file);
1.700     albertel 11386:     } elsif ($file=~m-^/adm/-) {
                   11387: 	$file=~s-^/adm/wrapper/-/-;
                   11388: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 11389:     }
                   11390:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   11391: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   11392:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  11393: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   11394: 	        {/uploaded/$1/$2/}x;
1.46      www      11395:     }
1.913     albertel 11396:     if ($file=~ m{^/userfiles/}) {
                   11397: 	$file =~ s{^/userfiles/}{/uploaded/};
                   11398:     }
1.462     albertel 11399:     return $file;
1.465     albertel 11400: }
                   11401: 
1.1139    www      11402: 
                   11403: 
                   11404: 
                   11405: 
1.465     albertel 11406: sub current_machine_domains {
1.853     albertel 11407:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   11408: }
                   11409: 
                   11410: sub machine_domains {
                   11411:     my ($hostname) = @_;
1.465     albertel 11412:     my @domains;
1.838     albertel 11413:     my %hostname = &all_hostnames();
1.465     albertel 11414:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  11415: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 11416: 	if ($hostname eq $name) {
1.844     albertel 11417: 	    push(@domains,&host_domain($id));
1.465     albertel 11418: 	}
                   11419:     }
                   11420:     return @domains;
                   11421: }
                   11422: 
                   11423: sub current_machine_ids {
1.853     albertel 11424:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   11425: }
                   11426: 
                   11427: sub machine_ids {
                   11428:     my ($hostname) = @_;
                   11429:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 11430:     my @ids;
1.888     albertel 11431:     my %name_to_host = &all_names();
1.889     albertel 11432:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   11433: 	return @{ $name_to_host{$hostname} };
                   11434:     }
                   11435:     return;
1.31      www      11436: }
                   11437: 
1.824     raeburn  11438: sub additional_machine_domains {
                   11439:     my @domains;
                   11440:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   11441:     while( my $line = <$fh>) {
                   11442:         $line =~ s/\s//g;
                   11443:         push(@domains,$line);
                   11444:     }
                   11445:     return @domains;
                   11446: }
                   11447: 
                   11448: sub default_login_domain {
                   11449:     my $domain = $perlvar{'lonDefDomain'};
                   11450:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   11451:     foreach my $posdom (&current_machine_domains(),
                   11452:                         &additional_machine_domains()) {
                   11453:         if (lc($posdom) eq lc($testdomain)) {
                   11454:             $domain=$posdom;
                   11455:             last;
                   11456:         }
                   11457:     }
                   11458:     return $domain;
                   11459: }
                   11460: 
1.31      www      11461: # ------------------------------------------------------------- Declutters URLs
                   11462: 
                   11463: sub declutter {
                   11464:     my $thisfn=shift;
1.569     albertel 11465:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 11466:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      11467:     $thisfn=~s/^\///;
1.697     albertel 11468:     $thisfn=~s|^adm/wrapper/||;
                   11469:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      11470:     $thisfn=~s/^res\///;
1.1172    bisitz   11471:     $thisfn=~s/^priv\///;
1.1032    raeburn  11472:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   11473:         $thisfn=~s/\?.+$//;
                   11474:     }
1.268     www      11475:     return $thisfn;
                   11476: }
                   11477: 
                   11478: # ------------------------------------------------------------- Clutter up URLs
                   11479: 
                   11480: sub clutter {
                   11481:     my $thisfn='/'.&declutter(shift);
1.887     albertel 11482:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 11483: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      11484:        $thisfn='/res'.$thisfn; 
                   11485:     }
1.1031    raeburn  11486:     if ($thisfn !~m|^/adm|) {
                   11487: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 11488: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 11489: 	} else {
                   11490: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   11491: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 11492: 	    if ($embstyle eq 'ssi'
                   11493: 		|| ($embstyle eq 'hdn')
                   11494: 		|| ($embstyle eq 'rat')
                   11495: 		|| ($embstyle eq 'prv')
                   11496: 		|| ($embstyle eq 'ign')) {
                   11497: 		#do nothing with these
                   11498: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 11499: 		|| ($embstyle eq 'emb')
                   11500: 		|| ($embstyle eq 'wrp')) {
                   11501: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 11502: 	    } elsif ($embstyle eq 'unk'
                   11503: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 11504: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 11505: 	    } else {
1.718     www      11506: #		&logthis("Got a blank emb style");
1.695     albertel 11507: 	    }
1.694     albertel 11508: 	}
                   11509:     }
1.31      www      11510:     return $thisfn;
1.12      www      11511: }
                   11512: 
1.787     albertel 11513: sub clutter_with_no_wrapper {
                   11514:     my $uri = &clutter(shift);
                   11515:     if ($uri =~ m-^/adm/-) {
                   11516: 	$uri =~ s-^/adm/wrapper/-/-;
                   11517: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   11518:     }
                   11519:     return $uri;
                   11520: }
                   11521: 
1.557     albertel 11522: sub freeze_escape {
                   11523:     my ($value)=@_;
                   11524:     if (ref($value)) {
                   11525: 	$value=&nfreeze($value);
                   11526: 	return '__FROZEN__'.&escape($value);
                   11527:     }
                   11528:     return &escape($value);
                   11529: }
                   11530: 
1.11      www      11531: 
1.557     albertel 11532: sub thaw_unescape {
                   11533:     my ($value)=@_;
                   11534:     if ($value =~ /^__FROZEN__/) {
                   11535: 	substr($value,0,10,undef);
                   11536: 	$value=&unescape($value);
                   11537: 	return &thaw($value);
                   11538:     }
                   11539:     return &unescape($value);
                   11540: }
                   11541: 
1.436     albertel 11542: sub correct_line_ends {
                   11543:     my ($result)=@_;
                   11544:     $$result =~s/\r\n/\n/mg;
                   11545:     $$result =~s/\r/\n/mg;
1.415     albertel 11546: }
1.1       albertel 11547: # ================================================================ Main Program
                   11548: 
1.184     www      11549: sub goodbye {
1.204     albertel 11550:    &logthis("Starting Shut down");
1.443     albertel 11551: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 11552:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 11553: #converted
1.599     albertel 11554: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 11555:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   11556: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   11557: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 11558: #1.1 only
1.870     albertel 11559: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   11560: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   11561: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   11562: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   11563:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 11564:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   11565:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      11566:    &flushcourselogs();
                   11567:    &logthis("Shutting down");
                   11568: }
                   11569: 
1.852     albertel 11570: sub get_dns {
1.1210    raeburn  11571:     my ($url,$func,$ignore_cache,$nocache,$hashref) = @_;
1.869     albertel 11572:     if (!$ignore_cache) {
                   11573: 	my ($content,$cached)=
                   11574: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   11575: 	if ($cached) {
1.1210    raeburn  11576: 	    &$func($content,$hashref);
1.869     albertel 11577: 	    return;
                   11578: 	}
                   11579:     }
                   11580: 
                   11581:     my %alldns;
1.852     albertel 11582:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   11583:     foreach my $dns (<$config>) {
                   11584: 	next if ($dns !~ /^\^(\S*)/x);
1.979     raeburn  11585:         my $line = $1;
                   11586:         my ($host,$protocol) = split(/:/,$line);
                   11587:         if ($protocol ne 'https') {
                   11588:             $protocol = 'http';
                   11589:         }
                   11590: 	$alldns{$host} = $protocol;
1.869     albertel 11591:     }
                   11592:     while (%alldns) {
                   11593: 	my ($dns) = keys(%alldns);
1.852     albertel 11594: 	my $ua=new LWP::UserAgent;
1.1134    raeburn  11595:         $ua->timeout(30);
1.979     raeburn  11596: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852     albertel 11597: 	my $response=$ua->request($request);
1.979     raeburn  11598:         delete($alldns{$dns});
1.852     albertel 11599: 	next if ($response->is_error());
                   11600: 	my @content = split("\n",$response->content);
1.1210    raeburn  11601: 	unless ($nocache) {
1.1219    raeburn  11602: 	    &do_cache_new('dns',$url,\@content,30*24*60*60);
1.1210    raeburn  11603: 	}
                   11604: 	&$func(\@content,$hashref);
1.869     albertel 11605: 	return;
1.852     albertel 11606:     }
                   11607:     close($config);
1.871     albertel 11608:     my $which = (split('/',$url))[3];
                   11609:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   11610:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 11611:     my @content = <$config>;
1.1210    raeburn  11612:     &$func(\@content,$hashref);
                   11613:     return;
                   11614: }
                   11615: 
                   11616: # ------------------------------------------------------Get DNS checksums file
1.1211    raeburn  11617: sub parse_dns_checksums_tab {
1.1210    raeburn  11618:     my ($lines,$hashref) = @_;
                   11619:     my $machine_dom = &Apache::lonnet::host_domain($perlvar{'lonHostID'});
                   11620:     my $loncaparev = &get_server_loncaparev($machine_dom);
                   11621:     my ($release,$timestamp) = split(/\-/,$loncaparev);
                   11622:     my (%chksum,%revnum);
                   11623:     if (ref($lines) eq 'ARRAY') {
                   11624:         chomp(@{$lines});
                   11625:         my $versions = shift(@{$lines});
                   11626:         my %supported;
                   11627:         if ($versions =~ /^VERSIONS\:([\w\.\,]+)$/) {
                   11628:             my $releaseslist = $1;
                   11629:             if ($releaseslist =~ /,/) {
                   11630:                 map { $supported{$_} = 1; } split(/,/,$releaseslist);
                   11631:             } elsif ($releaseslist) {
                   11632:                 $supported{$releaseslist} = 1;
                   11633:             }
                   11634:         }
                   11635:         if ($supported{$release}) {  
                   11636:             my $matchthis = 0;
                   11637:             foreach my $line (@{$lines}) {
                   11638:                 if ($line =~ /^(\d[\w\.]+)$/) {
                   11639:                     if ($matchthis) {
                   11640:                         last;
                   11641:                     } elsif ($1 eq $release) {
                   11642:                         $matchthis = 1;
                   11643:                     }
                   11644:                 } elsif ($matchthis) {
                   11645:                     my ($file,$version,$shasum) = split(/,/,$line);
                   11646:                     $chksum{$file} = $shasum;
                   11647:                     $revnum{$file} = $version;
                   11648:                 }
                   11649:             }
                   11650:             if (ref($hashref) eq 'HASH') {
                   11651:                 %{$hashref} = (
                   11652:                                 sums     => \%chksum,
                   11653:                                 versions => \%revnum,
                   11654:                               );
                   11655:             }
                   11656:         }
                   11657:     }
1.869     albertel 11658:     return;
1.852     albertel 11659: }
1.1210    raeburn  11660: 
                   11661: sub fetch_dns_checksums {
                   11662:     my %checksums; 
1.1211    raeburn  11663:     &get_dns('/adm/dns/checksums',\&parse_dns_checksums_tab,1,1,
                   11664:              \%checksums);
1.1210    raeburn  11665:     return \%checksums;
                   11666: }
                   11667: 
1.327     albertel 11668: # ------------------------------------------------------------ Read domain file
                   11669: {
1.852     albertel 11670:     my $loaded;
1.846     albertel 11671:     my %domain;
                   11672: 
1.852     albertel 11673:     sub parse_domain_tab {
                   11674: 	my ($lines) = @_;
                   11675: 	foreach my $line (@$lines) {
                   11676: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      11677: 
1.846     albertel 11678: 	    chomp($line);
1.852     albertel 11679: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 11680: 	    my %this_domain;
                   11681: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   11682: 			       'lang_def', 'city', 'longi', 'lati',
                   11683: 			       'primary') {
                   11684: 		$this_domain{$field} = shift(@elements);
                   11685: 	    }
                   11686: 	    $domain{$name} = \%this_domain;
1.852     albertel 11687: 	}
                   11688:     }
1.864     albertel 11689: 
                   11690:     sub reset_domain_info {
                   11691: 	undef($loaded);
                   11692: 	undef(%domain);
                   11693:     }
                   11694: 
1.852     albertel 11695:     sub load_domain_tab {
1.869     albertel 11696: 	my ($ignore_cache) = @_;
                   11697: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 11698: 	my $fh;
                   11699: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   11700: 	    my @lines = <$fh>;
                   11701: 	    &parse_domain_tab(\@lines);
1.448     albertel 11702: 	}
1.852     albertel 11703: 	close($fh);
                   11704: 	$loaded = 1;
1.327     albertel 11705:     }
1.846     albertel 11706: 
                   11707:     sub domain {
1.852     albertel 11708: 	&load_domain_tab() if (!$loaded);
                   11709: 
1.846     albertel 11710: 	my ($name,$what) = @_;
                   11711: 	return if ( !exists($domain{$name}) );
                   11712: 
                   11713: 	if (!$what) {
                   11714: 	    return $domain{$name}{'description'};
                   11715: 	}
                   11716: 	return $domain{$name}{$what};
                   11717:     }
1.974     raeburn  11718: 
                   11719:     sub domain_info {
                   11720:         &load_domain_tab() if (!$loaded);
                   11721:         return %domain;
                   11722:     }
                   11723: 
1.327     albertel 11724: }
                   11725: 
                   11726: 
1.1       albertel 11727: # ------------------------------------------------------------- Read hosts file
                   11728: {
1.838     albertel 11729:     my %hostname;
1.844     albertel 11730:     my %hostdom;
1.845     albertel 11731:     my %libserv;
1.852     albertel 11732:     my $loaded;
1.888     albertel 11733:     my %name_to_host;
1.1074    raeburn  11734:     my %internetdom;
1.1107    raeburn  11735:     my %LC_dns_serv;
1.852     albertel 11736: 
                   11737:     sub parse_hosts_tab {
                   11738: 	my ($file) = @_;
                   11739: 	foreach my $configline (@$file) {
                   11740: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  11741:             chomp($configline);
                   11742: 	    if ($configline =~ /^\^/) {
                   11743:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   11744:                     $LC_dns_serv{$1} = 1;
                   11745:                 }
                   11746:                 next;
                   11747:             }
1.1074    raeburn  11748: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 11749: 	    $name=~s/\s//g;
                   11750: 	    if ($id && $domain && $role && $name) {
                   11751: 		$hostname{$id}=$name;
1.888     albertel 11752: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 11753: 		$hostdom{$id}=$domain;
                   11754: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  11755:                 if (defined($protocol)) {
                   11756:                     if ($protocol eq 'https') {
                   11757:                         $protocol{$id} = $protocol;
                   11758:                     } else {
                   11759:                         $protocol{$id} = 'http'; 
                   11760:                     }
1.968     raeburn  11761:                 } else {
1.969     raeburn  11762:                     $protocol{$id} = 'http';
1.968     raeburn  11763:                 }
1.1074    raeburn  11764:                 if (defined($intdom)) {
                   11765:                     $internetdom{$id} = $intdom;
                   11766:                 }
1.852     albertel 11767: 	    }
                   11768: 	}
                   11769:     }
1.864     albertel 11770:     
                   11771:     sub reset_hosts_info {
1.897     albertel 11772: 	&purge_remembered();
1.864     albertel 11773: 	&reset_domain_info();
                   11774: 	&reset_hosts_ip_info();
1.892     albertel 11775: 	undef(%name_to_host);
1.864     albertel 11776: 	undef(%hostname);
                   11777: 	undef(%hostdom);
                   11778: 	undef(%libserv);
                   11779: 	undef($loaded);
                   11780:     }
1.1       albertel 11781: 
1.852     albertel 11782:     sub load_hosts_tab {
1.869     albertel 11783: 	my ($ignore_cache) = @_;
                   11784: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 11785: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   11786: 	my @config = <$config>;
                   11787: 	&parse_hosts_tab(\@config);
                   11788: 	close($config);
                   11789: 	$loaded=1;
1.1       albertel 11790:     }
1.852     albertel 11791: 
1.838     albertel 11792:     sub hostname {
1.852     albertel 11793: 	&load_hosts_tab() if (!$loaded);
                   11794: 
1.838     albertel 11795: 	my ($lonid) = @_;
                   11796: 	return $hostname{$lonid};
                   11797:     }
1.845     albertel 11798: 
1.838     albertel 11799:     sub all_hostnames {
1.852     albertel 11800: 	&load_hosts_tab() if (!$loaded);
                   11801: 
1.838     albertel 11802: 	return %hostname;
                   11803:     }
1.845     albertel 11804: 
1.888     albertel 11805:     sub all_names {
                   11806: 	&load_hosts_tab() if (!$loaded);
                   11807: 
                   11808: 	return %name_to_host;
                   11809:     }
                   11810: 
1.974     raeburn  11811:     sub all_host_domain {
                   11812:         &load_hosts_tab() if (!$loaded);
                   11813:         return %hostdom;
                   11814:     }
                   11815: 
1.845     albertel 11816:     sub is_library {
1.852     albertel 11817: 	&load_hosts_tab() if (!$loaded);
                   11818: 
1.845     albertel 11819: 	return exists($libserv{$_[0]});
                   11820:     }
                   11821: 
                   11822:     sub all_library {
1.852     albertel 11823: 	&load_hosts_tab() if (!$loaded);
                   11824: 
1.845     albertel 11825: 	return %libserv;
                   11826:     }
                   11827: 
1.1062    droeschl 11828:     sub unique_library {
                   11829: 	#2x reverse removes all hostnames that appear more than once
                   11830:         my %unique = reverse &all_library();
                   11831:         return reverse %unique;
                   11832:     }
                   11833: 
1.841     albertel 11834:     sub get_servers {
1.852     albertel 11835: 	&load_hosts_tab() if (!$loaded);
                   11836: 
1.841     albertel 11837: 	my ($domain,$type) = @_;
                   11838: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   11839: 	                                          : %hostname;
                   11840: 	my %result;
1.842     albertel 11841: 	if (ref($domain) eq 'ARRAY') {
                   11842: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 11843: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 11844: 		    $result{$host} = $hostname;
                   11845: 		}
                   11846: 	    }
                   11847: 	} else {
                   11848: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   11849: 		if ($hostdom{$host} eq $domain) {
                   11850: 		    $result{$host} = $hostname;
                   11851: 		}
1.841     albertel 11852: 	    }
                   11853: 	}
                   11854: 	return %result;
                   11855:     }
1.845     albertel 11856: 
1.1062    droeschl 11857:     sub get_unique_servers {
                   11858:         my %unique = reverse &get_servers(@_);
                   11859: 	return reverse %unique;
                   11860:     }
                   11861: 
1.844     albertel 11862:     sub host_domain {
1.852     albertel 11863: 	&load_hosts_tab() if (!$loaded);
                   11864: 
1.844     albertel 11865: 	my ($lonid) = @_;
                   11866: 	return $hostdom{$lonid};
                   11867:     }
                   11868: 
1.841     albertel 11869:     sub all_domains {
1.852     albertel 11870: 	&load_hosts_tab() if (!$loaded);
                   11871: 
1.841     albertel 11872: 	my %seen;
                   11873: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   11874: 	return @uniq;
                   11875:     }
1.1074    raeburn  11876: 
                   11877:     sub internet_dom {
                   11878:         &load_hosts_tab() if (!$loaded);
                   11879: 
                   11880:         my ($lonid) = @_;
                   11881:         return $internetdom{$lonid};
                   11882:     }
1.1107    raeburn  11883: 
                   11884:     sub is_LC_dns {
                   11885:         &load_hosts_tab() if (!$loaded);
                   11886: 
                   11887:         my ($hostname) = @_;
                   11888:         return exists($LC_dns_serv{$hostname});
                   11889:     }
                   11890: 
1.1       albertel 11891: }
                   11892: 
1.847     albertel 11893: { 
                   11894:     my %iphost;
1.856     albertel 11895:     my %name_to_ip;
                   11896:     my %lonid_to_ip;
1.869     albertel 11897: 
1.847     albertel 11898:     sub get_hosts_from_ip {
                   11899: 	my ($ip) = @_;
                   11900: 	my %iphosts = &get_iphost();
                   11901: 	if (ref($iphosts{$ip})) {
                   11902: 	    return @{$iphosts{$ip}};
                   11903: 	}
                   11904: 	return;
1.839     albertel 11905:     }
1.864     albertel 11906:     
                   11907:     sub reset_hosts_ip_info {
                   11908: 	undef(%iphost);
                   11909: 	undef(%name_to_ip);
                   11910: 	undef(%lonid_to_ip);
                   11911:     }
1.856     albertel 11912: 
                   11913:     sub get_host_ip {
                   11914: 	my ($lonid) = @_;
                   11915: 	if (exists($lonid_to_ip{$lonid})) {
                   11916: 	    return $lonid_to_ip{$lonid};
                   11917: 	}
                   11918: 	my $name=&hostname($lonid);
                   11919:    	my $ip = gethostbyname($name);
                   11920: 	return if (!$ip || length($ip) ne 4);
                   11921: 	$ip=inet_ntoa($ip);
                   11922: 	$name_to_ip{$name}   = $ip;
                   11923: 	$lonid_to_ip{$lonid} = $ip;
                   11924: 	return $ip;
                   11925:     }
1.847     albertel 11926:     
                   11927:     sub get_iphost {
1.869     albertel 11928: 	my ($ignore_cache) = @_;
1.894     albertel 11929: 
1.869     albertel 11930: 	if (!$ignore_cache) {
                   11931: 	    if (%iphost) {
                   11932: 		return %iphost;
                   11933: 	    }
                   11934: 	    my ($ip_info,$cached)=
                   11935: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   11936: 	    if ($cached) {
                   11937: 		%iphost      = %{$ip_info->[0]};
                   11938: 		%name_to_ip  = %{$ip_info->[1]};
                   11939: 		%lonid_to_ip = %{$ip_info->[2]};
                   11940: 		return %iphost;
                   11941: 	    }
                   11942: 	}
1.894     albertel 11943: 
                   11944: 	# get yesterday's info for fallback
                   11945: 	my %old_name_to_ip;
                   11946: 	my ($ip_info,$cached)=
                   11947: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   11948: 	if ($cached) {
                   11949: 	    %old_name_to_ip = %{$ip_info->[1]};
                   11950: 	}
                   11951: 
1.888     albertel 11952: 	my %name_to_host = &all_names();
                   11953: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 11954: 	    my $ip;
                   11955: 	    if (!exists($name_to_ip{$name})) {
                   11956: 		$ip = gethostbyname($name);
                   11957: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 11958: 		    if (defined($old_name_to_ip{$name})) {
                   11959: 			$ip = $old_name_to_ip{$name};
                   11960: 			&logthis("Can't find $name defaulting to old $ip");
                   11961: 		    } else {
                   11962: 			&logthis("Name $name no IP found");
                   11963: 			next;
                   11964: 		    }
                   11965: 		} else {
                   11966: 		    $ip=inet_ntoa($ip);
1.847     albertel 11967: 		}
                   11968: 		$name_to_ip{$name} = $ip;
                   11969: 	    } else {
                   11970: 		$ip = $name_to_ip{$name};
1.653     albertel 11971: 	    }
1.888     albertel 11972: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   11973: 		$lonid_to_ip{$id} = $ip;
                   11974: 	    }
                   11975: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 11976: 	}
1.1219    raeburn  11977: 	&do_cache_new('iphost','iphost',
                   11978: 		      [\%iphost,\%name_to_ip,\%lonid_to_ip],
                   11979: 		      48*60*60);
1.869     albertel 11980: 
1.847     albertel 11981: 	return %iphost;
1.598     albertel 11982:     }
                   11983: 
1.992     raeburn  11984:     #
                   11985:     #  Given a DNS returns the loncapa host name for that DNS 
                   11986:     # 
                   11987:     sub host_from_dns {
                   11988:         my ($dns) = @_;
                   11989:         my @hosts;
                   11990:         my $ip;
                   11991: 
1.993     raeburn  11992:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  11993:             $ip = $name_to_ip{$dns};
                   11994:         }
                   11995:         if (!$ip) {
                   11996:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   11997:             if (length($ip) == 4) { 
                   11998: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   11999:             }
                   12000:         }
                   12001:         if ($ip) {
                   12002: 	    @hosts = get_hosts_from_ip($ip);
                   12003: 	    return $hosts[0];
                   12004:         }
                   12005:         return undef;
1.986     foxr     12006:     }
1.992     raeburn  12007: 
1.1074    raeburn  12008:     sub get_internet_names {
                   12009:         my ($lonid) = @_;
                   12010:         return if ($lonid eq '');
                   12011:         my ($idnref,$cached)=
                   12012:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   12013:         if ($cached) {
                   12014:             return $idnref;
                   12015:         }
                   12016:         my $ip = &get_host_ip($lonid);
                   12017:         my @hosts = &get_hosts_from_ip($ip);
                   12018:         my %iphost = &get_iphost();
                   12019:         my (@idns,%seen);
                   12020:         foreach my $id (@hosts) {
                   12021:             my $dom = &host_domain($id);
                   12022:             my $prim_id = &domain($dom,'primary');
                   12023:             my $prim_ip = &get_host_ip($prim_id);
                   12024:             next if ($seen{$prim_ip});
                   12025:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   12026:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   12027:                     my $intdom = &internet_dom($id);
                   12028:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   12029:                         push(@idns,$intdom);
                   12030:                     }
                   12031:                 }
                   12032:             }
                   12033:             $seen{$prim_ip} = 1;
                   12034:         }
1.1219    raeburn  12035:         return &do_cache_new('internetnames',$lonid,\@idns,12*60*60);
1.1074    raeburn  12036:     }
                   12037: 
1.986     foxr     12038: }
                   12039: 
1.1079    raeburn  12040: sub all_loncaparevs {
                   12041:     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);
                   12042: }
                   12043: 
1.1227    raeburn  12044: # ---------------------------------------------------------- Read loncaparev table
                   12045: {
                   12046:     sub load_loncaparevs { 
                   12047:         if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
                   12048:             if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   12049:                 while (my $configline=<$config>) {
                   12050:                     chomp($configline);
                   12051:                     my ($hostid,$loncaparev)=split(/:/,$configline);
                   12052:                     $loncaparevs{$hostid}=$loncaparev;
                   12053:                 }
                   12054:                 close($config);
                   12055:             }
                   12056:         }
                   12057:     }
                   12058: }
                   12059: 
                   12060: # ---------------------------------------------------------- Read serverhostID table
                   12061: {
                   12062:     sub load_serverhomeIDs {
                   12063:         if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   12064:             if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   12065:                 while (my $configline=<$config>) {
                   12066:                     chomp($configline);
                   12067:                     my ($name,$id)=split(/:/,$configline);
                   12068:                     $serverhomeIDs{$name}=$id;
                   12069:                 }
                   12070:                 close($config);
                   12071:             }
                   12072:         }
                   12073:     }
                   12074: }
                   12075: 
                   12076: 
1.862     albertel 12077: BEGIN {
                   12078: 
                   12079: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   12080:     unless ($readit) {
                   12081: {
                   12082:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   12083:     %perlvar = (%perlvar,%{$configvars});
                   12084: }
                   12085: 
                   12086: 
1.1       albertel 12087: # ------------------------------------------------------ Read spare server file
                   12088: {
1.448     albertel 12089:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 12090: 
                   12091:     while (my $configline=<$config>) {
                   12092:        chomp($configline);
1.284     matthew  12093:        if ($configline) {
1.784     albertel 12094: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 12095: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 12096: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 12097:        }
                   12098:     }
1.448     albertel 12099:     close($config);
1.1       albertel 12100: }
1.11      www      12101: # ------------------------------------------------------------ Read permissions
                   12102: {
1.448     albertel 12103:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      12104: 
                   12105:     while (my $configline=<$config>) {
1.448     albertel 12106: 	chomp($configline);
                   12107: 	if ($configline) {
                   12108: 	    my ($role,$perm)=split(/ /,$configline);
                   12109: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   12110: 	}
1.11      www      12111:     }
1.448     albertel 12112:     close($config);
1.11      www      12113: }
                   12114: 
                   12115: # -------------------------------------------- Read plain texts for permissions
                   12116: {
1.448     albertel 12117:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      12118: 
                   12119:     while (my $configline=<$config>) {
1.448     albertel 12120: 	chomp($configline);
                   12121: 	if ($configline) {
1.742     raeburn  12122: 	    my ($short,@plain)=split(/:/,$configline);
                   12123:             %{$prp{$short}} = ();
                   12124: 	    if (@plain > 0) {
                   12125:                 $prp{$short}{'std'} = $plain[0];
                   12126:                 for (my $i=1; $i<@plain; $i++) {
                   12127:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   12128:                 }
                   12129:             }
1.448     albertel 12130: 	}
1.135     www      12131:     }
1.448     albertel 12132:     close($config);
1.135     www      12133: }
                   12134: 
                   12135: # ---------------------------------------------------------- Read package table
                   12136: {
1.448     albertel 12137:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      12138: 
                   12139:     while (my $configline=<$config>) {
1.483     albertel 12140: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 12141: 	chomp($configline);
                   12142: 	my ($short,$plain)=split(/:/,$configline);
                   12143: 	my ($pack,$name)=split(/\&/,$short);
                   12144: 	if ($plain ne '') {
                   12145: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   12146: 	    $packagetab{$short}=$plain; 
                   12147: 	}
1.11      www      12148:     }
1.448     albertel 12149:     close($config);
1.329     matthew  12150: }
                   12151: 
1.1073    raeburn  12152: # ---------------------------------------------------------- Read loncaparev table
1.1227    raeburn  12153: 
                   12154: &load_loncaparevs();
1.1073    raeburn  12155: 
1.1074    raeburn  12156: # ---------------------------------------------------------- Read serverhostID table
                   12157: 
1.1227    raeburn  12158: &load_serverhomeIDs();
                   12159: 
                   12160: # ---------------------------------------------------------- Read releaseslist XML
1.1079    raeburn  12161: {
                   12162:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   12163:     if (-e $file) {
                   12164:         my $parser = HTML::LCParser->new($file);
                   12165:         while (my $token = $parser->get_token()) {
                   12166:             if ($token->[0] eq 'S') {
                   12167:                 my $item = $token->[1];
                   12168:                 my $name = $token->[2]{'name'};
                   12169:                 my $value = $token->[2]{'value'};
                   12170:                 if ($item ne '' && $name ne '' && $value ne '') {
                   12171:                     my $release = $parser->get_text();
                   12172:                     $release =~ s/(^\s*|\s*$ )//gx;
                   12173:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
                   12174:                 }
                   12175:             }
                   12176:         }
                   12177:     }
1.1073    raeburn  12178: }
                   12179: 
1.1138    raeburn  12180: # ---------------------------------------------------------- Read managers table
                   12181: {
                   12182:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
                   12183:         if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
                   12184:             while (my $configline=<$config>) {
                   12185:                 chomp($configline);
                   12186:                 next if ($configline =~ /^\#/);
                   12187:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   12188:                     $managerstab{$configline} = 1;
                   12189:                 }
                   12190:             }
                   12191:             close($config);
                   12192:         }
                   12193:     }
                   12194: }
                   12195: 
1.329     matthew  12196: # ------------- set up temporary directory
                   12197: {
1.1117    foxr     12198:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  12199: 
1.11      www      12200: }
                   12201: 
1.794     albertel 12202: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   12203: 				'compress_threshold'=> 20_000,
                   12204:  			        });
1.185     www      12205: 
1.281     www      12206: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      12207: $dumpcount=0;
1.958     www      12208: $locknum=0;
1.22      www      12209: 
1.163     harris41 12210: &logtouch();
1.672     albertel 12211: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      12212: $readit=1;
1.564     albertel 12213:     {
                   12214: 	use integer;
                   12215: 	my $test=(2**32)+1;
1.568     albertel 12216: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 12217: 	&logthis(" Detected 64bit platform ($_64bit)");
                   12218:     }
1.1214    raeburn  12219: 
                   12220:     {
                   12221:         eval {
                   12222:             ($apache) =
                   12223:                 (Apache2::ServerUtil::get_server_version() =~ m{Apache/(\d+\.\d+)});
                   12224:         };
                   12225:         if ($@) {
                   12226:            $apache = 1.3;
1.1213    raeburn  12227:         }
                   12228:     }
1.1214    raeburn  12229: 
1.195     www      12230: }
1.1       albertel 12231: }
1.179     www      12232: 
1.1       albertel 12233: 1;
1.191     harris41 12234: __END__
                   12235: 
1.243     albertel 12236: =pod
                   12237: 
1.191     harris41 12238: =head1 NAME
                   12239: 
1.243     albertel 12240: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 12241: 
                   12242: =head1 SYNOPSIS
                   12243: 
1.243     albertel 12244: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 12245: 
                   12246:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   12247: 
1.243     albertel 12248: Common parameters:
                   12249: 
                   12250: =over 4
                   12251: 
                   12252: =item *
                   12253: 
                   12254: $uname : an internal username (if $cname expecting a course Id specifically)
                   12255: 
                   12256: =item *
                   12257: 
                   12258: $udom : a domain (if $cdom expecting a course's domain specifically)
                   12259: 
                   12260: =item *
                   12261: 
                   12262: $symb : a resource instance identifier
                   12263: 
                   12264: =item *
                   12265: 
                   12266: $namespace : the name of a .db file that contains the data needed or
                   12267: being set.
                   12268: 
                   12269: =back
                   12270: 
1.394     bowersj2 12271: =head1 OVERVIEW
1.191     harris41 12272: 
1.394     bowersj2 12273: lonnet provides subroutines which interact with the
                   12274: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   12275: about classes, users, and resources.
1.243     albertel 12276: 
                   12277: For many of these objects you can also use this to store data about
                   12278: them or modify them in various ways.
1.191     harris41 12279: 
1.394     bowersj2 12280: =head2 Symbs
1.191     harris41 12281: 
1.394     bowersj2 12282: To identify a specific instance of a resource, LON-CAPA uses symbols
                   12283: or "symbs"X<symb>. These identifiers are built from the URL of the
                   12284: map, the resource number of the resource in the map, and the URL of
                   12285: the resource itself. The latter is somewhat redundant, but might help
                   12286: if maps change.
                   12287: 
                   12288: An example is
                   12289: 
                   12290:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   12291: 
                   12292: The respective map entry is
                   12293: 
                   12294:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   12295:   title="Problem 2">
                   12296:  </resource>
                   12297: 
                   12298: Symbs are used by the random number generator, as well as to store and
                   12299: restore data specific to a certain instance of for example a problem.
                   12300: 
                   12301: =head2 Storing And Retrieving Data
                   12302: 
                   12303: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   12304: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   12305: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   12306: is is the non-critical message twin of cstore. These functions are for
                   12307: handlers to store a perl hash to a user's permanent data space in an
                   12308: easy manner, and to retrieve it again on another call. It is expected
                   12309: that a handler would use this once at the beginning to retrieve data,
                   12310: and then again once at the end to send only the new data back.
                   12311: 
                   12312: The data is stored in the user's data directory on the user's
                   12313: homeserver under the ID of the course.
                   12314: 
                   12315: The hash that is returned by restore will have all of the previous
                   12316: value for all of the elements of the hash.
                   12317: 
                   12318: Example:
                   12319: 
                   12320:  #creating a hash
                   12321:  my %hash;
                   12322:  $hash{'foo'}='bar';
                   12323: 
                   12324:  #storing it
                   12325:  &Apache::lonnet::cstore(\%hash);
                   12326: 
                   12327:  #changing a value
                   12328:  $hash{'foo'}='notbar';
                   12329: 
                   12330:  #adding a new value
                   12331:  $hash{'bar'}='foo';
                   12332:  &Apache::lonnet::cstore(\%hash);
                   12333: 
                   12334:  #retrieving the hash
                   12335:  my %history=&Apache::lonnet::restore();
                   12336: 
                   12337:  #print the hash
                   12338:  foreach my $key (sort(keys(%history))) {
                   12339:    print("\%history{$key} = $history{$key}");
                   12340:  }
                   12341: 
                   12342: Will print out:
1.191     harris41 12343: 
1.394     bowersj2 12344:  %history{1:foo} = bar
                   12345:  %history{1:keys} = foo:timestamp
                   12346:  %history{1:timestamp} = 990455579
                   12347:  %history{2:bar} = foo
                   12348:  %history{2:foo} = notbar
                   12349:  %history{2:keys} = foo:bar:timestamp
                   12350:  %history{2:timestamp} = 990455580
                   12351:  %history{bar} = foo
                   12352:  %history{foo} = notbar
                   12353:  %history{timestamp} = 990455580
                   12354:  %history{version} = 2
                   12355: 
                   12356: Note that the special hash entries C<keys>, C<version> and
                   12357: C<timestamp> were added to the hash. C<version> will be equal to the
                   12358: total number of versions of the data that have been stored. The
                   12359: C<timestamp> attribute will be the UNIX time the hash was
                   12360: stored. C<keys> is available in every historical section to list which
                   12361: keys were added or changed at a specific historical revision of a
                   12362: hash.
                   12363: 
                   12364: B<Warning>: do not store the hash that restore returns directly. This
                   12365: will cause a mess since it will restore the historical keys as if the
                   12366: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 12367: 
1.394     bowersj2 12368: Calling convention:
1.191     harris41 12369: 
1.1225    raeburn  12370:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname);
                   12371:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname);
1.191     harris41 12372: 
1.394     bowersj2 12373: For more detailed information, see lonnet specific documentation.
1.191     harris41 12374: 
1.394     bowersj2 12375: =head1 RETURN MESSAGES
1.191     harris41 12376: 
1.394     bowersj2 12377: =over 4
1.191     harris41 12378: 
1.394     bowersj2 12379: =item * B<con_lost>: unable to contact remote host
1.191     harris41 12380: 
1.394     bowersj2 12381: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   12382: when the connection is brought back up
1.191     harris41 12383: 
1.394     bowersj2 12384: =item * B<con_failed>: unable to contact remote host and unable to save message
                   12385: for later delivery
1.191     harris41 12386: 
1.967     bisitz   12387: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 12388: 
1.394     bowersj2 12389: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 12390: that was requested
1.191     harris41 12391: 
1.243     albertel 12392: =back
1.191     harris41 12393: 
1.243     albertel 12394: =head1 PUBLIC SUBROUTINES
1.191     harris41 12395: 
1.243     albertel 12396: =head2 Session Environment Functions
1.191     harris41 12397: 
1.243     albertel 12398: =over 4
1.191     harris41 12399: 
1.394     bowersj2 12400: =item * 
                   12401: X<appenv()>
1.949     raeburn  12402: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 12403: the user envirnoment file, and will be restored for each access this
1.620     albertel 12404: user makes during this session, also modifies the %env for the current
1.949     raeburn  12405: process. Optional rolesarrayref - if defined contains a reference to an array
                   12406: of roles which are exempt from the restriction on modifying user.role entries 
                   12407: in the user's environment.db and in %env.    
1.191     harris41 12408: 
                   12409: =item *
1.394     bowersj2 12410: X<delenv()>
1.987     raeburn  12411: B<delenv($delthis,$regexp)>: removes all items from the session
                   12412: environment file that begin with $delthis. If the 
                   12413: optional second arg - $regexp - is true, $delthis is treated as a 
                   12414: regular expression, otherwise \Q$delthis\E is used. 
                   12415: The values are also deleted from the current processes %env.
1.191     harris41 12416: 
1.795     albertel 12417: =item * get_env_multiple($name) 
                   12418: 
                   12419: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   12420: values may be defined and end up as an array ref.
                   12421: 
                   12422: returns an array of values
                   12423: 
1.243     albertel 12424: =back
                   12425: 
                   12426: =head2 User Information
1.191     harris41 12427: 
1.243     albertel 12428: =over 4
1.191     harris41 12429: 
                   12430: =item *
1.394     bowersj2 12431: X<queryauthenticate()>
                   12432: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 12433: authentication scheme
                   12434: 
                   12435: =item *
1.394     bowersj2 12436: X<authenticate()>
1.1073    raeburn  12437: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 12438: authenticate user from domain's lib servers (first use the current
                   12439: one). C<$upass> should be the users password.
1.1073    raeburn  12440: $checkdefauth is optional (value is 1 if a check should be made to
                   12441:    authenticate user using default authentication method, and allow
                   12442:    account creation if username does not have account in the domain).
                   12443: $clientcancheckhost is optional (value is 1 if checking whether the
                   12444:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 12445: 
                   12446: =item *
1.394     bowersj2 12447: X<homeserver()>
                   12448: B<homeserver($uname,$udom)>: find the server which has
                   12449: the user's directory and files (there must be only one), this caches
                   12450: the answer, and also caches if there is a borken connection.
1.191     harris41 12451: 
                   12452: =item *
1.394     bowersj2 12453: X<idget()>
                   12454: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   12455: (IDs are a unique resource in a domain, there must be only 1 ID per
                   12456: username, and only 1 username per ID in a specific domain) (returns
                   12457: hash: id=>name,id=>name)
1.191     harris41 12458: 
                   12459: =item *
1.394     bowersj2 12460: X<idrget()>
                   12461: B<idrget($udom,@unames)>: find the IDs behind a list of
                   12462: usernames (returns hash: name=>id,name=>id)
1.191     harris41 12463: 
                   12464: =item *
1.394     bowersj2 12465: X<idput()>
                   12466: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 12467: 
                   12468: =item *
1.394     bowersj2 12469: X<rolesinit()>
1.1169    droeschl 12470: B<rolesinit($udom,$username)>: get user privileges.
                   12471: returns user role, first access and timer interval hashes
1.243     albertel 12472: 
                   12473: =item *
1.1171    droeschl 12474: X<privileged()>
                   12475: B<privileged($username,$domain)>: returns a true if user has a
                   12476: privileged and active role (i.e. su or dc), false otherwise.
                   12477: 
                   12478: =item *
1.551     albertel 12479: X<getsection()>
                   12480: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 12481: course $cname, return section name/number or '' for "not in course"
                   12482: and '-1' for "no section"
                   12483: 
                   12484: =item *
1.394     bowersj2 12485: X<userenvironment()>
                   12486: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 12487: passed in @what from the requested user's environment, returns a hash
                   12488: 
1.858     raeburn  12489: =item * 
                   12490: X<userlog_query()>
1.859     albertel 12491: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   12492: activity.log file. %filters defines filters applied when parsing the
                   12493: log file. These can be start or end timestamps, or the type of action
                   12494: - log to look for Login or Logout events, check for Checkin or
                   12495: Checkout, role for role selection. The response is in the form
                   12496: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   12497: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  12498: 
1.243     albertel 12499: =back
                   12500: 
                   12501: =head2 User Roles
                   12502: 
                   12503: =over 4
                   12504: 
                   12505: =item *
                   12506: 
1.810     raeburn  12507: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 12508:  F: full access
                   12509:  U,I,K: authentication modes (cxx only)
                   12510:  '': forbidden
                   12511:  1: user needs to choose course
                   12512:  2: browse allowed
1.766     albertel 12513:  A: passphrase authentication needed
1.243     albertel 12514: 
                   12515: =item *
                   12516: 
1.1192    raeburn  12517: constructaccess($url,$setpriv) : check for access to construction space URL
                   12518: 
                   12519: See if the owner domain and name in the URL match those in the
                   12520: expected environment.  If so, return three element list
                   12521: ($ownername,$ownerdomain,$ownerhome).
                   12522: 
                   12523: Otherwise return the null string.
                   12524: 
                   12525: If second argument 'setpriv' is true, it assigns the privileges,
                   12526: and returns the same three element list, unless the owner has
                   12527: blocked "ad hoc" Domain Coordinator access to the Author Space,
                   12528: in which case the null string is returned.
                   12529: 
                   12530: =item *
                   12531: 
1.243     albertel 12532: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   12533: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   12534: and course level
                   12535: 
                   12536: =item *
                   12537: 
1.988     raeburn  12538: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   12539: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  12540: $type is Course (default) or Community.
1.988     raeburn  12541: If $forcedefault evaluates to true, text returned will be default 
                   12542: text for $type. Otherwise, if this is a course, the text returned 
                   12543: will be a custom name for the role (if defined in the course's 
                   12544: environment).  If no custom name is defined the default is returned.
                   12545:    
1.832     raeburn  12546: =item *
                   12547: 
1.1219    raeburn  12548: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv) :
1.858     raeburn  12549: All arguments are optional. Returns a hash of a roles, either for
                   12550: co-author/assistant author roles for a user's Construction Space
1.906     albertel 12551: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  12552: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   12553: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   12554: For each key, value is set to colon-separated start and end times for
                   12555: the role.  If no username and domain are specified, will default to
1.934     raeburn  12556: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  12557: of role statuses (active, future or previous), roles 
                   12558: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   12559: to restrict the list of roles reported. If no array ref is 
                   12560: provided for types, will default to return only active roles.
1.834     albertel 12561: 
1.1195    raeburn  12562: =item *
                   12563: 
                   12564: in_course($udom,$uname,$cdom,$cnum,$type,$hideprivileged) : determine if
1.1196    raeburn  12565: user: $uname:$udom has a role in the course: $cdom_$cnum. 
                   12566: 
                   12567: Additional optional arguments are: $type (if role checking is to be restricted 
                   12568: to certain user status types -- previous (expired roles), active (currently
1.1195    raeburn  12569: available roles) or future (roles available in the future), and
                   12570: $hideprivileged -- if true will not report course roles for users who
1.1219    raeburn  12571: have active Domain Coordinator role in course's domain or in additional
                   12572: domains (specified in 'Domains to check for privileged users' in course
                   12573: environment -- set via:  Course Settings -> Classlists and staff listing).
                   12574: 
                   12575: =item *
                   12576: 
                   12577: privileged($username,$domain,$possdomains,$possroles) : returns 1 if user
                   12578: $username:$domain is a privileged user (e.g., Domain Coordinator or Super User)
                   12579: $possdomains and $possroles are optional array refs -- to domains to check and
                   12580: roles to check.  If $possdomains is not specified, a dump will be done of the
                   12581: users' roles.db to check for a dc or su role in any domain. This can be
                   12582: time consuming if &privileged is called repeatedly (e.g., when displaying a
                   12583: classlist), so in such cases, supplying a $possdomains array is preferred, as
                   12584: this then allows &privileged_by_domain() to be used, which caches the identity
                   12585: of privileged users, eliminating the need for repeated calls to &dump().
                   12586: 
                   12587: =item *
                   12588: 
                   12589: privileged_by_domain($possdomains,$roles) : returns a hash of a hash of a hash,
                   12590: where the outer hash keys are domains specified in the $possdomains array ref,
                   12591: next inner hash keys are privileged roles specified in the $roles array ref,
                   12592: and the innermost hash contains key = value pairs for username:domain = end:start
                   12593: for active or future "privileged" users with that role in that domain. To avoid
                   12594: repeated dumps of domain roles -- via &get_domain_roles() -- contents of the
                   12595: innerhash are cached using priv_$role and $dom as the identifiers.
1.1195    raeburn  12596: 
1.243     albertel 12597: =back
                   12598: 
                   12599: =head2 User Modification
                   12600: 
                   12601: =over 4
                   12602: 
                   12603: =item *
                   12604: 
1.957     raeburn  12605: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 12606: user for the level given by URL.  Optional start and end dates (leave empty
                   12607: string or zero for "no date")
1.191     harris41 12608: 
                   12609: =item *
                   12610: 
1.243     albertel 12611: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   12612: change a users, password, possible return values are: ok,
                   12613: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   12614: refused
1.191     harris41 12615: 
                   12616: =item *
                   12617: 
1.243     albertel 12618: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 12619: 
                   12620: =item *
                   12621: 
1.1058    raeburn  12622: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   12623:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   12624: 
                   12625: will update user information (firstname,middlename,lastname,generation,
                   12626: permanentemail), and if forceid is true, student/employee ID also.
                   12627: A user's institutional affiliation(s) can also be updated.
                   12628: User information fields will not be overwritten with empty entries 
                   12629: unless the field is included in the $candelete array reference.
                   12630: This array is included when a single user is modified via "Manage Users",
                   12631: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 12632: 
                   12633: =item *
                   12634: 
1.286     matthew  12635: modifystudent
                   12636: 
1.957     raeburn  12637: modify a student's enrollment and identification information.
1.286     matthew  12638: The course id is resolved based on the current users environment.  
                   12639: This means the envoking user must be a course coordinator or otherwise
                   12640: associated with a course.
                   12641: 
1.297     matthew  12642: This call is essentially a wrapper for lonnet::modifyuser and
                   12643: lonnet::modify_student_enrollment
1.286     matthew  12644: 
                   12645: Inputs: 
                   12646: 
                   12647: =over 4
                   12648: 
1.957     raeburn  12649: =item B<$udom> Student's loncapa domain
1.286     matthew  12650: 
1.957     raeburn  12651: =item B<$uname> Student's loncapa login name
1.286     matthew  12652: 
1.964     bisitz   12653: =item B<$uid> Student/Employee ID
1.286     matthew  12654: 
1.957     raeburn  12655: =item B<$umode> Student's authentication mode
1.286     matthew  12656: 
1.957     raeburn  12657: =item B<$upass> Student's password
1.286     matthew  12658: 
1.957     raeburn  12659: =item B<$first> Student's first name
1.286     matthew  12660: 
1.957     raeburn  12661: =item B<$middle> Student's middle name
1.286     matthew  12662: 
1.957     raeburn  12663: =item B<$last> Student's last name
1.286     matthew  12664: 
1.957     raeburn  12665: =item B<$gene> Student's generation
1.286     matthew  12666: 
1.957     raeburn  12667: =item B<$usec> Student's section in course
1.286     matthew  12668: 
                   12669: =item B<$end> Unix time of the roles expiration
                   12670: 
                   12671: =item B<$start> Unix time of the roles start date
                   12672: 
                   12673: =item B<$forceid> If defined, allow $uid to be changed
                   12674: 
                   12675: =item B<$desiredhome> server to use as home server for student
                   12676: 
1.957     raeburn  12677: =item B<$email> Student's permanent e-mail address
                   12678: 
                   12679: =item B<$type> Type of enrollment (auto or manual)
                   12680: 
1.963     raeburn  12681: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   12682: 
                   12683: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  12684: 
1.963     raeburn  12685: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  12686: 
1.963     raeburn  12687: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  12688: 
1.1216    raeburn  12689: =item B<$inststatus> institutional status of user - : separated string of escaped status types
                   12690: 
                   12691: =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  12692: 
1.286     matthew  12693: =back
1.297     matthew  12694: 
                   12695: =item *
                   12696: 
                   12697: modify_student_enrollment
                   12698: 
                   12699: Change a students enrollment status in a class.  The environment variable
                   12700: 'role.request.course' must be defined for this function to proceed.
                   12701: 
                   12702: Inputs:
                   12703: 
                   12704: =over 4
                   12705: 
                   12706: =item $udom, students domain
                   12707: 
                   12708: =item $uname, students name
                   12709: 
                   12710: =item $uid, students user id
                   12711: 
                   12712: =item $first, students first name
                   12713: 
                   12714: =item $middle
                   12715: 
                   12716: =item $last
                   12717: 
                   12718: =item $gene
                   12719: 
                   12720: =item $usec
                   12721: 
                   12722: =item $end
                   12723: 
                   12724: =item $start
                   12725: 
1.957     raeburn  12726: =item $type
                   12727: 
                   12728: =item $locktype
                   12729: 
                   12730: =item $cid
                   12731: 
                   12732: =item $selfenroll
                   12733: 
                   12734: =item $context
                   12735: 
1.1216    raeburn  12736: =item $credits, number of credits student will earn from this class
                   12737: 
1.297     matthew  12738: =back
                   12739: 
1.191     harris41 12740: 
                   12741: =item *
                   12742: 
1.243     albertel 12743: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   12744: custom role; give a custom role to a user for the level given by URL.  Specify
                   12745: name and domain of role author, and role name
1.191     harris41 12746: 
                   12747: =item *
                   12748: 
1.243     albertel 12749: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 12750: 
                   12751: =item *
                   12752: 
1.243     albertel 12753: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   12754: 
                   12755: =back
                   12756: 
                   12757: =head2 Course Infomation
                   12758: 
                   12759: =over 4
1.191     harris41 12760: 
                   12761: =item *
                   12762: 
1.1118    foxr     12763: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 12764: specified course id, including all environment settings for the
                   12765: course, the description of the course will be in the hash under the
                   12766: key 'description'
1.191     harris41 12767: 
1.1118    foxr     12768: $options is an optional parameter that if supplied is a hash reference that controls
                   12769: what how this function works.  It has the following key/values:
                   12770: 
                   12771: =over 4
                   12772: 
                   12773: =item freshen_cache
                   12774: 
                   12775: If defined, and the environment cache for the course is valid, it is 
                   12776: returned in the returned hash.
                   12777: 
                   12778: =item one_time
                   12779: 
                   12780: If defined, the last cache time is set to _now_
                   12781: 
                   12782: =item user
                   12783: 
                   12784: If defined, the supplied username is used instead of the current user.
                   12785: 
                   12786: 
                   12787: =back
                   12788: 
1.191     harris41 12789: =item *
                   12790: 
1.624     albertel 12791: resdata($name,$domain,$type,@which) : request for current parameter
                   12792: setting for a specific $type, where $type is either 'course' or 'user',
                   12793: @what should be a list of parameters to ask about. This routine caches
                   12794: answers for 5 minutes.
1.243     albertel 12795: 
1.877     foxr     12796: =item *
                   12797: 
                   12798: get_courseresdata($courseid, $domain) : dump the entire course resource
                   12799: data base, returning a hash that is keyed by the resource name and has
                   12800: values that are the resource value.  I believe that the timestamps and
                   12801: versions are also returned.
                   12802: 
1.243     albertel 12803: =back
                   12804: 
                   12805: =head2 Course Modification
                   12806: 
                   12807: =over 4
1.191     harris41 12808: 
                   12809: =item *
                   12810: 
1.243     albertel 12811: writecoursepref($courseid,%prefs) : write preferences (environment
                   12812: database) for a course
1.191     harris41 12813: 
                   12814: =item *
                   12815: 
1.1011    raeburn  12816: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   12817: 
                   12818: =item *
                   12819: 
1.1038    raeburn  12820: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 12821: 
1.1167    droeschl 12822: =item *
                   12823: 
                   12824: is_course($courseid), is_course($cdom, $cnum)
                   12825: 
                   12826: Accepts either a combined $courseid (in the form of domain_courseid) or the
                   12827: two component version $cdom, $cnum. It checks if the specified course exists.
                   12828: 
                   12829: Returns:
                   12830:     undef if the course doesn't exist, otherwise
                   12831:     in scalar context the combined courseid.
                   12832:     in list context the two components of the course identifier, domain and 
                   12833:     courseid.    
                   12834: 
1.243     albertel 12835: =back
                   12836: 
                   12837: =head2 Resource Subroutines
                   12838: 
                   12839: =over 4
1.191     harris41 12840: 
                   12841: =item *
                   12842: 
1.243     albertel 12843: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 12844: 
                   12845: =item *
                   12846: 
1.243     albertel 12847: repcopy($filename) : subscribes to the requested file, and attempts to
                   12848: replicate from the owning library server, Might return
1.607     raeburn  12849: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   12850: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 12851: resource. Expects the local filesystem pathname
                   12852: (/home/httpd/html/res/....)
                   12853: 
                   12854: =back
                   12855: 
                   12856: =head2 Resource Information
                   12857: 
                   12858: =over 4
1.191     harris41 12859: 
                   12860: =item *
                   12861: 
1.1228    raeburn  12862: EXT($varname,$symb,$udom,$uname,$usection,$recurse,$cid) : evaluates 
                   12863: and returns the value of a variety of different possible values,
                   12864: $varname should be a request string, and the other parameters can be
                   12865: used to specify who and what one is asking about. Ordinarily, $cid 
                   12866: does not need to be specified, as it is retrived from 
                   12867: $env{'request.course.id'}, but &Apache::lonnet::EXT() is called
                   12868: within lonuserstate::loadmap() when initializing a course, before
                   12869: $env{'request.course.id'} has been set, so it needs to be provided
                   12870: in that one case.
1.243     albertel 12871: 
                   12872: Possible values for $varname are environment.lastname (or other item
                   12873: from the envirnment hash), user.name (or someother aspect about the
                   12874: user), resource.0.maxtries (or some other part and parameter of a
                   12875: resource)
1.204     albertel 12876: 
                   12877: =item *
                   12878: 
1.243     albertel 12879: directcondval($number) : get current value of a condition; reads from a state
                   12880: string
1.204     albertel 12881: 
                   12882: =item *
                   12883: 
1.243     albertel 12884: condval($condidx) : value of condition index based on state
1.204     albertel 12885: 
                   12886: =item *
                   12887: 
1.243     albertel 12888: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   12889: resource's metadata, $what should be either a specific key, or either
                   12890: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   12891: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   12892: 
                   12893: this function automatically caches all requests
1.191     harris41 12894: 
                   12895: =item *
                   12896: 
1.243     albertel 12897: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   12898: network of library servers; returns file handle of where SQL and regex results
                   12899: will be stored for query
1.191     harris41 12900: 
                   12901: =item *
                   12902: 
1.243     albertel 12903: symbread($filename) : return symbolic list entry (filename argument optional);
                   12904: returns the data handle
1.191     harris41 12905: 
                   12906: =item *
                   12907: 
1.1190    raeburn  12908: symbverify($symb,$thisfn,$encstate) : verifies that $symb actually exists
                   12909: and is a possible symb for the URL in $thisfn, and if is an encrypted
1.582     albertel 12910: resource that the user accessed using /enc/ returns a 1 on success, 0
1.1190    raeburn  12911: on failure, user must be in a course, as it assumes the existence of
                   12912: the course initial hash, and uses $env('request.course.id'}.  The third
                   12913: arg is an optional reference to a scalar.  If this arg is passed in the 
                   12914: call to symbverify, it will be set to 1 if the symb has been set to be 
                   12915: encrypted; otherwise it will be null.  
1.191     harris41 12916: 
                   12917: =item *
                   12918: 
1.243     albertel 12919: symbclean($symb) : removes versions numbers from a symb, returns the
                   12920: cleaned symb
1.191     harris41 12921: 
                   12922: =item *
                   12923: 
1.243     albertel 12924: is_on_map($uri) : checks if the $uri is somewhere on the current
                   12925: course map, user must be in a course for it to work.
1.191     harris41 12926: 
                   12927: =item *
                   12928: 
1.243     albertel 12929: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 12930: 
                   12931: =item *
                   12932: 
1.243     albertel 12933: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   12934: a random seed, all arguments are optional, if they aren't sent it uses the
                   12935: environment to derive them. Note: if symb isn't sent and it can't get one
                   12936: from &symbread it will use the current time as its return value
1.191     harris41 12937: 
                   12938: =item *
                   12939: 
1.243     albertel 12940: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   12941: unfakeable, receipt
1.191     harris41 12942: 
                   12943: =item *
                   12944: 
1.620     albertel 12945: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 12946: 
                   12947: =item *
                   12948: 
1.243     albertel 12949: countacc($url) : count the number of accesses to a given URL
1.191     harris41 12950: 
                   12951: =item *
                   12952: 
1.243     albertel 12953: 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 12954: 
                   12955: =item *
                   12956: 
1.243     albertel 12957: 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 12958: 
                   12959: =item *
                   12960: 
1.243     albertel 12961: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 12962: 
                   12963: =item *
                   12964: 
1.243     albertel 12965: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   12966: forcing spreadsheet to reevaluate the resource scores next time.
                   12967: 
1.1195    raeburn  12968: =item * 
                   12969: 
1.1196    raeburn  12970: can_edit_resource($file,$cnum,$cdom,$resurl,$symb,$group) : determine if current user can edit a particular resource,
                   12971: when viewing in course context.
1.1195    raeburn  12972: 
1.1196    raeburn  12973:  input: six args -- filename (decluttered), course number, course domain,
                   12974:                     url, symb (if registered) and group (if this is a 
                   12975:                     group item -- e.g., bulletin board, group page etc.).
1.1195    raeburn  12976: 
1.1196    raeburn  12977:  output: array of five scalars --
1.1195    raeburn  12978:          $cfile -- url for file editing if editable on current server
                   12979:          $home -- homeserver of resource (i.e., for author if published,
                   12980:                                           or course if uploaded.).
                   12981:          $switchserver --  1 if server switch will be needed.
1.1196    raeburn  12982:          $forceedit -- 1 if icon/link should be to go to edit mode 
                   12983:          $forceview -- 1 if icon/link should be to go to view mode
1.1195    raeburn  12984: 
                   12985: =item *
                   12986: 
                   12987: is_course_upload($file,$cnum,$cdom)
                   12988: 
                   12989: Used in course context to determine if current file was uploaded to 
                   12990: the course (i.e., would be found in /userfiles/docs on the course's 
                   12991: homeserver.
                   12992: 
                   12993:   input: 3 args -- filename (decluttered), course number and course domain.
                   12994:   output: boolean -- 1 if file was uploaded.
                   12995: 
1.243     albertel 12996: =back
                   12997: 
                   12998: =head2 Storing/Retreiving Data
                   12999: 
                   13000: =over 4
1.191     harris41 13001: 
                   13002: =item *
                   13003: 
1.243     albertel 13004: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   13005: for this url; hashref needs to be given and should be a \%hashname; the
                   13006: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 13007: be derived from the env
1.191     harris41 13008: 
                   13009: =item *
                   13010: 
1.243     albertel 13011: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   13012: uses critical subroutine
1.191     harris41 13013: 
                   13014: =item *
                   13015: 
1.243     albertel 13016: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   13017: all args are optional
1.191     harris41 13018: 
                   13019: =item *
                   13020: 
1.717     albertel 13021: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   13022: dumps the complete (or key matching regexp) namespace into a hash
                   13023: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   13024: normally &store()ed into
                   13025: 
                   13026: $range should be either an integer '100' (give me the first 100
                   13027:                                            matching records)
                   13028:               or be  two integers sperated by a - with no spaces
                   13029:                  '30-50' (give me the 30th through the 50th matching
                   13030:                           records)
                   13031: 
                   13032: 
                   13033: =item *
                   13034: 
                   13035: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   13036: replaces a &store() version of data with a replacement set of data
                   13037: for a particular resource in a namespace passed in the $storehash hash 
                   13038: reference
                   13039: 
                   13040: =item *
                   13041: 
1.243     albertel 13042: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   13043: works very similar to store/cstore, but all data is stored in a
                   13044: temporary location and can be reset using tmpreset, $storehash should
                   13045: be a hash reference, returns nothing on success
1.191     harris41 13046: 
                   13047: =item *
                   13048: 
1.243     albertel 13049: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   13050: similar to restore, but all data is stored in a temporary location and
                   13051: can be reset using tmpreset. Returns a hash of values on success,
                   13052: error string otherwise.
1.191     harris41 13053: 
                   13054: =item *
                   13055: 
1.243     albertel 13056: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   13057: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 13058: 
                   13059: =item *
                   13060: 
1.243     albertel 13061: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   13062: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 13063: 
                   13064: =item *
                   13065: 
1.243     albertel 13066: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   13067: namesp ($udom and $uname are optional)
1.191     harris41 13068: 
                   13069: =item *
                   13070: 
1.702     albertel 13071: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 13072: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 13073: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  13074: 
1.702     albertel 13075: $range should be either an integer '100' (give me the first 100
                   13076:                                            matching records)
                   13077:               or be  two integers sperated by a - with no spaces
                   13078:                  '30-50' (give me the 30th through the 50th matching
                   13079:                           records)
1.449     matthew  13080: =item *
                   13081: 
                   13082: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   13083: $store can be a scalar, an array reference, or if the amount to be 
                   13084: incremented is > 1, a hash reference.
                   13085: 
                   13086: ($udom and $uname are optional)
1.191     harris41 13087: 
                   13088: =item *
                   13089: 
1.243     albertel 13090: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   13091: ($udom and $uname are optional)
1.191     harris41 13092: 
                   13093: =item *
                   13094: 
1.243     albertel 13095: cput($namespace,$storehash,$udom,$uname) : critical put
                   13096: ($udom and $uname are optional)
1.191     harris41 13097: 
                   13098: =item *
                   13099: 
1.748     albertel 13100: newput($namespace,$storehash,$udom,$uname) :
                   13101: 
                   13102: Attempts to store the items in the $storehash, but only if they don't
                   13103: currently exist, if this succeeds you can be certain that you have 
                   13104: successfully created a new key value pair in the $namespace db.
                   13105: 
                   13106: 
                   13107: Args:
                   13108:  $namespace: name of database to store values to
                   13109:  $storehash: hashref to store to the db
                   13110:  $udom: (optional) domain of user containing the db
                   13111:  $uname: (optional) name of user caontaining the db
                   13112: 
                   13113: Returns:
                   13114:  'ok' -> succeeded in storing all keys of $storehash
                   13115:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   13116:                         least <key> already existed in the db (other
                   13117:                         requested keys may also already exist)
1.967     bisitz   13118:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 13119:  'con_lost' -> unable to contact request server
                   13120:  'refused' -> action was not allowed by remote machine
                   13121: 
                   13122: 
                   13123: =item *
                   13124: 
1.243     albertel 13125: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   13126: reference filled in from namesp (encrypts the return communication)
                   13127: ($udom and $uname are optional)
1.191     harris41 13128: 
                   13129: =item *
                   13130: 
1.243     albertel 13131: log($udom,$name,$home,$message) : write to permanent log for user; use
                   13132: critical subroutine
                   13133: 
1.806     raeburn  13134: =item *
                   13135: 
1.860     raeburn  13136: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   13137: array reference filled in from namespace found in domain level on either
                   13138: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  13139: 
                   13140: =item *
                   13141: 
1.860     raeburn  13142: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   13143: domain level either on specified domain server ($uhome) or primary domain 
                   13144: server ($udom and $uhome are optional)
1.806     raeburn  13145: 
1.943     raeburn  13146: =item * 
                   13147: 
                   13148: get_domain_defaults($target_domain) : returns hash with defaults for
                   13149: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
                   13150: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
                   13151: or localauth), initial password or a kerberos realm, language (e.g., en-us).
                   13152: Values are retrieved from cache (if current), or from domain's configuration.db
                   13153: (if available), or lastly from values in lonTabs/dns_domain,tab, 
                   13154: or lonTabs/domain.tab. 
                   13155: 
                   13156: %domdefaults = &get_auth_defaults($target_domain);
                   13157: 
1.243     albertel 13158: =back
                   13159: 
                   13160: =head2 Network Status Functions
                   13161: 
                   13162: =over 4
1.191     harris41 13163: 
                   13164: =item *
                   13165: 
1.1137    raeburn  13166: dirlist() : return directory list based on URI (first arg).
                   13167: 
                   13168: Inputs: 1 required, 5 optional.
                   13169: 
                   13170: =over
                   13171: 
                   13172: =item 
                   13173: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   13174: 
                   13175: =item
                   13176: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   13177: 
                   13178: =item
                   13179: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   13180: 
                   13181: =item
                   13182: $getpropath - boolean: 1 if prepend path using &propath(). 
                   13183: 
                   13184: =item
                   13185: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   13186: 
                   13187: =item 
                   13188: $alternateRoot - path to prepend in place of path from $uri.
                   13189: 
                   13190: =back
                   13191: 
                   13192: Returns: Array of up to two items.
                   13193: 
                   13194: =over
                   13195: 
                   13196: a reference to an array of files/subdirectories
                   13197: 
                   13198: =over
                   13199: 
                   13200: Each element in the array of files/subdirectories is a & separated list of
                   13201: item name and the result of running stat on the item.  If dirlist was requested
                   13202: for a file instead of a directory, the item name will be ''. For a directory 
                   13203: listing, if the item is a metadata file, the element will end &N&M 
                   13204: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   13205: default copyright set (1).  
                   13206: 
                   13207: =back
                   13208: 
                   13209: a scalar containing error condition (if encountered).
                   13210: 
                   13211: =over
                   13212: 
                   13213: =item 
                   13214: no_host (no homeserver identified for $username:$domain).
                   13215: 
                   13216: =item 
                   13217: no_such_host (server contacted for listing not identified as valid host).
                   13218: 
                   13219: =item 
                   13220: con_lost (connection to remote server failed).
                   13221: 
                   13222: =item 
                   13223: refused (invalid $username:$domain received on lond side).
                   13224: 
                   13225: =item 
                   13226: no_such_dir (directory at specified path on lond side does not exist). 
                   13227: 
                   13228: =item 
                   13229: empty (directory at specified path on lond side is empty).
                   13230: 
                   13231: =over
                   13232: 
                   13233: This is currently not encountered because the &ls3, &ls2, 
                   13234: &ls (_handler) routines on the lond side do not filter out
                   13235: . and .. from a directory listing. 
                   13236: 
                   13237: =back
                   13238: 
                   13239: =back
                   13240: 
                   13241: =back
1.191     harris41 13242: 
                   13243: =item *
                   13244: 
1.243     albertel 13245: spareserver() : find server with least workload from spare.tab
                   13246: 
1.986     foxr     13247: 
                   13248: =item *
                   13249: 
                   13250: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   13251: if there is no corresponding loncapa host.
                   13252: 
1.243     albertel 13253: =back
                   13254: 
1.986     foxr     13255: 
1.243     albertel 13256: =head2 Apache Request
                   13257: 
                   13258: =over 4
1.191     harris41 13259: 
                   13260: =item *
                   13261: 
1.243     albertel 13262: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   13263: localhost, posts hash
                   13264: 
                   13265: =back
                   13266: 
                   13267: =head2 Data to String to Data
                   13268: 
                   13269: =over 4
1.191     harris41 13270: 
                   13271: =item *
                   13272: 
1.243     albertel 13273: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   13274: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 13275: 
                   13276: =item *
                   13277: 
1.243     albertel 13278: hashref2str($hashref) : convert a hashref into a string complete with
                   13279: escaping and '=' and '&' separators, supports elements that are
                   13280: arrayrefs and hashrefs
1.191     harris41 13281: 
                   13282: =item *
                   13283: 
1.243     albertel 13284: arrayref2str($arrayref) : convert an arrayref into a string complete
                   13285: with escaping and '&' separators, supports elements that are arrayrefs
                   13286: and hashrefs
1.191     harris41 13287: 
                   13288: =item *
                   13289: 
1.243     albertel 13290: str2hash($string) : convert string to hash using unescaping and
                   13291: splitting on '=' and '&', supports elements that are arrayrefs and
                   13292: hashrefs
1.191     harris41 13293: 
                   13294: =item *
                   13295: 
1.243     albertel 13296: str2array($string) : convert string to hash using unescaping and
                   13297: splitting on '&', supports elements that are arrayrefs and hashrefs
                   13298: 
                   13299: =back
                   13300: 
                   13301: =head2 Logging Routines
                   13302: 
                   13303: 
                   13304: These routines allow one to make log messages in the lonnet.log and
                   13305: lonnet.perm logfiles.
1.191     harris41 13306: 
1.1119    foxr     13307: =over 4
                   13308: 
1.191     harris41 13309: =item *
                   13310: 
1.243     albertel 13311: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 13312: 
                   13313: =item *
                   13314: 
1.243     albertel 13315: logthis() : append message to the normal lonnet.log file, it gets
                   13316: preiodically rolled over and deleted.
1.191     harris41 13317: 
                   13318: =item *
                   13319: 
1.243     albertel 13320: logperm() : append a permanent message to lonnet.perm.log, this log
                   13321: file never gets deleted by any automated portion of the system, only
                   13322: messages of critical importance should go in here.
                   13323: 
1.1119    foxr     13324: 
1.243     albertel 13325: =back
                   13326: 
                   13327: =head2 General File Helper Routines
                   13328: 
                   13329: =over 4
1.191     harris41 13330: 
                   13331: =item *
                   13332: 
1.481     raeburn  13333: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   13334: (a) files in /uploaded
                   13335:   (i) If a local copy of the file exists - 
                   13336:       compares modification date of local copy with last-modified date for 
                   13337:       definitive version stored on home server for course. If local copy is 
                   13338:       stale, requests a new version from the home server and stores it. 
                   13339:       If the original has been removed from the home server, then local copy 
                   13340:       is unlinked.
                   13341:   (ii) If local copy does not exist -
                   13342:       requests the file from the home server and stores it. 
                   13343:   
                   13344:   If $caller is 'uploadrep':  
                   13345:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   13346:     for request for files originally uploaded via DOCS. 
                   13347:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   13348:   
                   13349:   Otherwise:
                   13350:      This indicates a call from the content generation phase of the request.
                   13351:      -  returns the entire contents of the file or -1.
                   13352:      
                   13353: (b) files in /res
                   13354:    - returns the entire contents of a file or -1; 
                   13355:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 13356: 
1.712     albertel 13357: 
                   13358: =item *
                   13359: 
                   13360: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   13361:                   reference
                   13362: 
                   13363: returns either a stat() list of data about the file or an empty list
                   13364: if the file doesn't exist or couldn't find out about it (connection
                   13365: problems or user unknown)
                   13366: 
1.191     harris41 13367: =item *
                   13368: 
1.243     albertel 13369: filelocation($dir,$file) : returns file system location of a file
                   13370: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   13371: directory that relative $file lookups are to looked in ($dir of /a/dir
                   13372: and a file of ../bob will become /a/bob)
1.191     harris41 13373: 
                   13374: =item *
                   13375: 
                   13376: hreflocation($dir,$file) : returns file system location or a URL; same as
                   13377: filelocation except for hrefs
                   13378: 
                   13379: =item *
                   13380: 
                   13381: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   13382: 
1.243     albertel 13383: =back
                   13384: 
1.608     albertel 13385: =head2 Usererfile file routines (/uploaded*)
                   13386: 
                   13387: =over 4
                   13388: 
                   13389: =item *
                   13390: 
                   13391: userfileupload(): main rotine for putting a file in a user or course's
                   13392:                   filespace, arguments are,
                   13393: 
1.620     albertel 13394:  formname - required - this is the name of the element in $env where the
1.608     albertel 13395:            filename, and the contents of the file to create/modifed exist
1.620     albertel 13396:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   13397:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  13398:  context - if coursedoc, store the file in the course of the active role
                   13399:              of the current user; 
                   13400:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   13401:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 13402:  subdir - required - subdirectory to put the file in under ../userfiles/
                   13403:          if undefined, it will be placed in "unknown"
                   13404: 
                   13405:  (This routine calls clean_filename() to remove any dangerous
                   13406:  characters from the filename, and then calls finuserfileupload() to
                   13407:  complete the transaction)
                   13408: 
                   13409:  returns either the url of the uploaded file (/uploaded/....) if successful
                   13410:  and /adm/notfound.html if unsuccessful
                   13411: 
                   13412: =item *
                   13413: 
                   13414: clean_filename(): routine for cleaing a filename up for storage in
                   13415:                  userfile space, argument is:
                   13416: 
                   13417:  filename - proposed filename
                   13418: 
                   13419: returns: the new clean filename
                   13420: 
                   13421: =item *
                   13422: 
1.1090    raeburn  13423: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 13424: userspace, probably shouldn't be called directly
                   13425: 
                   13426:   docuname: username or courseid of destination for the file
                   13427:   docudom: domain of user/course of destination for the file
                   13428:   formname: same as for userfileupload()
1.1090    raeburn  13429:   fname: filename (including subdirectories) for the file
                   13430:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
                   13431:   allfiles: reference to hash used to store objects found by parser
                   13432:   codebase: reference to hash used for codebases of java objects found by parser
                   13433:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   13434:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   13435:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   13436:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   13437:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   13438:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  13439:   mimetype: reference to scalar to accommodate mime type determined
                   13440:             from File::MMagic if $parser = parse.
1.608     albertel 13441: 
                   13442:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  13443:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   13444:  was 'overwrite').
                   13445:  
1.608     albertel 13446: 
                   13447: =item *
                   13448: 
                   13449: renameuserfile(): renames an existing userfile to a new name
                   13450: 
                   13451:   Args:
                   13452:    docuname: username or courseid of destination for the file
                   13453:    docudom: domain of user/course of destination for the file
                   13454:    old: current file name (including any subdirs under userfiles)
                   13455:    new: desired file name (including any subdirs under userfiles)
                   13456: 
                   13457: =item *
                   13458: 
                   13459: mkdiruserfile(): creates a directory is a userfiles dir
                   13460: 
                   13461:   Args:
                   13462:    docuname: username or courseid of destination for the file
                   13463:    docudom: domain of user/course of destination for the file
                   13464:    dir: dir to create (including any subdirs under userfiles)
                   13465: 
                   13466: =item *
                   13467: 
                   13468: removeuserfile(): removes a file that exists in userfiles
                   13469: 
                   13470:   Args:
                   13471:    docuname: username or courseid of destination for the file
                   13472:    docudom: domain of user/course of destination for the file
                   13473:    fname: filname to delete (including any subdirs under userfiles)
                   13474: 
                   13475: =item *
                   13476: 
                   13477: removeuploadedurl(): convience function for removeuserfile()
                   13478: 
                   13479:   Args:
                   13480:    url:  a full /uploaded/... url to delete
                   13481: 
1.747     albertel 13482: =item * 
                   13483: 
                   13484: get_portfile_permissions():
                   13485:   Args:
                   13486:     domain: domain of user or course contain the portfolio files
                   13487:     user: name of user or num of course contain the portfolio files
                   13488:   Returns:
                   13489:     hashref of a dump of the proper file_permissions.db
                   13490:    
                   13491: 
                   13492: =item * 
                   13493: 
                   13494: get_access_controls():
                   13495: 
                   13496: Args:
                   13497:   current_permissions: the hash ref returned from get_portfile_permissions()
                   13498:   group: (optional) the group you want the files associated with
                   13499:   file: (optional) the file you want access info on
                   13500: 
                   13501: Returns:
1.749     raeburn  13502:     a hash (keys are file names) of hashes containing
                   13503:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   13504:         values are XML containing access control settings (see below) 
1.747     albertel 13505: 
                   13506: Internal notes:
                   13507: 
1.749     raeburn  13508:  access controls are stored in file_permissions.db as key=value pairs.
                   13509:     key -> path to file/file_name\0uniqueID:scope_end_start
                   13510:         where scope -> public,guest,course,group,domains or users.
                   13511:               end -> UNIX time for end of access (0 -> no end date)
                   13512:               start -> UNIX time for start of access
                   13513: 
                   13514:     value -> XML description of access control
                   13515:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   13516:             <start></start>
                   13517:             <end></end>
                   13518: 
                   13519:             <password></password>  for scope type = guest
                   13520: 
                   13521:             <domain></domain>     for scope type = course or group
                   13522:             <number></number>
                   13523:             <roles id="">
                   13524:              <role></role>
                   13525:              <access></access>
                   13526:              <section></section>
                   13527:              <group></group>
                   13528:             </roles>
                   13529: 
                   13530:             <dom></dom>         for scope type = domains
                   13531: 
                   13532:             <users>             for scope type = users
                   13533:              <user>
                   13534:               <uname></uname>
                   13535:               <udom></udom>
                   13536:              </user>
                   13537:             </users>
                   13538:            </scope> 
                   13539:               
                   13540:  Access data is also aggregated for each file in an additional key=value pair:
                   13541:  key -> path to file/file_name\0accesscontrol 
                   13542:  value -> reference to hash
                   13543:           hash contains key = value pairs
                   13544:           where key = uniqueID:scope_end_start
                   13545:                 value = UNIX time record was last updated
                   13546: 
                   13547:           Used to improve speed of look-ups of access controls for each file.  
                   13548:  
                   13549:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   13550: 
1.1198    raeburn  13551: =item *
                   13552: 
1.749     raeburn  13553: modify_access_controls():
                   13554: 
                   13555: Modifies access controls for a portfolio file
                   13556: Args
                   13557: 1. file name
                   13558: 2. reference to hash of required changes,
                   13559: 3. domain
                   13560: 4. username
                   13561:   where domain,username are the domain of the portfolio owner 
                   13562:   (either a user or a course) 
                   13563: 
                   13564: Returns:
                   13565: 1. result of additions or updates ('ok' or 'error', with error message). 
                   13566: 2. result of deletions ('ok' or 'error', with error message).
                   13567: 3. reference to hash of any new or updated access controls.
                   13568: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   13569:    key = integer (inbound ID)
1.1198    raeburn  13570:    value = uniqueID
                   13571: 
                   13572: =item *
                   13573: 
                   13574: get_timebased_id():
                   13575: 
                   13576: Attempts to get a unique timestamp-based suffix for use with items added to a 
                   13577: course via the Course Editor (e.g., folders, composite pages, 
                   13578: group bulletin boards).
                   13579: 
                   13580: Args: (first three required; six others optional)
                   13581: 
                   13582: 1. prefix (alphanumeric): of keys in hash, e.g., suppsequence, docspage,
                   13583:    docssequence, or name of group
                   13584: 
                   13585: 2. keyid (alphanumeric): name of temporary locking key in hash,
                   13586:    e.g., num, boardids
                   13587: 
                   13588: 3. namespace: name of gdbm file used to store suffixes already assigned;  
                   13589:    file will be named nohist_namespace.db
                   13590: 
                   13591: 4. cdom: domain of course; default is current course domain from %env
                   13592: 
                   13593: 5. cnum: course number; default is current course number from %env
                   13594: 
                   13595: 6. idtype: set to concat if an additional digit is to be appended to the 
                   13596:    unix timestamp to form the suffix, if the plain timestamp is already
                   13597:    in use.  Default is to not do this, but simply increment the unix 
                   13598:    timestamp by 1 until a unique key is obtained.
                   13599: 
                   13600: 7. who: holder of locking key; defaults to user:domain for user.
                   13601: 
                   13602: 8. locktries: number of attempts to obtain a lock (sleep of 1s before 
                   13603:    retrying); default is 3.
                   13604: 
                   13605: 9. maxtries: number of attempts to obtain a unique suffix; default is 20.  
                   13606: 
                   13607: Returns:
                   13608: 
                   13609: 1. suffix obtained (numeric)
                   13610: 
                   13611: 2. result of deleting locking key (ok if deleted, or lock never obtained)
                   13612: 
                   13613: 3. error: contains (localized) error message if an error occurred.
                   13614: 
1.747     albertel 13615: 
1.608     albertel 13616: =back
                   13617: 
1.243     albertel 13618: =head2 HTTP Helper Routines
                   13619: 
                   13620: =over 4
                   13621: 
1.191     harris41 13622: =item *
                   13623: 
                   13624: escape() : unpack non-word characters into CGI-compatible hex codes
                   13625: 
                   13626: =item *
                   13627: 
                   13628: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   13629: 
1.243     albertel 13630: =back
                   13631: 
                   13632: =head1 PRIVATE SUBROUTINES
                   13633: 
                   13634: =head2 Underlying communication routines (Shouldn't call)
                   13635: 
                   13636: =over 4
                   13637: 
                   13638: =item *
                   13639: 
                   13640: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   13641: 
                   13642: =item *
                   13643: 
                   13644: reply() : uses subreply to send a message to remote machine, logs all failures
                   13645: 
                   13646: =item *
                   13647: 
                   13648: critical() : passes a critical message to another server; if cannot
                   13649: get through then place message in connection buffer directory and
                   13650: returns con_delayed, if incapable of saving message, returns
                   13651: con_failed
                   13652: 
                   13653: =item *
                   13654: 
                   13655: reconlonc() : tries to reconnect lonc client processes.
                   13656: 
                   13657: =back
                   13658: 
                   13659: =head2 Resource Access Logging
                   13660: 
                   13661: =over 4
                   13662: 
                   13663: =item *
                   13664: 
                   13665: flushcourselogs() : flush (save) buffer logs and access logs
                   13666: 
                   13667: =item *
                   13668: 
                   13669: courselog($what) : save message for course in hash
                   13670: 
                   13671: =item *
                   13672: 
                   13673: courseacclog($what) : save message for course using &courselog().  Perform
                   13674: special processing for specific resource types (problems, exams, quizzes, etc).
                   13675: 
1.191     harris41 13676: =item *
                   13677: 
                   13678: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   13679: as a PerlChildExitHandler
1.243     albertel 13680: 
                   13681: =back
                   13682: 
                   13683: =head2 Other
                   13684: 
                   13685: =over 4
                   13686: 
                   13687: =item *
                   13688: 
                   13689: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 13690: 
                   13691: =back
                   13692: 
                   13693: =cut
1.877     foxr     13694: 

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