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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1154  ! raeburn     4: # $Id: lonnet.pm,v 1.1153 2012/02/15 21:56:25 www 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 {
1.1154  ! raeburn  3873:     my ($uname,$udom)=@_;
        !          3874:     if (($cachedkey eq $uname.':'.$udom.':'.$courseid) &&
        !          3875:         (abs($cachedtime-time)<5)) {
        !          3876:         return;
        !          3877:     }
        !          3878:     $cachedtime=time;
        !          3879:     $cachedkey=$uname.':'.$udom;
        !          3880:     %cachedtimes=&dump('firstaccesstimes',$udom,$uname);
1.1153    www      3881: }
                   3882: 
1.504     albertel 3883: sub get_first_access {
                   3884:     my ($type,$argsymb)=@_;
1.790     albertel 3885:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3886:     if ($argsymb) { $symb=$argsymb; }
                   3887:     my ($map,$id,$res)=&decode_symb($symb);
1.926     albertel 3888:     if ($type eq 'course') {
                   3889: 	$res='course';
                   3890:     } elsif ($type eq 'map') {
1.588     albertel 3891: 	$res=&symbread($map);
                   3892:     } else {
                   3893: 	$res=$symb;
                   3894:     }
1.1153    www      3895:     &load_all_first_access($uname,$udom);
                   3896:     return $cachedtimes{"$courseid\0$res"};
1.504     albertel 3897: }
                   3898: 
                   3899: sub set_first_access {
                   3900:     my ($type)=@_;
1.790     albertel 3901:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3902:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 3903:     if ($type eq 'course') {
                   3904: 	$res='course';
                   3905:     } elsif ($type eq 'map') {
1.588     albertel 3906: 	$res=&symbread($map);
                   3907:     } else {
                   3908: 	$res=$symb;
                   3909:     }
1.1153    www      3910:     $cachedkey='';
1.588     albertel 3911:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 3912:     if (!$firstaccess) {
1.588     albertel 3913: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 3914:     }
                   3915:     return 'already_set';
1.504     albertel 3916: }
1.1153    www      3917: }
1.110     www      3918: # --------------------------------------------- Set Expire Date for Spreadsheet
                   3919: 
                   3920: sub expirespread {
                   3921:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 3922:     my $cid=$env{'request.course.id'}; 
1.110     www      3923:     if ($cid) {
                   3924:        my $now=time;
                   3925:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 3926:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   3927:                             $env{'course.'.$cid.'.num'}.
1.110     www      3928: 	        	    ':nohist_expirationdates:'.
                   3929:                             &escape($key).'='.$now,
1.620     albertel 3930:                             $env{'course.'.$cid.'.home'})
1.110     www      3931:     }
                   3932:     return 'ok';
1.14      www      3933: }
                   3934: 
1.109     www      3935: # ----------------------------------------------------- Devalidate Spreadsheets
                   3936: 
                   3937: sub devalidate {
1.325     www      3938:     my ($symb,$uname,$udom)=@_;
1.620     albertel 3939:     my $cid=$env{'request.course.id'}; 
1.109     www      3940:     if ($cid) {
1.391     matthew  3941:         # delete the stored spreadsheets for
                   3942:         # - the student level sheet of this user in course's homespace
                   3943:         # - the assessment level sheet for this resource 
                   3944:         #   for this user in user's homespace
1.553     albertel 3945: 	# - current conditional state info
1.325     www      3946: 	my $key=$uname.':'.$udom.':';
1.109     www      3947:         my $status=
1.299     matthew  3948: 	    &del('nohist_calculatedsheets',
1.391     matthew  3949: 		 [$key.'studentcalc:'],
1.620     albertel 3950: 		 $env{'course.'.$cid.'.domain'},
                   3951: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 3952: 		.' '.
                   3953: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  3954: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      3955:         unless ($status eq 'ok ok') {
                   3956:            &logthis('Could not devalidate spreadsheet '.
1.325     www      3957:                     $uname.' at '.$udom.' for '.
1.109     www      3958: 		    $symb.': '.$status);
1.133     albertel 3959:         }
1.553     albertel 3960: 	&delenv('user.state.'.$cid);
1.109     www      3961:     }
                   3962: }
                   3963: 
1.265     albertel 3964: sub get_scalar {
                   3965:     my ($string,$end) = @_;
                   3966:     my $value;
                   3967:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   3968: 	$value = $1;
                   3969:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   3970: 	$value = $1;
                   3971:     }
                   3972:     return &unescape($value);
                   3973: }
                   3974: 
                   3975: sub array2str {
                   3976:   my (@array) = @_;
                   3977:   my $result=&arrayref2str(\@array);
                   3978:   $result=~s/^__ARRAY_REF__//;
                   3979:   $result=~s/__END_ARRAY_REF__$//;
                   3980:   return $result;
                   3981: }
                   3982: 
1.204     albertel 3983: sub arrayref2str {
                   3984:   my ($arrayref) = @_;
1.265     albertel 3985:   my $result='__ARRAY_REF__';
1.204     albertel 3986:   foreach my $elem (@$arrayref) {
1.265     albertel 3987:     if(ref($elem) eq 'ARRAY') {
                   3988:       $result.=&arrayref2str($elem).'&';
                   3989:     } elsif(ref($elem) eq 'HASH') {
                   3990:       $result.=&hashref2str($elem).'&';
                   3991:     } elsif(ref($elem)) {
                   3992:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 3993:     } else {
                   3994:       $result.=&escape($elem).'&';
                   3995:     }
                   3996:   }
                   3997:   $result=~s/\&$//;
1.265     albertel 3998:   $result .= '__END_ARRAY_REF__';
1.204     albertel 3999:   return $result;
                   4000: }
                   4001: 
1.168     albertel 4002: sub hash2str {
1.204     albertel 4003:   my (%hash) = @_;
                   4004:   my $result=&hashref2str(\%hash);
1.265     albertel 4005:   $result=~s/^__HASH_REF__//;
                   4006:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 4007:   return $result;
                   4008: }
                   4009: 
                   4010: sub hashref2str {
                   4011:   my ($hashref)=@_;
1.265     albertel 4012:   my $result='__HASH_REF__';
1.800     albertel 4013:   foreach my $key (sort(keys(%$hashref))) {
                   4014:     if (ref($key) eq 'ARRAY') {
                   4015:       $result.=&arrayref2str($key).'=';
                   4016:     } elsif (ref($key) eq 'HASH') {
                   4017:       $result.=&hashref2str($key).'=';
                   4018:     } elsif (ref($key)) {
1.265     albertel 4019:       $result.='=';
1.800     albertel 4020:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 4021:     } else {
1.1132    raeburn  4022: 	if (defined($key)) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 4023:     }
                   4024: 
1.800     albertel 4025:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   4026:       $result.=&arrayref2str($hashref->{$key}).'&';
                   4027:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   4028:       $result.=&hashref2str($hashref->{$key}).'&';
                   4029:     } elsif(ref($hashref->{$key})) {
1.265     albertel 4030:        $result.='&';
1.800     albertel 4031:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 4032:     } else {
1.800     albertel 4033:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 4034:     }
                   4035:   }
1.168     albertel 4036:   $result=~s/\&$//;
1.265     albertel 4037:   $result .= '__END_HASH_REF__';
1.168     albertel 4038:   return $result;
                   4039: }
                   4040: 
                   4041: sub str2hash {
1.265     albertel 4042:     my ($string)=@_;
                   4043:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   4044:     return %$hash;
                   4045: }
                   4046: 
                   4047: sub str2hashref {
1.168     albertel 4048:   my ($string) = @_;
1.265     albertel 4049: 
                   4050:   my %hash;
                   4051: 
                   4052:   if($string !~ /^__HASH_REF__/) {
                   4053:       if (! ($string eq '' || !defined($string))) {
                   4054: 	  $hash{'error'}='Not hash reference';
                   4055:       }
                   4056:       return (\%hash, $string);
                   4057:   }
                   4058: 
                   4059:   $string =~ s/^__HASH_REF__//;
                   4060: 
                   4061:   while($string !~ /^__END_HASH_REF__/) {
                   4062:       #key
                   4063:       my $key='';
                   4064:       if($string =~ /^__HASH_REF__/) {
                   4065:           ($key, $string)=&str2hashref($string);
                   4066:           if(defined($key->{'error'})) {
                   4067:               $hash{'error'}='Bad data';
                   4068:               return (\%hash, $string);
                   4069:           }
                   4070:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4071:           ($key, $string)=&str2arrayref($string);
                   4072:           if($key->[0] eq 'Array reference error') {
                   4073:               $hash{'error'}='Bad data';
                   4074:               return (\%hash, $string);
                   4075:           }
                   4076:       } else {
                   4077:           $string =~ s/^(.*?)=//;
1.267     albertel 4078: 	  $key=&unescape($1);
1.265     albertel 4079:       }
                   4080:       $string =~ s/^=//;
                   4081: 
                   4082:       #value
                   4083:       my $value='';
                   4084:       if($string =~ /^__HASH_REF__/) {
                   4085:           ($value, $string)=&str2hashref($string);
                   4086:           if(defined($value->{'error'})) {
                   4087:               $hash{'error'}='Bad data';
                   4088:               return (\%hash, $string);
                   4089:           }
                   4090:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4091:           ($value, $string)=&str2arrayref($string);
                   4092:           if($value->[0] eq 'Array reference error') {
                   4093:               $hash{'error'}='Bad data';
                   4094:               return (\%hash, $string);
                   4095:           }
                   4096:       } else {
                   4097: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   4098:       }
                   4099:       $string =~ s/^&//;
                   4100: 
                   4101:       $hash{$key}=$value;
1.204     albertel 4102:   }
1.265     albertel 4103: 
                   4104:   $string =~ s/^__END_HASH_REF__//;
                   4105: 
                   4106:   return (\%hash, $string);
1.204     albertel 4107: }
                   4108: 
                   4109: sub str2array {
1.265     albertel 4110:     my ($string)=@_;
                   4111:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   4112:     return @$array;
                   4113: }
                   4114: 
                   4115: sub str2arrayref {
1.204     albertel 4116:   my ($string) = @_;
1.265     albertel 4117:   my @array;
                   4118: 
                   4119:   if($string !~ /^__ARRAY_REF__/) {
                   4120:       if (! ($string eq '' || !defined($string))) {
                   4121: 	  $array[0]='Array reference error';
                   4122:       }
                   4123:       return (\@array, $string);
                   4124:   }
                   4125: 
                   4126:   $string =~ s/^__ARRAY_REF__//;
                   4127: 
                   4128:   while($string !~ /^__END_ARRAY_REF__/) {
                   4129:       my $value='';
                   4130:       if($string =~ /^__HASH_REF__/) {
                   4131:           ($value, $string)=&str2hashref($string);
                   4132:           if(defined($value->{'error'})) {
                   4133:               $array[0] ='Array reference error';
                   4134:               return (\@array, $string);
                   4135:           }
                   4136:       } elsif($string =~ /^__ARRAY_REF__/) {
                   4137:           ($value, $string)=&str2arrayref($string);
                   4138:           if($value->[0] eq 'Array reference error') {
                   4139:               $array[0] ='Array reference error';
                   4140:               return (\@array, $string);
                   4141:           }
                   4142:       } else {
                   4143: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   4144:       }
                   4145:       $string =~ s/^&//;
                   4146: 
                   4147:       push(@array, $value);
1.191     harris41 4148:   }
1.265     albertel 4149: 
                   4150:   $string =~ s/^__END_ARRAY_REF__//;
                   4151: 
                   4152:   return (\@array, $string);
1.168     albertel 4153: }
                   4154: 
1.167     albertel 4155: # -------------------------------------------------------------------Temp Store
                   4156: 
1.168     albertel 4157: sub tmpreset {
                   4158:   my ($symb,$namespace,$domain,$stuname) = @_;
                   4159:   if (!$symb) {
                   4160:     $symb=&symbread();
1.620     albertel 4161:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4162:   }
                   4163:   $symb=escape($symb);
                   4164: 
1.620     albertel 4165:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 4166:   $namespace=~s/\//\_/g;
                   4167:   $namespace=~s/\W//g;
                   4168: 
1.620     albertel 4169:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4170:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4171:   if ($domain eq 'public' && $stuname eq 'public') {
                   4172:       $stuname=$ENV{'REMOTE_ADDR'};
                   4173:   }
1.1117    foxr     4174:   my $path=LONCAPA::tempdir();
1.168     albertel 4175:   my %hash;
                   4176:   if (tie(%hash,'GDBM_File',
                   4177: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4178: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  4179:     foreach my $key (keys(%hash)) {
1.180     albertel 4180:       if ($key=~ /:$symb/) {
1.168     albertel 4181: 	delete($hash{$key});
                   4182:       }
                   4183:     }
                   4184:   }
                   4185: }
                   4186: 
1.167     albertel 4187: sub tmpstore {
1.168     albertel 4188:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4189: 
                   4190:   if (!$symb) {
                   4191:     $symb=&symbread();
1.620     albertel 4192:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4193:   }
                   4194:   $symb=escape($symb);
                   4195: 
                   4196:   if (!$namespace) {
                   4197:     # I don't think we would ever want to store this for a course.
                   4198:     # it seems this will only be used if we don't have a course.
1.620     albertel 4199:     #$namespace=$env{'request.course.id'};
1.168     albertel 4200:     #if (!$namespace) {
1.620     albertel 4201:       $namespace=$env{'request.state'};
1.168     albertel 4202:     #}
                   4203:   }
                   4204:   $namespace=~s/\//\_/g;
                   4205:   $namespace=~s/\W//g;
1.620     albertel 4206:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4207:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4208:   if ($domain eq 'public' && $stuname eq 'public') {
                   4209:       $stuname=$ENV{'REMOTE_ADDR'};
                   4210:   }
1.168     albertel 4211:   my $now=time;
                   4212:   my %hash;
1.1117    foxr     4213:   my $path=LONCAPA::tempdir();
1.168     albertel 4214:   if (tie(%hash,'GDBM_File',
                   4215: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4216: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 4217:     $hash{"version:$symb"}++;
                   4218:     my $version=$hash{"version:$symb"};
                   4219:     my $allkeys=''; 
                   4220:     foreach my $key (keys(%$storehash)) {
                   4221:       $allkeys.=$key.':';
1.591     albertel 4222:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 4223:     }
                   4224:     $hash{"$version:$symb:timestamp"}=$now;
                   4225:     $allkeys.='timestamp';
                   4226:     $hash{"$version:keys:$symb"}=$allkeys;
                   4227:     if (untie(%hash)) {
                   4228:       return 'ok';
                   4229:     } else {
                   4230:       return "error:$!";
                   4231:     }
                   4232:   } else {
                   4233:     return "error:$!";
                   4234:   }
                   4235: }
1.167     albertel 4236: 
1.168     albertel 4237: # -----------------------------------------------------------------Temp Restore
1.167     albertel 4238: 
1.168     albertel 4239: sub tmprestore {
                   4240:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 4241: 
1.168     albertel 4242:   if (!$symb) {
                   4243:     $symb=&symbread();
1.620     albertel 4244:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 4245:   }
                   4246:   $symb=escape($symb);
                   4247: 
1.620     albertel 4248:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 4249: 
1.620     albertel 4250:   if (!$domain) { $domain=$env{'user.domain'}; }
                   4251:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 4252:   if ($domain eq 'public' && $stuname eq 'public') {
                   4253:       $stuname=$ENV{'REMOTE_ADDR'};
                   4254:   }
1.168     albertel 4255:   my %returnhash;
                   4256:   $namespace=~s/\//\_/g;
                   4257:   $namespace=~s/\W//g;
                   4258:   my %hash;
1.1117    foxr     4259:   my $path=LONCAPA::tempdir();
1.168     albertel 4260:   if (tie(%hash,'GDBM_File',
                   4261: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 4262: 	  &GDBM_READER(),0640)) {
1.168     albertel 4263:     my $version=$hash{"version:$symb"};
                   4264:     $returnhash{'version'}=$version;
                   4265:     my $scope;
                   4266:     for ($scope=1;$scope<=$version;$scope++) {
                   4267:       my $vkeys=$hash{"$scope:keys:$symb"};
                   4268:       my @keys=split(/:/,$vkeys);
                   4269:       my $key;
                   4270:       $returnhash{"$scope:keys"}=$vkeys;
                   4271:       foreach $key (@keys) {
1.591     albertel 4272: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   4273: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 4274:       }
                   4275:     }
1.168     albertel 4276:     if (!(untie(%hash))) {
                   4277:       return "error:$!";
                   4278:     }
                   4279:   } else {
                   4280:     return "error:$!";
                   4281:   }
                   4282:   return %returnhash;
1.167     albertel 4283: }
                   4284: 
1.9       www      4285: # ----------------------------------------------------------------------- Store
                   4286: 
                   4287: sub store {
1.124     www      4288:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4289:     my $home='';
                   4290: 
1.168     albertel 4291:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4292: 
1.213     www      4293:     $symb=&symbclean($symb);
1.122     albertel 4294:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4295: 
1.620     albertel 4296:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4297:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4298: 
                   4299:     &devalidate($symb,$stuname,$domain);
1.109     www      4300: 
                   4301:     $symb=escape($symb);
1.187     www      4302:     if (!$namespace) { 
1.620     albertel 4303:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4304:           return ''; 
                   4305:        } 
                   4306:     }
1.620     albertel 4307:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4308: 
                   4309:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4310:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   4311: 
1.12      www      4312:     my $namevalue='';
1.800     albertel 4313:     foreach my $key (keys(%$storehash)) {
                   4314:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4315:     }
1.12      www      4316:     $namevalue=~s/\&$//;
1.187     www      4317:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      4318:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      4319: }
                   4320: 
1.47      www      4321: # -------------------------------------------------------------- Critical Store
                   4322: 
                   4323: sub cstore {
1.124     www      4324:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   4325:     my $home='';
                   4326: 
1.168     albertel 4327:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4328: 
1.213     www      4329:     $symb=&symbclean($symb);
1.122     albertel 4330:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      4331: 
1.620     albertel 4332:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4333:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      4334: 
                   4335:     &devalidate($symb,$stuname,$domain);
1.109     www      4336: 
                   4337:     $symb=escape($symb);
1.187     www      4338:     if (!$namespace) { 
1.620     albertel 4339:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      4340:           return ''; 
                   4341:        } 
                   4342:     }
1.620     albertel 4343:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      4344: 
                   4345:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   4346:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 4347: 
1.47      www      4348:     my $namevalue='';
1.800     albertel 4349:     foreach my $key (keys(%$storehash)) {
                   4350:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 4351:     }
1.47      www      4352:     $namevalue=~s/\&$//;
1.187     www      4353:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      4354:     return critical
                   4355:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      4356: }
                   4357: 
1.9       www      4358: # --------------------------------------------------------------------- Restore
                   4359: 
                   4360: sub restore {
1.124     www      4361:     my ($symb,$namespace,$domain,$stuname) = @_;
                   4362:     my $home='';
                   4363: 
1.168     albertel 4364:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      4365: 
1.122     albertel 4366:     if (!$symb) {
                   4367:       unless ($symb=escape(&symbread())) { return ''; }
                   4368:     } else {
1.213     www      4369:       $symb=&escape(&symbclean($symb));
1.122     albertel 4370:     }
1.188     www      4371:     if (!$namespace) { 
1.620     albertel 4372:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      4373:           return ''; 
                   4374:        } 
                   4375:     }
1.620     albertel 4376:     if (!$domain) { $domain=$env{'user.domain'}; }
                   4377:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   4378:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 4379:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   4380: 
1.12      www      4381:     my %returnhash=();
1.800     albertel 4382:     foreach my $line (split(/\&/,$answer)) {
                   4383: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 4384:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 4385:     }
1.75      www      4386:     my $version;
                   4387:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 4388:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   4389:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 4390:        }
1.75      www      4391:     }
1.13      www      4392:     return %returnhash;
1.34      www      4393: }
                   4394: 
                   4395: # ---------------------------------------------------------- Course Description
1.1118    foxr     4396: #
                   4397: #  
1.34      www      4398: 
                   4399: sub coursedescription {
1.731     albertel 4400:     my ($courseid,$args)=@_;
1.34      www      4401:     $courseid=~s/^\///;
1.49      www      4402:     $courseid=~s/\_/\//g;
1.34      www      4403:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 4404:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 4405:     my $normalid=$cdomain.'_'.$cnum;
                   4406:     # need to always cache even if we get errors otherwise we keep 
                   4407:     # trying and trying and trying to get the course description.
                   4408:     my %envhash=();
                   4409:     my %returnhash=();
1.731     albertel 4410:     
                   4411:     my $expiretime=600;
                   4412:     if ($env{'request.course.id'} eq $normalid) {
                   4413: 	$expiretime=120;
                   4414:     }
                   4415: 
                   4416:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   4417:     if (!$args->{'freshen_cache'}
                   4418: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   4419: 	foreach my $key (keys(%env)) {
                   4420: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   4421: 	    my ($setting) = $1;
                   4422: 	    $returnhash{$setting} = $env{$key};
                   4423: 	}
                   4424: 	return %returnhash;
                   4425:     }
                   4426: 
1.1118    foxr     4427:     # get the data again
                   4428: 
1.731     albertel 4429:     if (!$args->{'one_time'}) {
                   4430: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   4431:     }
1.811     albertel 4432: 
1.34      www      4433:     if ($chome ne 'no_host') {
1.302     albertel 4434:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 4435:        if (!exists($returnhash{'con_lost'})) {
1.1118    foxr     4436: 	   my $username = $env{'user.name'}; # Defult username
                   4437: 	   if(defined $args->{'user'}) {
                   4438: 	       $username = $args->{'user'};
                   4439: 	   }
1.129     albertel 4440:            $returnhash{'home'}= $chome;
                   4441: 	   $returnhash{'domain'} = $cdomain;
                   4442: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  4443:            if (!defined($returnhash{'type'})) {
                   4444:                $returnhash{'type'} = 'Course';
                   4445:            }
1.130     albertel 4446:            while (my ($name,$value) = each %returnhash) {
1.53      www      4447:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 4448:            }
1.270     www      4449:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.1117    foxr     4450:            $returnhash{'fn'}=LONCAPA::tempdir() .
1.1118    foxr     4451: 	       $username.'_'.$cdomain.'_'.$cnum;
1.60      www      4452:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   4453:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   4454:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      4455:        }
                   4456:     }
1.731     albertel 4457:     if (!$args->{'one_time'}) {
1.949     raeburn  4458: 	&appenv(\%envhash);
1.731     albertel 4459:     }
1.302     albertel 4460:     return %returnhash;
1.461     www      4461: }
                   4462: 
1.1080    raeburn  4463: sub update_released_required {
                   4464:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   4465:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   4466:         $cid = $env{'request.course.id'};
                   4467:         $cdom = $env{'course.'.$cid.'.domain'};
                   4468:         $cnum = $env{'course.'.$cid.'.num'};
                   4469:         $chome = $env{'course.'.$cid.'.home'};
                   4470:     }
                   4471:     if ($needsrelease) {
                   4472:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   4473:         my $needsupdate;
                   4474:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   4475:             $needsupdate = 1;
                   4476:         } else {
                   4477:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   4478:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   4479:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   4480:                 $needsupdate = 1;
                   4481:             }
                   4482:         }
                   4483:         if ($needsupdate) {
                   4484:             my %needshash = (
                   4485:                              'internal.releaserequired' => $needsrelease,
                   4486:                             );
                   4487:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   4488:             if ($putresult eq 'ok') {
                   4489:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   4490:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   4491:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   4492:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   4493:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   4494:                 }
                   4495:             }
                   4496:         }
                   4497:     }
                   4498:     return;
                   4499: }
                   4500: 
1.461     www      4501: # -------------------------------------------------See if a user is privileged
                   4502: 
                   4503: sub privileged {
                   4504:     my ($username,$domain)=@_;
                   4505:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   4506: 			&homeserver($username,$domain));
1.1033    raeburn  4507:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
                   4508:         ($rolesdump =~ /^error:/)) {
                   4509:         return 0;
                   4510:     }
1.461     www      4511:     my $now=time;
                   4512:     if ($rolesdump ne '') {
1.800     albertel 4513:         foreach my $entry (split(/&/,$rolesdump)) {
                   4514: 	    if ($entry!~/^rolesdef_/) {
                   4515: 		my ($area,$role)=split(/=/,$entry);
1.461     www      4516: 		$area=~s/\_\w\w$//;
                   4517: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   4518: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   4519: 		    my $active=1;
                   4520: 		    if ($tend) {
                   4521: 			if ($tend<$now) { $active=0; }
                   4522: 		    }
                   4523: 		    if ($tstart) {
                   4524: 			if ($tstart>$now) { $active=0; }
                   4525: 		    }
                   4526: 		    if ($active) { return 1; }
                   4527: 		}
                   4528: 	    }
                   4529: 	}
                   4530:     }
                   4531:     return 0;
1.9       www      4532: }
1.1       albertel 4533: 
1.103     harris41 4534: # -------------------------------------------------------- Get user privileges
1.11      www      4535: 
                   4536: sub rolesinit {
                   4537:     my ($domain,$username,$authhost)=@_;
1.1034    raeburn  4538:     my $now=time;
                   4539:     my %userroles = ('user.login.time' => $now);
1.1086    raeburn  4540:     my $extra = &freeze_escape({'skipcheck' => 1});
1.1078    raeburn  4541:     my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033    raeburn  4542:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
1.1078    raeburn  4543:         ($rolesdump =~ /^error:/)) {
1.1033    raeburn  4544:         return \%userroles;
                   4545:     }
1.11      www      4546:     my %allroles=();
1.678     raeburn  4547:     my %allgroups=();   
1.11      www      4548: 
                   4549:     if ($rolesdump ne '') {
1.800     albertel 4550:         foreach my $entry (split(/&/,$rolesdump)) {
                   4551: 	  if ($entry!~/^rolesdef_/) {
                   4552:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 4553: 	    $area=~s/\_\w\w$//;
1.678     raeburn  4554:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 4555: 	    if ($role=~/^cr/) { 
1.807     albertel 4556: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   4557: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 4558: 		    ($tend,$tstart)=split('_',$trest);
                   4559: 		} else {
                   4560: 		    $trole=$role;
                   4561: 		}
1.678     raeburn  4562:             } elsif ($role =~ m|^gr/|) {
                   4563:                 ($trole,$tend,$tstart) = split(/_/,$role);
1.1104    raeburn  4564:                 next if ($tstart eq '-1');
1.678     raeburn  4565:                 ($trole,$group_privs) = split(/\//,$trole);
                   4566:                 $group_privs = &unescape($group_privs);
1.587     albertel 4567: 	    } else {
                   4568: 		($trole,$tend,$tstart)=split(/_/,$role);
                   4569: 	    }
1.743     albertel 4570: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   4571: 					 $username);
                   4572: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  4573:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   4574:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      4575:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 4576: 		my $spec=$trole.'.'.$area;
                   4577: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   4578: 		if ($trole =~ /^cr\//) {
1.567     raeburn  4579:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  4580:                 } elsif ($trole eq 'gr') {
                   4581:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 4582: 		} else {
1.567     raeburn  4583:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 4584: 		}
1.12      www      4585:             }
1.662     raeburn  4586:           }
1.191     harris41 4587:         }
1.743     albertel 4588:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   4589:         $userroles{'user.adv'}    = $adv;
                   4590: 	$userroles{'user.author'} = $author;
1.620     albertel 4591:         $env{'user.adv'}=$adv;
1.11      www      4592:     }
1.743     albertel 4593:     return \%userroles;  
1.11      www      4594: }
                   4595: 
1.567     raeburn  4596: sub set_arearole {
                   4597:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   4598: # log the associated role with the area
                   4599:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 4600:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  4601: }
                   4602: 
                   4603: sub custom_roleprivs {
                   4604:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   4605:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   4606:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 4607:     if (&hostname($homsvr) ne '') {
1.567     raeburn  4608:         my ($rdummy,$roledef)=
                   4609:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   4610:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   4611:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   4612:             if (defined($syspriv)) {
1.1043    raeburn  4613:                 if ($trest =~ /^$match_community$/) {
                   4614:                     $syspriv =~ s/bre\&S//; 
                   4615:                 }
1.567     raeburn  4616:                 $$allroles{'cm./'}.=':'.$syspriv;
                   4617:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   4618:             }
                   4619:             if ($tdomain ne '') {
                   4620:                 if (defined($dompriv)) {
                   4621:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   4622:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   4623:                 }
                   4624:                 if (($trest ne '') && (defined($coursepriv))) {
                   4625:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   4626:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   4627:                 }
                   4628:             }
                   4629:         }
                   4630:     }
                   4631: }
                   4632: 
1.678     raeburn  4633: sub group_roleprivs {
                   4634:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   4635:     my $access = 1;
                   4636:     my $now = time;
                   4637:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   4638:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   4639:     if ($access) {
1.811     albertel 4640:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  4641:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   4642:     }
                   4643: }
1.567     raeburn  4644: 
                   4645: sub standard_roleprivs {
                   4646:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   4647:     if (defined($pr{$trole.':s'})) {
                   4648:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   4649:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   4650:     }
                   4651:     if ($tdomain ne '') {
                   4652:         if (defined($pr{$trole.':d'})) {
                   4653:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4654:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4655:         }
                   4656:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   4657:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   4658:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   4659:         }
                   4660:     }
                   4661: }
                   4662: 
                   4663: sub set_userprivs {
1.1064    raeburn  4664:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  4665:     my $author=0;
                   4666:     my $adv=0;
1.678     raeburn  4667:     my %grouproles = ();
                   4668:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  4669:         my @groupkeys; 
1.1000    raeburn  4670:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  4671:             push(@groupkeys,$role);
                   4672:         }
                   4673:         if (ref($groups_roles) eq 'HASH') {
                   4674:             foreach my $key (keys(%{$groups_roles})) {
                   4675:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   4676:                     push(@groupkeys,$key);
                   4677:                 }
                   4678:             }
                   4679:         }
                   4680:         if (@groupkeys > 0) {
                   4681:             foreach my $role (@groupkeys) {
                   4682:                 my ($trole,$area,$sec,$extendedarea);
                   4683:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   4684:                     $trole = $1;
                   4685:                     $area = $2;
                   4686:                     $sec = $3;
                   4687:                     $extendedarea = $area.$sec;
                   4688:                     if (exists($$allgroups{$area})) {
                   4689:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   4690:                             my $spec = $trole.'.'.$extendedarea;
                   4691:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  4692:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  4693:                         }
1.678     raeburn  4694:                     }
                   4695:                 }
                   4696:             }
                   4697:         }
                   4698:     }
1.800     albertel 4699:     foreach my $group (keys(%grouproles)) {
                   4700:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  4701:     }
1.800     albertel 4702:     foreach my $role (keys(%{$allroles})) {
                   4703:         my %thesepriv;
1.941     raeburn  4704:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 4705:         foreach my $item (split(/:/,$$allroles{$role})) {
                   4706:             if ($item ne '') {
                   4707:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  4708:                 if ($restrictions eq '') {
                   4709:                     $thesepriv{$privilege}='F';
                   4710:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   4711:                     $thesepriv{$privilege}.=$restrictions;
                   4712:                 }
                   4713:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   4714:             }
                   4715:         }
                   4716:         my $thesestr='';
1.1104    raeburn  4717:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 4718: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   4719: 	}
                   4720:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  4721:     }
                   4722:     return ($author,$adv);
                   4723: }
                   4724: 
1.994     raeburn  4725: sub role_status {
1.1104    raeburn  4726:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  4727:     my @pwhere = ();
                   4728:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
                   4729:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                   4730:         unless (!defined($$role) || $$role eq '') {
                   4731:             $$where=join('.',@pwhere);
                   4732:             $$trolecode=$$role.'.'.$$where;
                   4733:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   4734:             $$tstatus='is';
1.1104    raeburn  4735:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  4736:                 $$tstatus='future';
1.1034    raeburn  4737:                 if ($$tstart<$now) {
                   4738:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  4739:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  4740:                             my (%allroles,%allgroups,$group_privs,
                   4741:                                 %groups_roles,@rolecodes);
1.1002    raeburn  4742:                             my %userroles = (
                   4743:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   4744:                             );
1.1064    raeburn  4745:                             @rolecodes = ('cm'); 
1.1002    raeburn  4746:                             my $spec=$$role.'.'.$$where;
                   4747:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   4748:                             if ($$role =~ /^cr\//) {
                   4749:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  4750:                                 push(@rolecodes,'cr');
1.1002    raeburn  4751:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  4752:                                 push(@rolecodes,$$role);
1.1002    raeburn  4753:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   4754:                                                     $env{'user.name'});
1.1064    raeburn  4755:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  4756:                                 (undef,my $group_privs) = split(/\//,$trole);
                   4757:                                 $group_privs = &unescape($group_privs);
                   4758:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  4759:                                 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  4760:                                 &get_groups_roles($tdomain,$trest,
                   4761:                                                   \%course_roles,\@rolecodes,
                   4762:                                                   \%groups_roles);
1.1002    raeburn  4763:                             } else {
1.1064    raeburn  4764:                                 push(@rolecodes,$$role);
1.1002    raeburn  4765:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   4766:                             }
1.1064    raeburn  4767:                             my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   4768:                             &appenv(\%userroles,\@rolecodes);
1.1002    raeburn  4769:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
                   4770:                         }
                   4771:                     }
1.1034    raeburn  4772:                     $$tstatus = 'is';
1.1002    raeburn  4773:                 }
1.994     raeburn  4774:             }
                   4775:             if ($$tend) {
1.1104    raeburn  4776:                 if ($$tend<$update) {
1.994     raeburn  4777:                     $$tstatus='expired';
                   4778:                 } elsif ($$tend<$now) {
                   4779:                     $$tstatus='will_not';
                   4780:                 }
                   4781:             }
                   4782:         }
                   4783:     }
                   4784: }
                   4785: 
1.1104    raeburn  4786: sub get_groups_roles {
                   4787:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   4788:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   4789:                   (ref($rolecodes) eq 'ARRAY') && 
                   4790:                   (ref($groups_roles) eq 'HASH')); 
                   4791:     if (keys(%{$cdom_courseroles}) > 0) {
                   4792:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   4793:         if ($cdom ne '' && $cnum ne '') {
                   4794:             foreach my $key (keys(%{$cdom_courseroles})) {
                   4795:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   4796:                     my $crsrole = $1;
                   4797:                     my $crssec = $2;
                   4798:                     if ($crsrole =~ /^cr/) {
                   4799:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   4800:                             push(@{$rolecodes},'cr');
                   4801:                         }
                   4802:                     } else {
                   4803:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   4804:                             push(@{$rolecodes},$crsrole);
                   4805:                         }
                   4806:                     }
                   4807:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   4808:                     if ($crssec ne '') {
                   4809:                         $rolekey .= "/$crssec";
                   4810:                     }
                   4811:                     $rolekey .= './';
                   4812:                     $groups_roles->{$rolekey} = $rolecodes;
                   4813:                 }
                   4814:             }
                   4815:         }
                   4816:     }
                   4817:     return;
                   4818: }
                   4819: 
                   4820: sub delete_env_groupprivs {
                   4821:     my ($where,$courseroles,$possroles) = @_;
                   4822:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   4823:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   4824:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   4825:         %{$courseroles->{$udom}} =
                   4826:             &get_my_roles('','','userroles',['active'],
                   4827:                           $possroles,[$udom],1);
                   4828:     }
                   4829:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   4830:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   4831:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   4832:             my $area = '/'.$cdom.'/'.$cnum;
                   4833:             my $privkey = "user.priv.$crsrole.$area";
                   4834:             if ($crssec ne '') {
                   4835:                 $privkey .= '/'.$crssec;
                   4836:             }
                   4837:             $privkey .= ".$area/$group";
                   4838:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   4839:         }
                   4840:     }
                   4841:     return;
                   4842: }
                   4843: 
1.994     raeburn  4844: sub check_adhoc_privs {
1.1104    raeburn  4845:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller) = @_;
1.994     raeburn  4846:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
                   4847:     if ($env{$cckey}) {
                   4848:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  4849:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  4850:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1088    raeburn  4851:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4852:         }
                   4853:     } else {
1.1088    raeburn  4854:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4855:     }
                   4856: }
                   4857: 
                   4858: sub set_adhoc_privileges {
                   4859: # role can be cc or ca
1.1088    raeburn  4860:     my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994     raeburn  4861:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   4862:     my $spec = $role.'.'.$area;
                   4863:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
                   4864:                                   $env{'user.name'});
                   4865:     my %ccrole = ();
                   4866:     &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
                   4867:     my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
                   4868:     &appenv(\%userroles,[$role,'cm']);
                   4869:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1088    raeburn  4870:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
                   4871:         &appenv( {'request.role'        => $spec,
                   4872:                   'request.role.domain' => $dcdom,
                   4873:                   'request.course.sec'  => ''
                   4874:                  }
                   4875:                );
                   4876:         my $tadv=0;
                   4877:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   4878:         &appenv({'request.role.adv'    => $tadv});
                   4879:     }
1.994     raeburn  4880: }
                   4881: 
1.12      www      4882: # --------------------------------------------------------------- get interface
                   4883: 
                   4884: sub get {
1.131     albertel 4885:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      4886:    my $items='';
1.800     albertel 4887:    foreach my $item (@$storearr) {
                   4888:        $items.=&escape($item).'&';
1.191     harris41 4889:    }
1.12      www      4890:    $items=~s/\&$//;
1.620     albertel 4891:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4892:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 4893:    my $uhome=&homeserver($uname,$udomain);
                   4894: 
1.133     albertel 4895:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4896:    my @pairs=split(/\&/,$rep);
1.273     albertel 4897:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   4898:      return @pairs;
                   4899:    }
1.15      www      4900:    my %returnhash=();
1.42      www      4901:    my $i=0;
1.800     albertel 4902:    foreach my $item (@$storearr) {
                   4903:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      4904:       $i++;
1.191     harris41 4905:    }
1.15      www      4906:    return %returnhash;
1.27      www      4907: }
                   4908: 
                   4909: # --------------------------------------------------------------- del interface
                   4910: 
                   4911: sub del {
1.133     albertel 4912:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      4913:    my $items='';
1.800     albertel 4914:    foreach my $item (@$storearr) {
                   4915:        $items.=&escape($item).'&';
1.191     harris41 4916:    }
1.984     neumanie 4917: 
1.27      www      4918:    $items=~s/\&$//;
1.620     albertel 4919:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4920:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 4921:    my $uhome=&homeserver($uname,$udomain);
                   4922:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4923: }
                   4924: 
                   4925: # -------------------------------------------------------------- dump interface
                   4926: 
                   4927: sub dump {
1.1086    raeburn  4928:     my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755     albertel 4929:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4930:     if (!$uname) { $uname=$env{'user.name'}; }
                   4931:     my $uhome=&homeserver($uname,$udomain);
                   4932:     if ($regexp) {
                   4933: 	$regexp=&escape($regexp);
                   4934:     } else {
                   4935: 	$regexp='.';
                   4936:     }
1.1086    raeburn  4937:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755     albertel 4938:     my @pairs=split(/\&/,$rep);
                   4939:     my %returnhash=();
1.1098    foxr     4940:     if (!($rep =~ /^error/ )) {
                   4941: 	foreach my $item (@pairs) {
                   4942: 	    my ($key,$value)=split(/=/,$item,2);
                   4943: 	    $key = &unescape($key);
                   4944: 	    next if ($key =~ /^error: 2 /);
                   4945: 	    $returnhash{$key}=&thaw_unescape($value);
                   4946: 	}
1.755     albertel 4947:     }
                   4948:     return %returnhash;
1.407     www      4949: }
                   4950: 
