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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1153  ! www         4: # $Id: lonnet.pm,v 1.1152 2012/02/01 00:18:29 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.871     albertel   78: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
1.1138    raeburn    79:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease
                     80:             %managerstab);
1.871     albertel   81: 
                     82: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     83:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     84:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        85:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        86: 
1.1       albertel   87: use IO::Socket;
1.31      www        88: use GDBM_File;
1.208     albertel   89: use HTML::LCParser;
1.88      www        90: use Fcntl qw(:flock);
1.870     albertel   91: use Storable qw(thaw nfreeze);
1.539     albertel   92: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   93: use Cache::Memcached;
1.676     albertel   94: use Digest::MD5;
1.790     albertel   95: use Math::Random;
1.1024    raeburn    96: use File::MMagic;
1.807     albertel   97: use LONCAPA qw(:DEFAULT :match);
1.740     www        98: use LONCAPA::Configuration;
1.1117    foxr       99: 
1.1090    raeburn   100: use File::Copy;
1.676     albertel  101: 
1.195     www       102: my $readit;
1.550     foxr      103: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel  104: 
1.619     albertel  105: require Exporter;
                    106: 
                    107: our @ISA = qw (Exporter);
                    108: our @EXPORT = qw(%env);
                    109: 
1.449     matthew   110: 
1.1       albertel  111: # --------------------------------------------------------------------- Logging
1.729     www       112: {
                    113:     my $logid;
                    114:     sub instructor_log {
1.957     raeburn   115: 	my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
                    116:         if (($cnum eq '') || ($cdom eq '')) {
                    117:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    118:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    119:         }
1.729     www       120: 	$logid++;
1.957     raeburn   121:         my $now = time();
                    122: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.729     www       123: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www       124: 				    { $id => {
                    125: 					'exe_uname' => $env{'user.name'},
                    126: 					'exe_udom'  => $env{'user.domain'},
1.957     raeburn   127: 					'exe_time'  => $now,
1.730     www       128: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    129: 					'delflag'   => $delflag,
                    130: 					'logentry'  => $storehash,
                    131: 					'uname'     => $uname,
                    132: 					'udom'      => $udom,
                    133: 				    }
1.957     raeburn   134: 				  },$cdom,$cnum);
1.729     www       135:     }
                    136: }
1.1       albertel  137: 
1.163     harris41  138: sub logtouch {
                    139:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  140:     unless (-e "$execdir/logs/lonnet.log") {	
                    141: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  142: 	close $fh;
                    143:     }
                    144:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    145:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    146: }
                    147: 
1.1       albertel  148: sub logthis {
                    149:     my $message=shift;
                    150:     my $execdir=$perlvar{'lonDaemons'};
                    151:     my $now=time;
                    152:     my $local=localtime($now);
1.448     albertel  153:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986     foxr      154: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    155: 	print $fh $logstring;
1.448     albertel  156: 	close($fh);
                    157:     }
1.1       albertel  158:     return 1;
                    159: }
                    160: 
                    161: sub logperm {
                    162:     my $message=shift;
                    163:     my $execdir=$perlvar{'lonDaemons'};
                    164:     my $now=time;
                    165:     my $local=localtime($now);
1.448     albertel  166:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    167: 	print $fh "$now:$message:$local\n";
                    168: 	close($fh);
                    169:     }
1.1       albertel  170:     return 1;
                    171: }
                    172: 
1.850     albertel  173: sub create_connection {
1.853     albertel  174:     my ($hostname,$lonid) = @_;
1.851     albertel  175:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  176: 				     Type    => SOCK_STREAM,
                    177: 				     Timeout => 10);
                    178:     return 0 if (!$client);
1.890     albertel  179:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  180:     my $result = <$client>;
                    181:     chomp($result);
                    182:     return 1 if ($result eq 'done');
                    183:     return 0;
                    184: }
                    185: 
1.983     raeburn   186: sub get_server_timezone {
                    187:     my ($cnum,$cdom) = @_;
                    188:     my $home=&homeserver($cnum,$cdom);
                    189:     if ($home ne 'no_host') {
                    190:         my $cachetime = 24*3600;
                    191:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    192:         if (defined($cached)) {
                    193:             return $timezone;
                    194:         } else {
                    195:             my $timezone = &reply('servertimezone',$home);
                    196:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    197:         }
                    198:     }
                    199: }
1.850     albertel  200: 
1.1106    raeburn   201: sub get_server_distarch {
                    202:     my ($lonhost,$ignore_cache) = @_;
                    203:     if (defined($lonhost)) {
                    204:         if (!defined(&hostname($lonhost))) {
                    205:             return;
                    206:         }
                    207:         my $cachetime = 12*3600;
                    208:         if (!$ignore_cache) {
                    209:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    210:             if (defined($cached)) {
                    211:                 return $distarch;
                    212:             }
                    213:         }
                    214:         my $rep = &reply('serverdistarch',$lonhost);
                    215:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    216:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    217:                 $rep eq '') {
                    218:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    219:         }
                    220:     }
                    221:     return;
                    222: }
                    223: 
1.993     raeburn   224: sub get_server_loncaparev {
1.1073    raeburn   225:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   226:     if (defined($lonhost)) {
                    227:         if (!defined(&hostname($lonhost))) {
                    228:             undef($lonhost);
                    229:         }
                    230:     }
                    231:     if (!defined($lonhost)) {
                    232:         if (defined(&domain($dom,'primary'))) {
                    233:             $lonhost=&domain($dom,'primary');
                    234:             if ($lonhost eq 'no_host') {
                    235:                 undef($lonhost);
                    236:             }
                    237:         }
                    238:     }
                    239:     if (defined($lonhost)) {
1.1073    raeburn   240:         my $cachetime = 12*3600;
                    241:         if (!$ignore_cache) {
                    242:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    243:             if (defined($cached)) {
                    244:                 return $loncaparev;
                    245:             }
                    246:         }
                    247:         my ($answer,$loncaparev);
                    248:         my @ids=&current_machine_ids();
                    249:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    250:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   251:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   252:                 $loncaparev = $1;
                    253:             }
                    254:         } else {
                    255:             $answer = &reply('serverloncaparev',$lonhost);
                    256:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    257:                 if ($caller eq 'loncron') {
                    258:                     my $ua=new LWP::UserAgent;
1.1082    raeburn   259:                     $ua->timeout(4);
1.1073    raeburn   260:                     my $protocol = $protocol{$lonhost};
                    261:                     $protocol = 'http' if ($protocol ne 'https');
                    262:                     my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
                    263:                     my $request=new HTTP::Request('GET',$url);
                    264:                     my $response=$ua->request($request);
                    265:                     unless ($response->is_error()) {
                    266:                         my $content = $response->content;
1.1081    raeburn   267:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   268:                             $loncaparev = $1;
                    269:                         }
                    270:                     }
                    271:                 } else {
                    272:                     $loncaparev = $loncaparevs{$lonhost};
                    273:                 }
1.1081    raeburn   274:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   275:                 $loncaparev = $1;
                    276:             }
1.993     raeburn   277:         }
1.1073    raeburn   278:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   279:     }
                    280: }
                    281: 
1.1074    raeburn   282: sub get_server_homeID {
                    283:     my ($hostname,$ignore_cache,$caller) = @_;
                    284:     unless ($ignore_cache) {
                    285:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    286:         if (defined($cached)) {
                    287:             return $serverhomeID;
                    288:         }
                    289:     }
                    290:     my $cachetime = 12*3600;
                    291:     my $serverhomeID;
                    292:     if ($caller eq 'loncron') { 
                    293:         my @machine_ids = &machine_ids($hostname);
                    294:         foreach my $id (@machine_ids) {
                    295:             my $response = &reply('serverhomeID',$id);
                    296:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    297:                 $serverhomeID = $response;
                    298:                 last;
                    299:             }
                    300:         }
                    301:         if ($serverhomeID eq '') {
                    302:             $serverhomeID = $machine_ids[-1];
                    303:         }
                    304:     } else {
                    305:         $serverhomeID = $serverhomeIDs{$hostname};
                    306:     }
                    307:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    308: }
                    309: 
1.1121    raeburn   310: sub get_remote_globals {
                    311:     my ($lonhost,$whathash,$ignore_cache) = @_;
1.1125    raeburn   312:     my ($result,%returnhash,%whatneeded);
                    313:     if (ref($whathash) eq 'HASH') {
1.1121    raeburn   314:         foreach my $what (sort(keys(%{$whathash}))) {
                    315:             my $hashid = $lonhost.'-'.$what;
1.1125    raeburn   316:             my ($response,$cached);
1.1121    raeburn   317:             unless ($ignore_cache) {
1.1125    raeburn   318:                 ($response,$cached)=&is_cached_new('lonnetglobal',$hashid);
1.1121    raeburn   319:             }
                    320:             if (defined($cached)) {
1.1125    raeburn   321:                 $returnhash{$what} = $response;
1.1121    raeburn   322:             } else {
1.1125    raeburn   323:                 $whatneeded{$what} = 1;
1.1121    raeburn   324:             }
                    325:         }
1.1125    raeburn   326:         if (keys(%whatneeded) == 0) {
                    327:             $result = 'ok';
                    328:         } else {
1.1121    raeburn   329:             my $requested = &freeze_escape(\%whatneeded);
                    330:             my $rep=&reply('readlonnetglobal:'.$requested,$lonhost);
1.1125    raeburn   331:             if (($rep=~/^(refused|rejected|error)/) || ($rep eq 'con_lost') ||
                    332:                 ($rep eq 'unknown_cmd')) {
                    333:                 $result = $rep;
                    334:             } else {
                    335:                 $result = 'ok';
1.1121    raeburn   336:                 my @pairs=split(/\&/,$rep);
1.1125    raeburn   337:                 foreach my $item (@pairs) {
                    338:                     my ($key,$value)=split(/=/,$item,2);
                    339:                     my $what = &unescape($key);
                    340:                     my $hashid = $lonhost.'-'.$what;
                    341:                     $returnhash{$what}=&thaw_unescape($value);
                    342:                     &do_cache_new('lonnetglobal',$hashid,$returnhash{$what},600);
1.1121    raeburn   343:                 }
                    344:             }
                    345:         }
                    346:     }
1.1125    raeburn   347:     return ($result,\%returnhash);
1.1121    raeburn   348: }
                    349: 
1.1124    raeburn   350: sub remote_devalidate_cache {
                    351:     my ($lonhost,$name,$id) = @_;
1.1130    raeburn   352:     my $response = &reply('devalidatecache:'.&escape($name).':'.&escape($id),$lonhost);
1.1124    raeburn   353:     return $response;
                    354: }
                    355: 
1.1       albertel  356: # -------------------------------------------------- Non-critical communication
                    357: sub subreply {
                    358:     my ($cmd,$server)=@_;
1.838     albertel  359:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      360:     #
                    361:     #  With loncnew process trimming, there's a timing hole between lonc server
                    362:     #  process exit and the master server picking up the listen on the AF_UNIX
                    363:     #  socket.  In that time interval, a lock file will exist:
                    364: 
                    365:     my $lockfile=$peerfile.".lock";
                    366:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    367: 	sleep(1);
                    368:     }
                    369:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      370:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      371:     #
1.550     foxr      372:     #   We'll give the connection a few tries before abandoning it.  If
                    373:     #   connection is not possible, we'll con_lost back to the client.
                    374:     #   
                    375:     my $client;
                    376:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    377: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    378: 				      Type    => SOCK_STREAM,
                    379: 				      Timeout => 10);
1.869     albertel  380: 	if ($client) {
1.550     foxr      381: 	    last;		# Connected!
1.850     albertel  382: 	} else {
1.853     albertel  383: 	    &create_connection(&hostname($server),$server);
1.550     foxr      384: 	}
1.850     albertel  385:         sleep(1);		# Try again later if failed connection.
1.550     foxr      386:     }
                    387:     my $answer;
                    388:     if ($client) {
1.704     albertel  389: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      390: 	$answer=<$client>;
                    391: 	if (!$answer) { $answer="con_lost"; }
                    392: 	chomp($answer);
                    393:     } else {
                    394: 	$answer = 'con_lost';	# Failed connection.
                    395:     }
1.1       albertel  396:     return $answer;
                    397: }
                    398: 
                    399: sub reply {
                    400:     my ($cmd,$server)=@_;
1.838     albertel  401:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  402:     my $answer=subreply($cmd,$server);
1.65      www       403:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  404:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       405:                 " $cmd to $server returned $answer</font>");
                    406:     }
1.1       albertel  407:     return $answer;
                    408: }
                    409: 
                    410: # ----------------------------------------------------------- Send USR1 to lonc
                    411: 
                    412: sub reconlonc {
1.891     albertel  413:     my ($lonid) = @_;
                    414:     my $hostname = &hostname($lonid);
                    415:     if ($lonid) {
                    416: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    417: 	if ($hostname && -e $peerfile) {
                    418: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    419: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    420: 					     Type    => SOCK_STREAM,
                    421: 					     Timeout => 10);
                    422: 	    if ($client) {
                    423: 		print $client ("reset_retries\n");
                    424: 		my $answer=<$client>;
                    425: 		#reset just this one.
                    426: 	    }
                    427: 	}
                    428: 	return;
                    429:     }
                    430: 
1.836     www       431:     &logthis("Trying to reconnect lonc");
1.1       albertel  432:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  433:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  434: 	my $loncpid=<$fh>;
                    435:         chomp($loncpid);
                    436:         if (kill 0 => $loncpid) {
                    437: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    438:             kill USR1 => $loncpid;
                    439:             sleep 1;
1.836     www       440:          } else {
1.12      www       441: 	    &logthis(
1.672     albertel  442:                "<font color=\"blue\">WARNING:".
1.12      www       443:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  444:         }
                    445:     } else {
1.836     www       446: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  447:     }
                    448: }
                    449: 
                    450: # ------------------------------------------------------ Critical communication
1.12      www       451: 
1.1       albertel  452: sub critical {
                    453:     my ($cmd,$server)=@_;
1.838     albertel  454:     unless (&hostname($server)) {
1.672     albertel  455:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       456:                " Critical message to unknown server ($server)</font>");
                    457:         return 'no_such_host';
                    458:     }
1.1       albertel  459:     my $answer=reply($cmd,$server);
                    460:     if ($answer eq 'con_lost') {
                    461: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  462: 	my $answer=reply($cmd,$server);
1.1       albertel  463:         if ($answer eq 'con_lost') {
                    464:             my $now=time;
                    465:             my $middlename=$cmd;
1.5       www       466:             $middlename=substr($middlename,0,16);
1.1       albertel  467:             $middlename=~s/\W//g;
                    468:             my $dfilename=
1.305     www       469:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    470:             $dumpcount++;
1.1       albertel  471:             {
1.448     albertel  472: 		my $dfh;
                    473: 		if (open($dfh,">$dfilename")) {
                    474: 		    print $dfh "$cmd\n"; 
                    475: 		    close($dfh);
                    476: 		}
1.1       albertel  477:             }
                    478:             sleep 2;
                    479:             my $wcmd='';
                    480:             {
1.448     albertel  481: 		my $dfh;
                    482: 		if (open($dfh,"<$dfilename")) {
                    483: 		    $wcmd=<$dfh>; 
                    484: 		    close($dfh);
                    485: 		}
1.1       albertel  486:             }
                    487:             chomp($wcmd);
1.7       www       488:             if ($wcmd eq $cmd) {
1.672     albertel  489: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       490:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  491:                 &logperm("D:$server:$cmd");
                    492: 	        return 'con_delayed';
                    493:             } else {
1.672     albertel  494:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       495:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  496:                 &logperm("F:$server:$cmd");
                    497:                 return 'con_failed';
                    498:             }
                    499:         }
                    500:     }
                    501:     return $answer;
1.405     albertel  502: }
                    503: 
1.755     albertel  504: # ------------------------------------------- check if return value is an error
                    505: 
                    506: sub error {
                    507:     my ($result) = @_;
1.756     albertel  508:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  509: 	if ($2 == 2) { return undef; }
                    510: 	return $1;
                    511:     }
                    512:     return undef;
                    513: }
                    514: 
1.783     albertel  515: sub convert_and_load_session_env {
                    516:     my ($lonidsdir,$handle)=@_;
                    517:     my @profile;
                    518:     {
1.917     albertel  519: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    520: 	if (!$opened) {
1.915     albertel  521: 	    return 0;
                    522: 	}
1.783     albertel  523: 	flock($idf,LOCK_SH);
                    524: 	@profile=<$idf>;
                    525: 	close($idf);
                    526:     }
                    527:     my %temp_env;
                    528:     foreach my $line (@profile) {
1.786     albertel  529: 	if ($line !~ m/=/) {
                    530: 	    return 0;
                    531: 	}
1.783     albertel  532: 	chomp($line);
                    533: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    534: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    535:     }
                    536:     unlink("$lonidsdir/$handle.id");
                    537:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    538: 	    0640)) {
                    539: 	%disk_env = %temp_env;
                    540: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    541: 	untie(%disk_env);
                    542:     }
1.786     albertel  543:     return 1;
1.783     albertel  544: }
                    545: 
1.374     www       546: # ------------------------------------------- Transfer profile into environment
1.780     albertel  547: my $env_loaded;
                    548: sub transfer_profile_to_env {
1.788     albertel  549:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    550:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       551: 
1.720     albertel  552:     if (!defined($lonidsdir)) {
                    553: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    554:     }
                    555:     if (!defined($handle)) {
                    556:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    557:     }
                    558: 
1.786     albertel  559:     my $convert;
                    560:     {
1.917     albertel  561:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    562: 	if (!$opened) {
1.915     albertel  563: 	    return;
                    564: 	}
1.786     albertel  565: 	flock($idf,LOCK_SH);
                    566: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    567: 		&GDBM_READER(),0640)) {
                    568: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    569: 	    untie(%disk_env);
                    570: 	} else {
                    571: 	    $convert = 1;
                    572: 	}
                    573:     }
                    574:     if ($convert) {
                    575: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    576: 	    &logthis("Failed to load session, or convert session.");
                    577: 	}
1.374     www       578:     }
1.783     albertel  579: 
1.786     albertel  580:     my %remove;
1.783     albertel  581:     while ( my $envname = each(%env) ) {
1.433     matthew   582:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    583:             if ($time < time-300) {
1.783     albertel  584:                 $remove{$key}++;
1.433     matthew   585:             }
                    586:         }
                    587:     }
1.783     albertel  588: 
1.619     albertel  589:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  590:     $env_loaded=1;
1.783     albertel  591:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   592:         &delenv($expired_key);
1.374     www       593:     }
1.1       albertel  594: }
                    595: 
1.916     albertel  596: # ---------------------------------------------------- Check for valid session 
                    597: sub check_for_valid_session {
                    598:     my ($r) = @_;
                    599:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    600:     my $lonid=$cookies{'lonID'};
                    601:     return undef if (!$lonid);
                    602: 
                    603:     my $handle=&LONCAPA::clean_handle($lonid->value);
                    604:     my $lonidsdir=$r->dir_config('lonIDsDir');
                    605:     return undef if (!-e "$lonidsdir/$handle.id");
                    606: 
1.917     albertel  607:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    608:     return undef if (!$opened);
1.916     albertel  609: 
                    610:     flock($idf,LOCK_SH);
                    611:     my %disk_env;
                    612:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    613: 	    &GDBM_READER(),0640)) {
                    614: 	return undef;	
                    615:     }
                    616: 
                    617:     if (!defined($disk_env{'user.name'})
                    618: 	|| !defined($disk_env{'user.domain'})) {
                    619: 	return undef;
                    620:     }
                    621:     return $handle;
                    622: }
                    623: 
1.830     albertel  624: sub timed_flock {
                    625:     my ($file,$lock_type) = @_;
                    626:     my $failed=0;
                    627:     eval {
                    628: 	local $SIG{__DIE__}='DEFAULT';
                    629: 	local $SIG{ALRM}=sub {
                    630: 	    $failed=1;
                    631: 	    die("failed lock");
                    632: 	};
                    633: 	alarm(13);
                    634: 	flock($file,$lock_type);
                    635: 	alarm(0);
                    636:     };
                    637:     if ($failed) {
                    638: 	return undef;
                    639:     } else {
                    640: 	return 1;
                    641:     }
                    642: }
                    643: 
1.5       www       644: # ---------------------------------------------------------- Append Environment
                    645: 
                    646: sub appenv {
1.949     raeburn   647:     my ($newenv,$roles) = @_;
                    648:     if (ref($newenv) eq 'HASH') {
                    649:         foreach my $key (keys(%{$newenv})) {
                    650:             my $refused = 0;
                    651: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    652:                 $refused = 1;
                    653:                 if (ref($roles) eq 'ARRAY') {
                    654:                     my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
                    655:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    656:                         $refused = 0;
                    657:                     }
                    658:                 }
                    659:             }
                    660:             if ($refused) {
                    661:                 &logthis("<font color=\"blue\">WARNING: ".
                    662:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    663:                          .'</font>');
                    664: 	        delete($newenv->{$key});
                    665:             } else {
                    666:                 $env{$key}=$newenv->{$key};
                    667:             }
                    668:         }
                    669:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    670:         if ($opened
                    671: 	    && &timed_flock($env_file,LOCK_EX)
                    672: 	    &&
                    673: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    674: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    675: 	    while (my ($key,$value) = each(%{$newenv})) {
                    676: 	        $disk_env{$key} = $value;
                    677: 	    }
                    678: 	    untie(%disk_env);
1.35      www       679:         }
1.191     harris41  680:     }
1.56      www       681:     return 'ok';
                    682: }
                    683: # ----------------------------------------------------- Delete from Environment
                    684: 
                    685: sub delenv {
1.1104    raeburn   686:     my ($delthis,$regexp,$roles) = @_;
                    687:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    688:         my $refused = 1;
                    689:         if (ref($roles) eq 'ARRAY') {
                    690:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    691:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    692:                 $refused = 0;
                    693:             }
                    694:         }
                    695:         if ($refused) {
                    696:             &logthis("<font color=\"blue\">WARNING: ".
                    697:                      "Attempt to delete from environment ".$delthis);
                    698:             return 'error';
                    699:         }
1.56      www       700:     }
1.917     albertel  701:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    702:     if ($opened
1.915     albertel  703: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  704: 	&&
                    705: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    706: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  707: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   708: 	    if ($regexp) {
                    709:                 if ($key=~/^$delthis/) {
                    710:                     delete($env{$key});
                    711:                     delete($disk_env{$key});
                    712:                 } 
                    713:             } else {
                    714:                 if ($key=~/^\Q$delthis\E/) {
                    715: 		    delete($env{$key});
                    716: 		    delete($disk_env{$key});
                    717: 	        }
                    718:             }
1.448     albertel  719: 	}
1.783     albertel  720: 	untie(%disk_env);
1.5       www       721:     }
                    722:     return 'ok';
1.369     albertel  723: }
                    724: 
1.790     albertel  725: sub get_env_multiple {
                    726:     my ($name) = @_;
                    727:     my @values;
                    728:     if (defined($env{$name})) {
                    729:         # exists is it an array
                    730:         if (ref($env{$name})) {
                    731:             @values=@{ $env{$name} };
                    732:         } else {
                    733:             $values[0]=$env{$name};
                    734:         }
                    735:     }
                    736:     return(@values);
                    737: }
                    738: 
1.958     www       739: # ------------------------------------------------------------------- Locking
                    740: 
                    741: sub set_lock {
                    742:     my ($text)=@_;
                    743:     $locknum++;
                    744:     my $id=$$.'-'.$locknum;
                    745:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    746:              'session.lock.'.$id => $text});
                    747:     return $id;
                    748: }
                    749: 
                    750: sub get_locks {
                    751:     my $num=0;
                    752:     my %texts=();
                    753:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    754:        if ($lock=~/\w/) {
                    755:           $num++;
                    756:           $texts{$lock}=$env{'session.lock.'.$lock};
                    757:        }
                    758:    }
                    759:    return ($num,%texts);
                    760: }
                    761: 
                    762: sub remove_lock {
                    763:     my ($id)=@_;
                    764:     my $newlocks='';
                    765:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    766:        if (($lock=~/\w/) && ($lock ne $id)) {
                    767:           $newlocks.=','.$lock;
                    768:        }
                    769:     }
                    770:     &appenv({'session.locks' => $newlocks});
                    771:     &delenv('session.lock.'.$id);
                    772: }
                    773: 
                    774: sub remove_all_locks {
                    775:     my $activelocks=$env{'session.locks'};
                    776:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    777:        if ($lock=~/\w/) {
                    778:           &remove_lock($lock);
                    779:        }
                    780:     }
                    781: }
                    782: 
                    783: 
1.369     albertel  784: # ------------------------------------------ Find out current server userload
                    785: sub userload {
                    786:     my $numusers=0;
                    787:     {
                    788: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    789: 	my $filename;
                    790: 	my $curtime=time;
                    791: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  792: 	    next if ($filename eq '.' || $filename eq '..');
                    793: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.404     albertel  794: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  795: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  796: 	}
                    797: 	closedir(LONIDS);
                    798:     }
                    799:     my $userloadpercent=0;
                    800:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    801:     if ($maxuserload) {
1.371     albertel  802: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  803:     }
1.372     albertel  804:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  805:     return $userloadpercent;
1.283     www       806: }
                    807: 
1.1       albertel  808: # ------------------------------ Find server with least workload from spare.tab
1.11      www       809: 
1.1       albertel  810: sub spareserver {
1.1083    raeburn   811:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  812:     my $spare_server;
1.370     albertel  813:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  814:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    815:                                                      :  $userloadpercent;
1.1083    raeburn   816:     my ($uint_dom,$remotesessions);
                    817:     if (($udom ne '') && (&domain($udom) ne '')) {
                    818:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    819:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    820:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    821:         $remotesessions = $udomdefaults{'remotesessions'};
                    822:     }
1.1123    raeburn   823:     my $spareshash = &this_host_spares($udom);
                    824:     if (ref($spareshash) eq 'HASH') {
                    825:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    826:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                    827:                 if ($uint_dom) {
                    828:                     next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    829:                                                  $try_server));
                    830:                 }
                    831: 	        ($spare_server, $lowest_load) =
                    832: 	            &compare_server_load($try_server, $spare_server, $lowest_load);
                    833:             }
1.1083    raeburn   834:         }
1.784     albertel  835: 
1.1123    raeburn   836:         my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    837: 
                    838:         if (!$found_server) {
                    839:             if (ref($spareshash->{'default'}) eq 'ARRAY') { 
                    840: 	        foreach my $try_server (@{ $spareshash->{'default'} }) {
                    841:                     if ($uint_dom) {
                    842:                         next unless (&spare_can_host($udom,$uint_dom,
                    843:                                                      $remotesessions,$try_server));
                    844:                     }
                    845: 	            ($spare_server, $lowest_load) =
                    846: 		        &compare_server_load($try_server, $spare_server, $lowest_load);
                    847:                 }
                    848: 	    }
                    849:         }
1.784     albertel  850:     }
                    851: 
                    852:     if (!$want_server_name) {
1.968     raeburn   853:         my $protocol = 'http';
                    854:         if ($protocol{$spare_server} eq 'https') {
                    855:             $protocol = $protocol{$spare_server};
                    856:         }
1.1001    raeburn   857:         if (defined($spare_server)) {
                    858:             my $hostname = &hostname($spare_server);
1.1083    raeburn   859:             if (defined($hostname)) {
1.1001    raeburn   860: 	        $spare_server = $protocol.'://'.$hostname;
                    861:             }
                    862:         }
1.784     albertel  863:     }
                    864:     return $spare_server;
                    865: }
                    866: 
                    867: sub compare_server_load {
                    868:     my ($try_server, $spare_server, $lowest_load) = @_;
                    869: 
                    870:     my $loadans     = &reply('load',    $try_server);
                    871:     my $userloadans = &reply('userload',$try_server);
                    872: 
                    873:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1114    raeburn   874: 	return ($spare_server, $lowest_load); #didn't get a number from the server
1.784     albertel  875:     }
                    876: 
                    877:     my $load;
                    878:     if ($loadans =~ /\d/) {
                    879: 	if ($userloadans =~ /\d/) {
                    880: 	    #both are numbers, pick the bigger one
                    881: 	    $load = ($loadans > $userloadans) ? $loadans 
                    882: 		                              : $userloadans;
1.411     albertel  883: 	} else {
1.784     albertel  884: 	    $load = $loadans;
1.411     albertel  885: 	}
1.784     albertel  886:     } else {
                    887: 	$load = $userloadans;
                    888:     }
                    889: 
                    890:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    891: 	$spare_server = $try_server;
                    892: 	$lowest_load  = $load;
1.370     albertel  893:     }
1.784     albertel  894:     return ($spare_server,$lowest_load);
1.202     matthew   895: }
1.914     albertel  896: 
                    897: # --------------------------- ask offload servers if user already has a session
                    898: sub find_existing_session {
                    899:     my ($udom,$uname) = @_;
1.1123    raeburn   900:     my $spareshash = &this_host_spares($udom);
                    901:     if (ref($spareshash) eq 'HASH') {
                    902:         if (ref($spareshash->{'primary'}) eq 'ARRAY') {
                    903:             foreach my $try_server (@{ $spareshash->{'primary'} }) {
                    904:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    905:             }
                    906:         }
                    907:         if (ref($spareshash->{'default'}) eq 'ARRAY') {
                    908:             foreach my $try_server (@{ $spareshash->{'default'} }) {
                    909:                 return $try_server if (&has_user_session($try_server, $udom, $uname));
                    910:             }
                    911:         }
1.914     albertel  912:     }
                    913:     return;
                    914: }
                    915: 
                    916: # -------------------------------- ask if server already has a session for user
                    917: sub has_user_session {
                    918:     my ($lonid,$udom,$uname) = @_;
                    919:     my $result = &reply(join(':','userhassession',
                    920: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                    921:     return 1 if ($result eq 'ok');
                    922: 
                    923:     return 0;
                    924: }
                    925: 
1.1076    raeburn   926: # --------- determine least loaded server in a user's domain which allows login
                    927: 
                    928: sub choose_server {
1.1115    raeburn   929:     my ($udom,$checkloginvia) = @_;
1.1076    raeburn   930:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn   931:     my %servers = &get_servers($udom);
1.1076    raeburn   932:     my $lowest_load = 30000;
1.1151    raeburn   933:     my ($login_host,$hostname,$portal_path,$isredirect);
1.1076    raeburn   934:     foreach my $lonhost (keys(%servers)) {
1.1115    raeburn   935:         my $loginvia;
                    936:         if ($checkloginvia) {
                    937:             $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
1.1116    raeburn   938:             if ($loginvia) {
                    939:                 my ($server,$path) = split(/:/,$loginvia);
                    940:                 ($login_host, $lowest_load) =
                    941:                     &compare_server_load($server, $login_host, $lowest_load);
                    942:                 if ($login_host eq $server) {
                    943:                     $portal_path = $path;
1.1151    raeburn   944:                     $isredirect = 1;
1.1116    raeburn   945:                 }
                    946:             } else {
                    947:                 ($login_host, $lowest_load) =
                    948:                     &compare_server_load($lonhost, $login_host, $lowest_load);
                    949:                 if ($login_host eq $lonhost) {
                    950:                     $portal_path = '';
1.1151    raeburn   951:                     $isredirect = ''; 
1.1116    raeburn   952:                 }
                    953:             }
                    954:         } else {
1.1076    raeburn   955:             ($login_host, $lowest_load) =
1.1116    raeburn   956:                 &compare_server_load($lonhost, $login_host, $lowest_load);
1.1076    raeburn   957:         }
                    958:     }
                    959:     if ($login_host ne '') {
1.1116    raeburn   960:         $hostname = &hostname($login_host);
1.1076    raeburn   961:     }
1.1151    raeburn   962:     return ($login_host,$hostname,$portal_path,$isredirect);
1.1076    raeburn   963: }
                    964: 
1.202     matthew   965: # --------------------------------------------- Try to change a user's password
                    966: 
                    967: sub changepass {
1.799     raeburn   968:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   969:     $currentpass = &escape($currentpass);
                    970:     $newpass     = &escape($newpass);
1.1030    raeburn   971:     my $lonhost = $perlvar{'lonHostID'};
                    972:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew   973: 		       $server);
                    974:     if (! $answer) {
                    975: 	&logthis("No reply on password change request to $server ".
                    976: 		 "by $uname in domain $udom.");
                    977:     } elsif ($answer =~ "^ok") {
                    978:         &logthis("$uname in $udom successfully changed their password ".
                    979: 		 "on $server.");
                    980:     } elsif ($answer =~ "^pwchange_failure") {
                    981: 	&logthis("$uname in $udom was unable to change their password ".
                    982: 		 "on $server.  The action was blocked by either lcpasswd ".
                    983: 		 "or pwchange");
                    984:     } elsif ($answer =~ "^non_authorized") {
                    985:         &logthis("$uname in $udom did not get their password correct when ".
                    986: 		 "attempting to change it on $server.");
                    987:     } elsif ($answer =~ "^auth_mode_error") {
                    988:         &logthis("$uname in $udom attempted to change their password despite ".
                    989: 		 "not being locally or internally authenticated on $server.");
                    990:     } elsif ($answer =~ "^unknown_user") {
                    991:         &logthis("$uname in $udom attempted to change their password ".
                    992: 		 "on $server but were unable to because $server is not ".
                    993: 		 "their home server.");
                    994:     } elsif ($answer =~ "^refused") {
                    995: 	&logthis("$server refused to change $uname in $udom password because ".
                    996: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn   997:     } elsif ($answer =~ "invalid_client") {
                    998:         &logthis("$server refused to change $uname in $udom password because ".
                    999:                  "it was a reset by e-mail originating from an invalid server.");
1.202     matthew  1000:     }
                   1001:     return $answer;
1.1       albertel 1002: }
                   1003: 
1.169     harris41 1004: # ----------------------- Try to determine user's current authentication scheme
                   1005: 
                   1006: sub queryauthenticate {
                   1007:     my ($uname,$udom)=@_;
1.456     albertel 1008:     my $uhome=&homeserver($uname,$udom);
                   1009:     if (!$uhome) {
                   1010: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                   1011: 	return 'no_host';
                   1012:     }
                   1013:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                   1014:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                   1015: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41 1016:     }
1.456     albertel 1017:     return $answer;
1.169     harris41 1018: }
                   1019: 
1.1       albertel 1020: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www      1021: 
1.1       albertel 1022: sub authenticate {
1.1073    raeburn  1023:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel 1024:     $upass=&escape($upass);
                   1025:     $uname= &LONCAPA::clean_username($uname);
1.836     www      1026:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn  1027:     my $newhome;
1.836     www      1028:     if ((!$uhome) || ($uhome eq 'no_host')) {
                   1029: # Maybe the machine was offline and only re-appeared again recently?
                   1030:         &reconlonc();
                   1031: # One more
1.952     raeburn  1032: 	$uhome=&homeserver($uname,$udom,1);
                   1033:         if (($uhome eq 'no_host') && $checkdefauth) {
                   1034:             if (defined(&domain($udom,'primary'))) {
                   1035:                 $newhome=&domain($udom,'primary');
                   1036:             }
                   1037:             if ($newhome ne '') {
                   1038:                 $uhome = $newhome;
                   1039:             }
                   1040:         }
1.836     www      1041: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                   1042: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn  1043: 	    return 'no_host';
                   1044:         }
1.1       albertel 1045:     }
1.1073    raeburn  1046:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel 1047:     if ($answer eq 'authorized') {
1.952     raeburn  1048:         if ($newhome) {
                   1049:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                   1050:             return 'no_account_on_host'; 
                   1051:         } else {
                   1052:             &logthis("User $uname at $udom authorized by $uhome");
                   1053:             return $uhome;
                   1054:         }
1.471     albertel 1055:     }
                   1056:     if ($answer eq 'non_authorized') {
                   1057: 	&logthis("User $uname at $udom rejected by $uhome");
                   1058: 	return 'no_host'; 
1.9       www      1059:     }
1.471     albertel 1060:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel 1061:     return 'no_host';
                   1062: }
                   1063: 
1.1073    raeburn  1064: sub can_host_session {
1.1074    raeburn  1065:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn  1066:     my $canhost = 1;
1.1074    raeburn  1067:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn  1068:     if (ref($remotesessions) eq 'HASH') {
                   1069:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn  1070:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn  1071:                 $canhost = 0;
                   1072:             } else {
                   1073:                 $canhost = 1;
                   1074:             }
                   1075:         }
                   1076:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn  1077:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn  1078:                 $canhost = 1;
                   1079:             } else {
                   1080:                 $canhost = 0;
                   1081:             }
                   1082:         }
                   1083:         if ($canhost) {
                   1084:             if ($remotesessions->{'version'} ne '') {
                   1085:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1086:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1087:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1088:                         my $major = $1;
                   1089:                         my $minor = $2;
                   1090:                         if (($major < $reqmajor ) ||
                   1091:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1092:                             $canhost = 0;
                   1093:                         }
                   1094:                     } else {
                   1095:                         $canhost = 0;
                   1096:                     }
                   1097:                 }
                   1098:             }
                   1099:         }
                   1100:     }
                   1101:     if ($canhost) {
                   1102:         if (ref($hostedsessions) eq 'HASH') {
1.1120    raeburn  1103:             my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1104:             my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.1073    raeburn  1105:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
1.1120    raeburn  1106:                 if (($uint_dom ne '') && 
                   1107:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'excludedomain'}}))) {
1.1073    raeburn  1108:                     $canhost = 0;
                   1109:                 } else {
                   1110:                     $canhost = 1;
                   1111:                 }
                   1112:             }
                   1113:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
1.1120    raeburn  1114:                 if (($uint_dom ne '') && 
                   1115:                     (grep(/^\Q$uint_dom\E$/,@{$hostedsessions->{'includedomain'}}))) {
1.1073    raeburn  1116:                     $canhost = 1;
                   1117:                 } else {
                   1118:                     $canhost = 0;
                   1119:                 }
                   1120:             }
                   1121:         }
                   1122:     }
                   1123:     return $canhost;
                   1124: }
                   1125: 
1.1083    raeburn  1126: sub spare_can_host {
                   1127:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1128:     my $canhost=1;
                   1129:     my @intdoms;
                   1130:     my $internet_names = &Apache::lonnet::get_internet_names($try_server);
                   1131:     if (ref($internet_names) eq 'ARRAY') {
                   1132:         @intdoms = @{$internet_names};
                   1133:     }
                   1134:     unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1135:         my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
                   1136:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   1137:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                   1138:         my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
                   1139:         $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1140:                                      $remotesessions,
                   1141:                                      $defdomdefaults{'hostedsessions'});
                   1142:     }
                   1143:     return $canhost;
                   1144: }
                   1145: 
1.1123    raeburn  1146: sub this_host_spares {
                   1147:     my ($dom) = @_;
1.1126    raeburn  1148:     my ($dom_in_use,$lonhost_in_use,$result);
1.1123    raeburn  1149:     my @hosts = &current_machine_ids();
                   1150:     foreach my $lonhost (@hosts) {
                   1151:         if (&host_domain($lonhost) eq $dom) {
1.1126    raeburn  1152:             $dom_in_use = $dom;
                   1153:             $lonhost_in_use = $lonhost;
1.1123    raeburn  1154:             last;
                   1155:         }
                   1156:     }
1.1126    raeburn  1157:     if ($dom_in_use ne '') {
                   1158:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1159:     }
                   1160:     if (ref($result) ne 'HASH') {
                   1161:         $lonhost_in_use = $perlvar{'lonHostID'};
                   1162:         $dom_in_use = &host_domain($lonhost_in_use);
                   1163:         $result = &spares_for_offload($dom_in_use,$lonhost_in_use);
                   1164:         if (ref($result) ne 'HASH') {
                   1165:             $result = \%spareid;
                   1166:         }
                   1167:     }
                   1168:     return $result;
                   1169: }
                   1170: 
                   1171: sub spares_for_offload  {
                   1172:     my ($dom_in_use,$lonhost_in_use) = @_;
                   1173:     my ($result,$cached)=&is_cached_new('spares',$dom_in_use);
1.1123    raeburn  1174:     if (defined($cached)) {
                   1175:         return $result;
                   1176:     } else {
1.1126    raeburn  1177:         my $cachetime = 60*60*24;
                   1178:         my %domconfig =
                   1179:             &Apache::lonnet::get_dom('configuration',['usersessions'],$dom_in_use);
                   1180:         if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1181:             if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   1182:                 if (ref($domconfig{'usersessions'}{'spares'}{$lonhost_in_use}) eq 'HASH') {
                   1183:                     return &do_cache_new('spares',$dom_in_use,$domconfig{'usersessions'}{'spares'}{$lonhost_in_use},$cachetime);
1.1123    raeburn  1184:                 }
                   1185:             }
                   1186:         }
                   1187:     }
1.1126    raeburn  1188:     return;
1.1123    raeburn  1189: }
                   1190: 
1.1129    raeburn  1191: sub get_lonbalancer_config {
                   1192:     my ($servers) = @_;
                   1193:     my ($currbalancer,$currtargets);
                   1194:     if (ref($servers) eq 'HASH') {
                   1195:         foreach my $server (keys(%{$servers})) {
                   1196:             my %what = (
                   1197:                          spareid => 1,
                   1198:                          perlvar => 1,
                   1199:                        );
                   1200:             my ($result,$returnhash) = &get_remote_globals($server,\%what);
                   1201:             if ($result eq 'ok') {
                   1202:                 if (ref($returnhash) eq 'HASH') {
                   1203:                     if (ref($returnhash->{'perlvar'}) eq 'HASH') {
                   1204:                         if ($returnhash->{'perlvar'}->{'lonBalancer'} eq 'yes') {
                   1205:                             $currbalancer = $server;
                   1206:                             $currtargets = {};
                   1207:                             if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   1208:                                 if (ref($returnhash->{'spareid'}->{'primary'}) eq 'ARRAY') {
                   1209:                                     $currtargets->{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   1210:                                 }
                   1211:                                 if (ref($returnhash->{'spareid'}->{'default'}) eq 'ARRAY') {
                   1212:                                     $currtargets->{'default'} = $returnhash->{'spareid'}->{'default'};
                   1213:                                 }
                   1214:                             }
                   1215:                             last;
                   1216:                         }
                   1217:                     }
                   1218:                 }
                   1219:             }
                   1220:         }
                   1221:     }
                   1222:     return ($currbalancer,$currtargets);
                   1223: }
                   1224: 
                   1225: sub check_loadbalancing {
                   1226:     my ($uname,$udom) = @_;
                   1227:     my ($is_balancer,$dom_in_use,$homeintdom,$rule_in_effect,
                   1228:         $offloadto,$otherserver);
                   1229:     my $lonhost = $perlvar{'lonHostID'};
                   1230:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                   1231:     my $uintdom = &Apache::lonnet::internet_dom($uprimary_id);
                   1232:     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                   1233:     my $serverhomedom = &host_domain($lonhost);
                   1234: 
                   1235:     my $cachetime = 60*60*24;
                   1236: 
                   1237:     if (($uintdom ne '') && ($uintdom eq $intdom)) {
                   1238:         $dom_in_use = $udom;
                   1239:         $homeintdom = 1;
                   1240:     } else {
                   1241:         $dom_in_use = $serverhomedom;
                   1242:     }
                   1243:     my ($result,$cached)=&is_cached_new('loadbalancing',$dom_in_use);
                   1244:     unless (defined($cached)) {
                   1245:         my %domconfig =
                   1246:             &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
                   1247:         if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1248:             $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1129    raeburn  1249:         }
                   1250:     }
                   1251:     if (ref($result) eq 'HASH') {
                   1252:         my $currbalancer = $result->{'lonhost'};
                   1253:         my $currtargets = $result->{'targets'};
                   1254:         my $currrules = $result->{'rules'};
                   1255:         if ($currbalancer ne '') {
                   1256:             my @hosts = &current_machine_ids();
                   1257:             if (grep(/^\Q$currbalancer\E$/,@hosts)) {
                   1258:                 $is_balancer = 1;
                   1259:             }
                   1260:         }
                   1261:         if ($is_balancer) {
                   1262:             if (ref($currrules) eq 'HASH') {
                   1263:                 if ($homeintdom) {
                   1264:                     if ($uname ne '') {
                   1265:                         if (($currrules->{'_LC_adv'} ne '') || ($currrules->{'_LC_author'} ne '')) {
                   1266:                             my ($is_adv,$is_author) = &is_advanced_user($udom,$uname);
                   1267:                             if (($currrules->{'_LC_author'} ne '') && ($is_author)) {
                   1268:                                 $rule_in_effect = $currrules->{'_LC_author'};
                   1269:                             } elsif (($currrules->{'_LC_adv'} ne '') && ($is_adv)) {
                   1270:                                 $rule_in_effect = $currrules->{'_LC_adv'}
                   1271:                             }
                   1272:                         }
                   1273:                         if ($rule_in_effect eq '') {
                   1274:                             my %userenv = &userenvironment($udom,$uname,'inststatus');
                   1275:                             if ($userenv{'inststatus'} ne '') {
                   1276:                                 my @statuses = map { &unescape($_); } split(/:/,$userenv{'inststatus'});
                   1277:                                 my ($othertitle,$usertypes,$types) =
                   1278:                                     &Apache::loncommon::sorted_inst_types($udom);
                   1279:                                 if (ref($types) eq 'ARRAY') {
                   1280:                                     foreach my $type (@{$types}) {
                   1281:                                         if (grep(/^\Q$type\E$/,@statuses)) {
                   1282:                                             if (exists($currrules->{$type})) {
                   1283:                                                 $rule_in_effect = $currrules->{$type};
                   1284:                                             }
                   1285:                                         }
                   1286:                                     }
                   1287:                                 }
                   1288:                             } else {
                   1289:                                 if (exists($currrules->{'default'})) {
                   1290:                                     $rule_in_effect = $currrules->{'default'};
                   1291:                                 }
                   1292:                             }
                   1293:                         }
                   1294:                     } else {
                   1295:                         if (exists($currrules->{'default'})) {
                   1296:                             $rule_in_effect = $currrules->{'default'};
                   1297:                         }
                   1298:                     }
                   1299:                 } else {
                   1300:                     if ($currrules->{'_LC_external'} ne '') {
                   1301:                         $rule_in_effect = $currrules->{'_LC_external'};
                   1302:                     }
                   1303:                 }
                   1304:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1305:                                                        $uname,$udom);
                   1306:             }
                   1307:         }
                   1308:     } elsif (($homeintdom) && ($udom ne $serverhomedom)) {
                   1309:         my ($result,$cached)=&is_cached_new('loadbalancing',$serverhomedom);
                   1310:         unless (defined($cached)) {
                   1311:             my %domconfig =
                   1312:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$serverhomedom);
                   1313:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.1130    raeburn  1314:                 $result = &do_cache_new('loadbalancing',$dom_in_use,$domconfig{'loadbalancing'},$cachetime);
1.1129    raeburn  1315:             }
                   1316:         }
                   1317:         if (ref($result) eq 'HASH') {
                   1318:             my $currbalancer = $result->{'lonhost'};
                   1319:             my $currtargets = $result->{'targets'};
                   1320:             my $currrules = $result->{'rules'};
                   1321: 
                   1322:             if ($currbalancer eq $lonhost) {
                   1323:                 $is_balancer = 1;
                   1324:                 if (ref($currrules) eq 'HASH') {
                   1325:                     if ($currrules->{'_LC_internetdom'} ne '') {
                   1326:                         $rule_in_effect = $currrules->{'_LC_internetdom'};
                   1327:                     }
                   1328:                 }
                   1329:                 $offloadto = &get_loadbalancer_targets($rule_in_effect,$currtargets,
                   1330:                                                        $uname,$udom);
                   1331:             }
                   1332:         } else {
                   1333:             if ($perlvar{'lonBalancer'} eq 'yes') {
                   1334:                 $is_balancer = 1;
                   1335:                 $offloadto = &this_host_spares($dom_in_use);
                   1336:             }
                   1337:         }
                   1338:     } else {
                   1339:         if ($perlvar{'lonBalancer'} eq 'yes') {
                   1340:             $is_balancer = 1;
                   1341:             $offloadto = &this_host_spares($dom_in_use);
                   1342:         }
                   1343:     }
                   1344:     my $lowest_load = 30000;
                   1345:     if (ref($offloadto) eq 'HASH') {
                   1346:         if (ref($offloadto->{'primary'}) eq 'ARRAY') {
                   1347:             foreach my $try_server (@{$offloadto->{'primary'}}) {
                   1348:                 ($otherserver,$lowest_load) =
                   1349:                     &compare_server_load($try_server,$otherserver,$lowest_load);
                   1350:             }
                   1351:         }
                   1352:         my $found_server = ($otherserver ne '' && $lowest_load < 100);
                   1353: 
                   1354:         if (!$found_server) {
                   1355:             if (ref($offloadto->{'default'}) eq 'ARRAY') {
                   1356:                 foreach my $try_server (@{$offloadto->{'default'}}) {
                   1357:                     ($otherserver,$lowest_load) =
                   1358:                         &compare_server_load($try_server,$otherserver,$lowest_load);
                   1359:                 }
                   1360:             }
                   1361:         }
                   1362:     } elsif (ref($offloadto) eq 'ARRAY') {
                   1363:         if (@{$offloadto} == 1) {
                   1364:             $otherserver = $offloadto->[0];
                   1365:         } elsif (@{$offloadto} > 1) {
                   1366:             foreach my $try_server (@{$offloadto}) {
                   1367:                 ($otherserver,$lowest_load) =
                   1368:                     &compare_server_load($try_server,$otherserver,$lowest_load);
                   1369:             }
                   1370:         }
                   1371:     }
                   1372:     return ($is_balancer,$otherserver);
                   1373: }
                   1374: 
                   1375: sub get_loadbalancer_targets {
                   1376:     my ($rule_in_effect,$currtargets,$uname,$udom) = @_;
                   1377:     my $offloadto;
                   1378:     if ($rule_in_effect eq '') {
                   1379:         $offloadto = $currtargets;
                   1380:     } else {
                   1381:         if ($rule_in_effect eq 'homeserver') {
                   1382:             my $homeserver = &homeserver($uname,$udom);
                   1383:             if ($homeserver ne 'no_host') {
                   1384:                 $offloadto = [$homeserver];
                   1385:             }
                   1386:         } elsif ($rule_in_effect eq 'externalbalancer') {
                   1387:             my %domconfig =
                   1388:                 &Apache::lonnet::get_dom('configuration',['loadbalancing'],$udom);
                   1389:             if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   1390:                 if ($domconfig{'loadbalancing'}{'lonhost'} ne '') {
                   1391:                     if (&hostname($domconfig{'loadbalancing'}{'lonhost'}) ne '') {
                   1392:                         $offloadto = [$domconfig{'loadbalancing'}{'lonhost'}];
                   1393:                     }
                   1394:                 }
                   1395:             } else {
                   1396:                 my %servers = &dom_servers($udom);
                   1397:                 my ($remotebalancer,$remotetargets) = &get_lonbalancer_config(\%servers);
                   1398:                 if (&hostname($remotebalancer) ne '') {
                   1399:                     $offloadto = [$remotebalancer];
                   1400:                 }
                   1401:             }
                   1402:         } elsif (&hostname($rule_in_effect) ne '') {
                   1403:             $offloadto = [$rule_in_effect];
                   1404:         }
                   1405:     }
                   1406:     return $offloadto;
                   1407: }
                   1408: 
1.1127    raeburn  1409: sub internet_dom_servers {
                   1410:     my ($dom) = @_;
                   1411:     my (%uniqservers,%servers);
                   1412:     my $primaryserver = &hostname(&domain($dom,'primary'));
                   1413:     my @machinedoms = &machine_domains($primaryserver);
                   1414:     foreach my $mdom (@machinedoms) {
                   1415:         my %currservers = %servers;
                   1416:         my %server = &get_servers($mdom);
                   1417:         %servers = (%currservers,%server);
                   1418:     }
                   1419:     my %by_hostname;
                   1420:     foreach my $id (keys(%servers)) {
                   1421:         push(@{$by_hostname{$servers{$id}}},$id);
                   1422:     }
                   1423:     foreach my $hostname (sort(keys(%by_hostname))) {
                   1424:         if (@{$by_hostname{$hostname}} > 1) {
                   1425:             my $match = 0;
                   1426:             foreach my $id (@{$by_hostname{$hostname}}) {
                   1427:                 if (&host_domain($id) eq $dom) {
                   1428:                     $uniqservers{$id} = $hostname;
                   1429:                     $match = 1;
                   1430:                 }
                   1431:             }
                   1432:             unless ($match) {
                   1433:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1434:             }
                   1435:         } else {
                   1436:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
                   1437:         }
                   1438:     }
                   1439:     return %uniqservers;
                   1440: }
                   1441: 
1.1       albertel 1442: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1443: 
1.599     albertel 1444: my %homecache;
1.1       albertel 1445: sub homeserver {
1.230     stredwic 1446:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1447:     my $index="$uname:$udom";
1.426     albertel 1448: 
1.599     albertel 1449:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1450: 
                   1451:     my %servers = &get_servers($udom,'library');
                   1452:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1453:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1454: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1455: 
                   1456: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1457: 	if ($answer eq 'found') {
                   1458: 	    delete($badServerCache{$tryserver}); 
                   1459: 	    return $homecache{$index}=$tryserver;
                   1460: 	} elsif ($answer eq 'no_host') {
                   1461: 	    $badServerCache{$tryserver}=1;
                   1462: 	}
1.1       albertel 1463:     }    
                   1464:     return 'no_host';
1.70      www      1465: }
                   1466: 
                   1467: # ------------------------------------- Find the usernames behind a list of IDs
                   1468: 
                   1469: sub idget {
                   1470:     my ($udom,@ids)=@_;
                   1471:     my %returnhash=();
                   1472:     
1.841     albertel 1473:     my %servers = &get_servers($udom,'library');
                   1474:     foreach my $tryserver (keys(%servers)) {
                   1475: 	my $idlist=join('&',@ids);
                   1476: 	$idlist=~tr/A-Z/a-z/; 
                   1477: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1478: 	my @answer=();
                   1479: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1480: 	    @answer=split(/\&/,$reply);
                   1481: 	}                    ;
                   1482: 	my $i;
                   1483: 	for ($i=0;$i<=$#ids;$i++) {
                   1484: 	    if ($answer[$i]) {
                   1485: 		$returnhash{$ids[$i]}=$answer[$i];
                   1486: 	    } 
                   1487: 	}
                   1488:     } 
1.70      www      1489:     return %returnhash;
                   1490: }
                   1491: 
                   1492: # ------------------------------------- Find the IDs behind a list of usernames
                   1493: 
                   1494: sub idrget {
                   1495:     my ($udom,@unames)=@_;
                   1496:     my %returnhash=();
1.800     albertel 1497:     foreach my $uname (@unames) {
                   1498:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1499:     }
1.70      www      1500:     return %returnhash;
                   1501: }
                   1502: 
                   1503: # ------------------------------- Store away a list of names and associated IDs
                   1504: 
                   1505: sub idput {
                   1506:     my ($udom,%ids)=@_;
                   1507:     my %servers=();
1.800     albertel 1508:     foreach my $uname (keys(%ids)) {
                   1509: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                   1510:         my $uhom=&homeserver($uname,$udom);
1.70      www      1511:         if ($uhom ne 'no_host') {
1.800     albertel 1512:             my $id=&escape($ids{$uname});
1.70      www      1513:             $id=~tr/A-Z/a-z/;
1.800     albertel 1514:             my $esc_unam=&escape($uname);
1.70      www      1515: 	    if ($servers{$uhom}) {
1.800     albertel 1516: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www      1517:             } else {
1.800     albertel 1518:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www      1519:             }
                   1520:         }
1.191     harris41 1521:     }
1.800     albertel 1522:     foreach my $server (keys(%servers)) {
                   1523:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41 1524:     }
1.344     www      1525: }
                   1526: 
1.1023    raeburn  1527: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  1528: sub dump_dom {
1.1023    raeburn  1529:     my ($namespace,$udom,$regexp,$range)=@_;
1.1012    raeburn  1530:     if (!$udom) {
                   1531:         $udom=$env{'user.domain'};
                   1532:     }
                   1533:     my %returnhash;
1.1023    raeburn  1534:     if ($udom) {
                   1535:         my $uname = &get_domainconfiguser($udom);
                   1536:         %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012    raeburn  1537:     }
                   1538:     return %returnhash;
                   1539: }
                   1540: 
1.1023    raeburn  1541: # ------------------------------------------ get items from domain db files   
1.806     raeburn  1542: 
                   1543: sub get_dom {
1.860     raeburn  1544:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn  1545:     my $items='';
                   1546:     foreach my $item (@$storearr) {
                   1547:         $items.=&escape($item).'&';
                   1548:     }
                   1549:     $items=~s/\&$//;
1.860     raeburn  1550:     if (!$udom) {
                   1551:         $udom=$env{'user.domain'};
                   1552:         if (defined(&domain($udom,'primary'))) {
                   1553:             $uhome=&domain($udom,'primary');
                   1554:         } else {
1.874     albertel 1555:             undef($uhome);
1.860     raeburn  1556:         }
                   1557:     } else {
                   1558:         if (!$uhome) {
                   1559:             if (defined(&domain($udom,'primary'))) {
                   1560:                 $uhome=&domain($udom,'primary');
                   1561:             }
                   1562:         }
                   1563:     }
                   1564:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1565:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn  1566:         my %returnhash;
1.875     albertel 1567:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  1568:             return %returnhash;
                   1569:         }
1.806     raeburn  1570:         my @pairs=split(/\&/,$rep);
                   1571:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   1572:             return @pairs;
                   1573:         }
                   1574:         my $i=0;
                   1575:         foreach my $item (@$storearr) {
                   1576:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   1577:             $i++;
                   1578:         }
                   1579:         return %returnhash;
                   1580:     } else {
1.880     banghart 1581:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  1582:     }
                   1583: }
                   1584: 
                   1585: # -------------------------------------------- put items in domain db files 
                   1586: 
                   1587: sub put_dom {
1.860     raeburn  1588:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   1589:     if (!$udom) {
                   1590:         $udom=$env{'user.domain'};
                   1591:         if (defined(&domain($udom,'primary'))) {
                   1592:             $uhome=&domain($udom,'primary');
                   1593:         } else {
1.874     albertel 1594:             undef($uhome);
1.860     raeburn  1595:         }
                   1596:     } else {
                   1597:         if (!$uhome) {
                   1598:             if (defined(&domain($udom,'primary'))) {
                   1599:                 $uhome=&domain($udom,'primary');
                   1600:             }
                   1601:         }
                   1602:     } 
                   1603:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1604:         my $items='';
                   1605:         foreach my $item (keys(%$storehash)) {
                   1606:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   1607:         }
                   1608:         $items=~s/\&$//;
                   1609:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                   1610:     } else {
1.860     raeburn  1611:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  1612:     }
                   1613: }
                   1614: 
1.1023    raeburn  1615: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  1616: sub newput_dom {
1.1023    raeburn  1617:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  1618:     my $result;
                   1619:     if (!$udom) {
                   1620:         $udom=$env{'user.domain'};
                   1621:     }
1.1023    raeburn  1622:     if ($udom) {
                   1623:         my $uname = &get_domainconfiguser($udom);
                   1624:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  1625:     }
                   1626:     return $result;
                   1627: }
                   1628: 
1.1023    raeburn  1629: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  1630: sub del_dom {
1.1023    raeburn  1631:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  1632:     if (ref($storearr) eq 'ARRAY') {
                   1633:         if (!$udom) {
                   1634:             $udom=$env{'user.domain'};
                   1635:         }
1.1023    raeburn  1636:         if ($udom) {
                   1637:             my $uname = &get_domainconfiguser($udom); 
                   1638:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  1639:         }
                   1640:     }
                   1641: }
                   1642: 
1.1023    raeburn  1643: # ----------------------------------construct domainconfig user for a domain 
                   1644: sub get_domainconfiguser {
                   1645:     my ($udom) = @_;
                   1646:     return $udom.'-domainconfig';
                   1647: }
                   1648: 
1.837     raeburn  1649: sub retrieve_inst_usertypes {
                   1650:     my ($udom) = @_;
                   1651:     my (%returnhash,@order);
1.989     raeburn  1652:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   1653:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   1654:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
                   1655:         %returnhash = %{$domdefs{'inststatustypes'}};
                   1656:         @order = @{$domdefs{'inststatusorder'}};
                   1657:     } else {
                   1658:         if (defined(&domain($udom,'primary'))) {
                   1659:             my $uhome=&domain($udom,'primary');
                   1660:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   1661:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
                   1662:                 &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
                   1663:                 return (\%returnhash,\@order);
                   1664:             }
                   1665:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   1666:             my @pairs=split(/\&/,$hashitems);
                   1667:             foreach my $item (@pairs) {
                   1668:                 my ($key,$value)=split(/=/,$item,2);
                   1669:                 $key = &unescape($key);
                   1670:                 next if ($key =~ /^error: 2 /);
                   1671:                 $returnhash{$key}=&thaw_unescape($value);
                   1672:             }
                   1673:             my @esc_order = split(/\&/,$orderitems);
                   1674:             foreach my $item (@esc_order) {
                   1675:                 push(@order,&unescape($item));
                   1676:             }
                   1677:         } else {
                   1678:             &logthis("get_dom failed - no primary domain server for $udom");
1.837     raeburn  1679:         }
                   1680:     }
                   1681:     return (\%returnhash,\@order);
                   1682: }
                   1683: 
1.868     raeburn  1684: sub is_domainimage {
                   1685:     my ($url) = @_;
                   1686:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                   1687:         if (&domain($1) ne '') {
                   1688:             return '1';
                   1689:         }
                   1690:     }
                   1691:     return;
                   1692: }
                   1693: 
1.899     raeburn  1694: sub inst_directory_query {
                   1695:     my ($srch) = @_;
                   1696:     my $udom = $srch->{'srchdomain'};
                   1697:     my %results;
                   1698:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  1699:     my $outcome;
1.899     raeburn  1700:     if ($homeserver ne '') {
1.904     albertel 1701: 	my $queryid=&reply("querysend:instdirsearch:".
                   1702: 			   &escape($srch->{'srchby'}).':'.
                   1703: 			   &escape($srch->{'srchterm'}).':'.
                   1704: 			   &escape($srch->{'srchtype'}),$homeserver);
                   1705: 	my $host=&hostname($homeserver);
                   1706: 	if ($queryid !~/^\Q$host\E\_/) {
                   1707: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1708: 	    return;
                   1709: 	}
                   1710: 	my $response = &get_query_reply($queryid);
                   1711: 	my $maxtries = 5;
                   1712: 	my $tries = 1;
                   1713: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1714: 	    $response = &get_query_reply($queryid);
                   1715: 	    $tries ++;
                   1716: 	}
                   1717: 
                   1718:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  1719:             if ($response eq 'unavailable') {
                   1720:                 $outcome = $response;
                   1721:             } else {
                   1722:                 $outcome = 'ok';
                   1723:                 my @matches = split(/\n/,$response);
                   1724:                 foreach my $match (@matches) {
                   1725:                     my ($key,$value) = split(/=/,$match);
                   1726:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   1727:                 }
1.899     raeburn  1728:             }
                   1729:         }
                   1730:     }
1.909     raeburn  1731:     return ($outcome,%results);
1.899     raeburn  1732: }
                   1733: 
                   1734: sub usersearch {
                   1735:     my ($srch) = @_;
                   1736:     my $dom = $srch->{'srchdomain'};
                   1737:     my %results;
                   1738:     my %libserv = &all_library();
                   1739:     my $query = 'usersearch';
                   1740:     foreach my $tryserver (keys(%libserv)) {
                   1741:         if (&host_domain($tryserver) eq $dom) {
                   1742:             my $host=&hostname($tryserver);
                   1743:             my $queryid=
1.911     raeburn  1744:                 &reply("querysend:".&escape($query).':'.
                   1745:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  1746:                        &escape($srch->{'srchtype'}).':'.
                   1747:                        &escape($srch->{'srchterm'}),$tryserver);
                   1748:             if ($queryid !~/^\Q$host\E\_/) {
                   1749:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  1750:                 next;
1.899     raeburn  1751:             }
                   1752:             my $reply = &get_query_reply($queryid);
                   1753:             my $maxtries = 1;
                   1754:             my $tries = 1;
                   1755:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   1756:                 $reply = &get_query_reply($queryid);
                   1757:                 $tries ++;
                   1758:             }
                   1759:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   1760:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   1761:             } else {
1.911     raeburn  1762:                 my @matches;
                   1763:                 if ($reply =~ /\n/) {
                   1764:                     @matches = split(/\n/,$reply);
                   1765:                 } else {
                   1766:                     @matches = split(/\&/,$reply);
                   1767:                 }
1.899     raeburn  1768:                 foreach my $match (@matches) {
                   1769:                     my ($uname,$udom,%userhash);
1.911     raeburn  1770:                     foreach my $entry (split(/:/,$match)) {
                   1771:                         my ($key,$value) =
                   1772:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  1773:                         $userhash{$key} = $value;
                   1774:                         if ($key eq 'username') {
                   1775:                             $uname = $value;
                   1776:                         } elsif ($key eq 'domain') {
                   1777:                             $udom = $value;
1.911     raeburn  1778:                         }
1.899     raeburn  1779:                     }
                   1780:                     $results{$uname.':'.$udom} = \%userhash;
                   1781:                 }
                   1782:             }
                   1783:         }
                   1784:     }
                   1785:     return %results;
                   1786: }
                   1787: 
1.912     raeburn  1788: sub get_instuser {
                   1789:     my ($udom,$uname,$id) = @_;
                   1790:     my $homeserver = &domain($udom,'primary');
                   1791:     my ($outcome,%results);
                   1792:     if ($homeserver ne '') {
                   1793:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   1794:                            &escape($id).':'.&escape($udom),$homeserver);
                   1795:         my $host=&hostname($homeserver);
                   1796:         if ($queryid !~/^\Q$host\E\_/) {
                   1797:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1798:             return;
                   1799:         }
                   1800:         my $response = &get_query_reply($queryid);
                   1801:         my $maxtries = 5;
                   1802:         my $tries = 1;
                   1803:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1804:             $response = &get_query_reply($queryid);
                   1805:             $tries ++;
                   1806:         }
                   1807:         if (!&error($response) && $response ne 'refused') {
                   1808:             if ($response eq 'unavailable') {
                   1809:                 $outcome = $response;
                   1810:             } else {
                   1811:                 $outcome = 'ok';
                   1812:                 my @matches = split(/\n/,$response);
                   1813:                 foreach my $match (@matches) {
                   1814:                     my ($key,$value) = split(/=/,$match);
                   1815:                     $results{&unescape($key)} = &thaw_unescape($value);
                   1816:                 }
                   1817:             }
                   1818:         }
                   1819:     }
                   1820:     my %userinfo;
                   1821:     if (ref($results{$uname}) eq 'HASH') {
                   1822:         %userinfo = %{$results{$uname}};
                   1823:     } 
                   1824:     return ($outcome,%userinfo);
                   1825: }
                   1826: 
                   1827: sub inst_rulecheck {
1.923     raeburn  1828:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  1829:     my %returnhash;
                   1830:     if ($udom ne '') {
                   1831:         if (ref($rules) eq 'ARRAY') {
                   1832:             @{$rules} = map {&escape($_);} (@{$rules});
                   1833:             my $rulestr = join(':',@{$rules});
                   1834:             my $homeserver=&domain($udom,'primary');
                   1835:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1836:                 my $response;
                   1837:                 if ($item eq 'username') {                
                   1838:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   1839:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  1840:                                               $homeserver));
1.923     raeburn  1841:                 } elsif ($item eq 'id') {
                   1842:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   1843:                                               ':'.&escape($id).':'.$rulestr,
                   1844:                                               $homeserver));
1.945     raeburn  1845:                 } elsif ($item eq 'selfcreate') {
                   1846:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  1847:                                                &escape($udom).':'.&escape($uname).
                   1848:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  1849:                 }
1.912     raeburn  1850:                 if ($response ne 'refused') {
                   1851:                     my @pairs=split(/\&/,$response);
                   1852:                     foreach my $item (@pairs) {
                   1853:                         my ($key,$value)=split(/=/,$item,2);
                   1854:                         $key = &unescape($key);
                   1855:                         next if ($key =~ /^error: 2 /);
                   1856:                         $returnhash{$key}=&thaw_unescape($value);
                   1857:                     }
                   1858:                 }
                   1859:             }
                   1860:         }
                   1861:     }
                   1862:     return %returnhash;
                   1863: }
                   1864: 
                   1865: sub inst_userrules {
1.923     raeburn  1866:     my ($udom,$check) = @_;
1.912     raeburn  1867:     my (%ruleshash,@ruleorder);
                   1868:     if ($udom ne '') {
                   1869:         my $homeserver=&domain($udom,'primary');
                   1870:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1871:             my $response;
                   1872:             if ($check eq 'id') {
                   1873:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  1874:                                  $homeserver);
1.943     raeburn  1875:             } elsif ($check eq 'email') {
                   1876:                 $response=&reply('instemailrules:'.&escape($udom),
                   1877:                                  $homeserver);
1.923     raeburn  1878:             } else {
                   1879:                 $response=&reply('instuserrules:'.&escape($udom),
                   1880:                                  $homeserver);
                   1881:             }
1.912     raeburn  1882:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  1883:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  1884:                 ($response ne 'no_such_host')) {
                   1885:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   1886:                 my @pairs=split(/\&/,$hashitems);
                   1887:                 foreach my $item (@pairs) {
                   1888:                     my ($key,$value)=split(/=/,$item,2);
                   1889:                     $key = &unescape($key);
                   1890:                     next if ($key =~ /^error: 2 /);
                   1891:                     $ruleshash{$key}=&thaw_unescape($value);
                   1892:                 }
                   1893:                 my @esc_order = split(/\&/,$orderitems);
                   1894:                 foreach my $item (@esc_order) {
                   1895:                     push(@ruleorder,&unescape($item));
                   1896:                 }
                   1897:             }
                   1898:         }
                   1899:     }
                   1900:     return (\%ruleshash,\@ruleorder);
                   1901: }
                   1902: 
1.976     raeburn  1903: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  1904: 
                   1905: sub get_domain_defaults {
                   1906:     my ($domain) = @_;
                   1907:     my $cachetime = 60*60*24;
                   1908:     my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   1909:     if (defined($cached)) {
                   1910:         if (ref($result) eq 'HASH') {
                   1911:             return %{$result};
                   1912:         }
                   1913:     }
                   1914:     my %domdefaults;
                   1915:     my %domconfig =
1.989     raeburn  1916:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  1917:                                   'requestcourses','inststatus',
1.1073    raeburn  1918:                                   'coursedefaults','usersessions'],$domain);
1.943     raeburn  1919:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   1920:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   1921:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   1922:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  1923:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  1924:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.1147    raeburn  1925:         $domdefaults{'portal_def'} = $domconfig{'defaults'}{'portal_def'};
1.943     raeburn  1926:     } else {
                   1927:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   1928:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   1929:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   1930:     }
1.976     raeburn  1931:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   1932:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   1933:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   1934:         } else {
                   1935:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
                   1936:         } 
                   1937:         my @usertools = ('aboutme','blog','portfolio');
                   1938:         foreach my $item (@usertools) {
                   1939:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   1940:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   1941:             }
                   1942:         }
                   1943:     }
1.985     raeburn  1944:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006    raeburn  1945:         foreach my $item ('official','unofficial','community') {
1.985     raeburn  1946:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   1947:         }
                   1948:     }
1.989     raeburn  1949:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
                   1950:         foreach my $item ('inststatustypes','inststatusorder') {
                   1951:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   1952:         }
                   1953:     }
1.1047    raeburn  1954:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
                   1955:         foreach my $item ('canuse_pdfforms') {
                   1956:             $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
                   1957:         }
                   1958:     }
1.1073    raeburn  1959:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1960:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   1961:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   1962:         }
                   1963:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   1964:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   1965:         }
                   1966:     }
1.943     raeburn  1967:     &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
                   1968:                                   $cachetime);
                   1969:     return %domdefaults;
                   1970: }
                   1971: 
1.344     www      1972: # --------------------------------------------------- Assign a key to a student
                   1973: 
                   1974: sub assign_access_key {
1.364     www      1975: #
                   1976: # a valid key looks like uname:udom#comments
                   1977: # comments are being appended
                   1978: #
1.498     www      1979:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   1980:     $kdom=
1.620     albertel 1981:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      1982:     $knum=
1.620     albertel 1983:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      1984:     $cdom=
1.620     albertel 1985:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1986:     $cnum=
1.620     albertel 1987:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1988:     $udom=$env{'user.name'} unless (defined($udom));
                   1989:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      1990:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      1991:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 1992:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      1993:                                                   # assigned to this person
                   1994:                                                   # - this should not happen,
1.345     www      1995:                                                   # unless something went wrong
                   1996:                                                   # the first time around
                   1997: # ready to assign
1.364     www      1998:         $logentry=$1.'; '.$logentry;
1.496     www      1999:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      2000:                                                  $kdom,$knum) eq 'ok') {
1.345     www      2001: # key now belongs to user
1.346     www      2002: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      2003:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  2004:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      2005:                 return 'ok';
                   2006:             } else {
                   2007:                 return 
                   2008:   'error: Count not permanently assign key, will need to be re-entered later.';
                   2009: 	    }
                   2010:         } else {
                   2011:             return 'error: Could not assign key, try again later.';
                   2012:         }
1.364     www      2013:     } elsif (!$existing{$ckey}) {
1.345     www      2014: # the key does not exist
                   2015: 	return 'error: The key does not exist';
                   2016:     } else {
                   2017: # the key is somebody else's
                   2018: 	return 'error: The key is already in use';
                   2019:     }
1.344     www      2020: }
                   2021: 
1.364     www      2022: # ------------------------------------------ put an additional comment on a key
                   2023: 
                   2024: sub comment_access_key {
                   2025: #
                   2026: # a valid key looks like uname:udom#comments
                   2027: # comments are being appended
                   2028: #
                   2029:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   2030:     $cdom=
1.620     albertel 2031:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      2032:     $cnum=
1.620     albertel 2033:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      2034:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   2035:     if ($existing{$ckey}) {
                   2036:         $existing{$ckey}.='; '.$logentry;
                   2037: # ready to assign
1.367     www      2038:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      2039:                                                  $cdom,$cnum) eq 'ok') {
                   2040: 	    return 'ok';
                   2041:         } else {
                   2042: 	    return 'error: Count not store comment.';
                   2043:         }
                   2044:     } else {
                   2045: # the key does not exist
                   2046: 	return 'error: The key does not exist';
                   2047:     }
                   2048: }
                   2049: 
1.344     www      2050: # ------------------------------------------------------ Generate a set of keys
                   2051: 
                   2052: sub generate_access_keys {
1.364     www      2053:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      2054:     $cdom=
1.620     albertel 2055:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2056:     $cnum=
1.620     albertel 2057:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      2058:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      2059:     unless (($cdom) && ($cnum)) { return 0; }
                   2060:     if ($number>10000) { return 0; }
                   2061:     sleep(2); # make sure don't get same seed twice
                   2062:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   2063:     my $total=0;
                   2064:     for (my $i=1;$i<=$number;$i++) {
                   2065:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   2066:                   sprintf("%lx",int(100000*rand)).'-'.
                   2067:                   sprintf("%lx",int(100000*rand));
                   2068:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   2069:        $newkey=~s/0/h/g; # and also 0 and O
                   2070:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   2071:        if ($existing{$newkey}) {
                   2072:            $i--;
                   2073:        } else {
1.364     www      2074: 	  if (&put('accesskeys',
                   2075:               { $newkey => '# generated '.localtime().
1.620     albertel 2076:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      2077:                            '; '.$logentry },
                   2078: 		   $cdom,$cnum) eq 'ok') {
1.344     www      2079:               $total++;
                   2080: 	  }
                   2081:        }
                   2082:     }
1.620     albertel 2083:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      2084:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   2085:     return $total;
                   2086: }
                   2087: 
                   2088: # ------------------------------------------------------- Validate an accesskey
                   2089: 
                   2090: sub validate_access_key {
                   2091:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   2092:     $cdom=
1.620     albertel 2093:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      2094:     $cnum=
1.620     albertel 2095:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   2096:     $udom=$env{'user.domain'} unless (defined($udom));
                   2097:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      2098:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 2099:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      2100: }
                   2101: 
                   2102: # ------------------------------------- Find the section of student in a course
1.652     albertel 2103: sub devalidate_getsection_cache {
                   2104:     my ($udom,$unam,$courseid)=@_;
                   2105:     my $hashid="$udom:$unam:$courseid";
                   2106:     &devalidate_cache_new('getsection',$hashid);
                   2107: }
1.298     matthew  2108: 
1.815     albertel 2109: sub courseid_to_courseurl {
                   2110:     my ($courseid) = @_;
                   2111:     #already url style courseid
                   2112:     return $courseid if ($courseid =~ m{^/});
                   2113: 
                   2114:     if (exists($env{'course.'.$courseid.'.num'})) {
                   2115: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   2116: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   2117: 	return "/$cdom/$cnum";
                   2118:     }
                   2119: 
                   2120:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   2121:     if (exists($courseinfo{'num'})) {
                   2122: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   2123:     }
                   2124: 
                   2125:     return undef;
                   2126: }
                   2127: 
1.298     matthew  2128: sub getsection {
                   2129:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 2130:     my $cachetime=1800;
1.551     albertel 2131: 
                   2132:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 2133:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 2134:     if (defined($cached)) { return $result; }
                   2135: 
1.298     matthew  2136:     my %Pending; 
                   2137:     my %Expired;
                   2138:     #
                   2139:     # Each role can either have not started yet (pending), be active, 
                   2140:     #    or have expired.
                   2141:     #
                   2142:     # If there is an active role, we are done.
                   2143:     #
                   2144:     # If there is more than one role which has not started yet, 
                   2145:     #     choose the one which will start sooner
                   2146:     # If there is one role which has not started yet, return it.
                   2147:     #
                   2148:     # If there is more than one expired role, choose the one which ended last.
                   2149:     # If there is a role which has expired, return it.
                   2150:     #
1.815     albertel 2151:     $courseid = &courseid_to_courseurl($courseid);
1.1086    raeburn  2152:     my $extra = &freeze_escape({'skipcheck' => 1});
                   2153:     my %roleshash = &dump('roles',$udom,$unam,$courseid,undef,$extra);
1.817     raeburn  2154:     foreach my $key (keys(%roleshash)) {
1.479     albertel 2155:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  2156:         my $section=$1;
                   2157:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  2158:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  2159:         my $now=time;
1.548     albertel 2160:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  2161:             $Expired{$end}=$section;
                   2162:             next;
                   2163:         }
1.548     albertel 2164:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  2165:             $Pending{$start}=$section;
                   2166:             next;
                   2167:         }
1.599     albertel 2168:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  2169:     }
                   2170:     #
                   2171:     # Presumedly there will be few matching roles from the above
                   2172:     # loop and the sorting time will be negligible.
                   2173:     if (scalar(keys(%Pending))) {
                   2174:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 2175:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  2176:     } 
                   2177:     if (scalar(keys(%Expired))) {
                   2178:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   2179:         my $time = pop(@sorted);
1.599     albertel 2180:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  2181:     }
1.599     albertel 2182:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  2183: }
1.70      www      2184: 
1.599     albertel 2185: sub save_cache {
                   2186:     &purge_remembered();
1.722     albertel 2187:     #&Apache::loncommon::validate_page();
1.620     albertel 2188:     undef(%env);
1.780     albertel 2189:     undef($env_loaded);
1.599     albertel 2190: }
1.452     albertel 2191: 
1.599     albertel 2192: my $to_remember=-1;
                   2193: my %remembered;
                   2194: my %accessed;
                   2195: my $kicks=0;
                   2196: my $hits=0;
1.849     albertel 2197: sub make_key {
                   2198:     my ($name,$id) = @_;
1.872     albertel 2199:     if (length($id) > 65 
                   2200: 	&& length(&escape($id)) > 200) {
                   2201: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   2202:     }
1.849     albertel 2203:     return &escape($name.':'.$id);
                   2204: }
                   2205: 
1.599     albertel 2206: sub devalidate_cache_new {
                   2207:     my ($name,$id,$debug) = @_;
                   2208:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 2209:     $id=&make_key($name,$id);
1.599     albertel 2210:     $memcache->delete($id);
                   2211:     delete($remembered{$id});
                   2212:     delete($accessed{$id});
                   2213: }
                   2214: 
                   2215: sub is_cached_new {
                   2216:     my ($name,$id,$debug) = @_;
1.849     albertel 2217:     $id=&make_key($name,$id);
1.599     albertel 2218:     if (exists($remembered{$id})) {
1.1133    foxr     2219: 	if ($debug) { &Apache::lonnet::logthis("Early return $id of $remembered{$id} "); }
1.599     albertel 2220: 	$accessed{$id}=[&gettimeofday()];
                   2221: 	$hits++;
                   2222: 	return ($remembered{$id},1);
                   2223:     }
                   2224:     my $value = $memcache->get($id);
                   2225:     if (!(defined($value))) {
                   2226: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 2227: 	return (undef,undef);
1.416     albertel 2228:     }
1.599     albertel 2229:     if ($value eq '__undef__') {
                   2230: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   2231: 	$value=undef;
                   2232:     }
                   2233:     &make_room($id,$value,$debug);
                   2234:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   2235:     return ($value,1);
                   2236: }
                   2237: 
                   2238: sub do_cache_new {
                   2239:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 2240:     $id=&make_key($name,$id);
1.599     albertel 2241:     my $setvalue=$value;
                   2242:     if (!defined($setvalue)) {
                   2243: 	$setvalue='__undef__';
                   2244:     }
1.623     albertel 2245:     if (!defined($time) ) {
                   2246: 	$time=600;
                   2247:     }
1.599     albertel 2248:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 2249:     my $result = $memcache->set($id,$setvalue,$time);
                   2250:     if (! $result) {
1.872     albertel 2251: 	&logthis("caching of id -> $id  failed");
1.910     albertel 2252: 	$memcache->disconnect_all();
1.872     albertel 2253:     }
1.600     albertel 2254:     # need to make a copy of $value
1.919     albertel 2255:     &make_room($id,$value,$debug);
1.599     albertel 2256:     return $value;
                   2257: }
                   2258: 
                   2259: sub make_room {
                   2260:     my ($id,$value,$debug)=@_;
1.919     albertel 2261: 
                   2262:     $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
                   2263:                                     : $value;
1.599     albertel 2264:     if ($to_remember<0) { return; }
                   2265:     $accessed{$id}=[&gettimeofday()];
                   2266:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   2267:     my $to_kick;
                   2268:     my $max_time=0;
                   2269:     foreach my $other (keys(%accessed)) {
                   2270: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   2271: 	    $to_kick=$other;
                   2272: 	    $max_time=&tv_interval($accessed{$other});
                   2273: 	}
                   2274:     }
                   2275:     delete($remembered{$to_kick});
                   2276:     delete($accessed{$to_kick});
                   2277:     $kicks++;
                   2278:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 2279:     return;
                   2280: }
                   2281: 
1.599     albertel 2282: sub purge_remembered {
1.604     albertel 2283:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   2284:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 2285:     undef(%remembered);
                   2286:     undef(%accessed);
1.428     albertel 2287: }
1.70      www      2288: # ------------------------------------- Read an entry from a user's environment
                   2289: 
                   2290: sub userenvironment {
                   2291:     my ($udom,$unam,@what)=@_;
1.976     raeburn  2292:     my $items;
                   2293:     foreach my $item (@what) {
                   2294:         $items.=&escape($item).'&';
                   2295:     }
                   2296:     $items=~s/\&$//;
1.70      www      2297:     my %returnhash=();
1.1009    raeburn  2298:     my $uhome = &homeserver($unam,$udom);
                   2299:     unless ($uhome eq 'no_host') {
                   2300:         my @answer=split(/\&/, 
                   2301:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  2302:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   2303:             return %returnhash;
                   2304:         }
1.1009    raeburn  2305:         my $i;
                   2306:         for ($i=0;$i<=$#what;$i++) {
                   2307: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   2308:         }
1.70      www      2309:     }
                   2310:     return %returnhash;
1.1       albertel 2311: }
                   2312: 
1.617     albertel 2313: # ---------------------------------------------------------- Get a studentphoto
                   2314: sub studentphoto {
                   2315:     my ($udom,$unam,$ext) = @_;
                   2316:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  2317:     if (defined($env{'request.course.id'})) {
1.708     raeburn  2318:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  2319:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   2320:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   2321:             } else {
                   2322:                 my ($result,$perm_reqd)=
1.707     albertel 2323: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2324:                 if ($result eq 'ok') {
                   2325:                     if (!($perm_reqd eq 'yes')) {
                   2326:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   2327:                     }
                   2328:                 }
                   2329:             }
                   2330:         }
                   2331:     } else {
                   2332:         my ($result,$perm_reqd) = 
1.707     albertel 2333: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  2334:         if ($result eq 'ok') {
                   2335:             if (!($perm_reqd eq 'yes')) {
                   2336:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   2337:             }
                   2338:         }
                   2339:     }
                   2340:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   2341: }
                   2342: 
                   2343: sub retrievestudentphoto {
                   2344:     my ($udom,$unam,$ext,$type) = @_;
                   2345:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   2346:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   2347:     if ($ret eq 'ok') {
                   2348:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   2349:         if ($type eq 'thumbnail') {
                   2350:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   2351:         }
                   2352:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   2353:         return $tokenurl;
                   2354:     } else {
                   2355:         if ($type eq 'thumbnail') {
                   2356:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   2357:         } else { 
                   2358:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   2359:         }
1.617     albertel 2360:     }
                   2361: }
                   2362: 
1.263     www      2363: # -------------------------------------------------------------------- New chat
                   2364: 
                   2365: sub chatsend {
1.724     raeburn  2366:     my ($newentry,$anon,$group)=@_;
1.620     albertel 2367:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2368:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2369:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      2370:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 2371: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  2372: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      2373: }
                   2374: 
                   2375: # ------------------------------------------ Find current version of a resource
                   2376: 
                   2377: sub getversion {
                   2378:     my $fname=&clutter(shift);
                   2379:     unless ($fname=~/^\/res\//) { return -1; }
                   2380:     return &currentversion(&filelocation('',$fname));
                   2381: }
                   2382: 
                   2383: sub currentversion {
                   2384:     my $fname=shift;
                   2385:     my $author=$fname;
                   2386:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2387:     my ($udom,$uname)=split(/\//,$author);
1.1112    www      2388:     my $home=&homeserver($uname,$udom);
1.292     www      2389:     if ($home eq 'no_host') { 
                   2390:         return -1; 
                   2391:     }
1.1112    www      2392:     my $answer=&reply("currentversion:$fname",$home);
1.292     www      2393:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2394: 	return -1;
                   2395:     }
1.1112    www      2396:     return $answer;
1.263     www      2397: }
                   2398: 
1.1111    www      2399: #
                   2400: # Return special version number of resource if set by override, empty otherwise
                   2401: #
                   2402: sub usedversion {
                   2403:     my $fname=shift;
                   2404:     unless ($fname) { $fname=$env{'request.uri'}; }
                   2405:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
                   2406:     if ($urlversion) { return $urlversion; }
                   2407:     return '';
                   2408: }
                   2409: 
1.1       albertel 2410: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      2411: 
1.1       albertel 2412: sub subscribe {
                   2413:     my $fname=shift;
1.761     raeburn  2414:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 2415:     $fname=~s/[\n\r]//g;
1.1       albertel 2416:     my $author=$fname;
                   2417:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2418:     my ($udom,$uname)=split(/\//,$author);
                   2419:     my $home=homeserver($uname,$udom);
1.335     albertel 2420:     if ($home eq 'no_host') {
                   2421:         return 'not_found';
1.1       albertel 2422:     }
                   2423:     my $answer=reply("sub:$fname",$home);
1.64      www      2424:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2425: 	$answer.=' by '.$home;
                   2426:     }
1.1       albertel 2427:     return $answer;
                   2428: }
                   2429:     
1.8       www      2430: # -------------------------------------------------------------- Replicate file
                   2431: 
                   2432: sub repcopy {
                   2433:     my $filename=shift;
1.23      www      2434:     $filename=~s/\/+/\//g;
1.1142    raeburn  2435:     my $londocroot = $perlvar{'lonDocRoot'};
                   2436:     if ($filename=~m{^\Q$londocroot/adm/\E}) { return 'ok'; }
                   2437:     if ($filename=~m{^\Q$londocroot/lonUsers/\E}) { return 'ok'; }
                   2438:     if ($filename=~m{^\Q$londocroot/userfiles/\E} or
                   2439: 	$filename=~m{^/*(uploaded|editupload)/}) {
1.538     albertel 2440: 	return &repcopy_userfile($filename);
                   2441:     }
1.532     albertel 2442:     $filename=~s/[\n\r]//g;
1.8       www      2443:     my $transname="$filename.in.transfer";
1.828     www      2444: # FIXME: this should flock
1.607     raeburn  2445:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      2446:     my $remoteurl=subscribe($filename);
1.64      www      2447:     if ($remoteurl =~ /^con_lost by/) {
                   2448: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2449:            return 'unavailable';
1.8       www      2450:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 2451: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  2452: 	   return 'not_found';
1.64      www      2453:     } elsif ($remoteurl =~ /^rejected by/) {
                   2454: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2455:            return 'forbidden';
1.20      www      2456:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  2457:            return 'ok';
1.8       www      2458:     } else {
1.290     www      2459:         my $author=$filename;
                   2460:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2461:         my ($udom,$uname)=split(/\//,$author);
                   2462:         my $home=homeserver($uname,$udom);
                   2463:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      2464:            my @parts=split(/\//,$filename);
                   2465:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1.1142    raeburn  2466:            if ($path ne "$londocroot/res") {
1.8       www      2467:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  2468: 	       return 'bad_request';
1.8       www      2469:            }
                   2470:            my $count;
                   2471:            for ($count=5;$count<$#parts;$count++) {
                   2472:                $path.="/$parts[$count]";
                   2473:                if ((-e $path)!=1) {
                   2474: 		   mkdir($path,0777);
                   2475:                }
                   2476:            }
                   2477:            my $ua=new LWP::UserAgent;
                   2478:            my $request=new HTTP::Request('GET',"$remoteurl");
                   2479:            my $response=$ua->request($request,$transname);
                   2480:            if ($response->is_error()) {
                   2481: 	       unlink($transname);
                   2482:                my $message=$response->status_line;
1.672     albertel 2483:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      2484:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  2485:                return 'unavailable';
1.8       www      2486:            } else {
1.16      www      2487: 	       if ($remoteurl!~/\.meta$/) {
                   2488:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   2489:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   2490:                   if ($mresponse->is_error()) {
                   2491: 		      unlink($filename.'.meta');
                   2492:                       &logthis(
1.672     albertel 2493:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      2494:                   }
                   2495: 	       }
1.8       www      2496:                rename($transname,$filename);
1.607     raeburn  2497:                return 'ok';
1.8       www      2498:            }
1.290     www      2499:        }
1.8       www      2500:     }
1.330     www      2501: }
                   2502: 
                   2503: # ------------------------------------------------ Get server side include body
                   2504: sub ssi_body {
1.381     albertel 2505:     my ($filelink,%form)=@_;
1.606     matthew  2506:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   2507:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   2508:     }
1.953     www      2509:     my $output='';
                   2510:     my $response;
1.980     raeburn  2511:     if ($filelink=~/^https?\:/) {
1.954     raeburn  2512:        ($output,$response)=&externalssi($filelink);
1.953     www      2513:     } else {
1.1004    droeschl 2514:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   2515:        $filelink .= 'inhibitmenu=yes';
1.953     www      2516:        ($output,$response)=&ssi($filelink,%form);
                   2517:     }
1.778     albertel 2518:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 2519:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 2520:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      2521:     if (wantarray) {
                   2522:         return ($output, $response);
                   2523:     } else {
                   2524:         return $output;
                   2525:     }
1.8       www      2526: }
                   2527: 
1.15      www      2528: # --------------------------------------------------------- Server Side Include
                   2529: 
1.782     albertel 2530: sub absolute_url {
                   2531:     my ($host_name) = @_;
                   2532:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   2533:     if ($host_name eq '') {
                   2534: 	$host_name = $ENV{'SERVER_NAME'};
                   2535:     }
                   2536:     return $protocol.$host_name;
                   2537: }
                   2538: 
1.942     foxr     2539: #
                   2540: #   Server side include.
                   2541: # Parameters:
                   2542: #  fn     Possibly encrypted resource name/id.
                   2543: #  form   Hash that describes how the rendering should be done
                   2544: #         and other things.
1.944     foxr     2545: # Returns:
1.950     raeburn  2546: #   Scalar context: The content of the response.
                   2547: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     2548: #     
1.15      www      2549: sub ssi {
                   2550: 
1.944     foxr     2551:     my ($fn,%form)=@_;
1.15      www      2552:     my $ua=new LWP::UserAgent;
1.23      www      2553:     my $request;
1.711     albertel 2554: 
                   2555:     $form{'no_update_last_known'}=1;
1.895     albertel 2556:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      2557:     if (%form) {
1.782     albertel 2558:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000    raeburn  2559:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23      www      2560:     } else {
1.782     albertel 2561:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      2562:     }
                   2563: 
1.15      www      2564:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   2565:     my $response=$ua->request($request);
                   2566: 
1.944     foxr     2567:     if (wantarray) {
                   2568: 	return ($response->content, $response);
                   2569:     } else {
                   2570: 	return $response->content;
1.942     foxr     2571:     }
1.324     www      2572: }
                   2573: 
                   2574: sub externalssi {
                   2575:     my ($url)=@_;
                   2576:     my $ua=new LWP::UserAgent;
                   2577:     my $request=new HTTP::Request('GET',$url);
                   2578:     my $response=$ua->request($request);
1.954     raeburn  2579:     if (wantarray) {
                   2580:         return ($response->content, $response);
                   2581:     } else {
                   2582:         return $response->content;
                   2583:     }
1.15      www      2584: }
1.254     www      2585: 
1.492     albertel 2586: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   2587: 
                   2588: sub allowuploaded {
                   2589:     my ($srcurl,$url)=@_;
                   2590:     $url=&clutter(&declutter($url));
                   2591:     my $dir=$url;
                   2592:     $dir=~s/\/[^\/]+$//;
                   2593:     my %httpref=();
                   2594:     my $httpurl=&hreflocation('',$url);
                   2595:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  2596:     &Apache::lonnet::appenv(\%httpref);
1.254     www      2597: }
1.477     raeburn  2598: 
1.478     albertel 2599: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 2600: # input: action, courseID, current domain, intended
1.637     raeburn  2601: #        path to file, source of file, instruction to parse file for objects,
                   2602: #        ref to hash for embedded objects,
                   2603: #        ref to hash for codebase of java objects.
1.1095    raeburn  2604: #        reference to scalar to accommodate mime type determined
                   2605: #          from File::MMagic if $parser = parse.
1.637     raeburn  2606: #
1.485     raeburn  2607: # output: url to file (if action was uploaddoc), 
                   2608: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  2609: #
1.478     albertel 2610: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   2611: # course.
1.477     raeburn  2612: #
1.478     albertel 2613: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2614: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   2615: #          course's home server.
1.477     raeburn  2616: #
1.478     albertel 2617: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   2618: #          be copied from $source (current location) to 
                   2619: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2620: #         and will then be copied to
                   2621: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   2622: #         course's home server.
1.485     raeburn  2623: #
1.481     raeburn  2624: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 2625: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  2626: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2627: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   2628: #         in course's home server.
1.637     raeburn  2629: #
1.477     raeburn  2630: 
                   2631: sub process_coursefile {
1.1095    raeburn  2632:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   2633:         $mimetype)=@_;
1.477     raeburn  2634:     my $fetchresult;
1.638     albertel 2635:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  2636:     if ($action eq 'propagate') {
1.638     albertel 2637:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   2638: 			     $home);
1.481     raeburn  2639:     } else {
1.477     raeburn  2640:         my $fpath = '';
                   2641:         my $fname = $file;
1.478     albertel 2642:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  2643:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  2644:         my $filepath = &build_filepath($fpath);
1.481     raeburn  2645:         if ($action eq 'copy') {
                   2646:             if ($source eq '') {
                   2647:                 $fetchresult = 'no source file';
                   2648:                 return $fetchresult;
                   2649:             } else {
                   2650:                 my $destination = $filepath.'/'.$fname;
                   2651:                 rename($source,$destination);
                   2652:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2653:                                  $home);
1.481     raeburn  2654:             }
                   2655:         } elsif ($action eq 'uploaddoc') {
                   2656:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 2657:             print $fh $env{'form.'.$source};
1.481     raeburn  2658:             close($fh);
1.637     raeburn  2659:             if ($parser eq 'parse') {
1.1024    raeburn  2660:                 my $mm = new File::MMagic;
1.1095    raeburn  2661:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   2662:                 if ($type eq 'text/html') {
1.1024    raeburn  2663:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   2664:                     unless ($parse_result eq 'ok') {
                   2665:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   2666:                     }
1.637     raeburn  2667:                 }
1.1095    raeburn  2668:                 if (ref($mimetype)) {
                   2669:                     $$mimetype = $type;
                   2670:                 } 
1.637     raeburn  2671:             }
1.477     raeburn  2672:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2673:                                  $home);
1.481     raeburn  2674:             if ($fetchresult eq 'ok') {
                   2675:                 return '/uploaded/'.$fpath.'/'.$fname;
                   2676:             } else {
                   2677:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2678:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  2679:                 return '/adm/notfound.html';
                   2680:             }
1.477     raeburn  2681:         }
                   2682:     }
1.485     raeburn  2683:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  2684:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2685:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  2686:     }
                   2687:     return $fetchresult;
                   2688: }
                   2689: 
1.637     raeburn  2690: sub build_filepath {
                   2691:     my ($fpath) = @_;
                   2692:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   2693:     unless ($fpath eq '') {
                   2694:         my @parts=split('/',$fpath);
                   2695:         foreach my $part (@parts) {
                   2696:             $filepath.= '/'.$part;
                   2697:             if ((-e $filepath)!=1) {
                   2698:                 mkdir($filepath,0777);
                   2699:             }
                   2700:         }
                   2701:     }
                   2702:     return $filepath;
                   2703: }
                   2704: 
                   2705: sub store_edited_file {
1.638     albertel 2706:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  2707:     my $file = $primary_url;
                   2708:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   2709:     my $fpath = '';
                   2710:     my $fname = $file;
                   2711:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   2712:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   2713:     my $filepath = &build_filepath($fpath);
                   2714:     open(my $fh,'>'.$filepath.'/'.$fname);
                   2715:     print $fh $content;
                   2716:     close($fh);
1.638     albertel 2717:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  2718:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2719: 			  $home);
1.637     raeburn  2720:     if ($$fetchresult eq 'ok') {
                   2721:         return '/uploaded/'.$fpath.'/'.$fname;
                   2722:     } else {
1.638     albertel 2723:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   2724: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  2725:         return '/adm/notfound.html';
                   2726:     }
                   2727: }
                   2728: 
1.531     albertel 2729: sub clean_filename {
1.831     albertel 2730:     my ($fname,$args)=@_;
1.315     www      2731: # Replace Windows backslashes by forward slashes
1.257     www      2732:     $fname=~s/\\/\//g;
1.831     albertel 2733:     if (!$args->{'keep_path'}) {
                   2734:         # Get rid of everything but the actual filename
                   2735: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   2736:     }
1.315     www      2737: # Replace spaces by underscores
                   2738:     $fname=~s/\s+/\_/g;
                   2739: # Replace all other weird characters by nothing
1.831     albertel 2740:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 2741: # Replace all .\d. sequences with _\d. so they no longer look like version
                   2742: # numbers
                   2743:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 2744:     return $fname;
                   2745: }
1.1051    raeburn  2746: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   2747: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   2748: # image with the same aspect ratio as the original, but with dimensions which do 
                   2749: # not exceed $resizewidth and $resizeheight.
                   2750:  
1.984     neumanie 2751: sub resizeImage {
1.1051    raeburn  2752:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   2753:     my $ima = Image::Magick->new;
                   2754:     my $resized;
                   2755:     if (-e $img_path) {
                   2756:         $ima->Read($img_path);
                   2757:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   2758:             my $width = $ima->Get('width');
                   2759:             my $height = $ima->Get('height');
                   2760:             if ($width > $resizewidth) {
                   2761: 	        my $factor = $width/$resizewidth;
                   2762:                 my $newheight = $height/$factor;
                   2763:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   2764:                 $resized = 1;
                   2765:             }
                   2766:         }
                   2767:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   2768:             my $width = $ima->Get('width');
                   2769:             my $height = $ima->Get('height');
                   2770:             if ($height > $resizeheight) {
                   2771:                 my $factor = $height/$resizeheight;
                   2772:                 my $newwidth = $width/$factor;
                   2773:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   2774:                 $resized = 1;
                   2775:             }
                   2776:         }
                   2777:         if ($resized) {
                   2778:             $ima->Write($img_path);
                   2779:         }
                   2780:     }
                   2781:     return;
1.977     amueller 2782: }
                   2783: 
1.608     albertel 2784: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 2785: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  2786: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  2787: #        $context - possible values: coursedoc, existingfile, overwrite, 
                   2788: #                                    canceloverwrite, or ''. 
                   2789: #                   if 'coursedoc': upload to the current course
                   2790: #                   if 'existingfile': write file to tmp/overwrites directory 
                   2791: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   2792: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 2793: #        $subdir - directory in userfile to store the file into
1.858     raeburn  2794: #        $parser - instruction to parse file for objects ($parser = parse)    
                   2795: #        $allfiles - reference to hash for embedded objects
                   2796: #        $codebase - reference to hash for codebase of java objects
                   2797: #        $desuname - username for permanent storage of uploaded file
                   2798: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  2799: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   2800: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  2801: #        $resizewidth - width (pixels) to which to resize uploaded image
                   2802: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  2803: #        $mimetype - reference to scalar to accommodate mime type determined
1.1152    raeburn  2804: #                    from File::MMagic.
1.858     raeburn  2805: # 
1.686     albertel 2806: # output: url of file in userspace, or error: <message> 
                   2807: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 2808: 
1.531     albertel 2809: sub userfileupload {
1.1090    raeburn  2810:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  2811:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 2812:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 2813:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 2814:     $fname=&clean_filename($fname);
1.1090    raeburn  2815:     # See if there is anything left
1.257     www      2816:     unless ($fname) { return 'error: no uploaded file'; }
1.1090    raeburn  2817:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   2818:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   2819:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   2820:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  2821:         my $now = time;
1.1090    raeburn  2822:         my $filepath;
1.1095    raeburn  2823:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  2824:              $filepath = 'tmp/helprequests/'.$now;
                   2825:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   2826:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   2827:                          '_'.$env{'user.domain'}.'/pending';
                   2828:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   2829:             my ($docuname,$docudom);
                   2830:             if ($destudom) {
                   2831:                 $docudom = $destudom;
                   2832:             } else {
                   2833:                 $docudom = $env{'user.domain'};
                   2834:             }
                   2835:             if ($destuname) {
                   2836:                 $docuname = $destuname;
                   2837:             } else {
                   2838:                 $docuname = $env{'user.name'};
                   2839:             }
                   2840:             if (exists($env{'form.group'})) {
                   2841:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2842:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2843:             }
                   2844:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   2845:             if ($context eq 'canceloverwrite') {
                   2846:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   2847:                 if (-e  $tempfile) {
                   2848:                     my @info = stat($tempfile);
                   2849:                     if ($info[9] eq $env{'form.timestamp'}) {
                   2850:                         unlink($tempfile);
                   2851:                     }
                   2852:                 }
                   2853:                 return;
1.523     raeburn  2854:             }
                   2855:         }
1.1090    raeburn  2856:         # Create the directory if not present
1.741     raeburn  2857:         my @parts=split(/\//,$filepath);
                   2858:         my $fullpath = $perlvar{'lonDaemons'};
                   2859:         for (my $i=0;$i<@parts;$i++) {
                   2860:             $fullpath .= '/'.$parts[$i];
                   2861:             if ((-e $fullpath)!=1) {
                   2862:                 mkdir($fullpath,0777);
                   2863:             }
                   2864:         }
                   2865:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   2866:         print $fh $env{'form.'.$formname};
                   2867:         close($fh);
1.1090    raeburn  2868:         if ($context eq 'existingfile') {
                   2869:             my @info = stat($fullpath.'/'.$fname);
                   2870:             return ($fullpath.'/'.$fname,$info[9]);
                   2871:         } else {
                   2872:             return $fullpath.'/'.$fname;
                   2873:         }
1.523     raeburn  2874:     }
1.995     raeburn  2875:     if ($subdir eq 'scantron') {
                   2876:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  2877:     } else {
1.995     raeburn  2878:         $fname="$subdir/$fname";
                   2879:     }
1.1090    raeburn  2880:     if ($context eq 'coursedoc') {
1.638     albertel 2881: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2882: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  2883:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 2884:             return &finishuserfileupload($docuname,$docudom,
                   2885: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  2886: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  2887:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  2888:         } else {
1.620     albertel 2889:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 2890:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   2891: 				       $fname,$formname,$parser,
1.1095    raeburn  2892: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  2893:         }
1.719     banghart 2894:     } elsif (defined($destuname)) {
                   2895:         my $docuname=$destuname;
                   2896:         my $docudom=$destudom;
1.860     raeburn  2897: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2898: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  2899:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  2900:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      2901:     } else {
1.638     albertel 2902:         my $docuname=$env{'user.name'};
                   2903:         my $docudom=$env{'user.domain'};
1.714     raeburn  2904:         if (exists($env{'form.group'})) {
                   2905:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2906:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2907:         }
1.860     raeburn  2908: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2909: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  2910:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  2911:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      2912:     }
1.271     www      2913: }
                   2914: 
                   2915: sub finishuserfileupload {
1.860     raeburn  2916:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  2917:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  2918:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      2919:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 2920:   
1.860     raeburn  2921:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 2922:     $file=$fname;
                   2923:     if ($fname=~m|/|) {
                   2924:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   2925: 	$path.=$fnamepath.'/';
                   2926:     }
1.259     www      2927:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      2928:     my $count;
                   2929:     for ($count=4;$count<=$#parts;$count++) {
                   2930:         $filepath.="/$parts[$count]";
                   2931:         if ((-e $filepath)!=1) {
                   2932: 	    mkdir($filepath,0777);
                   2933:         }
                   2934:     }
1.984     neumanie 2935: 
1.258     www      2936: # Save the file
                   2937:     {
1.701     albertel 2938: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   2939: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   2940: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   2941: 	    return '/adm/notfound.html';
                   2942: 	}
1.1090    raeburn  2943:         if ($context eq 'overwrite') {
1.1117    foxr     2944:             my $source =  LONCAPA::tempdir().'/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
1.1090    raeburn  2945:             my $target = $filepath.'/'.$file;
                   2946:             if (-e $source) {
                   2947:                 my @info = stat($source);
                   2948:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   2949:                     unless (&File::Copy::move($source,$target)) {
                   2950:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   2951:                         return "Moving from $source failed";
                   2952:                     }
                   2953:                 } else {
                   2954:                     return "Temporary file: $source had unexpected date/time for last modification";
                   2955:                 }
                   2956:             } else {
                   2957:                 return "Temporary file: $source missing";
                   2958:             }
                   2959:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 2960: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   2961: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   2962: 	    return '/adm/notfound.html';
                   2963: 	}
1.570     albertel 2964: 	close(FH);
1.1051    raeburn  2965:         if ($resizewidth && $resizeheight) {
                   2966:             my $mm = new File::MMagic;
                   2967:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   2968:             if ($mime_type =~ m{^image/}) {
                   2969: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   2970:             }  
1.977     amueller 2971: 	}
1.258     www      2972:     }
1.1152    raeburn  2973:     if (($context eq 'coursedoc') || ($parser eq 'parse')) {
                   2974:         if (ref($mimetype)) {
                   2975:             if ($$mimetype eq '') {
                   2976:                 my $mm = new File::MMagic;
                   2977:                 my $type = $mm->checktype_filename($filepath.'/'.$file);
                   2978:                 $$mimetype = $type;
                   2979:             }
                   2980:         }
                   2981:     }
1.637     raeburn  2982:     if ($parser eq 'parse') {
1.1152    raeburn  2983:         if ((ref($mimetype)) && ($$mimetype eq 'text/html')) {
1.1024    raeburn  2984:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   2985:                                                        $allfiles,$codebase);
                   2986:             unless ($parse_result eq 'ok') {
                   2987:                 &logthis('Failed to parse '.$filepath.$file.
                   2988: 	   	         ' for embedded media: '.$parse_result); 
                   2989:             }
1.637     raeburn  2990:         }
                   2991:     }
1.860     raeburn  2992:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   2993:         my $input = $filepath.'/'.$file;
                   2994:         my $output = $filepath.'/'.'tn-'.$file;
                   2995:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   2996:         system("convert -sample $thumbsize $input $output");
                   2997:         if (-e $filepath.'/'.'tn-'.$file) {
                   2998:             $fetchthumb  = 1; 
                   2999:         }
                   3000:     }
1.858     raeburn  3001:  
1.259     www      3002: # Notify homeserver to grep it
                   3003: #
1.984     neumanie 3004:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 3005:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      3006:     if ($fetchresult eq 'ok') {
1.860     raeburn  3007:         if ($fetchthumb) {
                   3008:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   3009:             if ($thumbresult ne 'ok') {
                   3010:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   3011:                          $docuhome.': '.$thumbresult);
                   3012:             }
                   3013:         }
1.259     www      3014: #
1.258     www      3015: # Return the URL to it
1.494     albertel 3016:         return '/uploaded/'.$path.$file;
1.263     www      3017:     } else {
1.494     albertel 3018:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   3019: 		 ': '.$fetchresult);
1.263     www      3020:         return '/adm/notfound.html';
1.858     raeburn  3021:     }
1.493     albertel 3022: }
                   3023: 
1.637     raeburn  3024: sub extract_embedded_items {
1.961     raeburn  3025:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  3026:     my @state = ();
                   3027:     my %javafiles = (
                   3028:                       codebase => '',
                   3029:                       code => '',
                   3030:                       archive => ''
                   3031:                     );
                   3032:     my %mediafiles = (
                   3033:                       src => '',
                   3034:                       movie => '',
                   3035:                      );
1.648     raeburn  3036:     my $p;
                   3037:     if ($content) {
                   3038:         $p = HTML::LCParser->new($content);
                   3039:     } else {
1.961     raeburn  3040:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  3041:     }
1.641     albertel 3042:     while (my $t=$p->get_token()) {
1.640     albertel 3043: 	if ($t->[0] eq 'S') {
                   3044: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 3045: 	    push(@state, $tagname);
1.648     raeburn  3046:             if (lc($tagname) eq 'allow') {
                   3047:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   3048:             }
1.640     albertel 3049: 	    if (lc($tagname) eq 'img') {
                   3050: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   3051: 	    }
1.886     albertel 3052: 	    if (lc($tagname) eq 'a') {
                   3053: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   3054: 	    }
1.645     raeburn  3055:             if (lc($tagname) eq 'script') {
                   3056:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   3057:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   3058:                 } else {
                   3059:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   3060:                 }
                   3061:             }
                   3062:             if (lc($tagname) eq 'link') {
                   3063:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   3064:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   3065:                 }
                   3066:             }
1.640     albertel 3067: 	    if (lc($tagname) eq 'object' ||
                   3068: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   3069: 		foreach my $item (keys(%javafiles)) {
                   3070: 		    $javafiles{$item} = '';
                   3071: 		}
                   3072: 	    }
                   3073: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   3074: 		my $name = lc($attr->{'name'});
                   3075: 		foreach my $item (keys(%javafiles)) {
                   3076: 		    if ($name eq $item) {
                   3077: 			$javafiles{$item} = $attr->{'value'};
                   3078: 			last;
                   3079: 		    }
                   3080: 		}
                   3081: 		foreach my $item (keys(%mediafiles)) {
                   3082: 		    if ($name eq $item) {
                   3083: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   3084: 			last;
                   3085: 		    }
                   3086: 		}
                   3087: 	    }
                   3088: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   3089: 		foreach my $item (keys(%javafiles)) {
                   3090: 		    if ($attr->{$item}) {
                   3091: 			$javafiles{$item} = $attr->{$item};
                   3092: 			last;
                   3093: 		    }
                   3094: 		}
                   3095: 		foreach my $item (keys(%mediafiles)) {
                   3096: 		    if ($attr->{$item}) {
                   3097: 			&add_filetype($allfiles,$attr->{$item},$item);
                   3098: 			last;
                   3099: 		    }
                   3100: 		}
                   3101: 	    }
                   3102: 	} elsif ($t->[0] eq 'E') {
                   3103: 	    my ($tagname) = ($t->[1]);
                   3104: 	    if ($javafiles{'codebase'} ne '') {
                   3105: 		$javafiles{'codebase'} .= '/';
                   3106: 	    }  
                   3107: 	    if (lc($tagname) eq 'applet' ||
                   3108: 		lc($tagname) eq 'object' ||
                   3109: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   3110: 		) {
                   3111: 		foreach my $item (keys(%javafiles)) {
                   3112: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   3113: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   3114: 			&add_filetype($allfiles,$file,$item);
                   3115: 		    }
                   3116: 		}
                   3117: 	    } 
                   3118: 	    pop @state;
                   3119: 	}
                   3120:     }
1.637     raeburn  3121:     return 'ok';
                   3122: }
                   3123: 
1.639     albertel 3124: sub add_filetype {
                   3125:     my ($allfiles,$file,$type)=@_;
                   3126:     if (exists($allfiles->{$file})) {
                   3127: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   3128: 	    push(@{$allfiles->{$file}}, &escape($type));
                   3129: 	}
                   3130:     } else {
                   3131: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  3132:     }
                   3133: }
                   3134: 
1.493     albertel 3135: sub removeuploadedurl {
1.984     neumanie 3136:     my ($url)=@_;	
                   3137:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 3138:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 3139: }
                   3140: 
                   3141: sub removeuserfile {
                   3142:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 3143:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  3144:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 3145:     if ($result eq 'ok') {	
1.798     raeburn  3146:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   3147:             my $metafile = $fname.'.meta';
                   3148:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 3149: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 3150:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  3151:             my $sqlresult = 
1.823     albertel 3152:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  3153:                                         'portfolio_metadata',$group,
                   3154:                                         'delete');
1.798     raeburn  3155:         }
                   3156:     }
                   3157:     return $result;
1.257     www      3158: }
1.15      www      3159: 
1.530     albertel 3160: sub mkdiruserfile {
                   3161:     my ($docuname,$docudom,$dir)=@_;
                   3162:     my $home=&homeserver($docuname,$docudom);
                   3163:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   3164: }
                   3165: 
1.531     albertel 3166: sub renameuserfile {
                   3167:     my ($docuname,$docudom,$old,$new)=@_;
                   3168:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  3169:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   3170:                         &escape("$old").':'.&escape("$new"),$home);
                   3171:     if ($result eq 'ok') {
                   3172:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   3173:             my $oldmeta = $old.'.meta';
                   3174:             my $newmeta = $new.'.meta';
                   3175:             my $metaresult = 
                   3176:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 3177: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   3178:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  3179:             my $sqlresult = 
1.823     albertel 3180:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  3181:                                         'portfolio_metadata',$group,
                   3182:                                         'delete');
1.798     raeburn  3183:         }
                   3184:     }
                   3185:     return $result;
1.531     albertel 3186: }
                   3187: 
1.14      www      3188: # ------------------------------------------------------------------------- Log
                   3189: 
                   3190: sub log {
                   3191:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      3192:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      3193: }
                   3194: 
                   3195: # ------------------------------------------------------------------ Course Log
1.352     www      3196: #
                   3197: # This routine flushes several buffers of non-mission-critical nature
                   3198: #
1.157     www      3199: 
                   3200: sub flushcourselogs {
1.352     www      3201:     &logthis('Flushing log buffers');
                   3202: #
                   3203: # course logs
                   3204: # This is a log of all transactions in a course, which can be used
                   3205: # for data mining purposes
                   3206: #
                   3207: # It also collects the courseid database, which lists last transaction
                   3208: # times and course titles for all courseids
                   3209: #
                   3210:     my %courseidbuffer=();
1.921     raeburn  3211:     foreach my $crsid (keys(%courselogs)) {
1.352     www      3212:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      3213: 		          &escape($courselogs{$crsid}),
                   3214: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      3215: 	    delete $courselogs{$crsid};
                   3216:         } else {
                   3217:             &logthis('Failed to flush log buffer for '.$crsid);
                   3218:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 3219:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      3220:                         " exceeded maximum size, deleting.</font>");
                   3221:                delete $courselogs{$crsid};
                   3222:             }
1.352     www      3223:         }
1.920     raeburn  3224:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  3225:             'description' => $coursedescrbuf{$crsid},
                   3226:             'inst_code'    => $courseinstcodebuf{$crsid},
                   3227:             'type'        => $coursetypebuf{$crsid},
                   3228:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  3229:         };
1.191     harris41 3230:     }
1.352     www      3231: #
                   3232: # Write course id database (reverse lookup) to homeserver of courses 
                   3233: # Is used in pickcourse
                   3234: #
1.840     albertel 3235:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  3236:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  3237:                                     $courseidbuffer{$crs_home},
                   3238:                                     $crs_home,'timeonly');
1.352     www      3239:     }
                   3240: #
                   3241: # File accesses
                   3242: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   3243: #
1.449     matthew  3244:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  3245:         if ($entry =~ /___count$/) {
                   3246:             my ($dom,$name);
1.807     albertel 3247:             ($dom,$name,undef)=
1.811     albertel 3248: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  3249:             if (! defined($dom) || $dom eq '' || 
                   3250:                 ! defined($name) || $name eq '') {
1.620     albertel 3251:                 my $cid = $env{'request.course.id'};
                   3252:                 $dom  = $env{'request.'.$cid.'.domain'};
                   3253:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  3254:             }
1.450     matthew  3255:             my $value = $accesshash{$entry};
                   3256:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   3257:             my %temphash=($url => $value);
1.449     matthew  3258:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   3259:             if ($result eq 'ok') {
                   3260:                 delete $accesshash{$entry};
                   3261:             } elsif ($result eq 'unknown_cmd') {
                   3262:                 # Target server has old code running on it.
1.450     matthew  3263:                 my %temphash=($entry => $value);
1.449     matthew  3264:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   3265:                     delete $accesshash{$entry};
                   3266:                 }
                   3267:             }
                   3268:         } else {
1.811     albertel 3269:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  3270:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  3271:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   3272:                 delete $accesshash{$entry};
                   3273:             }
1.185     www      3274:         }
1.191     harris41 3275:     }
1.352     www      3276: #
                   3277: # Roles
                   3278: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   3279: #
1.800     albertel 3280:     foreach my $entry (keys(%userrolehash)) {
1.351     www      3281:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      3282: 	    split(/\:/,$entry);
                   3283:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      3284:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      3285:                 $rudom,$runame) eq 'ok') {
                   3286: 	    delete $userrolehash{$entry};
                   3287:         }
                   3288:     }
1.662     raeburn  3289: #
                   3290: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   3291: #
                   3292:     my %domrolebuffer = ();
1.1000    raeburn  3293:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 3294:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  3295:         if ($domrolebuffer{$rudom}) {
                   3296:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   3297:                       '='.&escape($domainrolehash{$entry});
                   3298:         } else {
                   3299:             $domrolebuffer{$rudom}.=&escape($entry).
                   3300:                       '='.&escape($domainrolehash{$entry});
                   3301:         }
                   3302:         delete $domainrolehash{$entry};
                   3303:     }
                   3304:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 3305: 	my %servers = &get_servers($dom,'library');
                   3306: 	foreach my $tryserver (keys(%servers)) {
                   3307: 	    unless (&reply('domroleput:'.$dom.':'.
                   3308: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   3309: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   3310: 	    }
1.662     raeburn  3311:         }
                   3312:     }
1.186     www      3313:     $dumpcount++;
1.157     www      3314: }
                   3315: 
                   3316: sub courselog {
                   3317:     my $what=shift;
1.158     www      3318:     $what=time.':'.$what;
1.620     albertel 3319:     unless ($env{'request.course.id'}) { return ''; }
                   3320:     $coursedombuf{$env{'request.course.id'}}=
                   3321:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3322:     $coursenumbuf{$env{'request.course.id'}}=
                   3323:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   3324:     $coursehombuf{$env{'request.course.id'}}=
                   3325:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   3326:     $coursedescrbuf{$env{'request.course.id'}}=
                   3327:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   3328:     $courseinstcodebuf{$env{'request.course.id'}}=
                   3329:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   3330:     $courseownerbuf{$env{'request.course.id'}}=
                   3331:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  3332:     $coursetypebuf{$env{'request.course.id'}}=
                   3333:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 3334:     if (defined $courselogs{$env{'request.course.id'}}) {
                   3335: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      3336:     } else {
1.620     albertel 3337: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      3338:     }
1.620     albertel 3339:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      3340: 	&flushcourselogs();
                   3341:     }
1.158     www      3342: }
                   3343: 
                   3344: sub courseacclog {
                   3345:     my $fnsymb=shift;
1.620     albertel 3346:     unless ($env{'request.course.id'}) { return ''; }
                   3347:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.1144    www      3348:     if ($fnsymb=~/$LONCAPA::assess_re/) {
1.187     www      3349:         $what.=':POST';
1.583     matthew  3350:         # FIXME: Probably ought to escape things....
1.800     albertel 3351: 	foreach my $key (keys(%env)) {
                   3352:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  3353:                 my $formitem = $1;
                   3354:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   3355:                     $what.=':'.$formitem.'='.$env{$key};
                   3356:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
                   3357:                     $what.=':'.$formitem.'='.$env{$key};
                   3358:                 }
1.158     www      3359:             }
1.191     harris41 3360:         }
1.583     matthew  3361:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   3362:         # FIXME: We should not be depending on a form parameter that someone
                   3363:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 3364:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  3365:             $what.= ':POST';
                   3366:             # FIXME: Probably ought to escape things....
                   3367:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   3368:                                  'crsdiscuss') {
1.620     albertel 3369:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  3370:             }
                   3371:         }
1.158     www      3372:     }
                   3373:     &courselog($what);
1.149     www      3374: }
                   3375: 
1.185     www      3376: sub countacc {
                   3377:     my $url=&declutter(shift);
1.458     matthew  3378:     return if (! defined($url) || $url eq '');
1.620     albertel 3379:     unless ($env{'request.course.id'}) { return ''; }
                   3380:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      3381:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  3382:     $accesshash{$key}++;
1.185     www      3383: }
1.349     www      3384: 
1.361     www      3385: sub linklog {
                   3386:     my ($from,$to)=@_;
                   3387:     $from=&declutter($from);
                   3388:     $to=&declutter($to);
                   3389:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   3390:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   3391: }
                   3392:   
1.349     www      3393: sub userrolelog {
                   3394:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  3395:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  3396:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  3397:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041    raeburn  3398:         ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350     www      3399:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3400:        $userrolehash
                   3401:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      3402:                     =$tend.':'.$tstart;
1.662     raeburn  3403:     }
1.898     albertel 3404:     if (($env{'request.role'} =~ /dc\./) &&
                   3405: 	(($trole=~/^au/) || ($trole=~/^in/) ||
                   3406: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041    raeburn  3407: 	 ($trole=~/^cr/) || ($trole=~/^ta/) ||
                   3408:          ($trole=~/^co/))) {
1.898     albertel 3409:        $userrolehash
                   3410:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   3411:                     =$tend.':'.$tstart;
                   3412:     }
1.662     raeburn  3413:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   3414:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   3415:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   3416:         ($trole=~/^sc/)) {
                   3417:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3418:        $domainrolehash
                   3419:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   3420:                     = $tend.':'.$tstart;
                   3421:     }
1.351     www      3422: }
                   3423: 
1.957     raeburn  3424: sub courserolelog {
                   3425:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
                   3426:     if (($trole eq 'cc') || ($trole eq 'in') ||
                   3427:         ($trole eq 'ep') || ($trole eq 'ad') ||
                   3428:         ($trole eq 'ta') || ($trole eq 'st') ||
1.1044    raeburn  3429:         ($trole=~/^cr/) || ($trole eq 'gr') ||
                   3430:         ($trole eq 'co')) {
1.957     raeburn  3431:         if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   3432:             my $cdom = $1;
                   3433:             my $cnum = $2;
                   3434:             my $sec = $3;
                   3435:             my $namespace = 'rolelog';
                   3436:             my %storehash = (
                   3437:                                role    => $trole,
                   3438:                                start   => $tstart,
                   3439:                                end     => $tend,
                   3440:                                selfenroll => $selfenroll,
                   3441:                                context    => $context,
                   3442:                             );
                   3443:             if ($trole eq 'gr') {
                   3444:                 $namespace = 'groupslog';
                   3445:                 $storehash{'group'} = $sec;
                   3446:             } else {
                   3447:                 $storehash{'section'} = $sec;
                   3448:             }
                   3449:             &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996     raeburn  3450:             if (($trole ne 'st') || ($sec ne '')) {
                   3451:                 &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
                   3452:             }
1.957     raeburn  3453:         }
                   3454:     }
                   3455:     return;
                   3456: }
                   3457: 
1.351     www      3458: sub get_course_adv_roles {
1.948     raeburn  3459:     my ($cid,$codes) = @_;
1.620     albertel 3460:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      3461:     my %coursehash=&coursedescription($cid);
1.988     raeburn  3462:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      3463:     my %nothide=();
1.800     albertel 3464:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  3465:         if ($user !~ /:/) {
                   3466: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   3467:         } else {
                   3468:             $nothide{$user}=1;
                   3469:         }
1.470     www      3470:     }
1.351     www      3471:     my %returnhash=();
                   3472:     my %dumphash=
                   3473:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   3474:     my $now=time;
1.997     raeburn  3475:     my %privileged;
1.1000    raeburn  3476:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 3477: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      3478:         if (($tstart) && ($tstart<0)) { next; }
                   3479:         if (($tend) && ($tend<$now)) { next; }
                   3480:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 3481:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 3482: 	if ($username eq '' || $domain eq '') { next; }
1.997     raeburn  3483:         unless (ref($privileged{$domain}) eq 'HASH') {
                   3484:             my %dompersonnel =
1.998     raeburn  3485:                 &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997     raeburn  3486:             $privileged{$domain} = {};
                   3487:             foreach my $server (keys(%dompersonnel)) {
1.999     raeburn  3488:                 if (ref($dompersonnel{$server}) eq 'HASH') {
1.997     raeburn  3489:                     foreach my $user (keys(%{$dompersonnel{$server}})) {
                   3490:                         my ($trole,$uname,$udom) = split(/:/,$user);
                   3491:                         $privileged{$udom}{$uname} = 1;
                   3492:                     }
                   3493:                 }
                   3494:             }
                   3495:         }
                   3496:         if ((exists($privileged{$domain}{$username})) && 
                   3497:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 3498: 	if ($role eq 'cr') { next; }
1.948     raeburn  3499:         if ($codes) {
                   3500:             if ($section) { $role .= ':'.$section; }
                   3501:             if ($returnhash{$role}) {
                   3502:                 $returnhash{$role}.=','.$username.':'.$domain;
                   3503:             } else {
                   3504:                 $returnhash{$role}=$username.':'.$domain;
                   3505:             }
1.351     www      3506:         } else {
1.988     raeburn  3507:             my $key=&plaintext($role,$crstype);
1.973     bisitz   3508:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  3509:             if ($returnhash{$key}) {
                   3510: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   3511:             } else {
                   3512:                 $returnhash{$key}=$username.':'.$domain;
                   3513:             }
1.351     www      3514:         }
1.948     raeburn  3515:     }
1.400     www      3516:     return %returnhash;
                   3517: }
                   3518: 
                   3519: sub get_my_roles {
1.937     raeburn  3520:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 3521:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   3522:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  3523:     my (%dumphash,%nothide);
1.1086    raeburn  3524:     if ($context eq 'userroles') {
                   3525:         my $extra = &freeze_escape({'skipcheck' => 1});
                   3526:         %dumphash = &dump('roles',$udom,$uname,'.',undef,$extra);
1.858     raeburn  3527:     } else {
                   3528:         %dumphash=
1.400     www      3529:             &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  3530:         if ($hidepriv) {
                   3531:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   3532:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3533:                 if ($user !~ /:/) {
                   3534:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   3535:                 } else {
                   3536:                     $nothide{$user} = 1;
                   3537:                 }
                   3538:             }
                   3539:         }
1.858     raeburn  3540:     }
1.400     www      3541:     my %returnhash=();
                   3542:     my $now=time;
1.999     raeburn  3543:     my %privileged;
1.800     albertel 3544:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  3545:         my ($role,$tend,$tstart);
                   3546:         if ($context eq 'userroles') {
1.1149    raeburn  3547:             next if ($entry =~ /^rolesdef/);
1.867     raeburn  3548: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   3549:         } else {
                   3550:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   3551:         }
1.400     www      3552:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  3553:         my $status = 'active';
1.939     raeburn  3554:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  3555:             $status = 'previous';
                   3556:         } 
                   3557:         if (($tstart) && ($now<$tstart)) {
                   3558:             $status = 'future';
                   3559:         }
                   3560:         if (ref($types) eq 'ARRAY') {
                   3561:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   3562:                 next;
                   3563:             } 
                   3564:         } else {
                   3565:             if ($status ne 'active') {
                   3566:                 next;
                   3567:             }
                   3568:         }
1.867     raeburn  3569:         my ($rolecode,$username,$domain,$section,$area);
                   3570:         if ($context eq 'userroles') {
                   3571:             ($area,$rolecode) = split(/_/,$entry);
                   3572:             (undef,$domain,$username,$section) = split(/\//,$area);
                   3573:         } else {
                   3574:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   3575:         }
1.832     raeburn  3576:         if (ref($roledoms) eq 'ARRAY') {
                   3577:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   3578:                 next;
                   3579:             }
                   3580:         }
                   3581:         if (ref($roles) eq 'ARRAY') {
                   3582:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  3583:                 if ($role =~ /^cr\//) {
                   3584:                     if (!grep(/^cr$/,@{$roles})) {
                   3585:                         next;
                   3586:                     }
1.1104    raeburn  3587:                 } elsif ($role =~ /^gr\//) {
                   3588:                     if (!grep(/^gr$/,@{$roles})) {
                   3589:                         next;
                   3590:                     }
1.922     raeburn  3591:                 } else {
                   3592:                     next;
                   3593:                 }
1.832     raeburn  3594:             }
1.867     raeburn  3595:         }
1.937     raeburn  3596:         if ($hidepriv) {
1.999     raeburn  3597:             if ($context eq 'userroles') {
                   3598:                 if ((&privileged($username,$domain)) &&
                   3599:                     (!$nothide{$username.':'.$domain})) {
                   3600:                     next;
                   3601:                 }
                   3602:             } else {
                   3603:                 unless (ref($privileged{$domain}) eq 'HASH') {
                   3604:                     my %dompersonnel =
                   3605:                         &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
                   3606:                     $privileged{$domain} = {};
                   3607:                     if (keys(%dompersonnel)) {
                   3608:                         foreach my $server (keys(%dompersonnel)) {
                   3609:                             if (ref($dompersonnel{$server}) eq 'HASH') {
                   3610:                                 foreach my $user (keys(%{$dompersonnel{$server}})) {
                   3611:                                     my ($trole,$uname,$udom) = split(/:/,$user);
                   3612:                                     $privileged{$udom}{$uname} = $trole;
                   3613:                                 }
                   3614:                             }
                   3615:                         }
                   3616:                     }
                   3617:                 }
                   3618:                 if (exists($privileged{$domain}{$username})) {
                   3619:                     if (!$nothide{$username.':'.$domain}) {
                   3620:                         next;
                   3621:                     }
                   3622:                 }
1.937     raeburn  3623:             }
                   3624:         }
1.933     raeburn  3625:         if ($withsec) {
                   3626:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   3627:                 $tstart.':'.$tend;
                   3628:         } else {
                   3629:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   3630:         }
1.832     raeburn  3631:     }
1.373     www      3632:     return %returnhash;
1.399     www      3633: }
                   3634: 
                   3635: # ----------------------------------------------------- Frontpage Announcements
                   3636: #
                   3637: #
                   3638: 
                   3639: sub postannounce {
                   3640:     my ($server,$text)=@_;
1.844     albertel 3641:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      3642:     unless ($text=~/\w/) { $text=''; }
                   3643:     return &reply('setannounce:'.&escape($text),$server);
                   3644: }
                   3645: 
                   3646: sub getannounce {
1.448     albertel 3647: 
                   3648:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      3649: 	my $announcement='';
1.800     albertel 3650: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 3651: 	close($fh);
1.399     www      3652: 	if ($announcement=~/\w/) { 
                   3653: 	    return 
                   3654:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 3655:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      3656: 	} else {
                   3657: 	    return '';
                   3658: 	}
                   3659:     } else {
                   3660: 	return '';
                   3661:     }
1.351     www      3662: }
1.353     www      3663: 
                   3664: # ---------------------------------------------------------- Course ID routines
                   3665: # Deal with domain's nohist_courseid.db files
                   3666: #
                   3667: 
                   3668: sub courseidput {
1.921     raeburn  3669:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  3670:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  3671:     my $outcome;
                   3672:     if ($caller eq 'timeonly') {
                   3673:         my $cids = '';
                   3674:         foreach my $item (keys(%$storehash)) {
                   3675:             $cids.=&escape($item).'&';
                   3676:         }
                   3677:         $cids=~s/\&$//;
                   3678:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   3679:                           $coursehome);       
                   3680:     } else {
                   3681:         my $items = '';
                   3682:         foreach my $item (keys(%$storehash)) {
                   3683:             $items.= &escape($item).'='.
                   3684:                      &freeze_escape($$storehash{$item}).'&';
                   3685:         }
                   3686:         $items=~s/\&$//;
                   3687:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   3688:                           $coursehome);
1.918     raeburn  3689:     }
                   3690:     if ($outcome eq 'unknown_cmd') {
                   3691:         my $what;
                   3692:         foreach my $cid (keys(%$storehash)) {
                   3693:             $what .= &escape($cid).'=';
1.921     raeburn  3694:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  3695:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  3696:             }
                   3697:             $what =~ s/\:$/&/;
                   3698:         }
                   3699:         $what =~ s/\&$//;  
                   3700:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   3701:     } else {
                   3702:         return $outcome;
                   3703:     }
1.353     www      3704: }
                   3705: 
                   3706: sub courseiddump {
1.921     raeburn  3707:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  3708:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  3709:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1071    raeburn  3710:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918     raeburn  3711:     my $as_hash = 1;
                   3712:     my %returnhash;
                   3713:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 3714:     my %libserv = &all_library();
                   3715:     foreach my $tryserver (keys(%libserv)) {
                   3716:         if ( (  $hostidflag == 1 
                   3717: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   3718: 	     || (!defined($hostidflag)) ) {
                   3719: 
1.918     raeburn  3720: 	    if (($domfilter eq '') ||
                   3721: 		(&host_domain($tryserver) eq $domfilter)) {
                   3722:                 my $rep = 
                   3723:                   &reply('courseiddump:'.&host_domain($tryserver).':'.
                   3724:                          $sincefilter.':'.&escape($descfilter).':'.
                   3725:                          &escape($instcodefilter).':'.&escape($ownerfilter).
                   3726:                          ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947     raeburn  3727:                          ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962     raeburn  3728:                          &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008    raeburn  3729:                          $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029    raeburn  3730:                          &escape($cc_clone).':'.$cloneonly.':'.
                   3731:                          &escape($createdbefore).':'.&escape($createdafter).':'.
1.1071    raeburn  3732:                          &escape($creationcontext).':'.$domcloner,
                   3733:                          $tryserver);
1.918     raeburn  3734:                 my @pairs=split(/\&/,$rep);
                   3735:                 foreach my $item (@pairs) {
                   3736:                     my ($key,$value)=split(/\=/,$item,2);
                   3737:                     $key = &unescape($key);
                   3738:                     next if ($key =~ /^error: 2 /);
                   3739:                     my $result = &thaw_unescape($value);
                   3740:                     if (ref($result) eq 'HASH') {
                   3741:                         $returnhash{$key}=$result;
                   3742:                     } else {
1.921     raeburn  3743:                         my @responses = split(/:/,$value);
                   3744:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  3745:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  3746:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  3747:                         }
1.1008    raeburn  3748:                     }
1.353     www      3749:                 }
                   3750:             }
                   3751:         }
                   3752:     }
                   3753:     return %returnhash;
                   3754: }
                   3755: 
1.1055    raeburn  3756: sub courselastaccess {
                   3757:     my ($cdom,$cnum,$hostidref) = @_;
                   3758:     my %returnhash;
                   3759:     if ($cdom && $cnum) {
                   3760:         my $chome = &homeserver($cnum,$cdom);
                   3761:         if ($chome ne 'no_host') {
                   3762:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   3763:             &extract_lastaccess(\%returnhash,$rep);
                   3764:         }
                   3765:     } else {
                   3766:         if (!$cdom) { $cdom=''; }
                   3767:         my %libserv = &all_library();
                   3768:         foreach my $tryserver (keys(%libserv)) {
                   3769:             if (ref($hostidref) eq 'ARRAY') {
                   3770:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   3771:             } 
                   3772:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   3773:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   3774:                 &extract_lastaccess(\%returnhash,$rep);
                   3775:             }
                   3776:         }
                   3777:     }
                   3778:     return %returnhash;
                   3779: }
                   3780: 
                   3781: sub extract_lastaccess {
                   3782:     my ($returnhash,$rep) = @_;
                   3783:     if (ref($returnhash) eq 'HASH') {
                   3784:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   3785:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   3786:                  $rep eq '') {
                   3787:             my @pairs=split(/\&/,$rep);
                   3788:             foreach my $item (@pairs) {
                   3789:                 my ($key,$value)=split(/\=/,$item,2);
                   3790:                 $key = &unescape($key);
                   3791:                 next if ($key =~ /^error: 2 /);
                   3792:                 $returnhash->{$key} = &thaw_unescape($value);
                   3793:             }
                   3794:         }
                   3795:     }
                   3796:     return;
                   3797: }
                   3798: 
1.658     raeburn  3799: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  3800: 
                   3801: sub dcmailput {
1.685     raeburn  3802:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  3803:     my $status = &Apache::lonnet::critical(
1.740     www      3804:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   3805:        &escape($message),$server);
1.662     raeburn  3806:     return $status;
                   3807: }
                   3808: 
1.658     raeburn  3809: sub dcmaildump {
                   3810:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  3811:     my %returnhash=();
1.846     albertel 3812: 
                   3813:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  3814:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   3815:                                                          &escape($enddate).':';
                   3816: 	my @esc_senders=map { &escape($_)} @$senders;
                   3817: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 3818: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 3819:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  3820:             if (($key) && ($value)) {
                   3821:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  3822:             }
                   3823:         }
                   3824:     }
                   3825:     return %returnhash;
                   3826: }
1.662     raeburn  3827: # ---------------------------------------------------------- Domain roles
                   3828: 
                   3829: sub get_domain_roles {
                   3830:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  3831:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  3832:         $startdate = '.';
                   3833:     }
1.1018    raeburn  3834:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  3835:         $enddate = '.';
                   3836:     }
1.922     raeburn  3837:     my $rolelist;
                   3838:     if (ref($roles) eq 'ARRAY') {
                   3839:         $rolelist = join(':',@{$roles});
                   3840:     }
1.662     raeburn  3841:     my %personnel = ();
1.841     albertel 3842: 
                   3843:     my %servers = &get_servers($dom,'library');
                   3844:     foreach my $tryserver (keys(%servers)) {
                   3845: 	%{$personnel{$tryserver}}=();
                   3846: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   3847: 					    &escape($startdate).':'.
                   3848: 					    &escape($enddate).':'.
                   3849: 					    &escape($rolelist), $tryserver))) {
                   3850: 	    my ($key,$value) = split(/\=/,$line,2);
                   3851: 	    if (($key) && ($value)) {
                   3852: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   3853: 	    }
                   3854: 	}
1.662     raeburn  3855:     }
                   3856:     return %personnel;
                   3857: }
1.658     raeburn  3858: 
1.1057    www      3859: # ----------------------------------------------------------- Interval timing 
1.149     www      3860: 
1.1153  ! www      3861: {
        !          3862: # Caches needed for speedup of navmaps
        !          3863: # We don't want to cache this for very long at all (5 seconds at most)
        !          3864: # 
        !          3865: # The user for whom we cache
        !          3866: my $cachedkey='';
        !          3867: # The cached times for this user
        !          3868: my %cachedtimes=();
        !          3869: # When this was last done
        !          3870: my $cachedtime=();
        !          3871: 
        !          3872: sub load_all_first_access {
        !          3873:    my ($uname,$udom)=@_;
        !          3874:    if (($cachedkey eq $uname.':'.$udom) && (abs($cachedtime-time)<5)) { return; };
        !          3875:    $cachedtime=time;
        !          3876:    $cachedkey=$uname.':'.$udom;
        !          3877:    %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
        !          3878: }
        !          3879: 
1.504     albertel 3880: sub get_first_access {
                   3881:     my ($type,$argsymb)=@_;
1.790     albertel 3882:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3883:     if ($argsymb) { $symb=$argsymb; }
                   3884:     my ($map,$id,$res)=&decode_symb($symb);
1.926     albertel 3885:     if ($type eq 'course') {
                   3886: 	$res='course';
                   3887:     } elsif ($type eq 'map') {
1.588     albertel 3888: 	$res=&symbread($map);
                   3889:     } else {
                   3890: 	$res=$symb;
                   3891:     }
1.1153  ! www      3892:     &load_all_first_access($uname,$udom);
        !          3893:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 3894: }
                   3895: 
                   3896: sub set_first_access {
                   3897:     my ($type)=@_;
1.790     albertel 3898:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3899:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 3900:     if ($type eq 'course') {
                   3901: 	$res='course';
                   3902:     } elsif ($type eq 'map') {
1.588     albertel 3903: 	$res=&symbread($map);
                   3904:     } else {
                   3905: 	$res=$symb;
                   3906:     }
1.1153  ! www      3907:     $cachedkey='';
1.588     albertel 3908:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 3909:     if (!$firstaccess) {
1.588     albertel 3910: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 3911:     }
                   3912:     return 'already_set';
1.504     albertel 3913: }
1.1153  ! www      3914: }
1.110     www      3915: # --------------------------------------------- Set Expire Date for Spreadsheet
                   3916: 
                   3917: sub expirespread {
                   3918:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 3919:     my $cid=$env{'request.course.id'}; 
1.110     www      3920:     if ($cid) {
                   3921:        my $now=time;
                   3922:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 3923:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   3924:                             $env{'course.'.$cid.'.num'}.
1.110     www      3925: 	        	    ':nohist_expirationdates:'.
                   3926:                             &escape($key).'='.$now,
1.620     albertel 3927:                             $env{'course.'.$cid.'.home'})
1.110     www      3928:     }
                   3929:     return 'ok';
1.14      www      3930: }
                   3931: 
1.109     www      3932: # ----------------------------------------------------- Devalidate Spreadsheets
                   3933: 
                   3934: sub devalidate {
1.325     www      3935:     my ($symb,$uname,$udom)=@_;
1.620     albertel 3936:     my $cid=$env{'request.course.id'}; 
1.109     www      3937:     if ($cid) {
1.391     matthew  3938:         # delete the stored spreadsheets for
                   3939:         # - the student level sheet of this user in course's homespace
                   3940:         # - the assessment level sheet for this resource 
                   3941:         #   for this user in user's homespace
1.553     albertel 3942: 	# - current conditional state info
1.325     www      3943: 	my $key=$uname.':'.$udom.':';
1.109     www      3944:         my $status=
1.299     matthew  3945: 	    &del('nohist_calculatedsheets',
1.391     matthew  3946: 		 [$key.'studentcalc:'],
1.620     albertel 3947: 		 $env{'course.'.$cid.'.domain'},
                   3948: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 3949: 		.' '.
                   3950: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  3951: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      3952:         unless ($status eq 'ok ok') {
                   3953:            &logthis('Could not devalidate spreadsheet '.
1.325     www      3954:                     $uname.' at '.$udom.' for '.
1.109     www      3955: 		    $symb.': '.$status);
1.133     albertel 3956:         }
1.553     albertel 3957: 	&delenv('user.state.'.$cid);
1.109     www      3958:     }
                   3959: }
                   3960: 
1.265     albertel 3961: sub get_scalar {
                   3962:     my ($string,$end) = @_;
                   3963:     my $value;
                   3964:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   3965: 	$value = $1;
                   3966:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   3967: 	$value = $1;
                   3968:     }
                   3969:     return &unescape($value);
                   3970: }
                   3971: 
                   3972: sub array2str {
                   3973:   my (@array) = @_;
                   3974:   my $result=&arrayref2str(\@array);
                   3975:   $result=~s/^__ARRAY_REF__//;
                   3976:   $result=~s/__END_ARRAY_REF__$//;
                   3977:   return $result;
                   3978: }
                   3979: 
1.204     albertel 3980: sub arrayref2str {
                   3981:   my ($arrayref) = @_;
1.265     albertel 3982:   my $result='__ARRAY_REF__';
1.204     albertel 3983:   foreach my $elem (@$arrayref) {
1.265     albertel 3984:     if(ref($elem) eq 'ARRAY') {
                   3985:       $result.=&arrayref2str($elem).'&';
                   3986:     } elsif(ref($elem) eq 'HASH') {
                   3987:       $result.=&hashref2str($elem).'&';
                   3988:     } elsif(ref($elem)) {
                   3989:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 3990:     } else {
                   3991:       $result.=&escape($elem).'&';
                   3992:     }
                   3993:   }
                   3994:   $result=~s/\&$//;
1.265     albertel 3995:   $result .= '__END_ARRAY_REF__';
1.204     albertel 3996:   return $result;
                   3997: }
                   3998: 
1.168     albertel 3999: sub hash2str {
1.204     albertel 4000:   my (%hash) = @_;
                   4001:   my $result=&hashref2str(\%hash);
1.265     albertel 4002:   $result=~s/^__HASH_REF__//;
                   4003:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 4004:   return $result;
                   4005: }
                   4006: 
                   4007: sub hashref2str {
                   4008:   my ($hashref)=@_;
1.265     albertel 4009:   my $result='__HASH_REF__';
1.800     albertel 4010:   foreach my $key (sort(keys(%$hashref))) {
                   4011:     if (ref($key) eq 'ARRAY') {
                   4012:       $result.=&arrayref2str($key).'=';
                   4013:     } elsif (ref($key) eq 'HASH') {
                   4014:       $result.=&hashref2str($key).'=';
                   4015:     } elsif (ref($key)) {
1.265     albertel 4016:       $result.='=';
1.800     albertel 4017:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 4018:     } else {
1.1132    raeburn  4019: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 4020:     }
                   4021: 
1.800     albertel 4022:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   4023:       $result.=&arrayref2str($hashref->{$key}).'&';
                   4024:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   4025:       $result.=&hashref2str($hashref->{$key}).'&';
                   4026:     } elsif(ref($hashref->{$key})) {
1.265     albertel 4027:        $result.='&';
1.800     albertel 4028:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 4029:     } else {
1.800     albertel 4030:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 4031:     }
                   4032:   }
1.168     albertel 4033:   $result=~s/\&$//;
1.265     albertel 4034:   $result .= '__END_HASH_REF__';
1.168     albertel 4035:   return $result;
                   4036: }
                   4037: 
                   4038: sub str2hash {
1.265     albertel 4039:     my ($string)=@_;
                   4040:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   4041:     return %$hash;
                   4042: }
                   4043: 
                   4044: sub str2hashref {
1.168     albertel 4045:   my ($string) = @_;
1.265     albertel 4046: 
                   4047:   my %hash;
                   4048: 
                   4049:   if($string !~ /^__HASH_REF__/) {
                   4050:       if (! ($string eq '' || !defined($string))) {
                   4051: 	  $hash{'error'}='Not hash reference';
                   4052:       }
                   4053:       return (\%hash, $string);
                   4054:   }
                   4055: 
                   4056:   $string =~ s/^__HASH_REF__//;
                   4057: 
                   4058:   while($string !~ /^__END_HASH_REF__/) {
                   4059:       #key
                   4060:       my $key='';
                   4061:       if($string =~ /^__HASH_REF__/) {
                   4062:           ($key, $string)=&str2hashref($string);
                   4063:           if(defined($key->{'error'})) {
                   4064:               $hash{'error'}='Bad data';
                   4065:               return (\%hash, $string);
                   4066:           }
                   4067:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4068:           ($key, $string)=&str2arrayref($string);
                   4069:           if($key->[0] eq 'Array reference error') {
                   4070:               $hash{'error'}='Bad data';
                   4071:               return (\%hash, $string);
                   4072:           }
                   4073:       } else {
                   4074:           $string =~ s/^(.*?)=//;
1.267     albertel 4075: 	  $key=&unescape($1);
1.265     albertel 4076:       }
                   4077:       $string =~ s/^=//;
                   4078: 
                   4079:       #value
                   4080:       my $value='';
                   4081:       if($string =~ /^__HASH_REF__/) {
                   4082:           ($value, $string)=&str2hashref($string);
                   4083:           if(defined($value->{'error'})) {
                   4084:               $hash{'error'}='Bad data';
                   4085:               return (\%hash, $string);
                   4086:           }
                   4087:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4088:           ($value, $string)=&str2arrayref($string);
                   4089:           if($value->[0] eq 'Array reference error') {
                   4090:               $hash{'error'}='Bad data';
                   4091:               return (\%hash, $string);
                   4092:           }
                   4093:       } else {
                   4094: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   4095:       }
                   4096:       $string =~ s/^&//;
                   4097: 
                   4098:       $hash{$key}=$value;
1.204     albertel 4099:   }
1.265     albertel 4100: 
                   4101:   $string =~ s/^__END_HASH_REF__//;
                   4102: 
                   4103:   return (\%hash, $string);
1.204     albertel 4104: }
                   4105: 
                   4106: sub str2array {
1.265     albertel 4107:     my ($string)=@_;
                   4108:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   4109:     return @$array;
                   4110: }
                   4111: 
                   4112: sub str2arrayref {
1.204     albertel 4113:   my ($string) = @_;
1.265     albertel 4114:   my @array;
                   4115: 
                   4116:   if($string !~ /^__ARRAY_REF__/) {
                   4117:       if (! ($string eq '' || !defined($string))) {
                   4118: 	  $array[0]='Array reference error';
                   4119:       }
                   4120:       return (\@array, $string);
                   4121:   }
                   4122: 
                   4123:   $string =~ s/^__ARRAY_REF__//;
                   4124: 
                   4125:   while($string !~ /^__END_ARRAY_REF__/) {
                   4126:       my $value='';
                   4127:       if($string =~ /^__HASH_REF__/) {
                   4128:           ($value, $string)=&str2hashref($string);
                   4129:           if(defined($value->{'error'})) {
                   4130:               $array[0] ='Array reference error';
                   4131:               return (\@array, $string);
                   4132:           }
                   4133:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4134:           ($value, $string)=&str2arrayref($string);
                   4135:           if($value->[0] eq 'Array reference error') {
                   4136:               $array[0] ='Array reference error';
                   4137:               return (\@array, $string);
                   4138:           }
                   4139:       } else {
                   4140: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   4141:       }
                   4142:       $string =~ s/^&//;
                   4143: 
                   4144:       push(@array, $value);
1.191     harris41 4145:   }
1.265     albertel 4146: 
                   4147:   $string =~ s/^__END_ARRAY_REF__//;
                   4148: 
                   4149:   return (\@array, $string);
1.168     albertel 4150: }
                   4151: 
1.167     albertel 4152: # -------------------------------------------------------------------Temp Store
                   4153: 
1.168     albertel 4154: sub tmpreset {
                   4155:   my ($symb,$namespace,$domain,$stuname) = @_;
                   4156:   if (!$symb) {
                   4157:     $symb=&symbread();
1.620     albertel 4158:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4159:   }
                   4160:   $symb=escape($symb);
                   4161: 
1.620     albertel 4162:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 4163:   $namespace=~s/\//\_/g;
                   4164:   $namespace=~s/\W//g;
                   4165: 
1.620     albertel 4166:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4167:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4168:   if ($domain eq 'public' && $stuname eq 'public') {
                   4169:       $stuname=$ENV{'REMOTE_ADDR'};
                   4170:   }
1.1117    foxr     4171:   my $path=LONCAPA::tempdir();
1.168     albertel 4172:   my %hash;
                   4173:   if (tie(%hash,'GDBM_File',
                   4174: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4175: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  4176:     foreach my $key (keys(%hash)) {
1.180     albertel 4177:       if ($key=~ /:$symb/) {
1.168     albertel 4178: 	delete($hash{$key});
                   4179:       }
                   4180:     }
                   4181:   }
                   4182: }
                   4183: 
1.167     albertel 4184: sub tmpstore {
1.168     albertel 4185:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4186: 
                   4187:   if (!$symb) {
                   4188:     $symb=&symbread();
1.620     albertel 4189:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4190:   }
                   4191:   $symb=escape($symb);
                   4192: 
                   4193:   if (!$namespace) {
                   4194:     # I don't think we would ever want to store this for a course.
                   4195:     # it seems this will only be used if we don't have a course.
1.620     albertel 4196:     #$namespace=$env{'request.course.id'};
1.168     albertel 4197:     #if (!$namespace) {
1.620     albertel 4198:       $namespace=$env{'request.state'};
1.168     albertel 4199:     #}
                   4200:   }
                   4201:   $namespace=~s/\//\_/g;
                   4202:   $namespace=~s/\W//g;
1.620     albertel 4203:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4204:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4205:   if ($domain eq 'public' && $stuname eq 'public') {
                   4206:       $stuname=$ENV{'REMOTE_ADDR'};
                   4207:   }
1.168     albertel 4208:   my $now=time;
                   4209:   my %hash;
1.1117    foxr     4210:   my $path=LONCAPA::tempdir();
1.168     albertel 4211:   if (tie(%hash,'GDBM_File',
                   4212: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4213: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 4214:     $hash{"version:$symb"}++;
                   4215:     my $version=$hash{"version:$symb"};
                   4216:     my $allkeys=''; 
                   4217:     foreach my $key (keys(%$storehash)) {
                   4218:       $allkeys.=$key.':';
1.591     albertel 4219:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 4220:     }
                   4221:     $hash{"$version:$symb:timestamp"}=$now;
                   4222:     $allkeys.='timestamp';
                   4223:     $hash{"$version:keys:$symb"}=$allkeys;
                   4224:     if (untie(%hash)) {
                   4225:       return 'ok';
                   4226:     } else {
                   4227:       return "error:$!";
                   4228:     }
                   4229:   } else {
                   4230:     return "error:$!";
                   4231:   }
                   4232: }
1.167     albertel 4233: 
1.168     albertel 4234: # -----------------------------------------------------------------Temp Restore
1.167     albertel 4235: 
1.168     albertel 4236: sub tmprestore {
                   4237:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 4238: 
1.168     albertel 4239:   if (!$symb) {
                   4240:     $symb=&symbread();
1.620     albertel 4241:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4242:   }
                   4243:   $symb=escape($symb);
                   4244: 
1.620     albertel 4245:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 4246: 
1.620     albertel 4247:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4248:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4249:   if ($domain eq 'public' && $stuname eq 'public') {
                   4250:       $stuname=$ENV{'REMOTE_ADDR'};
                   4251:   }
1.168     albertel 4252:   my %returnhash;
                   4253:   $namespace=~s/\//\_/g;
                   4254:   $namespace=~s/\W//g;
                   4255:   my %hash;
1.1117    foxr     4256:   my $path=LONCAPA::tempdir();
1.168     albertel 4257:   if (tie(%hash,'GDBM_File',
                   4258: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4259: 	  &GDBM_READER(),0640)) {
1.168     albertel 4260:     my $version=$hash{"version:$symb"};
                   4261:     $returnhash{'version'}=$version;
                   4262:     my $scope;
                   4263:     for ($scope=1;$scope<=$version;$scope++) {
                   4264:       my $vkeys=$hash{"$scope:keys:$symb"};
                   4265:       my @keys=split(/:/,$vkeys);
                   4266:       my $key;
                   4267:       $returnhash{"$scope:keys"}=$vkeys;
                   4268:       foreach $key (@keys) {
1.591     albertel 4269: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   4270: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 4271:       }
                   4272:     }
1.168     albertel 4273:     if (!(untie(%hash))) {
                   4274:       return "error:$!";
                   4275:     }
                   4276:   } else {
                   4277:     return "error:$!";
                   4278:   }
                   4279:   return %returnhash;
1.167     albertel 4280: }
                   4281: 
1.9       www      4282: # ----------------------------------------------------------------------- Store
                   4283: 
                   4284: sub store {
1.124     www      4285:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4286:     my $home='';
                   4287: 
1.168     albertel 4288:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4289: 
1.213     www      4290:     $symb=&symbclean($symb);
1.122     albertel 4291:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4292: 
1.620     albertel 4293:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4294:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4295: 
                   4296:     &devalidate($symb,$stuname,$domain);
1.109     www      4297: 
                   4298:     $symb=escape($symb);
1.187     www      4299:     if (!$namespace) { 
1.620     albertel 4300:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4301:           return ''; 
                   4302:        } 
                   4303:     }
1.620     albertel 4304:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4305: 
                   4306:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4307:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   4308: 
1.12      www      4309:     my $namevalue='';
1.800     albertel 4310:     foreach my $key (keys(%$storehash)) {
                   4311:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4312:     }
1.12      www      4313:     $namevalue=~s/\&$//;
1.187     www      4314:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      4315:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      4316: }
                   4317: 
1.47      www      4318: # -------------------------------------------------------------- Critical Store
                   4319: 
                   4320: sub cstore {
1.124     www      4321:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4322:     my $home='';
                   4323: 
1.168     albertel 4324:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4325: 
1.213     www      4326:     $symb=&symbclean($symb);
1.122     albertel 4327:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4328: 
1.620     albertel 4329:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4330:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4331: 
                   4332:     &devalidate($symb,$stuname,$domain);
1.109     www      4333: 
                   4334:     $symb=escape($symb);
1.187     www      4335:     if (!$namespace) { 
1.620     albertel 4336:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4337:           return ''; 
                   4338:        } 
                   4339:     }
1.620     albertel 4340:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4341: 
                   4342:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4343:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 4344: 
1.47      www      4345:     my $namevalue='';
1.800     albertel 4346:     foreach my $key (keys(%$storehash)) {
                   4347:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4348:     }
1.47      www      4349:     $namevalue=~s/\&$//;
1.187     www      4350:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      4351:     return critical
                   4352:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      4353: }
                   4354: 
1.9       www      4355: # --------------------------------------------------------------------- Restore
                   4356: 
                   4357: sub restore {
1.124     www      4358:     my ($symb,$namespace,$domain,$stuname) = @_;
                   4359:     my $home='';
                   4360: 
1.168     albertel 4361:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4362: 
1.122     albertel 4363:     if (!$symb) {
                   4364:       unless ($symb=escape(&symbread())) { return ''; }
                   4365:     } else {
1.213     www      4366:       $symb=&escape(&symbclean($symb));
1.122     albertel 4367:     }
1.188     www      4368:     if (!$namespace) { 
1.620     albertel 4369:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      4370:           return ''; 
                   4371:        } 
                   4372:     }
1.620     albertel 4373:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4374:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   4375:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 4376:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   4377: 
1.12      www      4378:     my %returnhash=();
1.800     albertel 4379:     foreach my $line (split(/\&/,$answer)) {
                   4380: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 4381:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 4382:     }
1.75      www      4383:     my $version;
                   4384:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 4385:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   4386:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 4387:        }
1.75      www      4388:     }
1.13      www      4389:     return %returnhash;
1.34      www      4390: }
                   4391: 
                   4392: # ---------------------------------------------------------- Course Description
1.1118    foxr     4393: #
                   4394: #  
1.34      www      4395: 
                   4396: sub coursedescription {
1.731     albertel 4397:     my ($courseid,$args)=@_;
1.34      www      4398:     $courseid=~s/^\///;
1.49      www      4399:     $courseid=~s/\_/\//g;
1.34      www      4400:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 4401:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 4402:     my $normalid=$cdomain.'_'.$cnum;
                   4403:     # need to always cache even if we get errors otherwise we keep 
                   4404:     # trying and trying and trying to get the course description.
                   4405:     my %envhash=();
                   4406:     my %returnhash=();
1.731     albertel 4407:     
                   4408:     my $expiretime=600;
                   4409:     if ($env{'request.course.id'} eq $normalid) {
                   4410: 	$expiretime=120;
                   4411:     }
                   4412: 
                   4413:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   4414:     if (!$args->{'freshen_cache'}
                   4415: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   4416: 	foreach my $key (keys(%env)) {
                   4417: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   4418: 	    my ($setting) = $1;
                   4419: 	    $returnhash{$setting} = $env{$key};
                   4420: 	}
                   4421: 	return %returnhash;
                   4422:     }
                   4423: 
1.1118    foxr     4424:     # get the data again
                   4425: 
1.731     albertel 4426:     if (!$args->{'one_time'}) {
                   4427: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   4428:     }
1.811     albertel 4429: 
1.34      www      4430:     if ($chome ne 'no_host') {
1.302     albertel 4431:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 4432:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     4433: 	   my $username = $env{'user.name'}; # Defult username
                   4434: 	   if(defined $args->{'user'}) {
                   4435: 	       $username = $args->{'user'};
                   4436: 	   }
1.129     albertel 4437:            $returnhash{'home'}= $chome;
                   4438: 	   $returnhash{'domain'} = $cdomain;
                   4439: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  4440:            if (!defined($returnhash{'type'})) {
                   4441:                $returnhash{'type'} = 'Course';
                   4442:            }
1.130     albertel 4443:            while (my ($name,$value) = each %returnhash) {
1.53      www      4444:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 4445:            }
1.270     www      4446:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     4447:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     4448: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      4449:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   4450:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   4451:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      4452:        }
                   4453:     }
1.731     albertel 4454:     if (!$args->{'one_time'}) {
1.949     raeburn  4455: 	&appenv(\%envhash);
1.731     albertel 4456:     }
1.302     albertel 4457:     return %returnhash;
1.461     www      4458: }
                   4459: 
1.1080    raeburn  4460: sub update_released_required {
                   4461:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   4462:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   4463:         $cid = $env{'request.course.id'};
                   4464:         $cdom = $env{'course.'.$cid.'.domain'};
                   4465:         $cnum = $env{'course.'.$cid.'.num'};
                   4466:         $chome = $env{'course.'.$cid.'.home'};
                   4467:     }
                   4468:     if ($needsrelease) {
                   4469:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   4470:         my $needsupdate;
                   4471:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   4472:             $needsupdate = 1;
                   4473:         } else {
                   4474:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   4475:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   4476:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   4477:                 $needsupdate = 1;
                   4478:             }
                   4479:         }
                   4480:         if ($needsupdate) {
                   4481:             my %needshash = (
                   4482:                              'internal.releaserequired' => $needsrelease,
                   4483:                             );
                   4484:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   4485:             if ($putresult eq 'ok') {
                   4486:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   4487:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   4488:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   4489:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   4490:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   4491:                 }
                   4492:             }
                   4493:         }
                   4494:     }
                   4495:     return;
                   4496: }
                   4497: 
1.461     www      4498: # -------------------------------------------------See if a user is privileged
                   4499: 
                   4500: sub privileged {
                   4501:     my ($username,$domain)=@_;
                   4502:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   4503: 			&homeserver($username,$domain));
1.1033    raeburn  4504:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
                   4505:         ($rolesdump =~ /^error:/)) {
                   4506:         return 0;
                   4507:     }
1.461     www      4508:     my $now=time;
                   4509:     if ($rolesdump ne '') {
1.800     albertel 4510:         foreach my $entry (split(/&/,$rolesdump)) {
                   4511: 	    if ($entry!~/^rolesdef_/) {
                   4512: 		my ($area,$role)=split(/=/,$entry);
1.461     www      4513: 		$area=~s/\_\w\w$//;
                   4514: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   4515: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   4516: 		    my $active=1;
                   4517: 		    if ($tend) {
                   4518: 			if ($tend<$now) { $active=0; }
                   4519: 		    }
                   4520: 		    if ($tstart) {
                   4521: 			if ($tstart>$now) { $active=0; }
                   4522: 		    }
                   4523: 		    if ($active) { return 1; }
                   4524: 		}
                   4525: 	    }
                   4526: 	}
                   4527:     }
                   4528:     return 0;
1.9       www      4529: }
1.1       albertel 4530: 
1.103     harris41 4531: # -------------------------------------------------------- Get user privileges
1.11      www      4532: 
                   4533: sub rolesinit {
                   4534:     my ($domain,$username,$authhost)=@_;
1.1034    raeburn  4535:     my $now=time;
                   4536:     my %userroles = ('user.login.time' => $now);
1.1086    raeburn  4537:     my $extra = &freeze_escape({'skipcheck' => 1});
1.1078    raeburn  4538:     my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033    raeburn  4539:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
1.1078    raeburn  4540:         ($rolesdump =~ /^error:/)) {
1.1033    raeburn  4541:         return \%userroles;
                   4542:     }
1.11      www      4543:     my %allroles=();
1.678     raeburn  4544:     my %allgroups=();   
1.11      www      4545: 
                   4546:     if ($rolesdump ne '') {
1.800     albertel 4547:         foreach my $entry (split(/&/,$rolesdump)) {
                   4548: 	  if ($entry!~/^rolesdef_/) {
                   4549:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 4550: 	    $area=~s/\_\w\w$//;
1.678     raeburn  4551:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 4552: 	    if ($role=~/^cr/) { 
1.807     albertel 4553: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   4554: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 4555: 		    ($tend,$tstart)=split('_',$trest);
                   4556: 		} else {
                   4557: 		    $trole=$role;
                   4558: 		}
1.678     raeburn  4559:             } elsif ($role =~ m|^gr/|) {
                   4560:                 ($trole,$tend,$tstart) = split(/_/,$role);
1.1104    raeburn  4561:                 next if ($tstart eq '-1');
1.678     raeburn  4562:                 ($trole,$group_privs) = split(/\//,$trole);
                   4563:                 $group_privs = &unescape($group_privs);
1.587     albertel 4564: 	    } else {
                   4565: 		($trole,$tend,$tstart)=split(/_/,$role);
                   4566: 	    }
1.743     albertel 4567: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   4568: 					 $username);
                   4569: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  4570:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   4571:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      4572:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 4573: 		my $spec=$trole.'.'.$area;
                   4574: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   4575: 		if ($trole =~ /^cr\//) {
1.567     raeburn  4576:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  4577:                 } elsif ($trole eq 'gr') {
                   4578:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 4579: 		} else {
1.567     raeburn  4580:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 4581: 		}
1.12      www      4582:             }
1.662     raeburn  4583:           }
1.191     harris41 4584:         }
1.743     albertel 4585:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   4586:         $userroles{'user.adv'}    = $adv;
                   4587: 	$userroles{'user.author'} = $author;
1.620     albertel 4588:         $env{'user.adv'}=$adv;
1.11      www      4589:     }
1.743     albertel 4590:     return \%userroles;  
1.11      www      4591: }
                   4592: 
1.567     raeburn  4593: sub set_arearole {
                   4594:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   4595: # log the associated role with the area
                   4596:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 4597:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  4598: }
                   4599: 
                   4600: sub custom_roleprivs {
                   4601:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   4602:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   4603:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 4604:     if (&hostname($homsvr) ne '') {
1.567     raeburn  4605:         my ($rdummy,$roledef)=
                   4606:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   4607:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   4608:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   4609:             if (defined($syspriv)) {
1.1043    raeburn  4610:                 if ($trest =~ /^$match_community$/) {
                   4611:                     $syspriv =~ s/bre\&S//; 
                   4612:                 }
1.567     raeburn  4613:                 $$allroles{'cm./'}.=':'.$syspriv;
                   4614:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   4615:             }
                   4616:             if ($tdomain ne '') {
                   4617:                 if (defined($dompriv)) {
                   4618:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   4619:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   4620:                 }
                   4621:                 if (($trest ne '') && (defined($coursepriv))) {
                   4622:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   4623:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   4624:                 }
                   4625:             }
                   4626:         }
                   4627:     }
                   4628: }
                   4629: 
1.678     raeburn  4630: sub group_roleprivs {
                   4631:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   4632:     my $access = 1;
                   4633:     my $now = time;
                   4634:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   4635:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   4636:     if ($access) {
1.811     albertel 4637:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  4638:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   4639:     }
                   4640: }
1.567     raeburn  4641: 
                   4642: sub standard_roleprivs {
                   4643:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   4644:     if (defined($pr{$trole.':s'})) {
                   4645:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   4646:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   4647:     }
                   4648:     if ($tdomain ne '') {
                   4649:         if (defined($pr{$trole.':d'})) {
                   4650:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4651:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4652:         }
                   4653:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   4654:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   4655:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   4656:         }
                   4657:     }
                   4658: }
                   4659: 
                   4660: sub set_userprivs {
1.1064    raeburn  4661:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  4662:     my $author=0;
                   4663:     my $adv=0;
1.678     raeburn  4664:     my %grouproles = ();
                   4665:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  4666:         my @groupkeys; 
1.1000    raeburn  4667:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  4668:             push(@groupkeys,$role);
                   4669:         }
                   4670:         if (ref($groups_roles) eq 'HASH') {
                   4671:             foreach my $key (keys(%{$groups_roles})) {
                   4672:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   4673:                     push(@groupkeys,$key);
                   4674:                 }
                   4675:             }
                   4676:         }
                   4677:         if (@groupkeys > 0) {
                   4678:             foreach my $role (@groupkeys) {
                   4679:                 my ($trole,$area,$sec,$extendedarea);
                   4680:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   4681:                     $trole = $1;
                   4682:                     $area = $2;
                   4683:                     $sec = $3;
                   4684:                     $extendedarea = $area.$sec;
                   4685:                     if (exists($$allgroups{$area})) {
                   4686:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   4687:                             my $spec = $trole.'.'.$extendedarea;
                   4688:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  4689:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  4690:                         }
1.678     raeburn  4691:                     }
                   4692:                 }
                   4693:             }
                   4694:         }
                   4695:     }
1.800     albertel 4696:     foreach my $group (keys(%grouproles)) {
                   4697:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  4698:     }
1.800     albertel 4699:     foreach my $role (keys(%{$allroles})) {
                   4700:         my %thesepriv;
1.941     raeburn  4701:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 4702:         foreach my $item (split(/:/,$$allroles{$role})) {
                   4703:             if ($item ne '') {
                   4704:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  4705:                 if ($restrictions eq '') {
                   4706:                     $thesepriv{$privilege}='F';
                   4707:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   4708:                     $thesepriv{$privilege}.=$restrictions;
                   4709:                 }
                   4710:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   4711:             }
                   4712:         }
                   4713:         my $thesestr='';
1.1104    raeburn  4714:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 4715: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   4716: 	}
                   4717:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  4718:     }
                   4719:     return ($author,$adv);
                   4720: }
                   4721: 
1.994     raeburn  4722: sub role_status {
1.1104    raeburn  4723:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  4724:     my @pwhere = ();
                   4725:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
                   4726:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                   4727:         unless (!defined($$role) || $$role eq '') {
                   4728:             $$where=join('.',@pwhere);
                   4729:             $$trolecode=$$role.'.'.$$where;
                   4730:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   4731:             $$tstatus='is';
1.1104    raeburn  4732:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  4733:                 $$tstatus='future';
1.1034    raeburn  4734:                 if ($$tstart<$now) {
                   4735:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  4736:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  4737:                             my (%allroles,%allgroups,$group_privs,
                   4738:                                 %groups_roles,@rolecodes);
1.1002    raeburn  4739:                             my %userroles = (
                   4740:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   4741:                             );
1.1064    raeburn  4742:                             @rolecodes = ('cm'); 
1.1002    raeburn  4743:                             my $spec=$$role.'.'.$$where;
                   4744:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   4745:                             if ($$role =~ /^cr\//) {
                   4746:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  4747:                                 push(@rolecodes,'cr');
1.1002    raeburn  4748:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  4749:                                 push(@rolecodes,$$role);
1.1002    raeburn  4750:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   4751:                                                     $env{'user.name'});
1.1064    raeburn  4752:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  4753:                                 (undef,my $group_privs) = split(/\//,$trole);
                   4754:                                 $group_privs = &unescape($group_privs);
                   4755:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  4756:                                 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  4757:                                 &get_groups_roles($tdomain,$trest,
                   4758:                                                   \%course_roles,\@rolecodes,
                   4759:                                                   \%groups_roles);
1.1002    raeburn  4760:                             } else {
1.1064    raeburn  4761:                                 push(@rolecodes,$$role);
1.1002    raeburn  4762:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   4763:                             }
1.1064    raeburn  4764:                             my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   4765:                             &appenv(\%userroles,\@rolecodes);
1.1002    raeburn  4766:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
                   4767:                         }
                   4768:                     }
1.1034    raeburn  4769:                     $$tstatus = 'is';
1.1002    raeburn  4770:                 }
1.994     raeburn  4771:             }
                   4772:             if ($$tend) {
1.1104    raeburn  4773:                 if ($$tend<$update) {
1.994     raeburn  4774:                     $$tstatus='expired';
                   4775:                 } elsif ($$tend<$now) {
                   4776:                     $$tstatus='will_not';
                   4777:                 }
                   4778:             }
                   4779:         }
                   4780:     }
                   4781: }
                   4782: 
1.1104    raeburn  4783: sub get_groups_roles {
                   4784:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   4785:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   4786:                   (ref($rolecodes) eq 'ARRAY') && 
                   4787:                   (ref($groups_roles) eq 'HASH')); 
                   4788:     if (keys(%{$cdom_courseroles}) > 0) {
                   4789:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   4790:         if ($cdom ne '' && $cnum ne '') {
                   4791:             foreach my $key (keys(%{$cdom_courseroles})) {
                   4792:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   4793:                     my $crsrole = $1;
                   4794:                     my $crssec = $2;
                   4795:                     if ($crsrole =~ /^cr/) {
                   4796:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   4797:                             push(@{$rolecodes},'cr');
                   4798:                         }
                   4799:                     } else {
                   4800:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   4801:                             push(@{$rolecodes},$crsrole);
                   4802:                         }
                   4803:                     }
                   4804:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   4805:                     if ($crssec ne '') {
                   4806:                         $rolekey .= "/$crssec";
                   4807:                     }
                   4808:                     $rolekey .= './';
                   4809:                     $groups_roles->{$rolekey} = $rolecodes;
                   4810:                 }
                   4811:             }
                   4812:         }
                   4813:     }
                   4814:     return;
                   4815: }
                   4816: 
                   4817: sub delete_env_groupprivs {
                   4818:     my ($where,$courseroles,$possroles) = @_;
                   4819:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   4820:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   4821:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   4822:         %{$courseroles->{$udom}} =
                   4823:             &get_my_roles('','','userroles',['active'],
                   4824:                           $possroles,[$udom],1);
                   4825:     }
                   4826:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   4827:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   4828:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   4829:             my $area = '/'.$cdom.'/'.$cnum;
                   4830:             my $privkey = "user.priv.$crsrole.$area";
                   4831:             if ($crssec ne '') {
                   4832:                 $privkey .= '/'.$crssec;
                   4833:             }
                   4834:             $privkey .= ".$area/$group";
                   4835:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   4836:         }
                   4837:     }
                   4838:     return;
                   4839: }
                   4840: 
1.994     raeburn  4841: sub check_adhoc_privs {
1.1104    raeburn  4842:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller) = @_;
1.994     raeburn  4843:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
                   4844:     if ($env{$cckey}) {
                   4845:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  4846:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  4847:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1088    raeburn  4848:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4849:         }
                   4850:     } else {
1.1088    raeburn  4851:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4852:     }
                   4853: }
                   4854: 
                   4855: sub set_adhoc_privileges {
                   4856: # role can be cc or ca
1.1088    raeburn  4857:     my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994     raeburn  4858:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   4859:     my $spec = $role.'.'.$area;
                   4860:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
                   4861:                                   $env{'user.name'});
                   4862:     my %ccrole = ();
                   4863:     &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
                   4864:     my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
                   4865:     &appenv(\%userroles,[$role,'cm']);
                   4866:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1088    raeburn  4867:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
                   4868:         &appenv( {'request.role'        => $spec,
                   4869:                   'request.role.domain' => $dcdom,
                   4870:                   'request.course.sec'  => ''
                   4871:                  }
                   4872:                );
                   4873:         my $tadv=0;
                   4874:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   4875:         &appenv({'request.role.adv'    => $tadv});
                   4876:     }
1.994     raeburn  4877: }
                   4878: 
1.12      www      4879: # --------------------------------------------------------------- get interface
                   4880: 
                   4881: sub get {
1.131     albertel 4882:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      4883:    my $items='';
1.800     albertel 4884:    foreach my $item (@$storearr) {
                   4885:        $items.=&escape($item).'&';
1.191     harris41 4886:    }
1.12      www      4887:    $items=~s/\&$//;
1.620     albertel 4888:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4889:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 4890:    my $uhome=&homeserver($uname,$udomain);
                   4891: 
1.133     albertel 4892:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4893:    my @pairs=split(/\&/,$rep);
1.273     albertel 4894:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   4895:      return @pairs;
                   4896:    }
1.15      www      4897:    my %returnhash=();
1.42      www      4898:    my $i=0;
1.800     albertel 4899:    foreach my $item (@$storearr) {
                   4900:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      4901:       $i++;
1.191     harris41 4902:    }
1.15      www      4903:    return %returnhash;
1.27      www      4904: }
                   4905: 
                   4906: # --------------------------------------------------------------- del interface
                   4907: 
                   4908: sub del {
1.133     albertel 4909:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      4910:    my $items='';
1.800     albertel 4911:    foreach my $item (@$storearr) {
                   4912:        $items.=&escape($item).'&';
1.191     harris41 4913:    }
1.984     neumanie 4914: 
1.27      www      4915:    $items=~s/\&$//;
1.620     albertel 4916:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4917:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 4918:    my $uhome=&homeserver($uname,$udomain);
                   4919:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4920: }
                   4921: 
                   4922: # -------------------------------------------------------------- dump interface
                   4923: 
                   4924: sub dump {
1.1086    raeburn  4925:     my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755     albertel 4926:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4927:     if (!$uname) { $uname=$env{'user.name'}; }
                   4928:     my $uhome=&homeserver($uname,$udomain);
                   4929:     if ($regexp) {
                   4930: 	$regexp=&escape($regexp);
                   4931:     } else {
                   4932: 	$regexp='.';
                   4933:     }
1.1086    raeburn  4934:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755     albertel 4935:     my @pairs=split(/\&/,$rep);
                   4936:     my %returnhash=();
1.1098    foxr     4937:     if (!($rep =~ /^error/ )) {
                   4938: 	foreach my $item (@pairs) {
                   4939: 	    my ($key,$value)=split(/=/,$item,2);
                   4940: 	    $key = &unescape($key);
                   4941: 	    next if ($key =~ /^error: 2 /);
                   4942: 	    $returnhash{$key}=&thaw_unescape($value);
                   4943: 	}
1.755     albertel 4944:     }
                   4945:     return %returnhash;
1.407     www      4946: }
                   4947: 
1.1098    foxr     4948: 
1.717     albertel 4949: # --------------------------------------------------------- dumpstore interface
                   4950: 
                   4951: sub dumpstore {
                   4952:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 4953:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4954:    if (!$uname) { $uname=$env{'user.name'}; }
                   4955:    my $uhome=&homeserver($uname,$udomain);
                   4956:    if ($regexp) {
                   4957:        $regexp=&escape($regexp);
                   4958:    } else {
                   4959:        $regexp='.';
                   4960:    }
                   4961:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   4962:    my @pairs=split(/\&/,$rep);
                   4963:    my %returnhash=();
                   4964:    foreach my $item (@pairs) {
                   4965:        my ($key,$value)=split(/=/,$item,2);
                   4966:        next if ($key =~ /^error: 2 /);
                   4967:        $returnhash{$key}=&thaw_unescape($value);
                   4968:    }
                   4969:    return %returnhash;
1.717     albertel 4970: }
                   4971: 
1.407     www      4972: # -------------------------------------------------------------- keys interface
                   4973: 
                   4974: sub getkeys {
                   4975:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 4976:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4977:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      4978:    my $uhome=&homeserver($uname,$udomain);
                   4979:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   4980:    my @keyarray=();
1.800     albertel 4981:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  4982:       next if ($key =~ /^error: 2 /);
1.800     albertel 4983:       push(@keyarray,&unescape($key));
1.407     www      4984:    }
                   4985:    return @keyarray;
1.318     matthew  4986: }
                   4987: 
1.319     matthew  4988: # --------------------------------------------------------------- currentdump
                   4989: sub currentdump {
1.328     matthew  4990:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 4991:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   4992:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   4993:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  4994:    my $uhome = &homeserver($sname,$sdom);
                   4995:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  4996:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  4997:    #
1.318     matthew  4998:    my %returnhash=();
1.319     matthew  4999:    #
                   5000:    if ($rep eq "unknown_cmd") { 
                   5001:        # an old lond will not know currentdump
                   5002:        # Do a dump and make it look like a currentdump
1.822     albertel 5003:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  5004:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   5005:        my %hash = @tmp;
                   5006:        @tmp=();
1.424     matthew  5007:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  5008:    } else {
                   5009:        my @pairs=split(/\&/,$rep);
1.800     albertel 5010:        foreach my $pair (@pairs) {
                   5011:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  5012:            my ($symb,$param) = split(/:/,$key);
                   5013:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 5014:                                                         &thaw_unescape($value);
1.319     matthew  5015:        }
1.191     harris41 5016:    }
1.12      www      5017:    return %returnhash;
1.424     matthew  5018: }
                   5019: 
                   5020: sub convert_dump_to_currentdump{
                   5021:     my %hash = %{shift()};
                   5022:     my %returnhash;
                   5023:     # Code ripped from lond, essentially.  The only difference
                   5024:     # here is the unescaping done by lonnet::dump().  Conceivably
                   5025:     # we might run in to problems with parameter names =~ /^v\./
                   5026:     while (my ($key,$value) = each(%hash)) {
                   5027:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 5028: 	$symb  = &unescape($symb);
                   5029: 	$param = &unescape($param);
1.424     matthew  5030:         next if ($v eq 'version' || $symb eq 'keys');
                   5031:         next if (exists($returnhash{$symb}) &&
                   5032:                  exists($returnhash{$symb}->{$param}) &&
                   5033:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   5034:         $returnhash{$symb}->{$param}=$value;
                   5035:         $returnhash{$symb}->{'v.'.$param}=$v;
                   5036:     }
                   5037:     #
                   5038:     # Remove all of the keys in the hashes which keep track of
                   5039:     # the version of the parameter.
                   5040:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   5041:         # use a foreach because we are going to delete from the hash.
                   5042:         foreach my $key (keys(%$param_hash)) {
                   5043:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   5044:         }
                   5045:     }
                   5046:     return \%returnhash;
1.12      www      5047: }
                   5048: 
1.627     albertel 5049: # ------------------------------------------------------ critical inc interface
                   5050: 
                   5051: sub cinc {
                   5052:     return &inc(@_,'critical');
                   5053: }
                   5054: 
1.449     matthew  5055: # --------------------------------------------------------------- inc interface
                   5056: 
                   5057: sub inc {
1.627     albertel 5058:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 5059:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5060:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  5061:     my $uhome=&homeserver($uname,$udomain);
                   5062:     my $items='';
                   5063:     if (! ref($store)) {
                   5064:         # got a single value, so use that instead
                   5065:         $items = &escape($store).'=&';
                   5066:     } elsif (ref($store) eq 'SCALAR') {
                   5067:         $items = &escape($$store).'=&';        
                   5068:     } elsif (ref($store) eq 'ARRAY') {
                   5069:         $items = join('=&',map {&escape($_);} @{$store});
                   5070:     } elsif (ref($store) eq 'HASH') {
                   5071:         while (my($key,$value) = each(%{$store})) {
                   5072:             $items.= &escape($key).'='.&escape($value).'&';
                   5073:         }
                   5074:     }
                   5075:     $items=~s/\&$//;
1.627     albertel 5076:     if ($critical) {
                   5077: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5078:     } else {
                   5079: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5080:     }
1.449     matthew  5081: }
                   5082: 
1.12      www      5083: # --------------------------------------------------------------- put interface
                   5084: 
                   5085: sub put {
1.134     albertel 5086:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5087:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5088:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5089:    my $uhome=&homeserver($uname,$udomain);
1.12      www      5090:    my $items='';
1.800     albertel 5091:    foreach my $item (keys(%$storehash)) {
                   5092:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5093:    }
1.12      www      5094:    $items=~s/\&$//;
1.134     albertel 5095:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      5096: }
                   5097: 
1.631     albertel 5098: # ------------------------------------------------------------ newput interface
                   5099: 
                   5100: sub newput {
                   5101:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   5102:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5103:    if (!$uname) { $uname=$env{'user.name'}; }
                   5104:    my $uhome=&homeserver($uname,$udomain);
                   5105:    my $items='';
                   5106:    foreach my $key (keys(%$storehash)) {
                   5107:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   5108:    }
                   5109:    $items=~s/\&$//;
                   5110:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   5111: }
                   5112: 
                   5113: # ---------------------------------------------------------  putstore interface
                   5114: 
1.524     raeburn  5115: sub putstore {
1.715     albertel 5116:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 5117:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5118:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  5119:    my $uhome=&homeserver($uname,$udomain);
                   5120:    my $items='';
1.715     albertel 5121:    foreach my $key (keys(%$storehash)) {
                   5122:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  5123:    }
1.715     albertel 5124:    $items=~s/\&$//;
1.716     albertel 5125:    my $esc_symb=&escape($symb);
                   5126:    my $esc_v=&escape($version);
1.715     albertel 5127:    my $reply =
1.716     albertel 5128:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 5129: 	      $uhome);
                   5130:    if ($reply eq 'unknown_cmd') {
1.716     albertel 5131:        # gfall back to way things use to be done
1.715     albertel 5132:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   5133: 			    $uname);
1.524     raeburn  5134:    }
1.715     albertel 5135:    return $reply;
                   5136: }
                   5137: 
                   5138: sub old_putstore {
1.716     albertel 5139:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   5140:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5141:     if (!$uname) { $uname=$env{'user.name'}; }
                   5142:     my $uhome=&homeserver($uname,$udomain);
                   5143:     my %newstorehash;
1.800     albertel 5144:     foreach my $item (keys(%$storehash)) {
                   5145: 	my $key = $version.':'.&escape($symb).':'.$item;
                   5146: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 5147:     }
                   5148:     my $items='';
                   5149:     my %allitems = ();
1.800     albertel 5150:     foreach my $item (keys(%newstorehash)) {
                   5151: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 5152: 	    my $key = $1.':keys:'.$2;
                   5153: 	    $allitems{$key} .= $3.':';
                   5154: 	}
1.800     albertel 5155: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 5156:     }
1.800     albertel 5157:     foreach my $item (keys(%allitems)) {
                   5158: 	$allitems{$item} =~ s/\:$//;
                   5159: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 5160:     }
                   5161:     $items=~s/\&$//;
                   5162:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  5163: }
                   5164: 
1.47      www      5165: # ------------------------------------------------------ critical put interface
                   5166: 
                   5167: sub cput {
1.134     albertel 5168:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5169:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5170:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5171:    my $uhome=&homeserver($uname,$udomain);
1.47      www      5172:    my $items='';
1.800     albertel 5173:    foreach my $item (keys(%$storehash)) {
                   5174:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5175:    }
1.47      www      5176:    $items=~s/\&$//;
1.134     albertel 5177:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5178: }
                   5179: 
                   5180: # -------------------------------------------------------------- eget interface
                   5181: 
                   5182: sub eget {
1.133     albertel 5183:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      5184:    my $items='';
1.800     albertel 5185:    foreach my $item (@$storearr) {
                   5186:        $items.=&escape($item).'&';
1.191     harris41 5187:    }
1.12      www      5188:    $items=~s/\&$//;
1.620     albertel 5189:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5190:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 5191:    my $uhome=&homeserver($uname,$udomain);
                   5192:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5193:    my @pairs=split(/\&/,$rep);
                   5194:    my %returnhash=();
1.42      www      5195:    my $i=0;
1.800     albertel 5196:    foreach my $item (@$storearr) {
                   5197:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      5198:       $i++;
1.191     harris41 5199:    }
1.12      www      5200:    return %returnhash;
                   5201: }
                   5202: 
1.667     albertel 5203: # ------------------------------------------------------------ tmpput interface
                   5204: sub tmpput {
1.802     raeburn  5205:     my ($storehash,$server,$context)=@_;
1.667     albertel 5206:     my $items='';
1.800     albertel 5207:     foreach my $item (keys(%$storehash)) {
                   5208: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 5209:     }
                   5210:     $items=~s/\&$//;
1.802     raeburn  5211:     if (defined($context)) {
                   5212:         $items .= ':'.&escape($context);
                   5213:     }
1.667     albertel 5214:     return &reply("tmpput:$items",$server);
                   5215: }
                   5216: 
                   5217: # ------------------------------------------------------------ tmpget interface
                   5218: sub tmpget {
1.688     albertel 5219:     my ($token,$server)=@_;
                   5220:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5221:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 5222:     my %returnhash;
                   5223:     foreach my $item (split(/\&/,$rep)) {
                   5224: 	my ($key,$value)=split(/=/,$item);
1.951     raeburn  5225:         next if ($key =~ /^error: 2 /);
1.667     albertel 5226: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   5227:     }
                   5228:     return %returnhash;
                   5229: }
                   5230: 
1.1113    raeburn  5231: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 5232: sub tmpdel {
                   5233:     my ($token,$server)=@_;
                   5234:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5235:     return &reply("tmpdel:$token",$server);
                   5236: }
                   5237: 
1.765     albertel 5238: # -------------------------------------------------- portfolio access checking
                   5239: 
                   5240: sub portfolio_access {
1.766     albertel 5241:     my ($requrl) = @_;
1.765     albertel 5242:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   5243:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  5244:     if ($result) {
                   5245:         my %setters;
                   5246:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5247:             my ($startblock,$endblock) =
                   5248:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   5249:             if ($startblock && $endblock) {
                   5250:                 return 'B';
                   5251:             }
                   5252:         } else {
                   5253:             my ($startblock,$endblock) =
                   5254:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   5255:             if ($startblock && $endblock) {
                   5256:                 return 'B';
                   5257:             }
                   5258:         }
                   5259:     }
1.765     albertel 5260:     if ($result eq 'ok') {
1.766     albertel 5261:        return 'F';
1.765     albertel 5262:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 5263:        return 'A';
1.765     albertel 5264:     }
1.766     albertel 5265:     return '';
1.765     albertel 5266: }
                   5267: 
                   5268: sub get_portfolio_access {
1.767     albertel 5269:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   5270: 
                   5271:     if (!ref($access_hash)) {
                   5272: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   5273: 	my %access_controls = &get_access_controls($current_perms,$group,
                   5274: 						   $file_name);
                   5275: 	$access_hash = $access_controls{$file_name};
                   5276:     }
                   5277: 
1.765     albertel 5278:     my ($public,$guest,@domains,@users,@courses,@groups);
                   5279:     my $now = time;
                   5280:     if (ref($access_hash) eq 'HASH') {
                   5281:         foreach my $key (keys(%{$access_hash})) {
                   5282:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5283:             if ($start > $now) {
                   5284:                 next;
                   5285:             }
                   5286:             if ($end && $end<$now) {
                   5287:                 next;
                   5288:             }
                   5289:             if ($scope eq 'public') {
                   5290:                 $public = $key;
                   5291:                 last;
                   5292:             } elsif ($scope eq 'guest') {
                   5293:                 $guest = $key;
                   5294:             } elsif ($scope eq 'domains') {
                   5295:                 push(@domains,$key);
                   5296:             } elsif ($scope eq 'users') {
                   5297:                 push(@users,$key);
                   5298:             } elsif ($scope eq 'course') {
                   5299:                 push(@courses,$key);
                   5300:             } elsif ($scope eq 'group') {
                   5301:                 push(@groups,$key);
                   5302:             }
                   5303:         }
                   5304:         if ($public) {
                   5305:             return 'ok';
                   5306:         }
                   5307:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5308:             if ($guest) {
                   5309:                 return $guest;
                   5310:             }
                   5311:         } else {
                   5312:             if (@domains > 0) {
                   5313:                 foreach my $domkey (@domains) {
                   5314:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   5315:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   5316:                             return 'ok';
                   5317:                         }
                   5318:                     }
                   5319:                 }
                   5320:             }
                   5321:             if (@users > 0) {
                   5322:                 foreach my $userkey (@users) {
1.865     raeburn  5323:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   5324:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   5325:                             if (ref($item) eq 'HASH') {
                   5326:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   5327:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   5328:                                     return 'ok';
                   5329:                                 }
                   5330:                             }
                   5331:                         }
                   5332:                     } 
1.765     albertel 5333:                 }
                   5334:             }
                   5335:             my %roleshash;
                   5336:             my @courses_and_groups = @courses;
                   5337:             push(@courses_and_groups,@groups); 
                   5338:             if (@courses_and_groups > 0) {
                   5339:                 my (%allgroups,%allroles); 
                   5340:                 my ($start,$end,$role,$sec,$group);
                   5341:                 foreach my $envkey (%env) {
1.1060    raeburn  5342:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 5343:                         my $cid = $2.'_'.$3; 
                   5344:                         if ($1 eq 'gr') {
                   5345:                             $group = $4;
                   5346:                             $allgroups{$cid}{$group} = $env{$envkey};
                   5347:                         } else {
                   5348:                             if ($4 eq '') {
                   5349:                                 $sec = 'none';
                   5350:                             } else {
                   5351:                                 $sec = $4;
                   5352:                             }
                   5353:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   5354:                         }
1.811     albertel 5355:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 5356:                         my $cid = $2.'_'.$3;
                   5357:                         if ($4 eq '') {
                   5358:                             $sec = 'none';
                   5359:                         } else {
                   5360:                             $sec = $4;
                   5361:                         }
                   5362:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   5363:                     }
                   5364:                 }
                   5365:                 if (keys(%allroles) == 0) {
                   5366:                     return;
                   5367:                 }
                   5368:                 foreach my $key (@courses_and_groups) {
                   5369:                     my %content = %{$$access_hash{$key}};
                   5370:                     my $cnum = $content{'number'};
                   5371:                     my $cdom = $content{'domain'};
                   5372:                     my $cid = $cdom.'_'.$cnum;
                   5373:                     if (!exists($allroles{$cid})) {
                   5374:                         next;
                   5375:                     }    
                   5376:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   5377:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   5378:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   5379:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   5380:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   5381:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   5382:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   5383:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   5384:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   5385:                                         if (grep/^all$/,@sections) {
                   5386:                                             return 'ok';
                   5387:                                         } else {
                   5388:                                             if (grep/^$sec$/,@sections) {
                   5389:                                                 return 'ok';
                   5390:                                             }
                   5391:                                         }
                   5392:                                     }
                   5393:                                 }
                   5394:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   5395:                                     if (grep/^none$/,@groups) {
                   5396:                                         return 'ok';
                   5397:                                     }
                   5398:                                 } else {
                   5399:                                     if (grep/^all$/,@groups) {
                   5400:                                         return 'ok';
                   5401:                                     } 
                   5402:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   5403:                                         if (grep/^$group$/,@groups) {
                   5404:                                             return 'ok';
                   5405:                                         }
                   5406:                                     }
                   5407:                                 } 
                   5408:                             }
                   5409:                         }
                   5410:                     }
                   5411:                 }
                   5412:             }
                   5413:             if ($guest) {
                   5414:                 return $guest;
                   5415:             }
                   5416:         }
                   5417:     }
                   5418:     return;
                   5419: }
                   5420: 
                   5421: sub course_group_datechecker {
                   5422:     my ($dates,$now,$status) = @_;
                   5423:     my ($start,$end) = split(/\./,$dates);
                   5424:     if (!$start && !$end) {
                   5425:         return 'ok';
                   5426:     }
                   5427:     if (grep/^active$/,@{$status}) {
                   5428:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   5429:             return 'ok';
                   5430:         }
                   5431:     }
                   5432:     if (grep/^previous$/,@{$status}) {
                   5433:         if ($end > $now ) {
                   5434:             return 'ok';
                   5435:         }
                   5436:     }
                   5437:     if (grep/^future$/,@{$status}) {
                   5438:         if ($start > $now) {
                   5439:             return 'ok';
                   5440:         }
                   5441:     }
                   5442:     return; 
                   5443: }
                   5444: 
                   5445: sub parse_portfolio_url {
                   5446:     my ($url) = @_;
                   5447: 
                   5448:     my ($type,$udom,$unum,$group,$file_name);
                   5449:     
1.823     albertel 5450:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 5451: 	$type = 1;
                   5452:         $udom = $1;
                   5453:         $unum = $2;
                   5454:         $file_name = $3;
1.823     albertel 5455:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 5456: 	$type = 2;
                   5457:         $udom = $1;
                   5458:         $unum = $2;
                   5459:         $group = $3;
                   5460:         $file_name = $3.'/'.$4;
                   5461:     }
                   5462:     if (wantarray) {
                   5463: 	return ($type,$udom,$unum,$file_name,$group);
                   5464:     }
                   5465:     return $type;
                   5466: }
                   5467: 
                   5468: sub is_portfolio_url {
                   5469:     my ($url) = @_;
                   5470:     return scalar(&parse_portfolio_url($url));
                   5471: }
                   5472: 
1.798     raeburn  5473: sub is_portfolio_file {
                   5474:     my ($file) = @_;
1.820     raeburn  5475:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  5476:         return 1;
                   5477:     }
                   5478:     return;
                   5479: }
                   5480: 
1.976     raeburn  5481: sub usertools_access {
1.1084    raeburn  5482:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  5483:     my ($access,%tools);
                   5484:     if ($context eq '') {
                   5485:         $context = 'tools';
                   5486:     }
                   5487:     if ($context eq 'requestcourses') {
                   5488:         %tools = (
                   5489:                       official   => 1,
                   5490:                       unofficial => 1,
1.1006    raeburn  5491:                       community  => 1,
1.985     raeburn  5492:                  );
                   5493:     } else {
                   5494:         %tools = (
                   5495:                       aboutme   => 1,
                   5496:                       blog      => 1,
                   5497:                       portfolio => 1,
                   5498:                  );
                   5499:     }
1.976     raeburn  5500:     return if (!defined($tools{$tool}));
                   5501: 
                   5502:     if ((!defined($udom)) || (!defined($uname))) {
                   5503:         $udom = $env{'user.domain'};
                   5504:         $uname = $env{'user.name'};
                   5505:     }
                   5506: 
1.978     raeburn  5507:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   5508:         if ($action ne 'reload') {
1.985     raeburn  5509:             if ($context eq 'requestcourses') {
                   5510:                 return $env{'environment.canrequest.'.$tool};
                   5511:             } else {
                   5512:                 return $env{'environment.availabletools.'.$tool};
                   5513:             }
                   5514:         }
1.976     raeburn  5515:     }
                   5516: 
                   5517:     my ($toolstatus,$inststatus);
                   5518: 
1.985     raeburn  5519:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   5520:          ($action ne 'reload')) {
                   5521:         $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976     raeburn  5522:         $inststatus = $env{'environment.inststatus'};
                   5523:     } else {
1.1084    raeburn  5524:         if (ref($userenvref) eq 'HASH') {
                   5525:             $toolstatus = $userenvref->{$context.'.'.$tool};
                   5526:             $inststatus = $userenvref->{'inststatus'};
                   5527:         } else {
                   5528:             my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
                   5529:             $toolstatus = $userenv{$context.'.'.$tool};
                   5530:             $inststatus = $userenv{'inststatus'};
                   5531:         }
1.976     raeburn  5532:     }
                   5533: 
                   5534:     if ($toolstatus ne '') {
                   5535:         if ($toolstatus) {
                   5536:             $access = 1;
                   5537:         } else {
                   5538:             $access = 0;
                   5539:         }
                   5540:         return $access;
                   5541:     }
                   5542: 
1.1084    raeburn  5543:     my ($is_adv,%domdef);
                   5544:     if (ref($is_advref) eq 'HASH') {
                   5545:         $is_adv = $is_advref->{'is_adv'};
                   5546:     } else {
                   5547:         $is_adv = &is_advanced_user($udom,$uname);
                   5548:     }
                   5549:     if (ref($domdefref) eq 'HASH') {
                   5550:         %domdef = %{$domdefref};
                   5551:     } else {
                   5552:         %domdef = &get_domain_defaults($udom);
                   5553:     }
1.976     raeburn  5554:     if (ref($domdef{$tool}) eq 'HASH') {
                   5555:         if ($is_adv) {
                   5556:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   5557:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   5558:                     $access = 1;
                   5559:                 } else {
                   5560:                     $access = 0;
                   5561:                 }
                   5562:                 return $access;
                   5563:             }
                   5564:         }
                   5565:         if ($inststatus ne '') {
                   5566:             my ($hasaccess,$hasnoaccess);
                   5567:             foreach my $affiliation (split(/:/,$inststatus)) {
                   5568:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   5569:                     if ($domdef{$tool}{$affiliation}) {
                   5570:                         $hasaccess = 1;
                   5571:                     } else {
                   5572:                         $hasnoaccess = 1;
                   5573:                     }
                   5574:                 }
                   5575:             }
                   5576:             if ($hasaccess || $hasnoaccess) {
                   5577:                 if ($hasaccess) {
                   5578:                     $access = 1;
                   5579:                 } elsif ($hasnoaccess) {
                   5580:                     $access = 0; 
                   5581:                 }
                   5582:                 return $access;
                   5583:             }
                   5584:         } else {
                   5585:             if ($domdef{$tool}{'default'} ne '') {
                   5586:                 if ($domdef{$tool}{'default'}) {
                   5587:                     $access = 1;
                   5588:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   5589:                     $access = 0;
                   5590:                 }
                   5591:                 return $access;
                   5592:             }
                   5593:         }
                   5594:     } else {
1.985     raeburn  5595:         if ($context eq 'tools') {
                   5596:             $access = 1;
                   5597:         } else {
                   5598:             $access = 0;
                   5599:         }
1.976     raeburn  5600:         return $access;
                   5601:     }
                   5602: }
                   5603: 
1.1050    raeburn  5604: sub is_course_owner {
                   5605:     my ($cdom,$cnum,$udom,$uname) = @_;
                   5606:     if (($udom eq '') || ($uname eq '')) {
                   5607:         $udom = $env{'user.domain'};
                   5608:         $uname = $env{'user.name'};
                   5609:     }
                   5610:     unless (($udom eq '') || ($uname eq '')) {
                   5611:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   5612:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   5613:                 return 1;
                   5614:             } else {
                   5615:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   5616:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   5617:                     return 1;
                   5618:                 }
                   5619:             }
                   5620:         }
                   5621:     }
                   5622:     return;
                   5623: }
                   5624: 
1.976     raeburn  5625: sub is_advanced_user {
                   5626:     my ($udom,$uname) = @_;
1.1085    raeburn  5627:     if ($udom ne '' && $uname ne '') {
                   5628:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  5629:             if (wantarray) {
                   5630:                 return ($env{'user.adv'},$env{'user.author'});
                   5631:             } else {
                   5632:                 return $env{'user.adv'};
                   5633:             }
1.1085    raeburn  5634:         }
                   5635:     }
1.976     raeburn  5636:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   5637:     my %allroles;
1.1128    raeburn  5638:     my ($is_adv,$is_author);
1.976     raeburn  5639:     foreach my $role (keys(%roleshash)) {
                   5640:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   5641:         my $area = '/'.$tdomain.'/'.$trest;
                   5642:         if ($sec ne '') {
                   5643:             $area .= '/'.$sec;
                   5644:         }
                   5645:         if (($area ne '') && ($trole ne '')) {
                   5646:             my $spec=$trole.'.'.$area;
                   5647:             if ($trole =~ /^cr\//) {
                   5648:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   5649:             } elsif ($trole ne 'gr') {
                   5650:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   5651:             }
1.1128    raeburn  5652:             if ($trole eq 'au') {
                   5653:                 $is_author = 1;
                   5654:             }
1.976     raeburn  5655:         }
                   5656:     }
                   5657:     foreach my $role (keys(%allroles)) {
                   5658:         last if ($is_adv);
                   5659:         foreach my $item (split(/:/,$allroles{$role})) {
                   5660:             if ($item ne '') {
                   5661:                 my ($privilege,$restrictions)=split(/&/,$item);
                   5662:                 if ($privilege eq 'adv') {
                   5663:                     $is_adv = 1;
                   5664:                     last;
                   5665:                 }
                   5666:             }
                   5667:         }
                   5668:     }
1.1128    raeburn  5669:     if (wantarray) {
                   5670:         return ($is_adv,$is_author);
                   5671:     }
1.976     raeburn  5672:     return $is_adv;
                   5673: }
1.798     raeburn  5674: 
1.1035    raeburn  5675: sub check_can_request {
1.1036    raeburn  5676:     my ($dom,$can_request,$request_domains) = @_;
1.1035    raeburn  5677:     my $canreq = 0;
                   5678:     my ($types,$typename) = &Apache::loncommon::course_types();
                   5679:     my @options = ('approval','validate','autolimit');
                   5680:     my $optregex = join('|',@options);
                   5681:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   5682:         foreach my $type (@{$types}) {
                   5683:             if (&usertools_access($env{'user.name'},
                   5684:                                   $env{'user.domain'},
                   5685:                                   $type,undef,'requestcourses')) {
                   5686:                 $canreq ++;
1.1036    raeburn  5687:                 if (ref($request_domains) eq 'HASH') {
                   5688:                     push(@{$request_domains->{$type}},$env{'user.domain'});
                   5689:                 }
1.1035    raeburn  5690:                 if ($dom eq $env{'user.domain'}) {
                   5691:                     $can_request->{$type} = 1;
                   5692:                 }
                   5693:             }
                   5694:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                   5695:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   5696:                 if (@curr > 0) {
1.1036    raeburn  5697:                     foreach my $item (@curr) {
                   5698:                         if (ref($request_domains) eq 'HASH') {
                   5699:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   5700:                             if ($otherdom ne '') {
                   5701:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   5702:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   5703:                                         push(@{$request_domains->{$type}},$otherdom);
                   5704:                                     }
                   5705:                                 } else {
                   5706:                                     push(@{$request_domains->{$type}},$otherdom);
                   5707:                                 }
                   5708:                             }
                   5709:                         }
                   5710:                     }
                   5711:                     unless($dom eq $env{'user.domain'}) {
                   5712:                         $canreq ++;
1.1035    raeburn  5713:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   5714:                             $can_request->{$type} = 1;
                   5715:                         }
                   5716:                     }
                   5717:                 }
                   5718:             }
                   5719:         }
                   5720:     }
                   5721:     return $canreq;
                   5722: }
                   5723: 
1.341     www      5724: # ---------------------------------------------- Custom access rule evaluation
                   5725: 
                   5726: sub customaccess {
                   5727:     my ($priv,$uri)=@_;
1.807     albertel 5728:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      5729:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 5730:     $udom = &LONCAPA::clean_domain($udom);
                   5731:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      5732:     my $access=0;
1.800     albertel 5733:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 5734: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   5735: 	if ($type eq 'user') {
                   5736: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 5737: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 5738: 		if ($tdom) {
                   5739: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   5740: 		}
1.896     albertel 5741: 		if ($tuname) {
                   5742: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 5743: 		}
                   5744: 		$access=($effect eq 'allow');
                   5745: 		last;
                   5746: 	    }
                   5747: 	} else {
                   5748: 	    if ($role) {
                   5749: 		if ($role ne $urole) { next; }
                   5750: 	    }
                   5751: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   5752: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   5753: 		if ($tdom) {
                   5754: 		    if ($tdom ne $udom) { next; }
                   5755: 		}
                   5756: 		if ($tcrs) {
                   5757: 		    if ($tcrs ne $ucrs) { next; }
                   5758: 		}
                   5759: 		if ($tsec) {
                   5760: 		    if ($tsec ne $usec) { next; }
                   5761: 		}
                   5762: 		$access=($effect eq 'allow');
                   5763: 		last;
                   5764: 	    }
                   5765: 	    if ($realm eq '' && $role eq '') {
                   5766: 		$access=($effect eq 'allow');
                   5767: 	    }
1.402     bowersj2 5768: 	}
1.341     www      5769:     }
                   5770:     return $access;
                   5771: }
                   5772: 
1.103     harris41 5773: # ------------------------------------------------- Check for a user privilege
1.12      www      5774: 
                   5775: sub allowed {
1.810     raeburn  5776:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 5777:     my $ver_orguri=$uri;
1.439     www      5778:     $uri=&deversion($uri);
1.152     www      5779:     my $orguri=$uri;
1.52      www      5780:     $uri=&declutter($uri);
1.809     raeburn  5781: 
1.810     raeburn  5782:     if ($priv eq 'evb') {
                   5783: # Evade communication block restrictions for specified role in a course
                   5784:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   5785:             return $1;
                   5786:         } else {
                   5787:             return;
                   5788:         }
                   5789:     }
                   5790: 
1.620     albertel 5791:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      5792: # Free bre access to adm and meta resources
1.775     albertel 5793:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 5794: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   5795: 	&& ($priv eq 'bre')) {
1.14      www      5796: 	return 'F';
1.159     www      5797:     }
                   5798: 
1.545     banghart 5799: # Free bre access to user's own portfolio contents
1.714     raeburn  5800:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  5801:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  5802: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  5803:         my %setters;
                   5804:         my ($startblock,$endblock) = 
                   5805:             &Apache::loncommon::blockcheck(\%setters,'port');
                   5806:         if ($startblock && $endblock) {
                   5807:             return 'B';
                   5808:         } else {
                   5809:             return 'F';
                   5810:         }
1.545     banghart 5811:     }
                   5812: 
1.762     raeburn  5813: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  5814:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   5815:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   5816:         if (exists($env{'request.course.id'})) {
                   5817:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5818:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5819:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   5820:                 my $courseprivid=$env{'request.course.id'};
                   5821:                 $courseprivid=~s/\_/\//;
                   5822:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   5823:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   5824:                     return $1; 
1.762     raeburn  5825:                 } else {
                   5826:                     if ($env{'request.course.sec'}) {
                   5827:                         $courseprivid.='/'.$env{'request.course.sec'};
                   5828:                     }
                   5829:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   5830:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   5831:                         return $2;
                   5832:                     }
1.714     raeburn  5833:                 }
                   5834:             }
                   5835:         }
                   5836:     }
                   5837: 
1.159     www      5838: # Free bre to public access
                   5839: 
                   5840:     if ($priv eq 'bre') {
1.238     www      5841:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 5842: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      5843:            return 'F'; 
                   5844:         }
1.238     www      5845:         if ($copyright eq 'priv') {
                   5846:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5847: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      5848: 		return '';
                   5849:             }
                   5850:         }
                   5851:         if ($copyright eq 'domain') {
                   5852:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5853: 	    unless (($env{'user.domain'} eq $1) ||
                   5854:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      5855: 		return '';
                   5856:             }
1.262     matthew  5857:         }
1.620     albertel 5858:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  5859:             # Library role, so allow browsing of resources in this domain.
                   5860:             return 'F';
1.238     www      5861:         }
1.341     www      5862:         if ($copyright eq 'custom') {
                   5863: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   5864:         }
1.14      www      5865:     }
1.264     matthew  5866:     # Domain coordinator is trying to create a course
1.620     albertel 5867:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  5868:         # uri is the requested domain in this case.
                   5869:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  5870:         # a role of dc for the domain in question.
1.620     albertel 5871:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  5872:     }
1.29      www      5873: 
1.52      www      5874:     my $thisallowed='';
                   5875:     my $statecond=0;
                   5876:     my $courseprivid='';
                   5877: 
1.1039    raeburn  5878:     my $ownaccess;
1.1043    raeburn  5879:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  5880:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   5881:         if ($uri eq '') {
                   5882:             $ownaccess = 1;
                   5883:         } else {
                   5884:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   5885:                 my $udom = $env{'user.domain'};
                   5886:                 my $uname = $env{'user.name'};
                   5887:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   5888:                     $ownaccess = 1;
1.1040    raeburn  5889:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  5890:                     unless ($uri =~ m{\.\./}) {
                   5891:                         $ownaccess = 1;
                   5892:                     }
                   5893:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   5894:                     my $now = time;
                   5895:                     if ($uri =~ m{^([^/]+)/?$}) {
                   5896:                         my $adom = $1;
                   5897:                         foreach my $key (keys(%env)) {
1.1042    raeburn  5898:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  5899:                                 my ($start,$end) = split('.',$env{$key});
                   5900:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5901:                                     $ownaccess = 1;
                   5902:                                     last;
                   5903:                                 }
                   5904:                             }
                   5905:                         }
                   5906:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   5907:                         my $adom = $1;
                   5908:                         my $aname = $2;
1.1042    raeburn  5909:                         foreach my $role ('ca','aa') { 
                   5910:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   5911:                                 my ($start,$end) =
                   5912:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   5913:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5914:                                     $ownaccess = 1;
                   5915:                                     last;
                   5916:                                 }
1.1039    raeburn  5917:                             }
                   5918:                         }
                   5919:                     }
                   5920:                 }
                   5921:             }
                   5922:         }
                   5923:     }
                   5924: 
1.52      www      5925: # Course
                   5926: 
1.620     albertel 5927:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5928:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5929:             $thisallowed.=$1;
                   5930:         }
1.52      www      5931:     }
1.29      www      5932: 
1.52      www      5933: # Domain
                   5934: 
1.620     albertel 5935:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 5936:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5937:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5938:             $thisallowed.=$1;
                   5939:         }
1.12      www      5940:     }
1.52      www      5941: 
1.1141    raeburn  5942: # User who is not author or co-author might still be able to edit
                   5943: # resource of an author in the domain (e.g., if Domain Coordinator).
                   5944:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   5945:         (&allowed('mdc',$env{'request.course.id'}))) {
                   5946:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   5947:             $thisallowed.=$1;
                   5948:         }
                   5949:     }
                   5950: 
1.52      www      5951: # Course: uri itself is a course
1.66      www      5952:     my $courseuri=$uri;
                   5953:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      5954:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      5955: 
1.620     albertel 5956:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 5957:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5958:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5959:             $thisallowed.=$1;
                   5960:         }
1.12      www      5961:     }
1.29      www      5962: 
1.665     albertel 5963: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 5964: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 5965:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 5966: 	$thisallowed='';
1.671     raeburn  5967:         my ($match)=&is_on_map($uri);
                   5968:         if ($match) {
                   5969:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   5970:                   =~/\Q$priv\E\&([^\:]*)/) {
                   5971:                 $thisallowed.=$1;
                   5972:             }
                   5973:         } else {
1.705     albertel 5974:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  5975:             if ($refuri) {
                   5976:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  5977:                     $thisallowed='F';
1.671     raeburn  5978:                 } else {
                   5979:                     $refuri=&declutter($refuri);
                   5980:                     my ($match) = &is_on_map($refuri);
                   5981:                     if ($match) {
                   5982:                         $thisallowed='F';
                   5983:                     }
1.669     raeburn  5984:                 }
1.671     raeburn  5985:             }
                   5986:         }
1.314     www      5987:     }
1.492     albertel 5988: 
1.766     albertel 5989:     if ($priv eq 'bre'
                   5990: 	&& $thisallowed ne 'F' 
                   5991: 	&& $thisallowed ne '2'
                   5992: 	&& &is_portfolio_url($uri)) {
                   5993: 	$thisallowed = &portfolio_access($uri);
                   5994:     }
                   5995:     
1.52      www      5996: # Full access at system, domain or course-wide level? Exit.
1.29      www      5997:     if ($thisallowed=~/F/) {
                   5998: 	return 'F';
                   5999:     }
                   6000: 
1.52      www      6001: # If this is generating or modifying users, exit with special codes
1.29      www      6002: 
1.643     www      6003:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   6004: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 6005: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      6006: # no author name given, so this just checks on the general right to make a co-author in this domain
                   6007: 	    unless ($auname) { return $thisallowed; }
                   6008: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 6009: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   6010: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   6011: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   6012: 	}
1.52      www      6013: 	return $thisallowed;
                   6014:     }
                   6015: #
1.103     harris41 6016: # Gathered so far: system, domain and course wide privileges
1.52      www      6017: #
                   6018: # Course: See if uri or referer is an individual resource that is part of 
                   6019: # the course
                   6020: 
1.620     albertel 6021:     if ($env{'request.course.id'}) {
1.232     www      6022: 
1.620     albertel 6023:        $courseprivid=$env{'request.course.id'};
                   6024:        if ($env{'request.course.sec'}) {
                   6025:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      6026:        }
                   6027:        $courseprivid=~s/\_/\//;
                   6028:        my $checkreferer=1;
1.232     www      6029:        my ($match,$cond)=&is_on_map($uri);
                   6030:        if ($match) {
                   6031:            $statecond=$cond;
1.620     albertel 6032:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6033:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      6034:                $thisallowed.=$1;
                   6035:                $checkreferer=0;
                   6036:            }
1.29      www      6037:        }
1.83      www      6038:        
1.148     www      6039:        if ($checkreferer) {
1.620     albertel 6040: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      6041:             unless ($refuri) {
1.800     albertel 6042:                 foreach my $key (keys(%env)) {
                   6043: 		    if ($key=~/^httpref\..*\*/) {
                   6044: 			my $pattern=$key;
1.156     www      6045:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      6046:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   6047:                         $pattern=~s/\//\\\//g;
1.152     www      6048:                         if ($orguri=~/$pattern/) {
1.800     albertel 6049: 			    $refuri=$env{$key};
1.148     www      6050:                         }
                   6051:                     }
1.191     harris41 6052:                 }
1.148     www      6053:             }
1.232     www      6054: 
1.148     www      6055:          if ($refuri) { 
1.152     www      6056: 	  $refuri=&declutter($refuri);
1.232     www      6057:           my ($match,$cond)=&is_on_map($refuri);
                   6058:             if ($match) {
                   6059:               my $refstatecond=$cond;
1.620     albertel 6060:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6061:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      6062:                   $thisallowed.=$1;
1.53      www      6063:                   $uri=$refuri;
                   6064:                   $statecond=$refstatecond;
1.52      www      6065:               }
                   6066:           }
1.148     www      6067:         }
1.29      www      6068:        }
1.52      www      6069:    }
1.29      www      6070: 
1.52      www      6071: #
1.103     harris41 6072: # Gathered now: all privileges that could apply, and condition number
1.52      www      6073: # 
                   6074: #
                   6075: # Full or no access?
                   6076: #
1.29      www      6077: 
1.52      www      6078:     if ($thisallowed=~/F/) {
                   6079: 	return 'F';
                   6080:     }
1.29      www      6081: 
1.52      www      6082:     unless ($thisallowed) {
                   6083:         return '';
                   6084:     }
1.29      www      6085: 
1.52      www      6086: # Restrictions exist, deal with them
                   6087: #
                   6088: #   C:according to course preferences
                   6089: #   R:according to resource settings
                   6090: #   L:unless locked
                   6091: #   X:according to user session state
                   6092: #
                   6093: 
                   6094: # Possibly locked functionality, check all courses
1.54      www      6095: # Locks might take effect only after 10 minutes cache expiration for other
                   6096: # courses, and 2 minutes for current course
1.52      www      6097: 
                   6098:     my $envkey;
                   6099:     if ($thisallowed=~/L/) {
1.1000    raeburn  6100:         foreach $envkey (keys(%env)) {
1.54      www      6101:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   6102:                my $courseid=$2;
                   6103:                my $roleid=$1.'.'.$2;
1.92      www      6104:                $courseid=~s/^\///;
1.54      www      6105:                my $expiretime=600;
1.620     albertel 6106:                if ($env{'request.role'} eq $roleid) {
1.54      www      6107: 		  $expiretime=120;
                   6108:                }
                   6109: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   6110:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 6111:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 6112: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      6113:                }
1.620     albertel 6114:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6115:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   6116: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   6117:                        &log($env{'user.domain'},$env{'user.name'},
                   6118:                             $env{'user.home'},
1.57      www      6119:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      6120:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6121:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6122: 		       return '';
                   6123:                    }
                   6124:                }
1.620     albertel 6125:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6126:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   6127: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   6128:                        &log($env{'user.domain'},$env{'user.name'},
                   6129:                             $env{'user.home'},
1.57      www      6130:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      6131:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6132:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6133: 		       return '';
                   6134:                    }
                   6135:                }
                   6136: 	   }
1.29      www      6137:        }
1.52      www      6138:     }
                   6139:    
                   6140: #
                   6141: # Rest of the restrictions depend on selected course
                   6142: #
                   6143: 
1.620     albertel 6144:     unless ($env{'request.course.id'}) {
1.766     albertel 6145: 	if ($thisallowed eq 'A') {
                   6146: 	    return 'A';
1.814     raeburn  6147:         } elsif ($thisallowed eq 'B') {
                   6148:             return 'B';
1.766     albertel 6149: 	} else {
                   6150: 	    return '1';
                   6151: 	}
1.52      www      6152:     }
1.29      www      6153: 
1.52      www      6154: #
                   6155: # Now user is definitely in a course
                   6156: #
1.53      www      6157: 
                   6158: 
                   6159: # Course preferences
                   6160: 
                   6161:    if ($thisallowed=~/C/) {
1.620     albertel 6162:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   6163:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   6164:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 6165: 	   =~/\Q$rolecode\E/) {
1.1103    raeburn  6166: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6167: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6168: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   6169: 			$env{'request.course.id'});
                   6170: 	   }
1.237     www      6171:            return '';
                   6172:        }
                   6173: 
1.620     albertel 6174:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 6175: 	   =~/\Q$unamedom\E/) {
1.1103    raeburn  6176: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6177: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   6178: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   6179: 			$env{'request.course.id'});
                   6180: 	   }
1.54      www      6181:            return '';
                   6182:        }
1.53      www      6183:    }
                   6184: 
                   6185: # Resource preferences
                   6186: 
                   6187:    if ($thisallowed=~/R/) {
1.620     albertel 6188:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 6189:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  6190: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6191: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6192: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   6193: 	   }
                   6194: 	   return '';
1.54      www      6195:        }
1.53      www      6196:    }
1.30      www      6197: 
1.246     www      6198: # Restricted by state or randomout?
1.30      www      6199: 
1.52      www      6200:    if ($thisallowed=~/X/) {
1.620     albertel 6201:       if ($env{'acc.randomout'}) {
1.579     albertel 6202: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 6203:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      6204:             return ''; 
                   6205:          }
1.247     www      6206:       }
                   6207:       if (&condval($statecond)) {
1.52      www      6208: 	 return '2';
                   6209:       } else {
                   6210:          return '';
                   6211:       }
                   6212:    }
1.30      www      6213: 
1.766     albertel 6214:     if ($thisallowed eq 'A') {
                   6215: 	return 'A';
1.814     raeburn  6216:     } elsif ($thisallowed eq 'B') {
                   6217:         return 'B';
1.766     albertel 6218:     }
1.52      www      6219:    return 'F';
1.232     www      6220: }
1.1133    foxr     6221: #
                   6222: #   Removes the versino from a URI and
                   6223: #   splits it in to its filename and path to the filename.
                   6224: #   Seems like File::Basename could have done this more clearly.
                   6225: #   Parameters:
                   6226: #      $uri   - input URI
                   6227: #   Returns:
                   6228: #     Two element list consisting of 
                   6229: #     $pathname  - the URI up to and excluding the trailing /
                   6230: #     $filename  - The part of the URI following the last /
                   6231: #  NOTE:
                   6232: #    Another realization of this is simply:
                   6233: #    use File::Basename;
                   6234: #    ...
                   6235: #    $uri = shift;
                   6236: #    $filename = basename($uri);
                   6237: #    $path     = dirname($uri);
                   6238: #    return ($filename, $path);
                   6239: #
                   6240: #     The implementation below is probably faster however.
                   6241: #
1.710     albertel 6242: sub split_uri_for_cond {
                   6243:     my $uri=&deversion(&declutter(shift));
                   6244:     my @uriparts=split(/\//,$uri);
                   6245:     my $filename=pop(@uriparts);
                   6246:     my $pathname=join('/',@uriparts);
                   6247:     return ($pathname,$filename);
                   6248: }
1.232     www      6249: # --------------------------------------------------- Is a resource on the map?
                   6250: 
                   6251: sub is_on_map {
1.710     albertel 6252:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 6253:     #Trying to find the conditional for the file
1.620     albertel 6254:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 6255: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      6256:     if ($match) {
1.289     bowersj2 6257: 	return (1,$1);
                   6258:     } else {
1.434     www      6259: 	return (0,0);
1.289     bowersj2 6260:     }
1.12      www      6261: }
                   6262: 
1.427     www      6263: # --------------------------------------------------------- Get symb from alias
                   6264: 
                   6265: sub get_symb_from_alias {
                   6266:     my $symb=shift;
                   6267:     my ($map,$resid,$url)=&decode_symb($symb);
                   6268: # Already is a symb
                   6269:     if ($url) { return $symb; }
                   6270: # Must be an alias
                   6271:     my $aliassymb='';
                   6272:     my %bighash;
1.620     albertel 6273:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      6274:                             &GDBM_READER(),0640)) {
                   6275:         my $rid=$bighash{'mapalias_'.$symb};
                   6276: 	if ($rid) {
                   6277: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 6278: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   6279: 				    $resid,$bighash{'src_'.$rid});
1.427     www      6280: 	}
                   6281:         untie %bighash;
                   6282:     }
                   6283:     return $aliassymb;
                   6284: }
                   6285: 
1.12      www      6286: # ----------------------------------------------------------------- Define Role
                   6287: 
                   6288: sub definerole {
                   6289:   if (allowed('mcr','/')) {
                   6290:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 6291:     foreach my $role (split(':',$sysrole)) {
                   6292: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6293:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   6294:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   6295: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      6296:                return "refused:s:$crole&$cqual"; 
                   6297:             }
                   6298:         }
1.191     harris41 6299:     }
1.800     albertel 6300:     foreach my $role (split(':',$domrole)) {
                   6301: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6302:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   6303:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   6304: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      6305:                return "refused:d:$crole&$cqual"; 
                   6306:             }
                   6307:         }
1.191     harris41 6308:     }
1.800     albertel 6309:     foreach my $role (split(':',$courole)) {
                   6310: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6311:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   6312:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   6313: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      6314:                return "refused:c:$crole&$cqual"; 
                   6315:             }
                   6316:         }
1.191     harris41 6317:     }
1.620     albertel 6318:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   6319:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      6320: 	        "rolesdef_$rolename=".
                   6321:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 6322:     return reply($command,$env{'user.home'});
1.12      www      6323:   } else {
                   6324:     return 'refused';
                   6325:   }
1.105     harris41 6326: }
                   6327: 
                   6328: # ---------------- Make a metadata query against the network of library servers
                   6329: 
                   6330: sub metadata_query {
1.244     matthew  6331:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 6332:     my %rhash;
1.845     albertel 6333:     my %libserv = &all_library();
1.244     matthew  6334:     my @server_list = (defined($server_array) ? @$server_array
                   6335:                                               : keys(%libserv) );
                   6336:     for my $server (@server_list) {
1.118     harris41 6337: 	unless ($custom or $customshow) {
                   6338: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   6339: 	    $rhash{$server}=$reply;
                   6340: 	}
                   6341: 	else {
                   6342: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   6343: 			     &escape($custom).':'.&escape($customshow),
                   6344: 			     $server);
                   6345: 	    $rhash{$server}=$reply;
                   6346: 	}
1.112     harris41 6347:     }
1.118     harris41 6348:     return \%rhash;
1.240     www      6349: }
                   6350: 
                   6351: # ----------------------------------------- Send log queries and wait for reply
                   6352: 
                   6353: sub log_query {
                   6354:     my ($uname,$udom,$query,%filters)=@_;
                   6355:     my $uhome=&homeserver($uname,$udom);
                   6356:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 6357:     my $uhost=&hostname($uhome);
1.800     albertel 6358:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      6359:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   6360:                        $uhome);
1.479     albertel 6361:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      6362:     return get_query_reply($queryid);
                   6363: }
                   6364: 
1.818     raeburn  6365: # -------------------------- Update MySQL table for portfolio file
                   6366: 
                   6367: sub update_portfolio_table {
1.821     raeburn  6368:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  6369:     if ($group ne '') {
                   6370:         $file_name =~s /^\Q$group\E//;
                   6371:     }
1.818     raeburn  6372:     my $homeserver = &homeserver($uname,$udom);
                   6373:     my $queryid=
1.821     raeburn  6374:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   6375:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  6376:     my $reply = &get_query_reply($queryid);
                   6377:     return $reply;
                   6378: }
                   6379: 
1.899     raeburn  6380: # -------------------------- Update MySQL allusers table
                   6381: 
                   6382: sub update_allusers_table {
                   6383:     my ($uname,$udom,$names) = @_;
                   6384:     my $homeserver = &homeserver($uname,$udom);
                   6385:     my $queryid=
                   6386:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   6387:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   6388:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   6389:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   6390:                'generation='.&escape($names->{'generation'}).'%%'.
                   6391:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   6392:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  6393:     return;
1.899     raeburn  6394: }
                   6395: 
1.508     raeburn  6396: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  6397: 
                   6398: sub fetch_enrollment_query {
1.511     raeburn  6399:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  6400:     my $homeserver;
1.547     raeburn  6401:     my $maxtries = 1;
1.508     raeburn  6402:     if ($context eq 'automated') {
                   6403:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  6404:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  6405:     } else {
                   6406:         $homeserver = &homeserver($cnum,$dom);
                   6407:     }
1.838     albertel 6408:     my $host=&hostname($homeserver);
1.506     raeburn  6409:     my $cmd = '';
1.1000    raeburn  6410:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 6411:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  6412:     }
                   6413:     $cmd =~ s/%%$//;
                   6414:     $cmd = &escape($cmd);
                   6415:     my $query = 'fetchenrollment';
1.620     albertel 6416:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  6417:     unless ($queryid=~/^\Q$host\E\_/) { 
                   6418:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   6419:         return 'error: '.$queryid;
                   6420:     }
1.506     raeburn  6421:     my $reply = &get_query_reply($queryid);
1.547     raeburn  6422:     my $tries = 1;
                   6423:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   6424:         $reply = &get_query_reply($queryid);
                   6425:         $tries ++;
                   6426:     }
1.526     raeburn  6427:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 6428:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  6429:     } else {
1.901     albertel 6430:         my @responses = split(/:/,$reply);
1.515     raeburn  6431:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 6432:             foreach my $line (@responses) {
                   6433:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  6434:                 $$replyref{$key} = $value;
                   6435:             }
                   6436:         } else {
1.1117    foxr     6437:             my $pathname = LONCAPA::tempdir();
1.800     albertel 6438:             foreach my $line (@responses) {
                   6439:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  6440:                 $$replyref{$key} = $value;
                   6441:                 if ($value > 0) {
1.800     albertel 6442:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   6443:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  6444:                         my $destname = $pathname.'/'.$filename;
                   6445:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  6446:                         if ($xml_classlist =~ /^error/) {
                   6447:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   6448:                         } else {
1.506     raeburn  6449:                             if ( open(FILE,">$destname") ) {
                   6450:                                 print FILE &unescape($xml_classlist);
                   6451:                                 close(FILE);
1.526     raeburn  6452:                             } else {
                   6453:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  6454:                             }
                   6455:                         }
                   6456:                     }
                   6457:                 }
                   6458:             }
                   6459:         }
                   6460:         return 'ok';
                   6461:     }
                   6462:     return 'error';
                   6463: }
                   6464: 
1.242     www      6465: sub get_query_reply {
                   6466:     my $queryid=shift;
1.1117    foxr     6467:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      6468:     my $reply='';
                   6469:     for (1..100) {
                   6470: 	sleep 2;
                   6471:         if (-e $replyfile.'.end') {
1.448     albertel 6472: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 6473: 		$reply = join('',<$fh>);
                   6474: 		close($fh);
1.240     www      6475: 	   } else { return 'error: reply_file_error'; }
1.242     www      6476:            return &unescape($reply);
                   6477: 	}
1.240     www      6478:     }
1.242     www      6479:     return 'timeout:'.$queryid;
1.240     www      6480: }
                   6481: 
                   6482: sub courselog_query {
1.241     www      6483: #
                   6484: # possible filters:
                   6485: # url: url or symb
                   6486: # username
                   6487: # domain
                   6488: # action: view, submit, grade
                   6489: # start: timestamp
                   6490: # end: timestamp
                   6491: #
1.240     www      6492:     my (%filters)=@_;
1.620     albertel 6493:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      6494:     if ($filters{'url'}) {
                   6495: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   6496:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   6497:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   6498:     }
1.620     albertel 6499:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6500:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      6501:     return &log_query($cname,$cdom,'courselog',%filters);
                   6502: }
                   6503: 
                   6504: sub userlog_query {
1.858     raeburn  6505: #
                   6506: # possible filters:
                   6507: # action: log check role
                   6508: # start: timestamp
                   6509: # end: timestamp
                   6510: #
1.240     www      6511:     my ($uname,$udom,%filters)=@_;
                   6512:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      6513: }
                   6514: 
1.506     raeburn  6515: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   6516: 
                   6517: sub auto_run {
1.508     raeburn  6518:     my ($cnum,$cdom) = @_;
1.876     raeburn  6519:     my $response = 0;
                   6520:     my $settings;
                   6521:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   6522:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   6523:         $settings = $domconfig{'autoenroll'};
                   6524:         if ($settings->{'run'} eq '1') {
                   6525:             $response = 1;
                   6526:         }
                   6527:     } else {
1.934     raeburn  6528:         my $homeserver;
                   6529:         if (&is_course($cdom,$cnum)) {
                   6530:             $homeserver = &homeserver($cnum,$cdom);
                   6531:         } else {
                   6532:             $homeserver = &domain($cdom,'primary');
                   6533:         }
                   6534:         if ($homeserver ne 'no_host') {
                   6535:             $response = &reply('autorun:'.$cdom,$homeserver);
                   6536:         }
1.876     raeburn  6537:     }
1.506     raeburn  6538:     return $response;
                   6539: }
1.776     albertel 6540: 
1.506     raeburn  6541: sub auto_get_sections {
1.508     raeburn  6542:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  6543:     my $homeserver;
                   6544:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   6545:         $homeserver = &homeserver($cnum,$cdom);
                   6546:     }
                   6547:     if (!defined($homeserver)) { 
                   6548:         if ($cdom =~ /^$match_domain$/) {
                   6549:             $homeserver = &domain($cdom,'primary');
                   6550:         }
                   6551:     }
                   6552:     my @secs;
                   6553:     if (defined($homeserver)) {
                   6554:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   6555:         unless ($response eq 'refused') {
                   6556:             @secs = split(/:/,$response);
                   6557:         }
1.506     raeburn  6558:     }
                   6559:     return @secs;
                   6560: }
1.776     albertel 6561: 
1.506     raeburn  6562: sub auto_new_course {
1.1099    raeburn  6563:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  6564:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  6565:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  6566:     return $response;
                   6567: }
1.776     albertel 6568: 
1.506     raeburn  6569: sub auto_validate_courseID {
1.508     raeburn  6570:     my ($cnum,$cdom,$inst_course_id) = @_;
                   6571:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  6572:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  6573:     return $response;
                   6574: }
1.776     albertel 6575: 
1.1007    raeburn  6576: sub auto_validate_instcode {
1.1020    raeburn  6577:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  6578:     my ($homeserver,$response);
                   6579:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6580:         $homeserver = &homeserver($cnum,$cdom);
                   6581:     }
                   6582:     if (!defined($homeserver)) {
                   6583:         if ($cdom =~ /^$match_domain$/) {
                   6584:             $homeserver = &domain($cdom,'primary');
                   6585:         }
                   6586:     }
1.1065    raeburn  6587:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   6588:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1027    raeburn  6589:     my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
                   6590:     return ($outcome,$description);
1.1007    raeburn  6591: }
                   6592: 
1.506     raeburn  6593: sub auto_create_password {
1.873     raeburn  6594:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   6595:     my ($homeserver,$response);
1.506     raeburn  6596:     my $create_passwd = 0;
                   6597:     my $authchk = '';
1.873     raeburn  6598:     if ($udom =~ /^$match_domain$/) {
                   6599:         $homeserver = &domain($udom,'primary');
                   6600:     }
                   6601:     if ($homeserver eq '') {
                   6602:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6603:             $homeserver = &homeserver($cnum,$cdom);
                   6604:         }
                   6605:     }
                   6606:     if ($homeserver eq '') {
                   6607:         $authchk = 'nodomain';
1.506     raeburn  6608:     } else {
1.873     raeburn  6609:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   6610:         if ($response eq 'refused') {
                   6611:             $authchk = 'refused';
                   6612:         } else {
1.901     albertel 6613:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  6614:         }
1.506     raeburn  6615:     }
                   6616:     return ($authparam,$create_passwd,$authchk);
                   6617: }
                   6618: 
1.706     raeburn  6619: sub auto_photo_permission {
                   6620:     my ($cnum,$cdom,$students) = @_;
                   6621:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 6622:     my ($outcome,$perm_reqd,$conditions) = 
                   6623: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 6624:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6625: 	return (undef,undef);
                   6626:     }
1.706     raeburn  6627:     return ($outcome,$perm_reqd,$conditions);
                   6628: }
                   6629: 
                   6630: sub auto_checkphotos {
                   6631:     my ($uname,$udom,$pid) = @_;
                   6632:     my $homeserver = &homeserver($uname,$udom);
                   6633:     my ($result,$resulttype);
                   6634:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 6635: 				   &escape($uname).':'.&escape($pid),
                   6636: 				   $homeserver));
1.709     albertel 6637:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6638: 	return (undef,undef);
                   6639:     }
1.706     raeburn  6640:     if ($outcome) {
                   6641:         ($result,$resulttype) = split(/:/,$outcome);
                   6642:     } 
                   6643:     return ($result,$resulttype);
                   6644: }
                   6645: 
                   6646: sub auto_photochoice {
                   6647:     my ($cnum,$cdom) = @_;
                   6648:     my $homeserver = &homeserver($cnum,$cdom);
                   6649:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 6650: 						       &escape($cdom),
                   6651: 						       $homeserver)));
1.709     albertel 6652:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6653: 	return (undef,undef);
                   6654:     }
1.706     raeburn  6655:     return ($update,$comment);
                   6656: }
                   6657: 
                   6658: sub auto_photoupdate {
                   6659:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   6660:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 6661:     my $host=&hostname($homeserver);
1.706     raeburn  6662:     my $cmd = '';
                   6663:     my $maxtries = 1;
1.800     albertel 6664:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   6665:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  6666:     }
                   6667:     $cmd =~ s/%%$//;
                   6668:     $cmd = &escape($cmd);
                   6669:     my $query = 'institutionalphotos';
                   6670:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   6671:     unless ($queryid=~/^\Q$host\E\_/) {
                   6672:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   6673:         return 'error: '.$queryid;
                   6674:     }
                   6675:     my $reply = &get_query_reply($queryid);
                   6676:     my $tries = 1;
                   6677:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   6678:         $reply = &get_query_reply($queryid);
                   6679:         $tries ++;
                   6680:     }
                   6681:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6682:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   6683:     } else {
                   6684:         my @responses = split(/:/,$reply);
                   6685:         my $outcome = shift(@responses); 
                   6686:         foreach my $item (@responses) {
                   6687:             my ($key,$value) = split(/=/,$item);
                   6688:             $$photo{$key} = $value;
                   6689:         }
                   6690:         return $outcome;
                   6691:     }
                   6692:     return 'error';
                   6693: }
                   6694: 
1.521     raeburn  6695: sub auto_instcode_format {
1.793     albertel 6696:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   6697: 	$cat_order) = @_;
1.521     raeburn  6698:     my $courses = '';
1.772     raeburn  6699:     my @homeservers;
1.521     raeburn  6700:     if ($caller eq 'global') {
1.841     albertel 6701: 	my %servers = &get_servers($codedom,'library');
                   6702: 	foreach my $tryserver (keys(%servers)) {
                   6703: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6704: 		push(@homeservers,$tryserver);
                   6705: 	    }
1.584     raeburn  6706:         }
1.1022    raeburn  6707:     } elsif ($caller eq 'requests') {
                   6708:         if ($codedom =~ /^$match_domain$/) {
                   6709:             my $chome = &domain($codedom,'primary');
                   6710:             unless ($chome eq 'no_host') {
                   6711:                 push(@homeservers,$chome);
                   6712:             }
                   6713:         }
1.521     raeburn  6714:     } else {
1.772     raeburn  6715:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  6716:     }
1.793     albertel 6717:     foreach my $code (keys(%{$instcodes})) {
                   6718:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  6719:     }
                   6720:     chop($courses);
1.772     raeburn  6721:     my $ok_response = 0;
                   6722:     my $response;
                   6723:     while (@homeservers > 0 && $ok_response == 0) {
                   6724:         my $server = shift(@homeservers); 
                   6725:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   6726:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   6727:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 6728: 		split(/:/,$response);
1.772     raeburn  6729:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   6730:             push(@{$codetitles},&str2array($codetitles_str));
                   6731:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   6732:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   6733:             $ok_response = 1;
                   6734:         }
                   6735:     }
                   6736:     if ($ok_response) {
1.521     raeburn  6737:         return 'ok';
1.772     raeburn  6738:     } else {
                   6739:         return $response;
1.521     raeburn  6740:     }
                   6741: }
                   6742: 
1.792     raeburn  6743: sub auto_instcode_defaults {
                   6744:     my ($domain,$returnhash,$code_order) = @_;
                   6745:     my @homeservers;
1.841     albertel 6746: 
                   6747:     my %servers = &get_servers($domain,'library');
                   6748:     foreach my $tryserver (keys(%servers)) {
                   6749: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6750: 	    push(@homeservers,$tryserver);
                   6751: 	}
1.792     raeburn  6752:     }
1.841     albertel 6753: 
1.792     raeburn  6754:     my $response;
1.841     albertel 6755:     foreach my $server (@homeservers) {
1.792     raeburn  6756:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 6757:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   6758: 	
                   6759: 	foreach my $pair (split(/\&/,$response)) {
                   6760: 	    my ($name,$value)=split(/\=/,$pair);
                   6761: 	    if ($name eq 'code_order') {
                   6762: 		@{$code_order} = split(/\&/,&unescape($value));
                   6763: 	    } else {
                   6764: 		$returnhash->{&unescape($name)}=&unescape($value);
                   6765: 	    }
                   6766: 	}
                   6767: 	return 'ok';
1.792     raeburn  6768:     }
1.841     albertel 6769: 
                   6770:     return $response;
1.1003    raeburn  6771: }
                   6772: 
                   6773: sub auto_possible_instcodes {
1.1007    raeburn  6774:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   6775:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   6776:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   6777:         return;
                   6778:     }
1.1003    raeburn  6779:     my (@homeservers,$uhome);
                   6780:     if (defined(&domain($domain,'primary'))) {
                   6781:         $uhome=&domain($domain,'primary');
                   6782:         push(@homeservers,&domain($domain,'primary'));
                   6783:     } else {
                   6784:         my %servers = &get_servers($domain,'library');
                   6785:         foreach my $tryserver (keys(%servers)) {
                   6786:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6787:                 push(@homeservers,$tryserver);
                   6788:             }
                   6789:         }
                   6790:     }
                   6791:     my $response;
                   6792:     foreach my $server (@homeservers) {
                   6793:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   6794:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  6795:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   6796:             split(':',$response);
                   6797:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   6798:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  6799:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  6800:             my ($name,$value)=split('=',$item);
                   6801:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6802:         }
                   6803:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  6804:             my ($name,$value)=split('=',$item);
                   6805:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6806:         }
                   6807:         return 'ok';
                   6808:     }
                   6809:     return $response;
                   6810: }
1.792     raeburn  6811: 
1.1010    raeburn  6812: sub auto_courserequest_checks {
                   6813:     my ($dom) = @_;
1.1020    raeburn  6814:     my ($homeserver,%validations);
                   6815:     if ($dom =~ /^$match_domain$/) {
                   6816:         $homeserver = &domain($dom,'primary');
                   6817:     }
                   6818:     unless ($homeserver eq 'no_host') {
                   6819:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   6820:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   6821:             my @items = split(/&/,$response);
                   6822:             foreach my $item (@items) {
                   6823:                 my ($key,$value) = split('=',$item);
                   6824:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   6825:             }
                   6826:         }
                   6827:     }
1.1010    raeburn  6828:     return %validations; 
                   6829: }
                   6830: 
1.1020    raeburn  6831: sub auto_courserequest_validation {
                   6832:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
                   6833:     my ($homeserver,$response);
                   6834:     if ($dom =~ /^$match_domain$/) {
                   6835:         $homeserver = &domain($dom,'primary');
                   6836:     }
                   6837:     unless ($homeserver eq 'no_host') {  
1.1021    raeburn  6838:           
1.1020    raeburn  6839:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  6840:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020    raeburn  6841:                                     ':'.&escape($instcode).':'.&escape($instseclist),
                   6842:                                     $homeserver));
                   6843:     }
                   6844:     return $response;
                   6845: }
                   6846: 
1.777     albertel 6847: sub auto_validate_class_sec {
1.918     raeburn  6848:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  6849:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  6850:     my $ownerlist;
                   6851:     if (ref($owners) eq 'ARRAY') {
                   6852:         $ownerlist = join(',',@{$owners});
                   6853:     } else {
                   6854:         $ownerlist = $owners;
                   6855:     }
1.773     raeburn  6856:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  6857:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  6858:     return $response;
                   6859: }
                   6860: 
1.679     raeburn  6861: # ------------------------------------------------------- Course Group routines
                   6862: 
                   6863: sub get_coursegroups {
1.809     raeburn  6864:     my ($cdom,$cnum,$group,$namespace) = @_;
                   6865:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  6866: }
                   6867: 
1.679     raeburn  6868: sub modify_coursegroup {
                   6869:     my ($cdom,$cnum,$groupsettings) = @_;
                   6870:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   6871: }
                   6872: 
1.809     raeburn  6873: sub toggle_coursegroup_status {
                   6874:     my ($cdom,$cnum,$group,$action) = @_;
                   6875:     my ($from_namespace,$to_namespace);
                   6876:     if ($action eq 'delete') {
                   6877:         $from_namespace = 'coursegroups';
                   6878:         $to_namespace = 'deleted_groups';
                   6879:     } else {
                   6880:         $from_namespace = 'deleted_groups';
                   6881:         $to_namespace = 'coursegroups';
                   6882:     }
                   6883:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  6884:     if (my $tmp = &error(%curr_group)) {
                   6885:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   6886:         return ('read error',$tmp);
                   6887:     } else {
                   6888:         my %savedsettings = %curr_group; 
1.809     raeburn  6889:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  6890:         my $deloutcome;
                   6891:         if ($result eq 'ok') {
1.809     raeburn  6892:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  6893:         } else {
                   6894:             return ('write error',$result);
                   6895:         }
                   6896:         if ($deloutcome eq 'ok') {
                   6897:             return 'ok';
                   6898:         } else {
                   6899:             return ('delete error',$deloutcome);
                   6900:         }
                   6901:     }
                   6902: }
                   6903: 
1.679     raeburn  6904: sub modify_group_roles {
1.957     raeburn  6905:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  6906:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   6907:     my $role = 'gr/'.&escape($userprivs);
                   6908:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  6909:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  6910:     if ($result eq 'ok') {
                   6911:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   6912:     }
1.679     raeburn  6913:     return $result;
                   6914: }
                   6915: 
                   6916: sub modify_coursegroup_membership {
                   6917:     my ($cdom,$cnum,$membership) = @_;
                   6918:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   6919:     return $result;
                   6920: }
                   6921: 
1.682     raeburn  6922: sub get_active_groups {
                   6923:     my ($udom,$uname,$cdom,$cnum) = @_;
                   6924:     my $now = time;
                   6925:     my %groups = ();
                   6926:     foreach my $key (keys(%env)) {
1.811     albertel 6927:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  6928:             my ($start,$end) = split(/\./,$env{$key});
                   6929:             if (($end!=0) && ($end<$now)) { next; }
                   6930:             if (($start!=0) && ($start>$now)) { next; }
                   6931:             if ($1 eq $cdom && $2 eq $cnum) {
                   6932:                 $groups{$3} = $env{$key} ;
                   6933:             }
                   6934:         }
                   6935:     }
                   6936:     return %groups;
                   6937: }
                   6938: 
1.683     raeburn  6939: sub get_group_membership {
                   6940:     my ($cdom,$cnum,$group) = @_;
                   6941:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   6942: }
                   6943: 
                   6944: sub get_users_groups {
                   6945:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  6946:     my @usersgroups;
1.683     raeburn  6947:     my $cachetime=1800;
                   6948: 
                   6949:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  6950:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   6951:     if (defined($cached)) {
1.734     albertel 6952:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  6953:     } else {  
                   6954:         $grouplist = '';
1.816     raeburn  6955:         my $courseurl = &courseid_to_courseurl($courseid);
1.1086    raeburn  6956:         my $extra = &freeze_escape({'skipcheck' => 1});
                   6957:         my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817     raeburn  6958:         my $access_end = $env{'course.'.$courseid.
                   6959:                               '.default_enrollment_end_date'};
                   6960:         my $now = time;
                   6961:         foreach my $key (keys(%roleshash)) {
                   6962:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   6963:                 my $group = $1;
                   6964:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   6965:                     my $start = $2;
                   6966:                     my $end = $1;
                   6967:                     if ($start == -1) { next; } # deleted from group
                   6968:                     if (($start!=0) && ($start>$now)) { next; }
                   6969:                     if (($end!=0) && ($end<$now)) {
                   6970:                         if ($access_end && $access_end < $now) {
                   6971:                             if ($access_end - $end < 86400) {
                   6972:                                 push(@usersgroups,$group);
1.733     raeburn  6973:                             }
                   6974:                         }
1.817     raeburn  6975:                         next;
1.733     raeburn  6976:                     }
1.817     raeburn  6977:                     push(@usersgroups,$group);
1.683     raeburn  6978:                 }
                   6979:             }
                   6980:         }
1.817     raeburn  6981:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   6982:         $grouplist = join(':',@usersgroups);
                   6983:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  6984:     }
1.733     raeburn  6985:     return @usersgroups;
1.683     raeburn  6986: }
                   6987: 
                   6988: sub devalidate_getgroups_cache {
                   6989:     my ($udom,$uname,$cdom,$cnum)=@_;
                   6990:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 6991: 
1.683     raeburn  6992:     my $hashid="$udom:$uname:$courseid";
                   6993:     &devalidate_cache_new('getgroups',$hashid);
                   6994: }
                   6995: 
1.12      www      6996: # ------------------------------------------------------------------ Plain Text
                   6997: 
                   6998: sub plaintext {
1.988     raeburn  6999:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  7000:     if ($short =~ m{^cr/}) {
1.758     albertel 7001: 	return (split('/',$short))[-1];
                   7002:     }
1.742     raeburn  7003:     if (!defined($cid)) {
                   7004:         $cid = $env{'request.course.id'};
                   7005:     }
                   7006:     my %rolenames = (
1.1008    raeburn  7007:                       Course    => 'std',
                   7008:                       Community => 'alt1',
1.742     raeburn  7009:                     );
1.1037    raeburn  7010:     if ($cid ne '') {
                   7011:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   7012:             unless ($forcedefault) {
                   7013:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   7014:                 &Apache::lonlocal::mt_escape(\$roletext);
                   7015:                 return &Apache::lonlocal::mt($roletext);
                   7016:             }
                   7017:         }
                   7018:     }
                   7019:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   7020:         (defined($rolenames{$type})) && 
                   7021:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  7022:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  7023:     } elsif ($cid ne '') {
                   7024:         my $crstype = $env{'course.'.$cid.'.type'};
                   7025:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   7026:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   7027:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   7028:         }
1.742     raeburn  7029:     }
1.1037    raeburn  7030:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      7031: }
                   7032: 
                   7033: # ----------------------------------------------------------------- Assign Role
                   7034: 
                   7035: sub assignrole {
1.957     raeburn  7036:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   7037:         $context)=@_;
1.21      www      7038:     my $mrole;
                   7039:     if ($role =~ /^cr\//) {
1.393     www      7040:         my $cwosec=$url;
1.811     albertel 7041:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      7042: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  7043:            my $refused = 1;
                   7044:            if ($context eq 'requestcourses') {
                   7045:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   7046:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   7047:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   7048:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   7049:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7050:                            if ($crsenv{'internal.courseowner'} eq
                   7051:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   7052:                                $refused = '';
                   7053:                            }
                   7054:                        }
                   7055:                    }
                   7056:                }
                   7057:            }
                   7058:            if ($refused) {
                   7059:                &logthis('Refused custom assignrole: '.
                   7060:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   7061:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   7062:                return 'refused';
                   7063:            }
1.104     www      7064:         }
1.21      www      7065:         $mrole='cr';
1.678     raeburn  7066:     } elsif ($role =~ /^gr\//) {
                   7067:         my $cwogrp=$url;
1.811     albertel 7068:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  7069:         unless (&allowed('mdg',$cwogrp)) {
                   7070:             &logthis('Refused group assignrole: '.
                   7071:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   7072:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   7073:             return 'refused';
                   7074:         }
                   7075:         $mrole='gr';
1.21      www      7076:     } else {
1.82      www      7077:         my $cwosec=$url;
1.811     albertel 7078:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  7079:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   7080:             my $refused;
                   7081:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   7082:                 if (!(&allowed('c'.$role,$url))) {
                   7083:                     $refused = 1;
                   7084:                 }
                   7085:             } else {
                   7086:                 $refused = 1;
                   7087:             }
1.947     raeburn  7088:             if ($refused) {
1.1045    raeburn  7089:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   7090:                 if (!$selfenroll && $context eq 'course') {
                   7091:                     my %crsenv;
                   7092:                     if ($role eq 'cc' || $role eq 'co') {
                   7093:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7094:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   7095:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   7096:                                 if ($crsenv{'internal.courseowner'} eq 
                   7097:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   7098:                                     $refused = '';
                   7099:                                 }
                   7100:                             }
                   7101:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   7102:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   7103:                                 if ($crsenv{'internal.courseowner'} eq 
                   7104:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   7105:                                     $refused = '';
                   7106:                                 }
                   7107:                             }
                   7108:                         }
                   7109:                     }
                   7110:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947     raeburn  7111:                     $refused = '';
1.1017    raeburn  7112:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  7113:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  7114:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  7115:                         my $wrongcc;
                   7116:                         if ($cnum =~ /^$match_community$/) {
                   7117:                             $wrongcc = 1 if ($role eq 'cc');
                   7118:                         } else {
                   7119:                             $wrongcc = 1 if ($role eq 'co');
                   7120:                         }
                   7121:                         unless ($wrongcc) {
                   7122:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7123:                             if ($crsenv{'internal.courseowner'} eq 
                   7124:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   7125:                                 $refused = '';
                   7126:                             }
1.1017    raeburn  7127:                         }
                   7128:                     }
                   7129:                 }
                   7130:                 if ($refused) {
1.947     raeburn  7131:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   7132:                              ' '.$role.' '.$end.' '.$start.' by '.
                   7133: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   7134:                     return 'refused';
                   7135:                 }
1.932     raeburn  7136:             }
1.1131    raeburn  7137:         } elsif ($role eq 'au') {
                   7138:             if ($url ne '/'.$udom.'/') {
                   7139:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   7140:                          ' to assign author role for '.$uname.':'.$udom.
                   7141:                          ' in domain: '.$url.' refused (wrong domain).');
                   7142:                 return 'refused';
                   7143:             }
1.104     www      7144:         }
1.21      www      7145:         $mrole=$role;
                   7146:     }
1.620     albertel 7147:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      7148:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      7149:     if ($end) { $command.='_'.$end; }
1.21      www      7150:     if ($start) {
                   7151: 	if ($end) { 
1.81      www      7152:            $command.='_'.$start; 
1.21      www      7153:         } else {
1.81      www      7154:            $command.='_0_'.$start;
1.21      www      7155:         }
                   7156:     }
1.739     raeburn  7157:     my $origstart = $start;
                   7158:     my $origend = $end;
1.957     raeburn  7159:     my $delflag;
1.357     www      7160: # actually delete
                   7161:     if ($deleteflag) {
1.373     www      7162: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      7163: # modify command to delete the role
1.620     albertel 7164:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      7165:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 7166: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      7167: # set start and finish to negative values for userrolelog
                   7168:            $start=-1;
                   7169:            $end=-1;
1.957     raeburn  7170:            $delflag = 1;
1.357     www      7171:         }
                   7172:     }
                   7173: # send command
1.349     www      7174:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      7175: # log new user role if status is ok
1.349     www      7176:     if ($answer eq 'ok') {
1.663     raeburn  7177: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  7178: # for course roles, perform group memberships changes triggered by role change.
1.957     raeburn  7179:         &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739     raeburn  7180:         unless ($role =~ /^gr/) {
                   7181:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957     raeburn  7182:                                              $origstart,$selfenroll,$context);
1.739     raeburn  7183:         }
1.1053    raeburn  7184:         if ($role eq 'cc') {
                   7185:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   7186:         }
1.349     www      7187:     }
                   7188:     return $answer;
1.169     harris41 7189: }
                   7190: 
1.1053    raeburn  7191: sub autoupdate_coowners {
                   7192:     my ($url,$end,$start,$uname,$udom) = @_;
                   7193:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   7194:     if (($cdom ne '') && ($cnum ne '')) {
                   7195:         my $now = time;
                   7196:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   7197:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   7198:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   7199:             my $instcode = $coursehash{'internal.coursecode'};
                   7200:             if ($instcode ne '') {
1.1056    raeburn  7201:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   7202:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  7203:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  7204:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   7205:                         if ($result eq 'valid') {
                   7206:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  7207:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   7208:                                     push(@newcoowners,$coowner);
                   7209:                                 }
                   7210:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   7211:                                     push(@newcoowners,$uname.':'.$udom);
                   7212:                                 }
                   7213:                                 @newcoowners = sort(@newcoowners);
                   7214:                             } else {
                   7215:                                 push(@newcoowners,$uname.':'.$udom);
                   7216:                             }
                   7217:                         } else {
                   7218:                             if ($coursehash{'internal.co-owners'}) {
                   7219:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   7220:                                     unless ($coowner eq $uname.':'.$udom) {
                   7221:                                         push(@newcoowners,$coowner);
                   7222:                                     }
                   7223:                                 }
                   7224:                                 unless (@newcoowners > 0) {
                   7225:                                     $delcoowners = 1;
                   7226:                                     $coowners = '';
                   7227:                                 }
                   7228:                             }
                   7229:                         }
                   7230:                         if (@newcoowners || $delcoowners) {
                   7231:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   7232:                                             $delcoowners,@newcoowners);
                   7233:                         }
                   7234:                     }
                   7235:                 }
                   7236:             }
                   7237:         }
                   7238:     }
                   7239: }
                   7240: 
                   7241: sub store_coowners {
                   7242:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   7243:     my $cid = $cdom.'_'.$cnum;
                   7244:     my ($coowners,$delresult,$putresult);
                   7245:     if (@newcoowners) {
                   7246:         $coowners = join(',',@newcoowners);
                   7247:         my %coownershash = (
                   7248:                             'internal.co-owners' => $coowners,
                   7249:                            );
                   7250:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   7251:         if ($putresult eq 'ok') {
                   7252:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   7253:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   7254:             }
                   7255:         }
                   7256:     }
                   7257:     if ($delcoowners) {
                   7258:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   7259:         if ($delresult eq 'ok') {
                   7260:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   7261:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   7262:             }
                   7263:         }
                   7264:     }
                   7265:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   7266:         my %crsinfo =
                   7267:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   7268:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   7269:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   7270:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   7271:         }
                   7272:     }
                   7273: }
                   7274: 
1.169     harris41 7275: # -------------------------------------------------- Modify user authentication
1.197     www      7276: # Overrides without validation
                   7277: 
1.169     harris41 7278: sub modifyuserauth {
                   7279:     my ($udom,$uname,$umode,$upass)=@_;
                   7280:     my $uhome=&homeserver($uname,$udom);
1.197     www      7281:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   7282:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 7283:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   7284:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 7285:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   7286: 		     &escape($upass),$uhome);
1.620     albertel 7287:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      7288:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   7289:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   7290:     &log($udom,,$uname,$uhome,
1.620     albertel 7291:         'Authentication changed by '.$env{'user.domain'}.', '.
                   7292:                                      $env{'user.name'}.', '.$umode.
1.197     www      7293:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 7294:     unless ($reply eq 'ok') {
1.197     www      7295:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 7296: 	return 'error: '.$reply;
                   7297:     }   
1.170     harris41 7298:     return 'ok';
1.80      www      7299: }
                   7300: 
1.81      www      7301: # --------------------------------------------------------------- Modify a user
1.80      www      7302: 
1.81      www      7303: sub modifyuser {
1.206     matthew  7304:     my ($udom,    $uname, $uid,
                   7305:         $umode,   $upass, $first,
                   7306:         $middle,  $last,  $gene,
1.1058    raeburn  7307:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 7308:     $udom= &LONCAPA::clean_domain($udom);
                   7309:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  7310:     my $showcandelete = 'none';
                   7311:     if (ref($candelete) eq 'ARRAY') {
                   7312:         if (@{$candelete} > 0) {
                   7313:             $showcandelete = join(', ',@{$candelete});
                   7314:         }
                   7315:     }
1.81      www      7316:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      7317:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  7318: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  7319:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   7320:                                      ' desiredhome not specified'). 
1.620     albertel 7321:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   7322:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 7323:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  7324:     my $newuser;
                   7325:     if ($uhome eq 'no_host') {
                   7326:         $newuser = 1;
                   7327:     }
1.80      www      7328: # ----------------------------------------------------------------- Create User
1.406     albertel 7329:     if (($uhome eq 'no_host') && 
                   7330: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      7331:         my $unhome='';
1.844     albertel 7332:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  7333:             $unhome = $desiredhome;
1.620     albertel 7334: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   7335: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  7336:         } else { # load balancing routine for determining $unhome
1.81      www      7337:             my $loadm=10000000;
1.841     albertel 7338: 	    my %servers = &get_servers($udom,'library');
                   7339: 	    foreach my $tryserver (keys(%servers)) {
                   7340: 		my $answer=reply('load',$tryserver);
                   7341: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   7342: 		    $loadm=$answer;
                   7343: 		    $unhome=$tryserver;
                   7344: 		}
1.80      www      7345: 	    }
                   7346:         }
                   7347:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  7348: 	    return 'error: unable to find a home server for '.$uname.
                   7349:                    ' in domain '.$udom;
1.80      www      7350:         }
                   7351:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   7352:                          &escape($upass),$unhome);
                   7353: 	unless ($reply eq 'ok') {
                   7354:             return 'error: '.$reply;
                   7355:         }   
1.230     stredwic 7356:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      7357:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  7358: 	    return 'error: unable verify users home machine.';
1.80      www      7359:         }
1.209     matthew  7360:     }   # End of creation of new user
1.80      www      7361: # ---------------------------------------------------------------------- Add ID
                   7362:     if ($uid) {
                   7363:        $uid=~tr/A-Z/a-z/;
                   7364:        my %uidhash=&idrget($udom,$uname);
1.196     www      7365:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   7366:          && (!$forceid)) {
1.80      www      7367: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  7368: 	      return 'error: user id "'.$uid.'" does not match '.
                   7369:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      7370:           }
                   7371:        } else {
                   7372: 	  &idput($udom,($uname => $uid));
                   7373:        }
                   7374:     }
                   7375: # -------------------------------------------------------------- Add names, etc
1.313     matthew  7376:     my @tmp=&get('environment',
1.899     raeburn  7377: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  7378:                     'permanentemail','inststatus'],
1.134     albertel 7379: 		   $udom,$uname);
1.1075    raeburn  7380:     my (%names,%oldnames);
1.313     matthew  7381:     if ($tmp[0] =~ m/^error:.*/) { 
                   7382:         %names=(); 
                   7383:     } else {
                   7384:         %names = @tmp;
1.1075    raeburn  7385:         %oldnames = %names;
1.313     matthew  7386:     }
1.388     www      7387: #
1.1058    raeburn  7388: # If name, email and/or uid are blank (e.g., because an uploaded file
                   7389: # of users did not contain them), do not overwrite existing values
                   7390: # unless field is in $candelete array ref.  
                   7391: #
                   7392: 
                   7393:     my @fields = ('firstname','middlename','lastname','generation',
                   7394:                   'permanentemail','id');
                   7395:     my %newvalues;
                   7396:     if (ref($candelete) eq 'ARRAY') {
                   7397:         foreach my $field (@fields) {
                   7398:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   7399:                 if ($field eq 'firstname') {
                   7400:                     $names{$field} = $first;
                   7401:                 } elsif ($field eq 'middlename') {
                   7402:                     $names{$field} = $middle;
                   7403:                 } elsif ($field eq 'lastname') {
                   7404:                     $names{$field} = $last;
                   7405:                 } elsif ($field eq 'generation') { 
                   7406:                     $names{$field} = $gene;
                   7407:                 } elsif ($field eq 'permanentemail') {
                   7408:                     $names{$field} = $email;
                   7409:                 } elsif ($field eq 'id') {
                   7410:                     $names{$field}  = $uid;
                   7411:                 }
                   7412:             }
                   7413:         }
                   7414:     }
1.388     www      7415:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  7416:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      7417:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  7418:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      7419:     if ($email) {
                   7420:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  7421:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      7422:     }
1.899     raeburn  7423:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  7424:     if (defined($inststatus)) {
                   7425:         $names{'inststatus'} = '';
                   7426:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   7427:         if (ref($usertypes) eq 'HASH') {
                   7428:             my @okstatuses; 
                   7429:             foreach my $item (split(/:/,$inststatus)) {
                   7430:                 if (defined($usertypes->{$item})) {
                   7431:                     push(@okstatuses,$item);  
                   7432:                 }
                   7433:             }
                   7434:             if (@okstatuses) {
                   7435:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   7436:             }
                   7437:         }
                   7438:     }
1.1075    raeburn  7439:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  7440:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  7441:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  7442:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   7443:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   7444:     } else {
                   7445:         $logmsg .= ' during self creation';
                   7446:     }
1.1075    raeburn  7447:     my $changed;
                   7448:     if ($newuser) {
                   7449:         $changed = 1;
                   7450:     } else {
                   7451:         foreach my $field (@fields) {
                   7452:             if ($names{$field} ne $oldnames{$field}) {
                   7453:                 $changed = 1;
                   7454:                 last;
                   7455:             }
                   7456:         }
                   7457:     }
                   7458:     unless ($changed) {
                   7459:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   7460:         &logthis($logmsg);
                   7461:         return 'ok';
                   7462:     }
                   7463:     my $reply = &put('environment', \%names, $udom,$uname);
                   7464:     if ($reply ne 'ok') { 
                   7465:         return 'error: '.$reply;
                   7466:     }
1.1087    raeburn  7467:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   7468:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   7469:     }
1.1075    raeburn  7470:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   7471:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   7472:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  7473:     &logthis($logmsg);
1.134     albertel 7474:     return 'ok';
1.80      www      7475: }
                   7476: 
1.81      www      7477: # -------------------------------------------------------------- Modify student
1.80      www      7478: 
1.81      www      7479: sub modifystudent {
                   7480:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  7481:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990     raeburn  7482:         $selfenroll,$context,$inststatus)=@_;
1.455     albertel 7483:     if (!$cid) {
1.620     albertel 7484: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7485: 	    return 'not_in_class';
                   7486: 	}
1.80      www      7487:     }
                   7488: # --------------------------------------------------------------- Make the user
1.81      www      7489:     my $reply=&modifyuser
1.209     matthew  7490: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  7491:          $desiredhome,$email,$inststatus);
1.80      www      7492:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  7493:     # This will cause &modify_student_enrollment to get the uid from the
                   7494:     # students environment
                   7495:     $uid = undef if (!$forceid);
1.455     albertel 7496:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957     raeburn  7497: 					$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297     matthew  7498:     return $reply;
                   7499: }
                   7500: 
                   7501: sub modify_student_enrollment {
1.957     raeburn  7502:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455     albertel 7503:     my ($cdom,$cnum,$chome);
                   7504:     if (!$cid) {
1.620     albertel 7505: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7506: 	    return 'not_in_class';
                   7507: 	}
1.620     albertel 7508: 	$cdom=$env{'course.'.$cid.'.domain'};
                   7509: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 7510:     } else {
                   7511: 	($cdom,$cnum)=split(/_/,$cid);
                   7512:     }
1.620     albertel 7513:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 7514:     if (!$chome) {
1.457     raeburn  7515: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  7516:     }
1.455     albertel 7517:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  7518:     # Make sure the user exists
1.81      www      7519:     my $uhome=&homeserver($uname,$udom);
                   7520:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7521: 	return 'error: no such user';
                   7522:     }
1.297     matthew  7523:     # Get student data if we were not given enough information
                   7524:     if (!defined($first)  || $first  eq '' || 
                   7525:         !defined($last)   || $last   eq '' || 
                   7526:         !defined($uid)    || $uid    eq '' || 
                   7527:         !defined($middle) || $middle eq '' || 
                   7528:         !defined($gene)   || $gene   eq '') {
1.294     matthew  7529:         # They did not supply us with enough data to enroll the student, so
                   7530:         # we need to pick up more information.
1.297     matthew  7531:         my %tmp = &get('environment',
1.294     matthew  7532:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  7533:                        ,$udom,$uname);
                   7534: 
1.800     albertel 7535:         #foreach my $key (keys(%tmp)) {
                   7536:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 7537:         #}
1.294     matthew  7538:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   7539:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   7540:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  7541:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  7542:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   7543:     }
1.556     albertel 7544:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  7545:     my $user = "$uname:$udom";
                   7546:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 7547:     my $reply=cput('classlist',
1.1148    raeburn  7548: 		   {$user => 
1.515     raeburn  7549: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 7550: 		   $cdom,$cnum);
1.1148    raeburn  7551:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   7552:         &devalidate_getsection_cache($udom,$uname,$cid);
                   7553:     } else { 
1.81      www      7554: 	return 'error: '.$reply;
                   7555:     }
1.297     matthew  7556:     # Add student role to user
1.83      www      7557:     my $uurl='/'.$cid;
1.81      www      7558:     $uurl=~s/\_/\//g;
                   7559:     if ($usec) {
                   7560: 	$uurl.='/'.$usec;
                   7561:     }
1.1148    raeburn  7562:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   7563:                              $selfenroll,$context);
                   7564:     if ($result ne 'ok') {
                   7565:         if ($old_entry{$user} ne '') {
                   7566:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   7567:         } else {
                   7568:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   7569:         }
                   7570:     }
                   7571:     return $result; 
1.21      www      7572: }
                   7573: 
1.556     albertel 7574: sub format_name {
                   7575:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   7576:     my $name;
                   7577:     if ($first ne 'lastname') {
                   7578: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   7579:     } else {
                   7580: 	if ($lastname=~/\S/) {
                   7581: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   7582: 	    $name=~s/\s+,/,/;
                   7583: 	} else {
                   7584: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   7585: 	}
                   7586:     }
                   7587:     $name=~s/^\s+//;
                   7588:     $name=~s/\s+$//;
                   7589:     $name=~s/\s+/ /g;
                   7590:     return $name;
                   7591: }
                   7592: 
1.84      www      7593: # ------------------------------------------------- Write to course preferences
                   7594: 
                   7595: sub writecoursepref {
                   7596:     my ($courseid,%prefs)=@_;
                   7597:     $courseid=~s/^\///;
                   7598:     $courseid=~s/\_/\//g;
                   7599:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   7600:     my $chome=homeserver($cnum,$cdomain);
                   7601:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   7602: 	return 'error: no such course';
                   7603:     }
                   7604:     my $cstring='';
1.800     albertel 7605:     foreach my $pref (keys(%prefs)) {
                   7606: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 7607:     }
1.84      www      7608:     $cstring=~s/\&$//;
                   7609:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   7610: }
                   7611: 
                   7612: # ---------------------------------------------------------- Make/modify course
                   7613: 
                   7614: sub createcourse {
1.741     raeburn  7615:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  7616:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      7617:     $url=&declutter($url);
                   7618:     my $cid='';
1.1028    raeburn  7619:     if ($context eq 'requestcourses') {
                   7620:         my $can_create = 0;
                   7621:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   7622:         if ($udom eq $ownerdom) {
                   7623:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   7624:                                   $context)) {
                   7625:                 $can_create = 1;
                   7626:             }
                   7627:         } else {
                   7628:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   7629:                                            $category);
                   7630:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   7631:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   7632:                 if (@curr > 0) {
                   7633:                     my @options = qw(approval validate autolimit);
                   7634:                     my $optregex = join('|',@options);
                   7635:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   7636:                         $can_create = 1;
                   7637:                     }
                   7638:                 }
                   7639:             }
                   7640:         }
                   7641:         if ($can_create) {
                   7642:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   7643:                 unless (&allowed('ccc',$udom)) {
                   7644:                     return 'refused'; 
                   7645:                 }
1.1017    raeburn  7646:             }
                   7647:         } else {
                   7648:             return 'refused';
                   7649:         }
1.1028    raeburn  7650:     } elsif (!&allowed('ccc',$udom)) {
                   7651:         return 'refused';
1.84      www      7652:     }
1.1011    raeburn  7653: # --------------------------------------------------------------- Get Unique ID
                   7654:     my $uname;
                   7655:     if ($cnum =~ /^$match_courseid$/) {
                   7656:         my $chome=&homeserver($cnum,$udom,'true');
                   7657:         if (($chome eq '') || ($chome eq 'no_host')) {
                   7658:             $uname = $cnum;
                   7659:         } else {
1.1038    raeburn  7660:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7661:         }
                   7662:     } else {
1.1038    raeburn  7663:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7664:     }
                   7665:     return $uname if ($uname =~ /^error/);
                   7666: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  7667:     if (!defined($course_server)) {
                   7668:         if (defined(&domain($udom,'primary'))) {
                   7669:             $course_server = &domain($udom,'primary');
                   7670:         } else {
                   7671:             $course_server = $env{'user.home'}; 
                   7672:         }
                   7673:     }
                   7674:     my %host_servers =
                   7675:         &Apache::lonnet::get_servers($udom,'library');
                   7676:     unless ($host_servers{$course_server}) {
                   7677:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  7678:     }
1.84      www      7679: # ------------------------------------------------------------- Make the course
                   7680:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  7681:                       $course_server);
1.84      www      7682:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  7683:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      7684:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7685: 	return 'error: no such course';
                   7686:     }
1.271     www      7687: # ----------------------------------------------------------------- Course made
1.516     raeburn  7688: # log existence
1.1029    raeburn  7689:     my $now = time;
1.918     raeburn  7690:     my $newcourse = {
                   7691:                     $udom.'_'.$uname => {
1.921     raeburn  7692:                                      description => $description,
                   7693:                                      inst_code   => $inst_code,
                   7694:                                      owner       => $course_owner,
                   7695:                                      type        => $crstype,
1.1029    raeburn  7696:                                      creator     => $env{'user.name'}.':'.
                   7697:                                                     $env{'user.domain'},
                   7698:                                      created     => $now,
                   7699:                                      context     => $context,
1.918     raeburn  7700:                                                 },
                   7701:                     };
1.921     raeburn  7702:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      7703: # set toplevel url
1.271     www      7704:     my $topurl=$url;
                   7705:     unless ($nonstandard) {
                   7706: # ------------------------------------------ For standard courses, make top url
                   7707:         my $mapurl=&clutter($url);
1.278     www      7708:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 7709:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      7710: <map>
                   7711: <resource id="1" type="start"></resource>
                   7712: <resource id="2" src="$mapurl"></resource>
                   7713: <resource id="3" type="finish"></resource>
                   7714: <link index="1" from="1" to="2"></link>
                   7715: <link index="2" from="2" to="3"></link>
                   7716: </map>
                   7717: ENDINITMAP
                   7718:         $topurl=&declutter(
1.638     albertel 7719:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      7720:                           );
                   7721:     }
                   7722: # ----------------------------------------------------------- Write preferences
1.84      www      7723:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  7724:                      ('description'              => $description,
                   7725:                       'url'                      => $topurl,
                   7726:                       'internal.creator'         => $env{'user.name'}.':'.
                   7727:                                                     $env{'user.domain'},
                   7728:                       'internal.created'         => $now,
                   7729:                       'internal.creationcontext' => $context)
                   7730:                     );
1.84      www      7731:     return '/'.$udom.'/'.$uname;
                   7732: }
                   7733: 
1.1011    raeburn  7734: # ------------------------------------------------------------------- Create ID
                   7735: sub generate_coursenum {
1.1038    raeburn  7736:     my ($udom,$crstype) = @_;
1.1011    raeburn  7737:     my $domdesc = &domain($udom);
                   7738:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  7739:     my $first;
                   7740:     if ($crstype eq 'Community') {
                   7741:         $first = '0';
                   7742:     } else {
                   7743:         $first = int(1+rand(9)); 
                   7744:     } 
                   7745:     my $uname=$first.
1.1011    raeburn  7746:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7747:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7748:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7749: # ----------------------------------------------- Make sure that does not exist
                   7750:     my $uhome=&homeserver($uname,$udom,'true');
                   7751:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  7752:         if ($crstype eq 'Community') {
                   7753:             $first = '0';
                   7754:         } else {
                   7755:             $first = int(1+rand(9));
                   7756:         }
                   7757:         $uname=$first.
1.1011    raeburn  7758:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7759:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7760:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7761:         $uhome=&homeserver($uname,$udom,'true');
                   7762:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   7763:             return 'error: unable to generate unique course-ID';
                   7764:         }
                   7765:     }
                   7766:     return $uname;
                   7767: }
                   7768: 
1.813     albertel 7769: sub is_course {
                   7770:     my ($cdom,$cnum) = @_;
                   7771:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946     raeburn  7772: 				undef,'.');
1.813     albertel 7773:     if (exists($courses{$cdom.'_'.$cnum})) {
                   7774:         return 1;
                   7775:     }
                   7776:     return 0;
                   7777: }
                   7778: 
1.1015    raeburn  7779: sub store_userdata {
                   7780:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  7781:     my $result;
1.1016    raeburn  7782:     if ($datakey ne '') {
1.1013    raeburn  7783:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  7784:             if ($udom eq '' || $uname eq '') {
                   7785:                 $udom = $env{'user.domain'};
                   7786:                 $uname = $env{'user.name'};
                   7787:             }
                   7788:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  7789:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   7790:                 $result = 'error: no_host';
                   7791:             } else {
                   7792:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   7793:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   7794: 
                   7795:                 my $namevalue='';
                   7796:                 foreach my $key (keys(%{$storehash})) {
                   7797:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7798:                 }
                   7799:                 $namevalue=~s/\&$//;
1.1105    raeburn  7800:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   7801:                                   $namevalue,$uhome);
1.1013    raeburn  7802:             }
                   7803:         } else {
                   7804:             $result = 'error: data to store was not a hash reference'; 
                   7805:         }
                   7806:     } else {
                   7807:         $result= 'error: invalid requestkey'; 
                   7808:     }
                   7809:     return $result;
                   7810: }
                   7811: 
1.21      www      7812: # ---------------------------------------------------------- Assign Custom Role
                   7813: 
                   7814: sub assigncustomrole {
1.957     raeburn  7815:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7816:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  7817:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      7818: }
                   7819: 
                   7820: # ----------------------------------------------------------------- Revoke Role
                   7821: 
                   7822: sub revokerole {
1.957     raeburn  7823:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7824:     my $now=time;
1.965     raeburn  7825:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      7826: }
                   7827: 
                   7828: # ---------------------------------------------------------- Revoke Custom Role
                   7829: 
                   7830: sub revokecustomrole {
1.957     raeburn  7831:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7832:     my $now=time;
1.357     www      7833:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  7834:            $deleteflag,$selfenroll,$context);
1.17      www      7835: }
                   7836: 
1.533     banghart 7837: # ------------------------------------------------------------ Disk usage
1.535     albertel 7838: sub diskusage {
1.955     raeburn  7839:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   7840:     $directorypath =~ s/\/$//;
                   7841:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   7842:                        .&escape($getpropath).':'.&escape($uname).':'
                   7843:                        .&escape($udom),homeserver($uname,$udom));
                   7844:     if ($listing eq 'unknown_cmd') {
                   7845:         if ($getpropath) {
                   7846:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   7847:         }
                   7848:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   7849:     }
1.514     albertel 7850:     return $listing;
1.512     banghart 7851: }
                   7852: 
1.566     banghart 7853: sub is_locked {
1.1096    raeburn  7854:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 7855:     my @check;
                   7856:     my $is_locked;
1.1093    raeburn  7857:     push (@check,$file_name);
1.613     albertel 7858:     my %locked = &get('file_permissions',\@check,
1.620     albertel 7859: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 7860:     my ($tmp)=keys(%locked);
                   7861:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  7862:     
1.566     banghart 7863:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  7864:         $is_locked = 'false';
                   7865:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  7866:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  7867:                $is_locked = 'true';
1.1096    raeburn  7868:                if (ref($which) eq 'ARRAY') {
                   7869:                    push(@{$which},$entry);
                   7870:                } else {
                   7871:                    last;
                   7872:                }
1.745     raeburn  7873:            }
                   7874:        }
1.566     banghart 7875:     } else {
                   7876:         $is_locked = 'false';
                   7877:     }
1.1093    raeburn  7878:     return $is_locked;
1.566     banghart 7879: }
                   7880: 
1.759     albertel 7881: sub declutter_portfile {
                   7882:     my ($file) = @_;
1.833     albertel 7883:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 7884:     return $file;
                   7885: }
                   7886: 
1.559     banghart 7887: # ------------------------------------------------------------- Mark as Read Only
                   7888: 
                   7889: sub mark_as_readonly {
                   7890:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 7891:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7892:     my ($tmp)=keys(%current_permissions);
                   7893:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 7894:     foreach my $file (@{$files}) {
1.759     albertel 7895: 	$file = &declutter_portfile($file);
1.561     banghart 7896:         push(@{$current_permissions{$file}},$what);
1.559     banghart 7897:     }
1.613     albertel 7898:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 7899:     return;
                   7900: }
                   7901: 
1.572     banghart 7902: # ------------------------------------------------------------Save Selected Files
                   7903: 
                   7904: sub save_selected_files {
                   7905:     my ($user, $path, @files) = @_;
                   7906:     my $filename = $user."savedfiles";
1.573     banghart 7907:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 7908:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 7909:     foreach my $file (@files) {
1.620     albertel 7910:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 7911:     }
                   7912:     foreach my $file (@other_files) {
1.574     banghart 7913:         print (OUT $file."\n");
1.572     banghart 7914:     }
1.574     banghart 7915:     close (OUT);
1.572     banghart 7916:     return 'ok';
                   7917: }
                   7918: 
1.574     banghart 7919: sub clear_selected_files {
                   7920:     my ($user) = @_;
                   7921:     my $filename = $user."savedfiles";
1.1117    foxr     7922:     open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574     banghart 7923:     print (OUT undef);
                   7924:     close (OUT);
                   7925:     return ("ok");    
                   7926: }
                   7927: 
1.572     banghart 7928: sub files_in_path {
                   7929:     my ($user, $path) = @_;
                   7930:     my $filename = $user."savedfiles";
                   7931:     my %return_files;
1.1117    foxr     7932:     open (IN, '<'.LONCAPA::tempdir().$filename);
1.573     banghart 7933:     while (my $line_in = <IN>) {
1.574     banghart 7934:         chomp ($line_in);
                   7935:         my @paths_and_file = split (m!/!, $line_in);
                   7936:         my $file_part = pop (@paths_and_file);
                   7937:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 7938:         $path_part.='/';
                   7939:         my $path_and_file = $path_part.$file_part;
                   7940:         if ($path_part eq $path) {
                   7941:             $return_files{$file_part}= 'selected';
                   7942:         }
                   7943:     }
1.574     banghart 7944:     close (IN);
                   7945:     return (\%return_files);
1.572     banghart 7946: }
                   7947: 
                   7948: # called in portfolio select mode, to show files selected NOT in current directory
                   7949: sub files_not_in_path {
                   7950:     my ($user, $path) = @_;
                   7951:     my $filename = $user."savedfiles";
                   7952:     my @return_files;
                   7953:     my $path_part;
1.1117    foxr     7954:     open(IN, '<'.LONCAPA::.$filename);
1.800     albertel 7955:     while (my $line = <IN>) {
1.572     banghart 7956:         #ok, I know it's clunky, but I want it to work
1.800     albertel 7957:         my @paths_and_file = split(m|/|, $line);
                   7958:         my $file_part = pop(@paths_and_file);
                   7959:         chomp($file_part);
                   7960:         my $path_part = join('/', @paths_and_file);
1.572     banghart 7961:         $path_part .= '/';
                   7962:         my $path_and_file = $path_part.$file_part;
                   7963:         if ($path_part ne $path) {
1.800     albertel 7964:             push(@return_files, ($path_and_file));
1.572     banghart 7965:         }
                   7966:     }
1.800     albertel 7967:     close(OUT);
1.574     banghart 7968:     return (@return_files);
1.572     banghart 7969: }
                   7970: 
1.745     raeburn  7971: #----------------------------------------------Get portfolio file permissions
1.629     banghart 7972: 
1.745     raeburn  7973: sub get_portfile_permissions {
                   7974:     my ($domain,$user) = @_;
1.613     albertel 7975:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7976:     my ($tmp)=keys(%current_permissions);
                   7977:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  7978:     return \%current_permissions;
                   7979: }
                   7980: 
                   7981: #---------------------------------------------Get portfolio file access controls
                   7982: 
1.749     raeburn  7983: sub get_access_controls {
1.745     raeburn  7984:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 7985:     my %access;
                   7986:     my $real_file = $file;
                   7987:     $file =~ s/\.meta$//;
1.745     raeburn  7988:     if (defined($file)) {
1.749     raeburn  7989:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   7990:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 7991:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  7992:             }
                   7993:         }
1.745     raeburn  7994:     } else {
1.749     raeburn  7995:         foreach my $key (keys(%{$current_permissions})) {
                   7996:             if ($key =~ /\0accesscontrol$/) {
                   7997:                 if (defined($group)) {
                   7998:                     if ($key !~ m-^\Q$group\E/-) {
                   7999:                         next;
                   8000:                     }
                   8001:                 }
                   8002:                 my ($fullpath) = split(/\0/,$key);
                   8003:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   8004:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   8005:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   8006:                     }
                   8007:                 }
                   8008:             }
                   8009:         }
                   8010:     }
                   8011:     return %access;
                   8012: }
                   8013: 
                   8014: sub modify_access_controls {
                   8015:     my ($file_name,$changes,$domain,$user)=@_;
                   8016:     my ($outcome,$deloutcome);
                   8017:     my %store_permissions;
                   8018:     my %new_values;
                   8019:     my %new_control;
                   8020:     my %translation;
                   8021:     my @deletions = ();
                   8022:     my $now = time;
                   8023:     if (exists($$changes{'activate'})) {
                   8024:         if (ref($$changes{'activate'}) eq 'HASH') {
                   8025:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   8026:             my $numnew = scalar(@newitems);
                   8027:             for (my $i=0; $i<$numnew; $i++) {
                   8028:                 my $newkey = $newitems[$i];
                   8029:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  8030:                 if ($newkey =~ /^\d+:/) { 
                   8031:                     $newkey =~ s/^(\d+)/$newid/;
                   8032:                     $translation{$1} = $newid;
                   8033:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   8034:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   8035:                     $translation{$1} = $newid;
                   8036:                 }
1.749     raeburn  8037:                 $new_values{$file_name."\0".$newkey} = 
                   8038:                                           $$changes{'activate'}{$newitems[$i]};
                   8039:                 $new_control{$newkey} = $now;
                   8040:             }
                   8041:         }
                   8042:     }
                   8043:     my %todelete;
                   8044:     my %changed_items;
                   8045:     foreach my $action ('delete','update') {
                   8046:         if (exists($$changes{$action})) {
                   8047:             if (ref($$changes{$action}) eq 'HASH') {
                   8048:                 foreach my $key (keys(%{$$changes{$action}})) {
                   8049:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   8050:                     if ($action eq 'delete') { 
                   8051:                         $todelete{$itemnum} = 1;
                   8052:                     } else {
                   8053:                         $changed_items{$itemnum} = $key;
                   8054:                     }
                   8055:                 }
1.745     raeburn  8056:             }
                   8057:         }
1.749     raeburn  8058:     }
                   8059:     # get lock on access controls for file.
                   8060:     my $lockhash = {
                   8061:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   8062:                                                        ':'.$env{'user.domain'},
                   8063:                    }; 
                   8064:     my $tries = 0;
                   8065:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   8066:    
                   8067:     while (($gotlock ne 'ok') && $tries <3) {
                   8068:         $tries ++;
                   8069:         sleep 1;
                   8070:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   8071:     }
                   8072:     if ($gotlock eq 'ok') {
                   8073:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   8074:         my ($tmp)=keys(%curr_permissions);
                   8075:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   8076:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   8077:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   8078:             if (ref($curr_controls) eq 'HASH') {
                   8079:                 foreach my $control_item (keys(%{$curr_controls})) {
                   8080:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   8081:                     if (defined($todelete{$itemnum})) {
                   8082:                         push(@deletions,$file_name."\0".$control_item);
                   8083:                     } else {
                   8084:                         if (defined($changed_items{$itemnum})) {
                   8085:                             $new_control{$changed_items{$itemnum}} = $now;
                   8086:                             push(@deletions,$file_name."\0".$control_item);
                   8087:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   8088:                         } else {
                   8089:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   8090:                         }
                   8091:                     }
1.745     raeburn  8092:                 }
                   8093:             }
                   8094:         }
1.970     raeburn  8095:         my ($group);
                   8096:         if (&is_course($domain,$user)) {
                   8097:             ($group,my $file) = split(/\//,$file_name,2);
                   8098:         }
1.749     raeburn  8099:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   8100:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   8101:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   8102:         #  remove lock
                   8103:         my @del_lock = ($file_name."\0".'locked_access_records');
                   8104:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  8105:         my $sqlresult =
1.970     raeburn  8106:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  8107:                                     $group);
1.749     raeburn  8108:     } else {
                   8109:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  8110:     }
1.749     raeburn  8111:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  8112: }
                   8113: 
1.827     raeburn  8114: sub make_public_indefinitely {
                   8115:     my ($requrl) = @_;
                   8116:     my $now = time;
                   8117:     my $action = 'activate';
                   8118:     my $aclnum = 0;
                   8119:     if (&is_portfolio_url($requrl)) {
                   8120:         my (undef,$udom,$unum,$file_name,$group) =
                   8121:             &parse_portfolio_url($requrl);
                   8122:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   8123:         my %access_controls = &get_access_controls($current_perms,
                   8124:                                                    $group,$file_name);
                   8125:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   8126:             my ($num,$scope,$end,$start) = 
                   8127:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   8128:             if ($scope eq 'public') {
                   8129:                 if ($start <= $now && $end == 0) {
                   8130:                     $action = 'none';
                   8131:                 } else {
                   8132:                     $action = 'update';
                   8133:                     $aclnum = $num;
                   8134:                 }
                   8135:                 last;
                   8136:             }
                   8137:         }
                   8138:         if ($action eq 'none') {
                   8139:              return 'ok';
                   8140:         } else {
                   8141:             my %changes;
                   8142:             my $newend = 0;
                   8143:             my $newstart = $now;
                   8144:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   8145:             $changes{$action}{$newkey} = {
                   8146:                 type => 'public',
                   8147:                 time => {
                   8148:                     start => $newstart,
                   8149:                     end   => $newend,
                   8150:                 },
                   8151:             };
                   8152:             my ($outcome,$deloutcome,$new_values,$translation) =
                   8153:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   8154:             return $outcome;
                   8155:         }
                   8156:     } else {
                   8157:         return 'invalid';
                   8158:     }
                   8159: }
                   8160: 
1.745     raeburn  8161: #------------------------------------------------------Get Marked as Read Only
                   8162: 
                   8163: sub get_marked_as_readonly {
                   8164:     my ($domain,$user,$what,$group) = @_;
                   8165:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 8166:     my @readonly_files;
1.629     banghart 8167:     my $cmp1=$what;
                   8168:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  8169:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   8170:         if (defined($group)) {
                   8171:             if ($file_name !~ m-^\Q$group\E/-) {
                   8172:                 next;
                   8173:             }
                   8174:         }
1.561     banghart 8175:         if (ref($value) eq "ARRAY"){
                   8176:             foreach my $stored_what (@{$value}) {
1.629     banghart 8177:                 my $cmp2=$stored_what;
1.759     albertel 8178:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  8179:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  8180:                 }
1.629     banghart 8181:                 if ($cmp1 eq $cmp2) {
1.561     banghart 8182:                     push(@readonly_files, $file_name);
1.745     raeburn  8183:                     last;
1.563     banghart 8184:                 } elsif (!defined($what)) {
                   8185:                     push(@readonly_files, $file_name);
1.745     raeburn  8186:                     last;
1.561     banghart 8187:                 }
                   8188:             }
1.745     raeburn  8189:         }
1.561     banghart 8190:     }
                   8191:     return @readonly_files;
                   8192: }
1.577     banghart 8193: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 8194: 
1.577     banghart 8195: sub get_marked_as_readonly_hash {
1.745     raeburn  8196:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 8197:     my %readonly_files;
1.745     raeburn  8198:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   8199:         if (defined($group)) {
                   8200:             if ($file_name !~ m-^\Q$group\E/-) {
                   8201:                 next;
                   8202:             }
                   8203:         }
1.577     banghart 8204:         if (ref($value) eq "ARRAY"){
                   8205:             foreach my $stored_what (@{$value}) {
1.745     raeburn  8206:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 8207:                     foreach my $lock_descriptor(@{$stored_what}) {
                   8208:                         if ($lock_descriptor eq 'graded') {
                   8209:                             $readonly_files{$file_name} = 'graded';
                   8210:                         } elsif ($lock_descriptor eq 'handback') {
                   8211:                             $readonly_files{$file_name} = 'handback';
                   8212:                         } else {
                   8213:                             if (!exists($readonly_files{$file_name})) {
                   8214:                                 $readonly_files{$file_name} = 'locked';
                   8215:                             }
                   8216:                         }
1.745     raeburn  8217:                     }
1.750     banghart 8218:                 } 
1.577     banghart 8219:             }
                   8220:         } 
                   8221:     }
                   8222:     return %readonly_files;
                   8223: }
1.559     banghart 8224: # ------------------------------------------------------------ Unmark as Read Only
                   8225: 
                   8226: sub unmark_as_readonly {
1.629     banghart 8227:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   8228:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  8229:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 8230:     $file_name = &declutter_portfile($file_name);
1.634     albertel 8231:     my $symb_crs = $what;
                   8232:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  8233:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 8234:     my ($tmp)=keys(%current_permissions);
                   8235:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  8236:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 8237:     foreach my $file (@readonly_files) {
1.759     albertel 8238: 	my $clean_file = &declutter_portfile($file);
                   8239: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 8240: 	my $current_locks = $current_permissions{$file};
1.563     banghart 8241:         my @new_locks;
                   8242:         my @del_keys;
                   8243:         if (ref($current_locks) eq "ARRAY"){
                   8244:             foreach my $locker (@{$current_locks}) {
1.632     albertel 8245:                 my $compare=$locker;
1.749     raeburn  8246:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  8247:                     $compare=join('',@{$locker});
1.746     raeburn  8248:                     if ($compare ne $symb_crs) {
                   8249:                         push(@new_locks, $locker);
                   8250:                     }
1.563     banghart 8251:                 }
                   8252:             }
1.650     albertel 8253:             if (scalar(@new_locks) > 0) {
1.563     banghart 8254:                 $current_permissions{$file} = \@new_locks;
                   8255:             } else {
                   8256:                 push(@del_keys, $file);
1.613     albertel 8257:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 8258:                 delete($current_permissions{$file});
1.563     banghart 8259:             }
                   8260:         }
1.561     banghart 8261:     }
1.613     albertel 8262:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 8263:     return;
                   8264: }
1.512     banghart 8265: 
1.17      www      8266: # ------------------------------------------------------------ Directory lister
                   8267: 
                   8268: sub dirlist {
1.955     raeburn  8269:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      8270:     $uri=~s/^\///;
                   8271:     $uri=~s/\/$//;
1.253     stredwic 8272:     my ($udom, $uname);
1.955     raeburn  8273:     if ($getuserdir) {
1.253     stredwic 8274:         $udom = $userdomain;
                   8275:         $uname = $username;
1.955     raeburn  8276:     } else {
                   8277:         (undef,$udom,$uname)=split(/\//,$uri);
                   8278:         if(defined($userdomain)) {
                   8279:             $udom = $userdomain;
                   8280:         }
                   8281:         if(defined($username)) {
                   8282:             $uname = $username;
                   8283:         }
1.253     stredwic 8284:     }
1.955     raeburn  8285:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 8286: 
1.955     raeburn  8287:     $dirRoot = $perlvar{'lonDocRoot'};
                   8288:     if (defined($getpropath)) {
                   8289:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 8290:         $dirRoot =~ s/\/$//;
1.955     raeburn  8291:     } elsif (defined($getuserdir)) {
                   8292:         my $subdir=$uname.'__';
                   8293:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   8294:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   8295:                    ."/$udom/$subdir/$uname";
                   8296:     } elsif (defined($alternateRoot)) {
                   8297:         $dirRoot = $alternateRoot;
1.751     banghart 8298:     }
1.253     stredwic 8299: 
                   8300:     if($udom) {
                   8301:         if($uname) {
1.1135    raeburn  8302:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  8303:             if ($uhome eq 'no_host') {
                   8304:                 return ([],'no_host');
                   8305:             }
1.955     raeburn  8306:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  8307:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  8308:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  8309:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  8310:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  8311:             } else {
                   8312:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8313:             }
1.605     matthew  8314:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  8315:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  8316:                 @listing_results = split(/:/,$listing);
                   8317:             } else {
                   8318:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8319:             }
1.1135    raeburn  8320:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  8321:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   8322:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   8323:                 return ([],$listing);
                   8324:             } else {
                   8325:                 return (\@listing_results);
1.1135    raeburn  8326:             }
1.955     raeburn  8327:         } elsif(!$alternateRoot) {
1.1136    raeburn  8328:             my (%allusers,%listerror);
1.841     albertel 8329: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  8330:  	    foreach my $tryserver (keys(%servers)) {
                   8331:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   8332:                                   &escape($udom),$tryserver);
                   8333:                 if ($listing eq 'unknown_cmd') {
                   8334: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   8335: 				      $udom, $tryserver);
                   8336:                 } else {
                   8337:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8338:                 }
1.841     albertel 8339: 		if ($listing eq 'unknown_cmd') {
                   8340: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   8341: 				      $udom, $tryserver);
                   8342: 		    @listing_results = split(/:/,$listing);
                   8343: 		} else {
                   8344: 		    @listing_results =
                   8345: 			map { &unescape($_); } split(/:/,$listing);
                   8346: 		}
1.1136    raeburn  8347:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   8348:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   8349:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   8350:                     $listerror{$tryserver} = $listing;
                   8351:                 } else {
1.841     albertel 8352: 		    foreach my $line (@listing_results) {
                   8353: 			my ($entry) = split(/&/,$line,2);
                   8354: 			$allusers{$entry} = 1;
                   8355: 		    }
                   8356: 		}
1.253     stredwic 8357:             }
1.1136    raeburn  8358:             my @alluserslist=();
1.800     albertel 8359:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  8360:                 push(@alluserslist,$user.'&user');
1.253     stredwic 8361:             }
1.1136    raeburn  8362:             return (\@alluserslist);
1.253     stredwic 8363:         } else {
1.1136    raeburn  8364:             return ([],'missing username');
1.253     stredwic 8365:         }
1.955     raeburn  8366:     } elsif(!defined($getpropath)) {
1.1136    raeburn  8367:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   8368:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   8369:         return (\@all_domains);
1.955     raeburn  8370:     } else {
1.1136    raeburn  8371:         return ([],'missing domain');
1.275     stredwic 8372:     }
                   8373: }
                   8374: 
                   8375: # --------------------------------------------- GetFileTimestamp
                   8376: # This function utilizes dirlist and returns the date stamp for
                   8377: # when it was last modified.  It will also return an error of -1
                   8378: # if an error occurs
                   8379: 
                   8380: sub GetFileTimestamp {
1.955     raeburn  8381:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 8382:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   8383:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  8384:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   8385:                                     undef,$getuserdir);
                   8386:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   8387:         return -1;
                   8388:     }
                   8389:     if (ref($fileref) eq 'ARRAY') {
                   8390:         my @stats = split('&',$fileref->[0]);
1.375     matthew  8391:         # @stats contains first the filename, then the stat output
                   8392:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 8393:     } else {
                   8394:         return -1;
1.253     stredwic 8395:     }
1.26      www      8396: }
                   8397: 
1.712     albertel 8398: sub stat_file {
                   8399:     my ($uri) = @_;
1.787     albertel 8400:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 8401: 
1.955     raeburn  8402:     my ($udom,$uname,$file);
1.712     albertel 8403:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   8404: 	($udom,$uname,$file) =
1.811     albertel 8405: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 8406: 	$file = 'userfiles/'.$file;
                   8407:     }
                   8408:     if ($uri =~ m-^/res/-) {
                   8409: 	($udom,$uname) = 
1.807     albertel 8410: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 8411: 	$file = $uri;
                   8412:     }
                   8413: 
                   8414:     if (!$udom || !$uname || !$file) {
                   8415: 	# unable to handle the uri
                   8416: 	return ();
                   8417:     }
1.956     raeburn  8418:     my $getpropath;
                   8419:     if ($file =~ /^userfiles\//) {
                   8420:         $getpropath = 1;
                   8421:     }
1.1136    raeburn  8422:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   8423:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   8424:         return ();
                   8425:     } else {
                   8426:         if (ref($listref) eq 'ARRAY') {
                   8427:             my @stats = split('&',$listref->[0]);
                   8428: 	    shift(@stats); #filename is first
                   8429: 	    return @stats;
                   8430:         }
1.712     albertel 8431:     }
                   8432:     return ();
                   8433: }
                   8434: 
1.26      www      8435: # -------------------------------------------------------- Value of a Condition
                   8436: 
1.713     albertel 8437: # gets the value of a specific preevaluated condition
                   8438: #    stored in the string  $env{user.state.<cid>}
                   8439: # or looks up a condition reference in the bighash and if if hasn't
                   8440: # already been evaluated recurses into docondval to get the value of
                   8441: # the condition, then memoizing it to 
                   8442: #   $env{user.state.<cid>.<condition>}
1.40      www      8443: sub directcondval {
                   8444:     my $number=shift;
1.620     albertel 8445:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 8446: 	&Apache::lonuserstate::evalstate();
                   8447:     }
1.713     albertel 8448:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   8449: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   8450:     } elsif ($number =~ /^_/) {
                   8451: 	my $sub_condition;
                   8452: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   8453: 		&GDBM_READER(),0640)) {
                   8454: 	    $sub_condition=$bighash{'conditions'.$number};
                   8455: 	    untie(%bighash);
                   8456: 	}
                   8457: 	my $value = &docondval($sub_condition);
1.949     raeburn  8458: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 8459: 	return $value;
                   8460:     }
1.620     albertel 8461:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   8462:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      8463:     } else {
                   8464:        return 2;
                   8465:     }
                   8466: }
                   8467: 
1.713     albertel 8468: # get the collection of conditions for this resource
1.26      www      8469: sub condval {
                   8470:     my $condidx=shift;
1.54      www      8471:     my $allpathcond='';
1.713     albertel 8472:     foreach my $cond (split(/\|/,$condidx)) {
                   8473: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   8474: 	    $allpathcond.=
                   8475: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   8476: 	}
1.191     harris41 8477:     }
1.54      www      8478:     $allpathcond=~s/\|$//;
1.713     albertel 8479:     return &docondval($allpathcond);
                   8480: }
                   8481: 
                   8482: #evaluates an expression of conditions
                   8483: sub docondval {
                   8484:     my ($allpathcond) = @_;
                   8485:     my $result=0;
                   8486:     if ($env{'request.course.id'}
                   8487: 	&& defined($allpathcond)) {
                   8488: 	my $operand='|';
                   8489: 	my @stack;
                   8490: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   8491: 	    if ($chunk eq '(') {
                   8492: 		push @stack,($operand,$result);
                   8493: 	    } elsif ($chunk eq ')') {
                   8494: 		my $before=pop @stack;
                   8495: 		if (pop @stack eq '&') {
                   8496: 		    $result=$result>$before?$before:$result;
                   8497: 		} else {
                   8498: 		    $result=$result>$before?$result:$before;
                   8499: 		}
                   8500: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   8501: 		$operand=$chunk;
                   8502: 	    } else {
                   8503: 		my $new=directcondval($chunk);
                   8504: 		if ($operand eq '&') {
                   8505: 		    $result=$result>$new?$new:$result;
                   8506: 		} else {
                   8507: 		    $result=$result>$new?$result:$new;
                   8508: 		}
                   8509: 	    }
                   8510: 	}
1.26      www      8511:     }
                   8512:     return $result;
1.421     albertel 8513: }
                   8514: 
                   8515: # ---------------------------------------------------- Devalidate courseresdata
                   8516: 
                   8517: sub devalidatecourseresdata {
                   8518:     my ($coursenum,$coursedomain)=@_;
                   8519:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8520:     &devalidate_cache_new('courseres',$hashid);
1.28      www      8521: }
                   8522: 
1.763     www      8523: 
1.200     www      8524: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     8525: #
                   8526: #  Parameters:
                   8527: #      $coursenum    - Number of the course.
                   8528: #      $coursedomain - Domain at which the course was created.
                   8529: #  Returns:
                   8530: #     A hash of the course parameters along (I think) with timestamps
                   8531: #     and version info.
1.877     foxr     8532: 
1.624     albertel 8533: sub get_courseresdata {
                   8534:     my ($coursenum,$coursedomain)=@_;
1.200     www      8535:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   8536:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8537:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 8538:     my %dumpreply;
1.417     albertel 8539:     unless (defined($cached)) {
1.624     albertel 8540: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 8541: 	$result=\%dumpreply;
1.251     albertel 8542: 	my ($tmp) = keys(%dumpreply);
                   8543: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 8544: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 8545: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   8546: 	    return $tmp;
1.416     albertel 8547: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 8548: 	    $result=undef;
1.599     albertel 8549: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 8550: 	}
                   8551:     }
1.624     albertel 8552:     return $result;
                   8553: }
                   8554: 
1.633     albertel 8555: sub devalidateuserresdata {
                   8556:     my ($uname,$udom)=@_;
                   8557:     my $hashid="$udom:$uname";
                   8558:     &devalidate_cache_new('userres',$hashid);
                   8559: }
                   8560: 
1.624     albertel 8561: sub get_userresdata {
                   8562:     my ($uname,$udom)=@_;
                   8563:     #most student don\'t have any data set, check if there is some data
                   8564:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   8565: 
                   8566:     my $hashid="$udom:$uname";
                   8567:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   8568:     if (!defined($cached)) {
                   8569: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   8570: 	$result=\%resourcedata;
                   8571: 	&do_cache_new('userres',$hashid,$result,600);
                   8572:     }
                   8573:     my ($tmp)=keys(%$result);
                   8574:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   8575: 	return $result;
                   8576:     }
                   8577:     #error 2 occurs when the .db doesn't exist
                   8578:     if ($tmp!~/error: 2 /) {
1.672     albertel 8579: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 8580: 		 " Trying to get resource data for ".
                   8581: 		 $uname." at ".$udom.": ".
                   8582: 		 $tmp."</font>");
                   8583:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 8584: 	#&EXT_cache_set($udom,$uname);
                   8585: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 8586: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 8587:     }
                   8588:     return $tmp;
                   8589: }
1.879     foxr     8590: #----------------------------------------------- resdata - return resource data
                   8591: #  Purpose:
                   8592: #    Return resource data for either users or for a course.
                   8593: #  Parameters:
                   8594: #     $name      - Course/user name.
                   8595: #     $domain    - Name of the domain the user/course is registered on.
                   8596: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   8597: #     @which     - Array of names of resources desired.
                   8598: #  Returns:
                   8599: #     The value of the first reasource in @which that is found in the
                   8600: #     resource hash.
                   8601: #  Exceptional Conditions:
                   8602: #     If the $type passed in is not valid (not the string 'course' or 
                   8603: #     'user', an undefined  reference is returned.
                   8604: #     If none of the resources are found, an undef is returned
1.624     albertel 8605: sub resdata {
                   8606:     my ($name,$domain,$type,@which)=@_;
                   8607:     my $result;
                   8608:     if ($type eq 'course') {
                   8609: 	$result=&get_courseresdata($name,$domain);
                   8610:     } elsif ($type eq 'user') {
                   8611: 	$result=&get_userresdata($name,$domain);
                   8612:     }
                   8613:     if (!ref($result)) { return $result; }    
1.251     albertel 8614:     foreach my $item (@which) {
1.927     albertel 8615: 	if (defined($result->{$item->[0]})) {
                   8616: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 8617: 	}
1.250     albertel 8618:     }
1.291     albertel 8619:     return undef;
1.200     www      8620: }
                   8621: 
1.379     matthew  8622: #
                   8623: # EXT resource caching routines
                   8624: #
                   8625: 
                   8626: sub clear_EXT_cache_status {
1.383     albertel 8627:     &delenv('cache.EXT.');
1.379     matthew  8628: }
                   8629: 
                   8630: sub EXT_cache_status {
                   8631:     my ($target_domain,$target_user) = @_;
1.383     albertel 8632:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 8633:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  8634:         # We know already the user has no data
                   8635:         return 1;
                   8636:     } else {
                   8637:         return 0;
                   8638:     }
                   8639: }
                   8640: 
                   8641: sub EXT_cache_set {
                   8642:     my ($target_domain,$target_user) = @_;
1.383     albertel 8643:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  8644:     #&appenv({$cachename => time});
1.379     matthew  8645: }
                   8646: 
1.28      www      8647: # --------------------------------------------------------- Value of a Variable
1.58      www      8648: sub EXT {
1.715     albertel 8649: 
1.395     albertel 8650:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      8651:     unless ($varname) { return ''; }
1.218     albertel 8652:     #get real user name/domain, courseid and symb
                   8653:     my $courseid;
1.359     albertel 8654:     my $publicuser;
1.427     www      8655:     if ($symbparm) {
                   8656: 	$symbparm=&get_symb_from_alias($symbparm);
                   8657:     }
1.218     albertel 8658:     if (!($uname && $udom)) {
1.790     albertel 8659:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 8660:       if (!$symbparm) {	$symbparm=$cursymb; }
                   8661:     } else {
1.620     albertel 8662: 	$courseid=$env{'request.course.id'};
1.218     albertel 8663:     }
1.48      www      8664:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   8665:     my $rest;
1.320     albertel 8666:     if (defined($therest[0])) {
1.48      www      8667:        $rest=join('.',@therest);
                   8668:     } else {
                   8669:        $rest='';
                   8670:     }
1.320     albertel 8671: 
1.57      www      8672:     my $qualifierrest=$qualifier;
                   8673:     if ($rest) { $qualifierrest.='.'.$rest; }
                   8674:     my $spacequalifierrest=$space;
                   8675:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      8676:     if ($realm eq 'user') {
1.48      www      8677: # --------------------------------------------------------------- user.resource
                   8678: 	if ($space eq 'resource') {
1.651     albertel 8679: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   8680: 		  || defined($Apache::lonhomework::parsing_a_task))
                   8681: 		 &&
1.744     albertel 8682: 		 ($symbparm eq &symbread()) ) {	
                   8683: 		# if we are in the middle of processing the resource the
                   8684: 		# get the value we are planning on committing
                   8685:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   8686:                     return $Apache::lonhomework::results{$qualifierrest};
                   8687:                 } else {
                   8688:                     return $Apache::lonhomework::history{$qualifierrest};
                   8689:                 }
1.335     albertel 8690: 	    } else {
1.359     albertel 8691: 		my %restored;
1.620     albertel 8692: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 8693: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   8694: 		} else {
                   8695: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   8696: 		}
1.335     albertel 8697: 		return $restored{$qualifierrest};
                   8698: 	    }
1.48      www      8699: # ----------------------------------------------------------------- user.access
                   8700:         } elsif ($space eq 'access') {
1.218     albertel 8701: 	    # FIXME - not supporting calls for a specific user
1.48      www      8702:             return &allowed($qualifier,$rest);
                   8703: # ------------------------------------------ user.preferences, user.environment
                   8704:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 8705: 	    if (($uname eq $env{'user.name'}) &&
                   8706: 		($udom eq $env{'user.domain'})) {
                   8707: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 8708: 	    } else {
1.359     albertel 8709: 		my %returnhash;
                   8710: 		if (!$publicuser) {
                   8711: 		    %returnhash=&userenvironment($udom,$uname,
                   8712: 						 $qualifierrest);
                   8713: 		}
1.218     albertel 8714: 		return $returnhash{$qualifierrest};
                   8715: 	    }
1.48      www      8716: # ----------------------------------------------------------------- user.course
                   8717:         } elsif ($space eq 'course') {
1.218     albertel 8718: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8719:             return $env{join('.',('request.course',$qualifier))};
1.48      www      8720: # ------------------------------------------------------------------- user.role
                   8721:         } elsif ($space eq 'role') {
1.218     albertel 8722: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8723:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      8724:             if ($qualifier eq 'value') {
                   8725: 		return $role;
                   8726:             } elsif ($qualifier eq 'extent') {
                   8727:                 return $where;
                   8728:             }
                   8729: # ----------------------------------------------------------------- user.domain
                   8730:         } elsif ($space eq 'domain') {
1.218     albertel 8731:             return $udom;
1.48      www      8732: # ------------------------------------------------------------------- user.name
                   8733:         } elsif ($space eq 'name') {
1.218     albertel 8734:             return $uname;
1.48      www      8735: # ---------------------------------------------------- Any other user namespace
1.29      www      8736:         } else {
1.359     albertel 8737: 	    my %reply;
                   8738: 	    if (!$publicuser) {
                   8739: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   8740: 	    }
                   8741: 	    return $reply{$qualifierrest};
1.48      www      8742:         }
1.236     www      8743:     } elsif ($realm eq 'query') {
                   8744: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 8745:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   8746: 						[$spacequalifierrest]);
1.620     albertel 8747: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      8748:    } elsif ($realm eq 'request') {
1.48      www      8749: # ------------------------------------------------------------- request.browser
                   8750:         if ($space eq 'browser') {
1.1145    bisitz   8751:             return $env{'browser.'.$qualifier};
1.57      www      8752: # ------------------------------------------------------------ request.filename
                   8753:         } else {
1.620     albertel 8754:             return $env{'request.'.$spacequalifierrest};
1.29      www      8755:         }
1.28      www      8756:     } elsif ($realm eq 'course') {
1.48      www      8757: # ---------------------------------------------------------- course.description
1.620     albertel 8758:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      8759:     } elsif ($realm eq 'resource') {
1.165     www      8760: 
1.620     albertel 8761: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 8762: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   8763: 	}
1.693     albertel 8764: 
                   8765: 	if ($space eq 'title') {
                   8766: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   8767: 	    return &gettitle($symbparm);
                   8768: 	}
                   8769: 	
                   8770: 	if ($space eq 'map') {
                   8771: 	    my ($map) = &decode_symb($symbparm);
                   8772: 	    return &symbread($map);
                   8773: 	}
1.905     albertel 8774: 	if ($space eq 'filename') {
                   8775: 	    if ($symbparm) {
                   8776: 		return &clutter((&decode_symb($symbparm))[2]);
                   8777: 	    }
                   8778: 	    return &hreflocation('',$env{'request.filename'});
                   8779: 	}
1.693     albertel 8780: 
                   8781: 	my ($section, $group, @groups);
1.593     albertel 8782: 	my ($courselevelm,$courselevel);
1.539     albertel 8783: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8784: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      8785: 
1.218     albertel 8786: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      8787: 
1.60      www      8788: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 8789: 	    my $symbp=$symbparm;
1.735     albertel 8790: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 8791: 
                   8792: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   8793: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   8794: 
1.620     albertel 8795: 	    if (($env{'user.name'} eq $uname) &&
                   8796: 		($env{'user.domain'} eq $udom)) {
                   8797: 		$section=$env{'request.course.sec'};
1.733     raeburn  8798:                 @groups = split(/:/,$env{'request.course.groups'});  
                   8799:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 8800: 	    } else {
1.539     albertel 8801: 		if (! defined($usection)) {
1.551     albertel 8802: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 8803: 		} else {
                   8804: 		    $section = $usection;
                   8805: 		}
1.733     raeburn  8806:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 8807: 	    }
                   8808: 
                   8809: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   8810: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   8811: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   8812: 
1.593     albertel 8813: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 8814: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 8815: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      8816: 
1.60      www      8817: # ----------------------------------------------------------- first, check user
1.624     albertel 8818: 
                   8819: 	    my $userreply=&resdata($uname,$udom,'user',
1.927     albertel 8820: 				       ([$courselevelr,'resource'],
                   8821: 					[$courselevelm,'map'     ],
                   8822: 					[$courselevel, 'course'  ]));
1.931     albertel 8823: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      8824: 
1.594     albertel 8825: # ------------------------------------------------ second, check some of course
1.684     raeburn  8826:             my $coursereply;
1.691     raeburn  8827:             if (@groups > 0) {
                   8828:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   8829:                                        $mapparm,$spacequalifierrest);
1.927     albertel 8830:                 if (defined($coursereply)) { return &get_reply($coursereply); }
1.684     raeburn  8831:             }
1.96      www      8832: 
1.684     raeburn  8833: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 8834: 				  $env{'course.'.$courseid.'.domain'},
                   8835: 				  'course',
                   8836: 				  ([$seclevelr,   'resource'],
                   8837: 				   [$seclevelm,   'map'     ],
                   8838: 				   [$seclevel,    'course'  ],
                   8839: 				   [$courselevelr,'resource']));
                   8840: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      8841: 
1.60      www      8842: # ------------------------------------------------------ third, check map parms
1.218     albertel 8843: 	    my %parmhash=();
                   8844: 	    my $thisparm='';
                   8845: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 8846: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 8847: 		    &GDBM_READER(),0640)) {
1.218     albertel 8848: 		$thisparm=$parmhash{$symbparm};
                   8849: 		untie(%parmhash);
                   8850: 	    }
1.927     albertel 8851: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 8852: 	}
1.594     albertel 8853: # ------------------------------------------ fourth, look in resource metadata
1.71      www      8854: 
1.218     albertel 8855: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 8856: 	my $filename;
                   8857: 	if (!$symbparm) { $symbparm=&symbread(); }
                   8858: 	if ($symbparm) {
1.409     www      8859: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 8860: 	} else {
1.620     albertel 8861: 	    $filename=$env{'request.filename'};
1.282     albertel 8862: 	}
                   8863: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.927     albertel 8864: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282     albertel 8865: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927     albertel 8866: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      8867: 
1.927     albertel 8868: # ---------------------------------------------- fourth, look in rest of course
1.593     albertel 8869: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8870: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 8871: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   8872: 				     $env{'course.'.$courseid.'.domain'},
                   8873: 				     'course',
1.927     albertel 8874: 				     ([$courselevelm,'map'   ],
                   8875: 				      [$courselevel, 'course']));
                   8876: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 8877: 	}
1.145     www      8878: # ------------------------------------------------------------------ Cascade up
1.218     albertel 8879: 	unless ($space eq '0') {
1.336     albertel 8880: 	    my @parts=split(/_/,$space);
                   8881: 	    my $id=pop(@parts);
                   8882: 	    my $part=join('_',@parts);
                   8883: 	    if ($part eq '') { $part='0'; }
1.927     albertel 8884: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 8885: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  8886: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 8887: 	}
1.395     albertel 8888: 	if ($recurse) { return undef; }
                   8889: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 8890: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      8891: # ---------------------------------------------------- Any other user namespace
                   8892:     } elsif ($realm eq 'environment') {
                   8893: # ----------------------------------------------------------------- environment
1.620     albertel 8894: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   8895: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 8896: 	} else {
1.770     albertel 8897: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   8898: 		return '';
                   8899: 	    }
1.219     albertel 8900: 	    my %returnhash=&userenvironment($udom,$uname,
                   8901: 					    $spacequalifierrest);
                   8902: 	    return $returnhash{$spacequalifierrest};
                   8903: 	}
1.28      www      8904:     } elsif ($realm eq 'system') {
1.48      www      8905: # ----------------------------------------------------------------- system.time
                   8906: 	if ($space eq 'time') {
                   8907: 	    return time;
                   8908:         }
1.696     albertel 8909:     } elsif ($realm eq 'server') {
                   8910: # ----------------------------------------------------------------- system.time
                   8911: 	if ($space eq 'name') {
                   8912: 	    return $ENV{'SERVER_NAME'};
                   8913:         }
1.28      www      8914:     }
1.48      www      8915:     return '';
1.61      www      8916: }
                   8917: 
1.927     albertel 8918: sub get_reply {
                   8919:     my ($reply_value) = @_;
1.940     raeburn  8920:     if (ref($reply_value) eq 'ARRAY') {
                   8921:         if (wantarray) {
                   8922: 	    return @$reply_value;
                   8923:         }
                   8924:         return $reply_value->[0];
                   8925:     } else {
                   8926:         return $reply_value;
1.927     albertel 8927:     }
                   8928: }
                   8929: 
1.691     raeburn  8930: sub check_group_parms {
                   8931:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   8932:     my @groupitems = ();
                   8933:     my $resultitem;
1.927     albertel 8934:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691     raeburn  8935:     foreach my $group (@{$groups}) {
                   8936:         foreach my $level (@levels) {
1.927     albertel 8937:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   8938:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  8939:         }
                   8940:     }
                   8941:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   8942:                             $env{'course.'.$courseid.'.domain'},
                   8943:                                      'course',@groupitems);
                   8944:     return $coursereply;
                   8945: }
                   8946: 
                   8947: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  8948:     my ($courseid,@groups) = @_;
                   8949:     @groups = sort(@groups);
1.691     raeburn  8950:     return @groups;
                   8951: }
                   8952: 
1.395     albertel 8953: sub packages_tab_default {
                   8954:     my ($uri,$varname)=@_;
                   8955:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 8956: 
                   8957:     my (@extension,@specifics,$do_default);
                   8958:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 8959: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 8960: 	if ($pack_type eq 'default') {
                   8961: 	    $do_default=1;
                   8962: 	} elsif ($pack_type eq 'extension') {
                   8963: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 8964: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 8965: 	    # only look at packages defaults for packages that this id is
1.738     albertel 8966: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   8967: 	}
                   8968:     }
                   8969:     # first look for a package that matches the requested part id
                   8970:     foreach my $package (@specifics) {
                   8971: 	my (undef,$pack_type,$pack_part)=@{$package};
                   8972: 	next if ($pack_part ne $part);
                   8973: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8974: 	    return $packagetab{"$pack_type&$name&default"};
                   8975: 	}
                   8976:     }
                   8977:     # look for any possible matching non extension_ package
                   8978:     foreach my $package (@specifics) {
                   8979: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 8980: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8981: 	    return $packagetab{"$pack_type&$name&default"};
                   8982: 	}
1.585     albertel 8983: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 8984: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   8985: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 8986: 	}
                   8987:     }
1.738     albertel 8988:     # look for any posible extension_ match
                   8989:     foreach my $package (@extension) {
                   8990: 	my ($package,$pack_type)=@{$package};
                   8991: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8992: 	    return $packagetab{"$pack_type&$name&default"};
                   8993: 	}
                   8994: 	if (defined($packagetab{$package."&$name&default"})) {
                   8995: 	    return $packagetab{$package."&$name&default"};
                   8996: 	}
                   8997:     }
                   8998:     # look for a global default setting
                   8999:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   9000: 	return $packagetab{"default&$name&default"};
                   9001:     }
1.395     albertel 9002:     return undef;
                   9003: }
                   9004: 
1.334     albertel 9005: sub add_prefix_and_part {
                   9006:     my ($prefix,$part)=@_;
                   9007:     my $keyroot;
                   9008:     if (defined($prefix) && $prefix !~ /^__/) {
                   9009: 	# prefix that has a part already
                   9010: 	$keyroot=$prefix;
                   9011:     } elsif (defined($prefix)) {
                   9012: 	# prefix that is missing a part
                   9013: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   9014:     } else {
                   9015: 	# no prefix at all
                   9016: 	if (defined($part)) { $keyroot='_'.$part; }
                   9017:     }
                   9018:     return $keyroot;
                   9019: }
                   9020: 
1.71      www      9021: # ---------------------------------------------------------------- Get metadata
                   9022: 
1.599     albertel 9023: my %metaentry;
1.1070    www      9024: my %importedpartids;
1.71      www      9025: sub metadata {
1.176     www      9026:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      9027:     $uri=&declutter($uri);
1.288     albertel 9028:     # if it is a non metadata possible uri return quickly
1.529     albertel 9029:     if (($uri eq '') || 
                   9030: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 9031: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1108    raeburn  9032:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 9033: 	return undef;
                   9034:     }
1.1140    www      9035:     if (($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) 
1.924     albertel 9036: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 9037: 	return undef;
1.288     albertel 9038:     }
1.73      www      9039:     my $filename=$uri;
                   9040:     $uri=~s/\.meta$//;
1.172     www      9041: #
                   9042: # Is the metadata already cached?
1.177     www      9043: # Look at timestamp of caching
1.172     www      9044: # Everything is cached by the main uri, libraries are never directly cached
                   9045: #
1.428     albertel 9046:     if (!defined($liburi)) {
1.599     albertel 9047: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 9048: 	if (defined($cached)) { return $result->{':'.$what}; }
                   9049:     }
                   9050:     {
1.1069    www      9051: # Imported parts would go here
1.1070    www      9052:         my %importedids=();
                   9053:         my @origfileimportpartids=();
1.1069    www      9054:         my $importedparts=0;
1.172     www      9055: #
                   9056: # Is this a recursive call for a library?
                   9057: #
1.599     albertel 9058: #	if (! exists($metacache{$uri})) {
                   9059: #	    $metacache{$uri}={};
                   9060: #	}
1.924     albertel 9061: 	my $cachetime = 60*60;
1.171     www      9062:         if ($liburi) {
                   9063: 	    $liburi=&declutter($liburi);
                   9064:             $filename=$liburi;
1.401     bowersj2 9065:         } else {
1.599     albertel 9066: 	    &devalidate_cache_new('meta',$uri);
                   9067: 	    undef(%metaentry);
1.401     bowersj2 9068: 	}
1.140     www      9069:         my %metathesekeys=();
1.73      www      9070:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 9071: 	my $metastring;
1.1140    www      9072: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 9073: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 9074: 	    $metastring = 
1.929     albertel 9075: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 9076: 					  ('grade_target' => 'meta'));
                   9077: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  9078: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   9079:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 9080: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 9081: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 9082: 	    $metastring=&getfile($file);
1.489     albertel 9083: 	}
1.208     albertel 9084:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      9085:         my $token;
1.140     www      9086:         undef %metathesekeys;
1.71      www      9087:         while ($token=$parser->get_token) {
1.339     albertel 9088: 	    if ($token->[0] eq 'S') {
                   9089: 		if (defined($token->[2]->{'package'})) {
1.172     www      9090: #
                   9091: # This is a package - get package info
                   9092: #
1.339     albertel 9093: 		    my $package=$token->[2]->{'package'};
                   9094: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9095: 		    if (defined($token->[2]->{'id'})) { 
                   9096: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   9097: 		    }
1.599     albertel 9098: 		    if ($metaentry{':packages'}) {
                   9099: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 9100: 		    } else {
1.599     albertel 9101: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 9102: 		    }
1.736     albertel 9103: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 9104: 			my $part=$keyroot;
                   9105: 			$part=~s/^\_//;
1.736     albertel 9106: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   9107: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   9108: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 9109: 			    # ignore package.tab specified default values
                   9110:                             # here &package_tab_default() will fetch those
                   9111: 			    if ($subp eq 'default') { next; }
1.736     albertel 9112: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 9113: 			    my $unikey;
                   9114: 			    if ($pack =~ /_0$/) {
                   9115: 				$unikey='parameter_0_'.$name;
                   9116: 				$part=0;
                   9117: 			    } else {
                   9118: 				$unikey='parameter'.$keyroot.'_'.$name;
                   9119: 			    }
1.339     albertel 9120: 			    if ($subp eq 'display') {
                   9121: 				$value.=' [Part: '.$part.']';
                   9122: 			    }
1.599     albertel 9123: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 9124: 			    $metathesekeys{$unikey}=1;
1.599     albertel 9125: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   9126: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 9127: 			    }
1.599     albertel 9128: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   9129: 				$metaentry{':'.$unikey}=
                   9130: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 9131: 			    }
1.339     albertel 9132: 			}
                   9133: 		    }
                   9134: 		} else {
1.172     www      9135: #
                   9136: # This is not a package - some other kind of start tag
1.339     albertel 9137: #
                   9138: 		    my $entry=$token->[1];
1.1068    www      9139: 		    my $unikey='';
1.175     www      9140: 
1.339     albertel 9141: 		    if ($entry eq 'import') {
1.175     www      9142: #
                   9143: # Importing a library here
1.339     albertel 9144: #
1.1067    www      9145:                         my $location=$parser->get_text('/import');
                   9146:                         my $dir=$filename;
                   9147:                         $dir=~s|[^/]*$||;
                   9148:                         $location=&filelocation($dir,$location);
1.1069    www      9149:                        
1.1068    www      9150:                         my $importmode=$token->[2]->{'importmode'};
                   9151:                         if ($importmode eq 'problem') {
1.1069    www      9152: # Import as problem/response
1.1068    www      9153:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9154:                         } elsif ($importmode eq 'part') {
                   9155: # Import as part(s)
1.1069    www      9156:                            $importedparts=1;
                   9157: # We need to get the original file and the imported file to get the part order correct
                   9158: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
                   9159: # Load and inspect original file
1.1070    www      9160:                            if ($#origfileimportpartids<0) {
                   9161:                               undef(%importedpartids);
                   9162:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   9163:                               my $origfile=&getfile($origfilelocation);
                   9164:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   9165:                            }
                   9166: 
1.1069    www      9167: # Load and inspect imported file
                   9168:                            my $impfile=&getfile($location);
                   9169:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   9170:                            if ($#impfilepartids>=0) {
                   9171: # This problem had parts
1.1070    www      9172:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      9173:                            } else {
                   9174: # Importing by turning a single problem into a problem part
                   9175: # It gets the import-tags ID as part-ID
                   9176:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      9177:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      9178:                            }
1.1068    www      9179:                         } else {
                   9180: # Normal import
                   9181:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9182:                            if (defined($token->[2]->{'id'})) {
                   9183:                               $unikey.='_'.$token->[2]->{'id'};
                   9184:                            }
1.1067    www      9185:                         }
                   9186: 
1.339     albertel 9187: 			if ($depthcount<20) {
1.736     albertel 9188: 			    my $metadata = 
                   9189: 				&metadata($uri,'keys', $location,$unikey,
                   9190: 					  $depthcount+1);
                   9191: 			    foreach my $meta (split(',',$metadata)) {
                   9192: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   9193: 				$metathesekeys{$meta}=1;
1.339     albertel 9194: 			    }
1.1068    www      9195: 			
                   9196:                         }
1.1067    www      9197: 		    } else {
                   9198: #
                   9199: # Not importing, some other kind of non-package, non-library start tag
                   9200: # 
                   9201:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9202:                         if (defined($token->[2]->{'id'})) {
                   9203:                             $unikey.='_'.$token->[2]->{'id'};
                   9204:                         }
1.339     albertel 9205: 			if (defined($token->[2]->{'name'})) { 
                   9206: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   9207: 			}
                   9208: 			$metathesekeys{$unikey}=1;
1.736     albertel 9209: 			foreach my $param (@{$token->[3]}) {
                   9210: 			    $metaentry{':'.$unikey.'.'.$param} =
                   9211: 				$token->[2]->{$param};
1.339     albertel 9212: 			}
                   9213: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 9214: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 9215: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   9216: 		 # only ws inside the tag, and not in default, so use default
                   9217: 		 # as value
1.599     albertel 9218: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 9219: 			} elsif ( $internaltext =~ /\S/ ) {
                   9220: 		  # something interesting inside the tag
                   9221: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 9222: 			} else {
1.908     albertel 9223: 		  # no interesting values, don't set a default
1.339     albertel 9224: 			}
1.172     www      9225: # end of not-a-package not-a-library import
1.339     albertel 9226: 		    }
1.172     www      9227: # end of not-a-package start tag
1.339     albertel 9228: 		}
1.172     www      9229: # the next is the end of "start tag"
1.339     albertel 9230: 	    }
                   9231: 	}
1.483     albertel 9232: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 9233: 	$extension = lc($extension);
                   9234: 	if ($extension eq 'htm') { $extension='html'; }
                   9235: 
1.737     albertel 9236: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 9237: 	    #no specific packages #how's our extension
                   9238: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 9239: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 9240: 					 \%metathesekeys);
                   9241: 	}
1.883     albertel 9242: 
                   9243: 	if (!exists($metaentry{':packages'})
                   9244: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 9245: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 9246: 		#no specific packages well let's get default then
                   9247: 		if ($key!~/^default&/) { next; }
1.488     albertel 9248: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 9249: 					     \%metathesekeys);
                   9250: 	    }
                   9251: 	}
1.338     www      9252: # are there custom rights to evaluate
1.599     albertel 9253: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 9254: 
1.338     www      9255:     #
                   9256:     # Importing a rights file here
1.339     albertel 9257:     #
                   9258: 	    unless ($depthcount) {
1.599     albertel 9259: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 9260: 		my $dir=$filename;
                   9261: 		$dir=~s|[^/]*$||;
                   9262: 		$location=&filelocation($dir,$location);
1.736     albertel 9263: 		my $rights_metadata =
                   9264: 		    &metadata($uri,'keys',$location,'_rights',
                   9265: 			      $depthcount+1);
                   9266: 		foreach my $rights (split(',',$rights_metadata)) {
                   9267: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   9268: 		    $metathesekeys{$rights}=1;
1.339     albertel 9269: 		}
                   9270: 	    }
                   9271: 	}
1.737     albertel 9272: 	# uniqifiy package listing
                   9273: 	my %seen;
                   9274: 	my @uniq_packages =
                   9275: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   9276: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   9277: 
1.1070    www      9278:         if ($importedparts) {
                   9279: # We had imported parts and need to rebuild partorder
                   9280:            $metaentry{':partorder'}='';
                   9281:            $metathesekeys{'partorder'}=1;
                   9282:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
                   9283:                if ($origfileimportpartids[$index] eq 'part') {
                   9284: # original part, part of the problem
                   9285:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
                   9286:                } else {
                   9287: # we have imported parts at this position
                   9288:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
                   9289:                }
                   9290:            }
                   9291:            $metaentry{':partorder'}=~s/^\,//;
                   9292:         }
                   9293: 
1.737     albertel 9294: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 9295: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   9296: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924     albertel 9297: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      9298: # this is the end of "was not already recently cached
1.71      www      9299:     }
1.599     albertel 9300:     return $metaentry{':'.$what};
1.261     albertel 9301: }
                   9302: 
1.488     albertel 9303: sub metadata_create_package_def {
1.483     albertel 9304:     my ($uri,$key,$package,$metathesekeys)=@_;
                   9305:     my ($pack,$name,$subp)=split(/\&/,$key);
                   9306:     if ($subp eq 'default') { next; }
                   9307:     
1.599     albertel 9308:     if (defined($metaentry{':packages'})) {
                   9309: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 9310:     } else {
1.599     albertel 9311: 	$metaentry{':packages'}=$package;
1.483     albertel 9312:     }
                   9313:     my $value=$packagetab{$key};
                   9314:     my $unikey;
                   9315:     $unikey='parameter_0_'.$name;
1.599     albertel 9316:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 9317:     $$metathesekeys{$unikey}=1;
1.599     albertel 9318:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   9319: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 9320:     }
1.599     albertel 9321:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   9322: 	$metaentry{':'.$unikey}=
                   9323: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 9324:     }
                   9325: }
                   9326: 
1.261     albertel 9327: sub metadata_generate_part0 {
                   9328:     my ($metadata,$metacache,$uri) = @_;
                   9329:     my %allnames;
1.737     albertel 9330:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 9331: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 9332: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   9333: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 9334: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 9335: 	    $allnames{$name}=$part;
                   9336: 	  }
                   9337: 	}
                   9338:     }
                   9339:     foreach my $name (keys(%allnames)) {
                   9340:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 9341:       my $key=":parameter_0_$name";
1.261     albertel 9342:       $$metacache{"$key.part"}='0';
                   9343:       $$metacache{"$key.name"}=$name;
1.428     albertel 9344:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 9345: 					   $allnames{$name}.'_'.$name.
                   9346: 					   '.type'};
1.428     albertel 9347:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 9348: 			     '.display'};
1.644     www      9349:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 9350:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 9351:       $$metacache{"$key.display"}=$olddis;
                   9352:     }
1.71      www      9353: }
                   9354: 
1.764     albertel 9355: # ------------------------------------------------------ Devalidate title cache
                   9356: 
                   9357: sub devalidate_title_cache {
                   9358:     my ($url)=@_;
                   9359:     if (!$env{'request.course.id'}) { return; }
                   9360:     my $symb=&symbread($url);
                   9361:     if (!$symb) { return; }
                   9362:     my $key=$env{'request.course.id'}."\0".$symb;
                   9363:     &devalidate_cache_new('title',$key);
                   9364: }
                   9365: 
1.1014    droeschl 9366: # ------------------------------------------------- Get the title of a course
                   9367: 
                   9368: sub current_course_title {
                   9369:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   9370: }
1.301     www      9371: # ------------------------------------------------- Get the title of a resource
                   9372: 
                   9373: sub gettitle {
                   9374:     my $urlsymb=shift;
                   9375:     my $symb=&symbread($urlsymb);
1.534     albertel 9376:     if ($symb) {
1.620     albertel 9377: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 9378: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 9379: 	if (defined($cached)) { 
                   9380: 	    return $result;
                   9381: 	}
1.534     albertel 9382: 	my ($map,$resid,$url)=&decode_symb($symb);
                   9383: 	my $title='';
1.907     albertel 9384: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   9385: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   9386: 	} else {
                   9387: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   9388: 		    &GDBM_READER(),0640)) {
                   9389: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   9390: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   9391: 		untie(%bighash);
                   9392: 	    }
1.534     albertel 9393: 	}
                   9394: 	$title=~s/\&colon\;/\:/gs;
                   9395: 	if ($title) {
1.599     albertel 9396: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 9397: 	}
                   9398: 	$urlsymb=$url;
                   9399:     }
                   9400:     my $title=&metadata($urlsymb,'title');
                   9401:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   9402:     return $title;
1.301     www      9403: }
1.613     albertel 9404: 
1.614     albertel 9405: sub get_slot {
                   9406:     my ($which,$cnum,$cdom)=@_;
                   9407:     if (!$cnum || !$cdom) {
1.790     albertel 9408: 	(undef,my $courseid)=&whichuser();
1.620     albertel 9409: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   9410: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 9411:     }
1.703     albertel 9412:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   9413:     my %slotinfo;
                   9414:     if (exists($remembered{$key})) {
                   9415: 	$slotinfo{$which} = $remembered{$key};
                   9416:     } else {
                   9417: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   9418: 	&Apache::lonhomework::showhash(%slotinfo);
                   9419: 	my ($tmp)=keys(%slotinfo);
                   9420: 	if ($tmp=~/^error:/) { return (); }
                   9421: 	$remembered{$key} = $slotinfo{$which};
                   9422:     }
1.616     albertel 9423:     if (ref($slotinfo{$which}) eq 'HASH') {
                   9424: 	return %{$slotinfo{$which}};
                   9425:     }
                   9426:     return $slotinfo{$which};
1.614     albertel 9427: }
1.1150    raeburn  9428: 
                   9429: sub get_reservable_slots {
                   9430:     my ($cnum,$cdom,$uname,$udom) = @_;
                   9431:     my $now = time;
                   9432:     my $reservable_info;
                   9433:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   9434:     if (exists($remembered{$key})) {
                   9435:         $reservable_info = $remembered{$key};
                   9436:     } else {
                   9437:         my %resv;
                   9438:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   9439:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   9440:         $reservable_info = \%resv;
                   9441:         $remembered{$key} = $reservable_info;
                   9442:     }
                   9443:     return $reservable_info;
                   9444: }
                   9445: 
                   9446: sub get_course_slots {
                   9447:     my ($cnum,$cdom) = @_;
                   9448:     my $hashid=$cnum.':'.$cdom;
                   9449:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   9450:     if (defined($cached)) {
                   9451:         if (ref($result) eq 'HASH') {
                   9452:             return %{$result};
                   9453:         }
                   9454:     } else {
                   9455:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   9456:         my ($tmp) = keys(%slots);
                   9457:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   9458:             &Apache::lonnet::do_cache_new('allslots',$hashid,\%slots,600);
                   9459:             return %slots;
                   9460:         }
                   9461:     }
                   9462:     return;
                   9463: }
                   9464: 
                   9465: sub devalidate_slots_cache {
                   9466:     my ($cnum,$cdom)=@_;
                   9467:     my $hashid=$cnum.':'.$cdom;
                   9468:     &devalidate_cache_new('allslots',$hashid);
                   9469: }
                   9470: 
1.31      www      9471: # ------------------------------------------------- Update symbolic store links
                   9472: 
                   9473: sub symblist {
                   9474:     my ($mapname,%newhash)=@_;
1.438     www      9475:     $mapname=&deversion(&declutter($mapname));
1.31      www      9476:     my %hash;
1.620     albertel 9477:     if (($env{'request.course.fn'}) && (%newhash)) {
                   9478:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 9479:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  9480: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 9481: 		next if ($url eq 'last_known'
                   9482: 			 && $env{'form.no_update_last_known'});
                   9483: 		$hash{declutter($url)}=&encode_symb($mapname,
                   9484: 						    $newhash{$url}->[1],
                   9485: 						    $newhash{$url}->[0]);
1.191     harris41 9486:             }
1.31      www      9487:             if (untie(%hash)) {
                   9488: 		return 'ok';
                   9489:             }
                   9490:         }
                   9491:     }
                   9492:     return 'error';
1.212     www      9493: }
                   9494: 
                   9495: # --------------------------------------------------------------- Verify a symb
                   9496: 
                   9497: sub symbverify {
1.510     www      9498:     my ($symb,$thisurl)=@_;
                   9499:     my $thisfn=$thisurl;
1.439     www      9500:     $thisfn=&declutter($thisfn);
1.215     www      9501: # direct jump to resource in page or to a sequence - will construct own symbs
                   9502:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   9503: # check URL part
1.409     www      9504:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      9505: 
1.431     www      9506:     unless ($url eq $thisfn) { return 0; }
1.213     www      9507: 
1.216     www      9508:     $symb=&symbclean($symb);
1.510     www      9509:     $thisurl=&deversion($thisurl);
1.439     www      9510:     $thisfn=&deversion($thisfn);
1.213     www      9511: 
                   9512:     my %bighash;
                   9513:     my $okay=0;
1.431     www      9514: 
1.620     albertel 9515:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 9516:                             &GDBM_READER(),0640)) {
1.1032    raeburn  9517:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   9518:             $thisurl =~ s/\?.+$//;
                   9519:         }
1.510     www      9520:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1102    raeburn  9521:         unless ($ids) {
                   9522:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   9523:             $ids=$bighash{$idkey};
1.216     www      9524:         }
                   9525:         if ($ids) {
                   9526: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 9527: 	    foreach my $id (split(/\,/,$ids)) {
                   9528: 	       my ($mapid,$resid)=split(/\./,$id);
1.1032    raeburn  9529:                if ($thisfn =~ m{^/adm/wrapper/ext/}) {
                   9530:                    $symb =~ s/\?.+$//;
                   9531:                }
1.216     www      9532:                if (
                   9533:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   9534:    eq $symb) { 
1.620     albertel 9535: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  9536: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   9537:                        ($thisurl eq '/adm/navmaps')) {
1.582     albertel 9538: 		       $okay=1; 
                   9539: 		   }
                   9540: 	       }
1.216     www      9541: 	   }
                   9542:         }
1.213     www      9543: 	untie(%bighash);
                   9544:     }
                   9545:     return $okay;
1.31      www      9546: }
                   9547: 
1.210     www      9548: # --------------------------------------------------------------- Clean-up symb
                   9549: 
                   9550: sub symbclean {
                   9551:     my $symb=shift;
1.568     albertel 9552:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      9553: # remove version from map
                   9554:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      9555: 
1.210     www      9556: # remove version from URL
                   9557:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      9558: 
1.507     www      9559: # remove wrapper
                   9560: 
1.510     www      9561:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 9562:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      9563:     return $symb;
1.409     www      9564: }
                   9565: 
                   9566: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 9567: 
                   9568: sub encode_symb {
                   9569:     my ($map,$resid,$url)=@_;
                   9570:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   9571: }
1.409     www      9572: 
                   9573: sub decode_symb {
1.568     albertel 9574:     my $symb=shift;
                   9575:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   9576:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      9577:     return (&fixversion($map),$resid,&fixversion($url));
                   9578: }
                   9579: 
                   9580: sub fixversion {
                   9581:     my $fn=shift;
1.609     banghart 9582:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      9583:     my %bighash;
                   9584:     my $uri=&clutter($fn);
1.620     albertel 9585:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      9586: # is this cached?
1.599     albertel 9587:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      9588:     if (defined($cached)) { return $result; }
                   9589: # unfortunately not cached, or expired
1.620     albertel 9590:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      9591: 	    &GDBM_READER(),0640)) {
                   9592:  	if ($bighash{'version_'.$uri}) {
                   9593:  	    my $version=$bighash{'version_'.$uri};
1.444     www      9594:  	    unless (($version eq 'mostrecent') || 
                   9595: 		    ($version==&getversion($uri))) {
1.440     www      9596:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   9597:  	    }
                   9598:  	}
                   9599:  	untie %bighash;
1.413     www      9600:     }
1.599     albertel 9601:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      9602: }
                   9603: 
                   9604: sub deversion {
                   9605:     my $url=shift;
                   9606:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   9607:     return $url;
1.210     www      9608: }
                   9609: 
1.31      www      9610: # ------------------------------------------------------ Return symb list entry
                   9611: 
                   9612: sub symbread {
1.249     www      9613:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 9614:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 9615:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      9616: # no filename provided? try from environment
1.44      www      9617:     unless ($thisfn) {
1.620     albertel 9618:         if ($env{'request.symb'}) {
                   9619: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 9620: 	}
1.620     albertel 9621: 	$thisfn=$env{'request.filename'};
1.44      www      9622:     }
1.569     albertel 9623:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      9624: # is that filename actually a symb? Verify, clean, and return
                   9625:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 9626: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 9627: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 9628: 	}
1.242     www      9629:     }
1.44      www      9630:     $thisfn=declutter($thisfn);
1.31      www      9631:     my %hash;
1.37      www      9632:     my %bighash;
                   9633:     my $syval='';
1.620     albertel 9634:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  9635:         my $targetfn = $thisfn;
1.609     banghart 9636:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  9637:             $targetfn = 'adm/wrapper/'.$thisfn;
                   9638:         }
1.687     albertel 9639: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   9640: 	    $targetfn=$1;
                   9641: 	}
1.620     albertel 9642:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 9643:                       &GDBM_READER(),0640)) {
1.481     raeburn  9644: 	    $syval=$hash{$targetfn};
1.37      www      9645:             untie(%hash);
                   9646:         }
                   9647: # ---------------------------------------------------------- There was an entry
                   9648:         if ($syval) {
1.601     albertel 9649: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 9650: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  9651: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9652: 		    #return $env{$cache_str}='';
1.601     albertel 9653: 		#}    
                   9654: 		#$syval.=$1;
                   9655: 	    #}
1.37      www      9656:         } else {
                   9657: # ------------------------------------------------------- Was not in symb table
1.620     albertel 9658:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 9659:                             &GDBM_READER(),0640)) {
1.37      www      9660: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      9661:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      9662:               unless ($ids) { 
                   9663:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      9664:               }
                   9665:               unless ($ids) {
                   9666: # alias?
                   9667: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      9668:               }
1.37      www      9669:               if ($ids) {
                   9670: # ------------------------------------------------------------------- Has ID(s)
                   9671:                  my @possibilities=split(/\,/,$ids);
1.39      www      9672:                  if ($#possibilities==0) {
                   9673: # ----------------------------------------------- There is only one possibility
1.37      www      9674: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 9675: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9676: 						    $resid,$thisfn);
1.249     www      9677:                  } elsif (!$donotrecurse) {
1.39      www      9678: # ------------------------------------------ There is more than one possibility
                   9679:                      my $realpossible=0;
1.800     albertel 9680:                      foreach my $id (@possibilities) {
                   9681: 			 my $file=$bighash{'src_'.$id};
1.39      www      9682:                          if (&allowed('bre',$file)) {
1.800     albertel 9683:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      9684:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   9685: 				$realpossible++;
1.626     albertel 9686:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9687: 						    $resid,$thisfn);
1.39      www      9688:                             }
                   9689: 			 }
1.191     harris41 9690:                      }
1.39      www      9691: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      9692:                  } else {
                   9693:                      $syval='';
1.37      www      9694:                  }
                   9695: 	      }
                   9696:               untie(%bighash)
1.481     raeburn  9697:            }
1.31      www      9698:         }
1.62      www      9699:         if ($syval) {
1.620     albertel 9700: 	    return $env{$cache_str}=$syval;
1.62      www      9701:         }
1.31      www      9702:     }
1.949     raeburn  9703:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9704:     return $env{$cache_str}='';
1.31      www      9705: }
                   9706: 
                   9707: # ---------------------------------------------------------- Return random seed
                   9708: 
1.32      www      9709: sub numval {
                   9710:     my $txt=shift;
                   9711:     $txt=~tr/A-J/0-9/;
                   9712:     $txt=~tr/a-j/0-9/;
                   9713:     $txt=~tr/K-T/0-9/;
                   9714:     $txt=~tr/k-t/0-9/;
                   9715:     $txt=~tr/U-Z/0-5/;
                   9716:     $txt=~tr/u-z/0-5/;
                   9717:     $txt=~s/\D//g;
1.564     albertel 9718:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      9719:     return int($txt);
1.368     albertel 9720: }
                   9721: 
1.484     albertel 9722: sub numval2 {
                   9723:     my $txt=shift;
                   9724:     $txt=~tr/A-J/0-9/;
                   9725:     $txt=~tr/a-j/0-9/;
                   9726:     $txt=~tr/K-T/0-9/;
                   9727:     $txt=~tr/k-t/0-9/;
                   9728:     $txt=~tr/U-Z/0-5/;
                   9729:     $txt=~tr/u-z/0-5/;
                   9730:     $txt=~s/\D//g;
                   9731:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9732:     my $total;
                   9733:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 9734:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 9735:     return int($total);
                   9736: }
                   9737: 
1.575     albertel 9738: sub numval3 {
                   9739:     use integer;
                   9740:     my $txt=shift;
                   9741:     $txt=~tr/A-J/0-9/;
                   9742:     $txt=~tr/a-j/0-9/;
                   9743:     $txt=~tr/K-T/0-9/;
                   9744:     $txt=~tr/k-t/0-9/;
                   9745:     $txt=~tr/U-Z/0-5/;
                   9746:     $txt=~tr/u-z/0-5/;
                   9747:     $txt=~s/\D//g;
                   9748:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9749:     my $total;
                   9750:     foreach my $val (@txts) { $total+=$val; }
                   9751:     if ($_64bit) { $total=(($total<<32)>>32); }
                   9752:     return $total;
                   9753: }
                   9754: 
1.675     albertel 9755: sub digest {
                   9756:     my ($data)=@_;
                   9757:     my $digest=&Digest::MD5::md5($data);
                   9758:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   9759:     my ($e,$f);
                   9760:     {
                   9761:         use integer;
                   9762:         $e=($a+$b);
                   9763:         $f=($c+$d);
                   9764:         if ($_64bit) {
                   9765:             $e=(($e<<32)>>32);
                   9766:             $f=(($f<<32)>>32);
                   9767:         }
                   9768:     }
                   9769:     if (wantarray) {
                   9770: 	return ($e,$f);
                   9771:     } else {
                   9772: 	my $g;
                   9773: 	{
                   9774: 	    use integer;
                   9775: 	    $g=($e+$f);
                   9776: 	    if ($_64bit) {
                   9777: 		$g=(($g<<32)>>32);
                   9778: 	    }
                   9779: 	}
                   9780: 	return $g;
                   9781:     }
                   9782: }
                   9783: 
1.368     albertel 9784: sub latest_rnd_algorithm_id {
1.675     albertel 9785:     return '64bit5';
1.366     albertel 9786: }
1.32      www      9787: 
1.503     albertel 9788: sub get_rand_alg {
                   9789:     my ($courseid)=@_;
1.790     albertel 9790:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 9791:     if ($courseid) {
1.620     albertel 9792: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 9793:     }
                   9794:     return &latest_rnd_algorithm_id();
                   9795: }
                   9796: 
1.562     albertel 9797: sub validCODE {
                   9798:     my ($CODE)=@_;
                   9799:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   9800:     return 0;
                   9801: }
                   9802: 
1.491     albertel 9803: sub getCODE {
1.620     albertel 9804:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 9805:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   9806: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   9807: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 9808: 	return $Apache::lonhomework::history{'resource.CODE'};
                   9809:     }
                   9810:     return undef;
                   9811: }
1.1133    foxr     9812: #
                   9813: #  Determines the random seed for a specific context:
                   9814: #
                   9815: # parameters:
                   9816: #   symb      - in course context the symb for the seed.
                   9817: #   course_id - The course id of the form domain_coursenum.
                   9818: #   domain    - Domain for the user.
                   9819: #   course    - Course for the user.
                   9820: #   cenv      - environment of the course.
                   9821: #
                   9822: # NOTE:
                   9823: #   All parameters are picked out of the environment if missing
                   9824: #   or not defined.
                   9825: #   If a symb cannot be determined the current time is used instead.
                   9826: #
                   9827: #  For a given well defined symb, courside, domain, username,
                   9828: #  and course environment, the seed is reproducible.
                   9829: #
1.31      www      9830: sub rndseed {
1.1133    foxr     9831:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 9832:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 9833:     if (!defined($symb)) {
1.366     albertel 9834: 	unless ($symb=$wsymb) { return time; }
                   9835:     }
1.1146    foxr     9836:     if (!defined $courseid) { 
                   9837: 	$courseid=$wcourseid; 
                   9838:     }
                   9839:     if (!defined $domain) { $domain=$wdomain; }
                   9840:     if (!defined $username) { $username=$wusername }
1.1133    foxr     9841: 
                   9842:     my $which;
                   9843:     if (defined($cenv->{'rndseed'})) {
                   9844: 	$which = $cenv->{'rndseed'};
                   9845:     } else {
                   9846: 	$which =&get_rand_alg($courseid);
                   9847:     }
1.491     albertel 9848:     if (defined(&getCODE())) {
1.1133    foxr     9849: 
1.675     albertel 9850: 	if ($which eq '64bit5') {
                   9851: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   9852: 	} elsif ($which eq '64bit4') {
1.575     albertel 9853: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   9854: 	} else {
                   9855: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   9856: 	}
1.675     albertel 9857:     } elsif ($which eq '64bit5') {
                   9858: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 9859:     } elsif ($which eq '64bit4') {
                   9860: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 9861:     } elsif ($which eq '64bit3') {
                   9862: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 9863:     } elsif ($which eq '64bit2') {
                   9864: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 9865:     } elsif ($which eq '64bit') {
                   9866: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   9867:     }
                   9868:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   9869: }
                   9870: 
                   9871: sub rndseed_32bit {
                   9872:     my ($symb,$courseid,$domain,$username)=@_;
                   9873:     {
                   9874: 	use integer;
                   9875: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   9876: 	my $symbseed=numval($symb) << 22;
                   9877: 	my $namechck=unpack("%32C*",$username) << 17;
                   9878: 	my $nameseed=numval($username) << 12;
                   9879: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   9880: 	my $courseseed=unpack("%32C*",$courseid);
                   9881: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 9882: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9883: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9884: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 9885: 	return $num;
                   9886:     }
                   9887: }
                   9888: 
                   9889: sub rndseed_64bit {
                   9890:     my ($symb,$courseid,$domain,$username)=@_;
                   9891:     {
                   9892: 	use integer;
                   9893: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   9894: 	my $symbseed=numval($symb) << 10;
                   9895: 	my $namechck=unpack("%32S*",$username);
                   9896: 	
                   9897: 	my $nameseed=numval($username) << 21;
                   9898: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   9899: 	my $courseseed=unpack("%32S*",$courseid);
                   9900: 	
                   9901: 	my $num1=$symbchck+$symbseed+$namechck;
                   9902: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9903: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9904: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9905: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 9906: 	return "$num1,$num2";
1.155     albertel 9907:     }
1.366     albertel 9908: }
                   9909: 
1.443     albertel 9910: sub rndseed_64bit2 {
                   9911:     my ($symb,$courseid,$domain,$username)=@_;
                   9912:     {
                   9913: 	use integer;
                   9914: 	# strings need to be an even # of cahracters long, it it is odd the
                   9915:         # last characters gets thrown away
                   9916: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9917: 	my $symbseed=numval($symb) << 10;
                   9918: 	my $namechck=unpack("%32S*",$username.' ');
                   9919: 	
                   9920: 	my $nameseed=numval($username) << 21;
1.501     albertel 9921: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9922: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9923: 	
                   9924: 	my $num1=$symbchck+$symbseed+$namechck;
                   9925: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9926: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9927: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 9928: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 9929: 	return "$num1,$num2";
                   9930:     }
                   9931: }
                   9932: 
                   9933: sub rndseed_64bit3 {
                   9934:     my ($symb,$courseid,$domain,$username)=@_;
                   9935:     {
                   9936: 	use integer;
                   9937: 	# strings need to be an even # of cahracters long, it it is odd the
                   9938:         # last characters gets thrown away
                   9939: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9940: 	my $symbseed=numval2($symb) << 10;
                   9941: 	my $namechck=unpack("%32S*",$username.' ');
                   9942: 	
                   9943: 	my $nameseed=numval2($username) << 21;
1.443     albertel 9944: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9945: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9946: 	
                   9947: 	my $num1=$symbchck+$symbseed+$namechck;
                   9948: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9949: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9950: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 9951: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9952: 	
1.503     albertel 9953: 	return "$num1:$num2";
1.443     albertel 9954:     }
                   9955: }
                   9956: 
1.575     albertel 9957: sub rndseed_64bit4 {
                   9958:     my ($symb,$courseid,$domain,$username)=@_;
                   9959:     {
                   9960: 	use integer;
                   9961: 	# strings need to be an even # of cahracters long, it it is odd the
                   9962:         # last characters gets thrown away
                   9963: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9964: 	my $symbseed=numval3($symb) << 10;
                   9965: 	my $namechck=unpack("%32S*",$username.' ');
                   9966: 	
                   9967: 	my $nameseed=numval3($username) << 21;
                   9968: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9969: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9970: 	
                   9971: 	my $num1=$symbchck+$symbseed+$namechck;
                   9972: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9973: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9974: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 9975: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9976: 	
1.575     albertel 9977: 	return "$num1:$num2";
                   9978:     }
                   9979: }
                   9980: 
1.675     albertel 9981: sub rndseed_64bit5 {
                   9982:     my ($symb,$courseid,$domain,$username)=@_;
                   9983:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   9984:     return "$num1:$num2";
                   9985: }
                   9986: 
1.366     albertel 9987: sub rndseed_CODE_64bit {
                   9988:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 9989:     {
1.366     albertel 9990: 	use integer;
1.443     albertel 9991: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 9992: 	my $symbseed=numval2($symb);
1.491     albertel 9993: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   9994: 	my $CODEseed=numval(&getCODE());
1.443     albertel 9995: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 9996: 	my $num1=$symbseed+$CODEchck;
                   9997: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 9998: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   9999: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 10000: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   10001: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 10002: 	return "$num1:$num2";
1.366     albertel 10003:     }
                   10004: }
                   10005: 
1.575     albertel 10006: sub rndseed_CODE_64bit4 {
                   10007:     my ($symb,$courseid,$domain,$username)=@_;
                   10008:     {
                   10009: 	use integer;
                   10010: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   10011: 	my $symbseed=numval3($symb);
                   10012: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   10013: 	my $CODEseed=numval3(&getCODE());
                   10014: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   10015: 	my $num1=$symbseed+$CODEchck;
                   10016: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 10017: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   10018: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 10019: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   10020: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   10021: 	return "$num1:$num2";
                   10022:     }
                   10023: }
                   10024: 
1.675     albertel 10025: sub rndseed_CODE_64bit5 {
                   10026:     my ($symb,$courseid,$domain,$username)=@_;
                   10027:     my $code = &getCODE();
                   10028:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   10029:     return "$num1:$num2";
                   10030: }
                   10031: 
1.366     albertel 10032: sub setup_random_from_rndseed {
                   10033:     my ($rndseed)=@_;
1.503     albertel 10034:     if ($rndseed =~/([,:])/) {
                   10035: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 10036: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   10037:     } else {
                   10038: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 10039:     }
1.36      albertel 10040: }
                   10041: 
1.474     albertel 10042: sub latest_receipt_algorithm_id {
1.835     albertel 10043:     return 'receipt3';
1.474     albertel 10044: }
                   10045: 
1.480     www      10046: sub recunique {
                   10047:     my $fucourseid=shift;
                   10048:     my $unique;
1.835     albertel 10049:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   10050: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 10051: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      10052:     } else {
                   10053: 	$unique=$perlvar{'lonReceipt'};
                   10054:     }
                   10055:     return unpack("%32C*",$unique);
                   10056: }
                   10057: 
                   10058: sub recprefix {
                   10059:     my $fucourseid=shift;
                   10060:     my $prefix;
1.835     albertel 10061:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   10062: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 10063: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      10064:     } else {
                   10065: 	$prefix=$perlvar{'lonHostID'};
                   10066:     }
                   10067:     return unpack("%32C*",$prefix);
                   10068: }
                   10069: 
1.76      www      10070: sub ireceipt {
1.474     albertel 10071:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 10072: 
                   10073:     my $return =&recprefix($fucourseid).'-';
                   10074: 
                   10075:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   10076: 	$env{'request.state'} eq 'construct') {
                   10077: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   10078: 	return $return;
                   10079:     }
                   10080: 
1.76      www      10081:     my $cuname=unpack("%32C*",$funame);
                   10082:     my $cudom=unpack("%32C*",$fudom);
                   10083:     my $cucourseid=unpack("%32C*",$fucourseid);
                   10084:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      10085:     my $cunique=&recunique($fucourseid);
1.474     albertel 10086:     my $cpart=unpack("%32S*",$part);
1.835     albertel 10087:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   10088: 
1.790     albertel 10089: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 10090: 			       
                   10091: 	$return.= ($cunique%$cuname+
                   10092: 		   $cunique%$cudom+
                   10093: 		   $cusymb%$cuname+
                   10094: 		   $cusymb%$cudom+
                   10095: 		   $cucourseid%$cuname+
                   10096: 		   $cucourseid%$cudom+
                   10097: 		   $cpart%$cuname+
                   10098: 		   $cpart%$cudom);
                   10099:     } else {
                   10100: 	$return.= ($cunique%$cuname+
                   10101: 		   $cunique%$cudom+
                   10102: 		   $cusymb%$cuname+
                   10103: 		   $cusymb%$cudom+
                   10104: 		   $cucourseid%$cuname+
                   10105: 		   $cucourseid%$cudom);
                   10106:     }
                   10107:     return $return;
1.76      www      10108: }
                   10109: 
                   10110: sub receipt {
1.474     albertel 10111:     my ($part)=@_;
1.790     albertel 10112:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 10113:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      10114: }
1.260     ng       10115: 
1.790     albertel 10116: sub whichuser {
                   10117:     my ($passedsymb)=@_;
                   10118:     my ($symb,$courseid,$domain,$name,$publicuser);
                   10119:     if (defined($env{'form.grade_symb'})) {
                   10120: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   10121: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   10122: 	if (!$allowed &&
                   10123: 	    exists($env{'request.course.sec'}) &&
                   10124: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   10125: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   10126: 			      '/'.$env{'request.course.sec'});
                   10127: 	}
                   10128: 	if ($allowed) {
                   10129: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   10130: 	    $courseid=$tmp_courseid;
                   10131: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   10132: 	    ($name)=&get_env_multiple('form.grade_username');
                   10133: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   10134: 	}
                   10135:     }
                   10136:     if (!$passedsymb) {
                   10137: 	$symb=&symbread();
                   10138:     } else {
                   10139: 	$symb=$passedsymb;
                   10140:     }
                   10141:     $courseid=$env{'request.course.id'};
                   10142:     $domain=$env{'user.domain'};
                   10143:     $name=$env{'user.name'};
                   10144:     if ($name eq 'public' && $domain eq 'public') {
                   10145: 	if (!defined($env{'form.username'})) {
                   10146: 	    $env{'form.username'}.=time.rand(10000000);
                   10147: 	}
                   10148: 	$name.=$env{'form.username'};
                   10149:     }
                   10150:     return ($symb,$courseid,$domain,$name,$publicuser);
                   10151: 
                   10152: }
                   10153: 
1.36      albertel 10154: # ------------------------------------------------------------ Serves up a file
1.472     albertel 10155: # returns either the contents of the file or 
                   10156: # -1 if the file doesn't exist
1.481     raeburn  10157: #
                   10158: # if the target is a file that was uploaded via DOCS, 
                   10159: # a check will be made to see if a current copy exists on the local server,
                   10160: # if it does this will be served, otherwise a copy will be retrieved from
                   10161: # the home server for the course and stored in /home/httpd/html/userfiles on
                   10162: # the local server.   
1.472     albertel 10163: 
1.36      albertel 10164: sub getfile {
1.538     albertel 10165:     my ($file) = @_;
1.609     banghart 10166:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 10167:     &repcopy($file);
                   10168:     return &readfile($file);
                   10169: }
                   10170: 
                   10171: sub repcopy_userfile {
                   10172:     my ($file)=@_;
1.1142    raeburn  10173:     my $londocroot = $perlvar{'lonDocRoot'};
                   10174:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
                   10175:     if ($file =~ m{^\Q$londocroot/lonUsers/\E}) { return 'ok'; }
1.538     albertel 10176:     my ($cdom,$cnum,$filename) = 
1.811     albertel 10177: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 10178:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   10179:     if (-e "$file") {
1.828     www      10180: # we already have a local copy, check it out
1.538     albertel 10181: 	my @fileinfo = stat($file);
1.828     www      10182: 	my $rtncode;
                   10183: 	my $info;
1.538     albertel 10184: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 10185: 	if ($lwpresp ne 'ok') {
1.828     www      10186: # there is no such file anymore, even though we had a local copy
1.482     albertel 10187: 	    if ($rtncode eq '404') {
1.538     albertel 10188: 		unlink($file);
1.482     albertel 10189: 	    }
                   10190: 	    return -1;
                   10191: 	}
                   10192: 	if ($info < $fileinfo[9]) {
1.828     www      10193: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  10194: 	    return 'ok';
1.828     www      10195: 	} else {
                   10196: # the file is outdated, get rid of it
                   10197: 	    unlink($file);
1.482     albertel 10198: 	}
1.828     www      10199:     }
                   10200: # one way or the other, at this point, we don't have the file
                   10201: # construct the correct path for the file
                   10202:     my @parts = ($cdom,$cnum); 
                   10203:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   10204: 	push @parts, split(/\//,$1);
                   10205:     }
                   10206:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   10207:     foreach my $part (@parts) {
                   10208: 	$path .= '/'.$part;
                   10209: 	if (!-e $path) {
                   10210: 	    mkdir($path,0770);
1.482     albertel 10211: 	}
                   10212:     }
1.828     www      10213: # now the path exists for sure
                   10214: # get a user agent
                   10215:     my $ua=new LWP::UserAgent;
                   10216:     my $transferfile=$file.'.in.transfer';
                   10217: # FIXME: this should flock
                   10218:     if (-e $transferfile) { return 'ok'; }
                   10219:     my $request;
                   10220:     $uri=~s/^\///;
1.980     raeburn  10221:     my $homeserver = &homeserver($cnum,$cdom);
                   10222:     my $protocol = $protocol{$homeserver};
                   10223:     $protocol = 'http' if ($protocol ne 'https');
                   10224:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828     www      10225:     my $response=$ua->request($request,$transferfile);
                   10226: # did it work?
                   10227:     if ($response->is_error()) {
                   10228: 	unlink($transferfile);
                   10229: 	&logthis("Userfile repcopy failed for $uri");
                   10230: 	return -1;
                   10231:     }
                   10232: # worked, rename the transfer file
                   10233:     rename($transferfile,$file);
1.607     raeburn  10234:     return 'ok';
1.481     raeburn  10235: }
                   10236: 
1.517     albertel 10237: sub tokenwrapper {
                   10238:     my $uri=shift;
1.980     raeburn  10239:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 10240:     $uri=~s|^/||;
1.620     albertel 10241:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 10242:     my $token=$1;
1.552     albertel 10243:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   10244:     if ($udom && $uname && $file) {
                   10245: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  10246:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  10247:         my $homeserver = &homeserver($uname,$udom);
                   10248:         my $protocol = $protocol{$homeserver};
                   10249:         $protocol = 'http' if ($protocol ne 'https');
                   10250:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517     albertel 10251:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   10252:                                '&tokenissued='.$perlvar{'lonHostID'};
                   10253:     } else {
                   10254:         return '/adm/notfound.html';
                   10255:     }
                   10256: }
                   10257: 
1.828     www      10258: # call with reqtype HEAD: get last modification time
                   10259: # call with reqtype GET: get the file contents
                   10260: # Do not call this with reqtype GET for large files! It loads everything into memory
                   10261: #
1.481     raeburn  10262: sub getuploaded {
                   10263:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   10264:     $uri=~s/^\///;
1.980     raeburn  10265:     my $homeserver = &homeserver($cnum,$cdom);
                   10266:     my $protocol = $protocol{$homeserver};
                   10267:     $protocol = 'http' if ($protocol ne 'https');
                   10268:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481     raeburn  10269:     my $ua=new LWP::UserAgent;
                   10270:     my $request=new HTTP::Request($reqtype,$uri);
                   10271:     my $response=$ua->request($request);
                   10272:     $$rtncode = $response->code;
1.482     albertel 10273:     if (! $response->is_success()) {
                   10274: 	return 'failed';
                   10275:     }      
                   10276:     if ($reqtype eq 'HEAD') {
1.486     www      10277: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 10278:     } elsif ($reqtype eq 'GET') {
                   10279: 	$$info = $response->content;
1.472     albertel 10280:     }
1.482     albertel 10281:     return 'ok';
1.36      albertel 10282: }
                   10283: 
1.481     raeburn  10284: sub readfile {
                   10285:     my $file = shift;
                   10286:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   10287:     my $fh;
                   10288:     open($fh,"<$file");
                   10289:     my $a='';
1.800     albertel 10290:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  10291:     return $a;
                   10292: }
                   10293: 
1.36      albertel 10294: sub filelocation {
1.590     banghart 10295:     my ($dir,$file) = @_;
                   10296:     my $location;
                   10297:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 10298: 
                   10299:     if ($file =~ m-^/adm/-) {
                   10300: 	$file=~s-^/adm/wrapper/-/-;
                   10301: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   10302:     }
1.882     albertel 10303: 
1.1139    www      10304:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  10305:         $location = $file;
1.609     banghart 10306:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 10307:         my ($udom,$uname,$filename)=
1.811     albertel 10308:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 10309:         my $home=&homeserver($uname,$udom);
                   10310:         my $is_me=0;
                   10311:         my @ids=&current_machine_ids();
                   10312:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   10313:         if ($is_me) {
1.1117    foxr     10314:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 10315:         } else {
                   10316:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   10317:   	      $udom.'/'.$uname.'/'.$filename;
                   10318:         }
1.882     albertel 10319:     } elsif ($file =~ m-^/adm/-) {
                   10320: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 10321:     } else {
                   10322:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      10323:         $file=~s:^/(res|priv)/:/:;
                   10324:         my $space=$1;
1.590     banghart 10325:         if ( !( $file =~ m:^/:) ) {
                   10326:             $location = $dir. '/'.$file;
                   10327:         } else {
1.1142    raeburn  10328:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 10329:         }
1.59      albertel 10330:     }
1.590     banghart 10331:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 10332:     while ($location=~m{/\.\./}) {
                   10333: 	if ($location =~ m{/[^/]+/\.\./}) {
                   10334: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   10335: 	} else {
                   10336: 	    $location=~ s{/\.\./}{/}g;
                   10337: 	}
                   10338:     } #remove dir/..
1.590     banghart 10339:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   10340:     return $location;
1.46      www      10341: }
1.36      albertel 10342: 
1.46      www      10343: sub hreflocation {
                   10344:     my ($dir,$file)=@_;
1.980     raeburn  10345:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 10346: 	$file=filelocation($dir,$file);
1.700     albertel 10347:     } elsif ($file=~m-^/adm/-) {
                   10348: 	$file=~s-^/adm/wrapper/-/-;
                   10349: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 10350:     }
                   10351:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   10352: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   10353:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  10354: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   10355: 	        {/uploaded/$1/$2/}x;
1.46      www      10356:     }
1.913     albertel 10357:     if ($file=~ m{^/userfiles/}) {
                   10358: 	$file =~ s{^/userfiles/}{/uploaded/};
                   10359:     }
1.462     albertel 10360:     return $file;
1.465     albertel 10361: }
                   10362: 
1.1139    www      10363: 
                   10364: 
                   10365: 
                   10366: 
1.465     albertel 10367: sub current_machine_domains {
1.853     albertel 10368:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   10369: }
                   10370: 
                   10371: sub machine_domains {
                   10372:     my ($hostname) = @_;
1.465     albertel 10373:     my @domains;
1.838     albertel 10374:     my %hostname = &all_hostnames();
1.465     albertel 10375:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  10376: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 10377: 	if ($hostname eq $name) {
1.844     albertel 10378: 	    push(@domains,&host_domain($id));
1.465     albertel 10379: 	}
                   10380:     }
                   10381:     return @domains;
                   10382: }
                   10383: 
                   10384: sub current_machine_ids {
1.853     albertel 10385:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   10386: }
                   10387: 
                   10388: sub machine_ids {
                   10389:     my ($hostname) = @_;
                   10390:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 10391:     my @ids;
1.888     albertel 10392:     my %name_to_host = &all_names();
1.889     albertel 10393:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   10394: 	return @{ $name_to_host{$hostname} };
                   10395:     }
                   10396:     return;
1.31      www      10397: }
                   10398: 
1.824     raeburn  10399: sub additional_machine_domains {
                   10400:     my @domains;
                   10401:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   10402:     while( my $line = <$fh>) {
                   10403:         $line =~ s/\s//g;
                   10404:         push(@domains,$line);
                   10405:     }
                   10406:     return @domains;
                   10407: }
                   10408: 
                   10409: sub default_login_domain {
                   10410:     my $domain = $perlvar{'lonDefDomain'};
                   10411:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   10412:     foreach my $posdom (&current_machine_domains(),
                   10413:                         &additional_machine_domains()) {
                   10414:         if (lc($posdom) eq lc($testdomain)) {
                   10415:             $domain=$posdom;
                   10416:             last;
                   10417:         }
                   10418:     }
                   10419:     return $domain;
                   10420: }
                   10421: 
1.31      www      10422: # ------------------------------------------------------------- Declutters URLs
                   10423: 
                   10424: sub declutter {
                   10425:     my $thisfn=shift;
1.569     albertel 10426:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 10427:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      10428:     $thisfn=~s/^\///;
1.697     albertel 10429:     $thisfn=~s|^adm/wrapper/||;
                   10430:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      10431:     $thisfn=~s/^res\///;
1.1032    raeburn  10432:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   10433:         $thisfn=~s/\?.+$//;
                   10434:     }
1.268     www      10435:     return $thisfn;
                   10436: }
                   10437: 
                   10438: # ------------------------------------------------------------- Clutter up URLs
                   10439: 
                   10440: sub clutter {
                   10441:     my $thisfn='/'.&declutter(shift);
1.887     albertel 10442:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 10443: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      10444:        $thisfn='/res'.$thisfn; 
                   10445:     }
1.1031    raeburn  10446:     if ($thisfn !~m|^/adm|) {
                   10447: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 10448: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 10449: 	} else {
                   10450: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   10451: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 10452: 	    if ($embstyle eq 'ssi'
                   10453: 		|| ($embstyle eq 'hdn')
                   10454: 		|| ($embstyle eq 'rat')
                   10455: 		|| ($embstyle eq 'prv')
                   10456: 		|| ($embstyle eq 'ign')) {
                   10457: 		#do nothing with these
                   10458: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 10459: 		|| ($embstyle eq 'emb')
                   10460: 		|| ($embstyle eq 'wrp')) {
                   10461: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 10462: 	    } elsif ($embstyle eq 'unk'
                   10463: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 10464: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 10465: 	    } else {
1.718     www      10466: #		&logthis("Got a blank emb style");
1.695     albertel 10467: 	    }
1.694     albertel 10468: 	}
                   10469:     }
1.31      www      10470:     return $thisfn;
1.12      www      10471: }
                   10472: 
1.787     albertel 10473: sub clutter_with_no_wrapper {
                   10474:     my $uri = &clutter(shift);
                   10475:     if ($uri =~ m-^/adm/-) {
                   10476: 	$uri =~ s-^/adm/wrapper/-/-;
                   10477: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   10478:     }
                   10479:     return $uri;
                   10480: }
                   10481: 
1.557     albertel 10482: sub freeze_escape {
                   10483:     my ($value)=@_;
                   10484:     if (ref($value)) {
                   10485: 	$value=&nfreeze($value);
                   10486: 	return '__FROZEN__'.&escape($value);
                   10487:     }
                   10488:     return &escape($value);
                   10489: }
                   10490: 
1.11      www      10491: 
1.557     albertel 10492: sub thaw_unescape {
                   10493:     my ($value)=@_;
                   10494:     if ($value =~ /^__FROZEN__/) {
                   10495: 	substr($value,0,10,undef);
                   10496: 	$value=&unescape($value);
                   10497: 	return &thaw($value);
                   10498:     }
                   10499:     return &unescape($value);
                   10500: }
                   10501: 
1.436     albertel 10502: sub correct_line_ends {
                   10503:     my ($result)=@_;
                   10504:     $$result =~s/\r\n/\n/mg;
                   10505:     $$result =~s/\r/\n/mg;
1.415     albertel 10506: }
1.1       albertel 10507: # ================================================================ Main Program
                   10508: 
1.184     www      10509: sub goodbye {
1.204     albertel 10510:    &logthis("Starting Shut down");
1.443     albertel 10511: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 10512:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 10513: #converted
1.599     albertel 10514: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 10515:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   10516: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   10517: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 10518: #1.1 only
1.870     albertel 10519: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   10520: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   10521: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   10522: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   10523:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 10524:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   10525:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      10526:    &flushcourselogs();
                   10527:    &logthis("Shutting down");
                   10528: }
                   10529: 
1.852     albertel 10530: sub get_dns {
1.869     albertel 10531:     my ($url,$func,$ignore_cache) = @_;
                   10532:     if (!$ignore_cache) {
                   10533: 	my ($content,$cached)=
                   10534: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   10535: 	if ($cached) {
                   10536: 	    &$func($content);
                   10537: 	    return;
                   10538: 	}
                   10539:     }
                   10540: 
                   10541:     my %alldns;
1.852     albertel 10542:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10543:     foreach my $dns (<$config>) {
                   10544: 	next if ($dns !~ /^\^(\S*)/x);
1.979     raeburn  10545:         my $line = $1;
                   10546:         my ($host,$protocol) = split(/:/,$line);
                   10547:         if ($protocol ne 'https') {
                   10548:             $protocol = 'http';
                   10549:         }
                   10550: 	$alldns{$host} = $protocol;
1.869     albertel 10551:     }
                   10552:     while (%alldns) {
                   10553: 	my ($dns) = keys(%alldns);
1.852     albertel 10554: 	my $ua=new LWP::UserAgent;
1.1134    raeburn  10555:         $ua->timeout(30);
1.979     raeburn  10556: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852     albertel 10557: 	my $response=$ua->request($request);
1.979     raeburn  10558:         delete($alldns{$dns});
1.852     albertel 10559: 	next if ($response->is_error());
                   10560: 	my @content = split("\n",$response->content);
1.869     albertel 10561: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 10562: 	&$func(\@content);
1.869     albertel 10563: 	return;
1.852     albertel 10564:     }
                   10565:     close($config);
1.871     albertel 10566:     my $which = (split('/',$url))[3];
                   10567:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   10568:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 10569:     my @content = <$config>;
                   10570:     &$func(\@content);
                   10571:     return;
1.852     albertel 10572: }
1.327     albertel 10573: # ------------------------------------------------------------ Read domain file
                   10574: {
1.852     albertel 10575:     my $loaded;
1.846     albertel 10576:     my %domain;
                   10577: 
1.852     albertel 10578:     sub parse_domain_tab {
                   10579: 	my ($lines) = @_;
                   10580: 	foreach my $line (@$lines) {
                   10581: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      10582: 
1.846     albertel 10583: 	    chomp($line);
1.852     albertel 10584: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 10585: 	    my %this_domain;
                   10586: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   10587: 			       'lang_def', 'city', 'longi', 'lati',
                   10588: 			       'primary') {
                   10589: 		$this_domain{$field} = shift(@elements);
                   10590: 	    }
                   10591: 	    $domain{$name} = \%this_domain;
1.852     albertel 10592: 	}
                   10593:     }
1.864     albertel 10594: 
                   10595:     sub reset_domain_info {
                   10596: 	undef($loaded);
                   10597: 	undef(%domain);
                   10598:     }
                   10599: 
1.852     albertel 10600:     sub load_domain_tab {
1.869     albertel 10601: 	my ($ignore_cache) = @_;
                   10602: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 10603: 	my $fh;
                   10604: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   10605: 	    my @lines = <$fh>;
                   10606: 	    &parse_domain_tab(\@lines);
1.448     albertel 10607: 	}
1.852     albertel 10608: 	close($fh);
                   10609: 	$loaded = 1;
1.327     albertel 10610:     }
1.846     albertel 10611: 
                   10612:     sub domain {
1.852     albertel 10613: 	&load_domain_tab() if (!$loaded);
                   10614: 
1.846     albertel 10615: 	my ($name,$what) = @_;
                   10616: 	return if ( !exists($domain{$name}) );
                   10617: 
                   10618: 	if (!$what) {
                   10619: 	    return $domain{$name}{'description'};
                   10620: 	}
                   10621: 	return $domain{$name}{$what};
                   10622:     }
1.974     raeburn  10623: 
                   10624:     sub domain_info {
                   10625:         &load_domain_tab() if (!$loaded);
                   10626:         return %domain;
                   10627:     }
                   10628: 
1.327     albertel 10629: }
                   10630: 
                   10631: 
1.1       albertel 10632: # ------------------------------------------------------------- Read hosts file
                   10633: {
1.838     albertel 10634:     my %hostname;
1.844     albertel 10635:     my %hostdom;
1.845     albertel 10636:     my %libserv;
1.852     albertel 10637:     my $loaded;
1.888     albertel 10638:     my %name_to_host;
1.1074    raeburn  10639:     my %internetdom;
1.1107    raeburn  10640:     my %LC_dns_serv;
1.852     albertel 10641: 
                   10642:     sub parse_hosts_tab {
                   10643: 	my ($file) = @_;
                   10644: 	foreach my $configline (@$file) {
                   10645: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  10646:             chomp($configline);
                   10647: 	    if ($configline =~ /^\^/) {
                   10648:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   10649:                     $LC_dns_serv{$1} = 1;
                   10650:                 }
                   10651:                 next;
                   10652:             }
1.1074    raeburn  10653: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 10654: 	    $name=~s/\s//g;
                   10655: 	    if ($id && $domain && $role && $name) {
                   10656: 		$hostname{$id}=$name;
1.888     albertel 10657: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 10658: 		$hostdom{$id}=$domain;
                   10659: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  10660:                 if (defined($protocol)) {
                   10661:                     if ($protocol eq 'https') {
                   10662:                         $protocol{$id} = $protocol;
                   10663:                     } else {
                   10664:                         $protocol{$id} = 'http'; 
                   10665:                     }
1.968     raeburn  10666:                 } else {
1.969     raeburn  10667:                     $protocol{$id} = 'http';
1.968     raeburn  10668:                 }
1.1074    raeburn  10669:                 if (defined($intdom)) {
                   10670:                     $internetdom{$id} = $intdom;
                   10671:                 }
1.852     albertel 10672: 	    }
                   10673: 	}
                   10674:     }
1.864     albertel 10675:     
                   10676:     sub reset_hosts_info {
1.897     albertel 10677: 	&purge_remembered();
1.864     albertel 10678: 	&reset_domain_info();
                   10679: 	&reset_hosts_ip_info();
1.892     albertel 10680: 	undef(%name_to_host);
1.864     albertel 10681: 	undef(%hostname);
                   10682: 	undef(%hostdom);
                   10683: 	undef(%libserv);
                   10684: 	undef($loaded);
                   10685:     }
1.1       albertel 10686: 
1.852     albertel 10687:     sub load_hosts_tab {
1.869     albertel 10688: 	my ($ignore_cache) = @_;
                   10689: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 10690: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10691: 	my @config = <$config>;
                   10692: 	&parse_hosts_tab(\@config);
                   10693: 	close($config);
                   10694: 	$loaded=1;
1.1       albertel 10695:     }
1.852     albertel 10696: 
1.838     albertel 10697:     sub hostname {
1.852     albertel 10698: 	&load_hosts_tab() if (!$loaded);
                   10699: 
1.838     albertel 10700: 	my ($lonid) = @_;
                   10701: 	return $hostname{$lonid};
                   10702:     }
1.845     albertel 10703: 
1.838     albertel 10704:     sub all_hostnames {
1.852     albertel 10705: 	&load_hosts_tab() if (!$loaded);
                   10706: 
1.838     albertel 10707: 	return %hostname;
                   10708:     }
1.845     albertel 10709: 
1.888     albertel 10710:     sub all_names {
                   10711: 	&load_hosts_tab() if (!$loaded);
                   10712: 
                   10713: 	return %name_to_host;
                   10714:     }
                   10715: 
1.974     raeburn  10716:     sub all_host_domain {
                   10717:         &load_hosts_tab() if (!$loaded);
                   10718:         return %hostdom;
                   10719:     }
                   10720: 
1.845     albertel 10721:     sub is_library {
1.852     albertel 10722: 	&load_hosts_tab() if (!$loaded);
                   10723: 
1.845     albertel 10724: 	return exists($libserv{$_[0]});
                   10725:     }
                   10726: 
                   10727:     sub all_library {
1.852     albertel 10728: 	&load_hosts_tab() if (!$loaded);
                   10729: 
1.845     albertel 10730: 	return %libserv;
                   10731:     }
                   10732: 
1.1062    droeschl 10733:     sub unique_library {
                   10734: 	#2x reverse removes all hostnames that appear more than once
                   10735:         my %unique = reverse &all_library();
                   10736:         return reverse %unique;
                   10737:     }
                   10738: 
1.841     albertel 10739:     sub get_servers {
1.852     albertel 10740: 	&load_hosts_tab() if (!$loaded);
                   10741: 
1.841     albertel 10742: 	my ($domain,$type) = @_;
                   10743: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   10744: 	                                          : %hostname;
                   10745: 	my %result;
1.842     albertel 10746: 	if (ref($domain) eq 'ARRAY') {
                   10747: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 10748: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 10749: 		    $result{$host} = $hostname;
                   10750: 		}
                   10751: 	    }
                   10752: 	} else {
                   10753: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   10754: 		if ($hostdom{$host} eq $domain) {
                   10755: 		    $result{$host} = $hostname;
                   10756: 		}
1.841     albertel 10757: 	    }
                   10758: 	}
                   10759: 	return %result;
                   10760:     }
1.845     albertel 10761: 
1.1062    droeschl 10762:     sub get_unique_servers {
                   10763:         my %unique = reverse &get_servers(@_);
                   10764: 	return reverse %unique;
                   10765:     }
                   10766: 
1.844     albertel 10767:     sub host_domain {
1.852     albertel 10768: 	&load_hosts_tab() if (!$loaded);
                   10769: 
1.844     albertel 10770: 	my ($lonid) = @_;
                   10771: 	return $hostdom{$lonid};
                   10772:     }
                   10773: 
1.841     albertel 10774:     sub all_domains {
1.852     albertel 10775: 	&load_hosts_tab() if (!$loaded);
                   10776: 
1.841     albertel 10777: 	my %seen;
                   10778: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   10779: 	return @uniq;
                   10780:     }
1.1074    raeburn  10781: 
                   10782:     sub internet_dom {
                   10783:         &load_hosts_tab() if (!$loaded);
                   10784: 
                   10785:         my ($lonid) = @_;
                   10786:         return $internetdom{$lonid};
                   10787:     }
1.1107    raeburn  10788: 
                   10789:     sub is_LC_dns {
                   10790:         &load_hosts_tab() if (!$loaded);
                   10791: 
                   10792:         my ($hostname) = @_;
                   10793:         return exists($LC_dns_serv{$hostname});
                   10794:     }
                   10795: 
1.1       albertel 10796: }
                   10797: 
1.847     albertel 10798: { 
                   10799:     my %iphost;
1.856     albertel 10800:     my %name_to_ip;
                   10801:     my %lonid_to_ip;
1.869     albertel 10802: 
1.847     albertel 10803:     sub get_hosts_from_ip {
                   10804: 	my ($ip) = @_;
                   10805: 	my %iphosts = &get_iphost();
                   10806: 	if (ref($iphosts{$ip})) {
                   10807: 	    return @{$iphosts{$ip}};
                   10808: 	}
                   10809: 	return;
1.839     albertel 10810:     }
1.864     albertel 10811:     
                   10812:     sub reset_hosts_ip_info {
                   10813: 	undef(%iphost);
                   10814: 	undef(%name_to_ip);
                   10815: 	undef(%lonid_to_ip);
                   10816:     }
1.856     albertel 10817: 
                   10818:     sub get_host_ip {
                   10819: 	my ($lonid) = @_;
                   10820: 	if (exists($lonid_to_ip{$lonid})) {
                   10821: 	    return $lonid_to_ip{$lonid};
                   10822: 	}
                   10823: 	my $name=&hostname($lonid);
                   10824:    	my $ip = gethostbyname($name);
                   10825: 	return if (!$ip || length($ip) ne 4);
                   10826: 	$ip=inet_ntoa($ip);
                   10827: 	$name_to_ip{$name}   = $ip;
                   10828: 	$lonid_to_ip{$lonid} = $ip;
                   10829: 	return $ip;
                   10830:     }
1.847     albertel 10831:     
                   10832:     sub get_iphost {
1.869     albertel 10833: 	my ($ignore_cache) = @_;
1.894     albertel 10834: 
1.869     albertel 10835: 	if (!$ignore_cache) {
                   10836: 	    if (%iphost) {
                   10837: 		return %iphost;
                   10838: 	    }
                   10839: 	    my ($ip_info,$cached)=
                   10840: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   10841: 	    if ($cached) {
                   10842: 		%iphost      = %{$ip_info->[0]};
                   10843: 		%name_to_ip  = %{$ip_info->[1]};
                   10844: 		%lonid_to_ip = %{$ip_info->[2]};
                   10845: 		return %iphost;
                   10846: 	    }
                   10847: 	}
1.894     albertel 10848: 
                   10849: 	# get yesterday's info for fallback
                   10850: 	my %old_name_to_ip;
                   10851: 	my ($ip_info,$cached)=
                   10852: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   10853: 	if ($cached) {
                   10854: 	    %old_name_to_ip = %{$ip_info->[1]};
                   10855: 	}
                   10856: 
1.888     albertel 10857: 	my %name_to_host = &all_names();
                   10858: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 10859: 	    my $ip;
                   10860: 	    if (!exists($name_to_ip{$name})) {
                   10861: 		$ip = gethostbyname($name);
                   10862: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 10863: 		    if (defined($old_name_to_ip{$name})) {
                   10864: 			$ip = $old_name_to_ip{$name};
                   10865: 			&logthis("Can't find $name defaulting to old $ip");
                   10866: 		    } else {
                   10867: 			&logthis("Name $name no IP found");
                   10868: 			next;
                   10869: 		    }
                   10870: 		} else {
                   10871: 		    $ip=inet_ntoa($ip);
1.847     albertel 10872: 		}
                   10873: 		$name_to_ip{$name} = $ip;
                   10874: 	    } else {
                   10875: 		$ip = $name_to_ip{$name};
1.653     albertel 10876: 	    }
1.888     albertel 10877: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   10878: 		$lonid_to_ip{$id} = $ip;
                   10879: 	    }
                   10880: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 10881: 	}
1.869     albertel 10882: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   10883: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 10884: 				      48*60*60);
1.869     albertel 10885: 
1.847     albertel 10886: 	return %iphost;
1.598     albertel 10887:     }
                   10888: 
1.992     raeburn  10889:     #
                   10890:     #  Given a DNS returns the loncapa host name for that DNS 
                   10891:     # 
                   10892:     sub host_from_dns {
                   10893:         my ($dns) = @_;
                   10894:         my @hosts;
                   10895:         my $ip;
                   10896: 
1.993     raeburn  10897:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  10898:             $ip = $name_to_ip{$dns};
                   10899:         }
                   10900:         if (!$ip) {
                   10901:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   10902:             if (length($ip) == 4) { 
                   10903: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   10904:             }
                   10905:         }
                   10906:         if ($ip) {
                   10907: 	    @hosts = get_hosts_from_ip($ip);
                   10908: 	    return $hosts[0];
                   10909:         }
                   10910:         return undef;
1.986     foxr     10911:     }
1.992     raeburn  10912: 
1.1074    raeburn  10913:     sub get_internet_names {
                   10914:         my ($lonid) = @_;
                   10915:         return if ($lonid eq '');
                   10916:         my ($idnref,$cached)=
                   10917:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   10918:         if ($cached) {
                   10919:             return $idnref;
                   10920:         }
                   10921:         my $ip = &get_host_ip($lonid);
                   10922:         my @hosts = &get_hosts_from_ip($ip);
                   10923:         my %iphost = &get_iphost();
                   10924:         my (@idns,%seen);
                   10925:         foreach my $id (@hosts) {
                   10926:             my $dom = &host_domain($id);
                   10927:             my $prim_id = &domain($dom,'primary');
                   10928:             my $prim_ip = &get_host_ip($prim_id);
                   10929:             next if ($seen{$prim_ip});
                   10930:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   10931:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   10932:                     my $intdom = &internet_dom($id);
                   10933:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   10934:                         push(@idns,$intdom);
                   10935:                     }
                   10936:                 }
                   10937:             }
                   10938:             $seen{$prim_ip} = 1;
                   10939:         }
                   10940:         return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
                   10941:     }
                   10942: 
1.986     foxr     10943: }
                   10944: 
1.1079    raeburn  10945: sub all_loncaparevs {
                   10946:     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);
                   10947: }
                   10948: 
1.862     albertel 10949: BEGIN {
                   10950: 
                   10951: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   10952:     unless ($readit) {
                   10953: {
                   10954:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   10955:     %perlvar = (%perlvar,%{$configvars});
                   10956: }
                   10957: 
                   10958: 
1.1       albertel 10959: # ------------------------------------------------------ Read spare server file
                   10960: {
1.448     albertel 10961:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 10962: 
                   10963:     while (my $configline=<$config>) {
                   10964:        chomp($configline);
1.284     matthew  10965:        if ($configline) {
1.784     albertel 10966: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 10967: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 10968: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 10969:        }
                   10970:     }
1.448     albertel 10971:     close($config);
1.1       albertel 10972: }
1.11      www      10973: # ------------------------------------------------------------ Read permissions
                   10974: {
1.448     albertel 10975:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      10976: 
                   10977:     while (my $configline=<$config>) {
1.448     albertel 10978: 	chomp($configline);
                   10979: 	if ($configline) {
                   10980: 	    my ($role,$perm)=split(/ /,$configline);
                   10981: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   10982: 	}
1.11      www      10983:     }
1.448     albertel 10984:     close($config);
1.11      www      10985: }
                   10986: 
                   10987: # -------------------------------------------- Read plain texts for permissions
                   10988: {
1.448     albertel 10989:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      10990: 
                   10991:     while (my $configline=<$config>) {
1.448     albertel 10992: 	chomp($configline);
                   10993: 	if ($configline) {
1.742     raeburn  10994: 	    my ($short,@plain)=split(/:/,$configline);
                   10995:             %{$prp{$short}} = ();
                   10996: 	    if (@plain > 0) {
                   10997:                 $prp{$short}{'std'} = $plain[0];
                   10998:                 for (my $i=1; $i<@plain; $i++) {
                   10999:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   11000:                 }
                   11001:             }
1.448     albertel 11002: 	}
1.135     www      11003:     }
1.448     albertel 11004:     close($config);
1.135     www      11005: }
                   11006: 
                   11007: # ---------------------------------------------------------- Read package table
                   11008: {
1.448     albertel 11009:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      11010: 
                   11011:     while (my $configline=<$config>) {
1.483     albertel 11012: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 11013: 	chomp($configline);
                   11014: 	my ($short,$plain)=split(/:/,$configline);
                   11015: 	my ($pack,$name)=split(/\&/,$short);
                   11016: 	if ($plain ne '') {
                   11017: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   11018: 	    $packagetab{$short}=$plain; 
                   11019: 	}
1.11      www      11020:     }
1.448     albertel 11021:     close($config);
1.329     matthew  11022: }
                   11023: 
1.1073    raeburn  11024: # ---------------------------------------------------------- Read loncaparev table
                   11025: {
                   11026:     if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
                   11027:         if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   11028:             while (my $configline=<$config>) {
                   11029:                 chomp($configline);
                   11030:                 my ($hostid,$loncaparev)=split(/:/,$configline);
                   11031:                 $loncaparevs{$hostid}=$loncaparev;
                   11032:             }
                   11033:             close($config);
                   11034:         }
                   11035:     }
                   11036: }
                   11037: 
1.1074    raeburn  11038: # ---------------------------------------------------------- Read serverhostID table
                   11039: {
                   11040:     if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   11041:         if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   11042:             while (my $configline=<$config>) {
                   11043:                 chomp($configline);
                   11044:                 my ($name,$id)=split(/:/,$configline);
                   11045:                 $serverhomeIDs{$name}=$id;
                   11046:             }
                   11047:             close($config);
                   11048:         }
                   11049:     }
                   11050: }
                   11051: 
1.1079    raeburn  11052: {
                   11053:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   11054:     if (-e $file) {
                   11055:         my $parser = HTML::LCParser->new($file);
                   11056:         while (my $token = $parser->get_token()) {
                   11057:             if ($token->[0] eq 'S') {
                   11058:                 my $item = $token->[1];
                   11059:                 my $name = $token->[2]{'name'};
                   11060:                 my $value = $token->[2]{'value'};
                   11061:                 if ($item ne '' && $name ne '' && $value ne '') {
                   11062:                     my $release = $parser->get_text();
                   11063:                     $release =~ s/(^\s*|\s*$ )//gx;
                   11064:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
                   11065:                 }
                   11066:             }
                   11067:         }
                   11068:     }
1.1073    raeburn  11069: }
                   11070: 
1.1138    raeburn  11071: # ---------------------------------------------------------- Read managers table
                   11072: {
                   11073:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
                   11074:         if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
                   11075:             while (my $configline=<$config>) {
                   11076:                 chomp($configline);
                   11077:                 next if ($configline =~ /^\#/);
                   11078:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   11079:                     $managerstab{$configline} = 1;
                   11080:                 }
                   11081:             }
                   11082:             close($config);
                   11083:         }
                   11084:     }
                   11085: }
                   11086: 
1.329     matthew  11087: # ------------- set up temporary directory
                   11088: {
1.1117    foxr     11089:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  11090: 
1.11      www      11091: }
                   11092: 
1.794     albertel 11093: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   11094: 				'compress_threshold'=> 20_000,
                   11095:  			        });
1.185     www      11096: 
1.281     www      11097: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      11098: $dumpcount=0;
1.958     www      11099: $locknum=0;
1.22      www      11100: 
1.163     harris41 11101: &logtouch();
1.672     albertel 11102: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      11103: $readit=1;
1.564     albertel 11104:     {
                   11105: 	use integer;
                   11106: 	my $test=(2**32)+1;
1.568     albertel 11107: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 11108: 	&logthis(" Detected 64bit platform ($_64bit)");
                   11109:     }
1.195     www      11110: }
1.1       albertel 11111: }
1.179     www      11112: 
1.1       albertel 11113: 1;
1.191     harris41 11114: __END__
                   11115: 
1.243     albertel 11116: =pod
                   11117: 
1.191     harris41 11118: =head1 NAME
                   11119: 
1.243     albertel 11120: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 11121: 
                   11122: =head1 SYNOPSIS
                   11123: 
1.243     albertel 11124: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 11125: 
                   11126:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   11127: 
1.243     albertel 11128: Common parameters:
                   11129: 
                   11130: =over 4
                   11131: 
                   11132: =item *
                   11133: 
                   11134: $uname : an internal username (if $cname expecting a course Id specifically)
                   11135: 
                   11136: =item *
                   11137: 
                   11138: $udom : a domain (if $cdom expecting a course's domain specifically)
                   11139: 
                   11140: =item *
                   11141: 
                   11142: $symb : a resource instance identifier
                   11143: 
                   11144: =item *
                   11145: 
                   11146: $namespace : the name of a .db file that contains the data needed or
                   11147: being set.
                   11148: 
                   11149: =back
                   11150: 
1.394     bowersj2 11151: =head1 OVERVIEW
1.191     harris41 11152: 
1.394     bowersj2 11153: lonnet provides subroutines which interact with the
                   11154: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   11155: about classes, users, and resources.
1.243     albertel 11156: 
                   11157: For many of these objects you can also use this to store data about
                   11158: them or modify them in various ways.
1.191     harris41 11159: 
1.394     bowersj2 11160: =head2 Symbs
1.191     harris41 11161: 
1.394     bowersj2 11162: To identify a specific instance of a resource, LON-CAPA uses symbols
                   11163: or "symbs"X<symb>. These identifiers are built from the URL of the
                   11164: map, the resource number of the resource in the map, and the URL of
                   11165: the resource itself. The latter is somewhat redundant, but might help
                   11166: if maps change.
                   11167: 
                   11168: An example is
                   11169: 
                   11170:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   11171: 
                   11172: The respective map entry is
                   11173: 
                   11174:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   11175:   title="Problem 2">
                   11176:  </resource>
                   11177: 
                   11178: Symbs are used by the random number generator, as well as to store and
                   11179: restore data specific to a certain instance of for example a problem.
                   11180: 
                   11181: =head2 Storing And Retrieving Data
                   11182: 
                   11183: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   11184: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   11185: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   11186: is is the non-critical message twin of cstore. These functions are for
                   11187: handlers to store a perl hash to a user's permanent data space in an
                   11188: easy manner, and to retrieve it again on another call. It is expected
                   11189: that a handler would use this once at the beginning to retrieve data,
                   11190: and then again once at the end to send only the new data back.
                   11191: 
                   11192: The data is stored in the user's data directory on the user's
                   11193: homeserver under the ID of the course.
                   11194: 
                   11195: The hash that is returned by restore will have all of the previous
                   11196: value for all of the elements of the hash.
                   11197: 
                   11198: Example:
                   11199: 
                   11200:  #creating a hash
                   11201:  my %hash;
                   11202:  $hash{'foo'}='bar';
                   11203: 
                   11204:  #storing it
                   11205:  &Apache::lonnet::cstore(\%hash);
                   11206: 
                   11207:  #changing a value
                   11208:  $hash{'foo'}='notbar';
                   11209: 
                   11210:  #adding a new value
                   11211:  $hash{'bar'}='foo';
                   11212:  &Apache::lonnet::cstore(\%hash);
                   11213: 
                   11214:  #retrieving the hash
                   11215:  my %history=&Apache::lonnet::restore();
                   11216: 
                   11217:  #print the hash
                   11218:  foreach my $key (sort(keys(%history))) {
                   11219:    print("\%history{$key} = $history{$key}");
                   11220:  }
                   11221: 
                   11222: Will print out:
1.191     harris41 11223: 
1.394     bowersj2 11224:  %history{1:foo} = bar
                   11225:  %history{1:keys} = foo:timestamp
                   11226:  %history{1:timestamp} = 990455579
                   11227:  %history{2:bar} = foo
                   11228:  %history{2:foo} = notbar
                   11229:  %history{2:keys} = foo:bar:timestamp
                   11230:  %history{2:timestamp} = 990455580
                   11231:  %history{bar} = foo
                   11232:  %history{foo} = notbar
                   11233:  %history{timestamp} = 990455580
                   11234:  %history{version} = 2
                   11235: 
                   11236: Note that the special hash entries C<keys>, C<version> and
                   11237: C<timestamp> were added to the hash. C<version> will be equal to the
                   11238: total number of versions of the data that have been stored. The
                   11239: C<timestamp> attribute will be the UNIX time the hash was
                   11240: stored. C<keys> is available in every historical section to list which
                   11241: keys were added or changed at a specific historical revision of a
                   11242: hash.
                   11243: 
                   11244: B<Warning>: do not store the hash that restore returns directly. This
                   11245: will cause a mess since it will restore the historical keys as if the
                   11246: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 11247: 
1.394     bowersj2 11248: Calling convention:
1.191     harris41 11249: 
1.394     bowersj2 11250:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   11251:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 11252: 
1.394     bowersj2 11253: For more detailed information, see lonnet specific documentation.
1.191     harris41 11254: 
1.394     bowersj2 11255: =head1 RETURN MESSAGES
1.191     harris41 11256: 
1.394     bowersj2 11257: =over 4
1.191     harris41 11258: 
1.394     bowersj2 11259: =item * B<con_lost>: unable to contact remote host
1.191     harris41 11260: 
1.394     bowersj2 11261: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   11262: when the connection is brought back up
1.191     harris41 11263: 
1.394     bowersj2 11264: =item * B<con_failed>: unable to contact remote host and unable to save message
                   11265: for later delivery
1.191     harris41 11266: 
1.967     bisitz   11267: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 11268: 
1.394     bowersj2 11269: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 11270: that was requested
1.191     harris41 11271: 
1.243     albertel 11272: =back
1.191     harris41 11273: 
1.243     albertel 11274: =head1 PUBLIC SUBROUTINES
1.191     harris41 11275: 
1.243     albertel 11276: =head2 Session Environment Functions
1.191     harris41 11277: 
1.243     albertel 11278: =over 4
1.191     harris41 11279: 
1.394     bowersj2 11280: =item * 
                   11281: X<appenv()>
1.949     raeburn  11282: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 11283: the user envirnoment file, and will be restored for each access this
1.620     albertel 11284: user makes during this session, also modifies the %env for the current
1.949     raeburn  11285: process. Optional rolesarrayref - if defined contains a reference to an array
                   11286: of roles which are exempt from the restriction on modifying user.role entries 
                   11287: in the user's environment.db and in %env.    
1.191     harris41 11288: 
                   11289: =item *
1.394     bowersj2 11290: X<delenv()>
1.987     raeburn  11291: B<delenv($delthis,$regexp)>: removes all items from the session
                   11292: environment file that begin with $delthis. If the 
                   11293: optional second arg - $regexp - is true, $delthis is treated as a 
                   11294: regular expression, otherwise \Q$delthis\E is used. 
                   11295: The values are also deleted from the current processes %env.
1.191     harris41 11296: 
1.795     albertel 11297: =item * get_env_multiple($name) 
                   11298: 
                   11299: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   11300: values may be defined and end up as an array ref.
                   11301: 
                   11302: returns an array of values
                   11303: 
1.243     albertel 11304: =back
                   11305: 
                   11306: =head2 User Information
1.191     harris41 11307: 
1.243     albertel 11308: =over 4
1.191     harris41 11309: 
                   11310: =item *
1.394     bowersj2 11311: X<queryauthenticate()>
                   11312: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 11313: authentication scheme
                   11314: 
                   11315: =item *
1.394     bowersj2 11316: X<authenticate()>
1.1073    raeburn  11317: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 11318: authenticate user from domain's lib servers (first use the current
                   11319: one). C<$upass> should be the users password.
1.1073    raeburn  11320: $checkdefauth is optional (value is 1 if a check should be made to
                   11321:    authenticate user using default authentication method, and allow
                   11322:    account creation if username does not have account in the domain).
                   11323: $clientcancheckhost is optional (value is 1 if checking whether the
                   11324:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 11325: 
                   11326: =item *
1.394     bowersj2 11327: X<homeserver()>
                   11328: B<homeserver($uname,$udom)>: find the server which has
                   11329: the user's directory and files (there must be only one), this caches
                   11330: the answer, and also caches if there is a borken connection.
1.191     harris41 11331: 
                   11332: =item *
1.394     bowersj2 11333: X<idget()>
                   11334: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   11335: (IDs are a unique resource in a domain, there must be only 1 ID per
                   11336: username, and only 1 username per ID in a specific domain) (returns
                   11337: hash: id=>name,id=>name)
1.191     harris41 11338: 
                   11339: =item *
1.394     bowersj2 11340: X<idrget()>
                   11341: B<idrget($udom,@unames)>: find the IDs behind a list of
                   11342: usernames (returns hash: name=>id,name=>id)
1.191     harris41 11343: 
                   11344: =item *
1.394     bowersj2 11345: X<idput()>
                   11346: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 11347: 
                   11348: =item *
1.394     bowersj2 11349: X<rolesinit()>
                   11350: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 11351: 
                   11352: =item *
1.551     albertel 11353: X<getsection()>
                   11354: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 11355: course $cname, return section name/number or '' for "not in course"
                   11356: and '-1' for "no section"
                   11357: 
                   11358: =item *
1.394     bowersj2 11359: X<userenvironment()>
                   11360: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 11361: passed in @what from the requested user's environment, returns a hash
                   11362: 
1.858     raeburn  11363: =item * 
                   11364: X<userlog_query()>
1.859     albertel 11365: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   11366: activity.log file. %filters defines filters applied when parsing the
                   11367: log file. These can be start or end timestamps, or the type of action
                   11368: - log to look for Login or Logout events, check for Checkin or
                   11369: Checkout, role for role selection. The response is in the form
                   11370: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   11371: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  11372: 
1.243     albertel 11373: =back
                   11374: 
                   11375: =head2 User Roles
                   11376: 
                   11377: =over 4
                   11378: 
                   11379: =item *
                   11380: 
1.810     raeburn  11381: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 11382:  F: full access
                   11383:  U,I,K: authentication modes (cxx only)
                   11384:  '': forbidden
                   11385:  1: user needs to choose course
                   11386:  2: browse allowed
1.766     albertel 11387:  A: passphrase authentication needed
1.243     albertel 11388: 
                   11389: =item *
                   11390: 
                   11391: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   11392: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   11393: and course level
                   11394: 
                   11395: =item *
                   11396: 
1.988     raeburn  11397: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   11398: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  11399: $type is Course (default) or Community.
1.988     raeburn  11400: If $forcedefault evaluates to true, text returned will be default 
                   11401: text for $type. Otherwise, if this is a course, the text returned 
                   11402: will be a custom name for the role (if defined in the course's 
                   11403: environment).  If no custom name is defined the default is returned.
                   11404:    
1.832     raeburn  11405: =item *
                   11406: 
1.935     raeburn  11407: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858     raeburn  11408: All arguments are optional. Returns a hash of a roles, either for
                   11409: co-author/assistant author roles for a user's Construction Space
1.906     albertel 11410: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  11411: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   11412: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   11413: For each key, value is set to colon-separated start and end times for
                   11414: the role.  If no username and domain are specified, will default to
1.934     raeburn  11415: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  11416: of role statuses (active, future or previous), roles 
                   11417: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   11418: to restrict the list of roles reported. If no array ref is 
                   11419: provided for types, will default to return only active roles.
1.834     albertel 11420: 
1.243     albertel 11421: =back
                   11422: 
                   11423: =head2 User Modification
                   11424: 
                   11425: =over 4
                   11426: 
                   11427: =item *
                   11428: 
1.957     raeburn  11429: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 11430: user for the level given by URL.  Optional start and end dates (leave empty
                   11431: string or zero for "no date")
1.191     harris41 11432: 
                   11433: =item *
                   11434: 
1.243     albertel 11435: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   11436: change a users, password, possible return values are: ok,
                   11437: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   11438: refused
1.191     harris41 11439: 
                   11440: =item *
                   11441: 
1.243     albertel 11442: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 11443: 
                   11444: =item *
                   11445: 
1.1058    raeburn  11446: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   11447:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   11448: 
                   11449: will update user information (firstname,middlename,lastname,generation,
                   11450: permanentemail), and if forceid is true, student/employee ID also.
                   11451: A user's institutional affiliation(s) can also be updated.
                   11452: User information fields will not be overwritten with empty entries 
                   11453: unless the field is included in the $candelete array reference.
                   11454: This array is included when a single user is modified via "Manage Users",
                   11455: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 11456: 
                   11457: =item *
                   11458: 
1.286     matthew  11459: modifystudent
                   11460: 
1.957     raeburn  11461: modify a student's enrollment and identification information.
1.286     matthew  11462: The course id is resolved based on the current users environment.  
                   11463: This means the envoking user must be a course coordinator or otherwise
                   11464: associated with a course.
                   11465: 
1.297     matthew  11466: This call is essentially a wrapper for lonnet::modifyuser and
                   11467: lonnet::modify_student_enrollment
1.286     matthew  11468: 
                   11469: Inputs: 
                   11470: 
                   11471: =over 4
                   11472: 
1.957     raeburn  11473: =item B<$udom> Student's loncapa domain
1.286     matthew  11474: 
1.957     raeburn  11475: =item B<$uname> Student's loncapa login name
1.286     matthew  11476: 
1.964     bisitz   11477: =item B<$uid> Student/Employee ID
1.286     matthew  11478: 
1.957     raeburn  11479: =item B<$umode> Student's authentication mode
1.286     matthew  11480: 
1.957     raeburn  11481: =item B<$upass> Student's password
1.286     matthew  11482: 
1.957     raeburn  11483: =item B<$first> Student's first name
1.286     matthew  11484: 
1.957     raeburn  11485: =item B<$middle> Student's middle name
1.286     matthew  11486: 
1.957     raeburn  11487: =item B<$last> Student's last name
1.286     matthew  11488: 
1.957     raeburn  11489: =item B<$gene> Student's generation
1.286     matthew  11490: 
1.957     raeburn  11491: =item B<$usec> Student's section in course
1.286     matthew  11492: 
                   11493: =item B<$end> Unix time of the roles expiration
                   11494: 
                   11495: =item B<$start> Unix time of the roles start date
                   11496: 
                   11497: =item B<$forceid> If defined, allow $uid to be changed
                   11498: 
                   11499: =item B<$desiredhome> server to use as home server for student
                   11500: 
1.957     raeburn  11501: =item B<$email> Student's permanent e-mail address
                   11502: 
                   11503: =item B<$type> Type of enrollment (auto or manual)
                   11504: 
1.963     raeburn  11505: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   11506: 
                   11507: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  11508: 
1.963     raeburn  11509: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  11510: 
1.963     raeburn  11511: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  11512: 
1.963     raeburn  11513: =item B<$inststatus> institutional status of user - : separated string of escaped status types  
1.957     raeburn  11514: 
1.286     matthew  11515: =back
1.297     matthew  11516: 
                   11517: =item *
                   11518: 
                   11519: modify_student_enrollment
                   11520: 
                   11521: Change a students enrollment status in a class.  The environment variable
                   11522: 'role.request.course' must be defined for this function to proceed.
                   11523: 
                   11524: Inputs:
                   11525: 
                   11526: =over 4
                   11527: 
                   11528: =item $udom, students domain
                   11529: 
                   11530: =item $uname, students name
                   11531: 
                   11532: =item $uid, students user id
                   11533: 
                   11534: =item $first, students first name
                   11535: 
                   11536: =item $middle
                   11537: 
                   11538: =item $last
                   11539: 
                   11540: =item $gene
                   11541: 
                   11542: =item $usec
                   11543: 
                   11544: =item $end
                   11545: 
                   11546: =item $start
                   11547: 
1.957     raeburn  11548: =item $type
                   11549: 
                   11550: =item $locktype
                   11551: 
                   11552: =item $cid
                   11553: 
                   11554: =item $selfenroll
                   11555: 
                   11556: =item $context
                   11557: 
1.297     matthew  11558: =back
                   11559: 
1.191     harris41 11560: 
                   11561: =item *
                   11562: 
1.243     albertel 11563: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   11564: custom role; give a custom role to a user for the level given by URL.  Specify
                   11565: name and domain of role author, and role name
1.191     harris41 11566: 
                   11567: =item *
                   11568: 
1.243     albertel 11569: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 11570: 
                   11571: =item *
                   11572: 
1.243     albertel 11573: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   11574: 
                   11575: =back
                   11576: 
                   11577: =head2 Course Infomation
                   11578: 
                   11579: =over 4
1.191     harris41 11580: 
                   11581: =item *
                   11582: 
1.1118    foxr     11583: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 11584: specified course id, including all environment settings for the
                   11585: course, the description of the course will be in the hash under the
                   11586: key 'description'
1.191     harris41 11587: 
1.1118    foxr     11588: $options is an optional parameter that if supplied is a hash reference that controls
                   11589: what how this function works.  It has the following key/values:
                   11590: 
                   11591: =over 4
                   11592: 
                   11593: =item freshen_cache
                   11594: 
                   11595: If defined, and the environment cache for the course is valid, it is 
                   11596: returned in the returned hash.
                   11597: 
                   11598: =item one_time
                   11599: 
                   11600: If defined, the last cache time is set to _now_
                   11601: 
                   11602: =item user
                   11603: 
                   11604: If defined, the supplied username is used instead of the current user.
                   11605: 
                   11606: 
                   11607: =back
                   11608: 
1.191     harris41 11609: =item *
                   11610: 
1.624     albertel 11611: resdata($name,$domain,$type,@which) : request for current parameter
                   11612: setting for a specific $type, where $type is either 'course' or 'user',
                   11613: @what should be a list of parameters to ask about. This routine caches
                   11614: answers for 5 minutes.
1.243     albertel 11615: 
1.877     foxr     11616: =item *
                   11617: 
                   11618: get_courseresdata($courseid, $domain) : dump the entire course resource
                   11619: data base, returning a hash that is keyed by the resource name and has
                   11620: values that are the resource value.  I believe that the timestamps and
                   11621: versions are also returned.
                   11622: 
                   11623: 
1.243     albertel 11624: =back
                   11625: 
                   11626: =head2 Course Modification
                   11627: 
                   11628: =over 4
1.191     harris41 11629: 
                   11630: =item *
                   11631: 
1.243     albertel 11632: writecoursepref($courseid,%prefs) : write preferences (environment
                   11633: database) for a course
1.191     harris41 11634: 
                   11635: =item *
                   11636: 
1.1011    raeburn  11637: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   11638: 
                   11639: =item *
                   11640: 
1.1038    raeburn  11641: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 11642: 
                   11643: =back
                   11644: 
                   11645: =head2 Resource Subroutines
                   11646: 
                   11647: =over 4
1.191     harris41 11648: 
                   11649: =item *
                   11650: 
1.243     albertel 11651: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 11652: 
                   11653: =item *
                   11654: 
1.243     albertel 11655: repcopy($filename) : subscribes to the requested file, and attempts to
                   11656: replicate from the owning library server, Might return
1.607     raeburn  11657: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   11658: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 11659: resource. Expects the local filesystem pathname
                   11660: (/home/httpd/html/res/....)
                   11661: 
                   11662: =back
                   11663: 
                   11664: =head2 Resource Information
                   11665: 
                   11666: =over 4
1.191     harris41 11667: 
                   11668: =item *
                   11669: 
1.243     albertel 11670: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   11671: a vairety of different possible values, $varname should be a request
                   11672: string, and the other parameters can be used to specify who and what
                   11673: one is asking about.
                   11674: 
                   11675: Possible values for $varname are environment.lastname (or other item
                   11676: from the envirnment hash), user.name (or someother aspect about the
                   11677: user), resource.0.maxtries (or some other part and parameter of a
                   11678: resource)
1.204     albertel 11679: 
                   11680: =item *
                   11681: 
1.243     albertel 11682: directcondval($number) : get current value of a condition; reads from a state
                   11683: string
1.204     albertel 11684: 
                   11685: =item *
                   11686: 
1.243     albertel 11687: condval($condidx) : value of condition index based on state
1.204     albertel 11688: 
                   11689: =item *
                   11690: 
1.243     albertel 11691: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   11692: resource's metadata, $what should be either a specific key, or either
                   11693: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   11694: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   11695: 
                   11696: this function automatically caches all requests
1.191     harris41 11697: 
                   11698: =item *
                   11699: 
1.243     albertel 11700: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   11701: network of library servers; returns file handle of where SQL and regex results
                   11702: will be stored for query
1.191     harris41 11703: 
                   11704: =item *
                   11705: 
1.243     albertel 11706: symbread($filename) : return symbolic list entry (filename argument optional);
                   11707: returns the data handle
1.191     harris41 11708: 
                   11709: =item *
                   11710: 
1.243     albertel 11711: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 11712: a possible symb for the URL in $thisfn, and if is an encryypted
                   11713: resource that the user accessed using /enc/ returns a 1 on success, 0
                   11714: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 11715: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 11716: 
1.191     harris41 11717: 
                   11718: =item *
                   11719: 
1.243     albertel 11720: symbclean($symb) : removes versions numbers from a symb, returns the
                   11721: cleaned symb
1.191     harris41 11722: 
                   11723: =item *
                   11724: 
1.243     albertel 11725: is_on_map($uri) : checks if the $uri is somewhere on the current
                   11726: course map, user must be in a course for it to work.
1.191     harris41 11727: 
                   11728: =item *
                   11729: 
1.243     albertel 11730: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 11731: 
                   11732: =item *
                   11733: 
1.243     albertel 11734: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   11735: a random seed, all arguments are optional, if they aren't sent it uses the
                   11736: environment to derive them. Note: if symb isn't sent and it can't get one
                   11737: from &symbread it will use the current time as its return value
1.191     harris41 11738: 
                   11739: =item *
                   11740: 
1.243     albertel 11741: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   11742: unfakeable, receipt
1.191     harris41 11743: 
                   11744: =item *
                   11745: 
1.620     albertel 11746: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 11747: 
                   11748: =item *
                   11749: 
1.243     albertel 11750: countacc($url) : count the number of accesses to a given URL
1.191     harris41 11751: 
                   11752: =item *
                   11753: 
1.243     albertel 11754: 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 11755: 
                   11756: =item *
                   11757: 
1.243     albertel 11758: 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 11759: 
                   11760: =item *
                   11761: 
1.243     albertel 11762: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 11763: 
                   11764: =item *
                   11765: 
1.243     albertel 11766: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   11767: forcing spreadsheet to reevaluate the resource scores next time.
                   11768: 
                   11769: =back
                   11770: 
                   11771: =head2 Storing/Retreiving Data
                   11772: 
                   11773: =over 4
1.191     harris41 11774: 
                   11775: =item *
                   11776: 
1.243     albertel 11777: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   11778: for this url; hashref needs to be given and should be a \%hashname; the
                   11779: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 11780: be derived from the env
1.191     harris41 11781: 
                   11782: =item *
                   11783: 
1.243     albertel 11784: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   11785: uses critical subroutine
1.191     harris41 11786: 
                   11787: =item *
                   11788: 
1.243     albertel 11789: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   11790: all args are optional
1.191     harris41 11791: 
                   11792: =item *
                   11793: 
1.717     albertel 11794: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   11795: dumps the complete (or key matching regexp) namespace into a hash
                   11796: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   11797: normally &store()ed into
                   11798: 
                   11799: $range should be either an integer '100' (give me the first 100
                   11800:                                            matching records)
                   11801:               or be  two integers sperated by a - with no spaces
                   11802:                  '30-50' (give me the 30th through the 50th matching
                   11803:                           records)
                   11804: 
                   11805: 
                   11806: =item *
                   11807: 
                   11808: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   11809: replaces a &store() version of data with a replacement set of data
                   11810: for a particular resource in a namespace passed in the $storehash hash 
                   11811: reference
                   11812: 
                   11813: =item *
                   11814: 
1.243     albertel 11815: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   11816: works very similar to store/cstore, but all data is stored in a
                   11817: temporary location and can be reset using tmpreset, $storehash should
                   11818: be a hash reference, returns nothing on success
1.191     harris41 11819: 
                   11820: =item *
                   11821: 
1.243     albertel 11822: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   11823: similar to restore, but all data is stored in a temporary location and
                   11824: can be reset using tmpreset. Returns a hash of values on success,
                   11825: error string otherwise.
1.191     harris41 11826: 
                   11827: =item *
                   11828: 
1.243     albertel 11829: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   11830: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 11831: 
                   11832: =item *
                   11833: 
1.243     albertel 11834: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11835: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 11836: 
                   11837: =item *
                   11838: 
1.243     albertel 11839: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   11840: namesp ($udom and $uname are optional)
1.191     harris41 11841: 
                   11842: =item *
                   11843: 
1.702     albertel 11844: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 11845: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 11846: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  11847: 
1.702     albertel 11848: $range should be either an integer '100' (give me the first 100
                   11849:                                            matching records)
                   11850:               or be  two integers sperated by a - with no spaces
                   11851:                  '30-50' (give me the 30th through the 50th matching
                   11852:                           records)
1.449     matthew  11853: =item *
                   11854: 
                   11855: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   11856: $store can be a scalar, an array reference, or if the amount to be 
                   11857: incremented is > 1, a hash reference.
                   11858: 
                   11859: ($udom and $uname are optional)
1.191     harris41 11860: 
                   11861: =item *
                   11862: 
1.243     albertel 11863: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   11864: ($udom and $uname are optional)
1.191     harris41 11865: 
                   11866: =item *
                   11867: 
1.243     albertel 11868: cput($namespace,$storehash,$udom,$uname) : critical put
                   11869: ($udom and $uname are optional)
1.191     harris41 11870: 
                   11871: =item *
                   11872: 
1.748     albertel 11873: newput($namespace,$storehash,$udom,$uname) :
                   11874: 
                   11875: Attempts to store the items in the $storehash, but only if they don't
                   11876: currently exist, if this succeeds you can be certain that you have 
                   11877: successfully created a new key value pair in the $namespace db.
                   11878: 
                   11879: 
                   11880: Args:
                   11881:  $namespace: name of database to store values to
                   11882:  $storehash: hashref to store to the db
                   11883:  $udom: (optional) domain of user containing the db
                   11884:  $uname: (optional) name of user caontaining the db
                   11885: 
                   11886: Returns:
                   11887:  'ok' -> succeeded in storing all keys of $storehash
                   11888:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   11889:                         least <key> already existed in the db (other
                   11890:                         requested keys may also already exist)
1.967     bisitz   11891:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 11892:  'con_lost' -> unable to contact request server
                   11893:  'refused' -> action was not allowed by remote machine
                   11894: 
                   11895: 
                   11896: =item *
                   11897: 
1.243     albertel 11898: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11899: reference filled in from namesp (encrypts the return communication)
                   11900: ($udom and $uname are optional)
1.191     harris41 11901: 
                   11902: =item *
                   11903: 
1.243     albertel 11904: log($udom,$name,$home,$message) : write to permanent log for user; use
                   11905: critical subroutine
                   11906: 
1.806     raeburn  11907: =item *
                   11908: 
1.860     raeburn  11909: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   11910: array reference filled in from namespace found in domain level on either
                   11911: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  11912: 
                   11913: =item *
                   11914: 
1.860     raeburn  11915: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   11916: domain level either on specified domain server ($uhome) or primary domain 
                   11917: server ($udom and $uhome are optional)
1.806     raeburn  11918: 
1.943     raeburn  11919: =item * 
                   11920: 
                   11921: get_domain_defaults($target_domain) : returns hash with defaults for
                   11922: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
                   11923: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
                   11924: or localauth), initial password or a kerberos realm, language (e.g., en-us).
                   11925: Values are retrieved from cache (if current), or from domain's configuration.db
                   11926: (if available), or lastly from values in lonTabs/dns_domain,tab, 
                   11927: or lonTabs/domain.tab. 
                   11928: 
                   11929: %domdefaults = &get_auth_defaults($target_domain);
                   11930: 
1.243     albertel 11931: =back
                   11932: 
                   11933: =head2 Network Status Functions
                   11934: 
                   11935: =over 4
1.191     harris41 11936: 
                   11937: =item *
                   11938: 
1.1137    raeburn  11939: dirlist() : return directory list based on URI (first arg).
                   11940: 
                   11941: Inputs: 1 required, 5 optional.
                   11942: 
                   11943: =over
                   11944: 
                   11945: =item 
                   11946: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   11947: 
                   11948: =item
                   11949: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   11950: 
                   11951: =item
                   11952: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   11953: 
                   11954: =item
                   11955: $getpropath - boolean: 1 if prepend path using &propath(). 
                   11956: 
                   11957: =item
                   11958: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   11959: 
                   11960: =item 
                   11961: $alternateRoot - path to prepend in place of path from $uri.
                   11962: 
                   11963: =back
                   11964: 
                   11965: Returns: Array of up to two items.
                   11966: 
                   11967: =over
                   11968: 
                   11969: a reference to an array of files/subdirectories
                   11970: 
                   11971: =over
                   11972: 
                   11973: Each element in the array of files/subdirectories is a & separated list of
                   11974: item name and the result of running stat on the item.  If dirlist was requested
                   11975: for a file instead of a directory, the item name will be ''. For a directory 
                   11976: listing, if the item is a metadata file, the element will end &N&M 
                   11977: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   11978: default copyright set (1).  
                   11979: 
                   11980: =back
                   11981: 
                   11982: a scalar containing error condition (if encountered).
                   11983: 
                   11984: =over
                   11985: 
                   11986: =item 
                   11987: no_host (no homeserver identified for $username:$domain).
                   11988: 
                   11989: =item 
                   11990: no_such_host (server contacted for listing not identified as valid host).
                   11991: 
                   11992: =item 
                   11993: con_lost (connection to remote server failed).
                   11994: 
                   11995: =item 
                   11996: refused (invalid $username:$domain received on lond side).
                   11997: 
                   11998: =item 
                   11999: no_such_dir (directory at specified path on lond side does not exist). 
                   12000: 
                   12001: =item 
                   12002: empty (directory at specified path on lond side is empty).
                   12003: 
                   12004: =over
                   12005: 
                   12006: This is currently not encountered because the &ls3, &ls2, 
                   12007: &ls (_handler) routines on the lond side do not filter out
                   12008: . and .. from a directory listing. 
                   12009: 
                   12010: =back
                   12011: 
                   12012: =back
                   12013: 
                   12014: =back
1.191     harris41 12015: 
                   12016: =item *
                   12017: 
1.243     albertel 12018: spareserver() : find server with least workload from spare.tab
                   12019: 
1.986     foxr     12020: 
                   12021: =item *
                   12022: 
                   12023: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   12024: if there is no corresponding loncapa host.
                   12025: 
1.243     albertel 12026: =back
                   12027: 
1.986     foxr     12028: 
1.243     albertel 12029: =head2 Apache Request
                   12030: 
                   12031: =over 4
1.191     harris41 12032: 
                   12033: =item *
                   12034: 
1.243     albertel 12035: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   12036: localhost, posts hash
                   12037: 
                   12038: =back
                   12039: 
                   12040: =head2 Data to String to Data
                   12041: 
                   12042: =over 4
1.191     harris41 12043: 
                   12044: =item *
                   12045: 
1.243     albertel 12046: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   12047: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 12048: 
                   12049: =item *
                   12050: 
1.243     albertel 12051: hashref2str($hashref) : convert a hashref into a string complete with
                   12052: escaping and '=' and '&' separators, supports elements that are
                   12053: arrayrefs and hashrefs
1.191     harris41 12054: 
                   12055: =item *
                   12056: 
1.243     albertel 12057: arrayref2str($arrayref) : convert an arrayref into a string complete
                   12058: with escaping and '&' separators, supports elements that are arrayrefs
                   12059: and hashrefs
1.191     harris41 12060: 
                   12061: =item *
                   12062: 
1.243     albertel 12063: str2hash($string) : convert string to hash using unescaping and
                   12064: splitting on '=' and '&', supports elements that are arrayrefs and
                   12065: hashrefs
1.191     harris41 12066: 
                   12067: =item *
                   12068: 
1.243     albertel 12069: str2array($string) : convert string to hash using unescaping and
                   12070: splitting on '&', supports elements that are arrayrefs and hashrefs
                   12071: 
                   12072: =back
                   12073: 
                   12074: =head2 Logging Routines
                   12075: 
                   12076: 
                   12077: These routines allow one to make log messages in the lonnet.log and
                   12078: lonnet.perm logfiles.
1.191     harris41 12079: 
1.1119    foxr     12080: =over 4
                   12081: 
1.191     harris41 12082: =item *
                   12083: 
1.243     albertel 12084: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 12085: 
                   12086: =item *
                   12087: 
1.243     albertel 12088: logthis() : append message to the normal lonnet.log file, it gets
                   12089: preiodically rolled over and deleted.
1.191     harris41 12090: 
                   12091: =item *
                   12092: 
1.243     albertel 12093: logperm() : append a permanent message to lonnet.perm.log, this log
                   12094: file never gets deleted by any automated portion of the system, only
                   12095: messages of critical importance should go in here.
                   12096: 
1.1119    foxr     12097: 
1.243     albertel 12098: =back
                   12099: 
                   12100: =head2 General File Helper Routines
                   12101: 
                   12102: =over 4
1.191     harris41 12103: 
                   12104: =item *
                   12105: 
1.481     raeburn  12106: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   12107: (a) files in /uploaded
                   12108:   (i) If a local copy of the file exists - 
                   12109:       compares modification date of local copy with last-modified date for 
                   12110:       definitive version stored on home server for course. If local copy is 
                   12111:       stale, requests a new version from the home server and stores it. 
                   12112:       If the original has been removed from the home server, then local copy 
                   12113:       is unlinked.
                   12114:   (ii) If local copy does not exist -
                   12115:       requests the file from the home server and stores it. 
                   12116:   
                   12117:   If $caller is 'uploadrep':  
                   12118:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   12119:     for request for files originally uploaded via DOCS. 
                   12120:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   12121:   
                   12122:   Otherwise:
                   12123:      This indicates a call from the content generation phase of the request.
                   12124:      -  returns the entire contents of the file or -1.
                   12125:      
                   12126: (b) files in /res
                   12127:    - returns the entire contents of a file or -1; 
                   12128:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 12129: 
1.712     albertel 12130: 
                   12131: =item *
                   12132: 
                   12133: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   12134:                   reference
                   12135: 
                   12136: returns either a stat() list of data about the file or an empty list
                   12137: if the file doesn't exist or couldn't find out about it (connection
                   12138: problems or user unknown)
                   12139: 
1.191     harris41 12140: =item *
                   12141: 
1.243     albertel 12142: filelocation($dir,$file) : returns file system location of a file
                   12143: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   12144: directory that relative $file lookups are to looked in ($dir of /a/dir
                   12145: and a file of ../bob will become /a/bob)
1.191     harris41 12146: 
                   12147: =item *
                   12148: 
                   12149: hreflocation($dir,$file) : returns file system location or a URL; same as
                   12150: filelocation except for hrefs
                   12151: 
                   12152: =item *
                   12153: 
                   12154: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   12155: 
1.243     albertel 12156: =back
                   12157: 
1.608     albertel 12158: =head2 Usererfile file routines (/uploaded*)
                   12159: 
                   12160: =over 4
                   12161: 
                   12162: =item *
                   12163: 
                   12164: userfileupload(): main rotine for putting a file in a user or course's
                   12165:                   filespace, arguments are,
                   12166: 
1.620     albertel 12167:  formname - required - this is the name of the element in $env where the
1.608     albertel 12168:            filename, and the contents of the file to create/modifed exist
1.620     albertel 12169:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   12170:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  12171:  context - if coursedoc, store the file in the course of the active role
                   12172:              of the current user; 
                   12173:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   12174:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 12175:  subdir - required - subdirectory to put the file in under ../userfiles/
                   12176:          if undefined, it will be placed in "unknown"
                   12177: 
                   12178:  (This routine calls clean_filename() to remove any dangerous
                   12179:  characters from the filename, and then calls finuserfileupload() to
                   12180:  complete the transaction)
                   12181: 
                   12182:  returns either the url of the uploaded file (/uploaded/....) if successful
                   12183:  and /adm/notfound.html if unsuccessful
                   12184: 
                   12185: =item *
                   12186: 
                   12187: clean_filename(): routine for cleaing a filename up for storage in
                   12188:                  userfile space, argument is:
                   12189: 
                   12190:  filename - proposed filename
                   12191: 
                   12192: returns: the new clean filename
                   12193: 
                   12194: =item *
                   12195: 
1.1090    raeburn  12196: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 12197: userspace, probably shouldn't be called directly
                   12198: 
                   12199:   docuname: username or courseid of destination for the file
                   12200:   docudom: domain of user/course of destination for the file
                   12201:   formname: same as for userfileupload()
1.1090    raeburn  12202:   fname: filename (including subdirectories) for the file
                   12203:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
                   12204:   allfiles: reference to hash used to store objects found by parser
                   12205:   codebase: reference to hash used for codebases of java objects found by parser
                   12206:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   12207:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   12208:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   12209:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   12210:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   12211:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  12212:   mimetype: reference to scalar to accommodate mime type determined
                   12213:             from File::MMagic if $parser = parse.
1.608     albertel 12214: 
                   12215:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  12216:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   12217:  was 'overwrite').
                   12218:  
1.608     albertel 12219: 
                   12220: =item *
                   12221: 
                   12222: renameuserfile(): renames an existing userfile to a new name
                   12223: 
                   12224:   Args:
                   12225:    docuname: username or courseid of destination for the file
                   12226:    docudom: domain of user/course of destination for the file
                   12227:    old: current file name (including any subdirs under userfiles)
                   12228:    new: desired file name (including any subdirs under userfiles)
                   12229: 
                   12230: =item *
                   12231: 
                   12232: mkdiruserfile(): creates a directory is a userfiles dir
                   12233: 
                   12234:   Args:
                   12235:    docuname: username or courseid of destination for the file
                   12236:    docudom: domain of user/course of destination for the file
                   12237:    dir: dir to create (including any subdirs under userfiles)
                   12238: 
                   12239: =item *
                   12240: 
                   12241: removeuserfile(): removes a file that exists in userfiles
                   12242: 
                   12243:   Args:
                   12244:    docuname: username or courseid of destination for the file
                   12245:    docudom: domain of user/course of destination for the file
                   12246:    fname: filname to delete (including any subdirs under userfiles)
                   12247: 
                   12248: =item *
                   12249: 
                   12250: removeuploadedurl(): convience function for removeuserfile()
                   12251: 
                   12252:   Args:
                   12253:    url:  a full /uploaded/... url to delete
                   12254: 
1.747     albertel 12255: =item * 
                   12256: 
                   12257: get_portfile_permissions():
                   12258:   Args:
                   12259:     domain: domain of user or course contain the portfolio files
                   12260:     user: name of user or num of course contain the portfolio files
                   12261:   Returns:
                   12262:     hashref of a dump of the proper file_permissions.db
                   12263:    
                   12264: 
                   12265: =item * 
                   12266: 
                   12267: get_access_controls():
                   12268: 
                   12269: Args:
                   12270:   current_permissions: the hash ref returned from get_portfile_permissions()
                   12271:   group: (optional) the group you want the files associated with
                   12272:   file: (optional) the file you want access info on
                   12273: 
                   12274: Returns:
1.749     raeburn  12275:     a hash (keys are file names) of hashes containing
                   12276:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   12277:         values are XML containing access control settings (see below) 
1.747     albertel 12278: 
                   12279: Internal notes:
                   12280: 
1.749     raeburn  12281:  access controls are stored in file_permissions.db as key=value pairs.
                   12282:     key -> path to file/file_name\0uniqueID:scope_end_start
                   12283:         where scope -> public,guest,course,group,domains or users.
                   12284:               end -> UNIX time for end of access (0 -> no end date)
                   12285:               start -> UNIX time for start of access
                   12286: 
                   12287:     value -> XML description of access control
                   12288:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   12289:             <start></start>
                   12290:             <end></end>
                   12291: 
                   12292:             <password></password>  for scope type = guest
                   12293: 
                   12294:             <domain></domain>     for scope type = course or group
                   12295:             <number></number>
                   12296:             <roles id="">
                   12297:              <role></role>
                   12298:              <access></access>
                   12299:              <section></section>
                   12300:              <group></group>
                   12301:             </roles>
                   12302: 
                   12303:             <dom></dom>         for scope type = domains
                   12304: 
                   12305:             <users>             for scope type = users
                   12306:              <user>
                   12307:               <uname></uname>
                   12308:               <udom></udom>
                   12309:              </user>
                   12310:             </users>
                   12311:            </scope> 
                   12312:               
                   12313:  Access data is also aggregated for each file in an additional key=value pair:
                   12314:  key -> path to file/file_name\0accesscontrol 
                   12315:  value -> reference to hash
                   12316:           hash contains key = value pairs
                   12317:           where key = uniqueID:scope_end_start
                   12318:                 value = UNIX time record was last updated
                   12319: 
                   12320:           Used to improve speed of look-ups of access controls for each file.  
                   12321:  
                   12322:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   12323: 
                   12324: modify_access_controls():
                   12325: 
                   12326: Modifies access controls for a portfolio file
                   12327: Args
                   12328: 1. file name
                   12329: 2. reference to hash of required changes,
                   12330: 3. domain
                   12331: 4. username
                   12332:   where domain,username are the domain of the portfolio owner 
                   12333:   (either a user or a course) 
                   12334: 
                   12335: Returns:
                   12336: 1. result of additions or updates ('ok' or 'error', with error message). 
                   12337: 2. result of deletions ('ok' or 'error', with error message).
                   12338: 3. reference to hash of any new or updated access controls.
                   12339: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   12340:    key = integer (inbound ID)
                   12341:    value = uniqueID  
1.747     albertel 12342: 
1.608     albertel 12343: =back
                   12344: 
1.243     albertel 12345: =head2 HTTP Helper Routines
                   12346: 
                   12347: =over 4
                   12348: 
1.191     harris41 12349: =item *
                   12350: 
                   12351: escape() : unpack non-word characters into CGI-compatible hex codes
                   12352: 
                   12353: =item *
                   12354: 
                   12355: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   12356: 
1.243     albertel 12357: =back
                   12358: 
                   12359: =head1 PRIVATE SUBROUTINES
                   12360: 
                   12361: =head2 Underlying communication routines (Shouldn't call)
                   12362: 
                   12363: =over 4
                   12364: 
                   12365: =item *
                   12366: 
                   12367: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   12368: 
                   12369: =item *
                   12370: 
                   12371: reply() : uses subreply to send a message to remote machine, logs all failures
                   12372: 
                   12373: =item *
                   12374: 
                   12375: critical() : passes a critical message to another server; if cannot
                   12376: get through then place message in connection buffer directory and
                   12377: returns con_delayed, if incapable of saving message, returns
                   12378: con_failed
                   12379: 
                   12380: =item *
                   12381: 
                   12382: reconlonc() : tries to reconnect lonc client processes.
                   12383: 
                   12384: =back
                   12385: 
                   12386: =head2 Resource Access Logging
                   12387: 
                   12388: =over 4
                   12389: 
                   12390: =item *
                   12391: 
                   12392: flushcourselogs() : flush (save) buffer logs and access logs
                   12393: 
                   12394: =item *
                   12395: 
                   12396: courselog($what) : save message for course in hash
                   12397: 
                   12398: =item *
                   12399: 
                   12400: courseacclog($what) : save message for course using &courselog().  Perform
                   12401: special processing for specific resource types (problems, exams, quizzes, etc).
                   12402: 
1.191     harris41 12403: =item *
                   12404: 
                   12405: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   12406: as a PerlChildExitHandler
1.243     albertel 12407: 
                   12408: =back
                   12409: 
                   12410: =head2 Other
                   12411: 
                   12412: =over 4
                   12413: 
                   12414: =item *
                   12415: 
                   12416: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 12417: 
                   12418: =back
                   12419: 
                   12420: =cut
1.877     foxr     12421: 

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