1.1098    foxr     4951: 
1.717     albertel 4952: # --------------------------------------------------------- dumpstore interface
                   4953: 
                   4954: sub dumpstore {
                   4955:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 4956:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4957:    if (!$uname) { $uname=$env{'user.name'}; }
                   4958:    my $uhome=&homeserver($uname,$udomain);
                   4959:    if ($regexp) {
                   4960:        $regexp=&escape($regexp);
                   4961:    } else {
                   4962:        $regexp='.';
                   4963:    }
                   4964:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   4965:    my @pairs=split(/\&/,$rep);
                   4966:    my %returnhash=();
                   4967:    foreach my $item (@pairs) {
                   4968:        my ($key,$value)=split(/=/,$item,2);
                   4969:        next if ($key =~ /^error: 2 /);
                   4970:        $returnhash{$key}=&thaw_unescape($value);
                   4971:    }
                   4972:    return %returnhash;
1.717     albertel 4973: }
                   4974: 
1.407     www      4975: # -------------------------------------------------------------- keys interface
                   4976: 
                   4977: sub getkeys {
                   4978:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 4979:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4980:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      4981:    my $uhome=&homeserver($uname,$udomain);
                   4982:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   4983:    my @keyarray=();
1.800     albertel 4984:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  4985:       next if ($key =~ /^error: 2 /);
1.800     albertel 4986:       push(@keyarray,&unescape($key));
1.407     www      4987:    }
                   4988:    return @keyarray;
1.318     matthew  4989: }
                   4990: 
1.319     matthew  4991: # --------------------------------------------------------------- currentdump
                   4992: sub currentdump {
1.328     matthew  4993:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 4994:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   4995:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   4996:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  4997:    my $uhome = &homeserver($sname,$sdom);
                   4998:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  4999:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  5000:    #
1.318     matthew  5001:    my %returnhash=();
1.319     matthew  5002:    #
                   5003:    if ($rep eq "unknown_cmd") { 
                   5004:        # an old lond will not know currentdump
                   5005:        # Do a dump and make it look like a currentdump
1.822     albertel 5006:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  5007:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   5008:        my %hash = @tmp;
                   5009:        @tmp=();
1.424     matthew  5010:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  5011:    } else {
                   5012:        my @pairs=split(/\&/,$rep);
1.800     albertel 5013:        foreach my $pair (@pairs) {
                   5014:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  5015:            my ($symb,$param) = split(/:/,$key);
                   5016:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 5017:                                                         &thaw_unescape($value);
1.319     matthew  5018:        }
1.191     harris41 5019:    }
1.12      www      5020:    return %returnhash;
1.424     matthew  5021: }
                   5022: 
                   5023: sub convert_dump_to_currentdump{
                   5024:     my %hash = %{shift()};
                   5025:     my %returnhash;
                   5026:     # Code ripped from lond, essentially.  The only difference
                   5027:     # here is the unescaping done by lonnet::dump().  Conceivably
                   5028:     # we might run in to problems with parameter names =~ /^v\./
                   5029:     while (my ($key,$value) = each(%hash)) {
                   5030:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 5031: 	$symb  = &unescape($symb);
                   5032: 	$param = &unescape($param);
1.424     matthew  5033:         next if ($v eq 'version' || $symb eq 'keys');
                   5034:         next if (exists($returnhash{$symb}) &&
                   5035:                  exists($returnhash{$symb}->{$param}) &&
                   5036:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   5037:         $returnhash{$symb}->{$param}=$value;
                   5038:         $returnhash{$symb}->{'v.'.$param}=$v;
                   5039:     }
                   5040:     #
                   5041:     # Remove all of the keys in the hashes which keep track of
                   5042:     # the version of the parameter.
                   5043:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   5044:         # use a foreach because we are going to delete from the hash.
                   5045:         foreach my $key (keys(%$param_hash)) {
                   5046:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   5047:         }
                   5048:     }
                   5049:     return \%returnhash;
1.12      www      5050: }
                   5051: 
1.627     albertel 5052: # ------------------------------------------------------ critical inc interface
                   5053: 
                   5054: sub cinc {
                   5055:     return &inc(@_,'critical');
                   5056: }
                   5057: 
1.449     matthew  5058: # --------------------------------------------------------------- inc interface
                   5059: 
                   5060: sub inc {
1.627     albertel 5061:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 5062:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5063:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  5064:     my $uhome=&homeserver($uname,$udomain);
                   5065:     my $items='';
                   5066:     if (! ref($store)) {
                   5067:         # got a single value, so use that instead
                   5068:         $items = &escape($store).'=&';
                   5069:     } elsif (ref($store) eq 'SCALAR') {
                   5070:         $items = &escape($$store).'=&';        
                   5071:     } elsif (ref($store) eq 'ARRAY') {
                   5072:         $items = join('=&',map {&escape($_);} @{$store});
                   5073:     } elsif (ref($store) eq 'HASH') {
                   5074:         while (my($key,$value) = each(%{$store})) {
                   5075:             $items.= &escape($key).'='.&escape($value).'&';
                   5076:         }
                   5077:     }
                   5078:     $items=~s/\&$//;
1.627     albertel 5079:     if ($critical) {
                   5080: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5081:     } else {
                   5082: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   5083:     }
1.449     matthew  5084: }
                   5085: 
1.12      www      5086: # --------------------------------------------------------------- put interface
                   5087: 
                   5088: sub put {
1.134     albertel 5089:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5090:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5091:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5092:    my $uhome=&homeserver($uname,$udomain);
1.12      www      5093:    my $items='';
1.800     albertel 5094:    foreach my $item (keys(%$storehash)) {
                   5095:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5096:    }
1.12      www      5097:    $items=~s/\&$//;
1.134     albertel 5098:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      5099: }
                   5100: 
1.631     albertel 5101: # ------------------------------------------------------------ newput interface
                   5102: 
                   5103: sub newput {
                   5104:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   5105:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5106:    if (!$uname) { $uname=$env{'user.name'}; }
                   5107:    my $uhome=&homeserver($uname,$udomain);
                   5108:    my $items='';
                   5109:    foreach my $key (keys(%$storehash)) {
                   5110:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   5111:    }
                   5112:    $items=~s/\&$//;
                   5113:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   5114: }
                   5115: 
                   5116: # ---------------------------------------------------------  putstore interface
                   5117: 
1.524     raeburn  5118: sub putstore {
1.715     albertel 5119:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 5120:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5121:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  5122:    my $uhome=&homeserver($uname,$udomain);
                   5123:    my $items='';
1.715     albertel 5124:    foreach my $key (keys(%$storehash)) {
                   5125:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  5126:    }
1.715     albertel 5127:    $items=~s/\&$//;
1.716     albertel 5128:    my $esc_symb=&escape($symb);
                   5129:    my $esc_v=&escape($version);
1.715     albertel 5130:    my $reply =
1.716     albertel 5131:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 5132: 	      $uhome);
                   5133:    if ($reply eq 'unknown_cmd') {
1.716     albertel 5134:        # gfall back to way things use to be done
1.715     albertel 5135:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   5136: 			    $uname);
1.524     raeburn  5137:    }
1.715     albertel 5138:    return $reply;
                   5139: }
                   5140: 
                   5141: sub old_putstore {
1.716     albertel 5142:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   5143:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5144:     if (!$uname) { $uname=$env{'user.name'}; }
                   5145:     my $uhome=&homeserver($uname,$udomain);
                   5146:     my %newstorehash;
1.800     albertel 5147:     foreach my $item (keys(%$storehash)) {
                   5148: 	my $key = $version.':'.&escape($symb).':'.$item;
                   5149: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 5150:     }
                   5151:     my $items='';
                   5152:     my %allitems = ();
1.800     albertel 5153:     foreach my $item (keys(%newstorehash)) {
                   5154: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 5155: 	    my $key = $1.':keys:'.$2;
                   5156: 	    $allitems{$key} .= $3.':';
                   5157: 	}
1.800     albertel 5158: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 5159:     }
1.800     albertel 5160:     foreach my $item (keys(%allitems)) {
                   5161: 	$allitems{$item} =~ s/\:$//;
                   5162: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 5163:     }
                   5164:     $items=~s/\&$//;
                   5165:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  5166: }
                   5167: 
1.47      www      5168: # ------------------------------------------------------ critical put interface
                   5169: 
                   5170: sub cput {
1.134     albertel 5171:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 5172:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5173:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 5174:    my $uhome=&homeserver($uname,$udomain);
1.47      www      5175:    my $items='';
1.800     albertel 5176:    foreach my $item (keys(%$storehash)) {
                   5177:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 5178:    }
1.47      www      5179:    $items=~s/\&$//;
1.134     albertel 5180:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5181: }
                   5182: 
                   5183: # -------------------------------------------------------------- eget interface
                   5184: 
                   5185: sub eget {
1.133     albertel 5186:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      5187:    my $items='';
1.800     albertel 5188:    foreach my $item (@$storearr) {
                   5189:        $items.=&escape($item).'&';
1.191     harris41 5190:    }
1.12      www      5191:    $items=~s/\&$//;
1.620     albertel 5192:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   5193:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 5194:    my $uhome=&homeserver($uname,$udomain);
                   5195:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      5196:    my @pairs=split(/\&/,$rep);
                   5197:    my %returnhash=();
1.42      www      5198:    my $i=0;
1.800     albertel 5199:    foreach my $item (@$storearr) {
                   5200:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      5201:       $i++;
1.191     harris41 5202:    }
1.12      www      5203:    return %returnhash;
                   5204: }
                   5205: 
1.667     albertel 5206: # ------------------------------------------------------------ tmpput interface
                   5207: sub tmpput {
1.802     raeburn  5208:     my ($storehash,$server,$context)=@_;
1.667     albertel 5209:     my $items='';
1.800     albertel 5210:     foreach my $item (keys(%$storehash)) {
                   5211: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 5212:     }
                   5213:     $items=~s/\&$//;
1.802     raeburn  5214:     if (defined($context)) {
                   5215:         $items .= ':'.&escape($context);
                   5216:     }
1.667     albertel 5217:     return &reply("tmpput:$items",$server);
                   5218: }
                   5219: 
                   5220: # ------------------------------------------------------------ tmpget interface
                   5221: sub tmpget {
1.688     albertel 5222:     my ($token,$server)=@_;
                   5223:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5224:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 5225:     my %returnhash;
                   5226:     foreach my $item (split(/\&/,$rep)) {
                   5227: 	my ($key,$value)=split(/=/,$item);
1.951     raeburn  5228:         next if ($key =~ /^error: 2 /);
1.667     albertel 5229: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   5230:     }
                   5231:     return %returnhash;
                   5232: }
                   5233: 
1.1113    raeburn  5234: # ------------------------------------------------------------ tmpdel interface
1.688     albertel 5235: sub tmpdel {
                   5236:     my ($token,$server)=@_;
                   5237:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   5238:     return &reply("tmpdel:$token",$server);
                   5239: }
                   5240: 
1.765     albertel 5241: # -------------------------------------------------- portfolio access checking
                   5242: 
                   5243: sub portfolio_access {
1.766     albertel 5244:     my ($requrl) = @_;
1.765     albertel 5245:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   5246:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  5247:     if ($result) {
                   5248:         my %setters;
                   5249:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5250:             my ($startblock,$endblock) =
                   5251:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   5252:             if ($startblock && $endblock) {
                   5253:                 return 'B';
                   5254:             }
                   5255:         } else {
                   5256:             my ($startblock,$endblock) =
                   5257:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   5258:             if ($startblock && $endblock) {
                   5259:                 return 'B';
                   5260:             }
                   5261:         }
                   5262:     }
1.765     albertel 5263:     if ($result eq 'ok') {
1.766     albertel 5264:        return 'F';
1.765     albertel 5265:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 5266:        return 'A';
1.765     albertel 5267:     }
1.766     albertel 5268:     return '';
1.765     albertel 5269: }
                   5270: 
                   5271: sub get_portfolio_access {
1.767     albertel 5272:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   5273: 
                   5274:     if (!ref($access_hash)) {
                   5275: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   5276: 	my %access_controls = &get_access_controls($current_perms,$group,
                   5277: 						   $file_name);
                   5278: 	$access_hash = $access_controls{$file_name};
                   5279:     }
                   5280: 
1.765     albertel 5281:     my ($public,$guest,@domains,@users,@courses,@groups);
                   5282:     my $now = time;
                   5283:     if (ref($access_hash) eq 'HASH') {
                   5284:         foreach my $key (keys(%{$access_hash})) {
                   5285:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5286:             if ($start > $now) {
                   5287:                 next;
                   5288:             }
                   5289:             if ($end && $end<$now) {
                   5290:                 next;
                   5291:             }
                   5292:             if ($scope eq 'public') {
                   5293:                 $public = $key;
                   5294:                 last;
                   5295:             } elsif ($scope eq 'guest') {
                   5296:                 $guest = $key;
                   5297:             } elsif ($scope eq 'domains') {
                   5298:                 push(@domains,$key);
                   5299:             } elsif ($scope eq 'users') {
                   5300:                 push(@users,$key);
                   5301:             } elsif ($scope eq 'course') {
                   5302:                 push(@courses,$key);
                   5303:             } elsif ($scope eq 'group') {
                   5304:                 push(@groups,$key);
                   5305:             }
                   5306:         }
                   5307:         if ($public) {
                   5308:             return 'ok';
                   5309:         }
                   5310:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   5311:             if ($guest) {
                   5312:                 return $guest;
                   5313:             }
                   5314:         } else {
                   5315:             if (@domains > 0) {
                   5316:                 foreach my $domkey (@domains) {
                   5317:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   5318:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   5319:                             return 'ok';
                   5320:                         }
                   5321:                     }
                   5322:                 }
                   5323:             }
                   5324:             if (@users > 0) {
                   5325:                 foreach my $userkey (@users) {
1.865     raeburn  5326:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   5327:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   5328:                             if (ref($item) eq 'HASH') {
                   5329:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   5330:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   5331:                                     return 'ok';
                   5332:                                 }
                   5333:                             }
                   5334:                         }
                   5335:                     } 
1.765     albertel 5336:                 }
                   5337:             }
                   5338:             my %roleshash;
                   5339:             my @courses_and_groups = @courses;
                   5340:             push(@courses_and_groups,@groups); 
                   5341:             if (@courses_and_groups > 0) {
                   5342:                 my (%allgroups,%allroles); 
                   5343:                 my ($start,$end,$role,$sec,$group);
                   5344:                 foreach my $envkey (%env) {
1.1060    raeburn  5345:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 5346:                         my $cid = $2.'_'.$3; 
                   5347:                         if ($1 eq 'gr') {
                   5348:                             $group = $4;
                   5349:                             $allgroups{$cid}{$group} = $env{$envkey};
                   5350:                         } else {
                   5351:                             if ($4 eq '') {
                   5352:                                 $sec = 'none';
                   5353:                             } else {
                   5354:                                 $sec = $4;
                   5355:                             }
                   5356:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   5357:                         }
1.811     albertel 5358:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 5359:                         my $cid = $2.'_'.$3;
                   5360:                         if ($4 eq '') {
                   5361:                             $sec = 'none';
                   5362:                         } else {
                   5363:                             $sec = $4;
                   5364:                         }
                   5365:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   5366:                     }
                   5367:                 }
                   5368:                 if (keys(%allroles) == 0) {
                   5369:                     return;
                   5370:                 }
                   5371:                 foreach my $key (@courses_and_groups) {
                   5372:                     my %content = %{$$access_hash{$key}};
                   5373:                     my $cnum = $content{'number'};
                   5374:                     my $cdom = $content{'domain'};
                   5375:                     my $cid = $cdom.'_'.$cnum;
                   5376:                     if (!exists($allroles{$cid})) {
                   5377:                         next;
                   5378:                     }    
                   5379:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   5380:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   5381:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   5382:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   5383:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   5384:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   5385:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   5386:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   5387:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   5388:                                         if (grep/^all$/,@sections) {
                   5389:                                             return 'ok';
                   5390:                                         } else {
                   5391:                                             if (grep/^$sec$/,@sections) {
                   5392:                                                 return 'ok';
                   5393:                                             }
                   5394:                                         }
                   5395:                                     }
                   5396:                                 }
                   5397:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   5398:                                     if (grep/^none$/,@groups) {
                   5399:                                         return 'ok';
                   5400:                                     }
                   5401:                                 } else {
                   5402:                                     if (grep/^all$/,@groups) {
                   5403:                                         return 'ok';
                   5404:                                     } 
                   5405:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   5406:                                         if (grep/^$group$/,@groups) {
                   5407:                                             return 'ok';
                   5408:                                         }
                   5409:                                     }
                   5410:                                 } 
                   5411:                             }
                   5412:                         }
                   5413:                     }
                   5414:                 }
                   5415:             }
                   5416:             if ($guest) {
                   5417:                 return $guest;
                   5418:             }
                   5419:         }
                   5420:     }
                   5421:     return;
                   5422: }
                   5423: 
                   5424: sub course_group_datechecker {
                   5425:     my ($dates,$now,$status) = @_;
                   5426:     my ($start,$end) = split(/\./,$dates);
                   5427:     if (!$start && !$end) {
                   5428:         return 'ok';
                   5429:     }
                   5430:     if (grep/^active$/,@{$status}) {
                   5431:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   5432:             return 'ok';
                   5433:         }
                   5434:     }
                   5435:     if (grep/^previous$/,@{$status}) {
                   5436:         if ($end > $now ) {
                   5437:             return 'ok';
                   5438:         }
                   5439:     }
                   5440:     if (grep/^future$/,@{$status}) {
                   5441:         if ($start > $now) {
                   5442:             return 'ok';
                   5443:         }
                   5444:     }
                   5445:     return; 
                   5446: }
                   5447: 
                   5448: sub parse_portfolio_url {
                   5449:     my ($url) = @_;
                   5450: 
                   5451:     my ($type,$udom,$unum,$group,$file_name);
                   5452:     
1.823     albertel 5453:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 5454: 	$type = 1;
                   5455:         $udom = $1;
                   5456:         $unum = $2;
                   5457:         $file_name = $3;
1.823     albertel 5458:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 5459: 	$type = 2;
                   5460:         $udom = $1;
                   5461:         $unum = $2;
                   5462:         $group = $3;
                   5463:         $file_name = $3.'/'.$4;
                   5464:     }
                   5465:     if (wantarray) {
                   5466: 	return ($type,$udom,$unum,$file_name,$group);
                   5467:     }
                   5468:     return $type;
                   5469: }
                   5470: 
                   5471: sub is_portfolio_url {
                   5472:     my ($url) = @_;
                   5473:     return scalar(&parse_portfolio_url($url));
                   5474: }
                   5475: 
1.798     raeburn  5476: sub is_portfolio_file {
                   5477:     my ($file) = @_;
1.820     raeburn  5478:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  5479:         return 1;
                   5480:     }
                   5481:     return;
                   5482: }
                   5483: 
1.976     raeburn  5484: sub usertools_access {
1.1084    raeburn  5485:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  5486:     my ($access,%tools);
                   5487:     if ($context eq '') {
                   5488:         $context = 'tools';
                   5489:     }
                   5490:     if ($context eq 'requestcourses') {
                   5491:         %tools = (
                   5492:                       official   => 1,
                   5493:                       unofficial => 1,
1.1006    raeburn  5494:                       community  => 1,
1.985     raeburn  5495:                  );
                   5496:     } else {
                   5497:         %tools = (
                   5498:                       aboutme   => 1,
                   5499:                       blog      => 1,
                   5500:                       portfolio => 1,
                   5501:                  );
                   5502:     }
1.976     raeburn  5503:     return if (!defined($tools{$tool}));
                   5504: 
                   5505:     if ((!defined($udom)) || (!defined($uname))) {
                   5506:         $udom = $env{'user.domain'};
                   5507:         $uname = $env{'user.name'};
                   5508:     }
                   5509: 
1.978     raeburn  5510:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   5511:         if ($action ne 'reload') {
1.985     raeburn  5512:             if ($context eq 'requestcourses') {
                   5513:                 return $env{'environment.canrequest.'.$tool};
                   5514:             } else {
                   5515:                 return $env{'environment.availabletools.'.$tool};
                   5516:             }
                   5517:         }
1.976     raeburn  5518:     }
                   5519: 
                   5520:     my ($toolstatus,$inststatus);
                   5521: 
1.985     raeburn  5522:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   5523:          ($action ne 'reload')) {
                   5524:         $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976     raeburn  5525:         $inststatus = $env{'environment.inststatus'};
                   5526:     } else {
1.1084    raeburn  5527:         if (ref($userenvref) eq 'HASH') {
                   5528:             $toolstatus = $userenvref->{$context.'.'.$tool};
                   5529:             $inststatus = $userenvref->{'inststatus'};
                   5530:         } else {
                   5531:             my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
                   5532:             $toolstatus = $userenv{$context.'.'.$tool};
                   5533:             $inststatus = $userenv{'inststatus'};
                   5534:         }
1.976     raeburn  5535:     }
                   5536: 
                   5537:     if ($toolstatus ne '') {
                   5538:         if ($toolstatus) {
                   5539:             $access = 1;
                   5540:         } else {
                   5541:             $access = 0;
                   5542:         }
                   5543:         return $access;
                   5544:     }
                   5545: 
1.1084    raeburn  5546:     my ($is_adv,%domdef);
                   5547:     if (ref($is_advref) eq 'HASH') {
                   5548:         $is_adv = $is_advref->{'is_adv'};
                   5549:     } else {
                   5550:         $is_adv = &is_advanced_user($udom,$uname);
                   5551:     }
                   5552:     if (ref($domdefref) eq 'HASH') {
                   5553:         %domdef = %{$domdefref};
                   5554:     } else {
                   5555:         %domdef = &get_domain_defaults($udom);
                   5556:     }
1.976     raeburn  5557:     if (ref($domdef{$tool}) eq 'HASH') {
                   5558:         if ($is_adv) {
                   5559:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   5560:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   5561:                     $access = 1;
                   5562:                 } else {
                   5563:                     $access = 0;
                   5564:                 }
                   5565:                 return $access;
                   5566:             }
                   5567:         }
                   5568:         if ($inststatus ne '') {
                   5569:             my ($hasaccess,$hasnoaccess);
                   5570:             foreach my $affiliation (split(/:/,$inststatus)) {
                   5571:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   5572:                     if ($domdef{$tool}{$affiliation}) {
                   5573:                         $hasaccess = 1;
                   5574:                     } else {
                   5575:                         $hasnoaccess = 1;
                   5576:                     }
                   5577:                 }
                   5578:             }
                   5579:             if ($hasaccess || $hasnoaccess) {
                   5580:                 if ($hasaccess) {
                   5581:                     $access = 1;
                   5582:                 } elsif ($hasnoaccess) {
                   5583:                     $access = 0; 
                   5584:                 }
                   5585:                 return $access;
                   5586:             }
                   5587:         } else {
                   5588:             if ($domdef{$tool}{'default'} ne '') {
                   5589:                 if ($domdef{$tool}{'default'}) {
                   5590:                     $access = 1;
                   5591:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   5592:                     $access = 0;
                   5593:                 }
                   5594:                 return $access;
                   5595:             }
                   5596:         }
                   5597:     } else {
1.985     raeburn  5598:         if ($context eq 'tools') {
                   5599:             $access = 1;
                   5600:         } else {
                   5601:             $access = 0;
                   5602:         }
1.976     raeburn  5603:         return $access;
                   5604:     }
                   5605: }
                   5606: 
1.1050    raeburn  5607: sub is_course_owner {
                   5608:     my ($cdom,$cnum,$udom,$uname) = @_;
                   5609:     if (($udom eq '') || ($uname eq '')) {
                   5610:         $udom = $env{'user.domain'};
                   5611:         $uname = $env{'user.name'};
                   5612:     }
                   5613:     unless (($udom eq '') || ($uname eq '')) {
                   5614:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   5615:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   5616:                 return 1;
                   5617:             } else {
                   5618:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   5619:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   5620:                     return 1;
                   5621:                 }
                   5622:             }
                   5623:         }
                   5624:     }
                   5625:     return;
                   5626: }
                   5627: 
1.976     raeburn  5628: sub is_advanced_user {
                   5629:     my ($udom,$uname) = @_;
1.1085    raeburn  5630:     if ($udom ne '' && $uname ne '') {
                   5631:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.1128    raeburn  5632:             if (wantarray) {
                   5633:                 return ($env{'user.adv'},$env{'user.author'});
                   5634:             } else {
                   5635:                 return $env{'user.adv'};
                   5636:             }
1.1085    raeburn  5637:         }
                   5638:     }
1.976     raeburn  5639:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   5640:     my %allroles;
1.1128    raeburn  5641:     my ($is_adv,$is_author);
1.976     raeburn  5642:     foreach my $role (keys(%roleshash)) {
                   5643:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   5644:         my $area = '/'.$tdomain.'/'.$trest;
                   5645:         if ($sec ne '') {
                   5646:             $area .= '/'.$sec;
                   5647:         }
                   5648:         if (($area ne '') && ($trole ne '')) {
                   5649:             my $spec=$trole.'.'.$area;
                   5650:             if ($trole =~ /^cr\//) {
                   5651:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   5652:             } elsif ($trole ne 'gr') {
                   5653:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   5654:             }
1.1128    raeburn  5655:             if ($trole eq 'au') {
                   5656:                 $is_author = 1;
                   5657:             }
1.976     raeburn  5658:         }
                   5659:     }
                   5660:     foreach my $role (keys(%allroles)) {
                   5661:         last if ($is_adv);
                   5662:         foreach my $item (split(/:/,$allroles{$role})) {
                   5663:             if ($item ne '') {
                   5664:                 my ($privilege,$restrictions)=split(/&/,$item);
                   5665:                 if ($privilege eq 'adv') {
                   5666:                     $is_adv = 1;
                   5667:                     last;
                   5668:                 }
                   5669:             }
                   5670:         }
                   5671:     }
1.1128    raeburn  5672:     if (wantarray) {
                   5673:         return ($is_adv,$is_author);
                   5674:     }
1.976     raeburn  5675:     return $is_adv;
                   5676: }
1.798     raeburn  5677: 
1.1035    raeburn  5678: sub check_can_request {
1.1036    raeburn  5679:     my ($dom,$can_request,$request_domains) = @_;
1.1035    raeburn  5680:     my $canreq = 0;
                   5681:     my ($types,$typename) = &Apache::loncommon::course_types();
                   5682:     my @options = ('approval','validate','autolimit');
                   5683:     my $optregex = join('|',@options);
                   5684:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   5685:         foreach my $type (@{$types}) {
                   5686:             if (&usertools_access($env{'user.name'},
                   5687:                                   $env{'user.domain'},
                   5688:                                   $type,undef,'requestcourses')) {
                   5689:                 $canreq ++;
1.1036    raeburn  5690:                 if (ref($request_domains) eq 'HASH') {
                   5691:                     push(@{$request_domains->{$type}},$env{'user.domain'});
                   5692:                 }
1.1035    raeburn  5693:                 if ($dom eq $env{'user.domain'}) {
                   5694:                     $can_request->{$type} = 1;
                   5695:                 }
                   5696:             }
                   5697:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                   5698:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   5699:                 if (@curr > 0) {
1.1036    raeburn  5700:                     foreach my $item (@curr) {
                   5701:                         if (ref($request_domains) eq 'HASH') {
                   5702:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   5703:                             if ($otherdom ne '') {
                   5704:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   5705:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   5706:                                         push(@{$request_domains->{$type}},$otherdom);
                   5707:                                     }
                   5708:                                 } else {
                   5709:                                     push(@{$request_domains->{$type}},$otherdom);
                   5710:                                 }
                   5711:                             }
                   5712:                         }
                   5713:                     }
                   5714:                     unless($dom eq $env{'user.domain'}) {
                   5715:                         $canreq ++;
1.1035    raeburn  5716:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   5717:                             $can_request->{$type} = 1;
                   5718:                         }
                   5719:                     }
                   5720:                 }
                   5721:             }
                   5722:         }
                   5723:     }
                   5724:     return $canreq;
                   5725: }
                   5726: 
1.341     www      5727: # ---------------------------------------------- Custom access rule evaluation
                   5728: 
                   5729: sub customaccess {
                   5730:     my ($priv,$uri)=@_;
1.807     albertel 5731:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      5732:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 5733:     $udom = &LONCAPA::clean_domain($udom);
                   5734:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      5735:     my $access=0;
1.800     albertel 5736:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 5737: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   5738: 	if ($type eq 'user') {
                   5739: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 5740: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 5741: 		if ($tdom) {
                   5742: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   5743: 		}
1.896     albertel 5744: 		if ($tuname) {
                   5745: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 5746: 		}
                   5747: 		$access=($effect eq 'allow');
                   5748: 		last;
                   5749: 	    }
                   5750: 	} else {
                   5751: 	    if ($role) {
                   5752: 		if ($role ne $urole) { next; }
                   5753: 	    }
                   5754: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   5755: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   5756: 		if ($tdom) {
                   5757: 		    if ($tdom ne $udom) { next; }
                   5758: 		}
                   5759: 		if ($tcrs) {
                   5760: 		    if ($tcrs ne $ucrs) { next; }
                   5761: 		}
                   5762: 		if ($tsec) {
                   5763: 		    if ($tsec ne $usec) { next; }
                   5764: 		}
                   5765: 		$access=($effect eq 'allow');
                   5766: 		last;
                   5767: 	    }
                   5768: 	    if ($realm eq '' && $role eq '') {
                   5769: 		$access=($effect eq 'allow');
                   5770: 	    }
1.402     bowersj2 5771: 	}
1.341     www      5772:     }
                   5773:     return $access;
                   5774: }
                   5775: 
1.103     harris41 5776: # ------------------------------------------------- Check for a user privilege
1.12      www      5777: 
                   5778: sub allowed {
1.810     raeburn  5779:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 5780:     my $ver_orguri=$uri;
1.439     www      5781:     $uri=&deversion($uri);
1.152     www      5782:     my $orguri=$uri;
1.52      www      5783:     $uri=&declutter($uri);
1.809     raeburn  5784: 
1.810     raeburn  5785:     if ($priv eq 'evb') {
                   5786: # Evade communication block restrictions for specified role in a course
                   5787:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   5788:             return $1;
                   5789:         } else {
                   5790:             return;
                   5791:         }
                   5792:     }
                   5793: 
1.620     albertel 5794:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      5795: # Free bre access to adm and meta resources
1.775     albertel 5796:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 5797: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   5798: 	&& ($priv eq 'bre')) {
1.14      www      5799: 	return 'F';
1.159     www      5800:     }
                   5801: 
1.545     banghart 5802: # Free bre access to user's own portfolio contents
1.714     raeburn  5803:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  5804:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  5805: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  5806:         my %setters;
                   5807:         my ($startblock,$endblock) = 
                   5808:             &Apache::loncommon::blockcheck(\%setters,'port');
                   5809:         if ($startblock && $endblock) {
                   5810:             return 'B';
                   5811:         } else {
                   5812:             return 'F';
                   5813:         }
1.545     banghart 5814:     }
                   5815: 
1.762     raeburn  5816: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  5817:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   5818:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   5819:         if (exists($env{'request.course.id'})) {
                   5820:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5821:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5822:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   5823:                 my $courseprivid=$env{'request.course.id'};
                   5824:                 $courseprivid=~s/\_/\//;
                   5825:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   5826:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   5827:                     return $1; 
1.762     raeburn  5828:                 } else {
                   5829:                     if ($env{'request.course.sec'}) {
                   5830:                         $courseprivid.='/'.$env{'request.course.sec'};
                   5831:                     }
                   5832:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   5833:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   5834:                         return $2;
                   5835:                     }
1.714     raeburn  5836:                 }
                   5837:             }
                   5838:         }
                   5839:     }
                   5840: 
1.159     www      5841: # Free bre to public access
                   5842: 
                   5843:     if ($priv eq 'bre') {
1.238     www      5844:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 5845: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      5846:            return 'F'; 
                   5847:         }
1.238     www      5848:         if ($copyright eq 'priv') {
                   5849:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5850: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      5851: 		return '';
                   5852:             }
                   5853:         }
                   5854:         if ($copyright eq 'domain') {
                   5855:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5856: 	    unless (($env{'user.domain'} eq $1) ||
                   5857:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      5858: 		return '';
                   5859:             }
1.262     matthew  5860:         }
1.620     albertel 5861:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  5862:             # Library role, so allow browsing of resources in this domain.
                   5863:             return 'F';
1.238     www      5864:         }
1.341     www      5865:         if ($copyright eq 'custom') {
                   5866: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   5867:         }
1.14      www      5868:     }
1.264     matthew  5869:     # Domain coordinator is trying to create a course
1.620     albertel 5870:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  5871:         # uri is the requested domain in this case.
                   5872:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  5873:         # a role of dc for the domain in question.
1.620     albertel 5874:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  5875:     }
1.29      www      5876: 
1.52      www      5877:     my $thisallowed='';
                   5878:     my $statecond=0;
                   5879:     my $courseprivid='';
                   5880: 
1.1039    raeburn  5881:     my $ownaccess;
1.1043    raeburn  5882:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  5883:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   5884:         if ($uri eq '') {
                   5885:             $ownaccess = 1;
                   5886:         } else {
                   5887:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   5888:                 my $udom = $env{'user.domain'};
                   5889:                 my $uname = $env{'user.name'};
                   5890:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   5891:                     $ownaccess = 1;
1.1040    raeburn  5892:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  5893:                     unless ($uri =~ m{\.\./}) {
                   5894:                         $ownaccess = 1;
                   5895:                     }
                   5896:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   5897:                     my $now = time;
                   5898:                     if ($uri =~ m{^([^/]+)/?$}) {
                   5899:                         my $adom = $1;
                   5900:                         foreach my $key (keys(%env)) {
1.1042    raeburn  5901:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  5902:                                 my ($start,$end) = split('.',$env{$key});
                   5903:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5904:                                     $ownaccess = 1;
                   5905:                                     last;
                   5906:                                 }
                   5907:                             }
                   5908:                         }
                   5909:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   5910:                         my $adom = $1;
                   5911:                         my $aname = $2;
1.1042    raeburn  5912:                         foreach my $role ('ca','aa') { 
                   5913:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   5914:                                 my ($start,$end) =
                   5915:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   5916:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5917:                                     $ownaccess = 1;
                   5918:                                     last;
                   5919:                                 }
1.1039    raeburn  5920:                             }
                   5921:                         }
                   5922:                     }
                   5923:                 }
                   5924:             }
                   5925:         }
                   5926:     }
                   5927: 
1.52      www      5928: # Course
                   5929: 
1.620     albertel 5930:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5931:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5932:             $thisallowed.=$1;
                   5933:         }
1.52      www      5934:     }
1.29      www      5935: 
1.52      www      5936: # Domain
                   5937: 
1.620     albertel 5938:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 5939:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5940:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5941:             $thisallowed.=$1;
                   5942:         }
1.12      www      5943:     }
1.52      www      5944: 
1.1141    raeburn  5945: # User who is not author or co-author might still be able to edit
                   5946: # resource of an author in the domain (e.g., if Domain Coordinator).
                   5947:     if (($priv eq 'eco') && ($thisallowed eq '') && ($env{'request.course.id'}) &&
                   5948:         (&allowed('mdc',$env{'request.course.id'}))) {
                   5949:         if ($env{"user.priv.cm./$uri/"}=~/\Q$priv\E\&([^\:]*)/) {
                   5950:             $thisallowed.=$1;
                   5951:         }
                   5952:     }
                   5953: 
1.52      www      5954: # Course: uri itself is a course
1.66      www      5955:     my $courseuri=$uri;
                   5956:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      5957:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      5958: 
1.620     albertel 5959:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 5960:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5961:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5962:             $thisallowed.=$1;
                   5963:         }
1.12      www      5964:     }
1.29      www      5965: 
1.665     albertel 5966: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 5967: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 5968:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 5969: 	$thisallowed='';
1.671     raeburn  5970:         my ($match)=&is_on_map($uri);
                   5971:         if ($match) {
                   5972:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   5973:                   =~/\Q$priv\E\&([^\:]*)/) {
                   5974:                 $thisallowed.=$1;
                   5975:             }
                   5976:         } else {
1.705     albertel 5977:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  5978:             if ($refuri) {
                   5979:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  5980:                     $thisallowed='F';
1.671     raeburn  5981:                 } else {
                   5982:                     $refuri=&declutter($refuri);
                   5983:                     my ($match) = &is_on_map($refuri);
                   5984:                     if ($match) {
                   5985:                         $thisallowed='F';
                   5986:                     }
1.669     raeburn  5987:                 }
1.671     raeburn  5988:             }
                   5989:         }
1.314     www      5990:     }
1.492     albertel 5991: 
1.766     albertel 5992:     if ($priv eq 'bre'
                   5993: 	&& $thisallowed ne 'F' 
                   5994: 	&& $thisallowed ne '2'
                   5995: 	&& &is_portfolio_url($uri)) {
                   5996: 	$thisallowed = &portfolio_access($uri);
                   5997:     }
                   5998:     
1.52      www      5999: # Full access at system, domain or course-wide level? Exit.
1.29      www      6000:     if ($thisallowed=~/F/) {
                   6001: 	return 'F';
                   6002:     }
                   6003: 
1.52      www      6004: # If this is generating or modifying users, exit with special codes
1.29      www      6005: 
1.643     www      6006:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   6007: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 6008: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      6009: # no author name given, so this just checks on the general right to make a co-author in this domain
                   6010: 	    unless ($auname) { return $thisallowed; }
                   6011: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 6012: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   6013: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   6014: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   6015: 	}
1.52      www      6016: 	return $thisallowed;
                   6017:     }
                   6018: #
1.103     harris41 6019: # Gathered so far: system, domain and course wide privileges
1.52      www      6020: #
                   6021: # Course: See if uri or referer is an individual resource that is part of 
                   6022: # the course
                   6023: 
1.620     albertel 6024:     if ($env{'request.course.id'}) {
1.232     www      6025: 
1.620     albertel 6026:        $courseprivid=$env{'request.course.id'};
                   6027:        if ($env{'request.course.sec'}) {
                   6028:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      6029:        }
                   6030:        $courseprivid=~s/\_/\//;
                   6031:        my $checkreferer=1;
1.232     www      6032:        my ($match,$cond)=&is_on_map($uri);
                   6033:        if ($match) {
                   6034:            $statecond=$cond;
1.620     albertel 6035:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6036:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      6037:                $thisallowed.=$1;
                   6038:                $checkreferer=0;
                   6039:            }
1.29      www      6040:        }
1.83      www      6041:        
1.148     www      6042:        if ($checkreferer) {
1.620     albertel 6043: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      6044:             unless ($refuri) {
1.800     albertel 6045:                 foreach my $key (keys(%env)) {
                   6046: 		    if ($key=~/^httpref\..*\*/) {
                   6047: 			my $pattern=$key;
1.156     www      6048:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      6049:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   6050:                         $pattern=~s/\//\\\//g;
1.152     www      6051:                         if ($orguri=~/$pattern/) {
1.800     albertel 6052: 			    $refuri=$env{$key};
1.148     www      6053:                         }
                   6054:                     }
1.191     harris41 6055:                 }
1.148     www      6056:             }
1.232     www      6057: 
1.148     www      6058:          if ($refuri) { 
1.152     www      6059: 	  $refuri=&declutter($refuri);
1.232     www      6060:           my ($match,$cond)=&is_on_map($refuri);
                   6061:             if ($match) {
                   6062:               my $refstatecond=$cond;
1.620     albertel 6063:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 6064:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      6065:                   $thisallowed.=$1;
1.53      www      6066:                   $uri=$refuri;
                   6067:                   $statecond=$refstatecond;
1.52      www      6068:               }
                   6069:           }
1.148     www      6070:         }
1.29      www      6071:        }
1.52      www      6072:    }
1.29      www      6073: 
1.52      www      6074: #
1.103     harris41 6075: # Gathered now: all privileges that could apply, and condition number
1.52      www      6076: # 
                   6077: #
                   6078: # Full or no access?
                   6079: #
1.29      www      6080: 
1.52      www      6081:     if ($thisallowed=~/F/) {
                   6082: 	return 'F';
                   6083:     }
1.29      www      6084: 
1.52      www      6085:     unless ($thisallowed) {
                   6086:         return '';
                   6087:     }
1.29      www      6088: 
1.52      www      6089: # Restrictions exist, deal with them
                   6090: #
                   6091: #   C:according to course preferences
                   6092: #   R:according to resource settings
                   6093: #   L:unless locked
                   6094: #   X:according to user session state
                   6095: #
                   6096: 
                   6097: # Possibly locked functionality, check all courses
1.54      www      6098: # Locks might take effect only after 10 minutes cache expiration for other
                   6099: # courses, and 2 minutes for current course
1.52      www      6100: 
                   6101:     my $envkey;
                   6102:     if ($thisallowed=~/L/) {
1.1000    raeburn  6103:         foreach $envkey (keys(%env)) {
1.54      www      6104:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   6105:                my $courseid=$2;
                   6106:                my $roleid=$1.'.'.$2;
1.92      www      6107:                $courseid=~s/^\///;
1.54      www      6108:                my $expiretime=600;
1.620     albertel 6109:                if ($env{'request.role'} eq $roleid) {
1.54      www      6110: 		  $expiretime=120;
                   6111:                }
                   6112: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   6113:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 6114:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 6115: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      6116:                }
1.620     albertel 6117:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6118:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   6119: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   6120:                        &log($env{'user.domain'},$env{'user.name'},
                   6121:                             $env{'user.home'},
1.57      www      6122:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      6123:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6124:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6125: 		       return '';
                   6126:                    }
                   6127:                }
1.620     albertel 6128:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   6129:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   6130: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   6131:                        &log($env{'user.domain'},$env{'user.name'},
                   6132:                             $env{'user.home'},
1.57      www      6133:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      6134:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 6135:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      6136: 		       return '';
                   6137:                    }
                   6138:                }
                   6139: 	   }
1.29      www      6140:        }
1.52      www      6141:     }
                   6142:    
                   6143: #
                   6144: # Rest of the restrictions depend on selected course
                   6145: #
                   6146: 
1.620     albertel 6147:     unless ($env{'request.course.id'}) {
1.766     albertel 6148: 	if ($thisallowed eq 'A') {
                   6149: 	    return 'A';
1.814     raeburn  6150:         } elsif ($thisallowed eq 'B') {
                   6151:             return 'B';
1.766     albertel 6152: 	} else {
                   6153: 	    return '1';
                   6154: 	}
1.52      www      6155:     }
1.29      www      6156: 
1.52      www      6157: #
                   6158: # Now user is definitely in a course
                   6159: #
1.53      www      6160: 
                   6161: 
                   6162: # Course preferences
                   6163: 
                   6164:    if ($thisallowed=~/C/) {
1.620     albertel 6165:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   6166:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   6167:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 6168: 	   =~/\Q$rolecode\E/) {
1.1103    raeburn  6169: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6170: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6171: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   6172: 			$env{'request.course.id'});
                   6173: 	   }
1.237     www      6174:            return '';
                   6175:        }
                   6176: 
1.620     albertel 6177:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 6178: 	   =~/\Q$unamedom\E/) {
1.1103    raeburn  6179: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6180: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   6181: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   6182: 			$env{'request.course.id'});
                   6183: 	   }
1.54      www      6184:            return '';
                   6185:        }
1.53      www      6186:    }
                   6187: 
                   6188: # Resource preferences
                   6189: 
                   6190:    if ($thisallowed=~/R/) {
1.620     albertel 6191:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 6192:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  6193: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 6194: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   6195: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   6196: 	   }
                   6197: 	   return '';
1.54      www      6198:        }
1.53      www      6199:    }
1.30      www      6200: 
1.246     www      6201: # Restricted by state or randomout?
1.30      www      6202: 
1.52      www      6203:    if ($thisallowed=~/X/) {
1.620     albertel 6204:       if ($env{'acc.randomout'}) {
1.579     albertel 6205: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 6206:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      6207:             return ''; 
                   6208:          }
1.247     www      6209:       }
                   6210:       if (&condval($statecond)) {
1.52      www      6211: 	 return '2';
                   6212:       } else {
                   6213:          return '';
                   6214:       }
                   6215:    }
1.30      www      6216: 
1.766     albertel 6217:     if ($thisallowed eq 'A') {
                   6218: 	return 'A';
1.814     raeburn  6219:     } elsif ($thisallowed eq 'B') {
                   6220:         return 'B';
1.766     albertel 6221:     }
1.52      www      6222:    return 'F';
1.232     www      6223: }
1.1133    foxr     6224: #
                   6225: #   Removes the versino from a URI and
                   6226: #   splits it in to its filename and path to the filename.
                   6227: #   Seems like File::Basename could have done this more clearly.
                   6228: #   Parameters:
                   6229: #      $uri   - input URI
                   6230: #   Returns:
                   6231: #     Two element list consisting of 
                   6232: #     $pathname  - the URI up to and excluding the trailing /
                   6233: #     $filename  - The part of the URI following the last /
                   6234: #  NOTE:
                   6235: #    Another realization of this is simply:
                   6236: #    use File::Basename;
                   6237: #    ...
                   6238: #    $uri = shift;
                   6239: #    $filename = basename($uri);
                   6240: #    $path     = dirname($uri);
                   6241: #    return ($filename, $path);
                   6242: #
                   6243: #     The implementation below is probably faster however.
                   6244: #
1.710     albertel 6245: sub split_uri_for_cond {
                   6246:     my $uri=&deversion(&declutter(shift));
                   6247:     my @uriparts=split(/\//,$uri);
                   6248:     my $filename=pop(@uriparts);
                   6249:     my $pathname=join('/',@uriparts);
                   6250:     return ($pathname,$filename);
                   6251: }
1.232     www      6252: # --------------------------------------------------- Is a resource on the map?
                   6253: 
                   6254: sub is_on_map {
1.710     albertel 6255:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 6256:     #Trying to find the conditional for the file
1.620     albertel 6257:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 6258: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      6259:     if ($match) {
1.289     bowersj2 6260: 	return (1,$1);
                   6261:     } else {
1.434     www      6262: 	return (0,0);
1.289     bowersj2 6263:     }
1.12      www      6264: }
                   6265: 
1.427     www      6266: # --------------------------------------------------------- Get symb from alias
                   6267: 
                   6268: sub get_symb_from_alias {
                   6269:     my $symb=shift;
                   6270:     my ($map,$resid,$url)=&decode_symb($symb);
                   6271: # Already is a symb
                   6272:     if ($url) { return $symb; }
                   6273: # Must be an alias
                   6274:     my $aliassymb='';
                   6275:     my %bighash;
1.620     albertel 6276:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      6277:                             &GDBM_READER(),0640)) {
                   6278:         my $rid=$bighash{'mapalias_'.$symb};
                   6279: 	if ($rid) {
                   6280: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 6281: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   6282: 				    $resid,$bighash{'src_'.$rid});
1.427     www      6283: 	}
                   6284:         untie %bighash;
                   6285:     }
                   6286:     return $aliassymb;
                   6287: }
                   6288: 
1.12      www      6289: # ----------------------------------------------------------------- Define Role
                   6290: 
                   6291: sub definerole {
                   6292:   if (allowed('mcr','/')) {
                   6293:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 6294:     foreach my $role (split(':',$sysrole)) {
                   6295: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6296:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   6297:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   6298: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      6299:                return "refused:s:$crole&$cqual"; 
                   6300:             }
                   6301:         }
1.191     harris41 6302:     }
1.800     albertel 6303:     foreach my $role (split(':',$domrole)) {
                   6304: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6305:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   6306:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   6307: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      6308:                return "refused:d:$crole&$cqual"; 
                   6309:             }
                   6310:         }
1.191     harris41 6311:     }
1.800     albertel 6312:     foreach my $role (split(':',$courole)) {
                   6313: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 6314:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   6315:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   6316: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      6317:                return "refused:c:$crole&$cqual"; 
                   6318:             }
                   6319:         }
1.191     harris41 6320:     }
1.620     albertel 6321:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   6322:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      6323: 	        "rolesdef_$rolename=".
                   6324:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 6325:     return reply($command,$env{'user.home'});
1.12      www      6326:   } else {
                   6327:     return 'refused';
                   6328:   }
1.105     harris41 6329: }
                   6330: 
                   6331: # ---------------- Make a metadata query against the network of library servers
                   6332: 
                   6333: sub metadata_query {
1.244     matthew  6334:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 6335:     my %rhash;
1.845     albertel 6336:     my %libserv = &all_library();
1.244     matthew  6337:     my @server_list = (defined($server_array) ? @$server_array
                   6338:                                               : keys(%libserv) );
                   6339:     for my $server (@server_list) {
1.118     harris41 6340: 	unless ($custom or $customshow) {
                   6341: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   6342: 	    $rhash{$server}=$reply;
                   6343: 	}
                   6344: 	else {
                   6345: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   6346: 			     &escape($custom).':'.&escape($customshow),
                   6347: 			     $server);
                   6348: 	    $rhash{$server}=$reply;
                   6349: 	}
1.112     harris41 6350:     }
1.118     harris41 6351:     return \%rhash;
1.240     www      6352: }
                   6353: 
                   6354: # ----------------------------------------- Send log queries and wait for reply
                   6355: 
                   6356: sub log_query {
                   6357:     my ($uname,$udom,$query,%filters)=@_;
                   6358:     my $uhome=&homeserver($uname,$udom);
                   6359:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 6360:     my $uhost=&hostname($uhome);
1.800     albertel 6361:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      6362:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   6363:                        $uhome);
1.479     albertel 6364:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      6365:     return get_query_reply($queryid);
                   6366: }
                   6367: 
1.818     raeburn  6368: # -------------------------- Update MySQL table for portfolio file
                   6369: 
                   6370: sub update_portfolio_table {
1.821     raeburn  6371:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  6372:     if ($group ne '') {
                   6373:         $file_name =~s /^\Q$group\E//;
                   6374:     }
1.818     raeburn  6375:     my $homeserver = &homeserver($uname,$udom);
                   6376:     my $queryid=
1.821     raeburn  6377:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   6378:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  6379:     my $reply = &get_query_reply($queryid);
                   6380:     return $reply;
                   6381: }
                   6382: 
1.899     raeburn  6383: # -------------------------- Update MySQL allusers table
                   6384: 
                   6385: sub update_allusers_table {
                   6386:     my ($uname,$udom,$names) = @_;
                   6387:     my $homeserver = &homeserver($uname,$udom);
                   6388:     my $queryid=
                   6389:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   6390:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   6391:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   6392:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   6393:                'generation='.&escape($names->{'generation'}).'%%'.
                   6394:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   6395:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  6396:     return;
1.899     raeburn  6397: }
                   6398: 
1.508     raeburn  6399: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  6400: 
                   6401: sub fetch_enrollment_query {
1.511     raeburn  6402:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  6403:     my $homeserver;
1.547     raeburn  6404:     my $maxtries = 1;
1.508     raeburn  6405:     if ($context eq 'automated') {
                   6406:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  6407:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  6408:     } else {
                   6409:         $homeserver = &homeserver($cnum,$dom);
                   6410:     }
1.838     albertel 6411:     my $host=&hostname($homeserver);
1.506     raeburn  6412:     my $cmd = '';
1.1000    raeburn  6413:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 6414:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  6415:     }
                   6416:     $cmd =~ s/%%$//;
                   6417:     $cmd = &escape($cmd);
                   6418:     my $query = 'fetchenrollment';
1.620     albertel 6419:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  6420:     unless ($queryid=~/^\Q$host\E\_/) { 
                   6421:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   6422:         return 'error: '.$queryid;
                   6423:     }
1.506     raeburn  6424:     my $reply = &get_query_reply($queryid);
1.547     raeburn  6425:     my $tries = 1;
                   6426:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   6427:         $reply = &get_query_reply($queryid);
                   6428:         $tries ++;
                   6429:     }
1.526     raeburn  6430:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 6431:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  6432:     } else {
1.901     albertel 6433:         my @responses = split(/:/,$reply);
1.515     raeburn  6434:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 6435:             foreach my $line (@responses) {
                   6436:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  6437:                 $$replyref{$key} = $value;
                   6438:             }
                   6439:         } else {
1.1117    foxr     6440:             my $pathname = LONCAPA::tempdir();
1.800     albertel 6441:             foreach my $line (@responses) {
                   6442:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  6443:                 $$replyref{$key} = $value;
                   6444:                 if ($value > 0) {
1.800     albertel 6445:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   6446:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  6447:                         my $destname = $pathname.'/'.$filename;
                   6448:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  6449:                         if ($xml_classlist =~ /^error/) {
                   6450:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   6451:                         } else {
1.506     raeburn  6452:                             if ( open(FILE,">$destname") ) {
                   6453:                                 print FILE &unescape($xml_classlist);
                   6454:                                 close(FILE);
1.526     raeburn  6455:                             } else {
                   6456:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  6457:                             }
                   6458:                         }
                   6459:                     }
                   6460:                 }
                   6461:             }
                   6462:         }
                   6463:         return 'ok';
                   6464:     }
                   6465:     return 'error';
                   6466: }
                   6467: 
1.242     www      6468: sub get_query_reply {
                   6469:     my $queryid=shift;
1.1117    foxr     6470:     my $replyfile=LONCAPA::tempdir().$queryid;
1.240     www      6471:     my $reply='';
                   6472:     for (1..100) {
                   6473: 	sleep 2;
                   6474:         if (-e $replyfile.'.end') {
1.448     albertel 6475: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 6476: 		$reply = join('',<$fh>);
                   6477: 		close($fh);
1.240     www      6478: 	   } else { return 'error: reply_file_error'; }
1.242     www      6479:            return &unescape($reply);
                   6480: 	}
1.240     www      6481:     }
1.242     www      6482:     return 'timeout:'.$queryid;
1.240     www      6483: }
                   6484: 
                   6485: sub courselog_query {
1.241     www      6486: #
                   6487: # possible filters:
                   6488: # url: url or symb
                   6489: # username
                   6490: # domain
                   6491: # action: view, submit, grade
                   6492: # start: timestamp
                   6493: # end: timestamp
                   6494: #
1.240     www      6495:     my (%filters)=@_;
1.620     albertel 6496:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      6497:     if ($filters{'url'}) {
                   6498: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   6499:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   6500:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   6501:     }
1.620     albertel 6502:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6503:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      6504:     return &log_query($cname,$cdom,'courselog',%filters);
                   6505: }
                   6506: 
                   6507: sub userlog_query {
1.858     raeburn  6508: #
                   6509: # possible filters:
                   6510: # action: log check role
                   6511: # start: timestamp
                   6512: # end: timestamp
                   6513: #
1.240     www      6514:     my ($uname,$udom,%filters)=@_;
                   6515:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      6516: }
                   6517: 
1.506     raeburn  6518: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   6519: 
                   6520: sub auto_run {
1.508     raeburn  6521:     my ($cnum,$cdom) = @_;
1.876     raeburn  6522:     my $response = 0;
                   6523:     my $settings;
                   6524:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   6525:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   6526:         $settings = $domconfig{'autoenroll'};
                   6527:         if ($settings->{'run'} eq '1') {
                   6528:             $response = 1;
                   6529:         }
                   6530:     } else {
1.934     raeburn  6531:         my $homeserver;
                   6532:         if (&is_course($cdom,$cnum)) {
                   6533:             $homeserver = &homeserver($cnum,$cdom);
                   6534:         } else {
                   6535:             $homeserver = &domain($cdom,'primary');
                   6536:         }
                   6537:         if ($homeserver ne 'no_host') {
                   6538:             $response = &reply('autorun:'.$cdom,$homeserver);
                   6539:         }
1.876     raeburn  6540:     }
1.506     raeburn  6541:     return $response;
                   6542: }
1.776     albertel 6543: 
1.506     raeburn  6544: sub auto_get_sections {
1.508     raeburn  6545:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  6546:     my $homeserver;
                   6547:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   6548:         $homeserver = &homeserver($cnum,$cdom);
                   6549:     }
                   6550:     if (!defined($homeserver)) { 
                   6551:         if ($cdom =~ /^$match_domain$/) {
                   6552:             $homeserver = &domain($cdom,'primary');
                   6553:         }
                   6554:     }
                   6555:     my @secs;
                   6556:     if (defined($homeserver)) {
                   6557:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   6558:         unless ($response eq 'refused') {
                   6559:             @secs = split(/:/,$response);
                   6560:         }
1.506     raeburn  6561:     }
                   6562:     return @secs;
                   6563: }
1.776     albertel 6564: 
1.506     raeburn  6565: sub auto_new_course {
1.1099    raeburn  6566:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  6567:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  6568:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  6569:     return $response;
                   6570: }
1.776     albertel 6571: 
1.506     raeburn  6572: sub auto_validate_courseID {
1.508     raeburn  6573:     my ($cnum,$cdom,$inst_course_id) = @_;
                   6574:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  6575:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  6576:     return $response;
                   6577: }
1.776     albertel 6578: 
1.1007    raeburn  6579: sub auto_validate_instcode {
1.1020    raeburn  6580:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  6581:     my ($homeserver,$response);
                   6582:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6583:         $homeserver = &homeserver($cnum,$cdom);
                   6584:     }
                   6585:     if (!defined($homeserver)) {
                   6586:         if ($cdom =~ /^$match_domain$/) {
                   6587:             $homeserver = &domain($cdom,'primary');
                   6588:         }
                   6589:     }
1.1065    raeburn  6590:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   6591:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1027    raeburn  6592:     my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
                   6593:     return ($outcome,$description);
1.1007    raeburn  6594: }
                   6595: 
1.506     raeburn  6596: sub auto_create_password {
1.873     raeburn  6597:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   6598:     my ($homeserver,$response);
1.506     raeburn  6599:     my $create_passwd = 0;
                   6600:     my $authchk = '';
1.873     raeburn  6601:     if ($udom =~ /^$match_domain$/) {
                   6602:         $homeserver = &domain($udom,'primary');
                   6603:     }
                   6604:     if ($homeserver eq '') {
                   6605:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6606:             $homeserver = &homeserver($cnum,$cdom);
                   6607:         }
                   6608:     }
                   6609:     if ($homeserver eq '') {
                   6610:         $authchk = 'nodomain';
1.506     raeburn  6611:     } else {
1.873     raeburn  6612:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   6613:         if ($response eq 'refused') {
                   6614:             $authchk = 'refused';
                   6615:         } else {
1.901     albertel 6616:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  6617:         }
1.506     raeburn  6618:     }
                   6619:     return ($authparam,$create_passwd,$authchk);
                   6620: }
                   6621: 
1.706     raeburn  6622: sub auto_photo_permission {
                   6623:     my ($cnum,$cdom,$students) = @_;
                   6624:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 6625:     my ($outcome,$perm_reqd,$conditions) = 
                   6626: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 6627:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6628: 	return (undef,undef);
                   6629:     }
1.706     raeburn  6630:     return ($outcome,$perm_reqd,$conditions);
                   6631: }
                   6632: 
                   6633: sub auto_checkphotos {
                   6634:     my ($uname,$udom,$pid) = @_;
                   6635:     my $homeserver = &homeserver($uname,$udom);
                   6636:     my ($result,$resulttype);
                   6637:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 6638: 				   &escape($uname).':'.&escape($pid),
                   6639: 				   $homeserver));
1.709     albertel 6640:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6641: 	return (undef,undef);
                   6642:     }
1.706     raeburn  6643:     if ($outcome) {
                   6644:         ($result,$resulttype) = split(/:/,$outcome);
                   6645:     } 
                   6646:     return ($result,$resulttype);
                   6647: }
                   6648: 
                   6649: sub auto_photochoice {
                   6650:     my ($cnum,$cdom) = @_;
                   6651:     my $homeserver = &homeserver($cnum,$cdom);
                   6652:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 6653: 						       &escape($cdom),
                   6654: 						       $homeserver)));
1.709     albertel 6655:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6656: 	return (undef,undef);
                   6657:     }
1.706     raeburn  6658:     return ($update,$comment);
                   6659: }
                   6660: 
                   6661: sub auto_photoupdate {
                   6662:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   6663:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 6664:     my $host=&hostname($homeserver);
1.706     raeburn  6665:     my $cmd = '';
                   6666:     my $maxtries = 1;
1.800     albertel 6667:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   6668:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  6669:     }
                   6670:     $cmd =~ s/%%$//;
                   6671:     $cmd = &escape($cmd);
                   6672:     my $query = 'institutionalphotos';
                   6673:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   6674:     unless ($queryid=~/^\Q$host\E\_/) {
                   6675:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   6676:         return 'error: '.$queryid;
                   6677:     }
                   6678:     my $reply = &get_query_reply($queryid);
                   6679:     my $tries = 1;
                   6680:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   6681:         $reply = &get_query_reply($queryid);
                   6682:         $tries ++;
                   6683:     }
                   6684:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6685:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   6686:     } else {
                   6687:         my @responses = split(/:/,$reply);
                   6688:         my $outcome = shift(@responses); 
                   6689:         foreach my $item (@responses) {
                   6690:             my ($key,$value) = split(/=/,$item);
                   6691:             $$photo{$key} = $value;
                   6692:         }
                   6693:         return $outcome;
                   6694:     }
                   6695:     return 'error';
                   6696: }
                   6697: 
1.521     raeburn  6698: sub auto_instcode_format {
1.793     albertel 6699:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   6700: 	$cat_order) = @_;
1.521     raeburn  6701:     my $courses = '';
1.772     raeburn  6702:     my @homeservers;
1.521     raeburn  6703:     if ($caller eq 'global') {
1.841     albertel 6704: 	my %servers = &get_servers($codedom,'library');
                   6705: 	foreach my $tryserver (keys(%servers)) {
                   6706: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6707: 		push(@homeservers,$tryserver);
                   6708: 	    }
1.584     raeburn  6709:         }
1.1022    raeburn  6710:     } elsif ($caller eq 'requests') {
                   6711:         if ($codedom =~ /^$match_domain$/) {
                   6712:             my $chome = &domain($codedom,'primary');
                   6713:             unless ($chome eq 'no_host') {
                   6714:                 push(@homeservers,$chome);
                   6715:             }
                   6716:         }
1.521     raeburn  6717:     } else {
1.772     raeburn  6718:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  6719:     }
1.793     albertel 6720:     foreach my $code (keys(%{$instcodes})) {
                   6721:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  6722:     }
                   6723:     chop($courses);
1.772     raeburn  6724:     my $ok_response = 0;
                   6725:     my $response;
                   6726:     while (@homeservers > 0 && $ok_response == 0) {
                   6727:         my $server = shift(@homeservers); 
                   6728:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   6729:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   6730:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 6731: 		split(/:/,$response);
1.772     raeburn  6732:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   6733:             push(@{$codetitles},&str2array($codetitles_str));
                   6734:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   6735:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   6736:             $ok_response = 1;
                   6737:         }
                   6738:     }
                   6739:     if ($ok_response) {
1.521     raeburn  6740:         return 'ok';
1.772     raeburn  6741:     } else {
                   6742:         return $response;
1.521     raeburn  6743:     }
                   6744: }
                   6745: 
1.792     raeburn  6746: sub auto_instcode_defaults {
                   6747:     my ($domain,$returnhash,$code_order) = @_;
                   6748:     my @homeservers;
1.841     albertel 6749: 
                   6750:     my %servers = &get_servers($domain,'library');
                   6751:     foreach my $tryserver (keys(%servers)) {
                   6752: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6753: 	    push(@homeservers,$tryserver);
                   6754: 	}
1.792     raeburn  6755:     }
1.841     albertel 6756: 
1.792     raeburn  6757:     my $response;
1.841     albertel 6758:     foreach my $server (@homeservers) {
1.792     raeburn  6759:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 6760:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   6761: 	
                   6762: 	foreach my $pair (split(/\&/,$response)) {
                   6763: 	    my ($name,$value)=split(/\=/,$pair);
                   6764: 	    if ($name eq 'code_order') {
                   6765: 		@{$code_order} = split(/\&/,&unescape($value));
                   6766: 	    } else {
                   6767: 		$returnhash->{&unescape($name)}=&unescape($value);
                   6768: 	    }
                   6769: 	}
                   6770: 	return 'ok';
1.792     raeburn  6771:     }
1.841     albertel 6772: 
                   6773:     return $response;
1.1003    raeburn  6774: }
                   6775: 
                   6776: sub auto_possible_instcodes {
1.1007    raeburn  6777:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   6778:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   6779:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   6780:         return;
                   6781:     }
1.1003    raeburn  6782:     my (@homeservers,$uhome);
                   6783:     if (defined(&domain($domain,'primary'))) {
                   6784:         $uhome=&domain($domain,'primary');
                   6785:         push(@homeservers,&domain($domain,'primary'));
                   6786:     } else {
                   6787:         my %servers = &get_servers($domain,'library');
                   6788:         foreach my $tryserver (keys(%servers)) {
                   6789:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6790:                 push(@homeservers,$tryserver);
                   6791:             }
                   6792:         }
                   6793:     }
                   6794:     my $response;
                   6795:     foreach my $server (@homeservers) {
                   6796:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   6797:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  6798:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   6799:             split(':',$response);
                   6800:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   6801:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  6802:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  6803:             my ($name,$value)=split('=',$item);
                   6804:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6805:         }
                   6806:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  6807:             my ($name,$value)=split('=',$item);
                   6808:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6809:         }
                   6810:         return 'ok';
                   6811:     }
                   6812:     return $response;
                   6813: }
1.792     raeburn  6814: 
1.1010    raeburn  6815: sub auto_courserequest_checks {
                   6816:     my ($dom) = @_;
1.1020    raeburn  6817:     my ($homeserver,%validations);
                   6818:     if ($dom =~ /^$match_domain$/) {
                   6819:         $homeserver = &domain($dom,'primary');
                   6820:     }
                   6821:     unless ($homeserver eq 'no_host') {
                   6822:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   6823:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   6824:             my @items = split(/&/,$response);
                   6825:             foreach my $item (@items) {
                   6826:                 my ($key,$value) = split('=',$item);
                   6827:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   6828:             }
                   6829:         }
                   6830:     }
1.1010    raeburn  6831:     return %validations; 
                   6832: }
                   6833: 
1.1020    raeburn  6834: sub auto_courserequest_validation {
                   6835:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
                   6836:     my ($homeserver,$response);
                   6837:     if ($dom =~ /^$match_domain$/) {
                   6838:         $homeserver = &domain($dom,'primary');
                   6839:     }
                   6840:     unless ($homeserver eq 'no_host') {  
1.1021    raeburn  6841:           
1.1020    raeburn  6842:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  6843:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020    raeburn  6844:                                     ':'.&escape($instcode).':'.&escape($instseclist),
                   6845:                                     $homeserver));
                   6846:     }
                   6847:     return $response;
                   6848: }
                   6849: 
1.777     albertel 6850: sub auto_validate_class_sec {
1.918     raeburn  6851:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  6852:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  6853:     my $ownerlist;
                   6854:     if (ref($owners) eq 'ARRAY') {
                   6855:         $ownerlist = join(',',@{$owners});
                   6856:     } else {
                   6857:         $ownerlist = $owners;
                   6858:     }
1.773     raeburn  6859:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  6860:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  6861:     return $response;
                   6862: }
                   6863: 
1.679     raeburn  6864: # ------------------------------------------------------- Course Group routines
                   6865: 
                   6866: sub get_coursegroups {
1.809     raeburn  6867:     my ($cdom,$cnum,$group,$namespace) = @_;
                   6868:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  6869: }
                   6870: 
1.679     raeburn  6871: sub modify_coursegroup {
                   6872:     my ($cdom,$cnum,$groupsettings) = @_;
                   6873:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   6874: }
                   6875: 
1.809     raeburn  6876: sub toggle_coursegroup_status {
                   6877:     my ($cdom,$cnum,$group,$action) = @_;
                   6878:     my ($from_namespace,$to_namespace);
                   6879:     if ($action eq 'delete') {
                   6880:         $from_namespace = 'coursegroups';
                   6881:         $to_namespace = 'deleted_groups';
                   6882:     } else {
                   6883:         $from_namespace = 'deleted_groups';
                   6884:         $to_namespace = 'coursegroups';
                   6885:     }
                   6886:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  6887:     if (my $tmp = &error(%curr_group)) {
                   6888:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   6889:         return ('read error',$tmp);
                   6890:     } else {
                   6891:         my %savedsettings = %curr_group; 
1.809     raeburn  6892:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  6893:         my $deloutcome;
                   6894:         if ($result eq 'ok') {
1.809     raeburn  6895:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  6896:         } else {
                   6897:             return ('write error',$result);
                   6898:         }
                   6899:         if ($deloutcome eq 'ok') {
                   6900:             return 'ok';
                   6901:         } else {
                   6902:             return ('delete error',$deloutcome);
                   6903:         }
                   6904:     }
                   6905: }
                   6906: 
1.679     raeburn  6907: sub modify_group_roles {
1.957     raeburn  6908:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  6909:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   6910:     my $role = 'gr/'.&escape($userprivs);
                   6911:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  6912:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  6913:     if ($result eq 'ok') {
                   6914:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   6915:     }
1.679     raeburn  6916:     return $result;
                   6917: }
                   6918: 
                   6919: sub modify_coursegroup_membership {
                   6920:     my ($cdom,$cnum,$membership) = @_;
                   6921:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   6922:     return $result;
                   6923: }
                   6924: 
1.682     raeburn  6925: sub get_active_groups {
                   6926:     my ($udom,$uname,$cdom,$cnum) = @_;
                   6927:     my $now = time;
                   6928:     my %groups = ();
                   6929:     foreach my $key (keys(%env)) {
1.811     albertel 6930:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  6931:             my ($start,$end) = split(/\./,$env{$key});
                   6932:             if (($end!=0) && ($end<$now)) { next; }
                   6933:             if (($start!=0) && ($start>$now)) { next; }
                   6934:             if ($1 eq $cdom && $2 eq $cnum) {
                   6935:                 $groups{$3} = $env{$key} ;
                   6936:             }
                   6937:         }
                   6938:     }
                   6939:     return %groups;
                   6940: }
                   6941: 
1.683     raeburn  6942: sub get_group_membership {
                   6943:     my ($cdom,$cnum,$group) = @_;
                   6944:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   6945: }
                   6946: 
                   6947: sub get_users_groups {
                   6948:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  6949:     my @usersgroups;
1.683     raeburn  6950:     my $cachetime=1800;
                   6951: 
                   6952:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  6953:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   6954:     if (defined($cached)) {
1.734     albertel 6955:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  6956:     } else {  
                   6957:         $grouplist = '';
1.816     raeburn  6958:         my $courseurl = &courseid_to_courseurl($courseid);
1.1086    raeburn  6959:         my $extra = &freeze_escape({'skipcheck' => 1});
                   6960:         my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817     raeburn  6961:         my $access_end = $env{'course.'.$courseid.
                   6962:                               '.default_enrollment_end_date'};
                   6963:         my $now = time;
                   6964:         foreach my $key (keys(%roleshash)) {
                   6965:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   6966:                 my $group = $1;
                   6967:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   6968:                     my $start = $2;
                   6969:                     my $end = $1;
                   6970:                     if ($start == -1) { next; } # deleted from group
                   6971:                     if (($start!=0) && ($start>$now)) { next; }
                   6972:                     if (($end!=0) && ($end<$now)) {
                   6973:                         if ($access_end && $access_end < $now) {
                   6974:                             if ($access_end - $end < 86400) {
                   6975:                                 push(@usersgroups,$group);
1.733     raeburn  6976:                             }
                   6977:                         }
1.817     raeburn  6978:                         next;
1.733     raeburn  6979:                     }
1.817     raeburn  6980:                     push(@usersgroups,$group);
1.683     raeburn  6981:                 }
                   6982:             }
                   6983:         }
1.817     raeburn  6984:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   6985:         $grouplist = join(':',@usersgroups);
                   6986:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  6987:     }
1.733     raeburn  6988:     return @usersgroups;
1.683     raeburn  6989: }
                   6990: 
                   6991: sub devalidate_getgroups_cache {
                   6992:     my ($udom,$uname,$cdom,$cnum)=@_;
                   6993:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 6994: 
1.683     raeburn  6995:     my $hashid="$udom:$uname:$courseid";
                   6996:     &devalidate_cache_new('getgroups',$hashid);
                   6997: }
                   6998: 
1.12      www      6999: # ------------------------------------------------------------------ Plain Text
                   7000: 
                   7001: sub plaintext {
1.988     raeburn  7002:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  7003:     if ($short =~ m{^cr/}) {
1.758     albertel 7004: 	return (split('/',$short))[-1];
                   7005:     }
1.742     raeburn  7006:     if (!defined($cid)) {
                   7007:         $cid = $env{'request.course.id'};
                   7008:     }
                   7009:     my %rolenames = (
1.1008    raeburn  7010:                       Course    => 'std',
                   7011:                       Community => 'alt1',
1.742     raeburn  7012:                     );
1.1037    raeburn  7013:     if ($cid ne '') {
                   7014:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   7015:             unless ($forcedefault) {
                   7016:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   7017:                 &Apache::lonlocal::mt_escape(\$roletext);
                   7018:                 return &Apache::lonlocal::mt($roletext);
                   7019:             }
                   7020:         }
                   7021:     }
                   7022:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   7023:         (defined($rolenames{$type})) && 
                   7024:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  7025:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  7026:     } elsif ($cid ne '') {
                   7027:         my $crstype = $env{'course.'.$cid.'.type'};
                   7028:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   7029:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   7030:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   7031:         }
1.742     raeburn  7032:     }
1.1037    raeburn  7033:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      7034: }
                   7035: 
                   7036: # ----------------------------------------------------------------- Assign Role
                   7037: 
                   7038: sub assignrole {
1.957     raeburn  7039:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   7040:         $context)=@_;
1.21      www      7041:     my $mrole;
                   7042:     if ($role =~ /^cr\//) {
1.393     www      7043:         my $cwosec=$url;
1.811     albertel 7044:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      7045: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  7046:            my $refused = 1;
                   7047:            if ($context eq 'requestcourses') {
                   7048:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   7049:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   7050:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   7051:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   7052:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7053:                            if ($crsenv{'internal.courseowner'} eq
                   7054:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   7055:                                $refused = '';
                   7056:                            }
                   7057:                        }
                   7058:                    }
                   7059:                }
                   7060:            }
                   7061:            if ($refused) {
                   7062:                &logthis('Refused custom assignrole: '.
                   7063:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   7064:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   7065:                return 'refused';
                   7066:            }
1.104     www      7067:         }
1.21      www      7068:         $mrole='cr';
1.678     raeburn  7069:     } elsif ($role =~ /^gr\//) {
                   7070:         my $cwogrp=$url;
1.811     albertel 7071:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  7072:         unless (&allowed('mdg',$cwogrp)) {
                   7073:             &logthis('Refused group assignrole: '.
                   7074:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   7075:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   7076:             return 'refused';
                   7077:         }
                   7078:         $mrole='gr';
1.21      www      7079:     } else {
1.82      www      7080:         my $cwosec=$url;
1.811     albertel 7081:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  7082:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   7083:             my $refused;
                   7084:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   7085:                 if (!(&allowed('c'.$role,$url))) {
                   7086:                     $refused = 1;
                   7087:                 }
                   7088:             } else {
                   7089:                 $refused = 1;
                   7090:             }
1.947     raeburn  7091:             if ($refused) {
1.1045    raeburn  7092:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   7093:                 if (!$selfenroll && $context eq 'course') {
                   7094:                     my %crsenv;
                   7095:                     if ($role eq 'cc' || $role eq 'co') {
                   7096:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7097:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   7098:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   7099:                                 if ($crsenv{'internal.courseowner'} eq 
                   7100:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   7101:                                     $refused = '';
                   7102:                                 }
                   7103:                             }
                   7104:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   7105:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   7106:                                 if ($crsenv{'internal.courseowner'} eq 
                   7107:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   7108:                                     $refused = '';
                   7109:                                 }
                   7110:                             }
                   7111:                         }
                   7112:                     }
                   7113:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947     raeburn  7114:                     $refused = '';
1.1017    raeburn  7115:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  7116:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  7117:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  7118:                         my $wrongcc;
                   7119:                         if ($cnum =~ /^$match_community$/) {
                   7120:                             $wrongcc = 1 if ($role eq 'cc');
                   7121:                         } else {
                   7122:                             $wrongcc = 1 if ($role eq 'co');
                   7123:                         }
                   7124:                         unless ($wrongcc) {
                   7125:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   7126:                             if ($crsenv{'internal.courseowner'} eq 
                   7127:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   7128:                                 $refused = '';
                   7129:                             }
1.1017    raeburn  7130:                         }
                   7131:                     }
                   7132:                 }
                   7133:                 if ($refused) {
1.947     raeburn  7134:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   7135:                              ' '.$role.' '.$end.' '.$start.' by '.
                   7136: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   7137:                     return 'refused';
                   7138:                 }
1.932     raeburn  7139:             }
1.1131    raeburn  7140:         } elsif ($role eq 'au') {
                   7141:             if ($url ne '/'.$udom.'/') {
                   7142:                 &logthis('Attempt by '.$env{'user.name'}.':'.$env{'user.domain'}.
                   7143:                          ' to assign author role for '.$uname.':'.$udom.
                   7144:                          ' in domain: '.$url.' refused (wrong domain).');
                   7145:                 return 'refused';
                   7146:             }
1.104     www      7147:         }
1.21      www      7148:         $mrole=$role;
                   7149:     }
1.620     albertel 7150:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      7151:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      7152:     if ($end) { $command.='_'.$end; }
1.21      www      7153:     if ($start) {
                   7154: 	if ($end) { 
1.81      www      7155:            $command.='_'.$start; 
1.21      www      7156:         } else {
1.81      www      7157:            $command.='_0_'.$start;
1.21      www      7158:         }
                   7159:     }
1.739     raeburn  7160:     my $origstart = $start;
                   7161:     my $origend = $end;
1.957     raeburn  7162:     my $delflag;
1.357     www      7163: # actually delete
                   7164:     if ($deleteflag) {
1.373     www      7165: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      7166: # modify command to delete the role
1.620     albertel 7167:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      7168:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 7169: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      7170: # set start and finish to negative values for userrolelog
                   7171:            $start=-1;
                   7172:            $end=-1;
1.957     raeburn  7173:            $delflag = 1;
1.357     www      7174:         }
                   7175:     }
                   7176: # send command
1.349     www      7177:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      7178: # log new user role if status is ok
1.349     www      7179:     if ($answer eq 'ok') {
1.663     raeburn  7180: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  7181: # for course roles, perform group memberships changes triggered by role change.
1.957     raeburn  7182:         &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739     raeburn  7183:         unless ($role =~ /^gr/) {
                   7184:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957     raeburn  7185:                                              $origstart,$selfenroll,$context);
1.739     raeburn  7186:         }
1.1053    raeburn  7187:         if ($role eq 'cc') {
                   7188:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   7189:         }
1.349     www      7190:     }
                   7191:     return $answer;
1.169     harris41 7192: }
                   7193: 
1.1053    raeburn  7194: sub autoupdate_coowners {
                   7195:     my ($url,$end,$start,$uname,$udom) = @_;
                   7196:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   7197:     if (($cdom ne '') && ($cnum ne '')) {
                   7198:         my $now = time;
                   7199:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   7200:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   7201:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   7202:             my $instcode = $coursehash{'internal.coursecode'};
                   7203:             if ($instcode ne '') {
1.1056    raeburn  7204:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   7205:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  7206:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  7207:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   7208:                         if ($result eq 'valid') {
                   7209:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  7210:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   7211:                                     push(@newcoowners,$coowner);
                   7212:                                 }
                   7213:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   7214:                                     push(@newcoowners,$uname.':'.$udom);
                   7215:                                 }
                   7216:                                 @newcoowners = sort(@newcoowners);
                   7217:                             } else {
                   7218:                                 push(@newcoowners,$uname.':'.$udom);
                   7219:                             }
                   7220:                         } else {
                   7221:                             if ($coursehash{'internal.co-owners'}) {
                   7222:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   7223:                                     unless ($coowner eq $uname.':'.$udom) {
                   7224:                                         push(@newcoowners,$coowner);
                   7225:                                     }
                   7226:                                 }
                   7227:                                 unless (@newcoowners > 0) {
                   7228:                                     $delcoowners = 1;
                   7229:                                     $coowners = '';
                   7230:                                 }
                   7231:                             }
                   7232:                         }
                   7233:                         if (@newcoowners || $delcoowners) {
                   7234:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   7235:                                             $delcoowners,@newcoowners);
                   7236:                         }
                   7237:                     }
                   7238:                 }
                   7239:             }
                   7240:         }
                   7241:     }
                   7242: }
                   7243: 
                   7244: sub store_coowners {
                   7245:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   7246:     my $cid = $cdom.'_'.$cnum;
                   7247:     my ($coowners,$delresult,$putresult);
                   7248:     if (@newcoowners) {
                   7249:         $coowners = join(',',@newcoowners);
                   7250:         my %coownershash = (
                   7251:                             'internal.co-owners' => $coowners,
                   7252:                            );
                   7253:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   7254:         if ($putresult eq 'ok') {
                   7255:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   7256:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   7257:             }
                   7258:         }
                   7259:     }
                   7260:     if ($delcoowners) {
                   7261:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   7262:         if ($delresult eq 'ok') {
                   7263:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   7264:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   7265:             }
                   7266:         }
                   7267:     }
                   7268:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   7269:         my %crsinfo =
                   7270:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   7271:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   7272:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   7273:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   7274:         }
                   7275:     }
                   7276: }
                   7277: 
1.169     harris41 7278: # -------------------------------------------------- Modify user authentication
1.197     www      7279: # Overrides without validation
                   7280: 
1.169     harris41 7281: sub modifyuserauth {
                   7282:     my ($udom,$uname,$umode,$upass)=@_;
                   7283:     my $uhome=&homeserver($uname,$udom);
1.197     www      7284:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   7285:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 7286:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   7287:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 7288:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   7289: 		     &escape($upass),$uhome);
1.620     albertel 7290:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      7291:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   7292:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   7293:     &log($udom,,$uname,$uhome,
1.620     albertel 7294:         'Authentication changed by '.$env{'user.domain'}.', '.
                   7295:                                      $env{'user.name'}.', '.$umode.
1.197     www      7296:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 7297:     unless ($reply eq 'ok') {
1.197     www      7298:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 7299: 	return 'error: '.$reply;
                   7300:     }   
1.170     harris41 7301:     return 'ok';
1.80      www      7302: }
                   7303: 
1.81      www      7304: # --------------------------------------------------------------- Modify a user
1.80      www      7305: 
1.81      www      7306: sub modifyuser {
1.206     matthew  7307:     my ($udom,    $uname, $uid,
                   7308:         $umode,   $upass, $first,
                   7309:         $middle,  $last,  $gene,
1.1058    raeburn  7310:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 7311:     $udom= &LONCAPA::clean_domain($udom);
                   7312:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  7313:     my $showcandelete = 'none';
                   7314:     if (ref($candelete) eq 'ARRAY') {
                   7315:         if (@{$candelete} > 0) {
                   7316:             $showcandelete = join(', ',@{$candelete});
                   7317:         }
                   7318:     }
1.81      www      7319:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      7320:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  7321: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  7322:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   7323:                                      ' desiredhome not specified'). 
1.620     albertel 7324:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   7325:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 7326:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  7327:     my $newuser;
                   7328:     if ($uhome eq 'no_host') {
                   7329:         $newuser = 1;
                   7330:     }
1.80      www      7331: # ----------------------------------------------------------------- Create User
1.406     albertel 7332:     if (($uhome eq 'no_host') && 
                   7333: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      7334:         my $unhome='';
1.844     albertel 7335:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  7336:             $unhome = $desiredhome;
1.620     albertel 7337: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   7338: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  7339:         } else { # load balancing routine for determining $unhome
1.81      www      7340:             my $loadm=10000000;
1.841     albertel 7341: 	    my %servers = &get_servers($udom,'library');
                   7342: 	    foreach my $tryserver (keys(%servers)) {
                   7343: 		my $answer=reply('load',$tryserver);
                   7344: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   7345: 		    $loadm=$answer;
                   7346: 		    $unhome=$tryserver;
                   7347: 		}
1.80      www      7348: 	    }
                   7349:         }
                   7350:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  7351: 	    return 'error: unable to find a home server for '.$uname.
                   7352:                    ' in domain '.$udom;
1.80      www      7353:         }
                   7354:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   7355:                          &escape($upass),$unhome);
                   7356: 	unless ($reply eq 'ok') {
                   7357:             return 'error: '.$reply;
                   7358:         }   
1.230     stredwic 7359:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      7360:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  7361: 	    return 'error: unable verify users home machine.';
1.80      www      7362:         }
1.209     matthew  7363:     }   # End of creation of new user
1.80      www      7364: # ---------------------------------------------------------------------- Add ID
                   7365:     if ($uid) {
                   7366:        $uid=~tr/A-Z/a-z/;
                   7367:        my %uidhash=&idrget($udom,$uname);
1.196     www      7368:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   7369:          && (!$forceid)) {
1.80      www      7370: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  7371: 	      return 'error: user id "'.$uid.'" does not match '.
                   7372:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      7373:           }
                   7374:        } else {
                   7375: 	  &idput($udom,($uname => $uid));
                   7376:        }
                   7377:     }
                   7378: # -------------------------------------------------------------- Add names, etc
1.313     matthew  7379:     my @tmp=&get('environment',
1.899     raeburn  7380: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  7381:                     'permanentemail','inststatus'],
1.134     albertel 7382: 		   $udom,$uname);
1.1075    raeburn  7383:     my (%names,%oldnames);
1.313     matthew  7384:     if ($tmp[0] =~ m/^error:.*/) { 
                   7385:         %names=(); 
                   7386:     } else {
                   7387:         %names = @tmp;
1.1075    raeburn  7388:         %oldnames = %names;
1.313     matthew  7389:     }
1.388     www      7390: #
1.1058    raeburn  7391: # If name, email and/or uid are blank (e.g., because an uploaded file
                   7392: # of users did not contain them), do not overwrite existing values
                   7393: # unless field is in $candelete array ref.  
                   7394: #
                   7395: 
                   7396:     my @fields = ('firstname','middlename','lastname','generation',
                   7397:                   'permanentemail','id');
                   7398:     my %newvalues;
                   7399:     if (ref($candelete) eq 'ARRAY') {
                   7400:         foreach my $field (@fields) {
                   7401:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   7402:                 if ($field eq 'firstname') {
                   7403:                     $names{$field} = $first;
                   7404:                 } elsif ($field eq 'middlename') {
                   7405:                     $names{$field} = $middle;
                   7406:                 } elsif ($field eq 'lastname') {
                   7407:                     $names{$field} = $last;
                   7408:                 } elsif ($field eq 'generation') { 
                   7409:                     $names{$field} = $gene;
                   7410:                 } elsif ($field eq 'permanentemail') {
                   7411:                     $names{$field} = $email;
                   7412:                 } elsif ($field eq 'id') {
                   7413:                     $names{$field}  = $uid;
                   7414:                 }
                   7415:             }
                   7416:         }
                   7417:     }
1.388     www      7418:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  7419:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      7420:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  7421:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      7422:     if ($email) {
                   7423:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  7424:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      7425:     }
1.899     raeburn  7426:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  7427:     if (defined($inststatus)) {
                   7428:         $names{'inststatus'} = '';
                   7429:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   7430:         if (ref($usertypes) eq 'HASH') {
                   7431:             my @okstatuses; 
                   7432:             foreach my $item (split(/:/,$inststatus)) {
                   7433:                 if (defined($usertypes->{$item})) {
                   7434:                     push(@okstatuses,$item);  
                   7435:                 }
                   7436:             }
                   7437:             if (@okstatuses) {
                   7438:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   7439:             }
                   7440:         }
                   7441:     }
1.1075    raeburn  7442:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  7443:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  7444:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  7445:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   7446:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   7447:     } else {
                   7448:         $logmsg .= ' during self creation';
                   7449:     }
1.1075    raeburn  7450:     my $changed;
                   7451:     if ($newuser) {
                   7452:         $changed = 1;
                   7453:     } else {
                   7454:         foreach my $field (@fields) {
                   7455:             if ($names{$field} ne $oldnames{$field}) {
                   7456:                 $changed = 1;
                   7457:                 last;
                   7458:             }
                   7459:         }
                   7460:     }
                   7461:     unless ($changed) {
                   7462:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   7463:         &logthis($logmsg);
                   7464:         return 'ok';
                   7465:     }
                   7466:     my $reply = &put('environment', \%names, $udom,$uname);
                   7467:     if ($reply ne 'ok') { 
                   7468:         return 'error: '.$reply;
                   7469:     }
1.1087    raeburn  7470:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   7471:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   7472:     }
1.1075    raeburn  7473:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   7474:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   7475:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  7476:     &logthis($logmsg);
1.134     albertel 7477:     return 'ok';
1.80      www      7478: }
                   7479: 
1.81      www      7480: # -------------------------------------------------------------- Modify student
1.80      www      7481: 
1.81      www      7482: sub modifystudent {
                   7483:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  7484:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990     raeburn  7485:         $selfenroll,$context,$inststatus)=@_;
1.455     albertel 7486:     if (!$cid) {
1.620     albertel 7487: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7488: 	    return 'not_in_class';
                   7489: 	}
1.80      www      7490:     }
                   7491: # --------------------------------------------------------------- Make the user
1.81      www      7492:     my $reply=&modifyuser
1.209     matthew  7493: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  7494:          $desiredhome,$email,$inststatus);
1.80      www      7495:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  7496:     # This will cause &modify_student_enrollment to get the uid from the
                   7497:     # students environment
                   7498:     $uid = undef if (!$forceid);
1.455     albertel 7499:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957     raeburn  7500: 					$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297     matthew  7501:     return $reply;
                   7502: }
                   7503: 
                   7504: sub modify_student_enrollment {
1.957     raeburn  7505:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455     albertel 7506:     my ($cdom,$cnum,$chome);
                   7507:     if (!$cid) {
1.620     albertel 7508: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7509: 	    return 'not_in_class';
                   7510: 	}
1.620     albertel 7511: 	$cdom=$env{'course.'.$cid.'.domain'};
                   7512: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 7513:     } else {
                   7514: 	($cdom,$cnum)=split(/_/,$cid);
                   7515:     }
1.620     albertel 7516:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 7517:     if (!$chome) {
1.457     raeburn  7518: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  7519:     }
1.455     albertel 7520:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  7521:     # Make sure the user exists
1.81      www      7522:     my $uhome=&homeserver($uname,$udom);
                   7523:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7524: 	return 'error: no such user';
                   7525:     }
1.297     matthew  7526:     # Get student data if we were not given enough information
                   7527:     if (!defined($first)  || $first  eq '' || 
                   7528:         !defined($last)   || $last   eq '' || 
                   7529:         !defined($uid)    || $uid    eq '' || 
                   7530:         !defined($middle) || $middle eq '' || 
                   7531:         !defined($gene)   || $gene   eq '') {
1.294     matthew  7532:         # They did not supply us with enough data to enroll the student, so
                   7533:         # we need to pick up more information.
1.297     matthew  7534:         my %tmp = &get('environment',
1.294     matthew  7535:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  7536:                        ,$udom,$uname);
                   7537: 
1.800     albertel 7538:         #foreach my $key (keys(%tmp)) {
                   7539:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 7540:         #}
1.294     matthew  7541:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   7542:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   7543:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  7544:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  7545:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   7546:     }
1.556     albertel 7547:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.1148    raeburn  7548:     my $user = "$uname:$udom";
                   7549:     my %old_entry = &Apache::lonnet::get('classlist',[$user],$cdom,$cnum);
1.487     albertel 7550:     my $reply=cput('classlist',
1.1148    raeburn  7551: 		   {$user => 
1.515     raeburn  7552: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 7553: 		   $cdom,$cnum);
1.1148    raeburn  7554:     if (($reply eq 'ok') || ($reply eq 'delayed')) {
                   7555:         &devalidate_getsection_cache($udom,$uname,$cid);
                   7556:     } else { 
1.81      www      7557: 	return 'error: '.$reply;
                   7558:     }
1.297     matthew  7559:     # Add student role to user
1.83      www      7560:     my $uurl='/'.$cid;
1.81      www      7561:     $uurl=~s/\_/\//g;
                   7562:     if ($usec) {
                   7563: 	$uurl.='/'.$usec;
                   7564:     }
1.1148    raeburn  7565:     my $result = &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,
                   7566:                              $selfenroll,$context);
                   7567:     if ($result ne 'ok') {
                   7568:         if ($old_entry{$user} ne '') {
                   7569:             $reply = &cput('classlist',\%old_entry,$cdom,$cnum);
                   7570:         } else {
                   7571:             $reply = &del('classlist',[$user],$cdom,$cnum);
                   7572:         }
                   7573:     }
                   7574:     return $result; 
1.21      www      7575: }
                   7576: 
1.556     albertel 7577: sub format_name {
                   7578:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   7579:     my $name;
                   7580:     if ($first ne 'lastname') {
                   7581: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   7582:     } else {
                   7583: 	if ($lastname=~/\S/) {
                   7584: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   7585: 	    $name=~s/\s+,/,/;
                   7586: 	} else {
                   7587: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   7588: 	}
                   7589:     }
                   7590:     $name=~s/^\s+//;
                   7591:     $name=~s/\s+$//;
                   7592:     $name=~s/\s+/ /g;
                   7593:     return $name;
                   7594: }
                   7595: 
1.84      www      7596: # ------------------------------------------------- Write to course preferences
                   7597: 
                   7598: sub writecoursepref {
                   7599:     my ($courseid,%prefs)=@_;
                   7600:     $courseid=~s/^\///;
                   7601:     $courseid=~s/\_/\//g;
                   7602:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   7603:     my $chome=homeserver($cnum,$cdomain);
                   7604:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   7605: 	return 'error: no such course';
                   7606:     }
                   7607:     my $cstring='';
1.800     albertel 7608:     foreach my $pref (keys(%prefs)) {
                   7609: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 7610:     }
1.84      www      7611:     $cstring=~s/\&$//;
                   7612:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   7613: }
                   7614: 
                   7615: # ---------------------------------------------------------- Make/modify course
                   7616: 
                   7617: sub createcourse {
1.741     raeburn  7618:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  7619:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      7620:     $url=&declutter($url);
                   7621:     my $cid='';
1.1028    raeburn  7622:     if ($context eq 'requestcourses') {
                   7623:         my $can_create = 0;
                   7624:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   7625:         if ($udom eq $ownerdom) {
                   7626:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   7627:                                   $context)) {
                   7628:                 $can_create = 1;
                   7629:             }
                   7630:         } else {
                   7631:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   7632:                                            $category);
                   7633:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   7634:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   7635:                 if (@curr > 0) {
                   7636:                     my @options = qw(approval validate autolimit);
                   7637:                     my $optregex = join('|',@options);
                   7638:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   7639:                         $can_create = 1;
                   7640:                     }
                   7641:                 }
                   7642:             }
                   7643:         }
                   7644:         if ($can_create) {
                   7645:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   7646:                 unless (&allowed('ccc',$udom)) {
                   7647:                     return 'refused'; 
                   7648:                 }
1.1017    raeburn  7649:             }
                   7650:         } else {
                   7651:             return 'refused';
                   7652:         }
1.1028    raeburn  7653:     } elsif (!&allowed('ccc',$udom)) {
                   7654:         return 'refused';
1.84      www      7655:     }
1.1011    raeburn  7656: # --------------------------------------------------------------- Get Unique ID
                   7657:     my $uname;
                   7658:     if ($cnum =~ /^$match_courseid$/) {
                   7659:         my $chome=&homeserver($cnum,$udom,'true');
                   7660:         if (($chome eq '') || ($chome eq 'no_host')) {
                   7661:             $uname = $cnum;
                   7662:         } else {
1.1038    raeburn  7663:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7664:         }
                   7665:     } else {
1.1038    raeburn  7666:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7667:     }
                   7668:     return $uname if ($uname =~ /^error/);
                   7669: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  7670:     if (!defined($course_server)) {
                   7671:         if (defined(&domain($udom,'primary'))) {
                   7672:             $course_server = &domain($udom,'primary');
                   7673:         } else {
                   7674:             $course_server = $env{'user.home'}; 
                   7675:         }
                   7676:     }
                   7677:     my %host_servers =
                   7678:         &Apache::lonnet::get_servers($udom,'library');
                   7679:     unless ($host_servers{$course_server}) {
                   7680:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  7681:     }
1.84      www      7682: # ------------------------------------------------------------- Make the course
                   7683:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  7684:                       $course_server);
1.84      www      7685:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  7686:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      7687:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7688: 	return 'error: no such course';
                   7689:     }
1.271     www      7690: # ----------------------------------------------------------------- Course made
1.516     raeburn  7691: # log existence
1.1029    raeburn  7692:     my $now = time;
1.918     raeburn  7693:     my $newcourse = {
                   7694:                     $udom.'_'.$uname => {
1.921     raeburn  7695:                                      description => $description,
                   7696:                                      inst_code   => $inst_code,
                   7697:                                      owner       => $course_owner,
                   7698:                                      type        => $crstype,
1.1029    raeburn  7699:                                      creator     => $env{'user.name'}.':'.
                   7700:                                                     $env{'user.domain'},
                   7701:                                      created     => $now,
                   7702:                                      context     => $context,
1.918     raeburn  7703:                                                 },
                   7704:                     };
1.921     raeburn  7705:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      7706: # set toplevel url
1.271     www      7707:     my $topurl=$url;
                   7708:     unless ($nonstandard) {
                   7709: # ------------------------------------------ For standard courses, make top url
                   7710:         my $mapurl=&clutter($url);
1.278     www      7711:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 7712:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      7713: <map>
                   7714: <resource id="1" type="start"></resource>
                   7715: <resource id="2" src="$mapurl"></resource>
                   7716: <resource id="3" type="finish"></resource>
                   7717: <link index="1" from="1" to="2"></link>
                   7718: <link index="2" from="2" to="3"></link>
                   7719: </map>
                   7720: ENDINITMAP
                   7721:         $topurl=&declutter(
1.638     albertel 7722:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      7723:                           );
                   7724:     }
                   7725: # ----------------------------------------------------------- Write preferences
1.84      www      7726:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  7727:                      ('description'              => $description,
                   7728:                       'url'                      => $topurl,
                   7729:                       'internal.creator'         => $env{'user.name'}.':'.
                   7730:                                                     $env{'user.domain'},
                   7731:                       'internal.created'         => $now,
                   7732:                       'internal.creationcontext' => $context)
                   7733:                     );
1.84      www      7734:     return '/'.$udom.'/'.$uname;
                   7735: }
                   7736: 
1.1011    raeburn  7737: # ------------------------------------------------------------------- Create ID
                   7738: sub generate_coursenum {
1.1038    raeburn  7739:     my ($udom,$crstype) = @_;
1.1011    raeburn  7740:     my $domdesc = &domain($udom);
                   7741:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  7742:     my $first;
                   7743:     if ($crstype eq 'Community') {
                   7744:         $first = '0';
                   7745:     } else {
                   7746:         $first = int(1+rand(9)); 
                   7747:     } 
                   7748:     my $uname=$first.
1.1011    raeburn  7749:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7750:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7751:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7752: # ----------------------------------------------- Make sure that does not exist
                   7753:     my $uhome=&homeserver($uname,$udom,'true');
                   7754:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  7755:         if ($crstype eq 'Community') {
                   7756:             $first = '0';
                   7757:         } else {
                   7758:             $first = int(1+rand(9));
                   7759:         }
                   7760:         $uname=$first.
1.1011    raeburn  7761:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7762:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7763:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7764:         $uhome=&homeserver($uname,$udom,'true');
                   7765:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   7766:             return 'error: unable to generate unique course-ID';
                   7767:         }
                   7768:     }
                   7769:     return $uname;
                   7770: }
                   7771: 
1.813     albertel 7772: sub is_course {
                   7773:     my ($cdom,$cnum) = @_;
                   7774:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946     raeburn  7775: 				undef,'.');
1.813     albertel 7776:     if (exists($courses{$cdom.'_'.$cnum})) {
                   7777:         return 1;
                   7778:     }
                   7779:     return 0;
                   7780: }
                   7781: 
1.1015    raeburn  7782: sub store_userdata {
                   7783:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  7784:     my $result;
1.1016    raeburn  7785:     if ($datakey ne '') {
1.1013    raeburn  7786:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  7787:             if ($udom eq '' || $uname eq '') {
                   7788:                 $udom = $env{'user.domain'};
                   7789:                 $uname = $env{'user.name'};
                   7790:             }
                   7791:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  7792:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   7793:                 $result = 'error: no_host';
                   7794:             } else {
                   7795:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   7796:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   7797: 
                   7798:                 my $namevalue='';
                   7799:                 foreach my $key (keys(%{$storehash})) {
                   7800:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7801:                 }
                   7802:                 $namevalue=~s/\&$//;
1.1105    raeburn  7803:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   7804:                                   $namevalue,$uhome);
1.1013    raeburn  7805:             }
                   7806:         } else {
                   7807:             $result = 'error: data to store was not a hash reference'; 
                   7808:         }
                   7809:     } else {
                   7810:         $result= 'error: invalid requestkey'; 
                   7811:     }
                   7812:     return $result;
                   7813: }
                   7814: 
1.21      www      7815: # ---------------------------------------------------------- Assign Custom Role
                   7816: 
                   7817: sub assigncustomrole {
1.957     raeburn  7818:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7819:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  7820:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      7821: }
                   7822: 
                   7823: # ----------------------------------------------------------------- Revoke Role
                   7824: 
                   7825: sub revokerole {
1.957     raeburn  7826:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7827:     my $now=time;
1.965     raeburn  7828:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      7829: }
                   7830: 
                   7831: # ---------------------------------------------------------- Revoke Custom Role
                   7832: 
                   7833: sub revokecustomrole {
1.957     raeburn  7834:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7835:     my $now=time;
1.357     www      7836:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  7837:            $deleteflag,$selfenroll,$context);
1.17      www      7838: }
                   7839: 
1.533     banghart 7840: # ------------------------------------------------------------ Disk usage
1.535     albertel 7841: sub diskusage {
1.955     raeburn  7842:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   7843:     $directorypath =~ s/\/$//;
                   7844:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   7845:                        .&escape($getpropath).':'.&escape($uname).':'
                   7846:                        .&escape($udom),homeserver($uname,$udom));
                   7847:     if ($listing eq 'unknown_cmd') {
                   7848:         if ($getpropath) {
                   7849:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   7850:         }
                   7851:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   7852:     }
1.514     albertel 7853:     return $listing;
1.512     banghart 7854: }
                   7855: 
1.566     banghart 7856: sub is_locked {
1.1096    raeburn  7857:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 7858:     my @check;
                   7859:     my $is_locked;
1.1093    raeburn  7860:     push (@check,$file_name);
1.613     albertel 7861:     my %locked = &get('file_permissions',\@check,
1.620     albertel 7862: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 7863:     my ($tmp)=keys(%locked);
                   7864:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  7865:     
1.566     banghart 7866:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  7867:         $is_locked = 'false';
                   7868:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  7869:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  7870:                $is_locked = 'true';
1.1096    raeburn  7871:                if (ref($which) eq 'ARRAY') {
                   7872:                    push(@{$which},$entry);
                   7873:                } else {
                   7874:                    last;
                   7875:                }
1.745     raeburn  7876:            }
                   7877:        }
1.566     banghart 7878:     } else {
                   7879:         $is_locked = 'false';
                   7880:     }
1.1093    raeburn  7881:     return $is_locked;
1.566     banghart 7882: }
                   7883: 
1.759     albertel 7884: sub declutter_portfile {
                   7885:     my ($file) = @_;
1.833     albertel 7886:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 7887:     return $file;
                   7888: }
                   7889: 
1.559     banghart 7890: # ------------------------------------------------------------- Mark as Read Only
                   7891: 
                   7892: sub mark_as_readonly {
                   7893:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 7894:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7895:     my ($tmp)=keys(%current_permissions);
                   7896:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 7897:     foreach my $file (@{$files}) {
1.759     albertel 7898: 	$file = &declutter_portfile($file);
1.561     banghart 7899:         push(@{$current_permissions{$file}},$what);
1.559     banghart 7900:     }
1.613     albertel 7901:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 7902:     return;
                   7903: }
                   7904: 
1.572     banghart 7905: # ------------------------------------------------------------Save Selected Files
                   7906: 
                   7907: sub save_selected_files {
                   7908:     my ($user, $path, @files) = @_;
                   7909:     my $filename = $user."savedfiles";
1.573     banghart 7910:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 7911:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 7912:     foreach my $file (@files) {
1.620     albertel 7913:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 7914:     }
                   7915:     foreach my $file (@other_files) {
1.574     banghart 7916:         print (OUT $file."\n");
1.572     banghart 7917:     }
1.574     banghart 7918:     close (OUT);
1.572     banghart 7919:     return 'ok';
                   7920: }
                   7921: 
1.574     banghart 7922: sub clear_selected_files {
                   7923:     my ($user) = @_;
                   7924:     my $filename = $user."savedfiles";
1.1117    foxr     7925:     open (OUT, '>'.LONCAPA::tempdir().$filename);
1.574     banghart 7926:     print (OUT undef);
                   7927:     close (OUT);
                   7928:     return ("ok");    
                   7929: }
                   7930: 
1.572     banghart 7931: sub files_in_path {
                   7932:     my ($user, $path) = @_;
                   7933:     my $filename = $user."savedfiles";
                   7934:     my %return_files;
1.1117    foxr     7935:     open (IN, '<'.LONCAPA::tempdir().$filename);
1.573     banghart 7936:     while (my $line_in = <IN>) {
1.574     banghart 7937:         chomp ($line_in);
                   7938:         my @paths_and_file = split (m!/!, $line_in);
                   7939:         my $file_part = pop (@paths_and_file);
                   7940:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 7941:         $path_part.='/';
                   7942:         my $path_and_file = $path_part.$file_part;
                   7943:         if ($path_part eq $path) {
                   7944:             $return_files{$file_part}= 'selected';
                   7945:         }
                   7946:     }
1.574     banghart 7947:     close (IN);
                   7948:     return (\%return_files);
1.572     banghart 7949: }
                   7950: 
                   7951: # called in portfolio select mode, to show files selected NOT in current directory
                   7952: sub files_not_in_path {
                   7953:     my ($user, $path) = @_;
                   7954:     my $filename = $user."savedfiles";
                   7955:     my @return_files;
                   7956:     my $path_part;
1.1117    foxr     7957:     open(IN, '<'.LONCAPA::.$filename);
1.800     albertel 7958:     while (my $line = <IN>) {
1.572     banghart 7959:         #ok, I know it's clunky, but I want it to work
1.800     albertel 7960:         my @paths_and_file = split(m|/|, $line);
                   7961:         my $file_part = pop(@paths_and_file);
                   7962:         chomp($file_part);
                   7963:         my $path_part = join('/', @paths_and_file);
1.572     banghart 7964:         $path_part .= '/';
                   7965:         my $path_and_file = $path_part.$file_part;
                   7966:         if ($path_part ne $path) {
1.800     albertel 7967:             push(@return_files, ($path_and_file));
1.572     banghart 7968:         }
                   7969:     }
1.800     albertel 7970:     close(OUT);
1.574     banghart 7971:     return (@return_files);
1.572     banghart 7972: }
                   7973: 
1.745     raeburn  7974: #----------------------------------------------Get portfolio file permissions
1.629     banghart 7975: 
1.745     raeburn  7976: sub get_portfile_permissions {
                   7977:     my ($domain,$user) = @_;
1.613     albertel 7978:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7979:     my ($tmp)=keys(%current_permissions);
                   7980:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  7981:     return \%current_permissions;
                   7982: }
                   7983: 
                   7984: #---------------------------------------------Get portfolio file access controls
                   7985: 
1.749     raeburn  7986: sub get_access_controls {
1.745     raeburn  7987:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 7988:     my %access;
                   7989:     my $real_file = $file;
                   7990:     $file =~ s/\.meta$//;
1.745     raeburn  7991:     if (defined($file)) {
1.749     raeburn  7992:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   7993:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 7994:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  7995:             }
                   7996:         }
1.745     raeburn  7997:     } else {
1.749     raeburn  7998:         foreach my $key (keys(%{$current_permissions})) {
                   7999:             if ($key =~ /\0accesscontrol$/) {
                   8000:                 if (defined($group)) {
                   8001:                     if ($key !~ m-^\Q$group\E/-) {
                   8002:                         next;
                   8003:                     }
                   8004:                 }
                   8005:                 my ($fullpath) = split(/\0/,$key);
                   8006:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   8007:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   8008:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   8009:                     }
                   8010:                 }
                   8011:             }
                   8012:         }
                   8013:     }
                   8014:     return %access;
                   8015: }
                   8016: 
                   8017: sub modify_access_controls {
                   8018:     my ($file_name,$changes,$domain,$user)=@_;
                   8019:     my ($outcome,$deloutcome);
                   8020:     my %store_permissions;
                   8021:     my %new_values;
                   8022:     my %new_control;
                   8023:     my %translation;
                   8024:     my @deletions = ();
                   8025:     my $now = time;
                   8026:     if (exists($$changes{'activate'})) {
                   8027:         if (ref($$changes{'activate'}) eq 'HASH') {
                   8028:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   8029:             my $numnew = scalar(@newitems);
                   8030:             for (my $i=0; $i<$numnew; $i++) {
                   8031:                 my $newkey = $newitems[$i];
                   8032:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  8033:                 if ($newkey =~ /^\d+:/) { 
                   8034:                     $newkey =~ s/^(\d+)/$newid/;
                   8035:                     $translation{$1} = $newid;
                   8036:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   8037:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   8038:                     $translation{$1} = $newid;
                   8039:                 }
1.749     raeburn  8040:                 $new_values{$file_name."\0".$newkey} = 
                   8041:                                           $$changes{'activate'}{$newitems[$i]};
                   8042:                 $new_control{$newkey} = $now;
                   8043:             }
                   8044:         }
                   8045:     }
                   8046:     my %todelete;
                   8047:     my %changed_items;
                   8048:     foreach my $action ('delete','update') {
                   8049:         if (exists($$changes{$action})) {
                   8050:             if (ref($$changes{$action}) eq 'HASH') {
                   8051:                 foreach my $key (keys(%{$$changes{$action}})) {
                   8052:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   8053:                     if ($action eq 'delete') { 
                   8054:                         $todelete{$itemnum} = 1;
                   8055:                     } else {
                   8056:                         $changed_items{$itemnum} = $key;
                   8057:                     }
                   8058:                 }
1.745     raeburn  8059:             }
                   8060:         }
1.749     raeburn  8061:     }
                   8062:     # get lock on access controls for file.
                   8063:     my $lockhash = {
                   8064:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   8065:                                                        ':'.$env{'user.domain'},
                   8066:                    }; 
                   8067:     my $tries = 0;
                   8068:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   8069:    
                   8070:     while (($gotlock ne 'ok') && $tries <3) {
                   8071:         $tries ++;
                   8072:         sleep 1;
                   8073:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   8074:     }
                   8075:     if ($gotlock eq 'ok') {
                   8076:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   8077:         my ($tmp)=keys(%curr_permissions);
                   8078:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   8079:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   8080:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   8081:             if (ref($curr_controls) eq 'HASH') {
                   8082:                 foreach my $control_item (keys(%{$curr_controls})) {
                   8083:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   8084:                     if (defined($todelete{$itemnum})) {
                   8085:                         push(@deletions,$file_name."\0".$control_item);
                   8086:                     } else {
                   8087:                         if (defined($changed_items{$itemnum})) {
                   8088:                             $new_control{$changed_items{$itemnum}} = $now;
                   8089:                             push(@deletions,$file_name."\0".$control_item);
                   8090:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   8091:                         } else {
                   8092:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   8093:                         }
                   8094:                     }
1.745     raeburn  8095:                 }
                   8096:             }
                   8097:         }
1.970     raeburn  8098:         my ($group);
                   8099:         if (&is_course($domain,$user)) {
                   8100:             ($group,my $file) = split(/\//,$file_name,2);
                   8101:         }
1.749     raeburn  8102:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   8103:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   8104:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   8105:         #  remove lock
                   8106:         my @del_lock = ($file_name."\0".'locked_access_records');
                   8107:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  8108:         my $sqlresult =
1.970     raeburn  8109:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  8110:                                     $group);
1.749     raeburn  8111:     } else {
                   8112:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  8113:     }
1.749     raeburn  8114:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  8115: }
                   8116: 
1.827     raeburn  8117: sub make_public_indefinitely {
                   8118:     my ($requrl) = @_;
                   8119:     my $now = time;
                   8120:     my $action = 'activate';
                   8121:     my $aclnum = 0;
                   8122:     if (&is_portfolio_url($requrl)) {
                   8123:         my (undef,$udom,$unum,$file_name,$group) =
                   8124:             &parse_portfolio_url($requrl);
                   8125:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   8126:         my %access_controls = &get_access_controls($current_perms,
                   8127:                                                    $group,$file_name);
                   8128:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   8129:             my ($num,$scope,$end,$start) = 
                   8130:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   8131:             if ($scope eq 'public') {
                   8132:                 if ($start <= $now && $end == 0) {
                   8133:                     $action = 'none';
                   8134:                 } else {
                   8135:                     $action = 'update';
                   8136:                     $aclnum = $num;
                   8137:                 }
                   8138:                 last;
                   8139:             }
                   8140:         }
                   8141:         if ($action eq 'none') {
                   8142:              return 'ok';
                   8143:         } else {
                   8144:             my %changes;
                   8145:             my $newend = 0;
                   8146:             my $newstart = $now;
                   8147:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   8148:             $changes{$action}{$newkey} = {
                   8149:                 type => 'public',
                   8150:                 time => {
                   8151:                     start => $newstart,
                   8152:                     end   => $newend,
                   8153:                 },
                   8154:             };
                   8155:             my ($outcome,$deloutcome,$new_values,$translation) =
                   8156:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   8157:             return $outcome;
                   8158:         }
                   8159:     } else {
                   8160:         return 'invalid';
                   8161:     }
                   8162: }
                   8163: 
1.745     raeburn  8164: #------------------------------------------------------Get Marked as Read Only
                   8165: 
                   8166: sub get_marked_as_readonly {
                   8167:     my ($domain,$user,$what,$group) = @_;
                   8168:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 8169:     my @readonly_files;
1.629     banghart 8170:     my $cmp1=$what;
                   8171:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  8172:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   8173:         if (defined($group)) {
                   8174:             if ($file_name !~ m-^\Q$group\E/-) {
                   8175:                 next;
                   8176:             }
                   8177:         }
1.561     banghart 8178:         if (ref($value) eq "ARRAY"){
                   8179:             foreach my $stored_what (@{$value}) {
1.629     banghart 8180:                 my $cmp2=$stored_what;
1.759     albertel 8181:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  8182:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  8183:                 }
1.629     banghart 8184:                 if ($cmp1 eq $cmp2) {
1.561     banghart 8185:                     push(@readonly_files, $file_name);
1.745     raeburn  8186:                     last;
1.563     banghart 8187:                 } elsif (!defined($what)) {
                   8188:                     push(@readonly_files, $file_name);
1.745     raeburn  8189:                     last;
1.561     banghart 8190:                 }
                   8191:             }
1.745     raeburn  8192:         }
1.561     banghart 8193:     }
                   8194:     return @readonly_files;
                   8195: }
1.577     banghart 8196: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 8197: 
1.577     banghart 8198: sub get_marked_as_readonly_hash {
1.745     raeburn  8199:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 8200:     my %readonly_files;
1.745     raeburn  8201:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   8202:         if (defined($group)) {
                   8203:             if ($file_name !~ m-^\Q$group\E/-) {
                   8204:                 next;
                   8205:             }
                   8206:         }
1.577     banghart 8207:         if (ref($value) eq "ARRAY"){
                   8208:             foreach my $stored_what (@{$value}) {
1.745     raeburn  8209:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 8210:                     foreach my $lock_descriptor(@{$stored_what}) {
                   8211:                         if ($lock_descriptor eq 'graded') {
                   8212:                             $readonly_files{$file_name} = 'graded';
                   8213:                         } elsif ($lock_descriptor eq 'handback') {
                   8214:                             $readonly_files{$file_name} = 'handback';
                   8215:                         } else {
                   8216:                             if (!exists($readonly_files{$file_name})) {
                   8217:                                 $readonly_files{$file_name} = 'locked';
                   8218:                             }
                   8219:                         }
1.745     raeburn  8220:                     }
1.750     banghart 8221:                 } 
1.577     banghart 8222:             }
                   8223:         } 
                   8224:     }
                   8225:     return %readonly_files;
                   8226: }
1.559     banghart 8227: # ------------------------------------------------------------ Unmark as Read Only
                   8228: 
                   8229: sub unmark_as_readonly {
1.629     banghart 8230:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   8231:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  8232:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 8233:     $file_name = &declutter_portfile($file_name);
1.634     albertel 8234:     my $symb_crs = $what;
                   8235:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  8236:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 8237:     my ($tmp)=keys(%current_permissions);
                   8238:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  8239:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 8240:     foreach my $file (@readonly_files) {
1.759     albertel 8241: 	my $clean_file = &declutter_portfile($file);
                   8242: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 8243: 	my $current_locks = $current_permissions{$file};
1.563     banghart 8244:         my @new_locks;
                   8245:         my @del_keys;
                   8246:         if (ref($current_locks) eq "ARRAY"){
                   8247:             foreach my $locker (@{$current_locks}) {
1.632     albertel 8248:                 my $compare=$locker;
1.749     raeburn  8249:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  8250:                     $compare=join('',@{$locker});
1.746     raeburn  8251:                     if ($compare ne $symb_crs) {
                   8252:                         push(@new_locks, $locker);
                   8253:                     }
1.563     banghart 8254:                 }
                   8255:             }
1.650     albertel 8256:             if (scalar(@new_locks) > 0) {
1.563     banghart 8257:                 $current_permissions{$file} = \@new_locks;
                   8258:             } else {
                   8259:                 push(@del_keys, $file);
1.613     albertel 8260:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 8261:                 delete($current_permissions{$file});
1.563     banghart 8262:             }
                   8263:         }
1.561     banghart 8264:     }
1.613     albertel 8265:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 8266:     return;
                   8267: }
1.512     banghart 8268: 
1.17      www      8269: # ------------------------------------------------------------ Directory lister
                   8270: 
                   8271: sub dirlist {
1.955     raeburn  8272:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      8273:     $uri=~s/^\///;
                   8274:     $uri=~s/\/$//;
1.253     stredwic 8275:     my ($udom, $uname);
1.955     raeburn  8276:     if ($getuserdir) {
1.253     stredwic 8277:         $udom = $userdomain;
                   8278:         $uname = $username;
1.955     raeburn  8279:     } else {
                   8280:         (undef,$udom,$uname)=split(/\//,$uri);
                   8281:         if(defined($userdomain)) {
                   8282:             $udom = $userdomain;
                   8283:         }
                   8284:         if(defined($username)) {
                   8285:             $uname = $username;
                   8286:         }
1.253     stredwic 8287:     }
1.955     raeburn  8288:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 8289: 
1.955     raeburn  8290:     $dirRoot = $perlvar{'lonDocRoot'};
                   8291:     if (defined($getpropath)) {
                   8292:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 8293:         $dirRoot =~ s/\/$//;
1.955     raeburn  8294:     } elsif (defined($getuserdir)) {
                   8295:         my $subdir=$uname.'__';
                   8296:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   8297:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   8298:                    ."/$udom/$subdir/$uname";
                   8299:     } elsif (defined($alternateRoot)) {
                   8300:         $dirRoot = $alternateRoot;
1.751     banghart 8301:     }
1.253     stredwic 8302: 
                   8303:     if($udom) {
                   8304:         if($uname) {
1.1135    raeburn  8305:             my $uhome = &homeserver($uname,$udom);
1.1136    raeburn  8306:             if ($uhome eq 'no_host') {
                   8307:                 return ([],'no_host');
                   8308:             }
1.955     raeburn  8309:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  8310:                               .$getuserdir.':'.&escape($dirRoot)
1.1135    raeburn  8311:                               .':'.&escape($uname).':'.&escape($udom),$uhome);
1.955     raeburn  8312:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  8313:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,$uhome);
1.955     raeburn  8314:             } else {
                   8315:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8316:             }
1.605     matthew  8317:             if ($listing eq 'unknown_cmd') {
1.1135    raeburn  8318:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,$uhome);
1.605     matthew  8319:                 @listing_results = split(/:/,$listing);
                   8320:             } else {
                   8321:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8322:             }
1.1135    raeburn  8323:             if (($listing eq 'no_such_host') || ($listing eq 'con_lost') || 
1.1136    raeburn  8324:                 ($listing eq 'rejected') || ($listing eq 'refused') ||
                   8325:                 ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   8326:                 return ([],$listing);
                   8327:             } else {
                   8328:                 return (\@listing_results);
1.1135    raeburn  8329:             }
1.955     raeburn  8330:         } elsif(!$alternateRoot) {
1.1136    raeburn  8331:             my (%allusers,%listerror);
1.841     albertel 8332: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  8333:  	    foreach my $tryserver (keys(%servers)) {
                   8334:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   8335:                                   &escape($udom),$tryserver);
                   8336:                 if ($listing eq 'unknown_cmd') {
                   8337: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   8338: 				      $udom, $tryserver);
                   8339:                 } else {
                   8340:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   8341:                 }
1.841     albertel 8342: 		if ($listing eq 'unknown_cmd') {
                   8343: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   8344: 				      $udom, $tryserver);
                   8345: 		    @listing_results = split(/:/,$listing);
                   8346: 		} else {
                   8347: 		    @listing_results =
                   8348: 			map { &unescape($_); } split(/:/,$listing);
                   8349: 		}
1.1136    raeburn  8350:                 if (($listing eq 'no_such_host') || ($listing eq 'con_lost') ||
                   8351:                     ($listing eq 'rejected') || ($listing eq 'refused') ||
                   8352:                     ($listing eq 'no_such_dir') || ($listing eq 'empty')) {
                   8353:                     $listerror{$tryserver} = $listing;
                   8354:                 } else {
1.841     albertel 8355: 		    foreach my $line (@listing_results) {
                   8356: 			my ($entry) = split(/&/,$line,2);
                   8357: 			$allusers{$entry} = 1;
                   8358: 		    }
                   8359: 		}
1.253     stredwic 8360:             }
1.1136    raeburn  8361:             my @alluserslist=();
1.800     albertel 8362:             foreach my $user (sort(keys(%allusers))) {
1.1136    raeburn  8363:                 push(@alluserslist,$user.'&user');
1.253     stredwic 8364:             }
1.1136    raeburn  8365:             return (\@alluserslist);
1.253     stredwic 8366:         } else {
1.1136    raeburn  8367:             return ([],'missing username');
1.253     stredwic 8368:         }
1.955     raeburn  8369:     } elsif(!defined($getpropath)) {
1.1136    raeburn  8370:         my $path = $perlvar{'lonDocRoot'}.'/res/'; 
                   8371:         my @all_domains = map { $path.$_.'/&domain'; } (sort(&all_domains()));
                   8372:         return (\@all_domains);
1.955     raeburn  8373:     } else {
1.1136    raeburn  8374:         return ([],'missing domain');
1.275     stredwic 8375:     }
                   8376: }
                   8377: 
                   8378: # --------------------------------------------- GetFileTimestamp
                   8379: # This function utilizes dirlist and returns the date stamp for
                   8380: # when it was last modified.  It will also return an error of -1
                   8381: # if an error occurs
                   8382: 
                   8383: sub GetFileTimestamp {
1.955     raeburn  8384:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 8385:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   8386:     $studentName   = &LONCAPA::clean_username($studentName);
1.1136    raeburn  8387:     my ($fileref,$error) = &dirlist($filename,$studentDomain,$studentName,
                   8388:                                     undef,$getuserdir);
                   8389:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   8390:         return -1;
                   8391:     }
                   8392:     if (ref($fileref) eq 'ARRAY') {
                   8393:         my @stats = split('&',$fileref->[0]);
1.375     matthew  8394:         # @stats contains first the filename, then the stat output
                   8395:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 8396:     } else {
                   8397:         return -1;
1.253     stredwic 8398:     }
1.26      www      8399: }
                   8400: 
1.712     albertel 8401: sub stat_file {
                   8402:     my ($uri) = @_;
1.787     albertel 8403:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 8404: 
1.955     raeburn  8405:     my ($udom,$uname,$file);
1.712     albertel 8406:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   8407: 	($udom,$uname,$file) =
1.811     albertel 8408: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 8409: 	$file = 'userfiles/'.$file;
                   8410:     }
                   8411:     if ($uri =~ m-^/res/-) {
                   8412: 	($udom,$uname) = 
1.807     albertel 8413: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 8414: 	$file = $uri;
                   8415:     }
                   8416: 
                   8417:     if (!$udom || !$uname || !$file) {
                   8418: 	# unable to handle the uri
                   8419: 	return ();
                   8420:     }
1.956     raeburn  8421:     my $getpropath;
                   8422:     if ($file =~ /^userfiles\//) {
                   8423:         $getpropath = 1;
                   8424:     }
1.1136    raeburn  8425:     my ($listref,$error) = &dirlist($file,$udom,$uname,$getpropath);
                   8426:     if (($error eq 'empty') || ($error eq 'no_such_dir')) {
                   8427:         return ();
                   8428:     } else {
                   8429:         if (ref($listref) eq 'ARRAY') {
                   8430:             my @stats = split('&',$listref->[0]);
                   8431: 	    shift(@stats); #filename is first
                   8432: 	    return @stats;
                   8433:         }
1.712     albertel 8434:     }
                   8435:     return ();
                   8436: }
                   8437: 
1.26      www      8438: # -------------------------------------------------------- Value of a Condition
                   8439: 
1.713     albertel 8440: # gets the value of a specific preevaluated condition
                   8441: #    stored in the string  $env{user.state.<cid>}
                   8442: # or looks up a condition reference in the bighash and if if hasn't
                   8443: # already been evaluated recurses into docondval to get the value of
                   8444: # the condition, then memoizing it to 
                   8445: #   $env{user.state.<cid>.<condition>}
1.40      www      8446: sub directcondval {
                   8447:     my $number=shift;
1.620     albertel 8448:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 8449: 	&Apache::lonuserstate::evalstate();
                   8450:     }
1.713     albertel 8451:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   8452: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   8453:     } elsif ($number =~ /^_/) {
                   8454: 	my $sub_condition;
                   8455: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   8456: 		&GDBM_READER(),0640)) {
                   8457: 	    $sub_condition=$bighash{'conditions'.$number};
                   8458: 	    untie(%bighash);
                   8459: 	}
                   8460: 	my $value = &docondval($sub_condition);
1.949     raeburn  8461: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 8462: 	return $value;
                   8463:     }
1.620     albertel 8464:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   8465:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      8466:     } else {
                   8467:        return 2;
                   8468:     }
                   8469: }
                   8470: 
1.713     albertel 8471: # get the collection of conditions for this resource
1.26      www      8472: sub condval {
                   8473:     my $condidx=shift;
1.54      www      8474:     my $allpathcond='';
1.713     albertel 8475:     foreach my $cond (split(/\|/,$condidx)) {
                   8476: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   8477: 	    $allpathcond.=
                   8478: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   8479: 	}
1.191     harris41 8480:     }
1.54      www      8481:     $allpathcond=~s/\|$//;
1.713     albertel 8482:     return &docondval($allpathcond);
                   8483: }
                   8484: 
                   8485: #evaluates an expression of conditions
                   8486: sub docondval {
                   8487:     my ($allpathcond) = @_;
                   8488:     my $result=0;
                   8489:     if ($env{'request.course.id'}
                   8490: 	&& defined($allpathcond)) {
                   8491: 	my $operand='|';
                   8492: 	my @stack;
                   8493: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   8494: 	    if ($chunk eq '(') {
                   8495: 		push @stack,($operand,$result);
                   8496: 	    } elsif ($chunk eq ')') {
                   8497: 		my $before=pop @stack;
                   8498: 		if (pop @stack eq '&') {
                   8499: 		    $result=$result>$before?$before:$result;
                   8500: 		} else {
                   8501: 		    $result=$result>$before?$result:$before;
                   8502: 		}
                   8503: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   8504: 		$operand=$chunk;
                   8505: 	    } else {
                   8506: 		my $new=directcondval($chunk);
                   8507: 		if ($operand eq '&') {
                   8508: 		    $result=$result>$new?$new:$result;
                   8509: 		} else {
                   8510: 		    $result=$result>$new?$result:$new;
                   8511: 		}
                   8512: 	    }
                   8513: 	}
1.26      www      8514:     }
                   8515:     return $result;
1.421     albertel 8516: }
                   8517: 
                   8518: # ---------------------------------------------------- Devalidate courseresdata
                   8519: 
                   8520: sub devalidatecourseresdata {
                   8521:     my ($coursenum,$coursedomain)=@_;
                   8522:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8523:     &devalidate_cache_new('courseres',$hashid);
1.28      www      8524: }
                   8525: 
1.763     www      8526: 
1.200     www      8527: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     8528: #
                   8529: #  Parameters:
                   8530: #      $coursenum    - Number of the course.
                   8531: #      $coursedomain - Domain at which the course was created.
                   8532: #  Returns:
                   8533: #     A hash of the course parameters along (I think) with timestamps
                   8534: #     and version info.
1.877     foxr     8535: 
1.624     albertel 8536: sub get_courseresdata {
                   8537:     my ($coursenum,$coursedomain)=@_;
1.200     www      8538:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   8539:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8540:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 8541:     my %dumpreply;
1.417     albertel 8542:     unless (defined($cached)) {
1.624     albertel 8543: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 8544: 	$result=\%dumpreply;
1.251     albertel 8545: 	my ($tmp) = keys(%dumpreply);
                   8546: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 8547: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 8548: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   8549: 	    return $tmp;
1.416     albertel 8550: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 8551: 	    $result=undef;
1.599     albertel 8552: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 8553: 	}
                   8554:     }
1.624     albertel 8555:     return $result;
                   8556: }
                   8557: 
1.633     albertel 8558: sub devalidateuserresdata {
                   8559:     my ($uname,$udom)=@_;
                   8560:     my $hashid="$udom:$uname";
                   8561:     &devalidate_cache_new('userres',$hashid);
                   8562: }
                   8563: 
1.624     albertel 8564: sub get_userresdata {
                   8565:     my ($uname,$udom)=@_;
                   8566:     #most student don\'t have any data set, check if there is some data
                   8567:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   8568: 
                   8569:     my $hashid="$udom:$uname";
                   8570:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   8571:     if (!defined($cached)) {
                   8572: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   8573: 	$result=\%resourcedata;
                   8574: 	&do_cache_new('userres',$hashid,$result,600);
                   8575:     }
                   8576:     my ($tmp)=keys(%$result);
                   8577:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   8578: 	return $result;
                   8579:     }
                   8580:     #error 2 occurs when the .db doesn't exist
                   8581:     if ($tmp!~/error: 2 /) {
1.672     albertel 8582: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 8583: 		 " Trying to get resource data for ".
                   8584: 		 $uname." at ".$udom.": ".
                   8585: 		 $tmp."</font>");
                   8586:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 8587: 	#&EXT_cache_set($udom,$uname);
                   8588: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 8589: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 8590:     }
                   8591:     return $tmp;
                   8592: }
1.879     foxr     8593: #----------------------------------------------- resdata - return resource data
                   8594: #  Purpose:
                   8595: #    Return resource data for either users or for a course.
                   8596: #  Parameters:
                   8597: #     $name      - Course/user name.
                   8598: #     $domain    - Name of the domain the user/course is registered on.
                   8599: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   8600: #     @which     - Array of names of resources desired.
                   8601: #  Returns:
                   8602: #     The value of the first reasource in @which that is found in the
                   8603: #     resource hash.
                   8604: #  Exceptional Conditions:
                   8605: #     If the $type passed in is not valid (not the string 'course' or 
                   8606: #     'user', an undefined  reference is returned.
                   8607: #     If none of the resources are found, an undef is returned
1.624     albertel 8608: sub resdata {
                   8609:     my ($name,$domain,$type,@which)=@_;
                   8610:     my $result;
                   8611:     if ($type eq 'course') {
                   8612: 	$result=&get_courseresdata($name,$domain);
                   8613:     } elsif ($type eq 'user') {
                   8614: 	$result=&get_userresdata($name,$domain);
                   8615:     }
                   8616:     if (!ref($result)) { return $result; }    
1.251     albertel 8617:     foreach my $item (@which) {
1.927     albertel 8618: 	if (defined($result->{$item->[0]})) {
                   8619: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 8620: 	}
1.250     albertel 8621:     }
1.291     albertel 8622:     return undef;
1.200     www      8623: }
                   8624: 
1.379     matthew  8625: #
                   8626: # EXT resource caching routines
                   8627: #
                   8628: 
                   8629: sub clear_EXT_cache_status {
1.383     albertel 8630:     &delenv('cache.EXT.');
1.379     matthew  8631: }
                   8632: 
                   8633: sub EXT_cache_status {
                   8634:     my ($target_domain,$target_user) = @_;
1.383     albertel 8635:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 8636:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  8637:         # We know already the user has no data
                   8638:         return 1;
                   8639:     } else {
                   8640:         return 0;
                   8641:     }
                   8642: }
                   8643: 
                   8644: sub EXT_cache_set {
                   8645:     my ($target_domain,$target_user) = @_;
1.383     albertel 8646:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  8647:     #&appenv({$cachename => time});
1.379     matthew  8648: }
                   8649: 
1.28      www      8650: # --------------------------------------------------------- Value of a Variable
1.58      www      8651: sub EXT {
1.715     albertel 8652: 
1.395     albertel 8653:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      8654:     unless ($varname) { return ''; }
1.218     albertel 8655:     #get real user name/domain, courseid and symb
                   8656:     my $courseid;
1.359     albertel 8657:     my $publicuser;
1.427     www      8658:     if ($symbparm) {
                   8659: 	$symbparm=&get_symb_from_alias($symbparm);
                   8660:     }
1.218     albertel 8661:     if (!($uname && $udom)) {
1.790     albertel 8662:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 8663:       if (!$symbparm) {	$symbparm=$cursymb; }
                   8664:     } else {
1.620     albertel 8665: 	$courseid=$env{'request.course.id'};
1.218     albertel 8666:     }
1.48      www      8667:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   8668:     my $rest;
1.320     albertel 8669:     if (defined($therest[0])) {
1.48      www      8670:        $rest=join('.',@therest);
                   8671:     } else {
                   8672:        $rest='';
                   8673:     }
1.320     albertel 8674: 
1.57      www      8675:     my $qualifierrest=$qualifier;
                   8676:     if ($rest) { $qualifierrest.='.'.$rest; }
                   8677:     my $spacequalifierrest=$space;
                   8678:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      8679:     if ($realm eq 'user') {
1.48      www      8680: # --------------------------------------------------------------- user.resource
                   8681: 	if ($space eq 'resource') {
1.651     albertel 8682: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   8683: 		  || defined($Apache::lonhomework::parsing_a_task))
                   8684: 		 &&
1.744     albertel 8685: 		 ($symbparm eq &symbread()) ) {	
                   8686: 		# if we are in the middle of processing the resource the
                   8687: 		# get the value we are planning on committing
                   8688:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   8689:                     return $Apache::lonhomework::results{$qualifierrest};
                   8690:                 } else {
                   8691:                     return $Apache::lonhomework::history{$qualifierrest};
                   8692:                 }
1.335     albertel 8693: 	    } else {
1.359     albertel 8694: 		my %restored;
1.620     albertel 8695: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 8696: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   8697: 		} else {
                   8698: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   8699: 		}
1.335     albertel 8700: 		return $restored{$qualifierrest};
                   8701: 	    }
1.48      www      8702: # ----------------------------------------------------------------- user.access
                   8703:         } elsif ($space eq 'access') {
1.218     albertel 8704: 	    # FIXME - not supporting calls for a specific user
1.48      www      8705:             return &allowed($qualifier,$rest);
                   8706: # ------------------------------------------ user.preferences, user.environment
                   8707:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 8708: 	    if (($uname eq $env{'user.name'}) &&
                   8709: 		($udom eq $env{'user.domain'})) {
                   8710: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 8711: 	    } else {
1.359     albertel 8712: 		my %returnhash;
                   8713: 		if (!$publicuser) {
                   8714: 		    %returnhash=&userenvironment($udom,$uname,
                   8715: 						 $qualifierrest);
                   8716: 		}
1.218     albertel 8717: 		return $returnhash{$qualifierrest};
                   8718: 	    }
1.48      www      8719: # ----------------------------------------------------------------- user.course
                   8720:         } elsif ($space eq 'course') {
1.218     albertel 8721: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8722:             return $env{join('.',('request.course',$qualifier))};
1.48      www      8723: # ------------------------------------------------------------------- user.role
                   8724:         } elsif ($space eq 'role') {
1.218     albertel 8725: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8726:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      8727:             if ($qualifier eq 'value') {
                   8728: 		return $role;
                   8729:             } elsif ($qualifier eq 'extent') {
                   8730:                 return $where;
                   8731:             }
                   8732: # ----------------------------------------------------------------- user.domain
                   8733:         } elsif ($space eq 'domain') {
1.218     albertel 8734:             return $udom;
1.48      www      8735: # ------------------------------------------------------------------- user.name
                   8736:         } elsif ($space eq 'name') {
1.218     albertel 8737:             return $uname;
1.48      www      8738: # ---------------------------------------------------- Any other user namespace
1.29      www      8739:         } else {
1.359     albertel 8740: 	    my %reply;
                   8741: 	    if (!$publicuser) {
                   8742: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   8743: 	    }
                   8744: 	    return $reply{$qualifierrest};
1.48      www      8745:         }
1.236     www      8746:     } elsif ($realm eq 'query') {
                   8747: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 8748:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   8749: 						[$spacequalifierrest]);
1.620     albertel 8750: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      8751:    } elsif ($realm eq 'request') {
1.48      www      8752: # ------------------------------------------------------------- request.browser
                   8753:         if ($space eq 'browser') {
1.1145    bisitz   8754:             return $env{'browser.'.$qualifier};
1.57      www      8755: # ------------------------------------------------------------ request.filename
                   8756:         } else {
1.620     albertel 8757:             return $env{'request.'.$spacequalifierrest};
1.29      www      8758:         }
1.28      www      8759:     } elsif ($realm eq 'course') {
1.48      www      8760: # ---------------------------------------------------------- course.description
1.620     albertel 8761:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      8762:     } elsif ($realm eq 'resource') {
1.165     www      8763: 
1.620     albertel 8764: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 8765: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   8766: 	}
1.693     albertel 8767: 
                   8768: 	if ($space eq 'title') {
                   8769: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   8770: 	    return &gettitle($symbparm);
                   8771: 	}
                   8772: 	
                   8773: 	if ($space eq 'map') {
                   8774: 	    my ($map) = &decode_symb($symbparm);
                   8775: 	    return &symbread($map);
                   8776: 	}
1.905     albertel 8777: 	if ($space eq 'filename') {
                   8778: 	    if ($symbparm) {
                   8779: 		return &clutter((&decode_symb($symbparm))[2]);
                   8780: 	    }
                   8781: 	    return &hreflocation('',$env{'request.filename'});
                   8782: 	}
1.693     albertel 8783: 
                   8784: 	my ($section, $group, @groups);
1.593     albertel 8785: 	my ($courselevelm,$courselevel);
1.539     albertel 8786: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8787: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      8788: 
1.218     albertel 8789: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      8790: 
1.60      www      8791: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 8792: 	    my $symbp=$symbparm;
1.735     albertel 8793: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 8794: 
                   8795: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   8796: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   8797: 
1.620     albertel 8798: 	    if (($env{'user.name'} eq $uname) &&
                   8799: 		($env{'user.domain'} eq $udom)) {
                   8800: 		$section=$env{'request.course.sec'};
1.733     raeburn  8801:                 @groups = split(/:/,$env{'request.course.groups'});  
                   8802:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 8803: 	    } else {
1.539     albertel 8804: 		if (! defined($usection)) {
1.551     albertel 8805: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 8806: 		} else {
                   8807: 		    $section = $usection;
                   8808: 		}
1.733     raeburn  8809:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 8810: 	    }
                   8811: 
                   8812: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   8813: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   8814: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   8815: 
1.593     albertel 8816: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 8817: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 8818: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      8819: 
1.60      www      8820: # ----------------------------------------------------------- first, check user
1.624     albertel 8821: 
                   8822: 	    my $userreply=&resdata($uname,$udom,'user',
1.927     albertel 8823: 				       ([$courselevelr,'resource'],
                   8824: 					[$courselevelm,'map'     ],
                   8825: 					[$courselevel, 'course'  ]));
1.931     albertel 8826: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      8827: 
1.594     albertel 8828: # ------------------------------------------------ second, check some of course
1.684     raeburn  8829:             my $coursereply;
1.691     raeburn  8830:             if (@groups > 0) {
                   8831:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   8832:                                        $mapparm,$spacequalifierrest);
1.927     albertel 8833:                 if (defined($coursereply)) { return &get_reply($coursereply); }
1.684     raeburn  8834:             }
1.96      www      8835: 
1.684     raeburn  8836: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 8837: 				  $env{'course.'.$courseid.'.domain'},
                   8838: 				  'course',
                   8839: 				  ([$seclevelr,   'resource'],
                   8840: 				   [$seclevelm,   'map'     ],
                   8841: 				   [$seclevel,    'course'  ],
                   8842: 				   [$courselevelr,'resource']));
                   8843: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      8844: 
1.60      www      8845: # ------------------------------------------------------ third, check map parms
1.218     albertel 8846: 	    my %parmhash=();
                   8847: 	    my $thisparm='';
                   8848: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 8849: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 8850: 		    &GDBM_READER(),0640)) {
1.218     albertel 8851: 		$thisparm=$parmhash{$symbparm};
                   8852: 		untie(%parmhash);
                   8853: 	    }
1.927     albertel 8854: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 8855: 	}
1.594     albertel 8856: # ------------------------------------------ fourth, look in resource metadata
1.71      www      8857: 
1.218     albertel 8858: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 8859: 	my $filename;
                   8860: 	if (!$symbparm) { $symbparm=&symbread(); }
                   8861: 	if ($symbparm) {
1.409     www      8862: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 8863: 	} else {
1.620     albertel 8864: 	    $filename=$env{'request.filename'};
1.282     albertel 8865: 	}
                   8866: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.927     albertel 8867: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282     albertel 8868: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927     albertel 8869: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      8870: 
1.927     albertel 8871: # ---------------------------------------------- fourth, look in rest of course
1.593     albertel 8872: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8873: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 8874: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   8875: 				     $env{'course.'.$courseid.'.domain'},
                   8876: 				     'course',
1.927     albertel 8877: 				     ([$courselevelm,'map'   ],
                   8878: 				      [$courselevel, 'course']));
                   8879: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 8880: 	}
1.145     www      8881: # ------------------------------------------------------------------ Cascade up
1.218     albertel 8882: 	unless ($space eq '0') {
1.336     albertel 8883: 	    my @parts=split(/_/,$space);
                   8884: 	    my $id=pop(@parts);
                   8885: 	    my $part=join('_',@parts);
                   8886: 	    if ($part eq '') { $part='0'; }
1.927     albertel 8887: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 8888: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  8889: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 8890: 	}
1.395     albertel 8891: 	if ($recurse) { return undef; }
                   8892: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 8893: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      8894: # ---------------------------------------------------- Any other user namespace
                   8895:     } elsif ($realm eq 'environment') {
                   8896: # ----------------------------------------------------------------- environment
1.620     albertel 8897: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   8898: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 8899: 	} else {
1.770     albertel 8900: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   8901: 		return '';
                   8902: 	    }
1.219     albertel 8903: 	    my %returnhash=&userenvironment($udom,$uname,
                   8904: 					    $spacequalifierrest);
                   8905: 	    return $returnhash{$spacequalifierrest};
                   8906: 	}
1.28      www      8907:     } elsif ($realm eq 'system') {
1.48      www      8908: # ----------------------------------------------------------------- system.time
                   8909: 	if ($space eq 'time') {
                   8910: 	    return time;
                   8911:         }
1.696     albertel 8912:     } elsif ($realm eq 'server') {
                   8913: # ----------------------------------------------------------------- system.time
                   8914: 	if ($space eq 'name') {
                   8915: 	    return $ENV{'SERVER_NAME'};
                   8916:         }
1.28      www      8917:     }
1.48      www      8918:     return '';
1.61      www      8919: }
                   8920: 
1.927     albertel 8921: sub get_reply {
                   8922:     my ($reply_value) = @_;
1.940     raeburn  8923:     if (ref($reply_value) eq 'ARRAY') {
                   8924:         if (wantarray) {
                   8925: 	    return @$reply_value;
                   8926:         }
                   8927:         return $reply_value->[0];
                   8928:     } else {
                   8929:         return $reply_value;
1.927     albertel 8930:     }
                   8931: }
                   8932: 
1.691     raeburn  8933: sub check_group_parms {
                   8934:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   8935:     my @groupitems = ();
                   8936:     my $resultitem;
1.927     albertel 8937:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691     raeburn  8938:     foreach my $group (@{$groups}) {
                   8939:         foreach my $level (@levels) {
1.927     albertel 8940:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   8941:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  8942:         }
                   8943:     }
                   8944:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   8945:                             $env{'course.'.$courseid.'.domain'},
                   8946:                                      'course',@groupitems);
                   8947:     return $coursereply;
                   8948: }
                   8949: 
                   8950: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  8951:     my ($courseid,@groups) = @_;
                   8952:     @groups = sort(@groups);
1.691     raeburn  8953:     return @groups;
                   8954: }
                   8955: 
1.395     albertel 8956: sub packages_tab_default {
                   8957:     my ($uri,$varname)=@_;
                   8958:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 8959: 
                   8960:     my (@extension,@specifics,$do_default);
                   8961:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 8962: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 8963: 	if ($pack_type eq 'default') {
                   8964: 	    $do_default=1;
                   8965: 	} elsif ($pack_type eq 'extension') {
                   8966: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 8967: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 8968: 	    # only look at packages defaults for packages that this id is
1.738     albertel 8969: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   8970: 	}
                   8971:     }
                   8972:     # first look for a package that matches the requested part id
                   8973:     foreach my $package (@specifics) {
                   8974: 	my (undef,$pack_type,$pack_part)=@{$package};
                   8975: 	next if ($pack_part ne $part);
                   8976: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8977: 	    return $packagetab{"$pack_type&$name&default"};
                   8978: 	}
                   8979:     }
                   8980:     # look for any possible matching non extension_ package
                   8981:     foreach my $package (@specifics) {
                   8982: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 8983: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8984: 	    return $packagetab{"$pack_type&$name&default"};
                   8985: 	}
1.585     albertel 8986: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 8987: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   8988: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 8989: 	}
                   8990:     }
1.738     albertel 8991:     # look for any posible extension_ match
                   8992:     foreach my $package (@extension) {
                   8993: 	my ($package,$pack_type)=@{$package};
                   8994: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8995: 	    return $packagetab{"$pack_type&$name&default"};
                   8996: 	}
                   8997: 	if (defined($packagetab{$package."&$name&default"})) {
                   8998: 	    return $packagetab{$package."&$name&default"};
                   8999: 	}
                   9000:     }
                   9001:     # look for a global default setting
                   9002:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   9003: 	return $packagetab{"default&$name&default"};
                   9004:     }
1.395     albertel 9005:     return undef;
                   9006: }
                   9007: 
1.334     albertel 9008: sub add_prefix_and_part {
                   9009:     my ($prefix,$part)=@_;
                   9010:     my $keyroot;
                   9011:     if (defined($prefix) && $prefix !~ /^__/) {
                   9012: 	# prefix that has a part already
                   9013: 	$keyroot=$prefix;
                   9014:     } elsif (defined($prefix)) {
                   9015: 	# prefix that is missing a part
                   9016: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   9017:     } else {
                   9018: 	# no prefix at all
                   9019: 	if (defined($part)) { $keyroot='_'.$part; }
                   9020:     }
                   9021:     return $keyroot;
                   9022: }
                   9023: 
1.71      www      9024: # ---------------------------------------------------------------- Get metadata
                   9025: 
1.599     albertel 9026: my %metaentry;
1.1070    www      9027: my %importedpartids;
1.71      www      9028: sub metadata {
1.176     www      9029:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      9030:     $uri=&declutter($uri);
1.288     albertel 9031:     # if it is a non metadata possible uri return quickly
1.529     albertel 9032:     if (($uri eq '') || 
                   9033: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 9034: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1108    raeburn  9035:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 9036: 	return undef;
                   9037:     }
1.1140    www      9038:     if (($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) 
1.924     albertel 9039: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 9040: 	return undef;
1.288     albertel 9041:     }
1.73      www      9042:     my $filename=$uri;
                   9043:     $uri=~s/\.meta$//;
1.172     www      9044: #
                   9045: # Is the metadata already cached?
1.177     www      9046: # Look at timestamp of caching
1.172     www      9047: # Everything is cached by the main uri, libraries are never directly cached
                   9048: #
1.428     albertel 9049:     if (!defined($liburi)) {
1.599     albertel 9050: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 9051: 	if (defined($cached)) { return $result->{':'.$what}; }
                   9052:     }
                   9053:     {
1.1069    www      9054: # Imported parts would go here
1.1070    www      9055:         my %importedids=();
                   9056:         my @origfileimportpartids=();
1.1069    www      9057:         my $importedparts=0;
1.172     www      9058: #
                   9059: # Is this a recursive call for a library?
                   9060: #
1.599     albertel 9061: #	if (! exists($metacache{$uri})) {
                   9062: #	    $metacache{$uri}={};
                   9063: #	}
1.924     albertel 9064: 	my $cachetime = 60*60;
1.171     www      9065:         if ($liburi) {
                   9066: 	    $liburi=&declutter($liburi);
                   9067:             $filename=$liburi;
1.401     bowersj2 9068:         } else {
1.599     albertel 9069: 	    &devalidate_cache_new('meta',$uri);
                   9070: 	    undef(%metaentry);
1.401     bowersj2 9071: 	}
1.140     www      9072:         my %metathesekeys=();
1.73      www      9073:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 9074: 	my $metastring;
1.1140    www      9075: 	if ($uri =~ /^priv/ || $uri=~/home\/httpd\/html\/priv/) {
1.929     albertel 9076: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 9077: 	    $metastring = 
1.929     albertel 9078: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 9079: 					  ('grade_target' => 'meta'));
                   9080: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  9081: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   9082:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 9083: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 9084: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 9085: 	    $metastring=&getfile($file);
1.489     albertel 9086: 	}
1.208     albertel 9087:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      9088:         my $token;
1.140     www      9089:         undef %metathesekeys;
1.71      www      9090:         while ($token=$parser->get_token) {
1.339     albertel 9091: 	    if ($token->[0] eq 'S') {
                   9092: 		if (defined($token->[2]->{'package'})) {
1.172     www      9093: #
                   9094: # This is a package - get package info
                   9095: #
1.339     albertel 9096: 		    my $package=$token->[2]->{'package'};
                   9097: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9098: 		    if (defined($token->[2]->{'id'})) { 
                   9099: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   9100: 		    }
1.599     albertel 9101: 		    if ($metaentry{':packages'}) {
                   9102: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 9103: 		    } else {
1.599     albertel 9104: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 9105: 		    }
1.736     albertel 9106: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 9107: 			my $part=$keyroot;
                   9108: 			$part=~s/^\_//;
1.736     albertel 9109: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   9110: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   9111: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 9112: 			    # ignore package.tab specified default values
                   9113:                             # here &package_tab_default() will fetch those
                   9114: 			    if ($subp eq 'default') { next; }
1.736     albertel 9115: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 9116: 			    my $unikey;
                   9117: 			    if ($pack =~ /_0$/) {
                   9118: 				$unikey='parameter_0_'.$name;
                   9119: 				$part=0;
                   9120: 			    } else {
                   9121: 				$unikey='parameter'.$keyroot.'_'.$name;
                   9122: 			    }
1.339     albertel 9123: 			    if ($subp eq 'display') {
                   9124: 				$value.=' [Part: '.$part.']';
                   9125: 			    }
1.599     albertel 9126: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 9127: 			    $metathesekeys{$unikey}=1;
1.599     albertel 9128: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   9129: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 9130: 			    }
1.599     albertel 9131: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   9132: 				$metaentry{':'.$unikey}=
                   9133: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 9134: 			    }
1.339     albertel 9135: 			}
                   9136: 		    }
                   9137: 		} else {
1.172     www      9138: #
                   9139: # This is not a package - some other kind of start tag
1.339     albertel 9140: #
                   9141: 		    my $entry=$token->[1];
1.1068    www      9142: 		    my $unikey='';
1.175     www      9143: 
1.339     albertel 9144: 		    if ($entry eq 'import') {
1.175     www      9145: #
                   9146: # Importing a library here
1.339     albertel 9147: #
1.1067    www      9148:                         my $location=$parser->get_text('/import');
                   9149:                         my $dir=$filename;
                   9150:                         $dir=~s|[^/]*$||;
                   9151:                         $location=&filelocation($dir,$location);
1.1069    www      9152:                        
1.1068    www      9153:                         my $importmode=$token->[2]->{'importmode'};
                   9154:                         if ($importmode eq 'problem') {
1.1069    www      9155: # Import as problem/response
1.1068    www      9156:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9157:                         } elsif ($importmode eq 'part') {
                   9158: # Import as part(s)
1.1069    www      9159:                            $importedparts=1;
                   9160: # We need to get the original file and the imported file to get the part order correct
                   9161: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
                   9162: # Load and inspect original file
1.1070    www      9163:                            if ($#origfileimportpartids<0) {
                   9164:                               undef(%importedpartids);
                   9165:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   9166:                               my $origfile=&getfile($origfilelocation);
                   9167:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   9168:                            }
                   9169: 
1.1069    www      9170: # Load and inspect imported file
                   9171:                            my $impfile=&getfile($location);
                   9172:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   9173:                            if ($#impfilepartids>=0) {
                   9174: # This problem had parts
1.1070    www      9175:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      9176:                            } else {
                   9177: # Importing by turning a single problem into a problem part
                   9178: # It gets the import-tags ID as part-ID
                   9179:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      9180:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      9181:                            }
1.1068    www      9182:                         } else {
                   9183: # Normal import
                   9184:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9185:                            if (defined($token->[2]->{'id'})) {
                   9186:                               $unikey.='_'.$token->[2]->{'id'};
                   9187:                            }
1.1067    www      9188:                         }
                   9189: 
1.339     albertel 9190: 			if ($depthcount<20) {
1.736     albertel 9191: 			    my $metadata = 
                   9192: 				&metadata($uri,'keys', $location,$unikey,
                   9193: 					  $depthcount+1);
                   9194: 			    foreach my $meta (split(',',$metadata)) {
                   9195: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   9196: 				$metathesekeys{$meta}=1;
1.339     albertel 9197: 			    }
1.1068    www      9198: 			
                   9199:                         }
1.1067    www      9200: 		    } else {
                   9201: #
                   9202: # Not importing, some other kind of non-package, non-library start tag
                   9203: # 
                   9204:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   9205:                         if (defined($token->[2]->{'id'})) {
                   9206:                             $unikey.='_'.$token->[2]->{'id'};
                   9207:                         }
1.339     albertel 9208: 			if (defined($token->[2]->{'name'})) { 
                   9209: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   9210: 			}
                   9211: 			$metathesekeys{$unikey}=1;
1.736     albertel 9212: 			foreach my $param (@{$token->[3]}) {
                   9213: 			    $metaentry{':'.$unikey.'.'.$param} =
                   9214: 				$token->[2]->{$param};
1.339     albertel 9215: 			}
                   9216: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 9217: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 9218: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   9219: 		 # only ws inside the tag, and not in default, so use default
                   9220: 		 # as value
1.599     albertel 9221: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 9222: 			} elsif ( $internaltext =~ /\S/ ) {
                   9223: 		  # something interesting inside the tag
                   9224: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 9225: 			} else {
1.908     albertel 9226: 		  # no interesting values, don't set a default
1.339     albertel 9227: 			}
1.172     www      9228: # end of not-a-package not-a-library import
1.339     albertel 9229: 		    }
1.172     www      9230: # end of not-a-package start tag
1.339     albertel 9231: 		}
1.172     www      9232: # the next is the end of "start tag"
1.339     albertel 9233: 	    }
                   9234: 	}
1.483     albertel 9235: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 9236: 	$extension = lc($extension);
                   9237: 	if ($extension eq 'htm') { $extension='html'; }
                   9238: 
1.737     albertel 9239: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 9240: 	    #no specific packages #how's our extension
                   9241: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 9242: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 9243: 					 \%metathesekeys);
                   9244: 	}
1.883     albertel 9245: 
                   9246: 	if (!exists($metaentry{':packages'})
                   9247: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 9248: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 9249: 		#no specific packages well let's get default then
                   9250: 		if ($key!~/^default&/) { next; }
1.488     albertel 9251: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 9252: 					     \%metathesekeys);
                   9253: 	    }
                   9254: 	}
1.338     www      9255: # are there custom rights to evaluate
1.599     albertel 9256: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 9257: 
1.338     www      9258:     #
                   9259:     # Importing a rights file here
1.339     albertel 9260:     #
                   9261: 	    unless ($depthcount) {
1.599     albertel 9262: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 9263: 		my $dir=$filename;
                   9264: 		$dir=~s|[^/]*$||;
                   9265: 		$location=&filelocation($dir,$location);
1.736     albertel 9266: 		my $rights_metadata =
                   9267: 		    &metadata($uri,'keys',$location,'_rights',
                   9268: 			      $depthcount+1);
                   9269: 		foreach my $rights (split(',',$rights_metadata)) {
                   9270: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   9271: 		    $metathesekeys{$rights}=1;
1.339     albertel 9272: 		}
                   9273: 	    }
                   9274: 	}
1.737     albertel 9275: 	# uniqifiy package listing
                   9276: 	my %seen;
                   9277: 	my @uniq_packages =
                   9278: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   9279: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   9280: 
1.1070    www      9281:         if ($importedparts) {
                   9282: # We had imported parts and need to rebuild partorder
                   9283:            $metaentry{':partorder'}='';
                   9284:            $metathesekeys{'partorder'}=1;
                   9285:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
                   9286:                if ($origfileimportpartids[$index] eq 'part') {
                   9287: # original part, part of the problem
                   9288:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
                   9289:                } else {
                   9290: # we have imported parts at this position
                   9291:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
                   9292:                }
                   9293:            }
                   9294:            $metaentry{':partorder'}=~s/^\,//;
                   9295:         }
                   9296: 
1.737     albertel 9297: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 9298: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   9299: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924     albertel 9300: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      9301: # this is the end of "was not already recently cached
1.71      www      9302:     }
1.599     albertel 9303:     return $metaentry{':'.$what};
1.261     albertel 9304: }
                   9305: 
1.488     albertel 9306: sub metadata_create_package_def {
1.483     albertel 9307:     my ($uri,$key,$package,$metathesekeys)=@_;
                   9308:     my ($pack,$name,$subp)=split(/\&/,$key);
                   9309:     if ($subp eq 'default') { next; }
                   9310:     
1.599     albertel 9311:     if (defined($metaentry{':packages'})) {
                   9312: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 9313:     } else {
1.599     albertel 9314: 	$metaentry{':packages'}=$package;
1.483     albertel 9315:     }
                   9316:     my $value=$packagetab{$key};
                   9317:     my $unikey;
                   9318:     $unikey='parameter_0_'.$name;
1.599     albertel 9319:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 9320:     $$metathesekeys{$unikey}=1;
1.599     albertel 9321:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   9322: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 9323:     }
1.599     albertel 9324:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   9325: 	$metaentry{':'.$unikey}=
                   9326: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 9327:     }
                   9328: }
                   9329: 
1.261     albertel 9330: sub metadata_generate_part0 {
                   9331:     my ($metadata,$metacache,$uri) = @_;
                   9332:     my %allnames;
1.737     albertel 9333:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 9334: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 9335: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   9336: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 9337: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 9338: 	    $allnames{$name}=$part;
                   9339: 	  }
                   9340: 	}
                   9341:     }
                   9342:     foreach my $name (keys(%allnames)) {
                   9343:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 9344:       my $key=":parameter_0_$name";
1.261     albertel 9345:       $$metacache{"$key.part"}='0';
                   9346:       $$metacache{"$key.name"}=$name;
1.428     albertel 9347:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 9348: 					   $allnames{$name}.'_'.$name.
                   9349: 					   '.type'};
1.428     albertel 9350:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 9351: 			     '.display'};
1.644     www      9352:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 9353:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 9354:       $$metacache{"$key.display"}=$olddis;
                   9355:     }
1.71      www      9356: }
                   9357: 
1.764     albertel 9358: # ------------------------------------------------------ Devalidate title cache
                   9359: 
                   9360: sub devalidate_title_cache {
                   9361:     my ($url)=@_;
                   9362:     if (!$env{'request.course.id'}) { return; }
                   9363:     my $symb=&symbread($url);
                   9364:     if (!$symb) { return; }
                   9365:     my $key=$env{'request.course.id'}."\0".$symb;
                   9366:     &devalidate_cache_new('title',$key);
                   9367: }
                   9368: 
1.1014    droeschl 9369: # ------------------------------------------------- Get the title of a course
                   9370: 
                   9371: sub current_course_title {
                   9372:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   9373: }
1.301     www      9374: # ------------------------------------------------- Get the title of a resource
                   9375: 
                   9376: sub gettitle {
                   9377:     my $urlsymb=shift;
                   9378:     my $symb=&symbread($urlsymb);
1.534     albertel 9379:     if ($symb) {
1.620     albertel 9380: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 9381: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 9382: 	if (defined($cached)) { 
                   9383: 	    return $result;
                   9384: 	}
1.534     albertel 9385: 	my ($map,$resid,$url)=&decode_symb($symb);
                   9386: 	my $title='';
1.907     albertel 9387: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   9388: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   9389: 	} else {
                   9390: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   9391: 		    &GDBM_READER(),0640)) {
                   9392: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   9393: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   9394: 		untie(%bighash);
                   9395: 	    }
1.534     albertel 9396: 	}
                   9397: 	$title=~s/\&colon\;/\:/gs;
                   9398: 	if ($title) {
1.599     albertel 9399: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 9400: 	}
                   9401: 	$urlsymb=$url;
                   9402:     }
                   9403:     my $title=&metadata($urlsymb,'title');
                   9404:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   9405:     return $title;
1.301     www      9406: }
1.613     albertel 9407: 
1.614     albertel 9408: sub get_slot {
                   9409:     my ($which,$cnum,$cdom)=@_;
                   9410:     if (!$cnum || !$cdom) {
1.790     albertel 9411: 	(undef,my $courseid)=&whichuser();
1.620     albertel 9412: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   9413: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 9414:     }
1.703     albertel 9415:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   9416:     my %slotinfo;
                   9417:     if (exists($remembered{$key})) {
                   9418: 	$slotinfo{$which} = $remembered{$key};
                   9419:     } else {
                   9420: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   9421: 	&Apache::lonhomework::showhash(%slotinfo);
                   9422: 	my ($tmp)=keys(%slotinfo);
                   9423: 	if ($tmp=~/^error:/) { return (); }
                   9424: 	$remembered{$key} = $slotinfo{$which};
                   9425:     }
1.616     albertel 9426:     if (ref($slotinfo{$which}) eq 'HASH') {
                   9427: 	return %{$slotinfo{$which}};
                   9428:     }
                   9429:     return $slotinfo{$which};
1.614     albertel 9430: }
1.1150    raeburn  9431: 
                   9432: sub get_reservable_slots {
                   9433:     my ($cnum,$cdom,$uname,$udom) = @_;
                   9434:     my $now = time;
                   9435:     my $reservable_info;
                   9436:     my $key=join("\0",'reservableslots',$cdom,$cnum,$uname,$udom);
                   9437:     if (exists($remembered{$key})) {
                   9438:         $reservable_info = $remembered{$key};
                   9439:     } else {
                   9440:         my %resv;
                   9441:         ($resv{'now_order'},$resv{'now'},$resv{'future_order'},$resv{'future'}) =
                   9442:         &Apache::loncommon::get_future_slots($cnum,$cdom,$now);
                   9443:         $reservable_info = \%resv;
                   9444:         $remembered{$key} = $reservable_info;
                   9445:     }
                   9446:     return $reservable_info;
                   9447: }
                   9448: 
                   9449: sub get_course_slots {
                   9450:     my ($cnum,$cdom) = @_;
                   9451:     my $hashid=$cnum.':'.$cdom;
                   9452:     my ($result,$cached) = &Apache::lonnet::is_cached_new('allslots',$hashid);
                   9453:     if (defined($cached)) {
                   9454:         if (ref($result) eq 'HASH') {
                   9455:             return %{$result};
                   9456:         }
                   9457:     } else {
                   9458:         my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
                   9459:         my ($tmp) = keys(%slots);
                   9460:         if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                   9461:             &Apache::lonnet::do_cache_new('allslots',$hashid,\%slots,600);
                   9462:             return %slots;
                   9463:         }
                   9464:     }
                   9465:     return;
                   9466: }
                   9467: 
                   9468: sub devalidate_slots_cache {
                   9469:     my ($cnum,$cdom)=@_;
                   9470:     my $hashid=$cnum.':'.$cdom;
                   9471:     &devalidate_cache_new('allslots',$hashid);
                   9472: }
                   9473: 
1.31      www      9474: # ------------------------------------------------- Update symbolic store links
                   9475: 
                   9476: sub symblist {
                   9477:     my ($mapname,%newhash)=@_;
1.438     www      9478:     $mapname=&deversion(&declutter($mapname));
1.31      www      9479:     my %hash;
1.620     albertel 9480:     if (($env{'request.course.fn'}) && (%newhash)) {
                   9481:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 9482:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  9483: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 9484: 		next if ($url eq 'last_known'
                   9485: 			 && $env{'form.no_update_last_known'});
                   9486: 		$hash{declutter($url)}=&encode_symb($mapname,
                   9487: 						    $newhash{$url}->[1],
                   9488: 						    $newhash{$url}->[0]);
1.191     harris41 9489:             }
1.31      www      9490:             if (untie(%hash)) {
                   9491: 		return 'ok';
                   9492:             }
                   9493:         }
                   9494:     }
                   9495:     return 'error';
1.212     www      9496: }
                   9497: 
                   9498: # --------------------------------------------------------------- Verify a symb
                   9499: 
                   9500: sub symbverify {
1.510     www      9501:     my ($symb,$thisurl)=@_;
                   9502:     my $thisfn=$thisurl;
1.439     www      9503:     $thisfn=&declutter($thisfn);
1.215     www      9504: # direct jump to resource in page or to a sequence - will construct own symbs
                   9505:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   9506: # check URL part
1.409     www      9507:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      9508: 
1.431     www      9509:     unless ($url eq $thisfn) { return 0; }
1.213     www      9510: 
1.216     www      9511:     $symb=&symbclean($symb);
1.510     www      9512:     $thisurl=&deversion($thisurl);
1.439     www      9513:     $thisfn=&deversion($thisfn);
1.213     www      9514: 
                   9515:     my %bighash;
                   9516:     my $okay=0;
1.431     www      9517: 
1.620     albertel 9518:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 9519:                             &GDBM_READER(),0640)) {
1.1032    raeburn  9520:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   9521:             $thisurl =~ s/\?.+$//;
                   9522:         }
1.510     www      9523:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1102    raeburn  9524:         unless ($ids) {
                   9525:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   9526:             $ids=$bighash{$idkey};
1.216     www      9527:         }
                   9528:         if ($ids) {
                   9529: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 9530: 	    foreach my $id (split(/\,/,$ids)) {
                   9531: 	       my ($mapid,$resid)=split(/\./,$id);
1.1032    raeburn  9532:                if ($thisfn =~ m{^/adm/wrapper/ext/}) {
                   9533:                    $symb =~ s/\?.+$//;
                   9534:                }
1.216     www      9535:                if (
                   9536:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   9537:    eq $symb) { 
1.620     albertel 9538: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  9539: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   9540:                        ($thisurl eq '/adm/navmaps')) {
1.582     albertel 9541: 		       $okay=1; 
                   9542: 		   }
                   9543: 	       }
1.216     www      9544: 	   }
                   9545:         }
1.213     www      9546: 	untie(%bighash);
                   9547:     }
                   9548:     return $okay;
1.31      www      9549: }
                   9550: 
1.210     www      9551: # --------------------------------------------------------------- Clean-up symb
                   9552: 
                   9553: sub symbclean {
                   9554:     my $symb=shift;
1.568     albertel 9555:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      9556: # remove version from map
                   9557:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      9558: 
1.210     www      9559: # remove version from URL
                   9560:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      9561: 
1.507     www      9562: # remove wrapper
                   9563: 
1.510     www      9564:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 9565:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      9566:     return $symb;
1.409     www      9567: }
                   9568: 
                   9569: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 9570: 
                   9571: sub encode_symb {
                   9572:     my ($map,$resid,$url)=@_;
                   9573:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   9574: }
1.409     www      9575: 
                   9576: sub decode_symb {
1.568     albertel 9577:     my $symb=shift;
                   9578:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   9579:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      9580:     return (&fixversion($map),$resid,&fixversion($url));
                   9581: }
                   9582: 
                   9583: sub fixversion {
                   9584:     my $fn=shift;
1.609     banghart 9585:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      9586:     my %bighash;
                   9587:     my $uri=&clutter($fn);
1.620     albertel 9588:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      9589: # is this cached?
1.599     albertel 9590:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      9591:     if (defined($cached)) { return $result; }
                   9592: # unfortunately not cached, or expired
1.620     albertel 9593:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      9594: 	    &GDBM_READER(),0640)) {
                   9595:  	if ($bighash{'version_'.$uri}) {
                   9596:  	    my $version=$bighash{'version_'.$uri};
1.444     www      9597:  	    unless (($version eq 'mostrecent') || 
                   9598: 		    ($version==&getversion($uri))) {
1.440     www      9599:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   9600:  	    }
                   9601:  	}
                   9602:  	untie %bighash;
1.413     www      9603:     }
1.599     albertel 9604:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      9605: }
                   9606: 
                   9607: sub deversion {
                   9608:     my $url=shift;
                   9609:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   9610:     return $url;
1.210     www      9611: }
                   9612: 
1.31      www      9613: # ------------------------------------------------------ Return symb list entry
                   9614: 
                   9615: sub symbread {
1.249     www      9616:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 9617:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 9618:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      9619: # no filename provided? try from environment
1.44      www      9620:     unless ($thisfn) {
1.620     albertel 9621:         if ($env{'request.symb'}) {
                   9622: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 9623: 	}
1.620     albertel 9624: 	$thisfn=$env{'request.filename'};
1.44      www      9625:     }
1.569     albertel 9626:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      9627: # is that filename actually a symb? Verify, clean, and return
                   9628:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 9629: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 9630: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 9631: 	}
1.242     www      9632:     }
1.44      www      9633:     $thisfn=declutter($thisfn);
1.31      www      9634:     my %hash;
1.37      www      9635:     my %bighash;
                   9636:     my $syval='';
1.620     albertel 9637:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  9638:         my $targetfn = $thisfn;
1.609     banghart 9639:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  9640:             $targetfn = 'adm/wrapper/'.$thisfn;
                   9641:         }
1.687     albertel 9642: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   9643: 	    $targetfn=$1;
                   9644: 	}
1.620     albertel 9645:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 9646:                       &GDBM_READER(),0640)) {
1.481     raeburn  9647: 	    $syval=$hash{$targetfn};
1.37      www      9648:             untie(%hash);
                   9649:         }
                   9650: # ---------------------------------------------------------- There was an entry
                   9651:         if ($syval) {
1.601     albertel 9652: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 9653: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  9654: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9655: 		    #return $env{$cache_str}='';
1.601     albertel 9656: 		#}    
                   9657: 		#$syval.=$1;
                   9658: 	    #}
1.37      www      9659:         } else {
                   9660: # ------------------------------------------------------- Was not in symb table
1.620     albertel 9661:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 9662:                             &GDBM_READER(),0640)) {
1.37      www      9663: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      9664:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      9665:               unless ($ids) { 
                   9666:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      9667:               }
                   9668:               unless ($ids) {
                   9669: # alias?
                   9670: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      9671:               }
1.37      www      9672:               if ($ids) {
                   9673: # ------------------------------------------------------------------- Has ID(s)
                   9674:                  my @possibilities=split(/\,/,$ids);
1.39      www      9675:                  if ($#possibilities==0) {
                   9676: # ----------------------------------------------- There is only one possibility
1.37      www      9677: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 9678: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9679: 						    $resid,$thisfn);
1.249     www      9680:                  } elsif (!$donotrecurse) {
1.39      www      9681: # ------------------------------------------ There is more than one possibility
                   9682:                      my $realpossible=0;
1.800     albertel 9683:                      foreach my $id (@possibilities) {
                   9684: 			 my $file=$bighash{'src_'.$id};
1.39      www      9685:                          if (&allowed('bre',$file)) {
1.800     albertel 9686:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      9687:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   9688: 				$realpossible++;
1.626     albertel 9689:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9690: 						    $resid,$thisfn);
1.39      www      9691:                             }
                   9692: 			 }
1.191     harris41 9693:                      }
1.39      www      9694: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      9695:                  } else {
                   9696:                      $syval='';
1.37      www      9697:                  }
                   9698: 	      }
                   9699:               untie(%bighash)
1.481     raeburn  9700:            }
1.31      www      9701:         }
1.62      www      9702:         if ($syval) {
1.620     albertel 9703: 	    return $env{$cache_str}=$syval;
1.62      www      9704:         }
1.31      www      9705:     }
1.949     raeburn  9706:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9707:     return $env{$cache_str}='';
1.31      www      9708: }
                   9709: 
                   9710: # ---------------------------------------------------------- Return random seed
                   9711: 
1.32      www      9712: sub numval {
                   9713:     my $txt=shift;
                   9714:     $txt=~tr/A-J/0-9/;
                   9715:     $txt=~tr/a-j/0-9/;
                   9716:     $txt=~tr/K-T/0-9/;
                   9717:     $txt=~tr/k-t/0-9/;
                   9718:     $txt=~tr/U-Z/0-5/;
                   9719:     $txt=~tr/u-z/0-5/;
                   9720:     $txt=~s/\D//g;
1.564     albertel 9721:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      9722:     return int($txt);
1.368     albertel 9723: }
                   9724: 
1.484     albertel 9725: sub numval2 {
                   9726:     my $txt=shift;
                   9727:     $txt=~tr/A-J/0-9/;
                   9728:     $txt=~tr/a-j/0-9/;
                   9729:     $txt=~tr/K-T/0-9/;
                   9730:     $txt=~tr/k-t/0-9/;
                   9731:     $txt=~tr/U-Z/0-5/;
                   9732:     $txt=~tr/u-z/0-5/;
                   9733:     $txt=~s/\D//g;
                   9734:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9735:     my $total;
                   9736:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 9737:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 9738:     return int($total);
                   9739: }
                   9740: 
1.575     albertel 9741: sub numval3 {
                   9742:     use integer;
                   9743:     my $txt=shift;
                   9744:     $txt=~tr/A-J/0-9/;
                   9745:     $txt=~tr/a-j/0-9/;
                   9746:     $txt=~tr/K-T/0-9/;
                   9747:     $txt=~tr/k-t/0-9/;
                   9748:     $txt=~tr/U-Z/0-5/;
                   9749:     $txt=~tr/u-z/0-5/;
                   9750:     $txt=~s/\D//g;
                   9751:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9752:     my $total;
                   9753:     foreach my $val (@txts) { $total+=$val; }
                   9754:     if ($_64bit) { $total=(($total<<32)>>32); }
                   9755:     return $total;
                   9756: }
                   9757: 
1.675     albertel 9758: sub digest {
                   9759:     my ($data)=@_;
                   9760:     my $digest=&Digest::MD5::md5($data);
                   9761:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   9762:     my ($e,$f);
                   9763:     {
                   9764:         use integer;
                   9765:         $e=($a+$b);
                   9766:         $f=($c+$d);
                   9767:         if ($_64bit) {
                   9768:             $e=(($e<<32)>>32);
                   9769:             $f=(($f<<32)>>32);
                   9770:         }
                   9771:     }
                   9772:     if (wantarray) {
                   9773: 	return ($e,$f);
                   9774:     } else {
                   9775: 	my $g;
                   9776: 	{
                   9777: 	    use integer;
                   9778: 	    $g=($e+$f);
                   9779: 	    if ($_64bit) {
                   9780: 		$g=(($g<<32)>>32);
                   9781: 	    }
                   9782: 	}
                   9783: 	return $g;
                   9784:     }
                   9785: }
                   9786: 
1.368     albertel 9787: sub latest_rnd_algorithm_id {
1.675     albertel 9788:     return '64bit5';
1.366     albertel 9789: }
1.32      www      9790: 
1.503     albertel 9791: sub get_rand_alg {
                   9792:     my ($courseid)=@_;
1.790     albertel 9793:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 9794:     if ($courseid) {
1.620     albertel 9795: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 9796:     }
                   9797:     return &latest_rnd_algorithm_id();
                   9798: }
                   9799: 
1.562     albertel 9800: sub validCODE {
                   9801:     my ($CODE)=@_;
                   9802:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   9803:     return 0;
                   9804: }
                   9805: 
1.491     albertel 9806: sub getCODE {
1.620     albertel 9807:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 9808:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   9809: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   9810: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 9811: 	return $Apache::lonhomework::history{'resource.CODE'};
                   9812:     }
                   9813:     return undef;
                   9814: }
1.1133    foxr     9815: #
                   9816: #  Determines the random seed for a specific context:
                   9817: #
                   9818: # parameters:
                   9819: #   symb      - in course context the symb for the seed.
                   9820: #   course_id - The course id of the form domain_coursenum.
                   9821: #   domain    - Domain for the user.
                   9822: #   course    - Course for the user.
                   9823: #   cenv      - environment of the course.
                   9824: #
                   9825: # NOTE:
                   9826: #   All parameters are picked out of the environment if missing
                   9827: #   or not defined.
                   9828: #   If a symb cannot be determined the current time is used instead.
                   9829: #
                   9830: #  For a given well defined symb, courside, domain, username,
                   9831: #  and course environment, the seed is reproducible.
                   9832: #
1.31      www      9833: sub rndseed {
1.1133    foxr     9834:     my ($symb,$courseid,$domain,$username, $cenv)=@_;
1.790     albertel 9835:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 9836:     if (!defined($symb)) {
1.366     albertel 9837: 	unless ($symb=$wsymb) { return time; }
                   9838:     }
1.1146    foxr     9839:     if (!defined $courseid) { 
                   9840: 	$courseid=$wcourseid; 
                   9841:     }
                   9842:     if (!defined $domain) { $domain=$wdomain; }
                   9843:     if (!defined $username) { $username=$wusername }
1.1133    foxr     9844: 
                   9845:     my $which;
                   9846:     if (defined($cenv->{'rndseed'})) {
                   9847: 	$which = $cenv->{'rndseed'};
                   9848:     } else {
                   9849: 	$which =&get_rand_alg($courseid);
                   9850:     }
1.491     albertel 9851:     if (defined(&getCODE())) {
1.1133    foxr     9852: 
1.675     albertel 9853: 	if ($which eq '64bit5') {
                   9854: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   9855: 	} elsif ($which eq '64bit4') {
1.575     albertel 9856: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   9857: 	} else {
                   9858: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   9859: 	}
1.675     albertel 9860:     } elsif ($which eq '64bit5') {
                   9861: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 9862:     } elsif ($which eq '64bit4') {
                   9863: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 9864:     } elsif ($which eq '64bit3') {
                   9865: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 9866:     } elsif ($which eq '64bit2') {
                   9867: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 9868:     } elsif ($which eq '64bit') {
                   9869: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   9870:     }
                   9871:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   9872: }
                   9873: 
                   9874: sub rndseed_32bit {
                   9875:     my ($symb,$courseid,$domain,$username)=@_;
                   9876:     {
                   9877: 	use integer;
                   9878: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   9879: 	my $symbseed=numval($symb) << 22;
                   9880: 	my $namechck=unpack("%32C*",$username) << 17;
                   9881: 	my $nameseed=numval($username) << 12;
                   9882: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   9883: 	my $courseseed=unpack("%32C*",$courseid);
                   9884: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 9885: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9886: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9887: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 9888: 	return $num;
                   9889:     }
                   9890: }
                   9891: 
                   9892: sub rndseed_64bit {
                   9893:     my ($symb,$courseid,$domain,$username)=@_;
                   9894:     {
                   9895: 	use integer;
                   9896: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   9897: 	my $symbseed=numval($symb) << 10;
                   9898: 	my $namechck=unpack("%32S*",$username);
                   9899: 	
                   9900: 	my $nameseed=numval($username) << 21;
                   9901: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   9902: 	my $courseseed=unpack("%32S*",$courseid);
                   9903: 	
                   9904: 	my $num1=$symbchck+$symbseed+$namechck;
                   9905: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9906: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9907: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9908: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 9909: 	return "$num1,$num2";
1.155     albertel 9910:     }
1.366     albertel 9911: }
                   9912: 
1.443     albertel 9913: sub rndseed_64bit2 {
                   9914:     my ($symb,$courseid,$domain,$username)=@_;
                   9915:     {
                   9916: 	use integer;
                   9917: 	# strings need to be an even # of cahracters long, it it is odd the
                   9918:         # last characters gets thrown away
                   9919: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9920: 	my $symbseed=numval($symb) << 10;
                   9921: 	my $namechck=unpack("%32S*",$username.' ');
                   9922: 	
                   9923: 	my $nameseed=numval($username) << 21;
1.501     albertel 9924: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9925: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9926: 	
                   9927: 	my $num1=$symbchck+$symbseed+$namechck;
                   9928: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9929: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9930: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 9931: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 9932: 	return "$num1,$num2";
                   9933:     }
                   9934: }
                   9935: 
                   9936: sub rndseed_64bit3 {
                   9937:     my ($symb,$courseid,$domain,$username)=@_;
                   9938:     {
                   9939: 	use integer;
                   9940: 	# strings need to be an even # of cahracters long, it it is odd the
                   9941:         # last characters gets thrown away
                   9942: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9943: 	my $symbseed=numval2($symb) << 10;
                   9944: 	my $namechck=unpack("%32S*",$username.' ');
                   9945: 	
                   9946: 	my $nameseed=numval2($username) << 21;
1.443     albertel 9947: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9948: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9949: 	
                   9950: 	my $num1=$symbchck+$symbseed+$namechck;
                   9951: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9952: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9953: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 9954: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9955: 	
1.503     albertel 9956: 	return "$num1:$num2";
1.443     albertel 9957:     }
                   9958: }
                   9959: 
1.575     albertel 9960: sub rndseed_64bit4 {
                   9961:     my ($symb,$courseid,$domain,$username)=@_;
                   9962:     {
                   9963: 	use integer;
                   9964: 	# strings need to be an even # of cahracters long, it it is odd the
                   9965:         # last characters gets thrown away
                   9966: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9967: 	my $symbseed=numval3($symb) << 10;
                   9968: 	my $namechck=unpack("%32S*",$username.' ');
                   9969: 	
                   9970: 	my $nameseed=numval3($username) << 21;
                   9971: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9972: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9973: 	
                   9974: 	my $num1=$symbchck+$symbseed+$namechck;
                   9975: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9976: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9977: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 9978: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9979: 	
1.575     albertel 9980: 	return "$num1:$num2";
                   9981:     }
                   9982: }
                   9983: 
1.675     albertel 9984: sub rndseed_64bit5 {
                   9985:     my ($symb,$courseid,$domain,$username)=@_;
                   9986:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   9987:     return "$num1:$num2";
                   9988: }
                   9989: 
1.366     albertel 9990: sub rndseed_CODE_64bit {
                   9991:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 9992:     {
1.366     albertel 9993: 	use integer;
1.443     albertel 9994: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 9995: 	my $symbseed=numval2($symb);
1.491     albertel 9996: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   9997: 	my $CODEseed=numval(&getCODE());
1.443     albertel 9998: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 9999: 	my $num1=$symbseed+$CODEchck;
                   10000: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 10001: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   10002: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 10003: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   10004: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 10005: 	return "$num1:$num2";
1.366     albertel 10006:     }
                   10007: }
                   10008: 
1.575     albertel 10009: sub rndseed_CODE_64bit4 {
                   10010:     my ($symb,$courseid,$domain,$username)=@_;
                   10011:     {
                   10012: 	use integer;
                   10013: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   10014: 	my $symbseed=numval3($symb);
                   10015: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   10016: 	my $CODEseed=numval3(&getCODE());
                   10017: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   10018: 	my $num1=$symbseed+$CODEchck;
                   10019: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 10020: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   10021: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 10022: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   10023: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   10024: 	return "$num1:$num2";
                   10025:     }
                   10026: }
                   10027: 
1.675     albertel 10028: sub rndseed_CODE_64bit5 {
                   10029:     my ($symb,$courseid,$domain,$username)=@_;
                   10030:     my $code = &getCODE();
                   10031:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   10032:     return "$num1:$num2";
                   10033: }
                   10034: 
1.366     albertel 10035: sub setup_random_from_rndseed {
                   10036:     my ($rndseed)=@_;
1.503     albertel 10037:     if ($rndseed =~/([,:])/) {
                   10038: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 10039: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   10040:     } else {
                   10041: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 10042:     }
1.36      albertel 10043: }
                   10044: 
1.474     albertel 10045: sub latest_receipt_algorithm_id {
1.835     albertel 10046:     return 'receipt3';
1.474     albertel 10047: }
                   10048: 
1.480     www      10049: sub recunique {
                   10050:     my $fucourseid=shift;
                   10051:     my $unique;
1.835     albertel 10052:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   10053: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 10054: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      10055:     } else {
                   10056: 	$unique=$perlvar{'lonReceipt'};
                   10057:     }
                   10058:     return unpack("%32C*",$unique);
                   10059: }
                   10060: 
                   10061: sub recprefix {
                   10062:     my $fucourseid=shift;
                   10063:     my $prefix;
1.835     albertel 10064:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   10065: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 10066: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      10067:     } else {
                   10068: 	$prefix=$perlvar{'lonHostID'};
                   10069:     }
                   10070:     return unpack("%32C*",$prefix);
                   10071: }
                   10072: 
1.76      www      10073: sub ireceipt {
1.474     albertel 10074:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 10075: 
                   10076:     my $return =&recprefix($fucourseid).'-';
                   10077: 
                   10078:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   10079: 	$env{'request.state'} eq 'construct') {
                   10080: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   10081: 	return $return;
                   10082:     }
                   10083: 
1.76      www      10084:     my $cuname=unpack("%32C*",$funame);
                   10085:     my $cudom=unpack("%32C*",$fudom);
                   10086:     my $cucourseid=unpack("%32C*",$fucourseid);
                   10087:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      10088:     my $cunique=&recunique($fucourseid);
1.474     albertel 10089:     my $cpart=unpack("%32S*",$part);
1.835     albertel 10090:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   10091: 
1.790     albertel 10092: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 10093: 			       
                   10094: 	$return.= ($cunique%$cuname+
                   10095: 		   $cunique%$cudom+
                   10096: 		   $cusymb%$cuname+
                   10097: 		   $cusymb%$cudom+
                   10098: 		   $cucourseid%$cuname+
                   10099: 		   $cucourseid%$cudom+
                   10100: 		   $cpart%$cuname+
                   10101: 		   $cpart%$cudom);
                   10102:     } else {
                   10103: 	$return.= ($cunique%$cuname+
                   10104: 		   $cunique%$cudom+
                   10105: 		   $cusymb%$cuname+
                   10106: 		   $cusymb%$cudom+
                   10107: 		   $cucourseid%$cuname+
                   10108: 		   $cucourseid%$cudom);
                   10109:     }
                   10110:     return $return;
1.76      www      10111: }
                   10112: 
                   10113: sub receipt {
1.474     albertel 10114:     my ($part)=@_;
1.790     albertel 10115:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 10116:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      10117: }
1.260     ng       10118: 
1.790     albertel 10119: sub whichuser {
                   10120:     my ($passedsymb)=@_;
                   10121:     my ($symb,$courseid,$domain,$name,$publicuser);
                   10122:     if (defined($env{'form.grade_symb'})) {
                   10123: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   10124: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   10125: 	if (!$allowed &&
                   10126: 	    exists($env{'request.course.sec'}) &&
                   10127: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   10128: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   10129: 			      '/'.$env{'request.course.sec'});
                   10130: 	}
                   10131: 	if ($allowed) {
                   10132: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   10133: 	    $courseid=$tmp_courseid;
                   10134: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   10135: 	    ($name)=&get_env_multiple('form.grade_username');
                   10136: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   10137: 	}
                   10138:     }
                   10139:     if (!$passedsymb) {
                   10140: 	$symb=&symbread();
                   10141:     } else {
                   10142: 	$symb=$passedsymb;
                   10143:     }
                   10144:     $courseid=$env{'request.course.id'};
                   10145:     $domain=$env{'user.domain'};
                   10146:     $name=$env{'user.name'};
                   10147:     if ($name eq 'public' && $domain eq 'public') {
                   10148: 	if (!defined($env{'form.username'})) {
                   10149: 	    $env{'form.username'}.=time.rand(10000000);
                   10150: 	}
                   10151: 	$name.=$env{'form.username'};
                   10152:     }
                   10153:     return ($symb,$courseid,$domain,$name,$publicuser);
                   10154: 
                   10155: }
                   10156: 
1.36      albertel 10157: # ------------------------------------------------------------ Serves up a file
1.472     albertel 10158: # returns either the contents of the file or 
                   10159: # -1 if the file doesn't exist
1.481     raeburn  10160: #
                   10161: # if the target is a file that was uploaded via DOCS, 
                   10162: # a check will be made to see if a current copy exists on the local server,
                   10163: # if it does this will be served, otherwise a copy will be retrieved from
                   10164: # the home server for the course and stored in /home/httpd/html/userfiles on
                   10165: # the local server.   
1.472     albertel 10166: 
1.36      albertel 10167: sub getfile {
1.538     albertel 10168:     my ($file) = @_;
1.609     banghart 10169:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 10170:     &repcopy($file);
                   10171:     return &readfile($file);
                   10172: }
                   10173: 
                   10174: sub repcopy_userfile {
                   10175:     my ($file)=@_;
1.1142    raeburn  10176:     my $londocroot = $perlvar{'lonDocRoot'};
                   10177:     if ($file =~ m{^/*(uploaded|editupload)/}) { $file=&filelocation("",$file); }
                   10178:     if ($file =~ m{^\Q$londocroot/lonUsers/\E}) { return 'ok'; }
1.538     albertel 10179:     my ($cdom,$cnum,$filename) = 
1.811     albertel 10180: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 10181:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   10182:     if (-e "$file") {
1.828     www      10183: # we already have a local copy, check it out
1.538     albertel 10184: 	my @fileinfo = stat($file);
1.828     www      10185: 	my $rtncode;
                   10186: 	my $info;
1.538     albertel 10187: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 10188: 	if ($lwpresp ne 'ok') {
1.828     www      10189: # there is no such file anymore, even though we had a local copy
1.482     albertel 10190: 	    if ($rtncode eq '404') {
1.538     albertel 10191: 		unlink($file);
1.482     albertel 10192: 	    }
                   10193: 	    return -1;
                   10194: 	}
                   10195: 	if ($info < $fileinfo[9]) {
1.828     www      10196: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  10197: 	    return 'ok';
1.828     www      10198: 	} else {
                   10199: # the file is outdated, get rid of it
                   10200: 	    unlink($file);
1.482     albertel 10201: 	}
1.828     www      10202:     }
                   10203: # one way or the other, at this point, we don't have the file
                   10204: # construct the correct path for the file
                   10205:     my @parts = ($cdom,$cnum); 
                   10206:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   10207: 	push @parts, split(/\//,$1);
                   10208:     }
                   10209:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   10210:     foreach my $part (@parts) {
                   10211: 	$path .= '/'.$part;
                   10212: 	if (!-e $path) {
                   10213: 	    mkdir($path,0770);
1.482     albertel 10214: 	}
                   10215:     }
1.828     www      10216: # now the path exists for sure
                   10217: # get a user agent
                   10218:     my $ua=new LWP::UserAgent;
                   10219:     my $transferfile=$file.'.in.transfer';
                   10220: # FIXME: this should flock
                   10221:     if (-e $transferfile) { return 'ok'; }
                   10222:     my $request;
                   10223:     $uri=~s/^\///;
1.980     raeburn  10224:     my $homeserver = &homeserver($cnum,$cdom);
                   10225:     my $protocol = $protocol{$homeserver};
                   10226:     $protocol = 'http' if ($protocol ne 'https');
                   10227:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828     www      10228:     my $response=$ua->request($request,$transferfile);
                   10229: # did it work?
                   10230:     if ($response->is_error()) {
                   10231: 	unlink($transferfile);
                   10232: 	&logthis("Userfile repcopy failed for $uri");
                   10233: 	return -1;
                   10234:     }
                   10235: # worked, rename the transfer file
                   10236:     rename($transferfile,$file);
1.607     raeburn  10237:     return 'ok';
1.481     raeburn  10238: }
                   10239: 
1.517     albertel 10240: sub tokenwrapper {
                   10241:     my $uri=shift;
1.980     raeburn  10242:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 10243:     $uri=~s|^/||;
1.620     albertel 10244:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 10245:     my $token=$1;
1.552     albertel 10246:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   10247:     if ($udom && $uname && $file) {
                   10248: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  10249:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  10250:         my $homeserver = &homeserver($uname,$udom);
                   10251:         my $protocol = $protocol{$homeserver};
                   10252:         $protocol = 'http' if ($protocol ne 'https');
                   10253:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517     albertel 10254:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   10255:                                '&tokenissued='.$perlvar{'lonHostID'};
                   10256:     } else {
                   10257:         return '/adm/notfound.html';
                   10258:     }
                   10259: }
                   10260: 
1.828     www      10261: # call with reqtype HEAD: get last modification time
                   10262: # call with reqtype GET: get the file contents
                   10263: # Do not call this with reqtype GET for large files! It loads everything into memory
                   10264: #
1.481     raeburn  10265: sub getuploaded {
                   10266:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   10267:     $uri=~s/^\///;
1.980     raeburn  10268:     my $homeserver = &homeserver($cnum,$cdom);
                   10269:     my $protocol = $protocol{$homeserver};
                   10270:     $protocol = 'http' if ($protocol ne 'https');
                   10271:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481     raeburn  10272:     my $ua=new LWP::UserAgent;
                   10273:     my $request=new HTTP::Request($reqtype,$uri);
                   10274:     my $response=$ua->request($request);
                   10275:     $$rtncode = $response->code;
1.482     albertel 10276:     if (! $response->is_success()) {
                   10277: 	return 'failed';
                   10278:     }      
                   10279:     if ($reqtype eq 'HEAD') {
1.486     www      10280: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 10281:     } elsif ($reqtype eq 'GET') {
                   10282: 	$$info = $response->content;
1.472     albertel 10283:     }
1.482     albertel 10284:     return 'ok';
1.36      albertel 10285: }
                   10286: 
1.481     raeburn  10287: sub readfile {
                   10288:     my $file = shift;
                   10289:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   10290:     my $fh;
                   10291:     open($fh,"<$file");
                   10292:     my $a='';
1.800     albertel 10293:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  10294:     return $a;
                   10295: }
                   10296: 
1.36      albertel 10297: sub filelocation {
1.590     banghart 10298:     my ($dir,$file) = @_;
                   10299:     my $location;
                   10300:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 10301: 
                   10302:     if ($file =~ m-^/adm/-) {
                   10303: 	$file=~s-^/adm/wrapper/-/-;
                   10304: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   10305:     }
1.882     albertel 10306: 
1.1139    www      10307:     if ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
1.956     raeburn  10308:         $location = $file;
1.609     banghart 10309:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 10310:         my ($udom,$uname,$filename)=
1.811     albertel 10311:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 10312:         my $home=&homeserver($uname,$udom);
                   10313:         my $is_me=0;
                   10314:         my @ids=&current_machine_ids();
                   10315:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   10316:         if ($is_me) {
1.1117    foxr     10317:   	    $location=propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 10318:         } else {
                   10319:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   10320:   	      $udom.'/'.$uname.'/'.$filename;
                   10321:         }
1.882     albertel 10322:     } elsif ($file =~ m-^/adm/-) {
                   10323: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 10324:     } else {
                   10325:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.1139    www      10326:         $file=~s:^/(res|priv)/:/:;
                   10327:         my $space=$1;
1.590     banghart 10328:         if ( !( $file =~ m:^/:) ) {
                   10329:             $location = $dir. '/'.$file;
                   10330:         } else {
1.1142    raeburn  10331:             $location = $perlvar{'lonDocRoot'}.'/'.$space.$file;
1.590     banghart 10332:         }
1.59      albertel 10333:     }
1.590     banghart 10334:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 10335:     while ($location=~m{/\.\./}) {
                   10336: 	if ($location =~ m{/[^/]+/\.\./}) {
                   10337: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   10338: 	} else {
                   10339: 	    $location=~ s{/\.\./}{/}g;
                   10340: 	}
                   10341:     } #remove dir/..
1.590     banghart 10342:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   10343:     return $location;
1.46      www      10344: }
1.36      albertel 10345: 
1.46      www      10346: sub hreflocation {
                   10347:     my ($dir,$file)=@_;
1.980     raeburn  10348:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 10349: 	$file=filelocation($dir,$file);
1.700     albertel 10350:     } elsif ($file=~m-^/adm/-) {
                   10351: 	$file=~s-^/adm/wrapper/-/-;
                   10352: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 10353:     }
                   10354:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   10355: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
                   10356:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.1143    raeburn  10357: 	$file=~s{^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/}
                   10358: 	        {/uploaded/$1/$2/}x;
1.46      www      10359:     }
1.913     albertel 10360:     if ($file=~ m{^/userfiles/}) {
                   10361: 	$file =~ s{^/userfiles/}{/uploaded/};
                   10362:     }
1.462     albertel 10363:     return $file;
1.465     albertel 10364: }
                   10365: 
1.1139    www      10366: 
                   10367: 
                   10368: 
                   10369: 
1.465     albertel 10370: sub current_machine_domains {
1.853     albertel 10371:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   10372: }
                   10373: 
                   10374: sub machine_domains {
                   10375:     my ($hostname) = @_;
1.465     albertel 10376:     my @domains;
1.838     albertel 10377:     my %hostname = &all_hostnames();
1.465     albertel 10378:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  10379: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 10380: 	if ($hostname eq $name) {
1.844     albertel 10381: 	    push(@domains,&host_domain($id));
1.465     albertel 10382: 	}
                   10383:     }
                   10384:     return @domains;
                   10385: }
                   10386: 
                   10387: sub current_machine_ids {
1.853     albertel 10388:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   10389: }
                   10390: 
                   10391: sub machine_ids {
                   10392:     my ($hostname) = @_;
                   10393:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 10394:     my @ids;
1.888     albertel 10395:     my %name_to_host = &all_names();
1.889     albertel 10396:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   10397: 	return @{ $name_to_host{$hostname} };
                   10398:     }
                   10399:     return;
1.31      www      10400: }
                   10401: 
1.824     raeburn  10402: sub additional_machine_domains {
                   10403:     my @domains;
                   10404:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   10405:     while( my $line = <$fh>) {
                   10406:         $line =~ s/\s//g;
                   10407:         push(@domains,$line);
                   10408:     }
                   10409:     return @domains;
                   10410: }
                   10411: 
                   10412: sub default_login_domain {
                   10413:     my $domain = $perlvar{'lonDefDomain'};
                   10414:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   10415:     foreach my $posdom (&current_machine_domains(),
                   10416:                         &additional_machine_domains()) {
                   10417:         if (lc($posdom) eq lc($testdomain)) {
                   10418:             $domain=$posdom;
                   10419:             last;
                   10420:         }
                   10421:     }
                   10422:     return $domain;
                   10423: }
                   10424: 
1.31      www      10425: # ------------------------------------------------------------- Declutters URLs
                   10426: 
                   10427: sub declutter {
                   10428:     my $thisfn=shift;
1.569     albertel 10429:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 10430:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      10431:     $thisfn=~s/^\///;
1.697     albertel 10432:     $thisfn=~s|^adm/wrapper/||;
                   10433:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      10434:     $thisfn=~s/^res\///;
1.1032    raeburn  10435:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   10436:         $thisfn=~s/\?.+$//;
                   10437:     }
1.268     www      10438:     return $thisfn;
                   10439: }
                   10440: 
                   10441: # ------------------------------------------------------------- Clutter up URLs
                   10442: 
                   10443: sub clutter {
                   10444:     my $thisfn='/'.&declutter(shift);
1.887     albertel 10445:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 10446: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      10447:        $thisfn='/res'.$thisfn; 
                   10448:     }
1.1031    raeburn  10449:     if ($thisfn !~m|^/adm|) {
                   10450: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 10451: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 10452: 	} else {
                   10453: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   10454: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 10455: 	    if ($embstyle eq 'ssi'
                   10456: 		|| ($embstyle eq 'hdn')
                   10457: 		|| ($embstyle eq 'rat')
                   10458: 		|| ($embstyle eq 'prv')
                   10459: 		|| ($embstyle eq 'ign')) {
                   10460: 		#do nothing with these
                   10461: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 10462: 		|| ($embstyle eq 'emb')
                   10463: 		|| ($embstyle eq 'wrp')) {
                   10464: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 10465: 	    } elsif ($embstyle eq 'unk'
                   10466: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 10467: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 10468: 	    } else {
1.718     www      10469: #		&logthis("Got a blank emb style");
1.695     albertel 10470: 	    }
1.694     albertel 10471: 	}
                   10472:     }
1.31      www      10473:     return $thisfn;
1.12      www      10474: }
                   10475: 
1.787     albertel 10476: sub clutter_with_no_wrapper {
                   10477:     my $uri = &clutter(shift);
                   10478:     if ($uri =~ m-^/adm/-) {
                   10479: 	$uri =~ s-^/adm/wrapper/-/-;
                   10480: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   10481:     }
                   10482:     return $uri;
                   10483: }
                   10484: 
1.557     albertel 10485: sub freeze_escape {
                   10486:     my ($value)=@_;
                   10487:     if (ref($value)) {
                   10488: 	$value=&nfreeze($value);
                   10489: 	return '__FROZEN__'.&escape($value);
                   10490:     }
                   10491:     return &escape($value);
                   10492: }
                   10493: 
1.11      www      10494: 
1.557     albertel 10495: sub thaw_unescape {
                   10496:     my ($value)=@_;
                   10497:     if ($value =~ /^__FROZEN__/) {
                   10498: 	substr($value,0,10,undef);
                   10499: 	$value=&unescape($value);
                   10500: 	return &thaw($value);
                   10501:     }
                   10502:     return &unescape($value);
                   10503: }
                   10504: 
1.436     albertel 10505: sub correct_line_ends {
                   10506:     my ($result)=@_;
                   10507:     $$result =~s/\r\n/\n/mg;
                   10508:     $$result =~s/\r/\n/mg;
1.415     albertel 10509: }
1.1       albertel 10510: # ================================================================ Main Program
                   10511: 
1.184     www      10512: sub goodbye {
1.204     albertel 10513:    &logthis("Starting Shut down");
1.443     albertel 10514: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 10515:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 10516: #converted
1.599     albertel 10517: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 10518:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   10519: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   10520: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 10521: #1.1 only
1.870     albertel 10522: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   10523: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   10524: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   10525: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   10526:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 10527:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   10528:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      10529:    &flushcourselogs();
                   10530:    &logthis("Shutting down");
                   10531: }
                   10532: 
1.852     albertel 10533: sub get_dns {
1.869     albertel 10534:     my ($url,$func,$ignore_cache) = @_;
                   10535:     if (!$ignore_cache) {
                   10536: 	my ($content,$cached)=
                   10537: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   10538: 	if ($cached) {
                   10539: 	    &$func($content);
                   10540: 	    return;
                   10541: 	}
                   10542:     }
                   10543: 
                   10544:     my %alldns;
1.852     albertel 10545:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10546:     foreach my $dns (<$config>) {
                   10547: 	next if ($dns !~ /^\^(\S*)/x);
1.979     raeburn  10548:         my $line = $1;
                   10549:         my ($host,$protocol) = split(/:/,$line);
                   10550:         if ($protocol ne 'https') {
                   10551:             $protocol = 'http';
                   10552:         }
                   10553: 	$alldns{$host} = $protocol;
1.869     albertel 10554:     }
                   10555:     while (%alldns) {
                   10556: 	my ($dns) = keys(%alldns);
1.852     albertel 10557: 	my $ua=new LWP::UserAgent;
1.1134    raeburn  10558:         $ua->timeout(30);
1.979     raeburn  10559: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852     albertel 10560: 	my $response=$ua->request($request);
1.979     raeburn  10561:         delete($alldns{$dns});
1.852     albertel 10562: 	next if ($response->is_error());
                   10563: 	my @content = split("\n",$response->content);
1.869     albertel 10564: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 10565: 	&$func(\@content);
1.869     albertel 10566: 	return;
1.852     albertel 10567:     }
                   10568:     close($config);
1.871     albertel 10569:     my $which = (split('/',$url))[3];
                   10570:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   10571:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 10572:     my @content = <$config>;
                   10573:     &$func(\@content);
                   10574:     return;
1.852     albertel 10575: }
1.327     albertel 10576: # ------------------------------------------------------------ Read domain file
                   10577: {
1.852     albertel 10578:     my $loaded;
1.846     albertel 10579:     my %domain;
                   10580: 
1.852     albertel 10581:     sub parse_domain_tab {
                   10582: 	my ($lines) = @_;
                   10583: 	foreach my $line (@$lines) {
                   10584: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      10585: 
1.846     albertel 10586: 	    chomp($line);
1.852     albertel 10587: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 10588: 	    my %this_domain;
                   10589: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   10590: 			       'lang_def', 'city', 'longi', 'lati',
                   10591: 			       'primary') {
                   10592: 		$this_domain{$field} = shift(@elements);
                   10593: 	    }
                   10594: 	    $domain{$name} = \%this_domain;
1.852     albertel 10595: 	}
                   10596:     }
1.864     albertel 10597: 
                   10598:     sub reset_domain_info {
                   10599: 	undef($loaded);
                   10600: 	undef(%domain);
                   10601:     }
                   10602: 
1.852     albertel 10603:     sub load_domain_tab {
1.869     albertel 10604: 	my ($ignore_cache) = @_;
                   10605: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 10606: 	my $fh;
                   10607: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   10608: 	    my @lines = <$fh>;
                   10609: 	    &parse_domain_tab(\@lines);
1.448     albertel 10610: 	}
1.852     albertel 10611: 	close($fh);
                   10612: 	$loaded = 1;
1.327     albertel 10613:     }
1.846     albertel 10614: 
                   10615:     sub domain {
1.852     albertel 10616: 	&load_domain_tab() if (!$loaded);
                   10617: 
1.846     albertel 10618: 	my ($name,$what) = @_;
                   10619: 	return if ( !exists($domain{$name}) );
                   10620: 
                   10621: 	if (!$what) {
                   10622: 	    return $domain{$name}{'description'};
                   10623: 	}
                   10624: 	return $domain{$name}{$what};
                   10625:     }
1.974     raeburn  10626: 
                   10627:     sub domain_info {
                   10628:         &load_domain_tab() if (!$loaded);
                   10629:         return %domain;
                   10630:     }
                   10631: 
1.327     albertel 10632: }
                   10633: 
                   10634: 
1.1       albertel 10635: # ------------------------------------------------------------- Read hosts file
                   10636: {
1.838     albertel 10637:     my %hostname;
1.844     albertel 10638:     my %hostdom;
1.845     albertel 10639:     my %libserv;
1.852     albertel 10640:     my $loaded;
1.888     albertel 10641:     my %name_to_host;
1.1074    raeburn  10642:     my %internetdom;
1.1107    raeburn  10643:     my %LC_dns_serv;
1.852     albertel 10644: 
                   10645:     sub parse_hosts_tab {
                   10646: 	my ($file) = @_;
                   10647: 	foreach my $configline (@$file) {
                   10648: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  10649:             chomp($configline);
                   10650: 	    if ($configline =~ /^\^/) {
                   10651:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   10652:                     $LC_dns_serv{$1} = 1;
                   10653:                 }
                   10654:                 next;
                   10655:             }
1.1074    raeburn  10656: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 10657: 	    $name=~s/\s//g;
                   10658: 	    if ($id && $domain && $role && $name) {
                   10659: 		$hostname{$id}=$name;
1.888     albertel 10660: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 10661: 		$hostdom{$id}=$domain;
                   10662: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  10663:                 if (defined($protocol)) {
                   10664:                     if ($protocol eq 'https') {
                   10665:                         $protocol{$id} = $protocol;
                   10666:                     } else {
                   10667:                         $protocol{$id} = 'http'; 
                   10668:                     }
1.968     raeburn  10669:                 } else {
1.969     raeburn  10670:                     $protocol{$id} = 'http';
1.968     raeburn  10671:                 }
1.1074    raeburn  10672:                 if (defined($intdom)) {
                   10673:                     $internetdom{$id} = $intdom;
                   10674:                 }
1.852     albertel 10675: 	    }
                   10676: 	}
                   10677:     }
1.864     albertel 10678:     
                   10679:     sub reset_hosts_info {
1.897     albertel 10680: 	&purge_remembered();
1.864     albertel 10681: 	&reset_domain_info();
                   10682: 	&reset_hosts_ip_info();
1.892     albertel 10683: 	undef(%name_to_host);
1.864     albertel 10684: 	undef(%hostname);
                   10685: 	undef(%hostdom);
                   10686: 	undef(%libserv);
                   10687: 	undef($loaded);
                   10688:     }
1.1       albertel 10689: 
1.852     albertel 10690:     sub load_hosts_tab {
1.869     albertel 10691: 	my ($ignore_cache) = @_;
                   10692: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 10693: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10694: 	my @config = <$config>;
                   10695: 	&parse_hosts_tab(\@config);
                   10696: 	close($config);
                   10697: 	$loaded=1;
1.1       albertel 10698:     }
1.852     albertel 10699: 
1.838     albertel 10700:     sub hostname {
1.852     albertel 10701: 	&load_hosts_tab() if (!$loaded);
                   10702: 
1.838     albertel 10703: 	my ($lonid) = @_;
                   10704: 	return $hostname{$lonid};
                   10705:     }
1.845     albertel 10706: 
1.838     albertel 10707:     sub all_hostnames {
1.852     albertel 10708: 	&load_hosts_tab() if (!$loaded);
                   10709: 
1.838     albertel 10710: 	return %hostname;
                   10711:     }
1.845     albertel 10712: 
1.888     albertel 10713:     sub all_names {
                   10714: 	&load_hosts_tab() if (!$loaded);
                   10715: 
                   10716: 	return %name_to_host;
                   10717:     }
                   10718: 
1.974     raeburn  10719:     sub all_host_domain {
                   10720:         &load_hosts_tab() if (!$loaded);
                   10721:         return %hostdom;
                   10722:     }
                   10723: 
1.845     albertel 10724:     sub is_library {
1.852     albertel 10725: 	&load_hosts_tab() if (!$loaded);
                   10726: 
1.845     albertel 10727: 	return exists($libserv{$_[0]});
                   10728:     }
                   10729: 
                   10730:     sub all_library {
1.852     albertel 10731: 	&load_hosts_tab() if (!$loaded);
                   10732: 
1.845     albertel 10733: 	return %libserv;
                   10734:     }
                   10735: 
1.1062    droeschl 10736:     sub unique_library {
                   10737: 	#2x reverse removes all hostnames that appear more than once
                   10738:         my %unique = reverse &all_library();
                   10739:         return reverse %unique;
                   10740:     }
                   10741: 
1.841     albertel 10742:     sub get_servers {
1.852     albertel 10743: 	&load_hosts_tab() if (!$loaded);
                   10744: 
1.841     albertel 10745: 	my ($domain,$type) = @_;
                   10746: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   10747: 	                                          : %hostname;
                   10748: 	my %result;
1.842     albertel 10749: 	if (ref($domain) eq 'ARRAY') {
                   10750: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 10751: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 10752: 		    $result{$host} = $hostname;
                   10753: 		}
                   10754: 	    }
                   10755: 	} else {
                   10756: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   10757: 		if ($hostdom{$host} eq $domain) {
                   10758: 		    $result{$host} = $hostname;
                   10759: 		}
1.841     albertel 10760: 	    }
                   10761: 	}
                   10762: 	return %result;
                   10763:     }
1.845     albertel 10764: 
1.1062    droeschl 10765:     sub get_unique_servers {
                   10766:         my %unique = reverse &get_servers(@_);
                   10767: 	return reverse %unique;
                   10768:     }
                   10769: 
1.844     albertel 10770:     sub host_domain {
1.852     albertel 10771: 	&load_hosts_tab() if (!$loaded);
                   10772: 
1.844     albertel 10773: 	my ($lonid) = @_;
                   10774: 	return $hostdom{$lonid};
                   10775:     }
                   10776: 
1.841     albertel 10777:     sub all_domains {
1.852     albertel 10778: 	&load_hosts_tab() if (!$loaded);
                   10779: 
1.841     albertel 10780: 	my %seen;
                   10781: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   10782: 	return @uniq;
                   10783:     }
1.1074    raeburn  10784: 
                   10785:     sub internet_dom {
                   10786:         &load_hosts_tab() if (!$loaded);
                   10787: 
                   10788:         my ($lonid) = @_;
                   10789:         return $internetdom{$lonid};
                   10790:     }
1.1107    raeburn  10791: 
                   10792:     sub is_LC_dns {
                   10793:         &load_hosts_tab() if (!$loaded);
                   10794: 
                   10795:         my ($hostname) = @_;
                   10796:         return exists($LC_dns_serv{$hostname});
                   10797:     }
                   10798: 
1.1       albertel 10799: }
                   10800: 
1.847     albertel 10801: { 
                   10802:     my %iphost;
1.856     albertel 10803:     my %name_to_ip;
                   10804:     my %lonid_to_ip;
1.869     albertel 10805: 
1.847     albertel 10806:     sub get_hosts_from_ip {
                   10807: 	my ($ip) = @_;
                   10808: 	my %iphosts = &get_iphost();
                   10809: 	if (ref($iphosts{$ip})) {
                   10810: 	    return @{$iphosts{$ip}};
                   10811: 	}
                   10812: 	return;
1.839     albertel 10813:     }
1.864     albertel 10814:     
                   10815:     sub reset_hosts_ip_info {
                   10816: 	undef(%iphost);
                   10817: 	undef(%name_to_ip);
                   10818: 	undef(%lonid_to_ip);
                   10819:     }
1.856     albertel 10820: 
                   10821:     sub get_host_ip {
                   10822: 	my ($lonid) = @_;
                   10823: 	if (exists($lonid_to_ip{$lonid})) {
                   10824: 	    return $lonid_to_ip{$lonid};
                   10825: 	}
                   10826: 	my $name=&hostname($lonid);
                   10827:    	my $ip = gethostbyname($name);
                   10828: 	return if (!$ip || length($ip) ne 4);
                   10829: 	$ip=inet_ntoa($ip);
                   10830: 	$name_to_ip{$name}   = $ip;
                   10831: 	$lonid_to_ip{$lonid} = $ip;
                   10832: 	return $ip;
                   10833:     }
1.847     albertel 10834:     
                   10835:     sub get_iphost {
1.869     albertel 10836: 	my ($ignore_cache) = @_;
1.894     albertel 10837: 
1.869     albertel 10838: 	if (!$ignore_cache) {
                   10839: 	    if (%iphost) {
                   10840: 		return %iphost;
                   10841: 	    }
                   10842: 	    my ($ip_info,$cached)=
                   10843: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   10844: 	    if ($cached) {
                   10845: 		%iphost      = %{$ip_info->[0]};
                   10846: 		%name_to_ip  = %{$ip_info->[1]};
                   10847: 		%lonid_to_ip = %{$ip_info->[2]};
                   10848: 		return %iphost;
                   10849: 	    }
                   10850: 	}
1.894     albertel 10851: 
                   10852: 	# get yesterday's info for fallback
                   10853: 	my %old_name_to_ip;
                   10854: 	my ($ip_info,$cached)=
                   10855: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   10856: 	if ($cached) {
                   10857: 	    %old_name_to_ip = %{$ip_info->[1]};
                   10858: 	}
                   10859: 
1.888     albertel 10860: 	my %name_to_host = &all_names();
                   10861: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 10862: 	    my $ip;
                   10863: 	    if (!exists($name_to_ip{$name})) {
                   10864: 		$ip = gethostbyname($name);
                   10865: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 10866: 		    if (defined($old_name_to_ip{$name})) {
                   10867: 			$ip = $old_name_to_ip{$name};
                   10868: 			&logthis("Can't find $name defaulting to old $ip");
                   10869: 		    } else {
                   10870: 			&logthis("Name $name no IP found");
                   10871: 			next;
                   10872: 		    }
                   10873: 		} else {
                   10874: 		    $ip=inet_ntoa($ip);
1.847     albertel 10875: 		}
                   10876: 		$name_to_ip{$name} = $ip;
                   10877: 	    } else {
                   10878: 		$ip = $name_to_ip{$name};
1.653     albertel 10879: 	    }
1.888     albertel 10880: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   10881: 		$lonid_to_ip{$id} = $ip;
                   10882: 	    }
                   10883: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 10884: 	}
1.869     albertel 10885: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   10886: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 10887: 				      48*60*60);
1.869     albertel 10888: 
1.847     albertel 10889: 	return %iphost;
1.598     albertel 10890:     }
                   10891: 
1.992     raeburn  10892:     #
                   10893:     #  Given a DNS returns the loncapa host name for that DNS 
                   10894:     # 
                   10895:     sub host_from_dns {
                   10896:         my ($dns) = @_;
                   10897:         my @hosts;
                   10898:         my $ip;
                   10899: 
1.993     raeburn  10900:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  10901:             $ip = $name_to_ip{$dns};
                   10902:         }
                   10903:         if (!$ip) {
                   10904:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   10905:             if (length($ip) == 4) { 
                   10906: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   10907:             }
                   10908:         }
                   10909:         if ($ip) {
                   10910: 	    @hosts = get_hosts_from_ip($ip);
                   10911: 	    return $hosts[0];
                   10912:         }
                   10913:         return undef;
1.986     foxr     10914:     }
1.992     raeburn  10915: 
1.1074    raeburn  10916:     sub get_internet_names {
                   10917:         my ($lonid) = @_;
                   10918:         return if ($lonid eq '');
                   10919:         my ($idnref,$cached)=
                   10920:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   10921:         if ($cached) {
                   10922:             return $idnref;
                   10923:         }
                   10924:         my $ip = &get_host_ip($lonid);
                   10925:         my @hosts = &get_hosts_from_ip($ip);
                   10926:         my %iphost = &get_iphost();
                   10927:         my (@idns,%seen);
                   10928:         foreach my $id (@hosts) {
                   10929:             my $dom = &host_domain($id);
                   10930:             my $prim_id = &domain($dom,'primary');
                   10931:             my $prim_ip = &get_host_ip($prim_id);
                   10932:             next if ($seen{$prim_ip});
                   10933:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   10934:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   10935:                     my $intdom = &internet_dom($id);
                   10936:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   10937:                         push(@idns,$intdom);
                   10938:                     }
                   10939:                 }
                   10940:             }
                   10941:             $seen{$prim_ip} = 1;
                   10942:         }
                   10943:         return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
                   10944:     }
                   10945: 
1.986     foxr     10946: }
                   10947: 
1.1079    raeburn  10948: sub all_loncaparevs {
                   10949:     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);
                   10950: }
                   10951: 
1.862     albertel 10952: BEGIN {
                   10953: 
                   10954: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   10955:     unless ($readit) {
                   10956: {
                   10957:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   10958:     %perlvar = (%perlvar,%{$configvars});
                   10959: }
                   10960: 
                   10961: 
1.1       albertel 10962: # ------------------------------------------------------ Read spare server file
                   10963: {
1.448     albertel 10964:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 10965: 
                   10966:     while (my $configline=<$config>) {
                   10967:        chomp($configline);
1.284     matthew  10968:        if ($configline) {
1.784     albertel 10969: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 10970: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 10971: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 10972:        }
                   10973:     }
1.448     albertel 10974:     close($config);
1.1       albertel 10975: }
1.11      www      10976: # ------------------------------------------------------------ Read permissions
                   10977: {
1.448     albertel 10978:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      10979: 
                   10980:     while (my $configline=<$config>) {
1.448     albertel 10981: 	chomp($configline);
                   10982: 	if ($configline) {
                   10983: 	    my ($role,$perm)=split(/ /,$configline);
                   10984: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   10985: 	}
1.11      www      10986:     }
1.448     albertel 10987:     close($config);
1.11      www      10988: }
                   10989: 
                   10990: # -------------------------------------------- Read plain texts for permissions
                   10991: {
1.448     albertel 10992:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      10993: 
                   10994:     while (my $configline=<$config>) {
1.448     albertel 10995: 	chomp($configline);
                   10996: 	if ($configline) {
1.742     raeburn  10997: 	    my ($short,@plain)=split(/:/,$configline);
                   10998:             %{$prp{$short}} = ();
                   10999: 	    if (@plain > 0) {
                   11000:                 $prp{$short}{'std'} = $plain[0];
                   11001:                 for (my $i=1; $i<@plain; $i++) {
                   11002:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   11003:                 }
                   11004:             }
1.448     albertel 11005: 	}
1.135     www      11006:     }
1.448     albertel 11007:     close($config);
1.135     www      11008: }
                   11009: 
                   11010: # ---------------------------------------------------------- Read package table
                   11011: {
1.448     albertel 11012:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      11013: 
                   11014:     while (my $configline=<$config>) {
1.483     albertel 11015: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 11016: 	chomp($configline);
                   11017: 	my ($short,$plain)=split(/:/,$configline);
                   11018: 	my ($pack,$name)=split(/\&/,$short);
                   11019: 	if ($plain ne '') {
                   11020: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   11021: 	    $packagetab{$short}=$plain; 
                   11022: 	}
1.11      www      11023:     }
1.448     albertel 11024:     close($config);
1.329     matthew  11025: }
                   11026: 
1.1073    raeburn  11027: # ---------------------------------------------------------- Read loncaparev table
                   11028: {
                   11029:     if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
                   11030:         if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   11031:             while (my $configline=<$config>) {
                   11032:                 chomp($configline);
                   11033:                 my ($hostid,$loncaparev)=split(/:/,$configline);
                   11034:                 $loncaparevs{$hostid}=$loncaparev;
                   11035:             }
                   11036:             close($config);
                   11037:         }
                   11038:     }
                   11039: }
                   11040: 
1.1074    raeburn  11041: # ---------------------------------------------------------- Read serverhostID table
                   11042: {
                   11043:     if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   11044:         if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   11045:             while (my $configline=<$config>) {
                   11046:                 chomp($configline);
                   11047:                 my ($name,$id)=split(/:/,$configline);
                   11048:                 $serverhomeIDs{$name}=$id;
                   11049:             }
                   11050:             close($config);
                   11051:         }
                   11052:     }
                   11053: }
                   11054: 
1.1079    raeburn  11055: {
                   11056:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   11057:     if (-e $file) {
                   11058:         my $parser = HTML::LCParser->new($file);
                   11059:         while (my $token = $parser->get_token()) {
                   11060:             if ($token->[0] eq 'S') {
                   11061:                 my $item = $token->[1];
                   11062:                 my $name = $token->[2]{'name'};
                   11063:                 my $value = $token->[2]{'value'};
                   11064:                 if ($item ne '' && $name ne '' && $value ne '') {
                   11065:                     my $release = $parser->get_text();
                   11066:                     $release =~ s/(^\s*|\s*$ )//gx;
                   11067:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
                   11068:                 }
                   11069:             }
                   11070:         }
                   11071:     }
1.1073    raeburn  11072: }
                   11073: 
1.1138    raeburn  11074: # ---------------------------------------------------------- Read managers table
                   11075: {
                   11076:     if (-e "$perlvar{'lonTabDir'}/managers.tab") {
                   11077:         if (open(my $config,"<$perlvar{'lonTabDir'}/managers.tab")) {
                   11078:             while (my $configline=<$config>) {
                   11079:                 chomp($configline);
                   11080:                 next if ($configline =~ /^\#/);
                   11081:                 if (($configline =~ /^[\w\-]+$/) || ($configline =~ /^[\w\-]+\:[\w\-]+$/)) {
                   11082:                     $managerstab{$configline} = 1;
                   11083:                 }
                   11084:             }
                   11085:             close($config);
                   11086:         }
                   11087:     }
                   11088: }
                   11089: 
1.329     matthew  11090: # ------------- set up temporary directory
                   11091: {
1.1117    foxr     11092:     $tmpdir = LONCAPA::tempdir();
1.329     matthew  11093: 
1.11      www      11094: }
                   11095: 
1.794     albertel 11096: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   11097: 				'compress_threshold'=> 20_000,
                   11098:  			        });
1.185     www      11099: 
1.281     www      11100: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      11101: $dumpcount=0;
1.958     www      11102: $locknum=0;
1.22      www      11103: 
1.163     harris41 11104: &logtouch();
1.672     albertel 11105: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      11106: $readit=1;
1.564     albertel 11107:     {
                   11108: 	use integer;
                   11109: 	my $test=(2**32)+1;
1.568     albertel 11110: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 11111: 	&logthis(" Detected 64bit platform ($_64bit)");
                   11112:     }
1.195     www      11113: }
1.1       albertel 11114: }
1.179     www      11115: 
1.1       albertel 11116: 1;
1.191     harris41 11117: __END__
                   11118: 
1.243     albertel 11119: =pod
                   11120: 
1.191     harris41 11121: =head1 NAME
                   11122: 
1.243     albertel 11123: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 11124: 
                   11125: =head1 SYNOPSIS
                   11126: 
1.243     albertel 11127: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 11128: 
                   11129:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   11130: 
1.243     albertel 11131: Common parameters:
                   11132: 
                   11133: =over 4
                   11134: 
                   11135: =item *
                   11136: 
                   11137: $uname : an internal username (if $cname expecting a course Id specifically)
                   11138: 
                   11139: =item *
                   11140: 
                   11141: $udom : a domain (if $cdom expecting a course's domain specifically)
                   11142: 
                   11143: =item *
                   11144: 
                   11145: $symb : a resource instance identifier
                   11146: 
                   11147: =item *
                   11148: 
                   11149: $namespace : the name of a .db file that contains the data needed or
                   11150: being set.
                   11151: 
                   11152: =back
                   11153: 
1.394     bowersj2 11154: =head1 OVERVIEW
1.191     harris41 11155: 
1.394     bowersj2 11156: lonnet provides subroutines which interact with the
                   11157: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   11158: about classes, users, and resources.
1.243     albertel 11159: 
                   11160: For many of these objects you can also use this to store data about
                   11161: them or modify them in various ways.
1.191     harris41 11162: 
1.394     bowersj2 11163: =head2 Symbs
1.191     harris41 11164: 
1.394     bowersj2 11165: To identify a specific instance of a resource, LON-CAPA uses symbols
                   11166: or "symbs"X<symb>. These identifiers are built from the URL of the
                   11167: map, the resource number of the resource in the map, and the URL of
                   11168: the resource itself. The latter is somewhat redundant, but might help
                   11169: if maps change.
                   11170: 
                   11171: An example is
                   11172: 
                   11173:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   11174: 
                   11175: The respective map entry is
                   11176: 
                   11177:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   11178:   title="Problem 2">
                   11179:  </resource>
                   11180: 
                   11181: Symbs are used by the random number generator, as well as to store and
                   11182: restore data specific to a certain instance of for example a problem.
                   11183: 
                   11184: =head2 Storing And Retrieving Data
                   11185: 
                   11186: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   11187: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   11188: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   11189: is is the non-critical message twin of cstore. These functions are for
                   11190: handlers to store a perl hash to a user's permanent data space in an
                   11191: easy manner, and to retrieve it again on another call. It is expected
                   11192: that a handler would use this once at the beginning to retrieve data,
                   11193: and then again once at the end to send only the new data back.
                   11194: 
                   11195: The data is stored in the user's data directory on the user's
                   11196: homeserver under the ID of the course.
                   11197: 
                   11198: The hash that is returned by restore will have all of the previous
                   11199: value for all of the elements of the hash.
                   11200: 
                   11201: Example:
                   11202: 
                   11203:  #creating a hash
                   11204:  my %hash;
                   11205:  $hash{'foo'}='bar';
                   11206: 
                   11207:  #storing it
                   11208:  &Apache::lonnet::cstore(\%hash);
                   11209: 
                   11210:  #changing a value
                   11211:  $hash{'foo'}='notbar';
                   11212: 
                   11213:  #adding a new value
                   11214:  $hash{'bar'}='foo';
                   11215:  &Apache::lonnet::cstore(\%hash);
                   11216: 
                   11217:  #retrieving the hash
                   11218:  my %history=&Apache::lonnet::restore();
                   11219: 
                   11220:  #print the hash
                   11221:  foreach my $key (sort(keys(%history))) {
                   11222:    print("\%history{$key} = $history{$key}");
                   11223:  }
                   11224: 
                   11225: Will print out:
1.191     harris41 11226: 
1.394     bowersj2 11227:  %history{1:foo} = bar
                   11228:  %history{1:keys} = foo:timestamp
                   11229:  %history{1:timestamp} = 990455579
                   11230:  %history{2:bar} = foo
                   11231:  %history{2:foo} = notbar
                   11232:  %history{2:keys} = foo:bar:timestamp
                   11233:  %history{2:timestamp} = 990455580
                   11234:  %history{bar} = foo
                   11235:  %history{foo} = notbar
                   11236:  %history{timestamp} = 990455580
                   11237:  %history{version} = 2
                   11238: 
                   11239: Note that the special hash entries C<keys>, C<version> and
                   11240: C<timestamp> were added to the hash. C<version> will be equal to the
                   11241: total number of versions of the data that have been stored. The
                   11242: C<timestamp> attribute will be the UNIX time the hash was
                   11243: stored. C<keys> is available in every historical section to list which
                   11244: keys were added or changed at a specific historical revision of a
                   11245: hash.
                   11246: 
                   11247: B<Warning>: do not store the hash that restore returns directly. This
                   11248: will cause a mess since it will restore the historical keys as if the
                   11249: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 11250: 
1.394     bowersj2 11251: Calling convention:
1.191     harris41 11252: 
1.394     bowersj2 11253:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   11254:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 11255: 
1.394     bowersj2 11256: For more detailed information, see lonnet specific documentation.
1.191     harris41 11257: 
1.394     bowersj2 11258: =head1 RETURN MESSAGES
1.191     harris41 11259: 
1.394     bowersj2 11260: =over 4
1.191     harris41 11261: 
1.394     bowersj2 11262: =item * B<con_lost>: unable to contact remote host
1.191     harris41 11263: 
1.394     bowersj2 11264: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   11265: when the connection is brought back up
1.191     harris41 11266: 
1.394     bowersj2 11267: =item * B<con_failed>: unable to contact remote host and unable to save message
                   11268: for later delivery
1.191     harris41 11269: 
1.967     bisitz   11270: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 11271: 
1.394     bowersj2 11272: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 11273: that was requested
1.191     harris41 11274: 
1.243     albertel 11275: =back
1.191     harris41 11276: 
1.243     albertel 11277: =head1 PUBLIC SUBROUTINES
1.191     harris41 11278: 
1.243     albertel 11279: =head2 Session Environment Functions
1.191     harris41 11280: 
1.243     albertel 11281: =over 4
1.191     harris41 11282: 
1.394     bowersj2 11283: =item * 
                   11284: X<appenv()>
1.949     raeburn  11285: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 11286: the user envirnoment file, and will be restored for each access this
1.620     albertel 11287: user makes during this session, also modifies the %env for the current
1.949     raeburn  11288: process. Optional rolesarrayref - if defined contains a reference to an array
                   11289: of roles which are exempt from the restriction on modifying user.role entries 
                   11290: in the user's environment.db and in %env.    
1.191     harris41 11291: 
                   11292: =item *
1.394     bowersj2 11293: X<delenv()>
1.987     raeburn  11294: B<delenv($delthis,$regexp)>: removes all items from the session
                   11295: environment file that begin with $delthis. If the 
                   11296: optional second arg - $regexp - is true, $delthis is treated as a 
                   11297: regular expression, otherwise \Q$delthis\E is used. 
                   11298: The values are also deleted from the current processes %env.
1.191     harris41 11299: 
1.795     albertel 11300: =item * get_env_multiple($name) 
                   11301: 
                   11302: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   11303: values may be defined and end up as an array ref.
                   11304: 
                   11305: returns an array of values
                   11306: 
1.243     albertel 11307: =back
                   11308: 
                   11309: =head2 User Information
1.191     harris41 11310: 
1.243     albertel 11311: =over 4
1.191     harris41 11312: 
                   11313: =item *
1.394     bowersj2 11314: X<queryauthenticate()>
                   11315: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 11316: authentication scheme
                   11317: 
                   11318: =item *
1.394     bowersj2 11319: X<authenticate()>
1.1073    raeburn  11320: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 11321: authenticate user from domain's lib servers (first use the current
                   11322: one). C<$upass> should be the users password.
1.1073    raeburn  11323: $checkdefauth is optional (value is 1 if a check should be made to
                   11324:    authenticate user using default authentication method, and allow
                   11325:    account creation if username does not have account in the domain).
                   11326: $clientcancheckhost is optional (value is 1 if checking whether the
                   11327:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 11328: 
                   11329: =item *
1.394     bowersj2 11330: X<homeserver()>
                   11331: B<homeserver($uname,$udom)>: find the server which has
                   11332: the user's directory and files (there must be only one), this caches
                   11333: the answer, and also caches if there is a borken connection.
1.191     harris41 11334: 
                   11335: =item *
1.394     bowersj2 11336: X<idget()>
                   11337: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   11338: (IDs are a unique resource in a domain, there must be only 1 ID per
                   11339: username, and only 1 username per ID in a specific domain) (returns
                   11340: hash: id=>name,id=>name)
1.191     harris41 11341: 
                   11342: =item *
1.394     bowersj2 11343: X<idrget()>
                   11344: B<idrget($udom,@unames)>: find the IDs behind a list of
                   11345: usernames (returns hash: name=>id,name=>id)
1.191     harris41 11346: 
                   11347: =item *
1.394     bowersj2 11348: X<idput()>
                   11349: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 11350: 
                   11351: =item *
1.394     bowersj2 11352: X<rolesinit()>
                   11353: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 11354: 
                   11355: =item *
1.551     albertel 11356: X<getsection()>
                   11357: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 11358: course $cname, return section name/number or '' for "not in course"
                   11359: and '-1' for "no section"
                   11360: 
                   11361: =item *
1.394     bowersj2 11362: X<userenvironment()>
                   11363: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 11364: passed in @what from the requested user's environment, returns a hash
                   11365: 
1.858     raeburn  11366: =item * 
                   11367: X<userlog_query()>
1.859     albertel 11368: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   11369: activity.log file. %filters defines filters applied when parsing the
                   11370: log file. These can be start or end timestamps, or the type of action
                   11371: - log to look for Login or Logout events, check for Checkin or
                   11372: Checkout, role for role selection. The response is in the form
                   11373: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   11374: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  11375: 
1.243     albertel 11376: =back
                   11377: 
                   11378: =head2 User Roles
                   11379: 
                   11380: =over 4
                   11381: 
                   11382: =item *
                   11383: 
1.810     raeburn  11384: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 11385:  F: full access
                   11386:  U,I,K: authentication modes (cxx only)
                   11387:  '': forbidden
                   11388:  1: user needs to choose course
                   11389:  2: browse allowed
1.766     albertel 11390:  A: passphrase authentication needed
1.243     albertel 11391: 
                   11392: =item *
                   11393: 
                   11394: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   11395: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   11396: and course level
                   11397: 
                   11398: =item *
                   11399: 
1.988     raeburn  11400: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   11401: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  11402: $type is Course (default) or Community.
1.988     raeburn  11403: If $forcedefault evaluates to true, text returned will be default 
                   11404: text for $type. Otherwise, if this is a course, the text returned 
                   11405: will be a custom name for the role (if defined in the course's 
                   11406: environment).  If no custom name is defined the default is returned.
                   11407:    
1.832     raeburn  11408: =item *
                   11409: 
1.935     raeburn  11410: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858     raeburn  11411: All arguments are optional. Returns a hash of a roles, either for
                   11412: co-author/assistant author roles for a user's Construction Space
1.906     albertel 11413: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  11414: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   11415: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   11416: For each key, value is set to colon-separated start and end times for
                   11417: the role.  If no username and domain are specified, will default to
1.934     raeburn  11418: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  11419: of role statuses (active, future or previous), roles 
                   11420: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   11421: to restrict the list of roles reported. If no array ref is 
                   11422: provided for types, will default to return only active roles.
1.834     albertel 11423: 
1.243     albertel 11424: =back
                   11425: 
                   11426: =head2 User Modification
                   11427: 
                   11428: =over 4
                   11429: 
                   11430: =item *
                   11431: 
1.957     raeburn  11432: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 11433: user for the level given by URL.  Optional start and end dates (leave empty
                   11434: string or zero for "no date")
1.191     harris41 11435: 
                   11436: =item *
                   11437: 
1.243     albertel 11438: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   11439: change a users, password, possible return values are: ok,
                   11440: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   11441: refused
1.191     harris41 11442: 
                   11443: =item *
                   11444: 
1.243     albertel 11445: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 11446: 
                   11447: =item *
                   11448: 
1.1058    raeburn  11449: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   11450:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   11451: 
                   11452: will update user information (firstname,middlename,lastname,generation,
                   11453: permanentemail), and if forceid is true, student/employee ID also.
                   11454: A user's institutional affiliation(s) can also be updated.
                   11455: User information fields will not be overwritten with empty entries 
                   11456: unless the field is included in the $candelete array reference.
                   11457: This array is included when a single user is modified via "Manage Users",
                   11458: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 11459: 
                   11460: =item *
                   11461: 
1.286     matthew  11462: modifystudent
                   11463: 
1.957     raeburn  11464: modify a student's enrollment and identification information.
1.286     matthew  11465: The course id is resolved based on the current users environment.  
                   11466: This means the envoking user must be a course coordinator or otherwise
                   11467: associated with a course.
                   11468: 
1.297     matthew  11469: This call is essentially a wrapper for lonnet::modifyuser and
                   11470: lonnet::modify_student_enrollment
1.286     matthew  11471: 
                   11472: Inputs: 
                   11473: 
                   11474: =over 4
                   11475: 
1.957     raeburn  11476: =item B<$udom> Student's loncapa domain
1.286     matthew  11477: 
1.957     raeburn  11478: =item B<$uname> Student's loncapa login name
1.286     matthew  11479: 
1.964     bisitz   11480: =item B<$uid> Student/Employee ID
1.286     matthew  11481: 
1.957     raeburn  11482: =item B<$umode> Student's authentication mode
1.286     matthew  11483: 
1.957     raeburn  11484: =item B<$upass> Student's password
1.286     matthew  11485: 
1.957     raeburn  11486: =item B<$first> Student's first name
1.286     matthew  11487: 
1.957     raeburn  11488: =item B<$middle> Student's middle name
1.286     matthew  11489: 
1.957     raeburn  11490: =item B<$last> Student's last name
1.286     matthew  11491: 
1.957     raeburn  11492: =item B<$gene> Student's generation
1.286     matthew  11493: 
1.957     raeburn  11494: =item B<$usec> Student's section in course
1.286     matthew  11495: 
                   11496: =item B<$end> Unix time of the roles expiration
                   11497: 
                   11498: =item B<$start> Unix time of the roles start date
                   11499: 
                   11500: =item B<$forceid> If defined, allow $uid to be changed
                   11501: 
                   11502: =item B<$desiredhome> server to use as home server for student
                   11503: 
1.957     raeburn  11504: =item B<$email> Student's permanent e-mail address
                   11505: 
                   11506: =item B<$type> Type of enrollment (auto or manual)
                   11507: 
1.963     raeburn  11508: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   11509: 
                   11510: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  11511: 
1.963     raeburn  11512: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  11513: 
1.963     raeburn  11514: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  11515: 
1.963     raeburn  11516: =item B<$inststatus> institutional status of user - : separated string of escaped status types  
1.957     raeburn  11517: 
1.286     matthew  11518: =back
1.297     matthew  11519: 
                   11520: =item *
                   11521: 
                   11522: modify_student_enrollment
                   11523: 
                   11524: Change a students enrollment status in a class.  The environment variable
                   11525: 'role.request.course' must be defined for this function to proceed.
                   11526: 
                   11527: Inputs:
                   11528: 
                   11529: =over 4
                   11530: 
                   11531: =item $udom, students domain
                   11532: 
                   11533: =item $uname, students name
                   11534: 
                   11535: =item $uid, students user id
                   11536: 
                   11537: =item $first, students first name
                   11538: 
                   11539: =item $middle
                   11540: 
                   11541: =item $last
                   11542: 
                   11543: =item $gene
                   11544: 
                   11545: =item $usec
                   11546: 
                   11547: =item $end
                   11548: 
                   11549: =item $start
                   11550: 
1.957     raeburn  11551: =item $type
                   11552: 
                   11553: =item $locktype
                   11554: 
                   11555: =item $cid
                   11556: 
                   11557: =item $selfenroll
                   11558: 
                   11559: =item $context
                   11560: 
1.297     matthew  11561: =back
                   11562: 
1.191     harris41 11563: 
                   11564: =item *
                   11565: 
1.243     albertel 11566: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   11567: custom role; give a custom role to a user for the level given by URL.  Specify
                   11568: name and domain of role author, and role name
1.191     harris41 11569: 
                   11570: =item *
                   11571: 
1.243     albertel 11572: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 11573: 
                   11574: =item *
                   11575: 
1.243     albertel 11576: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   11577: 
                   11578: =back
                   11579: 
                   11580: =head2 Course Infomation
                   11581: 
                   11582: =over 4
1.191     harris41 11583: 
                   11584: =item *
                   11585: 
1.1118    foxr     11586: coursedescription($courseid,$options) : returns a hash of information about the
1.631     albertel 11587: specified course id, including all environment settings for the
                   11588: course, the description of the course will be in the hash under the
                   11589: key 'description'
1.191     harris41 11590: 
1.1118    foxr     11591: $options is an optional parameter that if supplied is a hash reference that controls
                   11592: what how this function works.  It has the following key/values:
                   11593: 
                   11594: =over 4
                   11595: 
                   11596: =item freshen_cache
                   11597: 
                   11598: If defined, and the environment cache for the course is valid, it is 
                   11599: returned in the returned hash.
                   11600: 
                   11601: =item one_time
                   11602: 
                   11603: If defined, the last cache time is set to _now_
                   11604: 
                   11605: =item user
                   11606: 
                   11607: If defined, the supplied username is used instead of the current user.
                   11608: 
                   11609: 
                   11610: =back
                   11611: 
1.191     harris41 11612: =item *
                   11613: 
1.624     albertel 11614: resdata($name,$domain,$type,@which) : request for current parameter
                   11615: setting for a specific $type, where $type is either 'course' or 'user',
                   11616: @what should be a list of parameters to ask about. This routine caches
                   11617: answers for 5 minutes.
1.243     albertel 11618: 
1.877     foxr     11619: =item *
                   11620: 
                   11621: get_courseresdata($courseid, $domain) : dump the entire course resource
                   11622: data base, returning a hash that is keyed by the resource name and has
                   11623: values that are the resource value.  I believe that the timestamps and
                   11624: versions are also returned.
                   11625: 
                   11626: 
1.243     albertel 11627: =back
                   11628: 
                   11629: =head2 Course Modification
                   11630: 
                   11631: =over 4
1.191     harris41 11632: 
                   11633: =item *
                   11634: 
1.243     albertel 11635: writecoursepref($courseid,%prefs) : write preferences (environment
                   11636: database) for a course
1.191     harris41 11637: 
                   11638: =item *
                   11639: 
1.1011    raeburn  11640: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   11641: 
                   11642: =item *
                   11643: 
1.1038    raeburn  11644: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 11645: 
                   11646: =back
                   11647: 
                   11648: =head2 Resource Subroutines
                   11649: 
                   11650: =over 4
1.191     harris41 11651: 
                   11652: =item *
                   11653: 
1.243     albertel 11654: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 11655: 
                   11656: =item *
                   11657: 
1.243     albertel 11658: repcopy($filename) : subscribes to the requested file, and attempts to
                   11659: replicate from the owning library server, Might return
1.607     raeburn  11660: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   11661: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 11662: resource. Expects the local filesystem pathname
                   11663: (/home/httpd/html/res/....)
                   11664: 
                   11665: =back
                   11666: 
                   11667: =head2 Resource Information
                   11668: 
                   11669: =over 4
1.191     harris41 11670: 
                   11671: =item *
                   11672: 
1.243     albertel 11673: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   11674: a vairety of different possible values, $varname should be a request
                   11675: string, and the other parameters can be used to specify who and what
                   11676: one is asking about.
                   11677: 
                   11678: Possible values for $varname are environment.lastname (or other item
                   11679: from the envirnment hash), user.name (or someother aspect about the
                   11680: user), resource.0.maxtries (or some other part and parameter of a
                   11681: resource)
1.204     albertel 11682: 
                   11683: =item *
                   11684: 
1.243     albertel 11685: directcondval($number) : get current value of a condition; reads from a state
                   11686: string
1.204     albertel 11687: 
                   11688: =item *
                   11689: 
1.243     albertel 11690: condval($condidx) : value of condition index based on state
1.204     albertel 11691: 
                   11692: =item *
                   11693: 
1.243     albertel 11694: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   11695: resource's metadata, $what should be either a specific key, or either
                   11696: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   11697: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   11698: 
                   11699: this function automatically caches all requests
1.191     harris41 11700: 
                   11701: =item *
                   11702: 
1.243     albertel 11703: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   11704: network of library servers; returns file handle of where SQL and regex results
                   11705: will be stored for query
1.191     harris41 11706: 
                   11707: =item *
                   11708: 
1.243     albertel 11709: symbread($filename) : return symbolic list entry (filename argument optional);
                   11710: returns the data handle
1.191     harris41 11711: 
                   11712: =item *
                   11713: 
1.243     albertel 11714: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 11715: a possible symb for the URL in $thisfn, and if is an encryypted
                   11716: resource that the user accessed using /enc/ returns a 1 on success, 0
                   11717: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 11718: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 11719: 
1.191     harris41 11720: 
                   11721: =item *
                   11722: 
1.243     albertel 11723: symbclean($symb) : removes versions numbers from a symb, returns the
                   11724: cleaned symb
1.191     harris41 11725: 
                   11726: =item *
                   11727: 
1.243     albertel 11728: is_on_map($uri) : checks if the $uri is somewhere on the current
                   11729: course map, user must be in a course for it to work.
1.191     harris41 11730: 
                   11731: =item *
                   11732: 
1.243     albertel 11733: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 11734: 
                   11735: =item *
                   11736: 
1.243     albertel 11737: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   11738: a random seed, all arguments are optional, if they aren't sent it uses the
                   11739: environment to derive them. Note: if symb isn't sent and it can't get one
                   11740: from &symbread it will use the current time as its return value
1.191     harris41 11741: 
                   11742: =item *
                   11743: 
1.243     albertel 11744: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   11745: unfakeable, receipt
1.191     harris41 11746: 
                   11747: =item *
                   11748: 
1.620     albertel 11749: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 11750: 
                   11751: =item *
                   11752: 
1.243     albertel 11753: countacc($url) : count the number of accesses to a given URL
1.191     harris41 11754: 
                   11755: =item *
                   11756: 
1.243     albertel 11757: 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 11758: 
                   11759: =item *
                   11760: 
1.243     albertel 11761: 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 11762: 
                   11763: =item *
                   11764: 
1.243     albertel 11765: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 11766: 
                   11767: =item *
                   11768: 
1.243     albertel 11769: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   11770: forcing spreadsheet to reevaluate the resource scores next time.
                   11771: 
                   11772: =back
                   11773: 
                   11774: =head2 Storing/Retreiving Data
                   11775: 
                   11776: =over 4
1.191     harris41 11777: 
                   11778: =item *
                   11779: 
1.243     albertel 11780: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   11781: for this url; hashref needs to be given and should be a \%hashname; the
                   11782: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 11783: be derived from the env
1.191     harris41 11784: 
                   11785: =item *
                   11786: 
1.243     albertel 11787: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   11788: uses critical subroutine
1.191     harris41 11789: 
                   11790: =item *
                   11791: 
1.243     albertel 11792: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   11793: all args are optional
1.191     harris41 11794: 
                   11795: =item *
                   11796: 
1.717     albertel 11797: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   11798: dumps the complete (or key matching regexp) namespace into a hash
                   11799: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   11800: normally &store()ed into
                   11801: 
                   11802: $range should be either an integer '100' (give me the first 100
                   11803:                                            matching records)
                   11804:               or be  two integers sperated by a - with no spaces
                   11805:                  '30-50' (give me the 30th through the 50th matching
                   11806:                           records)
                   11807: 
                   11808: 
                   11809: =item *
                   11810: 
                   11811: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   11812: replaces a &store() version of data with a replacement set of data
                   11813: for a particular resource in a namespace passed in the $storehash hash 
                   11814: reference
                   11815: 
                   11816: =item *
                   11817: 
1.243     albertel 11818: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   11819: works very similar to store/cstore, but all data is stored in a
                   11820: temporary location and can be reset using tmpreset, $storehash should
                   11821: be a hash reference, returns nothing on success
1.191     harris41 11822: 
                   11823: =item *
                   11824: 
1.243     albertel 11825: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   11826: similar to restore, but all data is stored in a temporary location and
                   11827: can be reset using tmpreset. Returns a hash of values on success,
                   11828: error string otherwise.
1.191     harris41 11829: 
                   11830: =item *
                   11831: 
1.243     albertel 11832: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   11833: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 11834: 
                   11835: =item *
                   11836: 
1.243     albertel 11837: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11838: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 11839: 
                   11840: =item *
                   11841: 
1.243     albertel 11842: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   11843: namesp ($udom and $uname are optional)
1.191     harris41 11844: 
                   11845: =item *
                   11846: 
1.702     albertel 11847: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 11848: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 11849: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  11850: 
1.702     albertel 11851: $range should be either an integer '100' (give me the first 100
                   11852:                                            matching records)
                   11853:               or be  two integers sperated by a - with no spaces
                   11854:                  '30-50' (give me the 30th through the 50th matching
                   11855:                           records)
1.449     matthew  11856: =item *
                   11857: 
                   11858: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   11859: $store can be a scalar, an array reference, or if the amount to be 
                   11860: incremented is > 1, a hash reference.
                   11861: 
                   11862: ($udom and $uname are optional)
1.191     harris41 11863: 
                   11864: =item *
                   11865: 
1.243     albertel 11866: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   11867: ($udom and $uname are optional)
1.191     harris41 11868: 
                   11869: =item *
                   11870: 
1.243     albertel 11871: cput($namespace,$storehash,$udom,$uname) : critical put
                   11872: ($udom and $uname are optional)
1.191     harris41 11873: 
                   11874: =item *
                   11875: 
1.748     albertel 11876: newput($namespace,$storehash,$udom,$uname) :
                   11877: 
                   11878: Attempts to store the items in the $storehash, but only if they don't
                   11879: currently exist, if this succeeds you can be certain that you have 
                   11880: successfully created a new key value pair in the $namespace db.
                   11881: 
                   11882: 
                   11883: Args:
                   11884:  $namespace: name of database to store values to
                   11885:  $storehash: hashref to store to the db
                   11886:  $udom: (optional) domain of user containing the db
                   11887:  $uname: (optional) name of user caontaining the db
                   11888: 
                   11889: Returns:
                   11890:  'ok' -> succeeded in storing all keys of $storehash
                   11891:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   11892:                         least <key> already existed in the db (other
                   11893:                         requested keys may also already exist)
1.967     bisitz   11894:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 11895:  'con_lost' -> unable to contact request server
                   11896:  'refused' -> action was not allowed by remote machine
                   11897: 
                   11898: 
                   11899: =item *
                   11900: 
1.243     albertel 11901: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11902: reference filled in from namesp (encrypts the return communication)
                   11903: ($udom and $uname are optional)
1.191     harris41 11904: 
                   11905: =item *
                   11906: 
1.243     albertel 11907: log($udom,$name,$home,$message) : write to permanent log for user; use
                   11908: critical subroutine
                   11909: 
1.806     raeburn  11910: =item *
                   11911: 
1.860     raeburn  11912: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   11913: array reference filled in from namespace found in domain level on either
                   11914: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  11915: 
                   11916: =item *
                   11917: 
1.860     raeburn  11918: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   11919: domain level either on specified domain server ($uhome) or primary domain 
                   11920: server ($udom and $uhome are optional)
1.806     raeburn  11921: 
1.943     raeburn  11922: =item * 
                   11923: 
                   11924: get_domain_defaults($target_domain) : returns hash with defaults for
                   11925: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
                   11926: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
                   11927: or localauth), initial password or a kerberos realm, language (e.g., en-us).
                   11928: Values are retrieved from cache (if current), or from domain's configuration.db
                   11929: (if available), or lastly from values in lonTabs/dns_domain,tab, 
                   11930: or lonTabs/domain.tab. 
                   11931: 
                   11932: %domdefaults = &get_auth_defaults($target_domain);
                   11933: 
1.243     albertel 11934: =back
                   11935: 
                   11936: =head2 Network Status Functions
                   11937: 
                   11938: =over 4
1.191     harris41 11939: 
                   11940: =item *
                   11941: 
1.1137    raeburn  11942: dirlist() : return directory list based on URI (first arg).
                   11943: 
                   11944: Inputs: 1 required, 5 optional.
                   11945: 
                   11946: =over
                   11947: 
                   11948: =item 
                   11949: $uri - path to file in filesystem (starts: /res or /userfiles/). Required.
                   11950: 
                   11951: =item
                   11952: $userdomain - domain of user/course to be listed. Extracted from $uri if absent. 
                   11953: 
                   11954: =item
                   11955: $username -  username of user/course to be listed. Extracted from $uri if absent. 
                   11956: 
                   11957: =item
                   11958: $getpropath - boolean: 1 if prepend path using &propath(). 
                   11959: 
                   11960: =item
                   11961: $getuserdir - boolean: 1 if prepend path for "userfiles".
                   11962: 
                   11963: =item 
                   11964: $alternateRoot - path to prepend in place of path from $uri.
                   11965: 
                   11966: =back
                   11967: 
                   11968: Returns: Array of up to two items.
                   11969: 
                   11970: =over
                   11971: 
                   11972: a reference to an array of files/subdirectories
                   11973: 
                   11974: =over
                   11975: 
                   11976: Each element in the array of files/subdirectories is a & separated list of
                   11977: item name and the result of running stat on the item.  If dirlist was requested
                   11978: for a file instead of a directory, the item name will be ''. For a directory 
                   11979: listing, if the item is a metadata file, the element will end &N&M 
                   11980: (where N amd M are either 0 or 1, corresponding to obsolete set (1), or
                   11981: default copyright set (1).  
                   11982: 
                   11983: =back
                   11984: 
                   11985: a scalar containing error condition (if encountered).
                   11986: 
                   11987: =over
                   11988: 
                   11989: =item 
                   11990: no_host (no homeserver identified for $username:$domain).
                   11991: 
                   11992: =item 
                   11993: no_such_host (server contacted for listing not identified as valid host).
                   11994: 
                   11995: =item 
                   11996: con_lost (connection to remote server failed).
                   11997: 
                   11998: =item 
                   11999: refused (invalid $username:$domain received on lond side).
                   12000: 
                   12001: =item 
                   12002: no_such_dir (directory at specified path on lond side does not exist). 
                   12003: 
                   12004: =item 
                   12005: empty (directory at specified path on lond side is empty).
                   12006: 
                   12007: =over
                   12008: 
                   12009: This is currently not encountered because the &ls3, &ls2, 
                   12010: &ls (_handler) routines on the lond side do not filter out
                   12011: . and .. from a directory listing. 
                   12012: 
                   12013: =back
                   12014: 
                   12015: =back
                   12016: 
                   12017: =back
1.191     harris41 12018: 
                   12019: =item *
                   12020: 
1.243     albertel 12021: spareserver() : find server with least workload from spare.tab
                   12022: 
1.986     foxr     12023: 
                   12024: =item *
                   12025: 
                   12026: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   12027: if there is no corresponding loncapa host.
                   12028: 
1.243     albertel 12029: =back
                   12030: 
1.986     foxr     12031: 
1.243     albertel 12032: =head2 Apache Request
                   12033: 
                   12034: =over 4
1.191     harris41 12035: 
                   12036: =item *
                   12037: 
1.243     albertel 12038: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   12039: localhost, posts hash
                   12040: 
                   12041: =back
                   12042: 
                   12043: =head2 Data to String to Data
                   12044: 
                   12045: =over 4
1.191     harris41 12046: 
                   12047: =item *
                   12048: 
1.243     albertel 12049: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   12050: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 12051: 
                   12052: =item *
                   12053: 
1.243     albertel 12054: hashref2str($hashref) : convert a hashref into a string complete with
                   12055: escaping and '=' and '&' separators, supports elements that are
                   12056: arrayrefs and hashrefs
1.191     harris41 12057: 
                   12058: =item *
                   12059: 
1.243     albertel 12060: arrayref2str($arrayref) : convert an arrayref into a string complete
                   12061: with escaping and '&' separators, supports elements that are arrayrefs
                   12062: and hashrefs
1.191     harris41 12063: 
                   12064: =item *
                   12065: 
1.243     albertel 12066: str2hash($string) : convert string to hash using unescaping and
                   12067: splitting on '=' and '&', supports elements that are arrayrefs and
                   12068: hashrefs
1.191     harris41 12069: 
                   12070: =item *
                   12071: 
1.243     albertel 12072: str2array($string) : convert string to hash using unescaping and
                   12073: splitting on '&', supports elements that are arrayrefs and hashrefs
                   12074: 
                   12075: =back
                   12076: 
                   12077: =head2 Logging Routines
                   12078: 
                   12079: 
                   12080: These routines allow one to make log messages in the lonnet.log and
                   12081: lonnet.perm logfiles.
1.191     harris41 12082: 
1.1119    foxr     12083: =over 4
                   12084: 
1.191     harris41 12085: =item *
                   12086: 
1.243     albertel 12087: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 12088: 
                   12089: =item *
                   12090: 
1.243     albertel 12091: logthis() : append message to the normal lonnet.log file, it gets
                   12092: preiodically rolled over and deleted.
1.191     harris41 12093: 
                   12094: =item *
                   12095: 
1.243     albertel 12096: logperm() : append a permanent message to lonnet.perm.log, this log
                   12097: file never gets deleted by any automated portion of the system, only
                   12098: messages of critical importance should go in here.
                   12099: 
1.1119    foxr     12100: 
1.243     albertel 12101: =back
                   12102: 
                   12103: =head2 General File Helper Routines
                   12104: 
                   12105: =over 4
1.191     harris41 12106: 
                   12107: =item *
                   12108: 
1.481     raeburn  12109: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   12110: (a) files in /uploaded
                   12111:   (i) If a local copy of the file exists - 
                   12112:       compares modification date of local copy with last-modified date for 
                   12113:       definitive version stored on home server for course. If local copy is 
                   12114:       stale, requests a new version from the home server and stores it. 
                   12115:       If the original has been removed from the home server, then local copy 
                   12116:       is unlinked.
                   12117:   (ii) If local copy does not exist -
                   12118:       requests the file from the home server and stores it. 
                   12119:   
                   12120:   If $caller is 'uploadrep':  
                   12121:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   12122:     for request for files originally uploaded via DOCS. 
                   12123:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   12124:   
                   12125:   Otherwise:
                   12126:      This indicates a call from the content generation phase of the request.
                   12127:      -  returns the entire contents of the file or -1.
                   12128:      
                   12129: (b) files in /res
                   12130:    - returns the entire contents of a file or -1; 
                   12131:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 12132: 
1.712     albertel 12133: 
                   12134: =item *
                   12135: 
                   12136: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   12137:                   reference
                   12138: 
                   12139: returns either a stat() list of data about the file or an empty list
                   12140: if the file doesn't exist or couldn't find out about it (connection
                   12141: problems or user unknown)
                   12142: 
1.191     harris41 12143: =item *
                   12144: 
1.243     albertel 12145: filelocation($dir,$file) : returns file system location of a file
                   12146: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   12147: directory that relative $file lookups are to looked in ($dir of /a/dir
                   12148: and a file of ../bob will become /a/bob)
1.191     harris41 12149: 
                   12150: =item *
                   12151: 
                   12152: hreflocation($dir,$file) : returns file system location or a URL; same as
                   12153: filelocation except for hrefs
                   12154: 
                   12155: =item *
                   12156: 
                   12157: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   12158: 
1.243     albertel 12159: =back
                   12160: 
1.608     albertel 12161: =head2 Usererfile file routines (/uploaded*)
                   12162: 
                   12163: =over 4
                   12164: 
                   12165: =item *
                   12166: 
                   12167: userfileupload(): main rotine for putting a file in a user or course's
                   12168:                   filespace, arguments are,
                   12169: 
1.620     albertel 12170:  formname - required - this is the name of the element in $env where the
1.608     albertel 12171:            filename, and the contents of the file to create/modifed exist
1.620     albertel 12172:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   12173:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  12174:  context - if coursedoc, store the file in the course of the active role
                   12175:              of the current user; 
                   12176:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   12177:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 12178:  subdir - required - subdirectory to put the file in under ../userfiles/
                   12179:          if undefined, it will be placed in "unknown"
                   12180: 
                   12181:  (This routine calls clean_filename() to remove any dangerous
                   12182:  characters from the filename, and then calls finuserfileupload() to
                   12183:  complete the transaction)
                   12184: 
                   12185:  returns either the url of the uploaded file (/uploaded/....) if successful
                   12186:  and /adm/notfound.html if unsuccessful
                   12187: 
                   12188: =item *
                   12189: 
                   12190: clean_filename(): routine for cleaing a filename up for storage in
                   12191:                  userfile space, argument is:
                   12192: 
                   12193:  filename - proposed filename
                   12194: 
                   12195: returns: the new clean filename
                   12196: 
                   12197: =item *
                   12198: 
1.1090    raeburn  12199: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 12200: userspace, probably shouldn't be called directly
                   12201: 
                   12202:   docuname: username or courseid of destination for the file
                   12203:   docudom: domain of user/course of destination for the file
                   12204:   formname: same as for userfileupload()
1.1090    raeburn  12205:   fname: filename (including subdirectories) for the file
                   12206:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
                   12207:   allfiles: reference to hash used to store objects found by parser
                   12208:   codebase: reference to hash used for codebases of java objects found by parser
                   12209:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   12210:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   12211:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   12212:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   12213:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   12214:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  12215:   mimetype: reference to scalar to accommodate mime type determined
                   12216:             from File::MMagic if $parser = parse.
1.608     albertel 12217: 
                   12218:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  12219:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   12220:  was 'overwrite').
                   12221:  
1.608     albertel 12222: 
                   12223: =item *
                   12224: 
                   12225: renameuserfile(): renames an existing userfile to a new name
                   12226: 
                   12227:   Args:
                   12228:    docuname: username or courseid of destination for the file
                   12229:    docudom: domain of user/course of destination for the file
                   12230:    old: current file name (including any subdirs under userfiles)
                   12231:    new: desired file name (including any subdirs under userfiles)
                   12232: 
                   12233: =item *
                   12234: 
                   12235: mkdiruserfile(): creates a directory is a userfiles dir
                   12236: 
                   12237:   Args:
                   12238:    docuname: username or courseid of destination for the file
                   12239:    docudom: domain of user/course of destination for the file
                   12240:    dir: dir to create (including any subdirs under userfiles)
                   12241: 
                   12242: =item *
                   12243: 
                   12244: removeuserfile(): removes a file that exists in userfiles
                   12245: 
                   12246:   Args:
                   12247:    docuname: username or courseid of destination for the file
                   12248:    docudom: domain of user/course of destination for the file
                   12249:    fname: filname to delete (including any subdirs under userfiles)
                   12250: 
                   12251: =item *
                   12252: 
                   12253: removeuploadedurl(): convience function for removeuserfile()
                   12254: 
                   12255:   Args:
                   12256:    url:  a full /uploaded/... url to delete
                   12257: 
1.747     albertel 12258: =item * 
                   12259: 
                   12260: get_portfile_permissions():
                   12261:   Args:
                   12262:     domain: domain of user or course contain the portfolio files
                   12263:     user: name of user or num of course contain the portfolio files
                   12264:   Returns:
                   12265:     hashref of a dump of the proper file_permissions.db
                   12266:    
                   12267: 
                   12268: =item * 
                   12269: 
                   12270: get_access_controls():
                   12271: 
                   12272: Args:
                   12273:   current_permissions: the hash ref returned from get_portfile_permissions()
                   12274:   group: (optional) the group you want the files associated with
                   12275:   file: (optional) the file you want access info on
                   12276: 
                   12277: Returns:
1.749     raeburn  12278:     a hash (keys are file names) of hashes containing
                   12279:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   12280:         values are XML containing access control settings (see below) 
1.747     albertel 12281: 
                   12282: Internal notes:
                   12283: 
1.749     raeburn  12284:  access controls are stored in file_permissions.db as key=value pairs.
                   12285:     key -> path to file/file_name\0uniqueID:scope_end_start
                   12286:         where scope -> public,guest,course,group,domains or users.
                   12287:               end -> UNIX time for end of access (0 -> no end date)
                   12288:               start -> UNIX time for start of access
                   12289: 
                   12290:     value -> XML description of access control
                   12291:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   12292:             <start></start>
                   12293:             <end></end>
                   12294: 
                   12295:             <password></password>  for scope type = guest
                   12296: 
                   12297:             <domain></domain>     for scope type = course or group
                   12298:             <number></number>
                   12299:             <roles id="">
                   12300:              <role></role>
                   12301:              <access></access>
                   12302:              <section></section>
                   12303:              <group></group>
                   12304:             </roles>
                   12305: 
                   12306:             <dom></dom>         for scope type = domains
                   12307: 
                   12308:             <users>             for scope type = users
                   12309:              <user>
                   12310:               <uname></uname>
                   12311:               <udom></udom>
                   12312:              </user>
                   12313:             </users>
                   12314:            </scope> 
                   12315:               
                   12316:  Access data is also aggregated for each file in an additional key=value pair:
                   12317:  key -> path to file/file_name\0accesscontrol 
                   12318:  value -> reference to hash
                   12319:           hash contains key = value pairs
                   12320:           where key = uniqueID:scope_end_start
                   12321:                 value = UNIX time record was last updated
                   12322: 
                   12323:           Used to improve speed of look-ups of access controls for each file.  
                   12324:  
                   12325:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   12326: 
                   12327: modify_access_controls():
                   12328: 
                   12329: Modifies access controls for a portfolio file
                   12330: Args
                   12331: 1. file name
                   12332: 2. reference to hash of required changes,
                   12333: 3. domain
                   12334: 4. username
                   12335:   where domain,username are the domain of the portfolio owner 
                   12336:   (either a user or a course) 
                   12337: 
                   12338: Returns:
                   12339: 1. result of additions or updates ('ok' or 'error', with error message). 
                   12340: 2. result of deletions ('ok' or 'error', with error message).
                   12341: 3. reference to hash of any new or updated access controls.
                   12342: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   12343:    key = integer (inbound ID)
                   12344:    value = uniqueID  
1.747     albertel 12345: 
1.608     albertel 12346: =back
                   12347: 
1.243     albertel 12348: =head2 HTTP Helper Routines
                   12349: 
                   12350: =over 4
                   12351: 
1.191     harris41 12352: =item *
                   12353: 
                   12354: escape() : unpack non-word characters into CGI-compatible hex codes
                   12355: 
                   12356: =item *
                   12357: 
                   12358: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   12359: 
1.243     albertel 12360: =back
                   12361: 
                   12362: =head1 PRIVATE SUBROUTINES
                   12363: 
                   12364: =head2 Underlying communication routines (Shouldn't call)
                   12365: 
                   12366: =over 4
                   12367: 
                   12368: =item *
                   12369: 
                   12370: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   12371: 
                   12372: =item *
                   12373: 
                   12374: reply() : uses subreply to send a message to remote machine, logs all failures
                   12375: 
                   12376: =item *
                   12377: 
                   12378: critical() : passes a critical message to another server; if cannot
                   12379: get through then place message in connection buffer directory and
                   12380: returns con_delayed, if incapable of saving message, returns
                   12381: con_failed
                   12382: 
                   12383: =item *
                   12384: 
                   12385: reconlonc() : tries to reconnect lonc client processes.
                   12386: 
                   12387: =back
                   12388: 
                   12389: =head2 Resource Access Logging
                   12390: 
                   12391: =over 4
                   12392: 
                   12393: =item *
                   12394: 
                   12395: flushcourselogs() : flush (save) buffer logs and access logs
                   12396: 
                   12397: =item *
                   12398: 
                   12399: courselog($what) : save message for course in hash
                   12400: 
                   12401: =item *
                   12402: 
                   12403: courseacclog($what) : save message for course using &courselog().  Perform
                   12404: special processing for specific resource types (problems, exams, quizzes, etc).
                   12405: 
1.191     harris41 12406: =item *
                   12407: 
                   12408: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   12409: as a PerlChildExitHandler
1.243     albertel 12410: 
                   12411: =back
                   12412: 
                   12413: =head2 Other
                   12414: 
                   12415: =over 4
                   12416: 
                   12417: =item *
                   12418: 
                   12419: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 12420: 
                   12421: =back
                   12422: 
                   12423: =cut
1.877     foxr     12424: 

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