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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.1111  ! www         4: # $Id: lonnet.pm,v 1.1110 2011/06/06 17:17:44 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.1079    raeburn    79:             $_64bit %env %protocol %loncaparevs %serverhomeIDs %needsrelease);
1.871     albertel   80: 
                     81: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     82:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     83:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
1.958     www        84:     %courseownerbuf, %coursetypebuf,$locknum);
1.403     www        85: 
1.1       albertel   86: use IO::Socket;
1.31      www        87: use GDBM_File;
1.208     albertel   88: use HTML::LCParser;
1.88      www        89: use Fcntl qw(:flock);
1.870     albertel   90: use Storable qw(thaw nfreeze);
1.539     albertel   91: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   92: use Cache::Memcached;
1.676     albertel   93: use Digest::MD5;
1.790     albertel   94: use Math::Random;
1.1024    raeburn    95: use File::MMagic;
1.807     albertel   96: use LONCAPA qw(:DEFAULT :match);
1.740     www        97: use LONCAPA::Configuration;
1.1090    raeburn    98: use File::Copy;
1.676     albertel   99: 
1.195     www       100: my $readit;
1.550     foxr      101: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel  102: 
1.619     albertel  103: require Exporter;
                    104: 
                    105: our @ISA = qw (Exporter);
                    106: our @EXPORT = qw(%env);
                    107: 
1.449     matthew   108: 
1.1       albertel  109: # --------------------------------------------------------------------- Logging
1.729     www       110: {
                    111:     my $logid;
                    112:     sub instructor_log {
1.957     raeburn   113: 	my ($hash_name,$storehash,$delflag,$uname,$udom,$cnum,$cdom)=@_;
                    114:         if (($cnum eq '') || ($cdom eq '')) {
                    115:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                    116:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    117:         }
1.729     www       118: 	$logid++;
1.957     raeburn   119:         my $now = time();
                    120: 	my $id=$now.'00000'.$$.'00000'.$logid;
1.729     www       121: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www       122: 				    { $id => {
                    123: 					'exe_uname' => $env{'user.name'},
                    124: 					'exe_udom'  => $env{'user.domain'},
1.957     raeburn   125: 					'exe_time'  => $now,
1.730     www       126: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    127: 					'delflag'   => $delflag,
                    128: 					'logentry'  => $storehash,
                    129: 					'uname'     => $uname,
                    130: 					'udom'      => $udom,
                    131: 				    }
1.957     raeburn   132: 				  },$cdom,$cnum);
1.729     www       133:     }
                    134: }
1.1       albertel  135: 
1.163     harris41  136: sub logtouch {
                    137:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  138:     unless (-e "$execdir/logs/lonnet.log") {	
                    139: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  140: 	close $fh;
                    141:     }
                    142:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    143:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    144: }
                    145: 
1.1       albertel  146: sub logthis {
                    147:     my $message=shift;
                    148:     my $execdir=$perlvar{'lonDaemons'};
                    149:     my $now=time;
                    150:     my $local=localtime($now);
1.448     albertel  151:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
1.986     foxr      152: 	my $logstring = $local. " ($$): ".$message."\n"; # Keep any \'s in string.
                    153: 	print $fh $logstring;
1.448     albertel  154: 	close($fh);
                    155:     }
1.1       albertel  156:     return 1;
                    157: }
                    158: 
                    159: sub logperm {
                    160:     my $message=shift;
                    161:     my $execdir=$perlvar{'lonDaemons'};
                    162:     my $now=time;
                    163:     my $local=localtime($now);
1.448     albertel  164:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    165: 	print $fh "$now:$message:$local\n";
                    166: 	close($fh);
                    167:     }
1.1       albertel  168:     return 1;
                    169: }
                    170: 
1.850     albertel  171: sub create_connection {
1.853     albertel  172:     my ($hostname,$lonid) = @_;
1.851     albertel  173:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  174: 				     Type    => SOCK_STREAM,
                    175: 				     Timeout => 10);
                    176:     return 0 if (!$client);
1.890     albertel  177:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  178:     my $result = <$client>;
                    179:     chomp($result);
                    180:     return 1 if ($result eq 'done');
                    181:     return 0;
                    182: }
                    183: 
1.983     raeburn   184: sub get_server_timezone {
                    185:     my ($cnum,$cdom) = @_;
                    186:     my $home=&homeserver($cnum,$cdom);
                    187:     if ($home ne 'no_host') {
                    188:         my $cachetime = 24*3600;
                    189:         my ($timezone,$cached)=&is_cached_new('servertimezone',$home);
                    190:         if (defined($cached)) {
                    191:             return $timezone;
                    192:         } else {
                    193:             my $timezone = &reply('servertimezone',$home);
                    194:             return &do_cache_new('servertimezone',$home,$timezone,$cachetime);
                    195:         }
                    196:     }
                    197: }
1.850     albertel  198: 
1.1106    raeburn   199: sub get_server_distarch {
                    200:     my ($lonhost,$ignore_cache) = @_;
                    201:     if (defined($lonhost)) {
                    202:         if (!defined(&hostname($lonhost))) {
                    203:             return;
                    204:         }
                    205:         my $cachetime = 12*3600;
                    206:         if (!$ignore_cache) {
                    207:             my ($distarch,$cached)=&is_cached_new('serverdistarch',$lonhost);
                    208:             if (defined($cached)) {
                    209:                 return $distarch;
                    210:             }
                    211:         }
                    212:         my $rep = &reply('serverdistarch',$lonhost);
                    213:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' ||
                    214:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                    215:                 $rep eq '') {
                    216:             return &do_cache_new('serverdistarch',$lonhost,$rep,$cachetime);
                    217:         }
                    218:     }
                    219:     return;
                    220: }
                    221: 
1.993     raeburn   222: sub get_server_loncaparev {
1.1073    raeburn   223:     my ($dom,$lonhost,$ignore_cache,$caller) = @_;
1.993     raeburn   224:     if (defined($lonhost)) {
                    225:         if (!defined(&hostname($lonhost))) {
                    226:             undef($lonhost);
                    227:         }
                    228:     }
                    229:     if (!defined($lonhost)) {
                    230:         if (defined(&domain($dom,'primary'))) {
                    231:             $lonhost=&domain($dom,'primary');
                    232:             if ($lonhost eq 'no_host') {
                    233:                 undef($lonhost);
                    234:             }
                    235:         }
                    236:     }
                    237:     if (defined($lonhost)) {
1.1073    raeburn   238:         my $cachetime = 12*3600;
                    239:         if (!$ignore_cache) {
                    240:             my ($loncaparev,$cached)=&is_cached_new('serverloncaparev',$lonhost);
                    241:             if (defined($cached)) {
                    242:                 return $loncaparev;
                    243:             }
                    244:         }
                    245:         my ($answer,$loncaparev);
                    246:         my @ids=&current_machine_ids();
                    247:         if (grep(/^\Q$lonhost\E$/,@ids)) {
                    248:             $answer = $perlvar{'lonVersion'};
1.1081    raeburn   249:             if ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   250:                 $loncaparev = $1;
                    251:             }
                    252:         } else {
                    253:             $answer = &reply('serverloncaparev',$lonhost);
                    254:             if (($answer eq 'unknown_cmd') || ($answer eq 'con_lost')) {
                    255:                 if ($caller eq 'loncron') {
                    256:                     my $ua=new LWP::UserAgent;
1.1082    raeburn   257:                     $ua->timeout(4);
1.1073    raeburn   258:                     my $protocol = $protocol{$lonhost};
                    259:                     $protocol = 'http' if ($protocol ne 'https');
                    260:                     my $url = $protocol.'://'.&hostname($lonhost).'/adm/about.html';
                    261:                     my $request=new HTTP::Request('GET',$url);
                    262:                     my $response=$ua->request($request);
                    263:                     unless ($response->is_error()) {
                    264:                         my $content = $response->content;
1.1081    raeburn   265:                         if ($content =~ /<p>VERSION\:\s*([\w.\-]+)<\/p>/) {
1.1073    raeburn   266:                             $loncaparev = $1;
                    267:                         }
                    268:                     }
                    269:                 } else {
                    270:                     $loncaparev = $loncaparevs{$lonhost};
                    271:                 }
1.1081    raeburn   272:             } elsif ($answer =~ /^[\'\"]?([\w.\-]+)[\'\"]?$/) {
1.1073    raeburn   273:                 $loncaparev = $1;
                    274:             }
1.993     raeburn   275:         }
1.1073    raeburn   276:         return &do_cache_new('serverloncaparev',$lonhost,$loncaparev,$cachetime);
1.993     raeburn   277:     }
                    278: }
                    279: 
1.1074    raeburn   280: sub get_server_homeID {
                    281:     my ($hostname,$ignore_cache,$caller) = @_;
                    282:     unless ($ignore_cache) {
                    283:         my ($serverhomeID,$cached)=&is_cached_new('serverhomeID',$hostname);
                    284:         if (defined($cached)) {
                    285:             return $serverhomeID;
                    286:         }
                    287:     }
                    288:     my $cachetime = 12*3600;
                    289:     my $serverhomeID;
                    290:     if ($caller eq 'loncron') { 
                    291:         my @machine_ids = &machine_ids($hostname);
                    292:         foreach my $id (@machine_ids) {
                    293:             my $response = &reply('serverhomeID',$id);
                    294:             unless (($response eq 'unknown_cmd') || ($response eq 'con_lost')) {
                    295:                 $serverhomeID = $response;
                    296:                 last;
                    297:             }
                    298:         }
                    299:         if ($serverhomeID eq '') {
                    300:             $serverhomeID = $machine_ids[-1];
                    301:         }
                    302:     } else {
                    303:         $serverhomeID = $serverhomeIDs{$hostname};
                    304:     }
                    305:     return &do_cache_new('serverhomeID',$hostname,$serverhomeID,$cachetime);
                    306: }
                    307: 
1.1       albertel  308: # -------------------------------------------------- Non-critical communication
                    309: sub subreply {
                    310:     my ($cmd,$server)=@_;
1.838     albertel  311:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      312:     #
                    313:     #  With loncnew process trimming, there's a timing hole between lonc server
                    314:     #  process exit and the master server picking up the listen on the AF_UNIX
                    315:     #  socket.  In that time interval, a lock file will exist:
                    316: 
                    317:     my $lockfile=$peerfile.".lock";
                    318:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    319: 	sleep(1);
                    320:     }
                    321:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      322:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      323:     #
1.550     foxr      324:     #   We'll give the connection a few tries before abandoning it.  If
                    325:     #   connection is not possible, we'll con_lost back to the client.
                    326:     #   
                    327:     my $client;
                    328:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    329: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    330: 				      Type    => SOCK_STREAM,
                    331: 				      Timeout => 10);
1.869     albertel  332: 	if ($client) {
1.550     foxr      333: 	    last;		# Connected!
1.850     albertel  334: 	} else {
1.853     albertel  335: 	    &create_connection(&hostname($server),$server);
1.550     foxr      336: 	}
1.850     albertel  337:         sleep(1);		# Try again later if failed connection.
1.550     foxr      338:     }
                    339:     my $answer;
                    340:     if ($client) {
1.704     albertel  341: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      342: 	$answer=<$client>;
                    343: 	if (!$answer) { $answer="con_lost"; }
                    344: 	chomp($answer);
                    345:     } else {
                    346: 	$answer = 'con_lost';	# Failed connection.
                    347:     }
1.1       albertel  348:     return $answer;
                    349: }
                    350: 
                    351: sub reply {
                    352:     my ($cmd,$server)=@_;
1.838     albertel  353:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  354:     my $answer=subreply($cmd,$server);
1.65      www       355:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  356:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       357:                 " $cmd to $server returned $answer</font>");
                    358:     }
1.1       albertel  359:     return $answer;
                    360: }
                    361: 
                    362: # ----------------------------------------------------------- Send USR1 to lonc
                    363: 
                    364: sub reconlonc {
1.891     albertel  365:     my ($lonid) = @_;
                    366:     my $hostname = &hostname($lonid);
                    367:     if ($lonid) {
                    368: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    369: 	if ($hostname && -e $peerfile) {
                    370: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    371: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    372: 					     Type    => SOCK_STREAM,
                    373: 					     Timeout => 10);
                    374: 	    if ($client) {
                    375: 		print $client ("reset_retries\n");
                    376: 		my $answer=<$client>;
                    377: 		#reset just this one.
                    378: 	    }
                    379: 	}
                    380: 	return;
                    381:     }
                    382: 
1.836     www       383:     &logthis("Trying to reconnect lonc");
1.1       albertel  384:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  385:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  386: 	my $loncpid=<$fh>;
                    387:         chomp($loncpid);
                    388:         if (kill 0 => $loncpid) {
                    389: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    390:             kill USR1 => $loncpid;
                    391:             sleep 1;
1.836     www       392:          } else {
1.12      www       393: 	    &logthis(
1.672     albertel  394:                "<font color=\"blue\">WARNING:".
1.12      www       395:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  396:         }
                    397:     } else {
1.836     www       398: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  399:     }
                    400: }
                    401: 
                    402: # ------------------------------------------------------ Critical communication
1.12      www       403: 
1.1       albertel  404: sub critical {
                    405:     my ($cmd,$server)=@_;
1.838     albertel  406:     unless (&hostname($server)) {
1.672     albertel  407:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       408:                " Critical message to unknown server ($server)</font>");
                    409:         return 'no_such_host';
                    410:     }
1.1       albertel  411:     my $answer=reply($cmd,$server);
                    412:     if ($answer eq 'con_lost') {
                    413: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  414: 	my $answer=reply($cmd,$server);
1.1       albertel  415:         if ($answer eq 'con_lost') {
                    416:             my $now=time;
                    417:             my $middlename=$cmd;
1.5       www       418:             $middlename=substr($middlename,0,16);
1.1       albertel  419:             $middlename=~s/\W//g;
                    420:             my $dfilename=
1.305     www       421:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    422:             $dumpcount++;
1.1       albertel  423:             {
1.448     albertel  424: 		my $dfh;
                    425: 		if (open($dfh,">$dfilename")) {
                    426: 		    print $dfh "$cmd\n"; 
                    427: 		    close($dfh);
                    428: 		}
1.1       albertel  429:             }
                    430:             sleep 2;
                    431:             my $wcmd='';
                    432:             {
1.448     albertel  433: 		my $dfh;
                    434: 		if (open($dfh,"<$dfilename")) {
                    435: 		    $wcmd=<$dfh>; 
                    436: 		    close($dfh);
                    437: 		}
1.1       albertel  438:             }
                    439:             chomp($wcmd);
1.7       www       440:             if ($wcmd eq $cmd) {
1.672     albertel  441: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       442:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  443:                 &logperm("D:$server:$cmd");
                    444: 	        return 'con_delayed';
                    445:             } else {
1.672     albertel  446:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       447:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  448:                 &logperm("F:$server:$cmd");
                    449:                 return 'con_failed';
                    450:             }
                    451:         }
                    452:     }
                    453:     return $answer;
1.405     albertel  454: }
                    455: 
1.755     albertel  456: # ------------------------------------------- check if return value is an error
                    457: 
                    458: sub error {
                    459:     my ($result) = @_;
1.756     albertel  460:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  461: 	if ($2 == 2) { return undef; }
                    462: 	return $1;
                    463:     }
                    464:     return undef;
                    465: }
                    466: 
1.783     albertel  467: sub convert_and_load_session_env {
                    468:     my ($lonidsdir,$handle)=@_;
                    469:     my @profile;
                    470:     {
1.917     albertel  471: 	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    472: 	if (!$opened) {
1.915     albertel  473: 	    return 0;
                    474: 	}
1.783     albertel  475: 	flock($idf,LOCK_SH);
                    476: 	@profile=<$idf>;
                    477: 	close($idf);
                    478:     }
                    479:     my %temp_env;
                    480:     foreach my $line (@profile) {
1.786     albertel  481: 	if ($line !~ m/=/) {
                    482: 	    return 0;
                    483: 	}
1.783     albertel  484: 	chomp($line);
                    485: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    486: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    487:     }
                    488:     unlink("$lonidsdir/$handle.id");
                    489:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    490: 	    0640)) {
                    491: 	%disk_env = %temp_env;
                    492: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    493: 	untie(%disk_env);
                    494:     }
1.786     albertel  495:     return 1;
1.783     albertel  496: }
                    497: 
1.374     www       498: # ------------------------------------------- Transfer profile into environment
1.780     albertel  499: my $env_loaded;
                    500: sub transfer_profile_to_env {
1.788     albertel  501:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    502:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       503: 
1.720     albertel  504:     if (!defined($lonidsdir)) {
                    505: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    506:     }
                    507:     if (!defined($handle)) {
                    508:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    509:     }
                    510: 
1.786     albertel  511:     my $convert;
                    512:     {
1.917     albertel  513:     	my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    514: 	if (!$opened) {
1.915     albertel  515: 	    return;
                    516: 	}
1.786     albertel  517: 	flock($idf,LOCK_SH);
                    518: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    519: 		&GDBM_READER(),0640)) {
                    520: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    521: 	    untie(%disk_env);
                    522: 	} else {
                    523: 	    $convert = 1;
                    524: 	}
                    525:     }
                    526:     if ($convert) {
                    527: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    528: 	    &logthis("Failed to load session, or convert session.");
                    529: 	}
1.374     www       530:     }
1.783     albertel  531: 
1.786     albertel  532:     my %remove;
1.783     albertel  533:     while ( my $envname = each(%env) ) {
1.433     matthew   534:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    535:             if ($time < time-300) {
1.783     albertel  536:                 $remove{$key}++;
1.433     matthew   537:             }
                    538:         }
                    539:     }
1.783     albertel  540: 
1.619     albertel  541:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  542:     $env_loaded=1;
1.783     albertel  543:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   544:         &delenv($expired_key);
1.374     www       545:     }
1.1       albertel  546: }
                    547: 
1.916     albertel  548: # ---------------------------------------------------- Check for valid session 
                    549: sub check_for_valid_session {
                    550:     my ($r) = @_;
                    551:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    552:     my $lonid=$cookies{'lonID'};
                    553:     return undef if (!$lonid);
                    554: 
                    555:     my $handle=&LONCAPA::clean_handle($lonid->value);
                    556:     my $lonidsdir=$r->dir_config('lonIDsDir');
                    557:     return undef if (!-e "$lonidsdir/$handle.id");
                    558: 
1.917     albertel  559:     my $opened = open(my $idf,'+<',"$lonidsdir/$handle.id");
                    560:     return undef if (!$opened);
1.916     albertel  561: 
                    562:     flock($idf,LOCK_SH);
                    563:     my %disk_env;
                    564:     if (!tie(%disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    565: 	    &GDBM_READER(),0640)) {
                    566: 	return undef;	
                    567:     }
                    568: 
                    569:     if (!defined($disk_env{'user.name'})
                    570: 	|| !defined($disk_env{'user.domain'})) {
                    571: 	return undef;
                    572:     }
                    573:     return $handle;
                    574: }
                    575: 
1.830     albertel  576: sub timed_flock {
                    577:     my ($file,$lock_type) = @_;
                    578:     my $failed=0;
                    579:     eval {
                    580: 	local $SIG{__DIE__}='DEFAULT';
                    581: 	local $SIG{ALRM}=sub {
                    582: 	    $failed=1;
                    583: 	    die("failed lock");
                    584: 	};
                    585: 	alarm(13);
                    586: 	flock($file,$lock_type);
                    587: 	alarm(0);
                    588:     };
                    589:     if ($failed) {
                    590: 	return undef;
                    591:     } else {
                    592: 	return 1;
                    593:     }
                    594: }
                    595: 
1.5       www       596: # ---------------------------------------------------------- Append Environment
                    597: 
                    598: sub appenv {
1.949     raeburn   599:     my ($newenv,$roles) = @_;
                    600:     if (ref($newenv) eq 'HASH') {
                    601:         foreach my $key (keys(%{$newenv})) {
                    602:             my $refused = 0;
                    603: 	    if (($key =~ /^user\.role/) || ($key =~ /^user\.priv/)) {
                    604:                 $refused = 1;
                    605:                 if (ref($roles) eq 'ARRAY') {
                    606:                     my ($type,$role) = ($key =~ /^user\.(role|priv)\.([^.]+)\./);
                    607:                     if (grep(/^\Q$role\E$/,@{$roles})) {
                    608:                         $refused = 0;
                    609:                     }
                    610:                 }
                    611:             }
                    612:             if ($refused) {
                    613:                 &logthis("<font color=\"blue\">WARNING: ".
                    614:                          "Attempt to modify environment ".$key." to ".$newenv->{$key}
                    615:                          .'</font>');
                    616: 	        delete($newenv->{$key});
                    617:             } else {
                    618:                 $env{$key}=$newenv->{$key};
                    619:             }
                    620:         }
                    621:         my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    622:         if ($opened
                    623: 	    && &timed_flock($env_file,LOCK_EX)
                    624: 	    &&
                    625: 	    tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    626: 	        (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
                    627: 	    while (my ($key,$value) = each(%{$newenv})) {
                    628: 	        $disk_env{$key} = $value;
                    629: 	    }
                    630: 	    untie(%disk_env);
1.35      www       631:         }
1.191     harris41  632:     }
1.56      www       633:     return 'ok';
                    634: }
                    635: # ----------------------------------------------------- Delete from Environment
                    636: 
                    637: sub delenv {
1.1104    raeburn   638:     my ($delthis,$regexp,$roles) = @_;
                    639:     if (($delthis=~/^user\.role/) || ($delthis=~/^user\.priv/)) {
                    640:         my $refused = 1;
                    641:         if (ref($roles) eq 'ARRAY') {
                    642:             my ($type,$role) = ($delthis =~ /^user\.(role|priv)\.([^.]+)\./);
                    643:             if (grep(/^\Q$role\E$/,@{$roles})) {
                    644:                 $refused = 0;
                    645:             }
                    646:         }
                    647:         if ($refused) {
                    648:             &logthis("<font color=\"blue\">WARNING: ".
                    649:                      "Attempt to delete from environment ".$delthis);
                    650:             return 'error';
                    651:         }
1.56      www       652:     }
1.917     albertel  653:     my $opened = open(my $env_file,'+<',$env{'user.environment'});
                    654:     if ($opened
1.915     albertel  655: 	&& &timed_flock($env_file,LOCK_EX)
1.830     albertel  656: 	&&
                    657: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    658: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  659: 	foreach my $key (keys(%disk_env)) {
1.987     raeburn   660: 	    if ($regexp) {
                    661:                 if ($key=~/^$delthis/) {
                    662:                     delete($env{$key});
                    663:                     delete($disk_env{$key});
                    664:                 } 
                    665:             } else {
                    666:                 if ($key=~/^\Q$delthis\E/) {
                    667: 		    delete($env{$key});
                    668: 		    delete($disk_env{$key});
                    669: 	        }
                    670:             }
1.448     albertel  671: 	}
1.783     albertel  672: 	untie(%disk_env);
1.5       www       673:     }
                    674:     return 'ok';
1.369     albertel  675: }
                    676: 
1.790     albertel  677: sub get_env_multiple {
                    678:     my ($name) = @_;
                    679:     my @values;
                    680:     if (defined($env{$name})) {
                    681:         # exists is it an array
                    682:         if (ref($env{$name})) {
                    683:             @values=@{ $env{$name} };
                    684:         } else {
                    685:             $values[0]=$env{$name};
                    686:         }
                    687:     }
                    688:     return(@values);
                    689: }
                    690: 
1.958     www       691: # ------------------------------------------------------------------- Locking
                    692: 
                    693: sub set_lock {
                    694:     my ($text)=@_;
                    695:     $locknum++;
                    696:     my $id=$$.'-'.$locknum;
                    697:     &appenv({'session.locks' => $env{'session.locks'}.','.$id,
                    698:              'session.lock.'.$id => $text});
                    699:     return $id;
                    700: }
                    701: 
                    702: sub get_locks {
                    703:     my $num=0;
                    704:     my %texts=();
                    705:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    706:        if ($lock=~/\w/) {
                    707:           $num++;
                    708:           $texts{$lock}=$env{'session.lock.'.$lock};
                    709:        }
                    710:    }
                    711:    return ($num,%texts);
                    712: }
                    713: 
                    714: sub remove_lock {
                    715:     my ($id)=@_;
                    716:     my $newlocks='';
                    717:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    718:        if (($lock=~/\w/) && ($lock ne $id)) {
                    719:           $newlocks.=','.$lock;
                    720:        }
                    721:     }
                    722:     &appenv({'session.locks' => $newlocks});
                    723:     &delenv('session.lock.'.$id);
                    724: }
                    725: 
                    726: sub remove_all_locks {
                    727:     my $activelocks=$env{'session.locks'};
                    728:     foreach my $lock (split(/\,/,$env{'session.locks'})) {
                    729:        if ($lock=~/\w/) {
                    730:           &remove_lock($lock);
                    731:        }
                    732:     }
                    733: }
                    734: 
                    735: 
1.369     albertel  736: # ------------------------------------------ Find out current server userload
                    737: sub userload {
                    738:     my $numusers=0;
                    739:     {
                    740: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    741: 	my $filename;
                    742: 	my $curtime=time;
                    743: 	while ($filename=readdir(LONIDS)) {
1.925     albertel  744: 	    next if ($filename eq '.' || $filename eq '..');
                    745: 	    next if ($filename =~ /publicuser_\d+\.id/);
1.404     albertel  746: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  747: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  748: 	}
                    749: 	closedir(LONIDS);
                    750:     }
                    751:     my $userloadpercent=0;
                    752:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    753:     if ($maxuserload) {
1.371     albertel  754: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  755:     }
1.372     albertel  756:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  757:     return $userloadpercent;
1.283     www       758: }
                    759: 
1.1       albertel  760: # ------------------------------ Find server with least workload from spare.tab
1.11      www       761: 
1.1       albertel  762: sub spareserver {
1.1083    raeburn   763:     my ($loadpercent,$userloadpercent,$want_server_name,$udom) = @_;
1.784     albertel  764:     my $spare_server;
1.370     albertel  765:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  766:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    767:                                                      :  $userloadpercent;
1.1083    raeburn   768:     my ($uint_dom,$remotesessions);
                    769:     if (($udom ne '') && (&domain($udom) ne '')) {
                    770:         my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    771:         $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    772:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    773:         $remotesessions = $udomdefaults{'remotesessions'};
                    774:     }
1.784     albertel  775:     foreach my $try_server (@{ $spareid{'primary'} }) {
1.1083    raeburn   776:         if ($uint_dom) {
                    777:              next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    778:                                           $try_server));
                    779:         }
1.784     albertel  780: 	($spare_server, $lowest_load) =
                    781: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
                    782:     }
                    783: 
                    784:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    785: 
                    786:     if (!$found_server) {
                    787: 	foreach my $try_server (@{ $spareid{'default'} }) {
1.1083    raeburn   788:             if ($uint_dom) {
                    789:                 next unless (&spare_can_host($udom,$uint_dom,$remotesessions,
                    790:                                              $try_server));
                    791:             }
1.784     albertel  792: 	    ($spare_server, $lowest_load) =
                    793: 		&compare_server_load($try_server, $spare_server, $lowest_load);
                    794: 	}
                    795:     }
                    796: 
                    797:     if (!$want_server_name) {
1.968     raeburn   798:         my $protocol = 'http';
                    799:         if ($protocol{$spare_server} eq 'https') {
                    800:             $protocol = $protocol{$spare_server};
                    801:         }
1.1001    raeburn   802:         if (defined($spare_server)) {
                    803:             my $hostname = &hostname($spare_server);
1.1083    raeburn   804:             if (defined($hostname)) {
1.1001    raeburn   805: 	        $spare_server = $protocol.'://'.$hostname;
                    806:             }
                    807:         }
1.784     albertel  808:     }
                    809:     return $spare_server;
                    810: }
                    811: 
                    812: sub compare_server_load {
                    813:     my ($try_server, $spare_server, $lowest_load) = @_;
                    814: 
                    815:     my $loadans     = &reply('load',    $try_server);
                    816:     my $userloadans = &reply('userload',$try_server);
                    817: 
                    818:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
1.1072    www       819: 	return; #didn't get a number from the server
1.784     albertel  820:     }
                    821: 
                    822:     my $load;
                    823:     if ($loadans =~ /\d/) {
                    824: 	if ($userloadans =~ /\d/) {
                    825: 	    #both are numbers, pick the bigger one
                    826: 	    $load = ($loadans > $userloadans) ? $loadans 
                    827: 		                              : $userloadans;
1.411     albertel  828: 	} else {
1.784     albertel  829: 	    $load = $loadans;
1.411     albertel  830: 	}
1.784     albertel  831:     } else {
                    832: 	$load = $userloadans;
                    833:     }
                    834: 
                    835:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    836: 	$spare_server = $try_server;
                    837: 	$lowest_load  = $load;
1.370     albertel  838:     }
1.784     albertel  839:     return ($spare_server,$lowest_load);
1.202     matthew   840: }
1.914     albertel  841: 
                    842: # --------------------------- ask offload servers if user already has a session
                    843: sub find_existing_session {
                    844:     my ($udom,$uname) = @_;
                    845:     foreach my $try_server (@{ $spareid{'primary'} },
                    846: 			    @{ $spareid{'default'} }) {
                    847: 	return $try_server if (&has_user_session($try_server, $udom, $uname));
                    848:     }
                    849:     return;
                    850: }
                    851: 
                    852: # -------------------------------- ask if server already has a session for user
                    853: sub has_user_session {
                    854:     my ($lonid,$udom,$uname) = @_;
                    855:     my $result = &reply(join(':','userhassession',
                    856: 			     map {&escape($_)} ($udom,$uname)),$lonid);
                    857:     return 1 if ($result eq 'ok');
                    858: 
                    859:     return 0;
                    860: }
                    861: 
1.1076    raeburn   862: # --------- determine least loaded server in a user's domain which allows login
                    863: 
                    864: sub choose_server {
                    865:     my ($udom) = @_;
                    866:     my %domconfhash = &Apache::loncommon::get_domainconf($udom);
1.1077    raeburn   867:     my %servers = &get_servers($udom);
1.1076    raeburn   868:     my $lowest_load = 30000;
                    869:     my ($login_host,$hostname);
                    870:     foreach my $lonhost (keys(%servers)) {
                    871:         my $loginvia = $domconfhash{$udom.'.login.loginvia_'.$lonhost};
                    872:         if ($loginvia eq '') {
                    873:             ($login_host, $lowest_load) =
                    874:             &compare_server_load($lonhost, $login_host, $lowest_load);
                    875:         }
                    876:     }
                    877:     if ($login_host ne '') {
                    878:         $hostname = $servers{$login_host};
                    879:     }
                    880:     return ($login_host,$hostname);
                    881: }
                    882: 
1.202     matthew   883: # --------------------------------------------- Try to change a user's password
                    884: 
                    885: sub changepass {
1.799     raeburn   886:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   887:     $currentpass = &escape($currentpass);
                    888:     $newpass     = &escape($newpass);
1.1030    raeburn   889:     my $lonhost = $perlvar{'lonHostID'};
                    890:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context:$lonhost",
1.202     matthew   891: 		       $server);
                    892:     if (! $answer) {
                    893: 	&logthis("No reply on password change request to $server ".
                    894: 		 "by $uname in domain $udom.");
                    895:     } elsif ($answer =~ "^ok") {
                    896:         &logthis("$uname in $udom successfully changed their password ".
                    897: 		 "on $server.");
                    898:     } elsif ($answer =~ "^pwchange_failure") {
                    899: 	&logthis("$uname in $udom was unable to change their password ".
                    900: 		 "on $server.  The action was blocked by either lcpasswd ".
                    901: 		 "or pwchange");
                    902:     } elsif ($answer =~ "^non_authorized") {
                    903:         &logthis("$uname in $udom did not get their password correct when ".
                    904: 		 "attempting to change it on $server.");
                    905:     } elsif ($answer =~ "^auth_mode_error") {
                    906:         &logthis("$uname in $udom attempted to change their password despite ".
                    907: 		 "not being locally or internally authenticated on $server.");
                    908:     } elsif ($answer =~ "^unknown_user") {
                    909:         &logthis("$uname in $udom attempted to change their password ".
                    910: 		 "on $server but were unable to because $server is not ".
                    911: 		 "their home server.");
                    912:     } elsif ($answer =~ "^refused") {
                    913: 	&logthis("$server refused to change $uname in $udom password because ".
                    914: 		 "it was sent an unencrypted request to change the password.");
1.1030    raeburn   915:     } elsif ($answer =~ "invalid_client") {
                    916:         &logthis("$server refused to change $uname in $udom password because ".
                    917:                  "it was a reset by e-mail originating from an invalid server.");
1.202     matthew   918:     }
                    919:     return $answer;
1.1       albertel  920: }
                    921: 
1.169     harris41  922: # ----------------------- Try to determine user's current authentication scheme
                    923: 
                    924: sub queryauthenticate {
                    925:     my ($uname,$udom)=@_;
1.456     albertel  926:     my $uhome=&homeserver($uname,$udom);
                    927:     if (!$uhome) {
                    928: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    929: 	return 'no_host';
                    930:     }
                    931:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    932:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    933: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  934:     }
1.456     albertel  935:     return $answer;
1.169     harris41  936: }
                    937: 
1.1       albertel  938: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       939: 
1.1       albertel  940: sub authenticate {
1.1073    raeburn   941:     my ($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)=@_;
1.807     albertel  942:     $upass=&escape($upass);
                    943:     $uname= &LONCAPA::clean_username($uname);
1.836     www       944:     my $uhome=&homeserver($uname,$udom,1);
1.952     raeburn   945:     my $newhome;
1.836     www       946:     if ((!$uhome) || ($uhome eq 'no_host')) {
                    947: # Maybe the machine was offline and only re-appeared again recently?
                    948:         &reconlonc();
                    949: # One more
1.952     raeburn   950: 	$uhome=&homeserver($uname,$udom,1);
                    951:         if (($uhome eq 'no_host') && $checkdefauth) {
                    952:             if (defined(&domain($udom,'primary'))) {
                    953:                 $newhome=&domain($udom,'primary');
                    954:             }
                    955:             if ($newhome ne '') {
                    956:                 $uhome = $newhome;
                    957:             }
                    958:         }
1.836     www       959: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                    960: 	    &logthis("User $uname at $udom is unknown in authenticate");
1.952     raeburn   961: 	    return 'no_host';
                    962:         }
1.1       albertel  963:     }
1.1073    raeburn   964:     my $answer=reply("encrypt:auth:$udom:$uname:$upass:$checkdefauth:$clientcancheckhost",$uhome);
1.471     albertel  965:     if ($answer eq 'authorized') {
1.952     raeburn   966:         if ($newhome) {
                    967:             &logthis("User $uname at $udom authorized by $uhome, but needs account");
                    968:             return 'no_account_on_host'; 
                    969:         } else {
                    970:             &logthis("User $uname at $udom authorized by $uhome");
                    971:             return $uhome;
                    972:         }
1.471     albertel  973:     }
                    974:     if ($answer eq 'non_authorized') {
                    975: 	&logthis("User $uname at $udom rejected by $uhome");
                    976: 	return 'no_host'; 
1.9       www       977:     }
1.471     albertel  978:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  979:     return 'no_host';
                    980: }
                    981: 
1.1073    raeburn   982: sub can_host_session {
1.1074    raeburn   983:     my ($udom,$lonhost,$remoterev,$remotesessions,$hostedsessions) = @_;
1.1073    raeburn   984:     my $canhost = 1;
1.1074    raeburn   985:     my $host_idn = &Apache::lonnet::internet_dom($lonhost);
1.1073    raeburn   986:     if (ref($remotesessions) eq 'HASH') {
                    987:         if (ref($remotesessions->{'excludedomain'}) eq 'ARRAY') {
1.1074    raeburn   988:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'excludedomain'}})) {
1.1073    raeburn   989:                 $canhost = 0;
                    990:             } else {
                    991:                 $canhost = 1;
                    992:             }
                    993:         }
                    994:         if (ref($remotesessions->{'includedomain'}) eq 'ARRAY') {
1.1074    raeburn   995:             if (grep(/^\Q$host_idn\E$/,@{$remotesessions->{'includedomain'}})) {
1.1073    raeburn   996:                 $canhost = 1;
                    997:             } else {
                    998:                 $canhost = 0;
                    999:             }
                   1000:         }
                   1001:         if ($canhost) {
                   1002:             if ($remotesessions->{'version'} ne '') {
                   1003:                 my ($reqmajor,$reqminor) = ($remotesessions->{'version'} =~ /^(\d+)\.(\d+)$/);
                   1004:                 if ($reqmajor ne '' && $reqminor ne '') {
                   1005:                     if ($remoterev =~ /^\'?(\d+)\.(\d+)/) {
                   1006:                         my $major = $1;
                   1007:                         my $minor = $2;
                   1008:                         if (($major < $reqmajor ) ||
                   1009:                             (($major == $reqmajor) && ($minor < $reqminor))) {
                   1010:                             $canhost = 0;
                   1011:                         }
                   1012:                     } else {
                   1013:                         $canhost = 0;
                   1014:                     }
                   1015:                 }
                   1016:             }
                   1017:         }
                   1018:     }
                   1019:     if ($canhost) {
                   1020:         if (ref($hostedsessions) eq 'HASH') {
                   1021:             if (ref($hostedsessions->{'excludedomain'}) eq 'ARRAY') {
                   1022:                 if (grep(/^\Q$udom\E$/,@{$hostedsessions->{'excludedomain'}})) {
                   1023:                     $canhost = 0;
                   1024:                 } else {
                   1025:                     $canhost = 1;
                   1026:                 }
                   1027:             }
                   1028:             if (ref($hostedsessions->{'includedomain'}) eq 'ARRAY') {
                   1029:                 if (grep(/^\Q$udom\E$/,@{$hostedsessions->{'includedomain'}})) {
                   1030:                     $canhost = 1;
                   1031:                 } else {
                   1032:                     $canhost = 0;
                   1033:                 }
                   1034:             }
                   1035:         }
                   1036:     }
                   1037:     return $canhost;
                   1038: }
                   1039: 
1.1083    raeburn  1040: sub spare_can_host {
                   1041:     my ($udom,$uint_dom,$remotesessions,$try_server)=@_;
                   1042:     my $canhost=1;
                   1043:     my @intdoms;
                   1044:     my $internet_names = &Apache::lonnet::get_internet_names($try_server);
                   1045:     if (ref($internet_names) eq 'ARRAY') {
                   1046:         @intdoms = @{$internet_names};
                   1047:     }
                   1048:     unless (grep(/^\Q$uint_dom\E$/,@intdoms)) {
                   1049:         my $serverhomeID = &Apache::lonnet::get_server_homeID($try_server);
                   1050:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                   1051:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                   1052:         my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
                   1053:         $canhost = &can_host_session($udom,$try_server,$remoterev,
                   1054:                                      $remotesessions,
                   1055:                                      $defdomdefaults{'hostedsessions'});
                   1056:     }
                   1057:     return $canhost;
                   1058: }
                   1059: 
1.1       albertel 1060: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www      1061: 
1.599     albertel 1062: my %homecache;
1.1       albertel 1063: sub homeserver {
1.230     stredwic 1064:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel 1065:     my $index="$uname:$udom";
1.426     albertel 1066: 
1.599     albertel 1067:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel 1068: 
                   1069:     my %servers = &get_servers($udom,'library');
                   1070:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic 1071:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic 1072: 		 exists($badServerCache{$tryserver}));
1.841     albertel 1073: 
                   1074: 	my $answer=reply("home:$udom:$uname",$tryserver);
                   1075: 	if ($answer eq 'found') {
                   1076: 	    delete($badServerCache{$tryserver}); 
                   1077: 	    return $homecache{$index}=$tryserver;
                   1078: 	} elsif ($answer eq 'no_host') {
                   1079: 	    $badServerCache{$tryserver}=1;
                   1080: 	}
1.1       albertel 1081:     }    
                   1082:     return 'no_host';
1.70      www      1083: }
                   1084: 
                   1085: # ------------------------------------- Find the usernames behind a list of IDs
                   1086: 
                   1087: sub idget {
                   1088:     my ($udom,@ids)=@_;
                   1089:     my %returnhash=();
                   1090:     
1.841     albertel 1091:     my %servers = &get_servers($udom,'library');
                   1092:     foreach my $tryserver (keys(%servers)) {
                   1093: 	my $idlist=join('&',@ids);
                   1094: 	$idlist=~tr/A-Z/a-z/; 
                   1095: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                   1096: 	my @answer=();
                   1097: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                   1098: 	    @answer=split(/\&/,$reply);
                   1099: 	}                    ;
                   1100: 	my $i;
                   1101: 	for ($i=0;$i<=$#ids;$i++) {
                   1102: 	    if ($answer[$i]) {
                   1103: 		$returnhash{$ids[$i]}=$answer[$i];
                   1104: 	    } 
                   1105: 	}
                   1106:     } 
1.70      www      1107:     return %returnhash;
                   1108: }
                   1109: 
                   1110: # ------------------------------------- Find the IDs behind a list of usernames
                   1111: 
                   1112: sub idrget {
                   1113:     my ($udom,@unames)=@_;
                   1114:     my %returnhash=();
1.800     albertel 1115:     foreach my $uname (@unames) {
                   1116:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41 1117:     }
1.70      www      1118:     return %returnhash;
                   1119: }
                   1120: 
                   1121: # ------------------------------- Store away a list of names and associated IDs
                   1122: 
                   1123: sub idput {
                   1124:     my ($udom,%ids)=@_;
                   1125:     my %servers=();
1.800     albertel 1126:     foreach my $uname (keys(%ids)) {
                   1127: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                   1128:         my $uhom=&homeserver($uname,$udom);
1.70      www      1129:         if ($uhom ne 'no_host') {
1.800     albertel 1130:             my $id=&escape($ids{$uname});
1.70      www      1131:             $id=~tr/A-Z/a-z/;
1.800     albertel 1132:             my $esc_unam=&escape($uname);
1.70      www      1133: 	    if ($servers{$uhom}) {
1.800     albertel 1134: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www      1135:             } else {
1.800     albertel 1136:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www      1137:             }
                   1138:         }
1.191     harris41 1139:     }
1.800     albertel 1140:     foreach my $server (keys(%servers)) {
                   1141:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41 1142:     }
1.344     www      1143: }
                   1144: 
1.1023    raeburn  1145: # ------------------------------dump from db file owned by domainconfig user
1.1012    raeburn  1146: sub dump_dom {
1.1023    raeburn  1147:     my ($namespace,$udom,$regexp,$range)=@_;
1.1012    raeburn  1148:     if (!$udom) {
                   1149:         $udom=$env{'user.domain'};
                   1150:     }
                   1151:     my %returnhash;
1.1023    raeburn  1152:     if ($udom) {
                   1153:         my $uname = &get_domainconfiguser($udom);
                   1154:         %returnhash = &dump($namespace,$udom,$uname,$regexp,$range);
1.1012    raeburn  1155:     }
                   1156:     return %returnhash;
                   1157: }
                   1158: 
1.1023    raeburn  1159: # ------------------------------------------ get items from domain db files   
1.806     raeburn  1160: 
                   1161: sub get_dom {
1.860     raeburn  1162:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn  1163:     my $items='';
                   1164:     foreach my $item (@$storearr) {
                   1165:         $items.=&escape($item).'&';
                   1166:     }
                   1167:     $items=~s/\&$//;
1.860     raeburn  1168:     if (!$udom) {
                   1169:         $udom=$env{'user.domain'};
                   1170:         if (defined(&domain($udom,'primary'))) {
                   1171:             $uhome=&domain($udom,'primary');
                   1172:         } else {
1.874     albertel 1173:             undef($uhome);
1.860     raeburn  1174:         }
                   1175:     } else {
                   1176:         if (!$uhome) {
                   1177:             if (defined(&domain($udom,'primary'))) {
                   1178:                 $uhome=&domain($udom,'primary');
                   1179:             }
                   1180:         }
                   1181:     }
                   1182:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1183:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn  1184:         my %returnhash;
1.875     albertel 1185:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn  1186:             return %returnhash;
                   1187:         }
1.806     raeburn  1188:         my @pairs=split(/\&/,$rep);
                   1189:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   1190:             return @pairs;
                   1191:         }
                   1192:         my $i=0;
                   1193:         foreach my $item (@$storearr) {
                   1194:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                   1195:             $i++;
                   1196:         }
                   1197:         return %returnhash;
                   1198:     } else {
1.880     banghart 1199:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn  1200:     }
                   1201: }
                   1202: 
                   1203: # -------------------------------------------- put items in domain db files 
                   1204: 
                   1205: sub put_dom {
1.860     raeburn  1206:     my ($namespace,$storehash,$udom,$uhome)=@_;
                   1207:     if (!$udom) {
                   1208:         $udom=$env{'user.domain'};
                   1209:         if (defined(&domain($udom,'primary'))) {
                   1210:             $uhome=&domain($udom,'primary');
                   1211:         } else {
1.874     albertel 1212:             undef($uhome);
1.860     raeburn  1213:         }
                   1214:     } else {
                   1215:         if (!$uhome) {
                   1216:             if (defined(&domain($udom,'primary'))) {
                   1217:                 $uhome=&domain($udom,'primary');
                   1218:             }
                   1219:         }
                   1220:     } 
                   1221:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn  1222:         my $items='';
                   1223:         foreach my $item (keys(%$storehash)) {
                   1224:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                   1225:         }
                   1226:         $items=~s/\&$//;
                   1227:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                   1228:     } else {
1.860     raeburn  1229:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn  1230:     }
                   1231: }
                   1232: 
1.1023    raeburn  1233: # --------------------- newput for items in db file owned by domainconfig user
1.1012    raeburn  1234: sub newput_dom {
1.1023    raeburn  1235:     my ($namespace,$storehash,$udom) = @_;
1.1012    raeburn  1236:     my $result;
                   1237:     if (!$udom) {
                   1238:         $udom=$env{'user.domain'};
                   1239:     }
1.1023    raeburn  1240:     if ($udom) {
                   1241:         my $uname = &get_domainconfiguser($udom);
                   1242:         $result = &newput($namespace,$storehash,$udom,$uname);
1.1012    raeburn  1243:     }
                   1244:     return $result;
                   1245: }
                   1246: 
1.1023    raeburn  1247: # --------------------- delete for items in db file owned by domainconfig user
1.1012    raeburn  1248: sub del_dom {
1.1023    raeburn  1249:     my ($namespace,$storearr,$udom)=@_;
1.1012    raeburn  1250:     if (ref($storearr) eq 'ARRAY') {
                   1251:         if (!$udom) {
                   1252:             $udom=$env{'user.domain'};
                   1253:         }
1.1023    raeburn  1254:         if ($udom) {
                   1255:             my $uname = &get_domainconfiguser($udom); 
                   1256:             return &del($namespace,$storearr,$udom,$uname);
1.1012    raeburn  1257:         }
                   1258:     }
                   1259: }
                   1260: 
1.1023    raeburn  1261: # ----------------------------------construct domainconfig user for a domain 
                   1262: sub get_domainconfiguser {
                   1263:     my ($udom) = @_;
                   1264:     return $udom.'-domainconfig';
                   1265: }
                   1266: 
1.837     raeburn  1267: sub retrieve_inst_usertypes {
                   1268:     my ($udom) = @_;
                   1269:     my (%returnhash,@order);
1.989     raeburn  1270:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
                   1271:     if ((ref($domdefs{'inststatustypes'}) eq 'HASH') && 
                   1272:         (ref($domdefs{'inststatusorder'}) eq 'ARRAY')) {
                   1273:         %returnhash = %{$domdefs{'inststatustypes'}};
                   1274:         @order = @{$domdefs{'inststatusorder'}};
                   1275:     } else {
                   1276:         if (defined(&domain($udom,'primary'))) {
                   1277:             my $uhome=&domain($udom,'primary');
                   1278:             my $rep=&reply("inst_usertypes:$udom",$uhome);
                   1279:             if ($rep =~ /^(con_lost|error|no_such_host|refused)/) {
                   1280:                 &logthis("get_dom failed - $rep returned from $uhome in domain: $udom");
                   1281:                 return (\%returnhash,\@order);
                   1282:             }
                   1283:             my ($hashitems,$orderitems) = split(/:/,$rep); 
                   1284:             my @pairs=split(/\&/,$hashitems);
                   1285:             foreach my $item (@pairs) {
                   1286:                 my ($key,$value)=split(/=/,$item,2);
                   1287:                 $key = &unescape($key);
                   1288:                 next if ($key =~ /^error: 2 /);
                   1289:                 $returnhash{$key}=&thaw_unescape($value);
                   1290:             }
                   1291:             my @esc_order = split(/\&/,$orderitems);
                   1292:             foreach my $item (@esc_order) {
                   1293:                 push(@order,&unescape($item));
                   1294:             }
                   1295:         } else {
                   1296:             &logthis("get_dom failed - no primary domain server for $udom");
1.837     raeburn  1297:         }
                   1298:     }
                   1299:     return (\%returnhash,\@order);
                   1300: }
                   1301: 
1.868     raeburn  1302: sub is_domainimage {
                   1303:     my ($url) = @_;
                   1304:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                   1305:         if (&domain($1) ne '') {
                   1306:             return '1';
                   1307:         }
                   1308:     }
                   1309:     return;
                   1310: }
                   1311: 
1.899     raeburn  1312: sub inst_directory_query {
                   1313:     my ($srch) = @_;
                   1314:     my $udom = $srch->{'srchdomain'};
                   1315:     my %results;
                   1316:     my $homeserver = &domain($udom,'primary');
1.909     raeburn  1317:     my $outcome;
1.899     raeburn  1318:     if ($homeserver ne '') {
1.904     albertel 1319: 	my $queryid=&reply("querysend:instdirsearch:".
                   1320: 			   &escape($srch->{'srchby'}).':'.
                   1321: 			   &escape($srch->{'srchterm'}).':'.
                   1322: 			   &escape($srch->{'srchtype'}),$homeserver);
                   1323: 	my $host=&hostname($homeserver);
                   1324: 	if ($queryid !~/^\Q$host\E\_/) {
                   1325: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1326: 	    return;
                   1327: 	}
                   1328: 	my $response = &get_query_reply($queryid);
                   1329: 	my $maxtries = 5;
                   1330: 	my $tries = 1;
                   1331: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1332: 	    $response = &get_query_reply($queryid);
                   1333: 	    $tries ++;
                   1334: 	}
                   1335: 
                   1336:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn  1337:             if ($response eq 'unavailable') {
                   1338:                 $outcome = $response;
                   1339:             } else {
                   1340:                 $outcome = 'ok';
                   1341:                 my @matches = split(/\n/,$response);
                   1342:                 foreach my $match (@matches) {
                   1343:                     my ($key,$value) = split(/=/,$match);
                   1344:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                   1345:                 }
1.899     raeburn  1346:             }
                   1347:         }
                   1348:     }
1.909     raeburn  1349:     return ($outcome,%results);
1.899     raeburn  1350: }
                   1351: 
                   1352: sub usersearch {
                   1353:     my ($srch) = @_;
                   1354:     my $dom = $srch->{'srchdomain'};
                   1355:     my %results;
                   1356:     my %libserv = &all_library();
                   1357:     my $query = 'usersearch';
                   1358:     foreach my $tryserver (keys(%libserv)) {
                   1359:         if (&host_domain($tryserver) eq $dom) {
                   1360:             my $host=&hostname($tryserver);
                   1361:             my $queryid=
1.911     raeburn  1362:                 &reply("querysend:".&escape($query).':'.
                   1363:                        &escape($srch->{'srchby'}).':'.
1.899     raeburn  1364:                        &escape($srch->{'srchtype'}).':'.
                   1365:                        &escape($srch->{'srchterm'}),$tryserver);
                   1366:             if ($queryid !~/^\Q$host\E\_/) {
                   1367:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn  1368:                 next;
1.899     raeburn  1369:             }
                   1370:             my $reply = &get_query_reply($queryid);
                   1371:             my $maxtries = 1;
                   1372:             my $tries = 1;
                   1373:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   1374:                 $reply = &get_query_reply($queryid);
                   1375:                 $tries ++;
                   1376:             }
                   1377:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   1378:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                   1379:             } else {
1.911     raeburn  1380:                 my @matches;
                   1381:                 if ($reply =~ /\n/) {
                   1382:                     @matches = split(/\n/,$reply);
                   1383:                 } else {
                   1384:                     @matches = split(/\&/,$reply);
                   1385:                 }
1.899     raeburn  1386:                 foreach my $match (@matches) {
                   1387:                     my ($uname,$udom,%userhash);
1.911     raeburn  1388:                     foreach my $entry (split(/:/,$match)) {
                   1389:                         my ($key,$value) =
                   1390:                             map {&unescape($_);} split(/=/,$entry);
1.899     raeburn  1391:                         $userhash{$key} = $value;
                   1392:                         if ($key eq 'username') {
                   1393:                             $uname = $value;
                   1394:                         } elsif ($key eq 'domain') {
                   1395:                             $udom = $value;
1.911     raeburn  1396:                         }
1.899     raeburn  1397:                     }
                   1398:                     $results{$uname.':'.$udom} = \%userhash;
                   1399:                 }
                   1400:             }
                   1401:         }
                   1402:     }
                   1403:     return %results;
                   1404: }
                   1405: 
1.912     raeburn  1406: sub get_instuser {
                   1407:     my ($udom,$uname,$id) = @_;
                   1408:     my $homeserver = &domain($udom,'primary');
                   1409:     my ($outcome,%results);
                   1410:     if ($homeserver ne '') {
                   1411:         my $queryid=&reply("querysend:getinstuser:".&escape($uname).':'.
                   1412:                            &escape($id).':'.&escape($udom),$homeserver);
                   1413:         my $host=&hostname($homeserver);
                   1414:         if ($queryid !~/^\Q$host\E\_/) {
                   1415:             &logthis('get_instuser invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                   1416:             return;
                   1417:         }
                   1418:         my $response = &get_query_reply($queryid);
                   1419:         my $maxtries = 5;
                   1420:         my $tries = 1;
                   1421:         while (($response=~/^timeout/) && ($tries < $maxtries)) {
                   1422:             $response = &get_query_reply($queryid);
                   1423:             $tries ++;
                   1424:         }
                   1425:         if (!&error($response) && $response ne 'refused') {
                   1426:             if ($response eq 'unavailable') {
                   1427:                 $outcome = $response;
                   1428:             } else {
                   1429:                 $outcome = 'ok';
                   1430:                 my @matches = split(/\n/,$response);
                   1431:                 foreach my $match (@matches) {
                   1432:                     my ($key,$value) = split(/=/,$match);
                   1433:                     $results{&unescape($key)} = &thaw_unescape($value);
                   1434:                 }
                   1435:             }
                   1436:         }
                   1437:     }
                   1438:     my %userinfo;
                   1439:     if (ref($results{$uname}) eq 'HASH') {
                   1440:         %userinfo = %{$results{$uname}};
                   1441:     } 
                   1442:     return ($outcome,%userinfo);
                   1443: }
                   1444: 
                   1445: sub inst_rulecheck {
1.923     raeburn  1446:     my ($udom,$uname,$id,$item,$rules) = @_;
1.912     raeburn  1447:     my %returnhash;
                   1448:     if ($udom ne '') {
                   1449:         if (ref($rules) eq 'ARRAY') {
                   1450:             @{$rules} = map {&escape($_);} (@{$rules});
                   1451:             my $rulestr = join(':',@{$rules});
                   1452:             my $homeserver=&domain($udom,'primary');
                   1453:             if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1454:                 my $response;
                   1455:                 if ($item eq 'username') {                
                   1456:                     $response=&unescape(&reply('instrulecheck:'.&escape($udom).
                   1457:                                               ':'.&escape($uname).':'.$rulestr,
1.912     raeburn  1458:                                               $homeserver));
1.923     raeburn  1459:                 } elsif ($item eq 'id') {
                   1460:                     $response=&unescape(&reply('instidrulecheck:'.&escape($udom).
                   1461:                                               ':'.&escape($id).':'.$rulestr,
                   1462:                                               $homeserver));
1.945     raeburn  1463:                 } elsif ($item eq 'selfcreate') {
                   1464:                     $response=&unescape(&reply('instselfcreatecheck:'.
1.943     raeburn  1465:                                                &escape($udom).':'.&escape($uname).
                   1466:                                               ':'.$rulestr,$homeserver));
1.923     raeburn  1467:                 }
1.912     raeburn  1468:                 if ($response ne 'refused') {
                   1469:                     my @pairs=split(/\&/,$response);
                   1470:                     foreach my $item (@pairs) {
                   1471:                         my ($key,$value)=split(/=/,$item,2);
                   1472:                         $key = &unescape($key);
                   1473:                         next if ($key =~ /^error: 2 /);
                   1474:                         $returnhash{$key}=&thaw_unescape($value);
                   1475:                     }
                   1476:                 }
                   1477:             }
                   1478:         }
                   1479:     }
                   1480:     return %returnhash;
                   1481: }
                   1482: 
                   1483: sub inst_userrules {
1.923     raeburn  1484:     my ($udom,$check) = @_;
1.912     raeburn  1485:     my (%ruleshash,@ruleorder);
                   1486:     if ($udom ne '') {
                   1487:         my $homeserver=&domain($udom,'primary');
                   1488:         if (($homeserver ne '') && ($homeserver ne 'no_host')) {
1.923     raeburn  1489:             my $response;
                   1490:             if ($check eq 'id') {
                   1491:                 $response=&reply('instidrules:'.&escape($udom),
1.912     raeburn  1492:                                  $homeserver);
1.943     raeburn  1493:             } elsif ($check eq 'email') {
                   1494:                 $response=&reply('instemailrules:'.&escape($udom),
                   1495:                                  $homeserver);
1.923     raeburn  1496:             } else {
                   1497:                 $response=&reply('instuserrules:'.&escape($udom),
                   1498:                                  $homeserver);
                   1499:             }
1.912     raeburn  1500:             if (($response ne 'refused') && ($response ne 'error') && 
1.923     raeburn  1501:                 ($response ne 'unknown_cmd') && 
1.912     raeburn  1502:                 ($response ne 'no_such_host')) {
                   1503:                 my ($hashitems,$orderitems) = split(/:/,$response);
                   1504:                 my @pairs=split(/\&/,$hashitems);
                   1505:                 foreach my $item (@pairs) {
                   1506:                     my ($key,$value)=split(/=/,$item,2);
                   1507:                     $key = &unescape($key);
                   1508:                     next if ($key =~ /^error: 2 /);
                   1509:                     $ruleshash{$key}=&thaw_unescape($value);
                   1510:                 }
                   1511:                 my @esc_order = split(/\&/,$orderitems);
                   1512:                 foreach my $item (@esc_order) {
                   1513:                     push(@ruleorder,&unescape($item));
                   1514:                 }
                   1515:             }
                   1516:         }
                   1517:     }
                   1518:     return (\%ruleshash,\@ruleorder);
                   1519: }
                   1520: 
1.976     raeburn  1521: # ------------- Get Authentication, Language and User Tools Defaults for Domain
1.943     raeburn  1522: 
                   1523: sub get_domain_defaults {
                   1524:     my ($domain) = @_;
                   1525:     my $cachetime = 60*60*24;
                   1526:     my ($result,$cached)=&is_cached_new('domdefaults',$domain);
                   1527:     if (defined($cached)) {
                   1528:         if (ref($result) eq 'HASH') {
                   1529:             return %{$result};
                   1530:         }
                   1531:     }
                   1532:     my %domdefaults;
                   1533:     my %domconfig =
1.989     raeburn  1534:          &Apache::lonnet::get_dom('configuration',['defaults','quotas',
1.1047    raeburn  1535:                                   'requestcourses','inststatus',
1.1073    raeburn  1536:                                   'coursedefaults','usersessions'],$domain);
1.943     raeburn  1537:     if (ref($domconfig{'defaults'}) eq 'HASH') {
                   1538:         $domdefaults{'lang_def'} = $domconfig{'defaults'}{'lang_def'}; 
                   1539:         $domdefaults{'auth_def'} = $domconfig{'defaults'}{'auth_def'};
                   1540:         $domdefaults{'auth_arg_def'} = $domconfig{'defaults'}{'auth_arg_def'};
1.982     raeburn  1541:         $domdefaults{'timezone_def'} = $domconfig{'defaults'}{'timezone_def'};
1.985     raeburn  1542:         $domdefaults{'datelocale_def'} = $domconfig{'defaults'}{'datelocale_def'};
1.943     raeburn  1543:     } else {
                   1544:         $domdefaults{'lang_def'} = &domain($domain,'lang_def');
                   1545:         $domdefaults{'auth_def'} = &domain($domain,'auth_def');
                   1546:         $domdefaults{'auth_arg_def'} = &domain($domain,'auth_arg_def');
                   1547:     }
1.976     raeburn  1548:     if (ref($domconfig{'quotas'}) eq 'HASH') {
                   1549:         if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   1550:             $domdefaults{'defaultquota'} = $domconfig{'quotas'}{'defaultquota'};
                   1551:         } else {
                   1552:             $domdefaults{'defaultquota'} = $domconfig{'quotas'};
                   1553:         } 
                   1554:         my @usertools = ('aboutme','blog','portfolio');
                   1555:         foreach my $item (@usertools) {
                   1556:             if (ref($domconfig{'quotas'}{$item}) eq 'HASH') {
                   1557:                 $domdefaults{$item} = $domconfig{'quotas'}{$item};
                   1558:             }
                   1559:         }
                   1560:     }
1.985     raeburn  1561:     if (ref($domconfig{'requestcourses'}) eq 'HASH') {
1.1006    raeburn  1562:         foreach my $item ('official','unofficial','community') {
1.985     raeburn  1563:             $domdefaults{$item} = $domconfig{'requestcourses'}{$item};
                   1564:         }
                   1565:     }
1.989     raeburn  1566:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
                   1567:         foreach my $item ('inststatustypes','inststatusorder') {
                   1568:             $domdefaults{$item} = $domconfig{'inststatus'}{$item};
                   1569:         }
                   1570:     }
1.1047    raeburn  1571:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
                   1572:         foreach my $item ('canuse_pdfforms') {
                   1573:             $domdefaults{$item} = $domconfig{'coursedefaults'}{$item};
                   1574:         }
                   1575:     }
1.1073    raeburn  1576:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   1577:         if (ref($domconfig{'usersessions'}{'remote'}) eq 'HASH') {
                   1578:             $domdefaults{'remotesessions'} = $domconfig{'usersessions'}{'remote'};
                   1579:         }
                   1580:         if (ref($domconfig{'usersessions'}{'hosted'}) eq 'HASH') {
                   1581:             $domdefaults{'hostedsessions'} = $domconfig{'usersessions'}{'hosted'};
                   1582:         }
                   1583:     }
1.943     raeburn  1584:     &Apache::lonnet::do_cache_new('domdefaults',$domain,\%domdefaults,
                   1585:                                   $cachetime);
                   1586:     return %domdefaults;
                   1587: }
                   1588: 
1.344     www      1589: # --------------------------------------------------- Assign a key to a student
                   1590: 
                   1591: sub assign_access_key {
1.364     www      1592: #
                   1593: # a valid key looks like uname:udom#comments
                   1594: # comments are being appended
                   1595: #
1.498     www      1596:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                   1597:     $kdom=
1.620     albertel 1598:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www      1599:     $knum=
1.620     albertel 1600:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www      1601:     $cdom=
1.620     albertel 1602:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1603:     $cnum=
1.620     albertel 1604:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1605:     $udom=$env{'user.name'} unless (defined($udom));
                   1606:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www      1607:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www      1608:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel 1609:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www      1610:                                                   # assigned to this person
                   1611:                                                   # - this should not happen,
1.345     www      1612:                                                   # unless something went wrong
                   1613:                                                   # the first time around
                   1614: # ready to assign
1.364     www      1615:         $logentry=$1.'; '.$logentry;
1.496     www      1616:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www      1617:                                                  $kdom,$knum) eq 'ok') {
1.345     www      1618: # key now belongs to user
1.346     www      1619: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www      1620:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
1.949     raeburn  1621:                 &appenv({'environment.'.$envkey => $ckey});
1.345     www      1622:                 return 'ok';
                   1623:             } else {
                   1624:                 return 
                   1625:   'error: Count not permanently assign key, will need to be re-entered later.';
                   1626: 	    }
                   1627:         } else {
                   1628:             return 'error: Could not assign key, try again later.';
                   1629:         }
1.364     www      1630:     } elsif (!$existing{$ckey}) {
1.345     www      1631: # the key does not exist
                   1632: 	return 'error: The key does not exist';
                   1633:     } else {
                   1634: # the key is somebody else's
                   1635: 	return 'error: The key is already in use';
                   1636:     }
1.344     www      1637: }
                   1638: 
1.364     www      1639: # ------------------------------------------ put an additional comment on a key
                   1640: 
                   1641: sub comment_access_key {
                   1642: #
                   1643: # a valid key looks like uname:udom#comments
                   1644: # comments are being appended
                   1645: #
                   1646:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   1647:     $cdom=
1.620     albertel 1648:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      1649:     $cnum=
1.620     albertel 1650:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      1651:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   1652:     if ($existing{$ckey}) {
                   1653:         $existing{$ckey}.='; '.$logentry;
                   1654: # ready to assign
1.367     www      1655:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      1656:                                                  $cdom,$cnum) eq 'ok') {
                   1657: 	    return 'ok';
                   1658:         } else {
                   1659: 	    return 'error: Count not store comment.';
                   1660:         }
                   1661:     } else {
                   1662: # the key does not exist
                   1663: 	return 'error: The key does not exist';
                   1664:     }
                   1665: }
                   1666: 
1.344     www      1667: # ------------------------------------------------------ Generate a set of keys
                   1668: 
                   1669: sub generate_access_keys {
1.364     www      1670:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      1671:     $cdom=
1.620     albertel 1672:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1673:     $cnum=
1.620     albertel 1674:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      1675:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      1676:     unless (($cdom) && ($cnum)) { return 0; }
                   1677:     if ($number>10000) { return 0; }
                   1678:     sleep(2); # make sure don't get same seed twice
                   1679:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   1680:     my $total=0;
                   1681:     for (my $i=1;$i<=$number;$i++) {
                   1682:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   1683:                   sprintf("%lx",int(100000*rand)).'-'.
                   1684:                   sprintf("%lx",int(100000*rand));
                   1685:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   1686:        $newkey=~s/0/h/g; # and also 0 and O
                   1687:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   1688:        if ($existing{$newkey}) {
                   1689:            $i--;
                   1690:        } else {
1.364     www      1691: 	  if (&put('accesskeys',
                   1692:               { $newkey => '# generated '.localtime().
1.620     albertel 1693:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      1694:                            '; '.$logentry },
                   1695: 		   $cdom,$cnum) eq 'ok') {
1.344     www      1696:               $total++;
                   1697: 	  }
                   1698:        }
                   1699:     }
1.620     albertel 1700:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      1701:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   1702:     return $total;
                   1703: }
                   1704: 
                   1705: # ------------------------------------------------------- Validate an accesskey
                   1706: 
                   1707: sub validate_access_key {
                   1708:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   1709:     $cdom=
1.620     albertel 1710:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1711:     $cnum=
1.620     albertel 1712:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1713:     $udom=$env{'user.domain'} unless (defined($udom));
                   1714:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      1715:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 1716:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      1717: }
                   1718: 
                   1719: # ------------------------------------- Find the section of student in a course
1.652     albertel 1720: sub devalidate_getsection_cache {
                   1721:     my ($udom,$unam,$courseid)=@_;
                   1722:     my $hashid="$udom:$unam:$courseid";
                   1723:     &devalidate_cache_new('getsection',$hashid);
                   1724: }
1.298     matthew  1725: 
1.815     albertel 1726: sub courseid_to_courseurl {
                   1727:     my ($courseid) = @_;
                   1728:     #already url style courseid
                   1729:     return $courseid if ($courseid =~ m{^/});
                   1730: 
                   1731:     if (exists($env{'course.'.$courseid.'.num'})) {
                   1732: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   1733: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   1734: 	return "/$cdom/$cnum";
                   1735:     }
                   1736: 
                   1737:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   1738:     if (exists($courseinfo{'num'})) {
                   1739: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   1740:     }
                   1741: 
                   1742:     return undef;
                   1743: }
                   1744: 
1.298     matthew  1745: sub getsection {
                   1746:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 1747:     my $cachetime=1800;
1.551     albertel 1748: 
                   1749:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 1750:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 1751:     if (defined($cached)) { return $result; }
                   1752: 
1.298     matthew  1753:     my %Pending; 
                   1754:     my %Expired;
                   1755:     #
                   1756:     # Each role can either have not started yet (pending), be active, 
                   1757:     #    or have expired.
                   1758:     #
                   1759:     # If there is an active role, we are done.
                   1760:     #
                   1761:     # If there is more than one role which has not started yet, 
                   1762:     #     choose the one which will start sooner
                   1763:     # If there is one role which has not started yet, return it.
                   1764:     #
                   1765:     # If there is more than one expired role, choose the one which ended last.
                   1766:     # If there is a role which has expired, return it.
                   1767:     #
1.815     albertel 1768:     $courseid = &courseid_to_courseurl($courseid);
1.1086    raeburn  1769:     my $extra = &freeze_escape({'skipcheck' => 1});
                   1770:     my %roleshash = &dump('roles',$udom,$unam,$courseid,undef,$extra);
1.817     raeburn  1771:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1772:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1773:         my $section=$1;
                   1774:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1775:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1776:         my $now=time;
1.548     albertel 1777:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1778:             $Expired{$end}=$section;
                   1779:             next;
                   1780:         }
1.548     albertel 1781:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1782:             $Pending{$start}=$section;
                   1783:             next;
                   1784:         }
1.599     albertel 1785:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1786:     }
                   1787:     #
                   1788:     # Presumedly there will be few matching roles from the above
                   1789:     # loop and the sorting time will be negligible.
                   1790:     if (scalar(keys(%Pending))) {
                   1791:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1792:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1793:     } 
                   1794:     if (scalar(keys(%Expired))) {
                   1795:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1796:         my $time = pop(@sorted);
1.599     albertel 1797:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1798:     }
1.599     albertel 1799:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1800: }
1.70      www      1801: 
1.599     albertel 1802: sub save_cache {
                   1803:     &purge_remembered();
1.722     albertel 1804:     #&Apache::loncommon::validate_page();
1.620     albertel 1805:     undef(%env);
1.780     albertel 1806:     undef($env_loaded);
1.599     albertel 1807: }
1.452     albertel 1808: 
1.599     albertel 1809: my $to_remember=-1;
                   1810: my %remembered;
                   1811: my %accessed;
                   1812: my $kicks=0;
                   1813: my $hits=0;
1.849     albertel 1814: sub make_key {
                   1815:     my ($name,$id) = @_;
1.872     albertel 1816:     if (length($id) > 65 
                   1817: 	&& length(&escape($id)) > 200) {
                   1818: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   1819:     }
1.849     albertel 1820:     return &escape($name.':'.$id);
                   1821: }
                   1822: 
1.599     albertel 1823: sub devalidate_cache_new {
                   1824:     my ($name,$id,$debug) = @_;
                   1825:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1826:     $id=&make_key($name,$id);
1.599     albertel 1827:     $memcache->delete($id);
                   1828:     delete($remembered{$id});
                   1829:     delete($accessed{$id});
                   1830: }
                   1831: 
                   1832: sub is_cached_new {
                   1833:     my ($name,$id,$debug) = @_;
1.849     albertel 1834:     $id=&make_key($name,$id);
1.599     albertel 1835:     if (exists($remembered{$id})) {
                   1836: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1837: 	$accessed{$id}=[&gettimeofday()];
                   1838: 	$hits++;
                   1839: 	return ($remembered{$id},1);
                   1840:     }
                   1841:     my $value = $memcache->get($id);
                   1842:     if (!(defined($value))) {
                   1843: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1844: 	return (undef,undef);
1.416     albertel 1845:     }
1.599     albertel 1846:     if ($value eq '__undef__') {
                   1847: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1848: 	$value=undef;
                   1849:     }
                   1850:     &make_room($id,$value,$debug);
                   1851:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1852:     return ($value,1);
                   1853: }
                   1854: 
                   1855: sub do_cache_new {
                   1856:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1857:     $id=&make_key($name,$id);
1.599     albertel 1858:     my $setvalue=$value;
                   1859:     if (!defined($setvalue)) {
                   1860: 	$setvalue='__undef__';
                   1861:     }
1.623     albertel 1862:     if (!defined($time) ) {
                   1863: 	$time=600;
                   1864:     }
1.599     albertel 1865:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 1866:     my $result = $memcache->set($id,$setvalue,$time);
                   1867:     if (! $result) {
1.872     albertel 1868: 	&logthis("caching of id -> $id  failed");
1.910     albertel 1869: 	$memcache->disconnect_all();
1.872     albertel 1870:     }
1.600     albertel 1871:     # need to make a copy of $value
1.919     albertel 1872:     &make_room($id,$value,$debug);
1.599     albertel 1873:     return $value;
                   1874: }
                   1875: 
                   1876: sub make_room {
                   1877:     my ($id,$value,$debug)=@_;
1.919     albertel 1878: 
                   1879:     $remembered{$id}= (ref($value)) ? &Storable::dclone($value)
                   1880:                                     : $value;
1.599     albertel 1881:     if ($to_remember<0) { return; }
                   1882:     $accessed{$id}=[&gettimeofday()];
                   1883:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1884:     my $to_kick;
                   1885:     my $max_time=0;
                   1886:     foreach my $other (keys(%accessed)) {
                   1887: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1888: 	    $to_kick=$other;
                   1889: 	    $max_time=&tv_interval($accessed{$other});
                   1890: 	}
                   1891:     }
                   1892:     delete($remembered{$to_kick});
                   1893:     delete($accessed{$to_kick});
                   1894:     $kicks++;
                   1895:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1896:     return;
                   1897: }
                   1898: 
1.599     albertel 1899: sub purge_remembered {
1.604     albertel 1900:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1901:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1902:     undef(%remembered);
                   1903:     undef(%accessed);
1.428     albertel 1904: }
1.70      www      1905: # ------------------------------------- Read an entry from a user's environment
                   1906: 
                   1907: sub userenvironment {
                   1908:     my ($udom,$unam,@what)=@_;
1.976     raeburn  1909:     my $items;
                   1910:     foreach my $item (@what) {
                   1911:         $items.=&escape($item).'&';
                   1912:     }
                   1913:     $items=~s/\&$//;
1.70      www      1914:     my %returnhash=();
1.1009    raeburn  1915:     my $uhome = &homeserver($unam,$udom);
                   1916:     unless ($uhome eq 'no_host') {
                   1917:         my @answer=split(/\&/, 
                   1918:             &reply('get:'.$udom.':'.$unam.':environment:'.$items,$uhome));
1.1048    raeburn  1919:         if ($#answer==0 && $answer[0] =~ /^(con_lost|error:|no_such_host)/i) {
                   1920:             return %returnhash;
                   1921:         }
1.1009    raeburn  1922:         my $i;
                   1923:         for ($i=0;$i<=$#what;$i++) {
                   1924: 	    $returnhash{$what[$i]}=&unescape($answer[$i]);
                   1925:         }
1.70      www      1926:     }
                   1927:     return %returnhash;
1.1       albertel 1928: }
                   1929: 
1.617     albertel 1930: # ---------------------------------------------------------- Get a studentphoto
                   1931: sub studentphoto {
                   1932:     my ($udom,$unam,$ext) = @_;
                   1933:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1934:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1935:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1936:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1937:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1938:             } else {
                   1939:                 my ($result,$perm_reqd)=
1.707     albertel 1940: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1941:                 if ($result eq 'ok') {
                   1942:                     if (!($perm_reqd eq 'yes')) {
                   1943:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1944:                     }
                   1945:                 }
                   1946:             }
                   1947:         }
                   1948:     } else {
                   1949:         my ($result,$perm_reqd) = 
1.707     albertel 1950: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1951:         if ($result eq 'ok') {
                   1952:             if (!($perm_reqd eq 'yes')) {
                   1953:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1954:             }
                   1955:         }
                   1956:     }
                   1957:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1958: }
                   1959: 
                   1960: sub retrievestudentphoto {
                   1961:     my ($udom,$unam,$ext,$type) = @_;
                   1962:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1963:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1964:     if ($ret eq 'ok') {
                   1965:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1966:         if ($type eq 'thumbnail') {
                   1967:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1968:         }
                   1969:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1970:         return $tokenurl;
                   1971:     } else {
                   1972:         if ($type eq 'thumbnail') {
                   1973:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1974:         } else { 
                   1975:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1976:         }
1.617     albertel 1977:     }
                   1978: }
                   1979: 
1.263     www      1980: # -------------------------------------------------------------------- New chat
                   1981: 
                   1982: sub chatsend {
1.724     raeburn  1983:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1984:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1985:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1986:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1987:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1988: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1989: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1990: }
                   1991: 
                   1992: # ------------------------------------------ Find current version of a resource
                   1993: 
                   1994: sub getversion {
                   1995:     my $fname=&clutter(shift);
                   1996:     unless ($fname=~/^\/res\//) { return -1; }
                   1997:     return &currentversion(&filelocation('',$fname));
                   1998: }
                   1999: 
                   2000: sub currentversion {
                   2001:     my $fname=shift;
1.599     albertel 2002:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      2003:     if (defined($cached)) { return $result; }
1.292     www      2004:     my $author=$fname;
                   2005:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2006:     my ($udom,$uname)=split(/\//,$author);
                   2007:     my $home=homeserver($uname,$udom);
                   2008:     if ($home eq 'no_host') { 
                   2009:         return -1; 
                   2010:     }
                   2011:     my $answer=reply("currentversion:$fname",$home);
                   2012:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2013: 	return -1;
                   2014:     }
1.599     albertel 2015:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      2016: }
                   2017: 
1.1111  ! www      2018: #
        !          2019: # Return special version number of resource if set by override, empty otherwise
        !          2020: #
        !          2021: sub usedversion {
        !          2022:     my $fname=shift;
        !          2023:     unless ($fname) { $fname=$env{'request.uri'}; }
        !          2024:     my ($urlversion)=($fname=~/\.(\d+)\.\w+$/);
        !          2025:     if ($urlversion) { return $urlversion; }
        !          2026:     return '';
        !          2027: }
        !          2028: 
1.1       albertel 2029: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      2030: 
1.1       albertel 2031: sub subscribe {
                   2032:     my $fname=shift;
1.761     raeburn  2033:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 2034:     $fname=~s/[\n\r]//g;
1.1       albertel 2035:     my $author=$fname;
                   2036:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2037:     my ($udom,$uname)=split(/\//,$author);
                   2038:     my $home=homeserver($uname,$udom);
1.335     albertel 2039:     if ($home eq 'no_host') {
                   2040:         return 'not_found';
1.1       albertel 2041:     }
                   2042:     my $answer=reply("sub:$fname",$home);
1.64      www      2043:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   2044: 	$answer.=' by '.$home;
                   2045:     }
1.1       albertel 2046:     return $answer;
                   2047: }
                   2048:     
1.8       www      2049: # -------------------------------------------------------------- Replicate file
                   2050: 
                   2051: sub repcopy {
                   2052:     my $filename=shift;
1.23      www      2053:     $filename=~s/\/+/\//g;
1.607     raeburn  2054:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   2055:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 2056:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 2057: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 2058: 	return &repcopy_userfile($filename);
                   2059:     }
1.532     albertel 2060:     $filename=~s/[\n\r]//g;
1.8       www      2061:     my $transname="$filename.in.transfer";
1.828     www      2062: # FIXME: this should flock
1.607     raeburn  2063:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      2064:     my $remoteurl=subscribe($filename);
1.64      www      2065:     if ($remoteurl =~ /^con_lost by/) {
                   2066: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2067:            return 'unavailable';
1.8       www      2068:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 2069: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  2070: 	   return 'not_found';
1.64      www      2071:     } elsif ($remoteurl =~ /^rejected by/) {
                   2072: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  2073:            return 'forbidden';
1.20      www      2074:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  2075:            return 'ok';
1.8       www      2076:     } else {
1.290     www      2077:         my $author=$filename;
                   2078:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   2079:         my ($udom,$uname)=split(/\//,$author);
                   2080:         my $home=homeserver($uname,$udom);
                   2081:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      2082:            my @parts=split(/\//,$filename);
                   2083:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   2084:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   2085:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  2086: 	       return 'bad_request';
1.8       www      2087:            }
                   2088:            my $count;
                   2089:            for ($count=5;$count<$#parts;$count++) {
                   2090:                $path.="/$parts[$count]";
                   2091:                if ((-e $path)!=1) {
                   2092: 		   mkdir($path,0777);
                   2093:                }
                   2094:            }
                   2095:            my $ua=new LWP::UserAgent;
                   2096:            my $request=new HTTP::Request('GET',"$remoteurl");
                   2097:            my $response=$ua->request($request,$transname);
                   2098:            if ($response->is_error()) {
                   2099: 	       unlink($transname);
                   2100:                my $message=$response->status_line;
1.672     albertel 2101:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      2102:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  2103:                return 'unavailable';
1.8       www      2104:            } else {
1.16      www      2105: 	       if ($remoteurl!~/\.meta$/) {
                   2106:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   2107:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   2108:                   if ($mresponse->is_error()) {
                   2109: 		      unlink($filename.'.meta');
                   2110:                       &logthis(
1.672     albertel 2111:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      2112:                   }
                   2113: 	       }
1.8       www      2114:                rename($transname,$filename);
1.607     raeburn  2115:                return 'ok';
1.8       www      2116:            }
1.290     www      2117:        }
1.8       www      2118:     }
1.330     www      2119: }
                   2120: 
                   2121: # ------------------------------------------------ Get server side include body
                   2122: sub ssi_body {
1.381     albertel 2123:     my ($filelink,%form)=@_;
1.606     matthew  2124:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   2125:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   2126:     }
1.953     www      2127:     my $output='';
                   2128:     my $response;
1.980     raeburn  2129:     if ($filelink=~/^https?\:/) {
1.954     raeburn  2130:        ($output,$response)=&externalssi($filelink);
1.953     www      2131:     } else {
1.1004    droeschl 2132:        $filelink .= $filelink=~/\?/ ? '&' : '?';
                   2133:        $filelink .= 'inhibitmenu=yes';
1.953     www      2134:        ($output,$response)=&ssi($filelink,%form);
                   2135:     }
1.778     albertel 2136:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 2137:     $output=~s/^.*?\<body[^\>]*\>//si;
1.930     albertel 2138:     $output=~s/\<\/body\s*\>.*?$//si;
1.953     www      2139:     if (wantarray) {
                   2140:         return ($output, $response);
                   2141:     } else {
                   2142:         return $output;
                   2143:     }
1.8       www      2144: }
                   2145: 
1.15      www      2146: # --------------------------------------------------------- Server Side Include
                   2147: 
1.782     albertel 2148: sub absolute_url {
                   2149:     my ($host_name) = @_;
                   2150:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   2151:     if ($host_name eq '') {
                   2152: 	$host_name = $ENV{'SERVER_NAME'};
                   2153:     }
                   2154:     return $protocol.$host_name;
                   2155: }
                   2156: 
1.942     foxr     2157: #
                   2158: #   Server side include.
                   2159: # Parameters:
                   2160: #  fn     Possibly encrypted resource name/id.
                   2161: #  form   Hash that describes how the rendering should be done
                   2162: #         and other things.
1.944     foxr     2163: # Returns:
1.950     raeburn  2164: #   Scalar context: The content of the response.
                   2165: #   Array context:  2 element list of the content and the full response object.
1.942     foxr     2166: #     
1.15      www      2167: sub ssi {
                   2168: 
1.944     foxr     2169:     my ($fn,%form)=@_;
1.15      www      2170:     my $ua=new LWP::UserAgent;
1.23      www      2171:     my $request;
1.711     albertel 2172: 
                   2173:     $form{'no_update_last_known'}=1;
1.895     albertel 2174:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      2175:     if (%form) {
1.782     albertel 2176:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.1000    raeburn  2177:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys(%form)));
1.23      www      2178:     } else {
1.782     albertel 2179:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      2180:     }
                   2181: 
1.15      www      2182:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   2183:     my $response=$ua->request($request);
                   2184: 
1.944     foxr     2185:     if (wantarray) {
                   2186: 	return ($response->content, $response);
                   2187:     } else {
                   2188: 	return $response->content;
1.942     foxr     2189:     }
1.324     www      2190: }
                   2191: 
                   2192: sub externalssi {
                   2193:     my ($url)=@_;
                   2194:     my $ua=new LWP::UserAgent;
                   2195:     my $request=new HTTP::Request('GET',$url);
                   2196:     my $response=$ua->request($request);
1.954     raeburn  2197:     if (wantarray) {
                   2198:         return ($response->content, $response);
                   2199:     } else {
                   2200:         return $response->content;
                   2201:     }
1.15      www      2202: }
1.254     www      2203: 
1.492     albertel 2204: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   2205: 
                   2206: sub allowuploaded {
                   2207:     my ($srcurl,$url)=@_;
                   2208:     $url=&clutter(&declutter($url));
                   2209:     my $dir=$url;
                   2210:     $dir=~s/\/[^\/]+$//;
                   2211:     my %httpref=();
                   2212:     my $httpurl=&hreflocation('',$url);
                   2213:     $httpref{'httpref.'.$httpurl}=$srcurl;
1.949     raeburn  2214:     &Apache::lonnet::appenv(\%httpref);
1.254     www      2215: }
1.477     raeburn  2216: 
1.478     albertel 2217: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 2218: # input: action, courseID, current domain, intended
1.637     raeburn  2219: #        path to file, source of file, instruction to parse file for objects,
                   2220: #        ref to hash for embedded objects,
                   2221: #        ref to hash for codebase of java objects.
1.1095    raeburn  2222: #        reference to scalar to accommodate mime type determined
                   2223: #          from File::MMagic if $parser = parse.
1.637     raeburn  2224: #
1.485     raeburn  2225: # output: url to file (if action was uploaddoc), 
                   2226: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  2227: #
1.478     albertel 2228: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   2229: # course.
1.477     raeburn  2230: #
1.478     albertel 2231: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2232: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   2233: #          course's home server.
1.477     raeburn  2234: #
1.478     albertel 2235: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   2236: #          be copied from $source (current location) to 
                   2237: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2238: #         and will then be copied to
                   2239: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   2240: #         course's home server.
1.485     raeburn  2241: #
1.481     raeburn  2242: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 2243: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  2244: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   2245: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   2246: #         in course's home server.
1.637     raeburn  2247: #
1.477     raeburn  2248: 
                   2249: sub process_coursefile {
1.1095    raeburn  2250:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase,
                   2251:         $mimetype)=@_;
1.477     raeburn  2252:     my $fetchresult;
1.638     albertel 2253:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  2254:     if ($action eq 'propagate') {
1.638     albertel 2255:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   2256: 			     $home);
1.481     raeburn  2257:     } else {
1.477     raeburn  2258:         my $fpath = '';
                   2259:         my $fname = $file;
1.478     albertel 2260:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  2261:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  2262:         my $filepath = &build_filepath($fpath);
1.481     raeburn  2263:         if ($action eq 'copy') {
                   2264:             if ($source eq '') {
                   2265:                 $fetchresult = 'no source file';
                   2266:                 return $fetchresult;
                   2267:             } else {
                   2268:                 my $destination = $filepath.'/'.$fname;
                   2269:                 rename($source,$destination);
                   2270:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2271:                                  $home);
1.481     raeburn  2272:             }
                   2273:         } elsif ($action eq 'uploaddoc') {
                   2274:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 2275:             print $fh $env{'form.'.$source};
1.481     raeburn  2276:             close($fh);
1.637     raeburn  2277:             if ($parser eq 'parse') {
1.1024    raeburn  2278:                 my $mm = new File::MMagic;
1.1095    raeburn  2279:                 my $type = $mm->checktype_filename($filepath.'/'.$fname);
                   2280:                 if ($type eq 'text/html') {
1.1024    raeburn  2281:                     my $parse_result = &extract_embedded_items($filepath.'/'.$fname,$allfiles,$codebase);
                   2282:                     unless ($parse_result eq 'ok') {
                   2283:                         &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   2284:                     }
1.637     raeburn  2285:                 }
1.1095    raeburn  2286:                 if (ref($mimetype)) {
                   2287:                     $$mimetype = $type;
                   2288:                 } 
1.637     raeburn  2289:             }
1.477     raeburn  2290:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2291:                                  $home);
1.481     raeburn  2292:             if ($fetchresult eq 'ok') {
                   2293:                 return '/uploaded/'.$fpath.'/'.$fname;
                   2294:             } else {
                   2295:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2296:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  2297:                 return '/adm/notfound.html';
                   2298:             }
1.477     raeburn  2299:         }
                   2300:     }
1.485     raeburn  2301:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  2302:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 2303:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  2304:     }
                   2305:     return $fetchresult;
                   2306: }
                   2307: 
1.637     raeburn  2308: sub build_filepath {
                   2309:     my ($fpath) = @_;
                   2310:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   2311:     unless ($fpath eq '') {
                   2312:         my @parts=split('/',$fpath);
                   2313:         foreach my $part (@parts) {
                   2314:             $filepath.= '/'.$part;
                   2315:             if ((-e $filepath)!=1) {
                   2316:                 mkdir($filepath,0777);
                   2317:             }
                   2318:         }
                   2319:     }
                   2320:     return $filepath;
                   2321: }
                   2322: 
                   2323: sub store_edited_file {
1.638     albertel 2324:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  2325:     my $file = $primary_url;
                   2326:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   2327:     my $fpath = '';
                   2328:     my $fname = $file;
                   2329:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   2330:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   2331:     my $filepath = &build_filepath($fpath);
                   2332:     open(my $fh,'>'.$filepath.'/'.$fname);
                   2333:     print $fh $content;
                   2334:     close($fh);
1.638     albertel 2335:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  2336:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 2337: 			  $home);
1.637     raeburn  2338:     if ($$fetchresult eq 'ok') {
                   2339:         return '/uploaded/'.$fpath.'/'.$fname;
                   2340:     } else {
1.638     albertel 2341:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   2342: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  2343:         return '/adm/notfound.html';
                   2344:     }
                   2345: }
                   2346: 
1.531     albertel 2347: sub clean_filename {
1.831     albertel 2348:     my ($fname,$args)=@_;
1.315     www      2349: # Replace Windows backslashes by forward slashes
1.257     www      2350:     $fname=~s/\\/\//g;
1.831     albertel 2351:     if (!$args->{'keep_path'}) {
                   2352:         # Get rid of everything but the actual filename
                   2353: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   2354:     }
1.315     www      2355: # Replace spaces by underscores
                   2356:     $fname=~s/\s+/\_/g;
                   2357: # Replace all other weird characters by nothing
1.831     albertel 2358:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 2359: # Replace all .\d. sequences with _\d. so they no longer look like version
                   2360: # numbers
                   2361:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 2362:     return $fname;
                   2363: }
1.1051    raeburn  2364: # This Function checks if an Image's dimensions exceed either $resizewidth (width) 
                   2365: # or $resizeheight (height) - both pixels. If so, the image is scaled to produce an 
                   2366: # image with the same aspect ratio as the original, but with dimensions which do 
                   2367: # not exceed $resizewidth and $resizeheight.
                   2368:  
1.984     neumanie 2369: sub resizeImage {
1.1051    raeburn  2370:     my ($img_path,$resizewidth,$resizeheight) = @_;
                   2371:     my $ima = Image::Magick->new;
                   2372:     my $resized;
                   2373:     if (-e $img_path) {
                   2374:         $ima->Read($img_path);
                   2375:         if (($resizewidth =~ /^\d+$/) && ($resizeheight > 0)) {
                   2376:             my $width = $ima->Get('width');
                   2377:             my $height = $ima->Get('height');
                   2378:             if ($width > $resizewidth) {
                   2379: 	        my $factor = $width/$resizewidth;
                   2380:                 my $newheight = $height/$factor;
                   2381:                 $ima->Scale(width=>$resizewidth,height=>$newheight);
                   2382:                 $resized = 1;
                   2383:             }
                   2384:         }
                   2385:         if (($resizeheight =~ /^\d+$/) && ($resizeheight > 0)) {
                   2386:             my $width = $ima->Get('width');
                   2387:             my $height = $ima->Get('height');
                   2388:             if ($height > $resizeheight) {
                   2389:                 my $factor = $height/$resizeheight;
                   2390:                 my $newwidth = $width/$factor;
                   2391:                 $ima->Scale(width=>$newwidth,height=>$resizeheight);
                   2392:                 $resized = 1;
                   2393:             }
                   2394:         }
                   2395:         if ($resized) {
                   2396:             $ima->Write($img_path);
                   2397:         }
                   2398:     }
                   2399:     return;
1.977     amueller 2400: }
                   2401: 
1.608     albertel 2402: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 2403: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.1093    raeburn  2404: #                    the desired filename is in $env{"form.$formname.filename"}
1.1090    raeburn  2405: #        $context - possible values: coursedoc, existingfile, overwrite, 
                   2406: #                                    canceloverwrite, or ''. 
                   2407: #                   if 'coursedoc': upload to the current course
                   2408: #                   if 'existingfile': write file to tmp/overwrites directory 
                   2409: #                   if 'canceloverwrite': delete file written to tmp/overwrites directory
                   2410: #                   $context is passed as argument to &finishuserfileupload
1.686     albertel 2411: #        $subdir - directory in userfile to store the file into
1.858     raeburn  2412: #        $parser - instruction to parse file for objects ($parser = parse)    
                   2413: #        $allfiles - reference to hash for embedded objects
                   2414: #        $codebase - reference to hash for codebase of java objects
                   2415: #        $desuname - username for permanent storage of uploaded file
                   2416: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  2417: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   2418: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.1051    raeburn  2419: #        $resizewidth - width (pixels) to which to resize uploaded image
                   2420: #        $resizeheight - height (pixels) to which to resize uploaded image
1.1095    raeburn  2421: #        $mimetype - reference to scalar to accommodate mime type determined
                   2422: #                    from File::MMagic if $parser = parse.
1.858     raeburn  2423: # 
1.686     albertel 2424: # output: url of file in userspace, or error: <message> 
                   2425: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 2426: 
1.531     albertel 2427: sub userfileupload {
1.1090    raeburn  2428:     my ($formname,$context,$subdir,$parser,$allfiles,$codebase,$destuname,
1.1095    raeburn  2429:         $destudom,$thumbwidth,$thumbheight,$resizewidth,$resizeheight,$mimetype)=@_;
1.531     albertel 2430:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 2431:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 2432:     $fname=&clean_filename($fname);
1.1090    raeburn  2433:     # See if there is anything left
1.257     www      2434:     unless ($fname) { return 'error: no uploaded file'; }
1.1090    raeburn  2435:     # Files uploaded to help request form, or uploaded to "create course" page are handled differently
                   2436:     if ((($formname eq 'screenshot') && ($subdir eq 'helprequests')) ||
                   2437:         (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) ||
                   2438:          ($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
1.523     raeburn  2439:         my $now = time;
1.1090    raeburn  2440:         my $filepath;
1.1095    raeburn  2441:         if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) {
1.1090    raeburn  2442:              $filepath = 'tmp/helprequests/'.$now;
                   2443:         } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) {
                   2444:              $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   2445:                          '_'.$env{'user.domain'}.'/pending';
                   2446:         } elsif (($context eq 'existingfile') || ($context eq 'canceloverwrite')) {
                   2447:             my ($docuname,$docudom);
                   2448:             if ($destudom) {
                   2449:                 $docudom = $destudom;
                   2450:             } else {
                   2451:                 $docudom = $env{'user.domain'};
                   2452:             }
                   2453:             if ($destuname) {
                   2454:                 $docuname = $destuname;
                   2455:             } else {
                   2456:                 $docuname = $env{'user.name'};
                   2457:             }
                   2458:             if (exists($env{'form.group'})) {
                   2459:                 $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2460:                 $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2461:             }
                   2462:             $filepath = 'tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$subdir;
                   2463:             if ($context eq 'canceloverwrite') {
                   2464:                 my $tempfile =  $perlvar{'lonDaemons'}.'/'.$filepath.'/'.$fname;
                   2465:                 if (-e  $tempfile) {
                   2466:                     my @info = stat($tempfile);
                   2467:                     if ($info[9] eq $env{'form.timestamp'}) {
                   2468:                         unlink($tempfile);
                   2469:                     }
                   2470:                 }
                   2471:                 return;
1.523     raeburn  2472:             }
                   2473:         }
1.1090    raeburn  2474:         # Create the directory if not present
1.741     raeburn  2475:         my @parts=split(/\//,$filepath);
                   2476:         my $fullpath = $perlvar{'lonDaemons'};
                   2477:         for (my $i=0;$i<@parts;$i++) {
                   2478:             $fullpath .= '/'.$parts[$i];
                   2479:             if ((-e $fullpath)!=1) {
                   2480:                 mkdir($fullpath,0777);
                   2481:             }
                   2482:         }
                   2483:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   2484:         print $fh $env{'form.'.$formname};
                   2485:         close($fh);
1.1090    raeburn  2486:         if ($context eq 'existingfile') {
                   2487:             my @info = stat($fullpath.'/'.$fname);
                   2488:             return ($fullpath.'/'.$fname,$info[9]);
                   2489:         } else {
                   2490:             return $fullpath.'/'.$fname;
                   2491:         }
1.523     raeburn  2492:     }
1.995     raeburn  2493:     if ($subdir eq 'scantron') {
                   2494:         $fname = 'scantron_orig_'.$fname;
1.1093    raeburn  2495:     } else {
1.995     raeburn  2496:         $fname="$subdir/$fname";
                   2497:     }
1.1090    raeburn  2498:     if ($context eq 'coursedoc') {
1.638     albertel 2499: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2500: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  2501:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 2502:             return &finishuserfileupload($docuname,$docudom,
                   2503: 					 $formname,$fname,$parser,$allfiles,
1.1051    raeburn  2504: 					 $codebase,$thumbwidth,$thumbheight,
1.1095    raeburn  2505:                                          $resizewidth,$resizeheight,$context,$mimetype);
1.481     raeburn  2506:         } else {
1.620     albertel 2507:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 2508:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   2509: 				       $fname,$formname,$parser,
1.1095    raeburn  2510: 				       $allfiles,$codebase,$mimetype);
1.481     raeburn  2511:         }
1.719     banghart 2512:     } elsif (defined($destuname)) {
                   2513:         my $docuname=$destuname;
                   2514:         my $docudom=$destudom;
1.860     raeburn  2515: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2516: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  2517:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  2518:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      2519:     } else {
1.638     albertel 2520:         my $docuname=$env{'user.name'};
                   2521:         my $docudom=$env{'user.domain'};
1.714     raeburn  2522:         if (exists($env{'form.group'})) {
                   2523:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   2524:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   2525:         }
1.860     raeburn  2526: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   2527: 				     $parser,$allfiles,$codebase,
1.1051    raeburn  2528:                                      $thumbwidth,$thumbheight,
1.1095    raeburn  2529:                                      $resizewidth,$resizeheight,$context,$mimetype);
1.259     www      2530:     }
1.271     www      2531: }
                   2532: 
                   2533: sub finishuserfileupload {
1.860     raeburn  2534:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
1.1095    raeburn  2535:         $thumbwidth,$thumbheight,$resizewidth,$resizeheight,$context,$mimetype) = @_;
1.477     raeburn  2536:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      2537:     my $filepath=$perlvar{'lonDocRoot'};
1.984     neumanie 2538:   
1.860     raeburn  2539:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 2540:     $file=$fname;
                   2541:     if ($fname=~m|/|) {
                   2542:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   2543: 	$path.=$fnamepath.'/';
                   2544:     }
1.259     www      2545:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      2546:     my $count;
                   2547:     for ($count=4;$count<=$#parts;$count++) {
                   2548:         $filepath.="/$parts[$count]";
                   2549:         if ((-e $filepath)!=1) {
                   2550: 	    mkdir($filepath,0777);
                   2551:         }
                   2552:     }
1.984     neumanie 2553: 
1.258     www      2554: # Save the file
                   2555:     {
1.701     albertel 2556: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   2557: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   2558: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   2559: 	    return '/adm/notfound.html';
                   2560: 	}
1.1090    raeburn  2561:         if ($context eq 'overwrite') {
                   2562:             my $source =  $perlvar{'lonDaemons'}.'/tmp/overwrites/'.$docudom.'/'.$docuname.'/'.$fname;
                   2563:             my $target = $filepath.'/'.$file;
                   2564:             if (-e $source) {
                   2565:                 my @info = stat($source);
                   2566:                 if ($info[9] eq $env{'form.timestamp'}) {   
                   2567:                     unless (&File::Copy::move($source,$target)) {
                   2568:                         &logthis('Failed to overwrite '.$filepath.'/'.$file);
                   2569:                         return "Moving from $source failed";
                   2570:                     }
                   2571:                 } else {
                   2572:                     return "Temporary file: $source had unexpected date/time for last modification";
                   2573:                 }
                   2574:             } else {
                   2575:                 return "Temporary file: $source missing";
                   2576:             }
                   2577:         } elsif (!print FH ($env{'form.'.$formname})) {
1.701     albertel 2578: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   2579: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   2580: 	    return '/adm/notfound.html';
                   2581: 	}
1.570     albertel 2582: 	close(FH);
1.1051    raeburn  2583:         if ($resizewidth && $resizeheight) {
                   2584:             my $mm = new File::MMagic;
                   2585:             my $mime_type = $mm->checktype_filename($filepath.'/'.$file);
                   2586:             if ($mime_type =~ m{^image/}) {
                   2587: 	        &resizeImage($filepath.'/'.$file,$resizewidth,$resizeheight);
                   2588:             }  
1.977     amueller 2589: 	}
1.258     www      2590:     }
1.637     raeburn  2591:     if ($parser eq 'parse') {
1.1024    raeburn  2592:         my $mm = new File::MMagic;
1.1095    raeburn  2593:         my $type = $mm->checktype_filename($filepath.'/'.$file);
                   2594:         if ($type eq 'text/html') {
1.1024    raeburn  2595:             my $parse_result = &extract_embedded_items($filepath.'/'.$file,
                   2596:                                                        $allfiles,$codebase);
                   2597:             unless ($parse_result eq 'ok') {
                   2598:                 &logthis('Failed to parse '.$filepath.$file.
                   2599: 	   	         ' for embedded media: '.$parse_result); 
                   2600:             }
1.637     raeburn  2601:         }
1.1095    raeburn  2602:         if (ref($mimetype)) {
                   2603:             $$mimetype = $type;
                   2604:         }
1.637     raeburn  2605:     }
1.860     raeburn  2606:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   2607:         my $input = $filepath.'/'.$file;
                   2608:         my $output = $filepath.'/'.'tn-'.$file;
                   2609:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   2610:         system("convert -sample $thumbsize $input $output");
                   2611:         if (-e $filepath.'/'.'tn-'.$file) {
                   2612:             $fetchthumb  = 1; 
                   2613:         }
                   2614:     }
1.858     raeburn  2615:  
1.259     www      2616: # Notify homeserver to grep it
                   2617: #
1.984     neumanie 2618:     my $docuhome=&homeserver($docuname,$docudom);	
1.494     albertel 2619:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      2620:     if ($fetchresult eq 'ok') {
1.860     raeburn  2621:         if ($fetchthumb) {
                   2622:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   2623:             if ($thumbresult ne 'ok') {
                   2624:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   2625:                          $docuhome.': '.$thumbresult);
                   2626:             }
                   2627:         }
1.259     www      2628: #
1.258     www      2629: # Return the URL to it
1.494     albertel 2630:         return '/uploaded/'.$path.$file;
1.263     www      2631:     } else {
1.494     albertel 2632:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   2633: 		 ': '.$fetchresult);
1.263     www      2634:         return '/adm/notfound.html';
1.858     raeburn  2635:     }
1.493     albertel 2636: }
                   2637: 
1.637     raeburn  2638: sub extract_embedded_items {
1.961     raeburn  2639:     my ($fullpath,$allfiles,$codebase,$content) = @_;
1.637     raeburn  2640:     my @state = ();
                   2641:     my %javafiles = (
                   2642:                       codebase => '',
                   2643:                       code => '',
                   2644:                       archive => ''
                   2645:                     );
                   2646:     my %mediafiles = (
                   2647:                       src => '',
                   2648:                       movie => '',
                   2649:                      );
1.648     raeburn  2650:     my $p;
                   2651:     if ($content) {
                   2652:         $p = HTML::LCParser->new($content);
                   2653:     } else {
1.961     raeburn  2654:         $p = HTML::LCParser->new($fullpath);
1.648     raeburn  2655:     }
1.641     albertel 2656:     while (my $t=$p->get_token()) {
1.640     albertel 2657: 	if ($t->[0] eq 'S') {
                   2658: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 2659: 	    push(@state, $tagname);
1.648     raeburn  2660:             if (lc($tagname) eq 'allow') {
                   2661:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   2662:             }
1.640     albertel 2663: 	    if (lc($tagname) eq 'img') {
                   2664: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   2665: 	    }
1.886     albertel 2666: 	    if (lc($tagname) eq 'a') {
                   2667: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   2668: 	    }
1.645     raeburn  2669:             if (lc($tagname) eq 'script') {
                   2670:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   2671:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   2672:                 } else {
                   2673:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   2674:                 }
                   2675:             }
                   2676:             if (lc($tagname) eq 'link') {
                   2677:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   2678:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   2679:                 }
                   2680:             }
1.640     albertel 2681: 	    if (lc($tagname) eq 'object' ||
                   2682: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   2683: 		foreach my $item (keys(%javafiles)) {
                   2684: 		    $javafiles{$item} = '';
                   2685: 		}
                   2686: 	    }
                   2687: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   2688: 		my $name = lc($attr->{'name'});
                   2689: 		foreach my $item (keys(%javafiles)) {
                   2690: 		    if ($name eq $item) {
                   2691: 			$javafiles{$item} = $attr->{'value'};
                   2692: 			last;
                   2693: 		    }
                   2694: 		}
                   2695: 		foreach my $item (keys(%mediafiles)) {
                   2696: 		    if ($name eq $item) {
                   2697: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   2698: 			last;
                   2699: 		    }
                   2700: 		}
                   2701: 	    }
                   2702: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   2703: 		foreach my $item (keys(%javafiles)) {
                   2704: 		    if ($attr->{$item}) {
                   2705: 			$javafiles{$item} = $attr->{$item};
                   2706: 			last;
                   2707: 		    }
                   2708: 		}
                   2709: 		foreach my $item (keys(%mediafiles)) {
                   2710: 		    if ($attr->{$item}) {
                   2711: 			&add_filetype($allfiles,$attr->{$item},$item);
                   2712: 			last;
                   2713: 		    }
                   2714: 		}
                   2715: 	    }
                   2716: 	} elsif ($t->[0] eq 'E') {
                   2717: 	    my ($tagname) = ($t->[1]);
                   2718: 	    if ($javafiles{'codebase'} ne '') {
                   2719: 		$javafiles{'codebase'} .= '/';
                   2720: 	    }  
                   2721: 	    if (lc($tagname) eq 'applet' ||
                   2722: 		lc($tagname) eq 'object' ||
                   2723: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   2724: 		) {
                   2725: 		foreach my $item (keys(%javafiles)) {
                   2726: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   2727: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   2728: 			&add_filetype($allfiles,$file,$item);
                   2729: 		    }
                   2730: 		}
                   2731: 	    } 
                   2732: 	    pop @state;
                   2733: 	}
                   2734:     }
1.637     raeburn  2735:     return 'ok';
                   2736: }
                   2737: 
1.639     albertel 2738: sub add_filetype {
                   2739:     my ($allfiles,$file,$type)=@_;
                   2740:     if (exists($allfiles->{$file})) {
                   2741: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   2742: 	    push(@{$allfiles->{$file}}, &escape($type));
                   2743: 	}
                   2744:     } else {
                   2745: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  2746:     }
                   2747: }
                   2748: 
1.493     albertel 2749: sub removeuploadedurl {
1.984     neumanie 2750:     my ($url)=@_;	
                   2751:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);    
1.613     albertel 2752:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 2753: }
                   2754: 
                   2755: sub removeuserfile {
                   2756:     my ($docuname,$docudom,$fname)=@_;
1.984     neumanie 2757:     my $home=&homeserver($docuname,$docudom);    
1.798     raeburn  2758:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
1.984     neumanie 2759:     if ($result eq 'ok') {	
1.798     raeburn  2760:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   2761:             my $metafile = $fname.'.meta';
                   2762:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 2763: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
1.984     neumanie 2764:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];	   
1.821     raeburn  2765:             my $sqlresult = 
1.823     albertel 2766:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  2767:                                         'portfolio_metadata',$group,
                   2768:                                         'delete');
1.798     raeburn  2769:         }
                   2770:     }
                   2771:     return $result;
1.257     www      2772: }
1.15      www      2773: 
1.530     albertel 2774: sub mkdiruserfile {
                   2775:     my ($docuname,$docudom,$dir)=@_;
                   2776:     my $home=&homeserver($docuname,$docudom);
                   2777:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   2778: }
                   2779: 
1.531     albertel 2780: sub renameuserfile {
                   2781:     my ($docuname,$docudom,$old,$new)=@_;
                   2782:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  2783:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   2784:                         &escape("$old").':'.&escape("$new"),$home);
                   2785:     if ($result eq 'ok') {
                   2786:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   2787:             my $oldmeta = $old.'.meta';
                   2788:             my $newmeta = $new.'.meta';
                   2789:             my $metaresult = 
                   2790:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 2791: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   2792:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  2793:             my $sqlresult = 
1.823     albertel 2794:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  2795:                                         'portfolio_metadata',$group,
                   2796:                                         'delete');
1.798     raeburn  2797:         }
                   2798:     }
                   2799:     return $result;
1.531     albertel 2800: }
                   2801: 
1.14      www      2802: # ------------------------------------------------------------------------- Log
                   2803: 
                   2804: sub log {
                   2805:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      2806:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      2807: }
                   2808: 
                   2809: # ------------------------------------------------------------------ Course Log
1.352     www      2810: #
                   2811: # This routine flushes several buffers of non-mission-critical nature
                   2812: #
1.157     www      2813: 
                   2814: sub flushcourselogs {
1.352     www      2815:     &logthis('Flushing log buffers');
                   2816: #
                   2817: # course logs
                   2818: # This is a log of all transactions in a course, which can be used
                   2819: # for data mining purposes
                   2820: #
                   2821: # It also collects the courseid database, which lists last transaction
                   2822: # times and course titles for all courseids
                   2823: #
                   2824:     my %courseidbuffer=();
1.921     raeburn  2825:     foreach my $crsid (keys(%courselogs)) {
1.352     www      2826:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      2827: 		          &escape($courselogs{$crsid}),
                   2828: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      2829: 	    delete $courselogs{$crsid};
                   2830:         } else {
                   2831:             &logthis('Failed to flush log buffer for '.$crsid);
                   2832:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 2833:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      2834:                         " exceeded maximum size, deleting.</font>");
                   2835:                delete $courselogs{$crsid};
                   2836:             }
1.352     www      2837:         }
1.920     raeburn  2838:         $courseidbuffer{$coursehombuf{$crsid}}{$crsid} = {
1.936     raeburn  2839:             'description' => $coursedescrbuf{$crsid},
                   2840:             'inst_code'    => $courseinstcodebuf{$crsid},
                   2841:             'type'        => $coursetypebuf{$crsid},
                   2842:             'owner'       => $courseownerbuf{$crsid},
1.920     raeburn  2843:         };
1.191     harris41 2844:     }
1.352     www      2845: #
                   2846: # Write course id database (reverse lookup) to homeserver of courses 
                   2847: # Is used in pickcourse
                   2848: #
1.840     albertel 2849:     foreach my $crs_home (keys(%courseidbuffer)) {
1.918     raeburn  2850:         my $response = &courseidput(&host_domain($crs_home),
1.921     raeburn  2851:                                     $courseidbuffer{$crs_home},
                   2852:                                     $crs_home,'timeonly');
1.352     www      2853:     }
                   2854: #
                   2855: # File accesses
                   2856: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   2857: #
1.449     matthew  2858:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  2859:         if ($entry =~ /___count$/) {
                   2860:             my ($dom,$name);
1.807     albertel 2861:             ($dom,$name,undef)=
1.811     albertel 2862: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  2863:             if (! defined($dom) || $dom eq '' || 
                   2864:                 ! defined($name) || $name eq '') {
1.620     albertel 2865:                 my $cid = $env{'request.course.id'};
                   2866:                 $dom  = $env{'request.'.$cid.'.domain'};
                   2867:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  2868:             }
1.450     matthew  2869:             my $value = $accesshash{$entry};
                   2870:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   2871:             my %temphash=($url => $value);
1.449     matthew  2872:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   2873:             if ($result eq 'ok') {
                   2874:                 delete $accesshash{$entry};
                   2875:             } elsif ($result eq 'unknown_cmd') {
                   2876:                 # Target server has old code running on it.
1.450     matthew  2877:                 my %temphash=($entry => $value);
1.449     matthew  2878:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2879:                     delete $accesshash{$entry};
                   2880:                 }
                   2881:             }
                   2882:         } else {
1.811     albertel 2883:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  2884:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  2885:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2886:                 delete $accesshash{$entry};
                   2887:             }
1.185     www      2888:         }
1.191     harris41 2889:     }
1.352     www      2890: #
                   2891: # Roles
                   2892: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   2893: #
1.800     albertel 2894:     foreach my $entry (keys(%userrolehash)) {
1.351     www      2895:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      2896: 	    split(/\:/,$entry);
                   2897:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      2898:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      2899:                 $rudom,$runame) eq 'ok') {
                   2900: 	    delete $userrolehash{$entry};
                   2901:         }
                   2902:     }
1.662     raeburn  2903: #
                   2904: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   2905: #
                   2906:     my %domrolebuffer = ();
1.1000    raeburn  2907:     foreach my $entry (keys(%domainrolehash)) {
1.901     albertel 2908:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  2909:         if ($domrolebuffer{$rudom}) {
                   2910:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   2911:                       '='.&escape($domainrolehash{$entry});
                   2912:         } else {
                   2913:             $domrolebuffer{$rudom}.=&escape($entry).
                   2914:                       '='.&escape($domainrolehash{$entry});
                   2915:         }
                   2916:         delete $domainrolehash{$entry};
                   2917:     }
                   2918:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 2919: 	my %servers = &get_servers($dom,'library');
                   2920: 	foreach my $tryserver (keys(%servers)) {
                   2921: 	    unless (&reply('domroleput:'.$dom.':'.
                   2922: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   2923: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   2924: 	    }
1.662     raeburn  2925:         }
                   2926:     }
1.186     www      2927:     $dumpcount++;
1.157     www      2928: }
                   2929: 
                   2930: sub courselog {
                   2931:     my $what=shift;
1.158     www      2932:     $what=time.':'.$what;
1.620     albertel 2933:     unless ($env{'request.course.id'}) { return ''; }
                   2934:     $coursedombuf{$env{'request.course.id'}}=
                   2935:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2936:     $coursenumbuf{$env{'request.course.id'}}=
                   2937:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2938:     $coursehombuf{$env{'request.course.id'}}=
                   2939:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2940:     $coursedescrbuf{$env{'request.course.id'}}=
                   2941:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2942:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2943:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2944:     $courseownerbuf{$env{'request.course.id'}}=
                   2945:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2946:     $coursetypebuf{$env{'request.course.id'}}=
                   2947:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2948:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2949: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2950:     } else {
1.620     albertel 2951: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2952:     }
1.620     albertel 2953:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2954: 	&flushcourselogs();
                   2955:     }
1.158     www      2956: }
                   2957: 
                   2958: sub courseacclog {
                   2959:     my $fnsymb=shift;
1.620     albertel 2960:     unless ($env{'request.course.id'}) { return ''; }
                   2961:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2962:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2963:         $what.=':POST';
1.583     matthew  2964:         # FIXME: Probably ought to escape things....
1.800     albertel 2965: 	foreach my $key (keys(%env)) {
                   2966:             if ($key=~/^form\.(.*)/) {
1.975     raeburn  2967:                 my $formitem = $1;
                   2968:                 if ($formitem =~ /^HWFILE(?:SIZE|TOOBIG)/) {
                   2969:                     $what.=':'.$formitem.'='.$env{$key};
                   2970:                 } elsif ($formitem !~ /^HWFILE(?:[^.]+)$/) {
                   2971:                     $what.=':'.$formitem.'='.$env{$key};
                   2972:                 }
1.158     www      2973:             }
1.191     harris41 2974:         }
1.583     matthew  2975:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2976:         # FIXME: We should not be depending on a form parameter that someone
                   2977:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2978:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2979:             $what.= ':POST';
                   2980:             # FIXME: Probably ought to escape things....
                   2981:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2982:                                  'crsdiscuss') {
1.620     albertel 2983:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2984:             }
                   2985:         }
1.158     www      2986:     }
                   2987:     &courselog($what);
1.149     www      2988: }
                   2989: 
1.185     www      2990: sub countacc {
                   2991:     my $url=&declutter(shift);
1.458     matthew  2992:     return if (! defined($url) || $url eq '');
1.620     albertel 2993:     unless ($env{'request.course.id'}) { return ''; }
                   2994:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2995:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2996:     $accesshash{$key}++;
1.185     www      2997: }
1.349     www      2998: 
1.361     www      2999: sub linklog {
                   3000:     my ($from,$to)=@_;
                   3001:     $from=&declutter($from);
                   3002:     $to=&declutter($to);
                   3003:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   3004:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   3005: }
                   3006:   
1.349     www      3007: sub userrolelog {
                   3008:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  3009:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  3010:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  3011:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
1.1041    raeburn  3012:         ($trole=~/^ta/) || ($trole=~/^co/)) {
1.350     www      3013:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3014:        $userrolehash
                   3015:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      3016:                     =$tend.':'.$tstart;
1.662     raeburn  3017:     }
1.898     albertel 3018:     if (($env{'request.role'} =~ /dc\./) &&
                   3019: 	(($trole=~/^au/) || ($trole=~/^in/) ||
                   3020: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
1.1041    raeburn  3021: 	 ($trole=~/^cr/) || ($trole=~/^ta/) ||
                   3022:          ($trole=~/^co/))) {
1.898     albertel 3023:        $userrolehash
                   3024:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   3025:                     =$tend.':'.$tstart;
                   3026:     }
1.662     raeburn  3027:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   3028:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   3029:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   3030:         ($trole=~/^sc/)) {
                   3031:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   3032:        $domainrolehash
                   3033:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   3034:                     = $tend.':'.$tstart;
                   3035:     }
1.351     www      3036: }
                   3037: 
1.957     raeburn  3038: sub courserolelog {
                   3039:     my ($trole,$username,$domain,$area,$tstart,$tend,$delflag,$selfenroll,$context)=@_;
                   3040:     if (($trole eq 'cc') || ($trole eq 'in') ||
                   3041:         ($trole eq 'ep') || ($trole eq 'ad') ||
                   3042:         ($trole eq 'ta') || ($trole eq 'st') ||
1.1044    raeburn  3043:         ($trole=~/^cr/) || ($trole eq 'gr') ||
                   3044:         ($trole eq 'co')) {
1.957     raeburn  3045:         if ($area =~ m-^/($match_domain)/($match_courseid)/?([^/]*)-) {
                   3046:             my $cdom = $1;
                   3047:             my $cnum = $2;
                   3048:             my $sec = $3;
                   3049:             my $namespace = 'rolelog';
                   3050:             my %storehash = (
                   3051:                                role    => $trole,
                   3052:                                start   => $tstart,
                   3053:                                end     => $tend,
                   3054:                                selfenroll => $selfenroll,
                   3055:                                context    => $context,
                   3056:                             );
                   3057:             if ($trole eq 'gr') {
                   3058:                 $namespace = 'groupslog';
                   3059:                 $storehash{'group'} = $sec;
                   3060:             } else {
                   3061:                 $storehash{'section'} = $sec;
                   3062:             }
                   3063:             &instructor_log($namespace,\%storehash,$delflag,$username,$domain,$cnum,$cdom);
1.996     raeburn  3064:             if (($trole ne 'st') || ($sec ne '')) {
                   3065:                 &devalidate_cache_new('getcourseroles',$cdom.'_'.$cnum);
                   3066:             }
1.957     raeburn  3067:         }
                   3068:     }
                   3069:     return;
                   3070: }
                   3071: 
1.351     www      3072: sub get_course_adv_roles {
1.948     raeburn  3073:     my ($cid,$codes) = @_;
1.620     albertel 3074:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      3075:     my %coursehash=&coursedescription($cid);
1.988     raeburn  3076:     my $crstype = &Apache::loncommon::course_type($cid);
1.470     www      3077:     my %nothide=();
1.800     albertel 3078:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
1.937     raeburn  3079:         if ($user !~ /:/) {
                   3080: 	    $nothide{join(':',split(/[\@]/,$user))}=1;
                   3081:         } else {
                   3082:             $nothide{$user}=1;
                   3083:         }
1.470     www      3084:     }
1.351     www      3085:     my %returnhash=();
                   3086:     my %dumphash=
                   3087:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   3088:     my $now=time;
1.997     raeburn  3089:     my %privileged;
1.1000    raeburn  3090:     foreach my $entry (keys(%dumphash)) {
1.800     albertel 3091: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      3092:         if (($tstart) && ($tstart<0)) { next; }
                   3093:         if (($tend) && ($tend<$now)) { next; }
                   3094:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 3095:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 3096: 	if ($username eq '' || $domain eq '') { next; }
1.997     raeburn  3097:         unless (ref($privileged{$domain}) eq 'HASH') {
                   3098:             my %dompersonnel =
1.998     raeburn  3099:                 &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
1.997     raeburn  3100:             $privileged{$domain} = {};
                   3101:             foreach my $server (keys(%dompersonnel)) {
1.999     raeburn  3102:                 if (ref($dompersonnel{$server}) eq 'HASH') {
1.997     raeburn  3103:                     foreach my $user (keys(%{$dompersonnel{$server}})) {
                   3104:                         my ($trole,$uname,$udom) = split(/:/,$user);
                   3105:                         $privileged{$udom}{$uname} = 1;
                   3106:                     }
                   3107:                 }
                   3108:             }
                   3109:         }
                   3110:         if ((exists($privileged{$domain}{$username})) && 
                   3111:             (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 3112: 	if ($role eq 'cr') { next; }
1.948     raeburn  3113:         if ($codes) {
                   3114:             if ($section) { $role .= ':'.$section; }
                   3115:             if ($returnhash{$role}) {
                   3116:                 $returnhash{$role}.=','.$username.':'.$domain;
                   3117:             } else {
                   3118:                 $returnhash{$role}=$username.':'.$domain;
                   3119:             }
1.351     www      3120:         } else {
1.988     raeburn  3121:             my $key=&plaintext($role,$crstype);
1.973     bisitz   3122:             if ($section) { $key.=' ('.&Apache::lonlocal::mt('Section [_1]',$section).')'; }
1.948     raeburn  3123:             if ($returnhash{$key}) {
                   3124: 	        $returnhash{$key}.=','.$username.':'.$domain;
                   3125:             } else {
                   3126:                 $returnhash{$key}=$username.':'.$domain;
                   3127:             }
1.351     www      3128:         }
1.948     raeburn  3129:     }
1.400     www      3130:     return %returnhash;
                   3131: }
                   3132: 
                   3133: sub get_my_roles {
1.937     raeburn  3134:     my ($uname,$udom,$context,$types,$roles,$roledoms,$withsec,$hidepriv)=@_;
1.620     albertel 3135:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   3136:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.937     raeburn  3137:     my (%dumphash,%nothide);
1.1086    raeburn  3138:     if ($context eq 'userroles') {
                   3139:         my $extra = &freeze_escape({'skipcheck' => 1});
                   3140:         %dumphash = &dump('roles',$udom,$uname,'.',undef,$extra);
1.858     raeburn  3141:     } else {
                   3142:         %dumphash=
1.400     www      3143:             &dump('nohist_userroles',$udom,$uname);
1.937     raeburn  3144:         if ($hidepriv) {
                   3145:             my %coursehash=&coursedescription($udom.'_'.$uname);
                   3146:             foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   3147:                 if ($user !~ /:/) {
                   3148:                     $nothide{join(':',split(/[\@]/,$user))} = 1;
                   3149:                 } else {
                   3150:                     $nothide{$user} = 1;
                   3151:                 }
                   3152:             }
                   3153:         }
1.858     raeburn  3154:     }
1.400     www      3155:     my %returnhash=();
                   3156:     my $now=time;
1.999     raeburn  3157:     my %privileged;
1.800     albertel 3158:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  3159:         my ($role,$tend,$tstart);
                   3160:         if ($context eq 'userroles') {
                   3161: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   3162:         } else {
                   3163:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   3164:         }
1.400     www      3165:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  3166:         my $status = 'active';
1.939     raeburn  3167:         if (($tend) && ($tend<=$now)) {
1.832     raeburn  3168:             $status = 'previous';
                   3169:         } 
                   3170:         if (($tstart) && ($now<$tstart)) {
                   3171:             $status = 'future';
                   3172:         }
                   3173:         if (ref($types) eq 'ARRAY') {
                   3174:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   3175:                 next;
                   3176:             } 
                   3177:         } else {
                   3178:             if ($status ne 'active') {
                   3179:                 next;
                   3180:             }
                   3181:         }
1.867     raeburn  3182:         my ($rolecode,$username,$domain,$section,$area);
                   3183:         if ($context eq 'userroles') {
                   3184:             ($area,$rolecode) = split(/_/,$entry);
                   3185:             (undef,$domain,$username,$section) = split(/\//,$area);
                   3186:         } else {
                   3187:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   3188:         }
1.832     raeburn  3189:         if (ref($roledoms) eq 'ARRAY') {
                   3190:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   3191:                 next;
                   3192:             }
                   3193:         }
                   3194:         if (ref($roles) eq 'ARRAY') {
                   3195:             if (!grep(/^\Q$role\E$/,@{$roles})) {
1.922     raeburn  3196:                 if ($role =~ /^cr\//) {
                   3197:                     if (!grep(/^cr$/,@{$roles})) {
                   3198:                         next;
                   3199:                     }
1.1104    raeburn  3200:                 } elsif ($role =~ /^gr\//) {
                   3201:                     if (!grep(/^gr$/,@{$roles})) {
                   3202:                         next;
                   3203:                     }
1.922     raeburn  3204:                 } else {
                   3205:                     next;
                   3206:                 }
1.832     raeburn  3207:             }
1.867     raeburn  3208:         }
1.937     raeburn  3209:         if ($hidepriv) {
1.999     raeburn  3210:             if ($context eq 'userroles') {
                   3211:                 if ((&privileged($username,$domain)) &&
                   3212:                     (!$nothide{$username.':'.$domain})) {
                   3213:                     next;
                   3214:                 }
                   3215:             } else {
                   3216:                 unless (ref($privileged{$domain}) eq 'HASH') {
                   3217:                     my %dompersonnel =
                   3218:                         &Apache::lonnet::get_domain_roles($domain,['dc'],$now,$now);
                   3219:                     $privileged{$domain} = {};
                   3220:                     if (keys(%dompersonnel)) {
                   3221:                         foreach my $server (keys(%dompersonnel)) {
                   3222:                             if (ref($dompersonnel{$server}) eq 'HASH') {
                   3223:                                 foreach my $user (keys(%{$dompersonnel{$server}})) {
                   3224:                                     my ($trole,$uname,$udom) = split(/:/,$user);
                   3225:                                     $privileged{$udom}{$uname} = $trole;
                   3226:                                 }
                   3227:                             }
                   3228:                         }
                   3229:                     }
                   3230:                 }
                   3231:                 if (exists($privileged{$domain}{$username})) {
                   3232:                     if (!$nothide{$username.':'.$domain}) {
                   3233:                         next;
                   3234:                     }
                   3235:                 }
1.937     raeburn  3236:             }
                   3237:         }
1.933     raeburn  3238:         if ($withsec) {
                   3239:             $returnhash{$username.':'.$domain.':'.$role.':'.$section} =
                   3240:                 $tstart.':'.$tend;
                   3241:         } else {
                   3242:             $returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
                   3243:         }
1.832     raeburn  3244:     }
1.373     www      3245:     return %returnhash;
1.399     www      3246: }
                   3247: 
                   3248: # ----------------------------------------------------- Frontpage Announcements
                   3249: #
                   3250: #
                   3251: 
                   3252: sub postannounce {
                   3253:     my ($server,$text)=@_;
1.844     albertel 3254:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      3255:     unless ($text=~/\w/) { $text=''; }
                   3256:     return &reply('setannounce:'.&escape($text),$server);
                   3257: }
                   3258: 
                   3259: sub getannounce {
1.448     albertel 3260: 
                   3261:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      3262: 	my $announcement='';
1.800     albertel 3263: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 3264: 	close($fh);
1.399     www      3265: 	if ($announcement=~/\w/) { 
                   3266: 	    return 
                   3267:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 3268:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      3269: 	} else {
                   3270: 	    return '';
                   3271: 	}
                   3272:     } else {
                   3273: 	return '';
                   3274:     }
1.351     www      3275: }
1.353     www      3276: 
                   3277: # ---------------------------------------------------------- Course ID routines
                   3278: # Deal with domain's nohist_courseid.db files
                   3279: #
                   3280: 
                   3281: sub courseidput {
1.921     raeburn  3282:     my ($domain,$storehash,$coursehome,$caller) = @_;
1.1054    raeburn  3283:     return unless (ref($storehash) eq 'HASH');
1.921     raeburn  3284:     my $outcome;
                   3285:     if ($caller eq 'timeonly') {
                   3286:         my $cids = '';
                   3287:         foreach my $item (keys(%$storehash)) {
                   3288:             $cids.=&escape($item).'&';
                   3289:         }
                   3290:         $cids=~s/\&$//;
                   3291:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$cids,
                   3292:                           $coursehome);       
                   3293:     } else {
                   3294:         my $items = '';
                   3295:         foreach my $item (keys(%$storehash)) {
                   3296:             $items.= &escape($item).'='.
                   3297:                      &freeze_escape($$storehash{$item}).'&';
                   3298:         }
                   3299:         $items=~s/\&$//;
                   3300:         $outcome = &reply('courseidputhash:'.$domain.':'.$caller.':'.$items,
                   3301:                           $coursehome);
1.918     raeburn  3302:     }
                   3303:     if ($outcome eq 'unknown_cmd') {
                   3304:         my $what;
                   3305:         foreach my $cid (keys(%$storehash)) {
                   3306:             $what .= &escape($cid).'=';
1.921     raeburn  3307:             foreach my $item ('description','inst_code','owner','type') {
1.936     raeburn  3308:                 $what .= &escape($storehash->{$cid}{$item}).':';
1.918     raeburn  3309:             }
                   3310:             $what =~ s/\:$/&/;
                   3311:         }
                   3312:         $what =~ s/\&$//;  
                   3313:         return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   3314:     } else {
                   3315:         return $outcome;
                   3316:     }
1.353     www      3317: }
                   3318: 
                   3319: sub courseiddump {
1.921     raeburn  3320:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,
1.947     raeburn  3321:         $coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok,
1.1029    raeburn  3322:         $selfenrollonly,$catfilter,$showhidden,$caller,$cloner,$cc_clone,
1.1071    raeburn  3323:         $cloneonly,$createdbefore,$createdafter,$creationcontext,$domcloner)=@_;
1.918     raeburn  3324:     my $as_hash = 1;
                   3325:     my %returnhash;
                   3326:     if (!$domfilter) { $domfilter=''; }
1.845     albertel 3327:     my %libserv = &all_library();
                   3328:     foreach my $tryserver (keys(%libserv)) {
                   3329:         if ( (  $hostidflag == 1 
                   3330: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   3331: 	     || (!defined($hostidflag)) ) {
                   3332: 
1.918     raeburn  3333: 	    if (($domfilter eq '') ||
                   3334: 		(&host_domain($tryserver) eq $domfilter)) {
                   3335:                 my $rep = 
                   3336:                   &reply('courseiddump:'.&host_domain($tryserver).':'.
                   3337:                          $sincefilter.':'.&escape($descfilter).':'.
                   3338:                          &escape($instcodefilter).':'.&escape($ownerfilter).
                   3339:                          ':'.&escape($coursefilter).':'.&escape($typefilter).
1.947     raeburn  3340:                          ':'.&escape($regexp_ok).':'.$as_hash.':'.
1.962     raeburn  3341:                          &escape($selfenrollonly).':'.&escape($catfilter).':'.
1.1008    raeburn  3342:                          $showhidden.':'.$caller.':'.&escape($cloner).':'.
1.1029    raeburn  3343:                          &escape($cc_clone).':'.$cloneonly.':'.
                   3344:                          &escape($createdbefore).':'.&escape($createdafter).':'.
1.1071    raeburn  3345:                          &escape($creationcontext).':'.$domcloner,
                   3346:                          $tryserver);
1.918     raeburn  3347:                 my @pairs=split(/\&/,$rep);
                   3348:                 foreach my $item (@pairs) {
                   3349:                     my ($key,$value)=split(/\=/,$item,2);
                   3350:                     $key = &unescape($key);
                   3351:                     next if ($key =~ /^error: 2 /);
                   3352:                     my $result = &thaw_unescape($value);
                   3353:                     if (ref($result) eq 'HASH') {
                   3354:                         $returnhash{$key}=$result;
                   3355:                     } else {
1.921     raeburn  3356:                         my @responses = split(/:/,$value);
                   3357:                         my @items = ('description','inst_code','owner','type');
1.918     raeburn  3358:                         for (my $i=0; $i<@responses; $i++) {
1.921     raeburn  3359:                             $returnhash{$key}{$items[$i]} = &unescape($responses[$i]);
1.918     raeburn  3360:                         }
1.1008    raeburn  3361:                     }
1.353     www      3362:                 }
                   3363:             }
                   3364:         }
                   3365:     }
                   3366:     return %returnhash;
                   3367: }
                   3368: 
1.1055    raeburn  3369: sub courselastaccess {
                   3370:     my ($cdom,$cnum,$hostidref) = @_;
                   3371:     my %returnhash;
                   3372:     if ($cdom && $cnum) {
                   3373:         my $chome = &homeserver($cnum,$cdom);
                   3374:         if ($chome ne 'no_host') {
                   3375:             my $rep = &reply('courselastaccess:'.$cdom.':'.$cnum,$chome);
                   3376:             &extract_lastaccess(\%returnhash,$rep);
                   3377:         }
                   3378:     } else {
                   3379:         if (!$cdom) { $cdom=''; }
                   3380:         my %libserv = &all_library();
                   3381:         foreach my $tryserver (keys(%libserv)) {
                   3382:             if (ref($hostidref) eq 'ARRAY') {
                   3383:                 next unless (grep(/^\Q$tryserver\E$/,@{$hostidref}));
                   3384:             } 
                   3385:             if (($cdom eq '') || (&host_domain($tryserver) eq $cdom)) {
                   3386:                 my $rep = &reply('courselastaccess:'.&host_domain($tryserver).':',$tryserver);
                   3387:                 &extract_lastaccess(\%returnhash,$rep);
                   3388:             }
                   3389:         }
                   3390:     }
                   3391:     return %returnhash;
                   3392: }
                   3393: 
                   3394: sub extract_lastaccess {
                   3395:     my ($returnhash,$rep) = @_;
                   3396:     if (ref($returnhash) eq 'HASH') {
                   3397:         unless ($rep eq 'unknown_command' || $rep eq 'no_such_host' || 
                   3398:                 $rep eq 'con_lost' || $rep eq 'rejected' || $rep eq 'refused' ||
                   3399:                  $rep eq '') {
                   3400:             my @pairs=split(/\&/,$rep);
                   3401:             foreach my $item (@pairs) {
                   3402:                 my ($key,$value)=split(/\=/,$item,2);
                   3403:                 $key = &unescape($key);
                   3404:                 next if ($key =~ /^error: 2 /);
                   3405:                 $returnhash->{$key} = &thaw_unescape($value);
                   3406:             }
                   3407:         }
                   3408:     }
                   3409:     return;
                   3410: }
                   3411: 
1.658     raeburn  3412: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  3413: 
                   3414: sub dcmailput {
1.685     raeburn  3415:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  3416:     my $status = &Apache::lonnet::critical(
1.740     www      3417:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   3418:        &escape($message),$server);
1.662     raeburn  3419:     return $status;
                   3420: }
                   3421: 
1.658     raeburn  3422: sub dcmaildump {
                   3423:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  3424:     my %returnhash=();
1.846     albertel 3425: 
                   3426:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  3427:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   3428:                                                          &escape($enddate).':';
                   3429: 	my @esc_senders=map { &escape($_)} @$senders;
                   3430: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 3431: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 3432:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  3433:             if (($key) && ($value)) {
                   3434:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  3435:             }
                   3436:         }
                   3437:     }
                   3438:     return %returnhash;
                   3439: }
1.662     raeburn  3440: # ---------------------------------------------------------- Domain roles
                   3441: 
                   3442: sub get_domain_roles {
                   3443:     my ($dom,$roles,$startdate,$enddate)=@_;
1.1018    raeburn  3444:     if ((!defined($startdate)) || ($startdate eq '')) {
1.662     raeburn  3445:         $startdate = '.';
                   3446:     }
1.1018    raeburn  3447:     if ((!defined($enddate)) || ($enddate eq '')) {
1.662     raeburn  3448:         $enddate = '.';
                   3449:     }
1.922     raeburn  3450:     my $rolelist;
                   3451:     if (ref($roles) eq 'ARRAY') {
                   3452:         $rolelist = join(':',@{$roles});
                   3453:     }
1.662     raeburn  3454:     my %personnel = ();
1.841     albertel 3455: 
                   3456:     my %servers = &get_servers($dom,'library');
                   3457:     foreach my $tryserver (keys(%servers)) {
                   3458: 	%{$personnel{$tryserver}}=();
                   3459: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   3460: 					    &escape($startdate).':'.
                   3461: 					    &escape($enddate).':'.
                   3462: 					    &escape($rolelist), $tryserver))) {
                   3463: 	    my ($key,$value) = split(/\=/,$line,2);
                   3464: 	    if (($key) && ($value)) {
                   3465: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   3466: 	    }
                   3467: 	}
1.662     raeburn  3468:     }
                   3469:     return %personnel;
                   3470: }
1.658     raeburn  3471: 
1.1057    www      3472: # ----------------------------------------------------------- Interval timing 
1.149     www      3473: 
1.504     albertel 3474: sub get_first_access {
                   3475:     my ($type,$argsymb)=@_;
1.790     albertel 3476:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3477:     if ($argsymb) { $symb=$argsymb; }
                   3478:     my ($map,$id,$res)=&decode_symb($symb);
1.926     albertel 3479:     if ($type eq 'course') {
                   3480: 	$res='course';
                   3481:     } elsif ($type eq 'map') {
1.588     albertel 3482: 	$res=&symbread($map);
                   3483:     } else {
                   3484: 	$res=$symb;
                   3485:     }
                   3486:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   3487:     return $times{"$courseid\0$res"};
1.504     albertel 3488: }
                   3489: 
                   3490: sub set_first_access {
                   3491:     my ($type)=@_;
1.790     albertel 3492:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 3493:     my ($map,$id,$res)=&decode_symb($symb);
1.928     albertel 3494:     if ($type eq 'course') {
                   3495: 	$res='course';
                   3496:     } elsif ($type eq 'map') {
1.588     albertel 3497: 	$res=&symbread($map);
                   3498:     } else {
                   3499: 	$res=$symb;
                   3500:     }
                   3501:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 3502:     if (!$firstaccess) {
1.588     albertel 3503: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 3504:     }
                   3505:     return 'already_set';
1.504     albertel 3506: }
                   3507: 
1.110     www      3508: # --------------------------------------------- Set Expire Date for Spreadsheet
                   3509: 
                   3510: sub expirespread {
                   3511:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 3512:     my $cid=$env{'request.course.id'}; 
1.110     www      3513:     if ($cid) {
                   3514:        my $now=time;
                   3515:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 3516:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   3517:                             $env{'course.'.$cid.'.num'}.
1.110     www      3518: 	        	    ':nohist_expirationdates:'.
                   3519:                             &escape($key).'='.$now,
1.620     albertel 3520:                             $env{'course.'.$cid.'.home'})
1.110     www      3521:     }
                   3522:     return 'ok';
1.14      www      3523: }
                   3524: 
1.109     www      3525: # ----------------------------------------------------- Devalidate Spreadsheets
                   3526: 
                   3527: sub devalidate {
1.325     www      3528:     my ($symb,$uname,$udom)=@_;
1.620     albertel 3529:     my $cid=$env{'request.course.id'}; 
1.109     www      3530:     if ($cid) {
1.391     matthew  3531:         # delete the stored spreadsheets for
                   3532:         # - the student level sheet of this user in course's homespace
                   3533:         # - the assessment level sheet for this resource 
                   3534:         #   for this user in user's homespace
1.553     albertel 3535: 	# - current conditional state info
1.325     www      3536: 	my $key=$uname.':'.$udom.':';
1.109     www      3537:         my $status=
1.299     matthew  3538: 	    &del('nohist_calculatedsheets',
1.391     matthew  3539: 		 [$key.'studentcalc:'],
1.620     albertel 3540: 		 $env{'course.'.$cid.'.domain'},
                   3541: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 3542: 		.' '.
                   3543: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  3544: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      3545:         unless ($status eq 'ok ok') {
                   3546:            &logthis('Could not devalidate spreadsheet '.
1.325     www      3547:                     $uname.' at '.$udom.' for '.
1.109     www      3548: 		    $symb.': '.$status);
1.133     albertel 3549:         }
1.553     albertel 3550: 	&delenv('user.state.'.$cid);
1.109     www      3551:     }
                   3552: }
                   3553: 
1.265     albertel 3554: sub get_scalar {
                   3555:     my ($string,$end) = @_;
                   3556:     my $value;
                   3557:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   3558: 	$value = $1;
                   3559:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   3560: 	$value = $1;
                   3561:     }
                   3562:     return &unescape($value);
                   3563: }
                   3564: 
                   3565: sub array2str {
                   3566:   my (@array) = @_;
                   3567:   my $result=&arrayref2str(\@array);
                   3568:   $result=~s/^__ARRAY_REF__//;
                   3569:   $result=~s/__END_ARRAY_REF__$//;
                   3570:   return $result;
                   3571: }
                   3572: 
1.204     albertel 3573: sub arrayref2str {
                   3574:   my ($arrayref) = @_;
1.265     albertel 3575:   my $result='__ARRAY_REF__';
1.204     albertel 3576:   foreach my $elem (@$arrayref) {
1.265     albertel 3577:     if(ref($elem) eq 'ARRAY') {
                   3578:       $result.=&arrayref2str($elem).'&';
                   3579:     } elsif(ref($elem) eq 'HASH') {
                   3580:       $result.=&hashref2str($elem).'&';
                   3581:     } elsif(ref($elem)) {
                   3582:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 3583:     } else {
                   3584:       $result.=&escape($elem).'&';
                   3585:     }
                   3586:   }
                   3587:   $result=~s/\&$//;
1.265     albertel 3588:   $result .= '__END_ARRAY_REF__';
1.204     albertel 3589:   return $result;
                   3590: }
                   3591: 
1.168     albertel 3592: sub hash2str {
1.204     albertel 3593:   my (%hash) = @_;
                   3594:   my $result=&hashref2str(\%hash);
1.265     albertel 3595:   $result=~s/^__HASH_REF__//;
                   3596:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 3597:   return $result;
                   3598: }
                   3599: 
                   3600: sub hashref2str {
                   3601:   my ($hashref)=@_;
1.265     albertel 3602:   my $result='__HASH_REF__';
1.800     albertel 3603:   foreach my $key (sort(keys(%$hashref))) {
                   3604:     if (ref($key) eq 'ARRAY') {
                   3605:       $result.=&arrayref2str($key).'=';
                   3606:     } elsif (ref($key) eq 'HASH') {
                   3607:       $result.=&hashref2str($key).'=';
                   3608:     } elsif (ref($key)) {
1.265     albertel 3609:       $result.='=';
1.800     albertel 3610:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 3611:     } else {
1.800     albertel 3612: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 3613:     }
                   3614: 
1.800     albertel 3615:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   3616:       $result.=&arrayref2str($hashref->{$key}).'&';
                   3617:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   3618:       $result.=&hashref2str($hashref->{$key}).'&';
                   3619:     } elsif(ref($hashref->{$key})) {
1.265     albertel 3620:        $result.='&';
1.800     albertel 3621:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 3622:     } else {
1.800     albertel 3623:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 3624:     }
                   3625:   }
1.168     albertel 3626:   $result=~s/\&$//;
1.265     albertel 3627:   $result .= '__END_HASH_REF__';
1.168     albertel 3628:   return $result;
                   3629: }
                   3630: 
                   3631: sub str2hash {
1.265     albertel 3632:     my ($string)=@_;
                   3633:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   3634:     return %$hash;
                   3635: }
                   3636: 
                   3637: sub str2hashref {
1.168     albertel 3638:   my ($string) = @_;
1.265     albertel 3639: 
                   3640:   my %hash;
                   3641: 
                   3642:   if($string !~ /^__HASH_REF__/) {
                   3643:       if (! ($string eq '' || !defined($string))) {
                   3644: 	  $hash{'error'}='Not hash reference';
                   3645:       }
                   3646:       return (\%hash, $string);
                   3647:   }
                   3648: 
                   3649:   $string =~ s/^__HASH_REF__//;
                   3650: 
                   3651:   while($string !~ /^__END_HASH_REF__/) {
                   3652:       #key
                   3653:       my $key='';
                   3654:       if($string =~ /^__HASH_REF__/) {
                   3655:           ($key, $string)=&str2hashref($string);
                   3656:           if(defined($key->{'error'})) {
                   3657:               $hash{'error'}='Bad data';
                   3658:               return (\%hash, $string);
                   3659:           }
                   3660:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3661:           ($key, $string)=&str2arrayref($string);
                   3662:           if($key->[0] eq 'Array reference error') {
                   3663:               $hash{'error'}='Bad data';
                   3664:               return (\%hash, $string);
                   3665:           }
                   3666:       } else {
                   3667:           $string =~ s/^(.*?)=//;
1.267     albertel 3668: 	  $key=&unescape($1);
1.265     albertel 3669:       }
                   3670:       $string =~ s/^=//;
                   3671: 
                   3672:       #value
                   3673:       my $value='';
                   3674:       if($string =~ /^__HASH_REF__/) {
                   3675:           ($value, $string)=&str2hashref($string);
                   3676:           if(defined($value->{'error'})) {
                   3677:               $hash{'error'}='Bad data';
                   3678:               return (\%hash, $string);
                   3679:           }
                   3680:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3681:           ($value, $string)=&str2arrayref($string);
                   3682:           if($value->[0] eq 'Array reference error') {
                   3683:               $hash{'error'}='Bad data';
                   3684:               return (\%hash, $string);
                   3685:           }
                   3686:       } else {
                   3687: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   3688:       }
                   3689:       $string =~ s/^&//;
                   3690: 
                   3691:       $hash{$key}=$value;
1.204     albertel 3692:   }
1.265     albertel 3693: 
                   3694:   $string =~ s/^__END_HASH_REF__//;
                   3695: 
                   3696:   return (\%hash, $string);
1.204     albertel 3697: }
                   3698: 
                   3699: sub str2array {
1.265     albertel 3700:     my ($string)=@_;
                   3701:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   3702:     return @$array;
                   3703: }
                   3704: 
                   3705: sub str2arrayref {
1.204     albertel 3706:   my ($string) = @_;
1.265     albertel 3707:   my @array;
                   3708: 
                   3709:   if($string !~ /^__ARRAY_REF__/) {
                   3710:       if (! ($string eq '' || !defined($string))) {
                   3711: 	  $array[0]='Array reference error';
                   3712:       }
                   3713:       return (\@array, $string);
                   3714:   }
                   3715: 
                   3716:   $string =~ s/^__ARRAY_REF__//;
                   3717: 
                   3718:   while($string !~ /^__END_ARRAY_REF__/) {
                   3719:       my $value='';
                   3720:       if($string =~ /^__HASH_REF__/) {
                   3721:           ($value, $string)=&str2hashref($string);
                   3722:           if(defined($value->{'error'})) {
                   3723:               $array[0] ='Array reference error';
                   3724:               return (\@array, $string);
                   3725:           }
                   3726:       } elsif($string =~ /^__ARRAY_REF__/) {
                   3727:           ($value, $string)=&str2arrayref($string);
                   3728:           if($value->[0] eq 'Array reference error') {
                   3729:               $array[0] ='Array reference error';
                   3730:               return (\@array, $string);
                   3731:           }
                   3732:       } else {
                   3733: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   3734:       }
                   3735:       $string =~ s/^&//;
                   3736: 
                   3737:       push(@array, $value);
1.191     harris41 3738:   }
1.265     albertel 3739: 
                   3740:   $string =~ s/^__END_ARRAY_REF__//;
                   3741: 
                   3742:   return (\@array, $string);
1.168     albertel 3743: }
                   3744: 
1.167     albertel 3745: # -------------------------------------------------------------------Temp Store
                   3746: 
1.168     albertel 3747: sub tmpreset {
                   3748:   my ($symb,$namespace,$domain,$stuname) = @_;
                   3749:   if (!$symb) {
                   3750:     $symb=&symbread();
1.620     albertel 3751:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3752:   }
                   3753:   $symb=escape($symb);
                   3754: 
1.620     albertel 3755:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 3756:   $namespace=~s/\//\_/g;
                   3757:   $namespace=~s/\W//g;
                   3758: 
1.620     albertel 3759:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3760:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3761:   if ($domain eq 'public' && $stuname eq 'public') {
                   3762:       $stuname=$ENV{'REMOTE_ADDR'};
                   3763:   }
1.168     albertel 3764:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3765:   my %hash;
                   3766:   if (tie(%hash,'GDBM_File',
                   3767: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3768: 	  &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  3769:     foreach my $key (keys(%hash)) {
1.180     albertel 3770:       if ($key=~ /:$symb/) {
1.168     albertel 3771: 	delete($hash{$key});
                   3772:       }
                   3773:     }
                   3774:   }
                   3775: }
                   3776: 
1.167     albertel 3777: sub tmpstore {
1.168     albertel 3778:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3779: 
                   3780:   if (!$symb) {
                   3781:     $symb=&symbread();
1.620     albertel 3782:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3783:   }
                   3784:   $symb=escape($symb);
                   3785: 
                   3786:   if (!$namespace) {
                   3787:     # I don't think we would ever want to store this for a course.
                   3788:     # it seems this will only be used if we don't have a course.
1.620     albertel 3789:     #$namespace=$env{'request.course.id'};
1.168     albertel 3790:     #if (!$namespace) {
1.620     albertel 3791:       $namespace=$env{'request.state'};
1.168     albertel 3792:     #}
                   3793:   }
                   3794:   $namespace=~s/\//\_/g;
                   3795:   $namespace=~s/\W//g;
1.620     albertel 3796:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3797:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3798:   if ($domain eq 'public' && $stuname eq 'public') {
                   3799:       $stuname=$ENV{'REMOTE_ADDR'};
                   3800:   }
1.168     albertel 3801:   my $now=time;
                   3802:   my %hash;
                   3803:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3804:   if (tie(%hash,'GDBM_File',
                   3805: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3806: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 3807:     $hash{"version:$symb"}++;
                   3808:     my $version=$hash{"version:$symb"};
                   3809:     my $allkeys=''; 
                   3810:     foreach my $key (keys(%$storehash)) {
                   3811:       $allkeys.=$key.':';
1.591     albertel 3812:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 3813:     }
                   3814:     $hash{"$version:$symb:timestamp"}=$now;
                   3815:     $allkeys.='timestamp';
                   3816:     $hash{"$version:keys:$symb"}=$allkeys;
                   3817:     if (untie(%hash)) {
                   3818:       return 'ok';
                   3819:     } else {
                   3820:       return "error:$!";
                   3821:     }
                   3822:   } else {
                   3823:     return "error:$!";
                   3824:   }
                   3825: }
1.167     albertel 3826: 
1.168     albertel 3827: # -----------------------------------------------------------------Temp Restore
1.167     albertel 3828: 
1.168     albertel 3829: sub tmprestore {
                   3830:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 3831: 
1.168     albertel 3832:   if (!$symb) {
                   3833:     $symb=&symbread();
1.620     albertel 3834:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 3835:   }
                   3836:   $symb=escape($symb);
                   3837: 
1.620     albertel 3838:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 3839: 
1.620     albertel 3840:   if (!$domain) { $domain=$env{'user.domain'}; }
                   3841:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 3842:   if ($domain eq 'public' && $stuname eq 'public') {
                   3843:       $stuname=$ENV{'REMOTE_ADDR'};
                   3844:   }
1.168     albertel 3845:   my %returnhash;
                   3846:   $namespace=~s/\//\_/g;
                   3847:   $namespace=~s/\W//g;
                   3848:   my %hash;
                   3849:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   3850:   if (tie(%hash,'GDBM_File',
                   3851: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 3852: 	  &GDBM_READER(),0640)) {
1.168     albertel 3853:     my $version=$hash{"version:$symb"};
                   3854:     $returnhash{'version'}=$version;
                   3855:     my $scope;
                   3856:     for ($scope=1;$scope<=$version;$scope++) {
                   3857:       my $vkeys=$hash{"$scope:keys:$symb"};
                   3858:       my @keys=split(/:/,$vkeys);
                   3859:       my $key;
                   3860:       $returnhash{"$scope:keys"}=$vkeys;
                   3861:       foreach $key (@keys) {
1.591     albertel 3862: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   3863: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 3864:       }
                   3865:     }
1.168     albertel 3866:     if (!(untie(%hash))) {
                   3867:       return "error:$!";
                   3868:     }
                   3869:   } else {
                   3870:     return "error:$!";
                   3871:   }
                   3872:   return %returnhash;
1.167     albertel 3873: }
                   3874: 
1.9       www      3875: # ----------------------------------------------------------------------- Store
                   3876: 
                   3877: sub store {
1.124     www      3878:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3879:     my $home='';
                   3880: 
1.168     albertel 3881:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3882: 
1.213     www      3883:     $symb=&symbclean($symb);
1.122     albertel 3884:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      3885: 
1.620     albertel 3886:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3887:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      3888: 
                   3889:     &devalidate($symb,$stuname,$domain);
1.109     www      3890: 
                   3891:     $symb=escape($symb);
1.187     www      3892:     if (!$namespace) { 
1.620     albertel 3893:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      3894:           return ''; 
                   3895:        } 
                   3896:     }
1.620     albertel 3897:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      3898: 
                   3899:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   3900:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   3901: 
1.12      www      3902:     my $namevalue='';
1.800     albertel 3903:     foreach my $key (keys(%$storehash)) {
                   3904:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 3905:     }
1.12      www      3906:     $namevalue=~s/\&$//;
1.187     www      3907:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      3908:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      3909: }
                   3910: 
1.47      www      3911: # -------------------------------------------------------------- Critical Store
                   3912: 
                   3913: sub cstore {
1.124     www      3914:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   3915:     my $home='';
                   3916: 
1.168     albertel 3917:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3918: 
1.213     www      3919:     $symb=&symbclean($symb);
1.122     albertel 3920:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      3921: 
1.620     albertel 3922:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3923:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      3924: 
                   3925:     &devalidate($symb,$stuname,$domain);
1.109     www      3926: 
                   3927:     $symb=escape($symb);
1.187     www      3928:     if (!$namespace) { 
1.620     albertel 3929:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      3930:           return ''; 
                   3931:        } 
                   3932:     }
1.620     albertel 3933:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      3934: 
                   3935:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   3936:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 3937: 
1.47      www      3938:     my $namevalue='';
1.800     albertel 3939:     foreach my $key (keys(%$storehash)) {
                   3940:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 3941:     }
1.47      www      3942:     $namevalue=~s/\&$//;
1.187     www      3943:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      3944:     return critical
                   3945:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      3946: }
                   3947: 
1.9       www      3948: # --------------------------------------------------------------------- Restore
                   3949: 
                   3950: sub restore {
1.124     www      3951:     my ($symb,$namespace,$domain,$stuname) = @_;
                   3952:     my $home='';
                   3953: 
1.168     albertel 3954:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3955: 
1.122     albertel 3956:     if (!$symb) {
                   3957:       unless ($symb=escape(&symbread())) { return ''; }
                   3958:     } else {
1.213     www      3959:       $symb=&escape(&symbclean($symb));
1.122     albertel 3960:     }
1.188     www      3961:     if (!$namespace) { 
1.620     albertel 3962:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      3963:           return ''; 
                   3964:        } 
                   3965:     }
1.620     albertel 3966:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3967:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   3968:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 3969:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   3970: 
1.12      www      3971:     my %returnhash=();
1.800     albertel 3972:     foreach my $line (split(/\&/,$answer)) {
                   3973: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 3974:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 3975:     }
1.75      www      3976:     my $version;
                   3977:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 3978:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   3979:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 3980:        }
1.75      www      3981:     }
1.13      www      3982:     return %returnhash;
1.34      www      3983: }
                   3984: 
                   3985: # ---------------------------------------------------------- Course Description
                   3986: 
                   3987: sub coursedescription {
1.731     albertel 3988:     my ($courseid,$args)=@_;
1.34      www      3989:     $courseid=~s/^\///;
1.49      www      3990:     $courseid=~s/\_/\//g;
1.34      www      3991:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 3992:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 3993:     my $normalid=$cdomain.'_'.$cnum;
                   3994:     # need to always cache even if we get errors otherwise we keep 
                   3995:     # trying and trying and trying to get the course description.
                   3996:     my %envhash=();
                   3997:     my %returnhash=();
1.731     albertel 3998:     
                   3999:     my $expiretime=600;
                   4000:     if ($env{'request.course.id'} eq $normalid) {
                   4001: 	$expiretime=120;
                   4002:     }
                   4003: 
                   4004:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   4005:     if (!$args->{'freshen_cache'}
                   4006: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   4007: 	foreach my $key (keys(%env)) {
                   4008: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   4009: 	    my ($setting) = $1;
                   4010: 	    $returnhash{$setting} = $env{$key};
                   4011: 	}
                   4012: 	return %returnhash;
                   4013:     }
                   4014: 
                   4015:     # get the data agin
                   4016:     if (!$args->{'one_time'}) {
                   4017: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   4018:     }
1.811     albertel 4019: 
1.34      www      4020:     if ($chome ne 'no_host') {
1.302     albertel 4021:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 4022:        if (!exists($returnhash{'con_lost'})) {
                   4023:            $returnhash{'home'}= $chome;
                   4024: 	   $returnhash{'domain'} = $cdomain;
                   4025: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  4026:            if (!defined($returnhash{'type'})) {
                   4027:                $returnhash{'type'} = 'Course';
                   4028:            }
1.130     albertel 4029:            while (my ($name,$value) = each %returnhash) {
1.53      www      4030:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 4031:            }
1.270     www      4032:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      4033:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 4034: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      4035:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   4036:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   4037:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      4038:        }
                   4039:     }
1.731     albertel 4040:     if (!$args->{'one_time'}) {
1.949     raeburn  4041: 	&appenv(\%envhash);
1.731     albertel 4042:     }
1.302     albertel 4043:     return %returnhash;
1.461     www      4044: }
                   4045: 
1.1080    raeburn  4046: sub update_released_required {
                   4047:     my ($needsrelease,$cdom,$cnum,$chome,$cid) = @_;
                   4048:     if ($cdom eq '' || $cnum eq '' || $chome eq '' || $cid eq '') {
                   4049:         $cid = $env{'request.course.id'};
                   4050:         $cdom = $env{'course.'.$cid.'.domain'};
                   4051:         $cnum = $env{'course.'.$cid.'.num'};
                   4052:         $chome = $env{'course.'.$cid.'.home'};
                   4053:     }
                   4054:     if ($needsrelease) {
                   4055:         my %curr_reqd_hash = &userenvironment($cdom,$cnum,'internal.releaserequired');
                   4056:         my $needsupdate;
                   4057:         if ($curr_reqd_hash{'internal.releaserequired'} eq '') {
                   4058:             $needsupdate = 1;
                   4059:         } else {
                   4060:             my ($currmajor,$currminor) = split(/\./,$curr_reqd_hash{'internal.releaserequired'});
                   4061:             my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   4062:             if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                   4063:                 $needsupdate = 1;
                   4064:             }
                   4065:         }
                   4066:         if ($needsupdate) {
                   4067:             my %needshash = (
                   4068:                              'internal.releaserequired' => $needsrelease,
                   4069:                             );
                   4070:             my $putresult = &put('environment',\%needshash,$cdom,$cnum);
                   4071:             if ($putresult eq 'ok') {
                   4072:                 &appenv({'course.'.$cid.'.internal.releaserequired' => $needsrelease});
                   4073:                 my %crsinfo = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   4074:                 if (ref($crsinfo{$cid}) eq 'HASH') {
                   4075:                     $crsinfo{$cid}{'releaserequired'} = $needsrelease;
                   4076:                     &courseidput($cdom,\%crsinfo,$chome,'notime');
                   4077:                 }
                   4078:             }
                   4079:         }
                   4080:     }
                   4081:     return;
                   4082: }
                   4083: 
1.461     www      4084: # -------------------------------------------------See if a user is privileged
                   4085: 
                   4086: sub privileged {
                   4087:     my ($username,$domain)=@_;
                   4088:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   4089: 			&homeserver($username,$domain));
1.1033    raeburn  4090:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
                   4091:         ($rolesdump =~ /^error:/)) {
                   4092:         return 0;
                   4093:     }
1.461     www      4094:     my $now=time;
                   4095:     if ($rolesdump ne '') {
1.800     albertel 4096:         foreach my $entry (split(/&/,$rolesdump)) {
                   4097: 	    if ($entry!~/^rolesdef_/) {
                   4098: 		my ($area,$role)=split(/=/,$entry);
1.461     www      4099: 		$area=~s/\_\w\w$//;
                   4100: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   4101: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   4102: 		    my $active=1;
                   4103: 		    if ($tend) {
                   4104: 			if ($tend<$now) { $active=0; }
                   4105: 		    }
                   4106: 		    if ($tstart) {
                   4107: 			if ($tstart>$now) { $active=0; }
                   4108: 		    }
                   4109: 		    if ($active) { return 1; }
                   4110: 		}
                   4111: 	    }
                   4112: 	}
                   4113:     }
                   4114:     return 0;
1.9       www      4115: }
1.1       albertel 4116: 
1.103     harris41 4117: # -------------------------------------------------------- Get user privileges
1.11      www      4118: 
                   4119: sub rolesinit {
                   4120:     my ($domain,$username,$authhost)=@_;
1.1034    raeburn  4121:     my $now=time;
                   4122:     my %userroles = ('user.login.time' => $now);
1.1086    raeburn  4123:     my $extra = &freeze_escape({'skipcheck' => 1});
1.1078    raeburn  4124:     my $rolesdump=reply("dump:$domain:$username:roles:.::$extra",$authhost);
1.1033    raeburn  4125:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '') || 
1.1078    raeburn  4126:         ($rolesdump =~ /^error:/)) {
1.1033    raeburn  4127:         return \%userroles;
                   4128:     }
1.11      www      4129:     my %allroles=();
1.678     raeburn  4130:     my %allgroups=();   
1.11      www      4131: 
                   4132:     if ($rolesdump ne '') {
1.800     albertel 4133:         foreach my $entry (split(/&/,$rolesdump)) {
                   4134: 	  if ($entry!~/^rolesdef_/) {
                   4135:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 4136: 	    $area=~s/\_\w\w$//;
1.678     raeburn  4137:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 4138: 	    if ($role=~/^cr/) { 
1.807     albertel 4139: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   4140: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 4141: 		    ($tend,$tstart)=split('_',$trest);
                   4142: 		} else {
                   4143: 		    $trole=$role;
                   4144: 		}
1.678     raeburn  4145:             } elsif ($role =~ m|^gr/|) {
                   4146:                 ($trole,$tend,$tstart) = split(/_/,$role);
1.1104    raeburn  4147:                 next if ($tstart eq '-1');
1.678     raeburn  4148:                 ($trole,$group_privs) = split(/\//,$trole);
                   4149:                 $group_privs = &unescape($group_privs);
1.587     albertel 4150: 	    } else {
                   4151: 		($trole,$tend,$tstart)=split(/_/,$role);
                   4152: 	    }
1.743     albertel 4153: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   4154: 					 $username);
                   4155: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  4156:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   4157:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      4158:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 4159: 		my $spec=$trole.'.'.$area;
                   4160: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   4161: 		if ($trole =~ /^cr\//) {
1.567     raeburn  4162:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  4163:                 } elsif ($trole eq 'gr') {
                   4164:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 4165: 		} else {
1.567     raeburn  4166:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 4167: 		}
1.12      www      4168:             }
1.662     raeburn  4169:           }
1.191     harris41 4170:         }
1.743     albertel 4171:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   4172:         $userroles{'user.adv'}    = $adv;
                   4173: 	$userroles{'user.author'} = $author;
1.620     albertel 4174:         $env{'user.adv'}=$adv;
1.11      www      4175:     }
1.743     albertel 4176:     return \%userroles;  
1.11      www      4177: }
                   4178: 
1.567     raeburn  4179: sub set_arearole {
                   4180:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   4181: # log the associated role with the area
                   4182:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 4183:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  4184: }
                   4185: 
                   4186: sub custom_roleprivs {
                   4187:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   4188:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   4189:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 4190:     if (&hostname($homsvr) ne '') {
1.567     raeburn  4191:         my ($rdummy,$roledef)=
                   4192:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   4193:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   4194:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   4195:             if (defined($syspriv)) {
1.1043    raeburn  4196:                 if ($trest =~ /^$match_community$/) {
                   4197:                     $syspriv =~ s/bre\&S//; 
                   4198:                 }
1.567     raeburn  4199:                 $$allroles{'cm./'}.=':'.$syspriv;
                   4200:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   4201:             }
                   4202:             if ($tdomain ne '') {
                   4203:                 if (defined($dompriv)) {
                   4204:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   4205:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   4206:                 }
                   4207:                 if (($trest ne '') && (defined($coursepriv))) {
                   4208:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   4209:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   4210:                 }
                   4211:             }
                   4212:         }
                   4213:     }
                   4214: }
                   4215: 
1.678     raeburn  4216: sub group_roleprivs {
                   4217:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   4218:     my $access = 1;
                   4219:     my $now = time;
                   4220:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   4221:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   4222:     if ($access) {
1.811     albertel 4223:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  4224:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   4225:     }
                   4226: }
1.567     raeburn  4227: 
                   4228: sub standard_roleprivs {
                   4229:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   4230:     if (defined($pr{$trole.':s'})) {
                   4231:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   4232:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   4233:     }
                   4234:     if ($tdomain ne '') {
                   4235:         if (defined($pr{$trole.':d'})) {
                   4236:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4237:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   4238:         }
                   4239:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   4240:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   4241:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   4242:         }
                   4243:     }
                   4244: }
                   4245: 
                   4246: sub set_userprivs {
1.1064    raeburn  4247:     my ($userroles,$allroles,$allgroups,$groups_roles) = @_; 
1.567     raeburn  4248:     my $author=0;
                   4249:     my $adv=0;
1.678     raeburn  4250:     my %grouproles = ();
                   4251:     if (keys(%{$allgroups}) > 0) {
1.1064    raeburn  4252:         my @groupkeys; 
1.1000    raeburn  4253:         foreach my $role (keys(%{$allroles})) {
1.1064    raeburn  4254:             push(@groupkeys,$role);
                   4255:         }
                   4256:         if (ref($groups_roles) eq 'HASH') {
                   4257:             foreach my $key (keys(%{$groups_roles})) {
                   4258:                 unless (grep(/^\Q$key\E$/,@groupkeys)) {
                   4259:                     push(@groupkeys,$key);
                   4260:                 }
                   4261:             }
                   4262:         }
                   4263:         if (@groupkeys > 0) {
                   4264:             foreach my $role (@groupkeys) {
                   4265:                 my ($trole,$area,$sec,$extendedarea);
                   4266:                 if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
                   4267:                     $trole = $1;
                   4268:                     $area = $2;
                   4269:                     $sec = $3;
                   4270:                     $extendedarea = $area.$sec;
                   4271:                     if (exists($$allgroups{$area})) {
                   4272:                         foreach my $group (keys(%{$$allgroups{$area}})) {
                   4273:                             my $spec = $trole.'.'.$extendedarea;
                   4274:                             $grouproles{$spec.'.'.$area.'/'.$group} = 
1.681     raeburn  4275:                                                 $$allgroups{$area}{$group};
1.1064    raeburn  4276:                         }
1.678     raeburn  4277:                     }
                   4278:                 }
                   4279:             }
                   4280:         }
                   4281:     }
1.800     albertel 4282:     foreach my $group (keys(%grouproles)) {
                   4283:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  4284:     }
1.800     albertel 4285:     foreach my $role (keys(%{$allroles})) {
                   4286:         my %thesepriv;
1.941     raeburn  4287:         if (($role=~/^au/) || ($role=~/^ca/) || ($role=~/^aa/)) { $author=1; }
1.800     albertel 4288:         foreach my $item (split(/:/,$$allroles{$role})) {
                   4289:             if ($item ne '') {
                   4290:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  4291:                 if ($restrictions eq '') {
                   4292:                     $thesepriv{$privilege}='F';
                   4293:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   4294:                     $thesepriv{$privilege}.=$restrictions;
                   4295:                 }
                   4296:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   4297:             }
                   4298:         }
                   4299:         my $thesestr='';
1.1104    raeburn  4300:         foreach my $priv (sort(keys(%thesepriv))) {
1.800     albertel 4301: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   4302: 	}
                   4303:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  4304:     }
                   4305:     return ($author,$adv);
                   4306: }
                   4307: 
1.994     raeburn  4308: sub role_status {
1.1104    raeburn  4309:     my ($rolekey,$update,$refresh,$now,$role,$where,$trolecode,$tstatus,$tstart,$tend) = @_;
1.994     raeburn  4310:     my @pwhere = ();
                   4311:     if (exists($env{$rolekey}) && $env{$rolekey} ne '') {
                   4312:         (undef,undef,$$role,@pwhere)=split(/\./,$rolekey);
                   4313:         unless (!defined($$role) || $$role eq '') {
                   4314:             $$where=join('.',@pwhere);
                   4315:             $$trolecode=$$role.'.'.$$where;
                   4316:             ($$tstart,$$tend)=split(/\./,$env{$rolekey});
                   4317:             $$tstatus='is';
1.1104    raeburn  4318:             if ($$tstart && $$tstart>$update) {
1.994     raeburn  4319:                 $$tstatus='future';
1.1034    raeburn  4320:                 if ($$tstart<$now) {
                   4321:                     if ($$tstart && $$tstart>$refresh) {
1.1002    raeburn  4322:                         if (($$where ne '') && ($$role ne '')) {
1.1064    raeburn  4323:                             my (%allroles,%allgroups,$group_privs,
                   4324:                                 %groups_roles,@rolecodes);
1.1002    raeburn  4325:                             my %userroles = (
                   4326:                                 'user.role.'.$$role.'.'.$$where => $$tstart.'.'.$$tend
                   4327:                             );
1.1064    raeburn  4328:                             @rolecodes = ('cm'); 
1.1002    raeburn  4329:                             my $spec=$$role.'.'.$$where;
                   4330:                             my ($tdummy,$tdomain,$trest)=split(/\//,$$where);
                   4331:                             if ($$role =~ /^cr\//) {
                   4332:                                 &custom_roleprivs(\%allroles,$$role,$tdomain,$trest,$spec,$$where);
1.1064    raeburn  4333:                                 push(@rolecodes,'cr');
1.1002    raeburn  4334:                             } elsif ($$role eq 'gr') {
1.1064    raeburn  4335:                                 push(@rolecodes,$$role);
1.1002    raeburn  4336:                                 my %rolehash = &get('roles',[$$where.'_'.$$role],$env{'user.domain'},
                   4337:                                                     $env{'user.name'});
1.1064    raeburn  4338:                                 my ($trole) = split('_',$rolehash{$$where.'_'.$$role},2);
1.1002    raeburn  4339:                                 (undef,my $group_privs) = split(/\//,$trole);
                   4340:                                 $group_privs = &unescape($group_privs);
                   4341:                                 &group_roleprivs(\%allgroups,$$where,$group_privs,$$tend,$$tstart);
1.1064    raeburn  4342:                                 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  4343:                                 &get_groups_roles($tdomain,$trest,
                   4344:                                                   \%course_roles,\@rolecodes,
                   4345:                                                   \%groups_roles);
1.1002    raeburn  4346:                             } else {
1.1064    raeburn  4347:                                 push(@rolecodes,$$role);
1.1002    raeburn  4348:                                 &standard_roleprivs(\%allroles,$$role,$tdomain,$spec,$trest,$$where);
                   4349:                             }
1.1064    raeburn  4350:                             my ($author,$adv)= &set_userprivs(\%userroles,\%allroles,\%allgroups,\%groups_roles);
                   4351:                             &appenv(\%userroles,\@rolecodes);
1.1002    raeburn  4352:                             &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
                   4353:                         }
                   4354:                     }
1.1034    raeburn  4355:                     $$tstatus = 'is';
1.1002    raeburn  4356:                 }
1.994     raeburn  4357:             }
                   4358:             if ($$tend) {
1.1104    raeburn  4359:                 if ($$tend<$update) {
1.994     raeburn  4360:                     $$tstatus='expired';
                   4361:                 } elsif ($$tend<$now) {
                   4362:                     $$tstatus='will_not';
                   4363:                 }
                   4364:             }
                   4365:         }
                   4366:     }
                   4367: }
                   4368: 
1.1104    raeburn  4369: sub get_groups_roles {
                   4370:     my ($cdom,$rest,$cdom_courseroles,$rolecodes,$groups_roles) = @_;
                   4371:     return unless((ref($cdom_courseroles) eq 'HASH') && 
                   4372:                   (ref($rolecodes) eq 'ARRAY') && 
                   4373:                   (ref($groups_roles) eq 'HASH')); 
                   4374:     if (keys(%{$cdom_courseroles}) > 0) {
                   4375:         my ($cnum) = ($rest =~ /^($match_courseid)/);
                   4376:         if ($cdom ne '' && $cnum ne '') {
                   4377:             foreach my $key (keys(%{$cdom_courseroles})) {
                   4378:                 if ($key =~ /^\Q$cnum\E:\Q$cdom\E:([^:]+):?([^:]*)/) {
                   4379:                     my $crsrole = $1;
                   4380:                     my $crssec = $2;
                   4381:                     if ($crsrole =~ /^cr/) {
                   4382:                         unless (grep(/^cr$/,@{$rolecodes})) {
                   4383:                             push(@{$rolecodes},'cr');
                   4384:                         }
                   4385:                     } else {
                   4386:                         unless(grep(/^\Q$crsrole\E$/,@{$rolecodes})) {
                   4387:                             push(@{$rolecodes},$crsrole);
                   4388:                         }
                   4389:                     }
                   4390:                     my $rolekey = "$crsrole./$cdom/$cnum";
                   4391:                     if ($crssec ne '') {
                   4392:                         $rolekey .= "/$crssec";
                   4393:                     }
                   4394:                     $rolekey .= './';
                   4395:                     $groups_roles->{$rolekey} = $rolecodes;
                   4396:                 }
                   4397:             }
                   4398:         }
                   4399:     }
                   4400:     return;
                   4401: }
                   4402: 
                   4403: sub delete_env_groupprivs {
                   4404:     my ($where,$courseroles,$possroles) = @_;
                   4405:     return unless((ref($courseroles) eq 'HASH') && (ref($possroles) eq 'ARRAY'));
                   4406:     my ($dummy,$udom,$uname,$group) = split(/\//,$where);
                   4407:     unless (ref($courseroles->{$udom}) eq 'HASH') {
                   4408:         %{$courseroles->{$udom}} =
                   4409:             &get_my_roles('','','userroles',['active'],
                   4410:                           $possroles,[$udom],1);
                   4411:     }
                   4412:     if (ref($courseroles->{$udom}) eq 'HASH') {
                   4413:         foreach my $item (keys(%{$courseroles->{$udom}})) {
                   4414:             my ($cnum,$cdom,$crsrole,$crssec) = split(/:/,$item);
                   4415:             my $area = '/'.$cdom.'/'.$cnum;
                   4416:             my $privkey = "user.priv.$crsrole.$area";
                   4417:             if ($crssec ne '') {
                   4418:                 $privkey .= '/'.$crssec;
                   4419:             }
                   4420:             $privkey .= ".$area/$group";
                   4421:             &Apache::lonnet::delenv($privkey,undef,[$crsrole]);
                   4422:         }
                   4423:     }
                   4424:     return;
                   4425: }
                   4426: 
1.994     raeburn  4427: sub check_adhoc_privs {
1.1104    raeburn  4428:     my ($cdom,$cnum,$update,$refresh,$now,$checkrole,$caller) = @_;
1.994     raeburn  4429:     my $cckey = 'user.role.'.$checkrole.'./'.$cdom.'/'.$cnum;
                   4430:     if ($env{$cckey}) {
                   4431:         my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus,$tpstart,$tpend);
1.1104    raeburn  4432:         &role_status($cckey,$update,$refresh,$now,\$role,\$where,\$trolecode,\$tstatus,\$tstart,\$tend);
1.994     raeburn  4433:         unless (($tstatus eq 'is') || ($tstatus eq 'will_not')) {
1.1088    raeburn  4434:             &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4435:         }
                   4436:     } else {
1.1088    raeburn  4437:         &set_adhoc_privileges($cdom,$cnum,$checkrole,$caller);
1.994     raeburn  4438:     }
                   4439: }
                   4440: 
                   4441: sub set_adhoc_privileges {
                   4442: # role can be cc or ca
1.1088    raeburn  4443:     my ($dcdom,$pickedcourse,$role,$caller) = @_;
1.994     raeburn  4444:     my $area = '/'.$dcdom.'/'.$pickedcourse;
                   4445:     my $spec = $role.'.'.$area;
                   4446:     my %userroles = &set_arearole($role,$area,'','',$env{'user.domain'},
                   4447:                                   $env{'user.name'});
                   4448:     my %ccrole = ();
                   4449:     &standard_roleprivs(\%ccrole,$role,$dcdom,$spec,$pickedcourse,$area);
                   4450:     my ($author,$adv)= &set_userprivs(\%userroles,\%ccrole);
                   4451:     &appenv(\%userroles,[$role,'cm']);
                   4452:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},"Role ".$role);
1.1088    raeburn  4453:     unless ($caller eq 'constructaccess' && $env{'request.course.id'}) {
                   4454:         &appenv( {'request.role'        => $spec,
                   4455:                   'request.role.domain' => $dcdom,
                   4456:                   'request.course.sec'  => ''
                   4457:                  }
                   4458:                );
                   4459:         my $tadv=0;
                   4460:         if (&allowed('adv') eq 'F') { $tadv=1; }
                   4461:         &appenv({'request.role.adv'    => $tadv});
                   4462:     }
1.994     raeburn  4463: }
                   4464: 
1.12      www      4465: # --------------------------------------------------------------- get interface
                   4466: 
                   4467: sub get {
1.131     albertel 4468:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      4469:    my $items='';
1.800     albertel 4470:    foreach my $item (@$storearr) {
                   4471:        $items.=&escape($item).'&';
1.191     harris41 4472:    }
1.12      www      4473:    $items=~s/\&$//;
1.620     albertel 4474:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4475:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 4476:    my $uhome=&homeserver($uname,$udomain);
                   4477: 
1.133     albertel 4478:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4479:    my @pairs=split(/\&/,$rep);
1.273     albertel 4480:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   4481:      return @pairs;
                   4482:    }
1.15      www      4483:    my %returnhash=();
1.42      www      4484:    my $i=0;
1.800     albertel 4485:    foreach my $item (@$storearr) {
                   4486:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      4487:       $i++;
1.191     harris41 4488:    }
1.15      www      4489:    return %returnhash;
1.27      www      4490: }
                   4491: 
                   4492: # --------------------------------------------------------------- del interface
                   4493: 
                   4494: sub del {
1.133     albertel 4495:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      4496:    my $items='';
1.800     albertel 4497:    foreach my $item (@$storearr) {
                   4498:        $items.=&escape($item).'&';
1.191     harris41 4499:    }
1.984     neumanie 4500: 
1.27      www      4501:    $items=~s/\&$//;
1.620     albertel 4502:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4503:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 4504:    my $uhome=&homeserver($uname,$udomain);
                   4505:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      4506: }
                   4507: 
                   4508: # -------------------------------------------------------------- dump interface
                   4509: 
                   4510: sub dump {
1.1086    raeburn  4511:     my ($namespace,$udomain,$uname,$regexp,$range,$extra)=@_;
1.755     albertel 4512:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4513:     if (!$uname) { $uname=$env{'user.name'}; }
                   4514:     my $uhome=&homeserver($uname,$udomain);
                   4515:     if ($regexp) {
                   4516: 	$regexp=&escape($regexp);
                   4517:     } else {
                   4518: 	$regexp='.';
                   4519:     }
1.1086    raeburn  4520:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range:$extra",$uhome);
1.755     albertel 4521:     my @pairs=split(/\&/,$rep);
                   4522:     my %returnhash=();
1.1098    foxr     4523:     if (!($rep =~ /^error/ )) {
                   4524: 	foreach my $item (@pairs) {
                   4525: 	    my ($key,$value)=split(/=/,$item,2);
                   4526: 	    $key = &unescape($key);
                   4527: 	    next if ($key =~ /^error: 2 /);
                   4528: 	    $returnhash{$key}=&thaw_unescape($value);
                   4529: 	}
1.755     albertel 4530:     }
                   4531:     return %returnhash;
1.407     www      4532: }
                   4533: 
1.1098    foxr     4534: 
1.717     albertel 4535: # --------------------------------------------------------- dumpstore interface
                   4536: 
                   4537: sub dumpstore {
                   4538:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 4539:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4540:    if (!$uname) { $uname=$env{'user.name'}; }
                   4541:    my $uhome=&homeserver($uname,$udomain);
                   4542:    if ($regexp) {
                   4543:        $regexp=&escape($regexp);
                   4544:    } else {
                   4545:        $regexp='.';
                   4546:    }
                   4547:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   4548:    my @pairs=split(/\&/,$rep);
                   4549:    my %returnhash=();
                   4550:    foreach my $item (@pairs) {
                   4551:        my ($key,$value)=split(/=/,$item,2);
                   4552:        next if ($key =~ /^error: 2 /);
                   4553:        $returnhash{$key}=&thaw_unescape($value);
                   4554:    }
                   4555:    return %returnhash;
1.717     albertel 4556: }
                   4557: 
1.407     www      4558: # -------------------------------------------------------------- keys interface
                   4559: 
                   4560: sub getkeys {
                   4561:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 4562:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4563:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      4564:    my $uhome=&homeserver($uname,$udomain);
                   4565:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   4566:    my @keyarray=();
1.800     albertel 4567:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  4568:       next if ($key =~ /^error: 2 /);
1.800     albertel 4569:       push(@keyarray,&unescape($key));
1.407     www      4570:    }
                   4571:    return @keyarray;
1.318     matthew  4572: }
                   4573: 
1.319     matthew  4574: # --------------------------------------------------------------- currentdump
                   4575: sub currentdump {
1.328     matthew  4576:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 4577:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   4578:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   4579:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  4580:    my $uhome = &homeserver($sname,$sdom);
                   4581:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  4582:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  4583:    #
1.318     matthew  4584:    my %returnhash=();
1.319     matthew  4585:    #
                   4586:    if ($rep eq "unknown_cmd") { 
                   4587:        # an old lond will not know currentdump
                   4588:        # Do a dump and make it look like a currentdump
1.822     albertel 4589:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  4590:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   4591:        my %hash = @tmp;
                   4592:        @tmp=();
1.424     matthew  4593:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  4594:    } else {
                   4595:        my @pairs=split(/\&/,$rep);
1.800     albertel 4596:        foreach my $pair (@pairs) {
                   4597:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  4598:            my ($symb,$param) = split(/:/,$key);
                   4599:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 4600:                                                         &thaw_unescape($value);
1.319     matthew  4601:        }
1.191     harris41 4602:    }
1.12      www      4603:    return %returnhash;
1.424     matthew  4604: }
                   4605: 
                   4606: sub convert_dump_to_currentdump{
                   4607:     my %hash = %{shift()};
                   4608:     my %returnhash;
                   4609:     # Code ripped from lond, essentially.  The only difference
                   4610:     # here is the unescaping done by lonnet::dump().  Conceivably
                   4611:     # we might run in to problems with parameter names =~ /^v\./
                   4612:     while (my ($key,$value) = each(%hash)) {
                   4613:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 4614: 	$symb  = &unescape($symb);
                   4615: 	$param = &unescape($param);
1.424     matthew  4616:         next if ($v eq 'version' || $symb eq 'keys');
                   4617:         next if (exists($returnhash{$symb}) &&
                   4618:                  exists($returnhash{$symb}->{$param}) &&
                   4619:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   4620:         $returnhash{$symb}->{$param}=$value;
                   4621:         $returnhash{$symb}->{'v.'.$param}=$v;
                   4622:     }
                   4623:     #
                   4624:     # Remove all of the keys in the hashes which keep track of
                   4625:     # the version of the parameter.
                   4626:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   4627:         # use a foreach because we are going to delete from the hash.
                   4628:         foreach my $key (keys(%$param_hash)) {
                   4629:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   4630:         }
                   4631:     }
                   4632:     return \%returnhash;
1.12      www      4633: }
                   4634: 
1.627     albertel 4635: # ------------------------------------------------------ critical inc interface
                   4636: 
                   4637: sub cinc {
                   4638:     return &inc(@_,'critical');
                   4639: }
                   4640: 
1.449     matthew  4641: # --------------------------------------------------------------- inc interface
                   4642: 
                   4643: sub inc {
1.627     albertel 4644:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 4645:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4646:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  4647:     my $uhome=&homeserver($uname,$udomain);
                   4648:     my $items='';
                   4649:     if (! ref($store)) {
                   4650:         # got a single value, so use that instead
                   4651:         $items = &escape($store).'=&';
                   4652:     } elsif (ref($store) eq 'SCALAR') {
                   4653:         $items = &escape($$store).'=&';        
                   4654:     } elsif (ref($store) eq 'ARRAY') {
                   4655:         $items = join('=&',map {&escape($_);} @{$store});
                   4656:     } elsif (ref($store) eq 'HASH') {
                   4657:         while (my($key,$value) = each(%{$store})) {
                   4658:             $items.= &escape($key).'='.&escape($value).'&';
                   4659:         }
                   4660:     }
                   4661:     $items=~s/\&$//;
1.627     albertel 4662:     if ($critical) {
                   4663: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   4664:     } else {
                   4665: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   4666:     }
1.449     matthew  4667: }
                   4668: 
1.12      www      4669: # --------------------------------------------------------------- put interface
                   4670: 
                   4671: sub put {
1.134     albertel 4672:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 4673:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4674:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 4675:    my $uhome=&homeserver($uname,$udomain);
1.12      www      4676:    my $items='';
1.800     albertel 4677:    foreach my $item (keys(%$storehash)) {
                   4678:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 4679:    }
1.12      www      4680:    $items=~s/\&$//;
1.134     albertel 4681:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      4682: }
                   4683: 
1.631     albertel 4684: # ------------------------------------------------------------ newput interface
                   4685: 
                   4686: sub newput {
                   4687:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   4688:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4689:    if (!$uname) { $uname=$env{'user.name'}; }
                   4690:    my $uhome=&homeserver($uname,$udomain);
                   4691:    my $items='';
                   4692:    foreach my $key (keys(%$storehash)) {
                   4693:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   4694:    }
                   4695:    $items=~s/\&$//;
                   4696:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   4697: }
                   4698: 
                   4699: # ---------------------------------------------------------  putstore interface
                   4700: 
1.524     raeburn  4701: sub putstore {
1.715     albertel 4702:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 4703:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4704:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  4705:    my $uhome=&homeserver($uname,$udomain);
                   4706:    my $items='';
1.715     albertel 4707:    foreach my $key (keys(%$storehash)) {
                   4708:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  4709:    }
1.715     albertel 4710:    $items=~s/\&$//;
1.716     albertel 4711:    my $esc_symb=&escape($symb);
                   4712:    my $esc_v=&escape($version);
1.715     albertel 4713:    my $reply =
1.716     albertel 4714:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 4715: 	      $uhome);
                   4716:    if ($reply eq 'unknown_cmd') {
1.716     albertel 4717:        # gfall back to way things use to be done
1.715     albertel 4718:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   4719: 			    $uname);
1.524     raeburn  4720:    }
1.715     albertel 4721:    return $reply;
                   4722: }
                   4723: 
                   4724: sub old_putstore {
1.716     albertel 4725:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   4726:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4727:     if (!$uname) { $uname=$env{'user.name'}; }
                   4728:     my $uhome=&homeserver($uname,$udomain);
                   4729:     my %newstorehash;
1.800     albertel 4730:     foreach my $item (keys(%$storehash)) {
                   4731: 	my $key = $version.':'.&escape($symb).':'.$item;
                   4732: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 4733:     }
                   4734:     my $items='';
                   4735:     my %allitems = ();
1.800     albertel 4736:     foreach my $item (keys(%newstorehash)) {
                   4737: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 4738: 	    my $key = $1.':keys:'.$2;
                   4739: 	    $allitems{$key} .= $3.':';
                   4740: 	}
1.800     albertel 4741: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 4742:     }
1.800     albertel 4743:     foreach my $item (keys(%allitems)) {
                   4744: 	$allitems{$item} =~ s/\:$//;
                   4745: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 4746:     }
                   4747:     $items=~s/\&$//;
                   4748:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  4749: }
                   4750: 
1.47      www      4751: # ------------------------------------------------------ critical put interface
                   4752: 
                   4753: sub cput {
1.134     albertel 4754:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 4755:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4756:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 4757:    my $uhome=&homeserver($uname,$udomain);
1.47      www      4758:    my $items='';
1.800     albertel 4759:    foreach my $item (keys(%$storehash)) {
                   4760:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 4761:    }
1.47      www      4762:    $items=~s/\&$//;
1.134     albertel 4763:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      4764: }
                   4765: 
                   4766: # -------------------------------------------------------------- eget interface
                   4767: 
                   4768: sub eget {
1.133     albertel 4769:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      4770:    my $items='';
1.800     albertel 4771:    foreach my $item (@$storearr) {
                   4772:        $items.=&escape($item).'&';
1.191     harris41 4773:    }
1.12      www      4774:    $items=~s/\&$//;
1.620     albertel 4775:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   4776:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 4777:    my $uhome=&homeserver($uname,$udomain);
                   4778:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      4779:    my @pairs=split(/\&/,$rep);
                   4780:    my %returnhash=();
1.42      www      4781:    my $i=0;
1.800     albertel 4782:    foreach my $item (@$storearr) {
                   4783:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      4784:       $i++;
1.191     harris41 4785:    }
1.12      www      4786:    return %returnhash;
                   4787: }
                   4788: 
1.667     albertel 4789: # ------------------------------------------------------------ tmpput interface
                   4790: sub tmpput {
1.802     raeburn  4791:     my ($storehash,$server,$context)=@_;
1.667     albertel 4792:     my $items='';
1.800     albertel 4793:     foreach my $item (keys(%$storehash)) {
                   4794: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 4795:     }
                   4796:     $items=~s/\&$//;
1.802     raeburn  4797:     if (defined($context)) {
                   4798:         $items .= ':'.&escape($context);
                   4799:     }
1.667     albertel 4800:     return &reply("tmpput:$items",$server);
                   4801: }
                   4802: 
                   4803: # ------------------------------------------------------------ tmpget interface
                   4804: sub tmpget {
1.688     albertel 4805:     my ($token,$server)=@_;
                   4806:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   4807:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 4808:     my %returnhash;
                   4809:     foreach my $item (split(/\&/,$rep)) {
                   4810: 	my ($key,$value)=split(/=/,$item);
1.951     raeburn  4811:         next if ($key =~ /^error: 2 /);
1.667     albertel 4812: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   4813:     }
                   4814:     return %returnhash;
                   4815: }
                   4816: 
1.688     albertel 4817: # ------------------------------------------------------------ tmpget interface
                   4818: sub tmpdel {
                   4819:     my ($token,$server)=@_;
                   4820:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   4821:     return &reply("tmpdel:$token",$server);
                   4822: }
                   4823: 
1.765     albertel 4824: # -------------------------------------------------- portfolio access checking
                   4825: 
                   4826: sub portfolio_access {
1.766     albertel 4827:     my ($requrl) = @_;
1.765     albertel 4828:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   4829:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  4830:     if ($result) {
                   4831:         my %setters;
                   4832:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4833:             my ($startblock,$endblock) =
                   4834:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   4835:             if ($startblock && $endblock) {
                   4836:                 return 'B';
                   4837:             }
                   4838:         } else {
                   4839:             my ($startblock,$endblock) =
                   4840:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   4841:             if ($startblock && $endblock) {
                   4842:                 return 'B';
                   4843:             }
                   4844:         }
                   4845:     }
1.765     albertel 4846:     if ($result eq 'ok') {
1.766     albertel 4847:        return 'F';
1.765     albertel 4848:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 4849:        return 'A';
1.765     albertel 4850:     }
1.766     albertel 4851:     return '';
1.765     albertel 4852: }
                   4853: 
                   4854: sub get_portfolio_access {
1.767     albertel 4855:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   4856: 
                   4857:     if (!ref($access_hash)) {
                   4858: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   4859: 	my %access_controls = &get_access_controls($current_perms,$group,
                   4860: 						   $file_name);
                   4861: 	$access_hash = $access_controls{$file_name};
                   4862:     }
                   4863: 
1.765     albertel 4864:     my ($public,$guest,@domains,@users,@courses,@groups);
                   4865:     my $now = time;
                   4866:     if (ref($access_hash) eq 'HASH') {
                   4867:         foreach my $key (keys(%{$access_hash})) {
                   4868:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   4869:             if ($start > $now) {
                   4870:                 next;
                   4871:             }
                   4872:             if ($end && $end<$now) {
                   4873:                 next;
                   4874:             }
                   4875:             if ($scope eq 'public') {
                   4876:                 $public = $key;
                   4877:                 last;
                   4878:             } elsif ($scope eq 'guest') {
                   4879:                 $guest = $key;
                   4880:             } elsif ($scope eq 'domains') {
                   4881:                 push(@domains,$key);
                   4882:             } elsif ($scope eq 'users') {
                   4883:                 push(@users,$key);
                   4884:             } elsif ($scope eq 'course') {
                   4885:                 push(@courses,$key);
                   4886:             } elsif ($scope eq 'group') {
                   4887:                 push(@groups,$key);
                   4888:             }
                   4889:         }
                   4890:         if ($public) {
                   4891:             return 'ok';
                   4892:         }
                   4893:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   4894:             if ($guest) {
                   4895:                 return $guest;
                   4896:             }
                   4897:         } else {
                   4898:             if (@domains > 0) {
                   4899:                 foreach my $domkey (@domains) {
                   4900:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   4901:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   4902:                             return 'ok';
                   4903:                         }
                   4904:                     }
                   4905:                 }
                   4906:             }
                   4907:             if (@users > 0) {
                   4908:                 foreach my $userkey (@users) {
1.865     raeburn  4909:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   4910:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   4911:                             if (ref($item) eq 'HASH') {
                   4912:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   4913:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   4914:                                     return 'ok';
                   4915:                                 }
                   4916:                             }
                   4917:                         }
                   4918:                     } 
1.765     albertel 4919:                 }
                   4920:             }
                   4921:             my %roleshash;
                   4922:             my @courses_and_groups = @courses;
                   4923:             push(@courses_and_groups,@groups); 
                   4924:             if (@courses_and_groups > 0) {
                   4925:                 my (%allgroups,%allroles); 
                   4926:                 my ($start,$end,$role,$sec,$group);
                   4927:                 foreach my $envkey (%env) {
1.1060    raeburn  4928:                     if ($envkey =~ m-^user\.role\.(gr|cc|co|in|ta|ep|ad|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 4929:                         my $cid = $2.'_'.$3; 
                   4930:                         if ($1 eq 'gr') {
                   4931:                             $group = $4;
                   4932:                             $allgroups{$cid}{$group} = $env{$envkey};
                   4933:                         } else {
                   4934:                             if ($4 eq '') {
                   4935:                                 $sec = 'none';
                   4936:                             } else {
                   4937:                                 $sec = $4;
                   4938:                             }
                   4939:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   4940:                         }
1.811     albertel 4941:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 4942:                         my $cid = $2.'_'.$3;
                   4943:                         if ($4 eq '') {
                   4944:                             $sec = 'none';
                   4945:                         } else {
                   4946:                             $sec = $4;
                   4947:                         }
                   4948:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   4949:                     }
                   4950:                 }
                   4951:                 if (keys(%allroles) == 0) {
                   4952:                     return;
                   4953:                 }
                   4954:                 foreach my $key (@courses_and_groups) {
                   4955:                     my %content = %{$$access_hash{$key}};
                   4956:                     my $cnum = $content{'number'};
                   4957:                     my $cdom = $content{'domain'};
                   4958:                     my $cid = $cdom.'_'.$cnum;
                   4959:                     if (!exists($allroles{$cid})) {
                   4960:                         next;
                   4961:                     }    
                   4962:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   4963:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   4964:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   4965:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   4966:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   4967:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   4968:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   4969:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   4970:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   4971:                                         if (grep/^all$/,@sections) {
                   4972:                                             return 'ok';
                   4973:                                         } else {
                   4974:                                             if (grep/^$sec$/,@sections) {
                   4975:                                                 return 'ok';
                   4976:                                             }
                   4977:                                         }
                   4978:                                     }
                   4979:                                 }
                   4980:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   4981:                                     if (grep/^none$/,@groups) {
                   4982:                                         return 'ok';
                   4983:                                     }
                   4984:                                 } else {
                   4985:                                     if (grep/^all$/,@groups) {
                   4986:                                         return 'ok';
                   4987:                                     } 
                   4988:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   4989:                                         if (grep/^$group$/,@groups) {
                   4990:                                             return 'ok';
                   4991:                                         }
                   4992:                                     }
                   4993:                                 } 
                   4994:                             }
                   4995:                         }
                   4996:                     }
                   4997:                 }
                   4998:             }
                   4999:             if ($guest) {
                   5000:                 return $guest;
                   5001:             }
                   5002:         }
                   5003:     }
                   5004:     return;
                   5005: }
                   5006: 
                   5007: sub course_group_datechecker {
                   5008:     my ($dates,$now,$status) = @_;
                   5009:     my ($start,$end) = split(/\./,$dates);
                   5010:     if (!$start && !$end) {
                   5011:         return 'ok';
                   5012:     }
                   5013:     if (grep/^active$/,@{$status}) {
                   5014:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   5015:             return 'ok';
                   5016:         }
                   5017:     }
                   5018:     if (grep/^previous$/,@{$status}) {
                   5019:         if ($end > $now ) {
                   5020:             return 'ok';
                   5021:         }
                   5022:     }
                   5023:     if (grep/^future$/,@{$status}) {
                   5024:         if ($start > $now) {
                   5025:             return 'ok';
                   5026:         }
                   5027:     }
                   5028:     return; 
                   5029: }
                   5030: 
                   5031: sub parse_portfolio_url {
                   5032:     my ($url) = @_;
                   5033: 
                   5034:     my ($type,$udom,$unum,$group,$file_name);
                   5035:     
1.823     albertel 5036:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 5037: 	$type = 1;
                   5038:         $udom = $1;
                   5039:         $unum = $2;
                   5040:         $file_name = $3;
1.823     albertel 5041:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 5042: 	$type = 2;
                   5043:         $udom = $1;
                   5044:         $unum = $2;
                   5045:         $group = $3;
                   5046:         $file_name = $3.'/'.$4;
                   5047:     }
                   5048:     if (wantarray) {
                   5049: 	return ($type,$udom,$unum,$file_name,$group);
                   5050:     }
                   5051:     return $type;
                   5052: }
                   5053: 
                   5054: sub is_portfolio_url {
                   5055:     my ($url) = @_;
                   5056:     return scalar(&parse_portfolio_url($url));
                   5057: }
                   5058: 
1.798     raeburn  5059: sub is_portfolio_file {
                   5060:     my ($file) = @_;
1.820     raeburn  5061:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  5062:         return 1;
                   5063:     }
                   5064:     return;
                   5065: }
                   5066: 
1.976     raeburn  5067: sub usertools_access {
1.1084    raeburn  5068:     my ($uname,$udom,$tool,$action,$context,$userenvref,$domdefref,$is_advref)=@_;
1.985     raeburn  5069:     my ($access,%tools);
                   5070:     if ($context eq '') {
                   5071:         $context = 'tools';
                   5072:     }
                   5073:     if ($context eq 'requestcourses') {
                   5074:         %tools = (
                   5075:                       official   => 1,
                   5076:                       unofficial => 1,
1.1006    raeburn  5077:                       community  => 1,
1.985     raeburn  5078:                  );
                   5079:     } else {
                   5080:         %tools = (
                   5081:                       aboutme   => 1,
                   5082:                       blog      => 1,
                   5083:                       portfolio => 1,
                   5084:                  );
                   5085:     }
1.976     raeburn  5086:     return if (!defined($tools{$tool}));
                   5087: 
                   5088:     if ((!defined($udom)) || (!defined($uname))) {
                   5089:         $udom = $env{'user.domain'};
                   5090:         $uname = $env{'user.name'};
                   5091:     }
                   5092: 
1.978     raeburn  5093:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   5094:         if ($action ne 'reload') {
1.985     raeburn  5095:             if ($context eq 'requestcourses') {
                   5096:                 return $env{'environment.canrequest.'.$tool};
                   5097:             } else {
                   5098:                 return $env{'environment.availabletools.'.$tool};
                   5099:             }
                   5100:         }
1.976     raeburn  5101:     }
                   5102: 
                   5103:     my ($toolstatus,$inststatus);
                   5104: 
1.985     raeburn  5105:     if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'}) &&
                   5106:          ($action ne 'reload')) {
                   5107:         $toolstatus = $env{'environment.'.$context.'.'.$tool};
1.976     raeburn  5108:         $inststatus = $env{'environment.inststatus'};
                   5109:     } else {
1.1084    raeburn  5110:         if (ref($userenvref) eq 'HASH') {
                   5111:             $toolstatus = $userenvref->{$context.'.'.$tool};
                   5112:             $inststatus = $userenvref->{'inststatus'};
                   5113:         } else {
                   5114:             my %userenv = &userenvironment($udom,$uname,$context.'.'.$tool,'inststatus');
                   5115:             $toolstatus = $userenv{$context.'.'.$tool};
                   5116:             $inststatus = $userenv{'inststatus'};
                   5117:         }
1.976     raeburn  5118:     }
                   5119: 
                   5120:     if ($toolstatus ne '') {
                   5121:         if ($toolstatus) {
                   5122:             $access = 1;
                   5123:         } else {
                   5124:             $access = 0;
                   5125:         }
                   5126:         return $access;
                   5127:     }
                   5128: 
1.1084    raeburn  5129:     my ($is_adv,%domdef);
                   5130:     if (ref($is_advref) eq 'HASH') {
                   5131:         $is_adv = $is_advref->{'is_adv'};
                   5132:     } else {
                   5133:         $is_adv = &is_advanced_user($udom,$uname);
                   5134:     }
                   5135:     if (ref($domdefref) eq 'HASH') {
                   5136:         %domdef = %{$domdefref};
                   5137:     } else {
                   5138:         %domdef = &get_domain_defaults($udom);
                   5139:     }
1.976     raeburn  5140:     if (ref($domdef{$tool}) eq 'HASH') {
                   5141:         if ($is_adv) {
                   5142:             if ($domdef{$tool}{'_LC_adv'} ne '') {
                   5143:                 if ($domdef{$tool}{'_LC_adv'}) { 
                   5144:                     $access = 1;
                   5145:                 } else {
                   5146:                     $access = 0;
                   5147:                 }
                   5148:                 return $access;
                   5149:             }
                   5150:         }
                   5151:         if ($inststatus ne '') {
                   5152:             my ($hasaccess,$hasnoaccess);
                   5153:             foreach my $affiliation (split(/:/,$inststatus)) {
                   5154:                 if ($domdef{$tool}{$affiliation} ne '') { 
                   5155:                     if ($domdef{$tool}{$affiliation}) {
                   5156:                         $hasaccess = 1;
                   5157:                     } else {
                   5158:                         $hasnoaccess = 1;
                   5159:                     }
                   5160:                 }
                   5161:             }
                   5162:             if ($hasaccess || $hasnoaccess) {
                   5163:                 if ($hasaccess) {
                   5164:                     $access = 1;
                   5165:                 } elsif ($hasnoaccess) {
                   5166:                     $access = 0; 
                   5167:                 }
                   5168:                 return $access;
                   5169:             }
                   5170:         } else {
                   5171:             if ($domdef{$tool}{'default'} ne '') {
                   5172:                 if ($domdef{$tool}{'default'}) {
                   5173:                     $access = 1;
                   5174:                 } elsif ($domdef{$tool}{'default'} == 0) {
                   5175:                     $access = 0;
                   5176:                 }
                   5177:                 return $access;
                   5178:             }
                   5179:         }
                   5180:     } else {
1.985     raeburn  5181:         if ($context eq 'tools') {
                   5182:             $access = 1;
                   5183:         } else {
                   5184:             $access = 0;
                   5185:         }
1.976     raeburn  5186:         return $access;
                   5187:     }
                   5188: }
                   5189: 
1.1050    raeburn  5190: sub is_course_owner {
                   5191:     my ($cdom,$cnum,$udom,$uname) = @_;
                   5192:     if (($udom eq '') || ($uname eq '')) {
                   5193:         $udom = $env{'user.domain'};
                   5194:         $uname = $env{'user.name'};
                   5195:     }
                   5196:     unless (($udom eq '') || ($uname eq '')) {
                   5197:         if (exists($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'})) {
                   5198:             if ($env{'course.'.$cdom.'_'.$cnum.'.internal.courseowner'} eq $uname.':'.$udom) {
                   5199:                 return 1;
                   5200:             } else {
                   5201:                 my %courseinfo = &Apache::lonnet::coursedescription($cdom.'/'.$cnum);
                   5202:                 if ($courseinfo{'internal.courseowner'} eq $uname.':'.$udom) {
                   5203:                     return 1;
                   5204:                 }
                   5205:             }
                   5206:         }
                   5207:     }
                   5208:     return;
                   5209: }
                   5210: 
1.976     raeburn  5211: sub is_advanced_user {
                   5212:     my ($udom,$uname) = @_;
1.1085    raeburn  5213:     if ($udom ne '' && $uname ne '') {
                   5214:         if (($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
                   5215:             return $env{'user.adv'};  
                   5216:         }
                   5217:     }
1.976     raeburn  5218:     my %roleshash = &get_my_roles($uname,$udom,'userroles',undef,undef,undef,1);
                   5219:     my %allroles;
                   5220:     my $is_adv;
                   5221:     foreach my $role (keys(%roleshash)) {
                   5222:         my ($trest,$tdomain,$trole,$sec) = split(/:/,$role);
                   5223:         my $area = '/'.$tdomain.'/'.$trest;
                   5224:         if ($sec ne '') {
                   5225:             $area .= '/'.$sec;
                   5226:         }
                   5227:         if (($area ne '') && ($trole ne '')) {
                   5228:             my $spec=$trole.'.'.$area;
                   5229:             if ($trole =~ /^cr\//) {
                   5230:                 &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
                   5231:             } elsif ($trole ne 'gr') {
                   5232:                 &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
                   5233:             }
                   5234:         }
                   5235:     }
                   5236:     foreach my $role (keys(%allroles)) {
                   5237:         last if ($is_adv);
                   5238:         foreach my $item (split(/:/,$allroles{$role})) {
                   5239:             if ($item ne '') {
                   5240:                 my ($privilege,$restrictions)=split(/&/,$item);
                   5241:                 if ($privilege eq 'adv') {
                   5242:                     $is_adv = 1;
                   5243:                     last;
                   5244:                 }
                   5245:             }
                   5246:         }
                   5247:     }
                   5248:     return $is_adv;
                   5249: }
1.798     raeburn  5250: 
1.1035    raeburn  5251: sub check_can_request {
1.1036    raeburn  5252:     my ($dom,$can_request,$request_domains) = @_;
1.1035    raeburn  5253:     my $canreq = 0;
                   5254:     my ($types,$typename) = &Apache::loncommon::course_types();
                   5255:     my @options = ('approval','validate','autolimit');
                   5256:     my $optregex = join('|',@options);
                   5257:     if ((ref($can_request) eq 'HASH') && (ref($types) eq 'ARRAY')) {
                   5258:         foreach my $type (@{$types}) {
                   5259:             if (&usertools_access($env{'user.name'},
                   5260:                                   $env{'user.domain'},
                   5261:                                   $type,undef,'requestcourses')) {
                   5262:                 $canreq ++;
1.1036    raeburn  5263:                 if (ref($request_domains) eq 'HASH') {
                   5264:                     push(@{$request_domains->{$type}},$env{'user.domain'});
                   5265:                 }
1.1035    raeburn  5266:                 if ($dom eq $env{'user.domain'}) {
                   5267:                     $can_request->{$type} = 1;
                   5268:                 }
                   5269:             }
                   5270:             if ($env{'environment.reqcrsotherdom.'.$type} ne '') {
                   5271:                 my @curr = split(',',$env{'environment.reqcrsotherdom.'.$type});
                   5272:                 if (@curr > 0) {
1.1036    raeburn  5273:                     foreach my $item (@curr) {
                   5274:                         if (ref($request_domains) eq 'HASH') {
                   5275:                             my ($otherdom) = ($item =~ /^($match_domain):($optregex)(=?\d*)$/);
                   5276:                             if ($otherdom ne '') {
                   5277:                                 if (ref($request_domains->{$type}) eq 'ARRAY') {
                   5278:                                     unless (grep(/^\Q$otherdom\E$/,@{$request_domains->{$type}})) {
                   5279:                                         push(@{$request_domains->{$type}},$otherdom);
                   5280:                                     }
                   5281:                                 } else {
                   5282:                                     push(@{$request_domains->{$type}},$otherdom);
                   5283:                                 }
                   5284:                             }
                   5285:                         }
                   5286:                     }
                   5287:                     unless($dom eq $env{'user.domain'}) {
                   5288:                         $canreq ++;
1.1035    raeburn  5289:                         if (grep(/^\Q$dom\E:($optregex)(=?\d*)$/,@curr)) {
                   5290:                             $can_request->{$type} = 1;
                   5291:                         }
                   5292:                     }
                   5293:                 }
                   5294:             }
                   5295:         }
                   5296:     }
                   5297:     return $canreq;
                   5298: }
                   5299: 
1.341     www      5300: # ---------------------------------------------- Custom access rule evaluation
                   5301: 
                   5302: sub customaccess {
                   5303:     my ($priv,$uri)=@_;
1.807     albertel 5304:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      5305:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 5306:     $udom = &LONCAPA::clean_domain($udom);
                   5307:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      5308:     my $access=0;
1.800     albertel 5309:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 5310: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   5311: 	if ($type eq 'user') {
                   5312: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 5313: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 5314: 		if ($tdom) {
                   5315: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   5316: 		}
1.896     albertel 5317: 		if ($tuname) {
                   5318: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 5319: 		}
                   5320: 		$access=($effect eq 'allow');
                   5321: 		last;
                   5322: 	    }
                   5323: 	} else {
                   5324: 	    if ($role) {
                   5325: 		if ($role ne $urole) { next; }
                   5326: 	    }
                   5327: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   5328: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   5329: 		if ($tdom) {
                   5330: 		    if ($tdom ne $udom) { next; }
                   5331: 		}
                   5332: 		if ($tcrs) {
                   5333: 		    if ($tcrs ne $ucrs) { next; }
                   5334: 		}
                   5335: 		if ($tsec) {
                   5336: 		    if ($tsec ne $usec) { next; }
                   5337: 		}
                   5338: 		$access=($effect eq 'allow');
                   5339: 		last;
                   5340: 	    }
                   5341: 	    if ($realm eq '' && $role eq '') {
                   5342: 		$access=($effect eq 'allow');
                   5343: 	    }
1.402     bowersj2 5344: 	}
1.341     www      5345:     }
                   5346:     return $access;
                   5347: }
                   5348: 
1.103     harris41 5349: # ------------------------------------------------- Check for a user privilege
1.12      www      5350: 
                   5351: sub allowed {
1.810     raeburn  5352:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 5353:     my $ver_orguri=$uri;
1.439     www      5354:     $uri=&deversion($uri);
1.152     www      5355:     my $orguri=$uri;
1.52      www      5356:     $uri=&declutter($uri);
1.809     raeburn  5357: 
1.810     raeburn  5358:     if ($priv eq 'evb') {
                   5359: # Evade communication block restrictions for specified role in a course
                   5360:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   5361:             return $1;
                   5362:         } else {
                   5363:             return;
                   5364:         }
                   5365:     }
                   5366: 
1.620     albertel 5367:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      5368: # Free bre access to adm and meta resources
1.775     albertel 5369:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 5370: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   5371: 	&& ($priv eq 'bre')) {
1.14      www      5372: 	return 'F';
1.159     www      5373:     }
                   5374: 
1.545     banghart 5375: # Free bre access to user's own portfolio contents
1.714     raeburn  5376:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  5377:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  5378: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  5379:         my %setters;
                   5380:         my ($startblock,$endblock) = 
                   5381:             &Apache::loncommon::blockcheck(\%setters,'port');
                   5382:         if ($startblock && $endblock) {
                   5383:             return 'B';
                   5384:         } else {
                   5385:             return 'F';
                   5386:         }
1.545     banghart 5387:     }
                   5388: 
1.762     raeburn  5389: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  5390:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   5391:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   5392:         if (exists($env{'request.course.id'})) {
                   5393:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   5394:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   5395:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   5396:                 my $courseprivid=$env{'request.course.id'};
                   5397:                 $courseprivid=~s/\_/\//;
                   5398:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   5399:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   5400:                     return $1; 
1.762     raeburn  5401:                 } else {
                   5402:                     if ($env{'request.course.sec'}) {
                   5403:                         $courseprivid.='/'.$env{'request.course.sec'};
                   5404:                     }
                   5405:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   5406:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   5407:                         return $2;
                   5408:                     }
1.714     raeburn  5409:                 }
                   5410:             }
                   5411:         }
                   5412:     }
                   5413: 
1.159     www      5414: # Free bre to public access
                   5415: 
                   5416:     if ($priv eq 'bre') {
1.238     www      5417:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 5418: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      5419:            return 'F'; 
                   5420:         }
1.238     www      5421:         if ($copyright eq 'priv') {
                   5422:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5423: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      5424: 		return '';
                   5425:             }
                   5426:         }
                   5427:         if ($copyright eq 'domain') {
                   5428:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 5429: 	    unless (($env{'user.domain'} eq $1) ||
                   5430:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      5431: 		return '';
                   5432:             }
1.262     matthew  5433:         }
1.620     albertel 5434:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  5435:             # Library role, so allow browsing of resources in this domain.
                   5436:             return 'F';
1.238     www      5437:         }
1.341     www      5438:         if ($copyright eq 'custom') {
                   5439: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   5440:         }
1.14      www      5441:     }
1.264     matthew  5442:     # Domain coordinator is trying to create a course
1.620     albertel 5443:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  5444:         # uri is the requested domain in this case.
                   5445:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  5446:         # a role of dc for the domain in question.
1.620     albertel 5447:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  5448:     }
1.29      www      5449: 
1.52      www      5450:     my $thisallowed='';
                   5451:     my $statecond=0;
                   5452:     my $courseprivid='';
                   5453: 
1.1039    raeburn  5454:     my $ownaccess;
1.1043    raeburn  5455:     # Community Coordinator or Assistant Co-author browsing resource space.
1.1039    raeburn  5456:     if (($priv eq 'bro') && ($env{'user.author'})) {
                   5457:         if ($uri eq '') {
                   5458:             $ownaccess = 1;
                   5459:         } else {
                   5460:             if (($env{'user.domain'} ne '') && ($env{'user.name'} ne '')) {
                   5461:                 my $udom = $env{'user.domain'};
                   5462:                 my $uname = $env{'user.name'};
                   5463:                 if ($uri =~ m{^\Q$udom\E/?$}) {
                   5464:                     $ownaccess = 1;
1.1040    raeburn  5465:                 } elsif ($uri =~ m{^\Q$udom\E/\Q$uname\E/?}) {
1.1039    raeburn  5466:                     unless ($uri =~ m{\.\./}) {
                   5467:                         $ownaccess = 1;
                   5468:                     }
                   5469:                 } elsif (($udom ne 'public') && ($uname ne 'public')) {
                   5470:                     my $now = time;
                   5471:                     if ($uri =~ m{^([^/]+)/?$}) {
                   5472:                         my $adom = $1;
                   5473:                         foreach my $key (keys(%env)) {
1.1042    raeburn  5474:                             if ($key =~ m{^user\.role\.(ca|aa)/\Q$adom\E}) {
1.1039    raeburn  5475:                                 my ($start,$end) = split('.',$env{$key});
                   5476:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5477:                                     $ownaccess = 1;
                   5478:                                     last;
                   5479:                                 }
                   5480:                             }
                   5481:                         }
                   5482:                     } elsif ($uri =~ m{^([^/]+)/([^/]+)/?}) {
                   5483:                         my $adom = $1;
                   5484:                         my $aname = $2;
1.1042    raeburn  5485:                         foreach my $role ('ca','aa') { 
                   5486:                             if ($env{"user.role.$role./$adom/$aname"}) {
                   5487:                                 my ($start,$end) =
                   5488:                                     split('.',$env{"user.role.$role./$adom/$aname"});
                   5489:                                 if (($now >= $start) && (!$end || $end < $now)) {
                   5490:                                     $ownaccess = 1;
                   5491:                                     last;
                   5492:                                 }
1.1039    raeburn  5493:                             }
                   5494:                         }
                   5495:                     }
                   5496:                 }
                   5497:             }
                   5498:         }
                   5499:     }
                   5500: 
1.52      www      5501: # Course
                   5502: 
1.620     albertel 5503:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5504:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5505:             $thisallowed.=$1;
                   5506:         }
1.52      www      5507:     }
1.29      www      5508: 
1.52      www      5509: # Domain
                   5510: 
1.620     albertel 5511:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 5512:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5513:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5514:             $thisallowed.=$1;
                   5515:         }
1.12      www      5516:     }
1.52      www      5517: 
                   5518: # Course: uri itself is a course
1.66      www      5519:     my $courseuri=$uri;
                   5520:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      5521:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      5522: 
1.620     albertel 5523:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 5524:        =~/\Q$priv\E\&([^\:]*)/) {
1.1043    raeburn  5525:         unless (($priv eq 'bro') && (!$ownaccess)) {
1.1039    raeburn  5526:             $thisallowed.=$1;
                   5527:         }
1.12      www      5528:     }
1.29      www      5529: 
1.665     albertel 5530: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 5531: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 5532:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 5533: 	$thisallowed='';
1.671     raeburn  5534:         my ($match)=&is_on_map($uri);
                   5535:         if ($match) {
                   5536:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   5537:                   =~/\Q$priv\E\&([^\:]*)/) {
                   5538:                 $thisallowed.=$1;
                   5539:             }
                   5540:         } else {
1.705     albertel 5541:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  5542:             if ($refuri) {
                   5543:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  5544:                     $thisallowed='F';
1.671     raeburn  5545:                 } else {
                   5546:                     $refuri=&declutter($refuri);
                   5547:                     my ($match) = &is_on_map($refuri);
                   5548:                     if ($match) {
                   5549:                         $thisallowed='F';
                   5550:                     }
1.669     raeburn  5551:                 }
1.671     raeburn  5552:             }
                   5553:         }
1.314     www      5554:     }
1.492     albertel 5555: 
1.766     albertel 5556:     if ($priv eq 'bre'
                   5557: 	&& $thisallowed ne 'F' 
                   5558: 	&& $thisallowed ne '2'
                   5559: 	&& &is_portfolio_url($uri)) {
                   5560: 	$thisallowed = &portfolio_access($uri);
                   5561:     }
                   5562:     
1.52      www      5563: # Full access at system, domain or course-wide level? Exit.
1.29      www      5564:     if ($thisallowed=~/F/) {
                   5565: 	return 'F';
                   5566:     }
                   5567: 
1.52      www      5568: # If this is generating or modifying users, exit with special codes
1.29      www      5569: 
1.643     www      5570:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   5571: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 5572: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      5573: # no author name given, so this just checks on the general right to make a co-author in this domain
                   5574: 	    unless ($auname) { return $thisallowed; }
                   5575: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 5576: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   5577: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   5578: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   5579: 	}
1.52      www      5580: 	return $thisallowed;
                   5581:     }
                   5582: #
1.103     harris41 5583: # Gathered so far: system, domain and course wide privileges
1.52      www      5584: #
                   5585: # Course: See if uri or referer is an individual resource that is part of 
                   5586: # the course
                   5587: 
1.620     albertel 5588:     if ($env{'request.course.id'}) {
1.232     www      5589: 
1.620     albertel 5590:        $courseprivid=$env{'request.course.id'};
                   5591:        if ($env{'request.course.sec'}) {
                   5592:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      5593:        }
                   5594:        $courseprivid=~s/\_/\//;
                   5595:        my $checkreferer=1;
1.232     www      5596:        my ($match,$cond)=&is_on_map($uri);
                   5597:        if ($match) {
                   5598:            $statecond=$cond;
1.620     albertel 5599:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 5600:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      5601:                $thisallowed.=$1;
                   5602:                $checkreferer=0;
                   5603:            }
1.29      www      5604:        }
1.83      www      5605:        
1.148     www      5606:        if ($checkreferer) {
1.620     albertel 5607: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      5608:             unless ($refuri) {
1.800     albertel 5609:                 foreach my $key (keys(%env)) {
                   5610: 		    if ($key=~/^httpref\..*\*/) {
                   5611: 			my $pattern=$key;
1.156     www      5612:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      5613:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   5614:                         $pattern=~s/\//\\\//g;
1.152     www      5615:                         if ($orguri=~/$pattern/) {
1.800     albertel 5616: 			    $refuri=$env{$key};
1.148     www      5617:                         }
                   5618:                     }
1.191     harris41 5619:                 }
1.148     www      5620:             }
1.232     www      5621: 
1.148     www      5622:          if ($refuri) { 
1.152     www      5623: 	  $refuri=&declutter($refuri);
1.232     www      5624:           my ($match,$cond)=&is_on_map($refuri);
                   5625:             if ($match) {
                   5626:               my $refstatecond=$cond;
1.620     albertel 5627:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 5628:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      5629:                   $thisallowed.=$1;
1.53      www      5630:                   $uri=$refuri;
                   5631:                   $statecond=$refstatecond;
1.52      www      5632:               }
                   5633:           }
1.148     www      5634:         }
1.29      www      5635:        }
1.52      www      5636:    }
1.29      www      5637: 
1.52      www      5638: #
1.103     harris41 5639: # Gathered now: all privileges that could apply, and condition number
1.52      www      5640: # 
                   5641: #
                   5642: # Full or no access?
                   5643: #
1.29      www      5644: 
1.52      www      5645:     if ($thisallowed=~/F/) {
                   5646: 	return 'F';
                   5647:     }
1.29      www      5648: 
1.52      www      5649:     unless ($thisallowed) {
                   5650:         return '';
                   5651:     }
1.29      www      5652: 
1.52      www      5653: # Restrictions exist, deal with them
                   5654: #
                   5655: #   C:according to course preferences
                   5656: #   R:according to resource settings
                   5657: #   L:unless locked
                   5658: #   X:according to user session state
                   5659: #
                   5660: 
                   5661: # Possibly locked functionality, check all courses
1.54      www      5662: # Locks might take effect only after 10 minutes cache expiration for other
                   5663: # courses, and 2 minutes for current course
1.52      www      5664: 
                   5665:     my $envkey;
                   5666:     if ($thisallowed=~/L/) {
1.1000    raeburn  5667:         foreach $envkey (keys(%env)) {
1.54      www      5668:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   5669:                my $courseid=$2;
                   5670:                my $roleid=$1.'.'.$2;
1.92      www      5671:                $courseid=~s/^\///;
1.54      www      5672:                my $expiretime=600;
1.620     albertel 5673:                if ($env{'request.role'} eq $roleid) {
1.54      www      5674: 		  $expiretime=120;
                   5675:                }
                   5676: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   5677:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 5678:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 5679: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      5680:                }
1.620     albertel 5681:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   5682:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   5683: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   5684:                        &log($env{'user.domain'},$env{'user.name'},
                   5685:                             $env{'user.home'},
1.57      www      5686:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      5687:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 5688:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      5689: 		       return '';
                   5690:                    }
                   5691:                }
1.620     albertel 5692:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   5693:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   5694: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   5695:                        &log($env{'user.domain'},$env{'user.name'},
                   5696:                             $env{'user.home'},
1.57      www      5697:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      5698:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 5699:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      5700: 		       return '';
                   5701:                    }
                   5702:                }
                   5703: 	   }
1.29      www      5704:        }
1.52      www      5705:     }
                   5706:    
                   5707: #
                   5708: # Rest of the restrictions depend on selected course
                   5709: #
                   5710: 
1.620     albertel 5711:     unless ($env{'request.course.id'}) {
1.766     albertel 5712: 	if ($thisallowed eq 'A') {
                   5713: 	    return 'A';
1.814     raeburn  5714:         } elsif ($thisallowed eq 'B') {
                   5715:             return 'B';
1.766     albertel 5716: 	} else {
                   5717: 	    return '1';
                   5718: 	}
1.52      www      5719:     }
1.29      www      5720: 
1.52      www      5721: #
                   5722: # Now user is definitely in a course
                   5723: #
1.53      www      5724: 
                   5725: 
                   5726: # Course preferences
                   5727: 
                   5728:    if ($thisallowed=~/C/) {
1.620     albertel 5729:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   5730:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   5731:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 5732: 	   =~/\Q$rolecode\E/) {
1.1103    raeburn  5733: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 5734: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   5735: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   5736: 			$env{'request.course.id'});
                   5737: 	   }
1.237     www      5738:            return '';
                   5739:        }
                   5740: 
1.620     albertel 5741:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 5742: 	   =~/\Q$unamedom\E/) {
1.1103    raeburn  5743: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 5744: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   5745: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   5746: 			$env{'request.course.id'});
                   5747: 	   }
1.54      www      5748:            return '';
                   5749:        }
1.53      www      5750:    }
                   5751: 
                   5752: # Resource preferences
                   5753: 
                   5754:    if ($thisallowed=~/R/) {
1.620     albertel 5755:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 5756:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.1103    raeburn  5757: 	   if (($priv ne 'pch') && ($priv ne 'plc')) { 
1.689     albertel 5758: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   5759: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   5760: 	   }
                   5761: 	   return '';
1.54      www      5762:        }
1.53      www      5763:    }
1.30      www      5764: 
1.246     www      5765: # Restricted by state or randomout?
1.30      www      5766: 
1.52      www      5767:    if ($thisallowed=~/X/) {
1.620     albertel 5768:       if ($env{'acc.randomout'}) {
1.579     albertel 5769: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 5770:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      5771:             return ''; 
                   5772:          }
1.247     www      5773:       }
                   5774:       if (&condval($statecond)) {
1.52      www      5775: 	 return '2';
                   5776:       } else {
                   5777:          return '';
                   5778:       }
                   5779:    }
1.30      www      5780: 
1.766     albertel 5781:     if ($thisallowed eq 'A') {
                   5782: 	return 'A';
1.814     raeburn  5783:     } elsif ($thisallowed eq 'B') {
                   5784:         return 'B';
1.766     albertel 5785:     }
1.52      www      5786:    return 'F';
1.232     www      5787: }
                   5788: 
1.710     albertel 5789: sub split_uri_for_cond {
                   5790:     my $uri=&deversion(&declutter(shift));
                   5791:     my @uriparts=split(/\//,$uri);
                   5792:     my $filename=pop(@uriparts);
                   5793:     my $pathname=join('/',@uriparts);
                   5794:     return ($pathname,$filename);
                   5795: }
1.232     www      5796: # --------------------------------------------------- Is a resource on the map?
                   5797: 
                   5798: sub is_on_map {
1.710     albertel 5799:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 5800:     #Trying to find the conditional for the file
1.620     albertel 5801:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 5802: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      5803:     if ($match) {
1.289     bowersj2 5804: 	return (1,$1);
                   5805:     } else {
1.434     www      5806: 	return (0,0);
1.289     bowersj2 5807:     }
1.12      www      5808: }
                   5809: 
1.427     www      5810: # --------------------------------------------------------- Get symb from alias
                   5811: 
                   5812: sub get_symb_from_alias {
                   5813:     my $symb=shift;
                   5814:     my ($map,$resid,$url)=&decode_symb($symb);
                   5815: # Already is a symb
                   5816:     if ($url) { return $symb; }
                   5817: # Must be an alias
                   5818:     my $aliassymb='';
                   5819:     my %bighash;
1.620     albertel 5820:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      5821:                             &GDBM_READER(),0640)) {
                   5822:         my $rid=$bighash{'mapalias_'.$symb};
                   5823: 	if ($rid) {
                   5824: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 5825: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   5826: 				    $resid,$bighash{'src_'.$rid});
1.427     www      5827: 	}
                   5828:         untie %bighash;
                   5829:     }
                   5830:     return $aliassymb;
                   5831: }
                   5832: 
1.12      www      5833: # ----------------------------------------------------------------- Define Role
                   5834: 
                   5835: sub definerole {
                   5836:   if (allowed('mcr','/')) {
                   5837:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 5838:     foreach my $role (split(':',$sysrole)) {
                   5839: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 5840:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   5841:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   5842: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      5843:                return "refused:s:$crole&$cqual"; 
                   5844:             }
                   5845:         }
1.191     harris41 5846:     }
1.800     albertel 5847:     foreach my $role (split(':',$domrole)) {
                   5848: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 5849:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   5850:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   5851: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      5852:                return "refused:d:$crole&$cqual"; 
                   5853:             }
                   5854:         }
1.191     harris41 5855:     }
1.800     albertel 5856:     foreach my $role (split(':',$courole)) {
                   5857: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 5858:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   5859:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   5860: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      5861:                return "refused:c:$crole&$cqual"; 
                   5862:             }
                   5863:         }
1.191     harris41 5864:     }
1.620     albertel 5865:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   5866:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      5867: 	        "rolesdef_$rolename=".
                   5868:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 5869:     return reply($command,$env{'user.home'});
1.12      www      5870:   } else {
                   5871:     return 'refused';
                   5872:   }
1.105     harris41 5873: }
                   5874: 
                   5875: # ---------------- Make a metadata query against the network of library servers
                   5876: 
                   5877: sub metadata_query {
1.244     matthew  5878:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 5879:     my %rhash;
1.845     albertel 5880:     my %libserv = &all_library();
1.244     matthew  5881:     my @server_list = (defined($server_array) ? @$server_array
                   5882:                                               : keys(%libserv) );
                   5883:     for my $server (@server_list) {
1.118     harris41 5884: 	unless ($custom or $customshow) {
                   5885: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   5886: 	    $rhash{$server}=$reply;
                   5887: 	}
                   5888: 	else {
                   5889: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   5890: 			     &escape($custom).':'.&escape($customshow),
                   5891: 			     $server);
                   5892: 	    $rhash{$server}=$reply;
                   5893: 	}
1.112     harris41 5894:     }
1.118     harris41 5895:     return \%rhash;
1.240     www      5896: }
                   5897: 
                   5898: # ----------------------------------------- Send log queries and wait for reply
                   5899: 
                   5900: sub log_query {
                   5901:     my ($uname,$udom,$query,%filters)=@_;
                   5902:     my $uhome=&homeserver($uname,$udom);
                   5903:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 5904:     my $uhost=&hostname($uhome);
1.800     albertel 5905:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      5906:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   5907:                        $uhome);
1.479     albertel 5908:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      5909:     return get_query_reply($queryid);
                   5910: }
                   5911: 
1.818     raeburn  5912: # -------------------------- Update MySQL table for portfolio file
                   5913: 
                   5914: sub update_portfolio_table {
1.821     raeburn  5915:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.970     raeburn  5916:     if ($group ne '') {
                   5917:         $file_name =~s /^\Q$group\E//;
                   5918:     }
1.818     raeburn  5919:     my $homeserver = &homeserver($uname,$udom);
                   5920:     my $queryid=
1.821     raeburn  5921:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   5922:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  5923:     my $reply = &get_query_reply($queryid);
                   5924:     return $reply;
                   5925: }
                   5926: 
1.899     raeburn  5927: # -------------------------- Update MySQL allusers table
                   5928: 
                   5929: sub update_allusers_table {
                   5930:     my ($uname,$udom,$names) = @_;
                   5931:     my $homeserver = &homeserver($uname,$udom);
                   5932:     my $queryid=
                   5933:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   5934:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   5935:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   5936:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   5937:                'generation='.&escape($names->{'generation'}).'%%'.
                   5938:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   5939:                'id='.&escape($names->{'id'}),$homeserver);
1.1075    raeburn  5940:     return;
1.899     raeburn  5941: }
                   5942: 
1.508     raeburn  5943: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  5944: 
                   5945: sub fetch_enrollment_query {
1.511     raeburn  5946:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  5947:     my $homeserver;
1.547     raeburn  5948:     my $maxtries = 1;
1.508     raeburn  5949:     if ($context eq 'automated') {
                   5950:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  5951:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  5952:     } else {
                   5953:         $homeserver = &homeserver($cnum,$dom);
                   5954:     }
1.838     albertel 5955:     my $host=&hostname($homeserver);
1.506     raeburn  5956:     my $cmd = '';
1.1000    raeburn  5957:     foreach my $affiliate (keys(%{$affiliatesref})) {
1.800     albertel 5958:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  5959:     }
                   5960:     $cmd =~ s/%%$//;
                   5961:     $cmd = &escape($cmd);
                   5962:     my $query = 'fetchenrollment';
1.620     albertel 5963:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  5964:     unless ($queryid=~/^\Q$host\E\_/) { 
                   5965:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   5966:         return 'error: '.$queryid;
                   5967:     }
1.506     raeburn  5968:     my $reply = &get_query_reply($queryid);
1.547     raeburn  5969:     my $tries = 1;
                   5970:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   5971:         $reply = &get_query_reply($queryid);
                   5972:         $tries ++;
                   5973:     }
1.526     raeburn  5974:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 5975:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  5976:     } else {
1.901     albertel 5977:         my @responses = split(/:/,$reply);
1.515     raeburn  5978:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 5979:             foreach my $line (@responses) {
                   5980:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  5981:                 $$replyref{$key} = $value;
                   5982:             }
                   5983:         } else {
1.506     raeburn  5984:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 5985:             foreach my $line (@responses) {
                   5986:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  5987:                 $$replyref{$key} = $value;
                   5988:                 if ($value > 0) {
1.800     albertel 5989:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   5990:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  5991:                         my $destname = $pathname.'/'.$filename;
                   5992:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  5993:                         if ($xml_classlist =~ /^error/) {
                   5994:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   5995:                         } else {
1.506     raeburn  5996:                             if ( open(FILE,">$destname") ) {
                   5997:                                 print FILE &unescape($xml_classlist);
                   5998:                                 close(FILE);
1.526     raeburn  5999:                             } else {
                   6000:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  6001:                             }
                   6002:                         }
                   6003:                     }
                   6004:                 }
                   6005:             }
                   6006:         }
                   6007:         return 'ok';
                   6008:     }
                   6009:     return 'error';
                   6010: }
                   6011: 
1.242     www      6012: sub get_query_reply {
                   6013:     my $queryid=shift;
1.240     www      6014:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   6015:     my $reply='';
                   6016:     for (1..100) {
                   6017: 	sleep 2;
                   6018:         if (-e $replyfile.'.end') {
1.448     albertel 6019: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 6020: 		$reply = join('',<$fh>);
                   6021: 		close($fh);
1.240     www      6022: 	   } else { return 'error: reply_file_error'; }
1.242     www      6023:            return &unescape($reply);
                   6024: 	}
1.240     www      6025:     }
1.242     www      6026:     return 'timeout:'.$queryid;
1.240     www      6027: }
                   6028: 
                   6029: sub courselog_query {
1.241     www      6030: #
                   6031: # possible filters:
                   6032: # url: url or symb
                   6033: # username
                   6034: # domain
                   6035: # action: view, submit, grade
                   6036: # start: timestamp
                   6037: # end: timestamp
                   6038: #
1.240     www      6039:     my (%filters)=@_;
1.620     albertel 6040:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      6041:     if ($filters{'url'}) {
                   6042: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   6043:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   6044:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   6045:     }
1.620     albertel 6046:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   6047:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      6048:     return &log_query($cname,$cdom,'courselog',%filters);
                   6049: }
                   6050: 
                   6051: sub userlog_query {
1.858     raeburn  6052: #
                   6053: # possible filters:
                   6054: # action: log check role
                   6055: # start: timestamp
                   6056: # end: timestamp
                   6057: #
1.240     www      6058:     my ($uname,$udom,%filters)=@_;
                   6059:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      6060: }
                   6061: 
1.506     raeburn  6062: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   6063: 
                   6064: sub auto_run {
1.508     raeburn  6065:     my ($cnum,$cdom) = @_;
1.876     raeburn  6066:     my $response = 0;
                   6067:     my $settings;
                   6068:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   6069:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   6070:         $settings = $domconfig{'autoenroll'};
                   6071:         if ($settings->{'run'} eq '1') {
                   6072:             $response = 1;
                   6073:         }
                   6074:     } else {
1.934     raeburn  6075:         my $homeserver;
                   6076:         if (&is_course($cdom,$cnum)) {
                   6077:             $homeserver = &homeserver($cnum,$cdom);
                   6078:         } else {
                   6079:             $homeserver = &domain($cdom,'primary');
                   6080:         }
                   6081:         if ($homeserver ne 'no_host') {
                   6082:             $response = &reply('autorun:'.$cdom,$homeserver);
                   6083:         }
1.876     raeburn  6084:     }
1.506     raeburn  6085:     return $response;
                   6086: }
1.776     albertel 6087: 
1.506     raeburn  6088: sub auto_get_sections {
1.508     raeburn  6089:     my ($cnum,$cdom,$inst_coursecode) = @_;
1.1007    raeburn  6090:     my $homeserver;
                   6091:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) { 
                   6092:         $homeserver = &homeserver($cnum,$cdom);
                   6093:     }
                   6094:     if (!defined($homeserver)) { 
                   6095:         if ($cdom =~ /^$match_domain$/) {
                   6096:             $homeserver = &domain($cdom,'primary');
                   6097:         }
                   6098:     }
                   6099:     my @secs;
                   6100:     if (defined($homeserver)) {
                   6101:         my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
                   6102:         unless ($response eq 'refused') {
                   6103:             @secs = split(/:/,$response);
                   6104:         }
1.506     raeburn  6105:     }
                   6106:     return @secs;
                   6107: }
1.776     albertel 6108: 
1.506     raeburn  6109: sub auto_new_course {
1.1099    raeburn  6110:     my ($cnum,$cdom,$inst_course_id,$owner,$coowners) = @_;
1.508     raeburn  6111:     my $homeserver = &homeserver($cnum,$cdom);
1.1099    raeburn  6112:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.&escape($owner).':'.$cdom.':'.&escape($coowners),$homeserver));
1.506     raeburn  6113:     return $response;
                   6114: }
1.776     albertel 6115: 
1.506     raeburn  6116: sub auto_validate_courseID {
1.508     raeburn  6117:     my ($cnum,$cdom,$inst_course_id) = @_;
                   6118:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  6119:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  6120:     return $response;
                   6121: }
1.776     albertel 6122: 
1.1007    raeburn  6123: sub auto_validate_instcode {
1.1020    raeburn  6124:     my ($cnum,$cdom,$instcode,$owner) = @_;
1.1007    raeburn  6125:     my ($homeserver,$response);
                   6126:     if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6127:         $homeserver = &homeserver($cnum,$cdom);
                   6128:     }
                   6129:     if (!defined($homeserver)) {
                   6130:         if ($cdom =~ /^$match_domain$/) {
                   6131:             $homeserver = &domain($cdom,'primary');
                   6132:         }
                   6133:     }
1.1065    raeburn  6134:     $response=&unescape(&reply('autovalidateinstcode:'.$cdom.':'.
                   6135:                         &escape($instcode).':'.&escape($owner),$homeserver));
1.1027    raeburn  6136:     my ($outcome,$description) = map { &unescape($_); } split('&',$response,2);
                   6137:     return ($outcome,$description);
1.1007    raeburn  6138: }
                   6139: 
1.506     raeburn  6140: sub auto_create_password {
1.873     raeburn  6141:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   6142:     my ($homeserver,$response);
1.506     raeburn  6143:     my $create_passwd = 0;
                   6144:     my $authchk = '';
1.873     raeburn  6145:     if ($udom =~ /^$match_domain$/) {
                   6146:         $homeserver = &domain($udom,'primary');
                   6147:     }
                   6148:     if ($homeserver eq '') {
                   6149:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   6150:             $homeserver = &homeserver($cnum,$cdom);
                   6151:         }
                   6152:     }
                   6153:     if ($homeserver eq '') {
                   6154:         $authchk = 'nodomain';
1.506     raeburn  6155:     } else {
1.873     raeburn  6156:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   6157:         if ($response eq 'refused') {
                   6158:             $authchk = 'refused';
                   6159:         } else {
1.901     albertel 6160:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  6161:         }
1.506     raeburn  6162:     }
                   6163:     return ($authparam,$create_passwd,$authchk);
                   6164: }
                   6165: 
1.706     raeburn  6166: sub auto_photo_permission {
                   6167:     my ($cnum,$cdom,$students) = @_;
                   6168:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 6169:     my ($outcome,$perm_reqd,$conditions) = 
                   6170: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 6171:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6172: 	return (undef,undef);
                   6173:     }
1.706     raeburn  6174:     return ($outcome,$perm_reqd,$conditions);
                   6175: }
                   6176: 
                   6177: sub auto_checkphotos {
                   6178:     my ($uname,$udom,$pid) = @_;
                   6179:     my $homeserver = &homeserver($uname,$udom);
                   6180:     my ($result,$resulttype);
                   6181:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 6182: 				   &escape($uname).':'.&escape($pid),
                   6183: 				   $homeserver));
1.709     albertel 6184:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6185: 	return (undef,undef);
                   6186:     }
1.706     raeburn  6187:     if ($outcome) {
                   6188:         ($result,$resulttype) = split(/:/,$outcome);
                   6189:     } 
                   6190:     return ($result,$resulttype);
                   6191: }
                   6192: 
                   6193: sub auto_photochoice {
                   6194:     my ($cnum,$cdom) = @_;
                   6195:     my $homeserver = &homeserver($cnum,$cdom);
                   6196:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 6197: 						       &escape($cdom),
                   6198: 						       $homeserver)));
1.709     albertel 6199:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   6200: 	return (undef,undef);
                   6201:     }
1.706     raeburn  6202:     return ($update,$comment);
                   6203: }
                   6204: 
                   6205: sub auto_photoupdate {
                   6206:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   6207:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 6208:     my $host=&hostname($homeserver);
1.706     raeburn  6209:     my $cmd = '';
                   6210:     my $maxtries = 1;
1.800     albertel 6211:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   6212:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  6213:     }
                   6214:     $cmd =~ s/%%$//;
                   6215:     $cmd = &escape($cmd);
                   6216:     my $query = 'institutionalphotos';
                   6217:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   6218:     unless ($queryid=~/^\Q$host\E\_/) {
                   6219:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   6220:         return 'error: '.$queryid;
                   6221:     }
                   6222:     my $reply = &get_query_reply($queryid);
                   6223:     my $tries = 1;
                   6224:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   6225:         $reply = &get_query_reply($queryid);
                   6226:         $tries ++;
                   6227:     }
                   6228:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   6229:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   6230:     } else {
                   6231:         my @responses = split(/:/,$reply);
                   6232:         my $outcome = shift(@responses); 
                   6233:         foreach my $item (@responses) {
                   6234:             my ($key,$value) = split(/=/,$item);
                   6235:             $$photo{$key} = $value;
                   6236:         }
                   6237:         return $outcome;
                   6238:     }
                   6239:     return 'error';
                   6240: }
                   6241: 
1.521     raeburn  6242: sub auto_instcode_format {
1.793     albertel 6243:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   6244: 	$cat_order) = @_;
1.521     raeburn  6245:     my $courses = '';
1.772     raeburn  6246:     my @homeservers;
1.521     raeburn  6247:     if ($caller eq 'global') {
1.841     albertel 6248: 	my %servers = &get_servers($codedom,'library');
                   6249: 	foreach my $tryserver (keys(%servers)) {
                   6250: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6251: 		push(@homeservers,$tryserver);
                   6252: 	    }
1.584     raeburn  6253:         }
1.1022    raeburn  6254:     } elsif ($caller eq 'requests') {
                   6255:         if ($codedom =~ /^$match_domain$/) {
                   6256:             my $chome = &domain($codedom,'primary');
                   6257:             unless ($chome eq 'no_host') {
                   6258:                 push(@homeservers,$chome);
                   6259:             }
                   6260:         }
1.521     raeburn  6261:     } else {
1.772     raeburn  6262:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  6263:     }
1.793     albertel 6264:     foreach my $code (keys(%{$instcodes})) {
                   6265:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  6266:     }
                   6267:     chop($courses);
1.772     raeburn  6268:     my $ok_response = 0;
                   6269:     my $response;
                   6270:     while (@homeservers > 0 && $ok_response == 0) {
                   6271:         my $server = shift(@homeservers); 
                   6272:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   6273:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   6274:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 6275: 		split(/:/,$response);
1.772     raeburn  6276:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   6277:             push(@{$codetitles},&str2array($codetitles_str));
                   6278:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   6279:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   6280:             $ok_response = 1;
                   6281:         }
                   6282:     }
                   6283:     if ($ok_response) {
1.521     raeburn  6284:         return 'ok';
1.772     raeburn  6285:     } else {
                   6286:         return $response;
1.521     raeburn  6287:     }
                   6288: }
                   6289: 
1.792     raeburn  6290: sub auto_instcode_defaults {
                   6291:     my ($domain,$returnhash,$code_order) = @_;
                   6292:     my @homeservers;
1.841     albertel 6293: 
                   6294:     my %servers = &get_servers($domain,'library');
                   6295:     foreach my $tryserver (keys(%servers)) {
                   6296: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6297: 	    push(@homeservers,$tryserver);
                   6298: 	}
1.792     raeburn  6299:     }
1.841     albertel 6300: 
1.792     raeburn  6301:     my $response;
1.841     albertel 6302:     foreach my $server (@homeservers) {
1.792     raeburn  6303:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 6304:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   6305: 	
                   6306: 	foreach my $pair (split(/\&/,$response)) {
                   6307: 	    my ($name,$value)=split(/\=/,$pair);
                   6308: 	    if ($name eq 'code_order') {
                   6309: 		@{$code_order} = split(/\&/,&unescape($value));
                   6310: 	    } else {
                   6311: 		$returnhash->{&unescape($name)}=&unescape($value);
                   6312: 	    }
                   6313: 	}
                   6314: 	return 'ok';
1.792     raeburn  6315:     }
1.841     albertel 6316: 
                   6317:     return $response;
1.1003    raeburn  6318: }
                   6319: 
                   6320: sub auto_possible_instcodes {
1.1007    raeburn  6321:     my ($domain,$codetitles,$cat_titles,$cat_orders,$code_order) = @_;
                   6322:     unless ((ref($codetitles) eq 'ARRAY') && (ref($cat_titles) eq 'HASH') && 
                   6323:             (ref($cat_orders) eq 'HASH') && (ref($code_order) eq 'ARRAY')) {
                   6324:         return;
                   6325:     }
1.1003    raeburn  6326:     my (@homeservers,$uhome);
                   6327:     if (defined(&domain($domain,'primary'))) {
                   6328:         $uhome=&domain($domain,'primary');
                   6329:         push(@homeservers,&domain($domain,'primary'));
                   6330:     } else {
                   6331:         my %servers = &get_servers($domain,'library');
                   6332:         foreach my $tryserver (keys(%servers)) {
                   6333:             if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   6334:                 push(@homeservers,$tryserver);
                   6335:             }
                   6336:         }
                   6337:     }
                   6338:     my $response;
                   6339:     foreach my $server (@homeservers) {
                   6340:         $response=&reply('autopossibleinstcodes:'.$domain,$server);
                   6341:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
1.1007    raeburn  6342:         my ($codetitlestr,$codeorderstr,$cat_title,$cat_order) = 
                   6343:             split(':',$response);
                   6344:         @{$codetitles} = map { &unescape($_); } (split('&',$codetitlestr));
                   6345:         @{$code_order} = map { &unescape($_); } (split('&',$codeorderstr));
1.1003    raeburn  6346:         foreach my $item (split('&',$cat_title)) {   
1.1005    raeburn  6347:             my ($name,$value)=split('=',$item);
                   6348:             $cat_titles->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6349:         }
                   6350:         foreach my $item (split('&',$cat_order)) {
1.1005    raeburn  6351:             my ($name,$value)=split('=',$item);
                   6352:             $cat_orders->{&unescape($name)}=&thaw_unescape($value);
1.1003    raeburn  6353:         }
                   6354:         return 'ok';
                   6355:     }
                   6356:     return $response;
                   6357: }
1.792     raeburn  6358: 
1.1010    raeburn  6359: sub auto_courserequest_checks {
                   6360:     my ($dom) = @_;
1.1020    raeburn  6361:     my ($homeserver,%validations);
                   6362:     if ($dom =~ /^$match_domain$/) {
                   6363:         $homeserver = &domain($dom,'primary');
                   6364:     }
                   6365:     unless ($homeserver eq 'no_host') {
                   6366:         my $response=&reply('autocrsreqchecks:'.$dom,$homeserver);
                   6367:         unless ($response =~ /(con_lost|error|no_such_host|refused)/) {
                   6368:             my @items = split(/&/,$response);
                   6369:             foreach my $item (@items) {
                   6370:                 my ($key,$value) = split('=',$item);
                   6371:                 $validations{&unescape($key)} = &thaw_unescape($value);
                   6372:             }
                   6373:         }
                   6374:     }
1.1010    raeburn  6375:     return %validations; 
                   6376: }
                   6377: 
1.1020    raeburn  6378: sub auto_courserequest_validation {
                   6379:     my ($dom,$owner,$crstype,$inststatuslist,$instcode,$instseclist) = @_;
                   6380:     my ($homeserver,$response);
                   6381:     if ($dom =~ /^$match_domain$/) {
                   6382:         $homeserver = &domain($dom,'primary');
                   6383:     }
                   6384:     unless ($homeserver eq 'no_host') {  
1.1021    raeburn  6385:           
1.1020    raeburn  6386:         $response=&unescape(&reply('autocrsreqvalidation:'.$dom.':'.&escape($owner).
1.1021    raeburn  6387:                                     ':'.&escape($crstype).':'.&escape($inststatuslist).
1.1020    raeburn  6388:                                     ':'.&escape($instcode).':'.&escape($instseclist),
                   6389:                                     $homeserver));
                   6390:     }
                   6391:     return $response;
                   6392: }
                   6393: 
1.777     albertel 6394: sub auto_validate_class_sec {
1.918     raeburn  6395:     my ($cdom,$cnum,$owners,$inst_class) = @_;
1.773     raeburn  6396:     my $homeserver = &homeserver($cnum,$cdom);
1.918     raeburn  6397:     my $ownerlist;
                   6398:     if (ref($owners) eq 'ARRAY') {
                   6399:         $ownerlist = join(',',@{$owners});
                   6400:     } else {
                   6401:         $ownerlist = $owners;
                   6402:     }
1.773     raeburn  6403:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.918     raeburn  6404:                         &escape($ownerlist).':'.$cdom,$homeserver);
1.773     raeburn  6405:     return $response;
                   6406: }
                   6407: 
1.679     raeburn  6408: # ------------------------------------------------------- Course Group routines
                   6409: 
                   6410: sub get_coursegroups {
1.809     raeburn  6411:     my ($cdom,$cnum,$group,$namespace) = @_;
                   6412:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  6413: }
                   6414: 
1.679     raeburn  6415: sub modify_coursegroup {
                   6416:     my ($cdom,$cnum,$groupsettings) = @_;
                   6417:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   6418: }
                   6419: 
1.809     raeburn  6420: sub toggle_coursegroup_status {
                   6421:     my ($cdom,$cnum,$group,$action) = @_;
                   6422:     my ($from_namespace,$to_namespace);
                   6423:     if ($action eq 'delete') {
                   6424:         $from_namespace = 'coursegroups';
                   6425:         $to_namespace = 'deleted_groups';
                   6426:     } else {
                   6427:         $from_namespace = 'deleted_groups';
                   6428:         $to_namespace = 'coursegroups';
                   6429:     }
                   6430:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  6431:     if (my $tmp = &error(%curr_group)) {
                   6432:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   6433:         return ('read error',$tmp);
                   6434:     } else {
                   6435:         my %savedsettings = %curr_group; 
1.809     raeburn  6436:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  6437:         my $deloutcome;
                   6438:         if ($result eq 'ok') {
1.809     raeburn  6439:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  6440:         } else {
                   6441:             return ('write error',$result);
                   6442:         }
                   6443:         if ($deloutcome eq 'ok') {
                   6444:             return 'ok';
                   6445:         } else {
                   6446:             return ('delete error',$deloutcome);
                   6447:         }
                   6448:     }
                   6449: }
                   6450: 
1.679     raeburn  6451: sub modify_group_roles {
1.957     raeburn  6452:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs,$selfenroll,$context) = @_;
1.679     raeburn  6453:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   6454:     my $role = 'gr/'.&escape($userprivs);
                   6455:     my ($uname,$udom) = split(/:/,$user);
1.957     raeburn  6456:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start,'',$selfenroll,$context);
1.684     raeburn  6457:     if ($result eq 'ok') {
                   6458:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   6459:     }
1.679     raeburn  6460:     return $result;
                   6461: }
                   6462: 
                   6463: sub modify_coursegroup_membership {
                   6464:     my ($cdom,$cnum,$membership) = @_;
                   6465:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   6466:     return $result;
                   6467: }
                   6468: 
1.682     raeburn  6469: sub get_active_groups {
                   6470:     my ($udom,$uname,$cdom,$cnum) = @_;
                   6471:     my $now = time;
                   6472:     my %groups = ();
                   6473:     foreach my $key (keys(%env)) {
1.811     albertel 6474:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  6475:             my ($start,$end) = split(/\./,$env{$key});
                   6476:             if (($end!=0) && ($end<$now)) { next; }
                   6477:             if (($start!=0) && ($start>$now)) { next; }
                   6478:             if ($1 eq $cdom && $2 eq $cnum) {
                   6479:                 $groups{$3} = $env{$key} ;
                   6480:             }
                   6481:         }
                   6482:     }
                   6483:     return %groups;
                   6484: }
                   6485: 
1.683     raeburn  6486: sub get_group_membership {
                   6487:     my ($cdom,$cnum,$group) = @_;
                   6488:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   6489: }
                   6490: 
                   6491: sub get_users_groups {
                   6492:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  6493:     my @usersgroups;
1.683     raeburn  6494:     my $cachetime=1800;
                   6495: 
                   6496:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  6497:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   6498:     if (defined($cached)) {
1.734     albertel 6499:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  6500:     } else {  
                   6501:         $grouplist = '';
1.816     raeburn  6502:         my $courseurl = &courseid_to_courseurl($courseid);
1.1086    raeburn  6503:         my $extra = &freeze_escape({'skipcheck' => 1});
                   6504:         my %roleshash = &dump('roles',$udom,$uname,$courseurl,undef,$extra);
1.817     raeburn  6505:         my $access_end = $env{'course.'.$courseid.
                   6506:                               '.default_enrollment_end_date'};
                   6507:         my $now = time;
                   6508:         foreach my $key (keys(%roleshash)) {
                   6509:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   6510:                 my $group = $1;
                   6511:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   6512:                     my $start = $2;
                   6513:                     my $end = $1;
                   6514:                     if ($start == -1) { next; } # deleted from group
                   6515:                     if (($start!=0) && ($start>$now)) { next; }
                   6516:                     if (($end!=0) && ($end<$now)) {
                   6517:                         if ($access_end && $access_end < $now) {
                   6518:                             if ($access_end - $end < 86400) {
                   6519:                                 push(@usersgroups,$group);
1.733     raeburn  6520:                             }
                   6521:                         }
1.817     raeburn  6522:                         next;
1.733     raeburn  6523:                     }
1.817     raeburn  6524:                     push(@usersgroups,$group);
1.683     raeburn  6525:                 }
                   6526:             }
                   6527:         }
1.817     raeburn  6528:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   6529:         $grouplist = join(':',@usersgroups);
                   6530:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  6531:     }
1.733     raeburn  6532:     return @usersgroups;
1.683     raeburn  6533: }
                   6534: 
                   6535: sub devalidate_getgroups_cache {
                   6536:     my ($udom,$uname,$cdom,$cnum)=@_;
                   6537:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 6538: 
1.683     raeburn  6539:     my $hashid="$udom:$uname:$courseid";
                   6540:     &devalidate_cache_new('getgroups',$hashid);
                   6541: }
                   6542: 
1.12      www      6543: # ------------------------------------------------------------------ Plain Text
                   6544: 
                   6545: sub plaintext {
1.988     raeburn  6546:     my ($short,$type,$cid,$forcedefault) = @_;
1.1046    raeburn  6547:     if ($short =~ m{^cr/}) {
1.758     albertel 6548: 	return (split('/',$short))[-1];
                   6549:     }
1.742     raeburn  6550:     if (!defined($cid)) {
                   6551:         $cid = $env{'request.course.id'};
                   6552:     }
                   6553:     my %rolenames = (
1.1008    raeburn  6554:                       Course    => 'std',
                   6555:                       Community => 'alt1',
1.742     raeburn  6556:                     );
1.1037    raeburn  6557:     if ($cid ne '') {
                   6558:         if ($env{'course.'.$cid.'.'.$short.'.plaintext'} ne '') {
                   6559:             unless ($forcedefault) {
                   6560:                 my $roletext = $env{'course.'.$cid.'.'.$short.'.plaintext'}; 
                   6561:                 &Apache::lonlocal::mt_escape(\$roletext);
                   6562:                 return &Apache::lonlocal::mt($roletext);
                   6563:             }
                   6564:         }
                   6565:     }
                   6566:     if ((defined($type)) && (defined($rolenames{$type})) &&
                   6567:         (defined($rolenames{$type})) && 
                   6568:         (defined($prp{$short}{$rolenames{$type}}))) {
1.742     raeburn  6569:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
1.1037    raeburn  6570:     } elsif ($cid ne '') {
                   6571:         my $crstype = $env{'course.'.$cid.'.type'};
                   6572:         if (($crstype ne '') && (defined($rolenames{$crstype})) &&
                   6573:             (defined($prp{$short}{$rolenames{$crstype}}))) {
                   6574:             return &Apache::lonlocal::mt($prp{$short}{$rolenames{$crstype}});
                   6575:         }
1.742     raeburn  6576:     }
1.1037    raeburn  6577:     return &Apache::lonlocal::mt($prp{$short}{'std'});
1.12      www      6578: }
                   6579: 
                   6580: # ----------------------------------------------------------------- Assign Role
                   6581: 
                   6582: sub assignrole {
1.957     raeburn  6583:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,
                   6584:         $context)=@_;
1.21      www      6585:     my $mrole;
                   6586:     if ($role =~ /^cr\//) {
1.393     www      6587:         my $cwosec=$url;
1.811     albertel 6588:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      6589: 	unless (&allowed('ccr',$cwosec)) {
1.1026    raeburn  6590:            my $refused = 1;
                   6591:            if ($context eq 'requestcourses') {
                   6592:                if (($env{'user.name'} ne '') && ($env{'user.domain'} ne '')) {
                   6593:                    if ($role =~ m{^cr/($match_domain)/($match_username)/([^/]+)$}) {
                   6594:                        if (($1 eq $env{'user.domain'}) && ($2 eq $env{'user.name'})) {
                   6595:                            my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   6596:                            my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   6597:                            if ($crsenv{'internal.courseowner'} eq
                   6598:                                $env{'user.name'}.':'.$env{'user.domain'}) {
                   6599:                                $refused = '';
                   6600:                            }
                   6601:                        }
                   6602:                    }
                   6603:                }
                   6604:            }
                   6605:            if ($refused) {
                   6606:                &logthis('Refused custom assignrole: '.
                   6607:                         $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.
                   6608:                         ' by '.$env{'user.name'}.' at '.$env{'user.domain'});
                   6609:                return 'refused';
                   6610:            }
1.104     www      6611:         }
1.21      www      6612:         $mrole='cr';
1.678     raeburn  6613:     } elsif ($role =~ /^gr\//) {
                   6614:         my $cwogrp=$url;
1.811     albertel 6615:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  6616:         unless (&allowed('mdg',$cwogrp)) {
                   6617:             &logthis('Refused group assignrole: '.
                   6618:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   6619:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   6620:             return 'refused';
                   6621:         }
                   6622:         $mrole='gr';
1.21      www      6623:     } else {
1.82      www      6624:         my $cwosec=$url;
1.811     albertel 6625:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.932     raeburn  6626:         if (!(&allowed('c'.$role,$cwosec)) && !(&allowed('c'.$role,$udom))) {
                   6627:             my $refused;
                   6628:             if (($env{'request.course.sec'}  ne '') && ($role eq 'st')) {
                   6629:                 if (!(&allowed('c'.$role,$url))) {
                   6630:                     $refused = 1;
                   6631:                 }
                   6632:             } else {
                   6633:                 $refused = 1;
                   6634:             }
1.947     raeburn  6635:             if ($refused) {
1.1045    raeburn  6636:                 my ($cdom,$cnum) = ($cwosec =~ m{^/?($match_domain)/($match_courseid)$});
                   6637:                 if (!$selfenroll && $context eq 'course') {
                   6638:                     my %crsenv;
                   6639:                     if ($role eq 'cc' || $role eq 'co') {
                   6640:                         %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   6641:                         if (($role eq 'cc') && ($cnum !~ /^$match_community$/)) {
                   6642:                             if ($env{'request.role'} eq 'cc./'.$cdom.'/'.$cnum) {
                   6643:                                 if ($crsenv{'internal.courseowner'} eq 
                   6644:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   6645:                                     $refused = '';
                   6646:                                 }
                   6647:                             }
                   6648:                         } elsif (($role eq 'co') && ($cnum =~ /^$match_community$/)) { 
                   6649:                             if ($env{'request.role'} eq 'co./'.$cdom.'/'.$cnum) {
                   6650:                                 if ($crsenv{'internal.courseowner'} eq 
                   6651:                                     $env{'user.name'}.':'.$env{'user.domain'}) {
                   6652:                                     $refused = '';
                   6653:                                 }
                   6654:                             }
                   6655:                         }
                   6656:                     }
                   6657:                 } elsif (($selfenroll == 1) && ($role eq 'st') && ($udom eq $env{'user.domain'}) && ($uname eq $env{'user.name'})) {
1.947     raeburn  6658:                     $refused = '';
1.1017    raeburn  6659:                 } elsif ($context eq 'requestcourses') {
1.1041    raeburn  6660:                     my @possroles = ('st','ta','ep','in','cc','co');
1.1026    raeburn  6661:                     if ((grep(/^\Q$role\E$/,@possroles)) && ($env{'user.name'} ne '' && $env{'user.domain'} ne '')) {
1.1041    raeburn  6662:                         my $wrongcc;
                   6663:                         if ($cnum =~ /^$match_community$/) {
                   6664:                             $wrongcc = 1 if ($role eq 'cc');
                   6665:                         } else {
                   6666:                             $wrongcc = 1 if ($role eq 'co');
                   6667:                         }
                   6668:                         unless ($wrongcc) {
                   6669:                             my %crsenv = &userenvironment($cdom,$cnum,('internal.courseowner'));
                   6670:                             if ($crsenv{'internal.courseowner'} eq 
                   6671:                                  $env{'user.name'}.':'.$env{'user.domain'}) {
                   6672:                                 $refused = '';
                   6673:                             }
1.1017    raeburn  6674:                         }
                   6675:                     }
                   6676:                 }
                   6677:                 if ($refused) {
1.947     raeburn  6678:                     &logthis('Refused assignrole: '.$udom.' '.$uname.' '.$url.
                   6679:                              ' '.$role.' '.$end.' '.$start.' by '.
                   6680: 	  	             $env{'user.name'}.' at '.$env{'user.domain'});
                   6681:                     return 'refused';
                   6682:                 }
1.932     raeburn  6683:             }
1.104     www      6684:         }
1.21      www      6685:         $mrole=$role;
                   6686:     }
1.620     albertel 6687:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      6688:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      6689:     if ($end) { $command.='_'.$end; }
1.21      www      6690:     if ($start) {
                   6691: 	if ($end) { 
1.81      www      6692:            $command.='_'.$start; 
1.21      www      6693:         } else {
1.81      www      6694:            $command.='_0_'.$start;
1.21      www      6695:         }
                   6696:     }
1.739     raeburn  6697:     my $origstart = $start;
                   6698:     my $origend = $end;
1.957     raeburn  6699:     my $delflag;
1.357     www      6700: # actually delete
                   6701:     if ($deleteflag) {
1.373     www      6702: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      6703: # modify command to delete the role
1.620     albertel 6704:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      6705:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 6706: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      6707: # set start and finish to negative values for userrolelog
                   6708:            $start=-1;
                   6709:            $end=-1;
1.957     raeburn  6710:            $delflag = 1;
1.357     www      6711:         }
                   6712:     }
                   6713: # send command
1.349     www      6714:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      6715: # log new user role if status is ok
1.349     www      6716:     if ($answer eq 'ok') {
1.663     raeburn  6717: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  6718: # for course roles, perform group memberships changes triggered by role change.
1.957     raeburn  6719:         &courserolelog($role,$uname,$udom,$url,$origstart,$origend,$delflag,$selfenroll,$context);
1.739     raeburn  6720:         unless ($role =~ /^gr/) {
                   6721:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
1.957     raeburn  6722:                                              $origstart,$selfenroll,$context);
1.739     raeburn  6723:         }
1.1053    raeburn  6724:         if ($role eq 'cc') {
                   6725:             &autoupdate_coowners($url,$end,$start,$uname,$udom);
                   6726:         }
1.349     www      6727:     }
                   6728:     return $answer;
1.169     harris41 6729: }
                   6730: 
1.1053    raeburn  6731: sub autoupdate_coowners {
                   6732:     my ($url,$end,$start,$uname,$udom) = @_;
                   6733:     my ($cdom,$cnum) = ($url =~ m{^/($match_domain)/($match_courseid)});
                   6734:     if (($cdom ne '') && ($cnum ne '')) {
                   6735:         my $now = time;
                   6736:         my %domdesign = &Apache::loncommon::get_domainconf($cdom);
                   6737:         if ($domdesign{$cdom.'.autoassign.co-owners'}) {
                   6738:             my %coursehash = &coursedescription($cdom.'_'.$cnum);
                   6739:             my $instcode = $coursehash{'internal.coursecode'};
                   6740:             if ($instcode ne '') {
1.1056    raeburn  6741:                 if (($start && $start <= $now) && ($end == 0) || ($end > $now)) {
                   6742:                     unless ($coursehash{'internal.courseowner'} eq $uname.':'.$udom) {
1.1053    raeburn  6743:                         my ($delcoowners,@newcoowners,$putresult,$delresult,$coowners);
1.1056    raeburn  6744:                         my ($result,$desc) = &auto_validate_instcode($cnum,$cdom,$instcode,$uname.':'.$udom);
                   6745:                         if ($result eq 'valid') {
                   6746:                             if ($coursehash{'internal.co-owners'}) {
1.1053    raeburn  6747:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   6748:                                     push(@newcoowners,$coowner);
                   6749:                                 }
                   6750:                                 unless (grep(/^\Q$uname\E:\Q$udom\E$/,@newcoowners)) {
                   6751:                                     push(@newcoowners,$uname.':'.$udom);
                   6752:                                 }
                   6753:                                 @newcoowners = sort(@newcoowners);
                   6754:                             } else {
                   6755:                                 push(@newcoowners,$uname.':'.$udom);
                   6756:                             }
                   6757:                         } else {
                   6758:                             if ($coursehash{'internal.co-owners'}) {
                   6759:                                 foreach my $coowner (split(',',$coursehash{'internal.co-owners'})) {
                   6760:                                     unless ($coowner eq $uname.':'.$udom) {
                   6761:                                         push(@newcoowners,$coowner);
                   6762:                                     }
                   6763:                                 }
                   6764:                                 unless (@newcoowners > 0) {
                   6765:                                     $delcoowners = 1;
                   6766:                                     $coowners = '';
                   6767:                                 }
                   6768:                             }
                   6769:                         }
                   6770:                         if (@newcoowners || $delcoowners) {
                   6771:                             &store_coowners($cdom,$cnum,$coursehash{'home'},
                   6772:                                             $delcoowners,@newcoowners);
                   6773:                         }
                   6774:                     }
                   6775:                 }
                   6776:             }
                   6777:         }
                   6778:     }
                   6779: }
                   6780: 
                   6781: sub store_coowners {
                   6782:     my ($cdom,$cnum,$chome,$delcoowners,@newcoowners) = @_;
                   6783:     my $cid = $cdom.'_'.$cnum;
                   6784:     my ($coowners,$delresult,$putresult);
                   6785:     if (@newcoowners) {
                   6786:         $coowners = join(',',@newcoowners);
                   6787:         my %coownershash = (
                   6788:                             'internal.co-owners' => $coowners,
                   6789:                            );
                   6790:         $putresult = &put('environment',\%coownershash,$cdom,$cnum);
                   6791:         if ($putresult eq 'ok') {
                   6792:             if ($env{'course.'.$cid.'.num'} eq $cnum) {
                   6793:                 &appenv({'course.'.$cid.'.internal.co-owners' => $coowners});
                   6794:             }
                   6795:         }
                   6796:     }
                   6797:     if ($delcoowners) {
                   6798:         $delresult = &Apache::lonnet::del('environment',['internal.co-owners'],$cdom,$cnum);
                   6799:         if ($delresult eq 'ok') {
                   6800:             if ($env{'course.'.$cid.'.internal.co-owners'}) {
                   6801:                 &Apache::lonnet::delenv('course.'.$cid.'.internal.co-owners');
                   6802:             }
                   6803:         }
                   6804:     }
                   6805:     if (($putresult eq 'ok') || ($delresult eq 'ok')) {
                   6806:         my %crsinfo =
                   6807:             &Apache::lonnet::courseiddump($cdom,'.',1,'.','.',$cnum,undef,undef,'.');
                   6808:         if (ref($crsinfo{$cid}) eq 'HASH') {
                   6809:             $crsinfo{$cid}{'co-owners'} = \@newcoowners;
                   6810:             my $cidput = &Apache::lonnet::courseidput($cdom,\%crsinfo,$chome,'notime');
                   6811:         }
                   6812:     }
                   6813: }
                   6814: 
1.169     harris41 6815: # -------------------------------------------------- Modify user authentication
1.197     www      6816: # Overrides without validation
                   6817: 
1.169     harris41 6818: sub modifyuserauth {
                   6819:     my ($udom,$uname,$umode,$upass)=@_;
                   6820:     my $uhome=&homeserver($uname,$udom);
1.197     www      6821:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   6822:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 6823:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   6824:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 6825:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   6826: 		     &escape($upass),$uhome);
1.620     albertel 6827:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      6828:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   6829:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   6830:     &log($udom,,$uname,$uhome,
1.620     albertel 6831:         'Authentication changed by '.$env{'user.domain'}.', '.
                   6832:                                      $env{'user.name'}.', '.$umode.
1.197     www      6833:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 6834:     unless ($reply eq 'ok') {
1.197     www      6835:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 6836: 	return 'error: '.$reply;
                   6837:     }   
1.170     harris41 6838:     return 'ok';
1.80      www      6839: }
                   6840: 
1.81      www      6841: # --------------------------------------------------------------- Modify a user
1.80      www      6842: 
1.81      www      6843: sub modifyuser {
1.206     matthew  6844:     my ($udom,    $uname, $uid,
                   6845:         $umode,   $upass, $first,
                   6846:         $middle,  $last,  $gene,
1.1058    raeburn  6847:         $forceid, $desiredhome, $email, $inststatus, $candelete)=@_;
1.807     albertel 6848:     $udom= &LONCAPA::clean_domain($udom);
                   6849:     $uname=&LONCAPA::clean_username($uname);
1.1059    raeburn  6850:     my $showcandelete = 'none';
                   6851:     if (ref($candelete) eq 'ARRAY') {
                   6852:         if (@{$candelete} > 0) {
                   6853:             $showcandelete = join(', ',@{$candelete});
                   6854:         }
                   6855:     }
1.81      www      6856:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      6857:              $umode.', '.$first.', '.$middle.', '.
1.1059    raeburn  6858: 	     $last.', '.$gene.'(forceid: '.$forceid.'; candelete: '.$showcandelete.')'.
1.206     matthew  6859:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   6860:                                      ' desiredhome not specified'). 
1.620     albertel 6861:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   6862:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 6863:     my $uhome=&homeserver($uname,$udom,'true');
1.1075    raeburn  6864:     my $newuser;
                   6865:     if ($uhome eq 'no_host') {
                   6866:         $newuser = 1;
                   6867:     }
1.80      www      6868: # ----------------------------------------------------------------- Create User
1.406     albertel 6869:     if (($uhome eq 'no_host') && 
                   6870: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      6871:         my $unhome='';
1.844     albertel 6872:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  6873:             $unhome = $desiredhome;
1.620     albertel 6874: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   6875: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  6876:         } else { # load balancing routine for determining $unhome
1.81      www      6877:             my $loadm=10000000;
1.841     albertel 6878: 	    my %servers = &get_servers($udom,'library');
                   6879: 	    foreach my $tryserver (keys(%servers)) {
                   6880: 		my $answer=reply('load',$tryserver);
                   6881: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   6882: 		    $loadm=$answer;
                   6883: 		    $unhome=$tryserver;
                   6884: 		}
1.80      www      6885: 	    }
                   6886:         }
                   6887:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  6888: 	    return 'error: unable to find a home server for '.$uname.
                   6889:                    ' in domain '.$udom;
1.80      www      6890:         }
                   6891:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   6892:                          &escape($upass),$unhome);
                   6893: 	unless ($reply eq 'ok') {
                   6894:             return 'error: '.$reply;
                   6895:         }   
1.230     stredwic 6896:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      6897:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  6898: 	    return 'error: unable verify users home machine.';
1.80      www      6899:         }
1.209     matthew  6900:     }   # End of creation of new user
1.80      www      6901: # ---------------------------------------------------------------------- Add ID
                   6902:     if ($uid) {
                   6903:        $uid=~tr/A-Z/a-z/;
                   6904:        my %uidhash=&idrget($udom,$uname);
1.196     www      6905:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   6906:          && (!$forceid)) {
1.80      www      6907: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  6908: 	      return 'error: user id "'.$uid.'" does not match '.
                   6909:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      6910:           }
                   6911:        } else {
                   6912: 	  &idput($udom,($uname => $uid));
                   6913:        }
                   6914:     }
                   6915: # -------------------------------------------------------------- Add names, etc
1.313     matthew  6916:     my @tmp=&get('environment',
1.899     raeburn  6917: 		   ['firstname','middlename','lastname','generation','id',
1.963     raeburn  6918:                     'permanentemail','inststatus'],
1.134     albertel 6919: 		   $udom,$uname);
1.1075    raeburn  6920:     my (%names,%oldnames);
1.313     matthew  6921:     if ($tmp[0] =~ m/^error:.*/) { 
                   6922:         %names=(); 
                   6923:     } else {
                   6924:         %names = @tmp;
1.1075    raeburn  6925:         %oldnames = %names;
1.313     matthew  6926:     }
1.388     www      6927: #
1.1058    raeburn  6928: # If name, email and/or uid are blank (e.g., because an uploaded file
                   6929: # of users did not contain them), do not overwrite existing values
                   6930: # unless field is in $candelete array ref.  
                   6931: #
                   6932: 
                   6933:     my @fields = ('firstname','middlename','lastname','generation',
                   6934:                   'permanentemail','id');
                   6935:     my %newvalues;
                   6936:     if (ref($candelete) eq 'ARRAY') {
                   6937:         foreach my $field (@fields) {
                   6938:             if (grep(/^\Q$field\E$/,@{$candelete})) {
                   6939:                 if ($field eq 'firstname') {
                   6940:                     $names{$field} = $first;
                   6941:                 } elsif ($field eq 'middlename') {
                   6942:                     $names{$field} = $middle;
                   6943:                 } elsif ($field eq 'lastname') {
                   6944:                     $names{$field} = $last;
                   6945:                 } elsif ($field eq 'generation') { 
                   6946:                     $names{$field} = $gene;
                   6947:                 } elsif ($field eq 'permanentemail') {
                   6948:                     $names{$field} = $email;
                   6949:                 } elsif ($field eq 'id') {
                   6950:                     $names{$field}  = $uid;
                   6951:                 }
                   6952:             }
                   6953:         }
                   6954:     }
1.388     www      6955:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  6956:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      6957:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  6958:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      6959:     if ($email) {
                   6960:        $email=~s/[^\w\@\.\-\,]//gs;
1.963     raeburn  6961:        if ($email=~/\@/) { $names{'permanentemail'} = $email; }
1.592     www      6962:     }
1.899     raeburn  6963:     if ($uid) { $names{'id'}  = $uid; }
1.989     raeburn  6964:     if (defined($inststatus)) {
                   6965:         $names{'inststatus'} = '';
                   6966:         my ($usertypes,$typesorder) = &retrieve_inst_usertypes($udom);
                   6967:         if (ref($usertypes) eq 'HASH') {
                   6968:             my @okstatuses; 
                   6969:             foreach my $item (split(/:/,$inststatus)) {
                   6970:                 if (defined($usertypes->{$item})) {
                   6971:                     push(@okstatuses,$item);  
                   6972:                 }
                   6973:             }
                   6974:             if (@okstatuses) {
                   6975:                 $names{'inststatus'} = join(':', map { &escape($_); } @okstatuses);
                   6976:             }
                   6977:         }
                   6978:     }
1.1075    raeburn  6979:     my $logmsg = $udom.', '.$uname.', '.$uid.', '.
1.963     raeburn  6980:                  $umode.', '.$first.', '.$middle.', '.
1.1075    raeburn  6981:                  $last.', '.$gene.', '.$email.', '.$inststatus;
1.963     raeburn  6982:     if ($env{'user.name'} ne '' && $env{'user.domain'}) {
                   6983:         $logmsg .= ' by '.$env{'user.name'}.' at '.$env{'user.domain'};
                   6984:     } else {
                   6985:         $logmsg .= ' during self creation';
                   6986:     }
1.1075    raeburn  6987:     my $changed;
                   6988:     if ($newuser) {
                   6989:         $changed = 1;
                   6990:     } else {
                   6991:         foreach my $field (@fields) {
                   6992:             if ($names{$field} ne $oldnames{$field}) {
                   6993:                 $changed = 1;
                   6994:                 last;
                   6995:             }
                   6996:         }
                   6997:     }
                   6998:     unless ($changed) {
                   6999:         $logmsg = 'No changes in user information needed for: '.$logmsg;
                   7000:         &logthis($logmsg);
                   7001:         return 'ok';
                   7002:     }
                   7003:     my $reply = &put('environment', \%names, $udom,$uname);
                   7004:     if ($reply ne 'ok') { 
                   7005:         return 'error: '.$reply;
                   7006:     }
1.1087    raeburn  7007:     if ($names{'permanentemail'} ne $oldnames{'permanentemail'}) {
                   7008:         &Apache::lonnet::devalidate_cache_new('emailscache',$uname.':'.$udom);
                   7009:     }
1.1075    raeburn  7010:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
                   7011:     &devalidate_cache_new('namescache',$uname.':'.$udom);
                   7012:     $logmsg = 'Success modifying user '.$logmsg;
1.963     raeburn  7013:     &logthis($logmsg);
1.134     albertel 7014:     return 'ok';
1.80      www      7015: }
                   7016: 
1.81      www      7017: # -------------------------------------------------------------- Modify student
1.80      www      7018: 
1.81      www      7019: sub modifystudent {
                   7020:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.957     raeburn  7021:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid,
1.990     raeburn  7022:         $selfenroll,$context,$inststatus)=@_;
1.455     albertel 7023:     if (!$cid) {
1.620     albertel 7024: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7025: 	    return 'not_in_class';
                   7026: 	}
1.80      www      7027:     }
                   7028: # --------------------------------------------------------------- Make the user
1.81      www      7029:     my $reply=&modifyuser
1.209     matthew  7030: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.990     raeburn  7031:          $desiredhome,$email,$inststatus);
1.80      www      7032:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  7033:     # This will cause &modify_student_enrollment to get the uid from the
                   7034:     # students environment
                   7035:     $uid = undef if (!$forceid);
1.455     albertel 7036:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.957     raeburn  7037: 					$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context);
1.297     matthew  7038:     return $reply;
                   7039: }
                   7040: 
                   7041: sub modify_student_enrollment {
1.957     raeburn  7042:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid,$selfenroll,$context) = @_;
1.455     albertel 7043:     my ($cdom,$cnum,$chome);
                   7044:     if (!$cid) {
1.620     albertel 7045: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 7046: 	    return 'not_in_class';
                   7047: 	}
1.620     albertel 7048: 	$cdom=$env{'course.'.$cid.'.domain'};
                   7049: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 7050:     } else {
                   7051: 	($cdom,$cnum)=split(/_/,$cid);
                   7052:     }
1.620     albertel 7053:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 7054:     if (!$chome) {
1.457     raeburn  7055: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  7056:     }
1.455     albertel 7057:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  7058:     # Make sure the user exists
1.81      www      7059:     my $uhome=&homeserver($uname,$udom);
                   7060:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7061: 	return 'error: no such user';
                   7062:     }
1.297     matthew  7063:     # Get student data if we were not given enough information
                   7064:     if (!defined($first)  || $first  eq '' || 
                   7065:         !defined($last)   || $last   eq '' || 
                   7066:         !defined($uid)    || $uid    eq '' || 
                   7067:         !defined($middle) || $middle eq '' || 
                   7068:         !defined($gene)   || $gene   eq '') {
1.294     matthew  7069:         # They did not supply us with enough data to enroll the student, so
                   7070:         # we need to pick up more information.
1.297     matthew  7071:         my %tmp = &get('environment',
1.294     matthew  7072:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  7073:                        ,$udom,$uname);
                   7074: 
1.800     albertel 7075:         #foreach my $key (keys(%tmp)) {
                   7076:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 7077:         #}
1.294     matthew  7078:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   7079:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   7080:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  7081:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  7082:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   7083:     }
1.556     albertel 7084:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 7085:     my $reply=cput('classlist',
                   7086: 		   {"$uname:$udom" => 
1.515     raeburn  7087: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 7088: 		   $cdom,$cnum);
1.81      www      7089:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   7090: 	return 'error: '.$reply;
1.652     albertel 7091:     } else {
                   7092: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      7093:     }
1.297     matthew  7094:     # Add student role to user
1.83      www      7095:     my $uurl='/'.$cid;
1.81      www      7096:     $uurl=~s/\_/\//g;
                   7097:     if ($usec) {
                   7098: 	$uurl.='/'.$usec;
                   7099:     }
1.957     raeburn  7100:     return &assignrole($udom,$uname,$uurl,'st',$end,$start,undef,$selfenroll,$context);
1.21      www      7101: }
                   7102: 
1.556     albertel 7103: sub format_name {
                   7104:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   7105:     my $name;
                   7106:     if ($first ne 'lastname') {
                   7107: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   7108:     } else {
                   7109: 	if ($lastname=~/\S/) {
                   7110: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   7111: 	    $name=~s/\s+,/,/;
                   7112: 	} else {
                   7113: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   7114: 	}
                   7115:     }
                   7116:     $name=~s/^\s+//;
                   7117:     $name=~s/\s+$//;
                   7118:     $name=~s/\s+/ /g;
                   7119:     return $name;
                   7120: }
                   7121: 
1.84      www      7122: # ------------------------------------------------- Write to course preferences
                   7123: 
                   7124: sub writecoursepref {
                   7125:     my ($courseid,%prefs)=@_;
                   7126:     $courseid=~s/^\///;
                   7127:     $courseid=~s/\_/\//g;
                   7128:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   7129:     my $chome=homeserver($cnum,$cdomain);
                   7130:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   7131: 	return 'error: no such course';
                   7132:     }
                   7133:     my $cstring='';
1.800     albertel 7134:     foreach my $pref (keys(%prefs)) {
                   7135: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 7136:     }
1.84      www      7137:     $cstring=~s/\&$//;
                   7138:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   7139: }
                   7140: 
                   7141: # ---------------------------------------------------------- Make/modify course
                   7142: 
                   7143: sub createcourse {
1.741     raeburn  7144:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
1.1017    raeburn  7145:         $course_owner,$crstype,$cnum,$context,$category)=@_;
1.84      www      7146:     $url=&declutter($url);
                   7147:     my $cid='';
1.1028    raeburn  7148:     if ($context eq 'requestcourses') {
                   7149:         my $can_create = 0;
                   7150:         my ($ownername,$ownerdom) = split(':',$course_owner);
                   7151:         if ($udom eq $ownerdom) {
                   7152:             if (&usertools_access($ownername,$ownerdom,$category,undef,
                   7153:                                   $context)) {
                   7154:                 $can_create = 1;
                   7155:             }
                   7156:         } else {
                   7157:             my %userenv = &userenvironment($ownerdom,$ownername,'reqcrsotherdom.'.
                   7158:                                            $category);
                   7159:             if ($userenv{'reqcrsotherdom.'.$category} ne '') {
                   7160:                 my @curr = split(',',$userenv{'reqcrsotherdom.'.$category});
                   7161:                 if (@curr > 0) {
                   7162:                     my @options = qw(approval validate autolimit);
                   7163:                     my $optregex = join('|',@options);
                   7164:                     if (grep(/^\Q$udom\E:($optregex)(=?\d*)$/,@curr)) {
                   7165:                         $can_create = 1;
                   7166:                     }
                   7167:                 }
                   7168:             }
                   7169:         }
                   7170:         if ($can_create) {
                   7171:             unless ($ownername eq $env{'user.name'} && $ownerdom eq $env{'user.domain'}) {
                   7172:                 unless (&allowed('ccc',$udom)) {
                   7173:                     return 'refused'; 
                   7174:                 }
1.1017    raeburn  7175:             }
                   7176:         } else {
                   7177:             return 'refused';
                   7178:         }
1.1028    raeburn  7179:     } elsif (!&allowed('ccc',$udom)) {
                   7180:         return 'refused';
1.84      www      7181:     }
1.1011    raeburn  7182: # --------------------------------------------------------------- Get Unique ID
                   7183:     my $uname;
                   7184:     if ($cnum =~ /^$match_courseid$/) {
                   7185:         my $chome=&homeserver($cnum,$udom,'true');
                   7186:         if (($chome eq '') || ($chome eq 'no_host')) {
                   7187:             $uname = $cnum;
                   7188:         } else {
1.1038    raeburn  7189:             $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7190:         }
                   7191:     } else {
1.1038    raeburn  7192:         $uname = &generate_coursenum($udom,$crstype);
1.1011    raeburn  7193:     }
                   7194:     return $uname if ($uname =~ /^error/);
                   7195: # -------------------------------------------------- Check supplied server name
1.1052    raeburn  7196:     if (!defined($course_server)) {
                   7197:         if (defined(&domain($udom,'primary'))) {
                   7198:             $course_server = &domain($udom,'primary');
                   7199:         } else {
                   7200:             $course_server = $env{'user.home'}; 
                   7201:         }
                   7202:     }
                   7203:     my %host_servers =
                   7204:         &Apache::lonnet::get_servers($udom,'library');
                   7205:     unless ($host_servers{$course_server}) {
                   7206:         return 'error: invalid home server for course: '.$course_server;
1.264     matthew  7207:     }
1.84      www      7208: # ------------------------------------------------------------- Make the course
                   7209:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  7210:                       $course_server);
1.84      www      7211:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.1011    raeburn  7212:     my $uhome=&homeserver($uname,$udom,'true');
1.84      www      7213:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   7214: 	return 'error: no such course';
                   7215:     }
1.271     www      7216: # ----------------------------------------------------------------- Course made
1.516     raeburn  7217: # log existence
1.1029    raeburn  7218:     my $now = time;
1.918     raeburn  7219:     my $newcourse = {
                   7220:                     $udom.'_'.$uname => {
1.921     raeburn  7221:                                      description => $description,
                   7222:                                      inst_code   => $inst_code,
                   7223:                                      owner       => $course_owner,
                   7224:                                      type        => $crstype,
1.1029    raeburn  7225:                                      creator     => $env{'user.name'}.':'.
                   7226:                                                     $env{'user.domain'},
                   7227:                                      created     => $now,
                   7228:                                      context     => $context,
1.918     raeburn  7229:                                                 },
                   7230:                     };
1.921     raeburn  7231:     &courseidput($udom,$newcourse,$uhome,'notime');
1.358     www      7232: # set toplevel url
1.271     www      7233:     my $topurl=$url;
                   7234:     unless ($nonstandard) {
                   7235: # ------------------------------------------ For standard courses, make top url
                   7236:         my $mapurl=&clutter($url);
1.278     www      7237:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 7238:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      7239: <map>
                   7240: <resource id="1" type="start"></resource>
                   7241: <resource id="2" src="$mapurl"></resource>
                   7242: <resource id="3" type="finish"></resource>
                   7243: <link index="1" from="1" to="2"></link>
                   7244: <link index="2" from="2" to="3"></link>
                   7245: </map>
                   7246: ENDINITMAP
                   7247:         $topurl=&declutter(
1.638     albertel 7248:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      7249:                           );
                   7250:     }
                   7251: # ----------------------------------------------------------- Write preferences
1.84      www      7252:     &writecoursepref($udom.'_'.$uname,
1.1056    raeburn  7253:                      ('description'              => $description,
                   7254:                       'url'                      => $topurl,
                   7255:                       'internal.creator'         => $env{'user.name'}.':'.
                   7256:                                                     $env{'user.domain'},
                   7257:                       'internal.created'         => $now,
                   7258:                       'internal.creationcontext' => $context)
                   7259:                     );
1.84      www      7260:     return '/'.$udom.'/'.$uname;
                   7261: }
                   7262: 
1.1011    raeburn  7263: # ------------------------------------------------------------------- Create ID
                   7264: sub generate_coursenum {
1.1038    raeburn  7265:     my ($udom,$crstype) = @_;
1.1011    raeburn  7266:     my $domdesc = &domain($udom);
                   7267:     return 'error: invalid domain' if ($domdesc eq '');
1.1038    raeburn  7268:     my $first;
                   7269:     if ($crstype eq 'Community') {
                   7270:         $first = '0';
                   7271:     } else {
                   7272:         $first = int(1+rand(9)); 
                   7273:     } 
                   7274:     my $uname=$first.
1.1011    raeburn  7275:         ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7276:         substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7277:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7278: # ----------------------------------------------- Make sure that does not exist
                   7279:     my $uhome=&homeserver($uname,$udom,'true');
                   7280:     unless (($uhome eq '') || ($uhome eq 'no_host')) {
1.1038    raeburn  7281:         if ($crstype eq 'Community') {
                   7282:             $first = '0';
                   7283:         } else {
                   7284:             $first = int(1+rand(9));
                   7285:         }
                   7286:         $uname=$first.
1.1011    raeburn  7287:                ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   7288:                substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   7289:                unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   7290:         $uhome=&homeserver($uname,$udom,'true');
                   7291:         unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   7292:             return 'error: unable to generate unique course-ID';
                   7293:         }
                   7294:     }
                   7295:     return $uname;
                   7296: }
                   7297: 
1.813     albertel 7298: sub is_course {
                   7299:     my ($cdom,$cnum) = @_;
                   7300:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
1.946     raeburn  7301: 				undef,'.');
1.813     albertel 7302:     if (exists($courses{$cdom.'_'.$cnum})) {
                   7303:         return 1;
                   7304:     }
                   7305:     return 0;
                   7306: }
                   7307: 
1.1015    raeburn  7308: sub store_userdata {
                   7309:     my ($storehash,$datakey,$namespace,$udom,$uname) = @_;
1.1013    raeburn  7310:     my $result;
1.1016    raeburn  7311:     if ($datakey ne '') {
1.1013    raeburn  7312:         if (ref($storehash) eq 'HASH') {
1.1017    raeburn  7313:             if ($udom eq '' || $uname eq '') {
                   7314:                 $udom = $env{'user.domain'};
                   7315:                 $uname = $env{'user.name'};
                   7316:             }
                   7317:             my $uhome=&homeserver($uname,$udom);
1.1013    raeburn  7318:             if (($uhome eq '') || ($uhome eq 'no_host')) {
                   7319:                 $result = 'error: no_host';
                   7320:             } else {
                   7321:                 $storehash->{'ip'} = $ENV{'REMOTE_ADDR'};
                   7322:                 $storehash->{'host'} = $perlvar{'lonHostID'};
                   7323: 
                   7324:                 my $namevalue='';
                   7325:                 foreach my $key (keys(%{$storehash})) {
                   7326:                     $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   7327:                 }
                   7328:                 $namevalue=~s/\&$//;
1.1105    raeburn  7329:                 $result =  &reply("store:$udom:$uname:$namespace:$datakey:".
                   7330:                                   $namevalue,$uhome);
1.1013    raeburn  7331:             }
                   7332:         } else {
                   7333:             $result = 'error: data to store was not a hash reference'; 
                   7334:         }
                   7335:     } else {
                   7336:         $result= 'error: invalid requestkey'; 
                   7337:     }
                   7338:     return $result;
                   7339: }
                   7340: 
1.21      www      7341: # ---------------------------------------------------------- Assign Custom Role
                   7342: 
                   7343: sub assigncustomrole {
1.957     raeburn  7344:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7345:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.957     raeburn  7346:                        $end,$start,$deleteflag,$selfenroll,$context);
1.21      www      7347: }
                   7348: 
                   7349: # ----------------------------------------------------------------- Revoke Role
                   7350: 
                   7351: sub revokerole {
1.957     raeburn  7352:     my ($udom,$uname,$url,$role,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7353:     my $now=time;
1.965     raeburn  7354:     return &assignrole($udom,$uname,$url,$role,$now,undef,$deleteflag,$selfenroll,$context);
1.21      www      7355: }
                   7356: 
                   7357: # ---------------------------------------------------------- Revoke Custom Role
                   7358: 
                   7359: sub revokecustomrole {
1.957     raeburn  7360:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag,$selfenroll,$context)=@_;
1.21      www      7361:     my $now=time;
1.357     www      7362:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
1.957     raeburn  7363:            $deleteflag,$selfenroll,$context);
1.17      www      7364: }
                   7365: 
1.533     banghart 7366: # ------------------------------------------------------------ Disk usage
1.535     albertel 7367: sub diskusage {
1.955     raeburn  7368:     my ($udom,$uname,$directorypath,$getpropath)=@_;
                   7369:     $directorypath =~ s/\/$//;
                   7370:     my $listing=&reply('du2:'.&escape($directorypath).':'
                   7371:                        .&escape($getpropath).':'.&escape($uname).':'
                   7372:                        .&escape($udom),homeserver($uname,$udom));
                   7373:     if ($listing eq 'unknown_cmd') {
                   7374:         if ($getpropath) {
                   7375:             $directorypath = &propath($udom,$uname).'/'.$directorypath; 
                   7376:         }
                   7377:         $listing = &reply('du:'.$directorypath,homeserver($uname,$udom));
                   7378:     }
1.514     albertel 7379:     return $listing;
1.512     banghart 7380: }
                   7381: 
1.566     banghart 7382: sub is_locked {
1.1096    raeburn  7383:     my ($file_name, $domain, $user, $which) = @_;
1.566     banghart 7384:     my @check;
                   7385:     my $is_locked;
1.1093    raeburn  7386:     push (@check,$file_name);
1.613     albertel 7387:     my %locked = &get('file_permissions',\@check,
1.620     albertel 7388: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 7389:     my ($tmp)=keys(%locked);
                   7390:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  7391:     
1.566     banghart 7392:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  7393:         $is_locked = 'false';
                   7394:         foreach my $entry (@{$locked{$file_name}}) {
1.1096    raeburn  7395:            if (ref($entry) eq 'ARRAY') {
1.746     raeburn  7396:                $is_locked = 'true';
1.1096    raeburn  7397:                if (ref($which) eq 'ARRAY') {
                   7398:                    push(@{$which},$entry);
                   7399:                } else {
                   7400:                    last;
                   7401:                }
1.745     raeburn  7402:            }
                   7403:        }
1.566     banghart 7404:     } else {
                   7405:         $is_locked = 'false';
                   7406:     }
1.1093    raeburn  7407:     return $is_locked;
1.566     banghart 7408: }
                   7409: 
1.759     albertel 7410: sub declutter_portfile {
                   7411:     my ($file) = @_;
1.833     albertel 7412:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 7413:     return $file;
                   7414: }
                   7415: 
1.559     banghart 7416: # ------------------------------------------------------------- Mark as Read Only
                   7417: 
                   7418: sub mark_as_readonly {
                   7419:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 7420:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7421:     my ($tmp)=keys(%current_permissions);
                   7422:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 7423:     foreach my $file (@{$files}) {
1.759     albertel 7424: 	$file = &declutter_portfile($file);
1.561     banghart 7425:         push(@{$current_permissions{$file}},$what);
1.559     banghart 7426:     }
1.613     albertel 7427:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 7428:     return;
                   7429: }
                   7430: 
1.572     banghart 7431: # ------------------------------------------------------------Save Selected Files
                   7432: 
                   7433: sub save_selected_files {
                   7434:     my ($user, $path, @files) = @_;
                   7435:     my $filename = $user."savedfiles";
1.573     banghart 7436:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 7437:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 7438:     foreach my $file (@files) {
1.620     albertel 7439:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 7440:     }
                   7441:     foreach my $file (@other_files) {
1.574     banghart 7442:         print (OUT $file."\n");
1.572     banghart 7443:     }
1.574     banghart 7444:     close (OUT);
1.572     banghart 7445:     return 'ok';
                   7446: }
                   7447: 
1.574     banghart 7448: sub clear_selected_files {
                   7449:     my ($user) = @_;
                   7450:     my $filename = $user."savedfiles";
                   7451:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   7452:     print (OUT undef);
                   7453:     close (OUT);
                   7454:     return ("ok");    
                   7455: }
                   7456: 
1.572     banghart 7457: sub files_in_path {
                   7458:     my ($user, $path) = @_;
                   7459:     my $filename = $user."savedfiles";
                   7460:     my %return_files;
1.574     banghart 7461:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 7462:     while (my $line_in = <IN>) {
1.574     banghart 7463:         chomp ($line_in);
                   7464:         my @paths_and_file = split (m!/!, $line_in);
                   7465:         my $file_part = pop (@paths_and_file);
                   7466:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 7467:         $path_part.='/';
                   7468:         my $path_and_file = $path_part.$file_part;
                   7469:         if ($path_part eq $path) {
                   7470:             $return_files{$file_part}= 'selected';
                   7471:         }
                   7472:     }
1.574     banghart 7473:     close (IN);
                   7474:     return (\%return_files);
1.572     banghart 7475: }
                   7476: 
                   7477: # called in portfolio select mode, to show files selected NOT in current directory
                   7478: sub files_not_in_path {
                   7479:     my ($user, $path) = @_;
                   7480:     my $filename = $user."savedfiles";
                   7481:     my @return_files;
                   7482:     my $path_part;
1.800     albertel 7483:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   7484:     while (my $line = <IN>) {
1.572     banghart 7485:         #ok, I know it's clunky, but I want it to work
1.800     albertel 7486:         my @paths_and_file = split(m|/|, $line);
                   7487:         my $file_part = pop(@paths_and_file);
                   7488:         chomp($file_part);
                   7489:         my $path_part = join('/', @paths_and_file);
1.572     banghart 7490:         $path_part .= '/';
                   7491:         my $path_and_file = $path_part.$file_part;
                   7492:         if ($path_part ne $path) {
1.800     albertel 7493:             push(@return_files, ($path_and_file));
1.572     banghart 7494:         }
                   7495:     }
1.800     albertel 7496:     close(OUT);
1.574     banghart 7497:     return (@return_files);
1.572     banghart 7498: }
                   7499: 
1.745     raeburn  7500: #----------------------------------------------Get portfolio file permissions
1.629     banghart 7501: 
1.745     raeburn  7502: sub get_portfile_permissions {
                   7503:     my ($domain,$user) = @_;
1.613     albertel 7504:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 7505:     my ($tmp)=keys(%current_permissions);
                   7506:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  7507:     return \%current_permissions;
                   7508: }
                   7509: 
                   7510: #---------------------------------------------Get portfolio file access controls
                   7511: 
1.749     raeburn  7512: sub get_access_controls {
1.745     raeburn  7513:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 7514:     my %access;
                   7515:     my $real_file = $file;
                   7516:     $file =~ s/\.meta$//;
1.745     raeburn  7517:     if (defined($file)) {
1.749     raeburn  7518:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   7519:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 7520:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  7521:             }
                   7522:         }
1.745     raeburn  7523:     } else {
1.749     raeburn  7524:         foreach my $key (keys(%{$current_permissions})) {
                   7525:             if ($key =~ /\0accesscontrol$/) {
                   7526:                 if (defined($group)) {
                   7527:                     if ($key !~ m-^\Q$group\E/-) {
                   7528:                         next;
                   7529:                     }
                   7530:                 }
                   7531:                 my ($fullpath) = split(/\0/,$key);
                   7532:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   7533:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   7534:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   7535:                     }
                   7536:                 }
                   7537:             }
                   7538:         }
                   7539:     }
                   7540:     return %access;
                   7541: }
                   7542: 
                   7543: sub modify_access_controls {
                   7544:     my ($file_name,$changes,$domain,$user)=@_;
                   7545:     my ($outcome,$deloutcome);
                   7546:     my %store_permissions;
                   7547:     my %new_values;
                   7548:     my %new_control;
                   7549:     my %translation;
                   7550:     my @deletions = ();
                   7551:     my $now = time;
                   7552:     if (exists($$changes{'activate'})) {
                   7553:         if (ref($$changes{'activate'}) eq 'HASH') {
                   7554:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   7555:             my $numnew = scalar(@newitems);
                   7556:             for (my $i=0; $i<$numnew; $i++) {
                   7557:                 my $newkey = $newitems[$i];
                   7558:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  7559:                 if ($newkey =~ /^\d+:/) { 
                   7560:                     $newkey =~ s/^(\d+)/$newid/;
                   7561:                     $translation{$1} = $newid;
                   7562:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   7563:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   7564:                     $translation{$1} = $newid;
                   7565:                 }
1.749     raeburn  7566:                 $new_values{$file_name."\0".$newkey} = 
                   7567:                                           $$changes{'activate'}{$newitems[$i]};
                   7568:                 $new_control{$newkey} = $now;
                   7569:             }
                   7570:         }
                   7571:     }
                   7572:     my %todelete;
                   7573:     my %changed_items;
                   7574:     foreach my $action ('delete','update') {
                   7575:         if (exists($$changes{$action})) {
                   7576:             if (ref($$changes{$action}) eq 'HASH') {
                   7577:                 foreach my $key (keys(%{$$changes{$action}})) {
                   7578:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   7579:                     if ($action eq 'delete') { 
                   7580:                         $todelete{$itemnum} = 1;
                   7581:                     } else {
                   7582:                         $changed_items{$itemnum} = $key;
                   7583:                     }
                   7584:                 }
1.745     raeburn  7585:             }
                   7586:         }
1.749     raeburn  7587:     }
                   7588:     # get lock on access controls for file.
                   7589:     my $lockhash = {
                   7590:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   7591:                                                        ':'.$env{'user.domain'},
                   7592:                    }; 
                   7593:     my $tries = 0;
                   7594:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   7595:    
                   7596:     while (($gotlock ne 'ok') && $tries <3) {
                   7597:         $tries ++;
                   7598:         sleep 1;
                   7599:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   7600:     }
                   7601:     if ($gotlock eq 'ok') {
                   7602:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   7603:         my ($tmp)=keys(%curr_permissions);
                   7604:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   7605:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   7606:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   7607:             if (ref($curr_controls) eq 'HASH') {
                   7608:                 foreach my $control_item (keys(%{$curr_controls})) {
                   7609:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   7610:                     if (defined($todelete{$itemnum})) {
                   7611:                         push(@deletions,$file_name."\0".$control_item);
                   7612:                     } else {
                   7613:                         if (defined($changed_items{$itemnum})) {
                   7614:                             $new_control{$changed_items{$itemnum}} = $now;
                   7615:                             push(@deletions,$file_name."\0".$control_item);
                   7616:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   7617:                         } else {
                   7618:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   7619:                         }
                   7620:                     }
1.745     raeburn  7621:                 }
                   7622:             }
                   7623:         }
1.970     raeburn  7624:         my ($group);
                   7625:         if (&is_course($domain,$user)) {
                   7626:             ($group,my $file) = split(/\//,$file_name,2);
                   7627:         }
1.749     raeburn  7628:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   7629:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   7630:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   7631:         #  remove lock
                   7632:         my @del_lock = ($file_name."\0".'locked_access_records');
                   7633:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  7634:         my $sqlresult =
1.970     raeburn  7635:             &update_portfolio_table($user,$domain,$file_name,'portfolio_access',
1.818     raeburn  7636:                                     $group);
1.749     raeburn  7637:     } else {
                   7638:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  7639:     }
1.749     raeburn  7640:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  7641: }
                   7642: 
1.827     raeburn  7643: sub make_public_indefinitely {
                   7644:     my ($requrl) = @_;
                   7645:     my $now = time;
                   7646:     my $action = 'activate';
                   7647:     my $aclnum = 0;
                   7648:     if (&is_portfolio_url($requrl)) {
                   7649:         my (undef,$udom,$unum,$file_name,$group) =
                   7650:             &parse_portfolio_url($requrl);
                   7651:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   7652:         my %access_controls = &get_access_controls($current_perms,
                   7653:                                                    $group,$file_name);
                   7654:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   7655:             my ($num,$scope,$end,$start) = 
                   7656:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   7657:             if ($scope eq 'public') {
                   7658:                 if ($start <= $now && $end == 0) {
                   7659:                     $action = 'none';
                   7660:                 } else {
                   7661:                     $action = 'update';
                   7662:                     $aclnum = $num;
                   7663:                 }
                   7664:                 last;
                   7665:             }
                   7666:         }
                   7667:         if ($action eq 'none') {
                   7668:              return 'ok';
                   7669:         } else {
                   7670:             my %changes;
                   7671:             my $newend = 0;
                   7672:             my $newstart = $now;
                   7673:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   7674:             $changes{$action}{$newkey} = {
                   7675:                 type => 'public',
                   7676:                 time => {
                   7677:                     start => $newstart,
                   7678:                     end   => $newend,
                   7679:                 },
                   7680:             };
                   7681:             my ($outcome,$deloutcome,$new_values,$translation) =
                   7682:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   7683:             return $outcome;
                   7684:         }
                   7685:     } else {
                   7686:         return 'invalid';
                   7687:     }
                   7688: }
                   7689: 
1.745     raeburn  7690: #------------------------------------------------------Get Marked as Read Only
                   7691: 
                   7692: sub get_marked_as_readonly {
                   7693:     my ($domain,$user,$what,$group) = @_;
                   7694:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 7695:     my @readonly_files;
1.629     banghart 7696:     my $cmp1=$what;
                   7697:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  7698:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   7699:         if (defined($group)) {
                   7700:             if ($file_name !~ m-^\Q$group\E/-) {
                   7701:                 next;
                   7702:             }
                   7703:         }
1.561     banghart 7704:         if (ref($value) eq "ARRAY"){
                   7705:             foreach my $stored_what (@{$value}) {
1.629     banghart 7706:                 my $cmp2=$stored_what;
1.759     albertel 7707:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  7708:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  7709:                 }
1.629     banghart 7710:                 if ($cmp1 eq $cmp2) {
1.561     banghart 7711:                     push(@readonly_files, $file_name);
1.745     raeburn  7712:                     last;
1.563     banghart 7713:                 } elsif (!defined($what)) {
                   7714:                     push(@readonly_files, $file_name);
1.745     raeburn  7715:                     last;
1.561     banghart 7716:                 }
                   7717:             }
1.745     raeburn  7718:         }
1.561     banghart 7719:     }
                   7720:     return @readonly_files;
                   7721: }
1.577     banghart 7722: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 7723: 
1.577     banghart 7724: sub get_marked_as_readonly_hash {
1.745     raeburn  7725:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 7726:     my %readonly_files;
1.745     raeburn  7727:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   7728:         if (defined($group)) {
                   7729:             if ($file_name !~ m-^\Q$group\E/-) {
                   7730:                 next;
                   7731:             }
                   7732:         }
1.577     banghart 7733:         if (ref($value) eq "ARRAY"){
                   7734:             foreach my $stored_what (@{$value}) {
1.745     raeburn  7735:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 7736:                     foreach my $lock_descriptor(@{$stored_what}) {
                   7737:                         if ($lock_descriptor eq 'graded') {
                   7738:                             $readonly_files{$file_name} = 'graded';
                   7739:                         } elsif ($lock_descriptor eq 'handback') {
                   7740:                             $readonly_files{$file_name} = 'handback';
                   7741:                         } else {
                   7742:                             if (!exists($readonly_files{$file_name})) {
                   7743:                                 $readonly_files{$file_name} = 'locked';
                   7744:                             }
                   7745:                         }
1.745     raeburn  7746:                     }
1.750     banghart 7747:                 } 
1.577     banghart 7748:             }
                   7749:         } 
                   7750:     }
                   7751:     return %readonly_files;
                   7752: }
1.559     banghart 7753: # ------------------------------------------------------------ Unmark as Read Only
                   7754: 
                   7755: sub unmark_as_readonly {
1.629     banghart 7756:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   7757:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  7758:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 7759:     $file_name = &declutter_portfile($file_name);
1.634     albertel 7760:     my $symb_crs = $what;
                   7761:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  7762:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 7763:     my ($tmp)=keys(%current_permissions);
                   7764:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  7765:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 7766:     foreach my $file (@readonly_files) {
1.759     albertel 7767: 	my $clean_file = &declutter_portfile($file);
                   7768: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 7769: 	my $current_locks = $current_permissions{$file};
1.563     banghart 7770:         my @new_locks;
                   7771:         my @del_keys;
                   7772:         if (ref($current_locks) eq "ARRAY"){
                   7773:             foreach my $locker (@{$current_locks}) {
1.632     albertel 7774:                 my $compare=$locker;
1.749     raeburn  7775:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  7776:                     $compare=join('',@{$locker});
1.746     raeburn  7777:                     if ($compare ne $symb_crs) {
                   7778:                         push(@new_locks, $locker);
                   7779:                     }
1.563     banghart 7780:                 }
                   7781:             }
1.650     albertel 7782:             if (scalar(@new_locks) > 0) {
1.563     banghart 7783:                 $current_permissions{$file} = \@new_locks;
                   7784:             } else {
                   7785:                 push(@del_keys, $file);
1.613     albertel 7786:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 7787:                 delete($current_permissions{$file});
1.563     banghart 7788:             }
                   7789:         }
1.561     banghart 7790:     }
1.613     albertel 7791:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 7792:     return;
                   7793: }
1.512     banghart 7794: 
1.17      www      7795: # ------------------------------------------------------------ Directory lister
                   7796: 
                   7797: sub dirlist {
1.955     raeburn  7798:     my ($uri,$userdomain,$username,$getpropath,$getuserdir,$alternateRoot)=@_;
1.18      www      7799:     $uri=~s/^\///;
                   7800:     $uri=~s/\/$//;
1.253     stredwic 7801:     my ($udom, $uname);
1.955     raeburn  7802:     if ($getuserdir) {
1.253     stredwic 7803:         $udom = $userdomain;
                   7804:         $uname = $username;
1.955     raeburn  7805:     } else {
                   7806:         (undef,$udom,$uname)=split(/\//,$uri);
                   7807:         if(defined($userdomain)) {
                   7808:             $udom = $userdomain;
                   7809:         }
                   7810:         if(defined($username)) {
                   7811:             $uname = $username;
                   7812:         }
1.253     stredwic 7813:     }
1.955     raeburn  7814:     my ($dirRoot,$listing,@listing_results);
1.253     stredwic 7815: 
1.955     raeburn  7816:     $dirRoot = $perlvar{'lonDocRoot'};
                   7817:     if (defined($getpropath)) {
                   7818:         $dirRoot = &propath($udom,$uname);
1.253     stredwic 7819:         $dirRoot =~ s/\/$//;
1.955     raeburn  7820:     } elsif (defined($getuserdir)) {
                   7821:         my $subdir=$uname.'__';
                   7822:         $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   7823:         $dirRoot = $Apache::lonnet::perlvar{'lonUsersDir'}
                   7824:                    ."/$udom/$subdir/$uname";
                   7825:     } elsif (defined($alternateRoot)) {
                   7826:         $dirRoot = $alternateRoot;
1.751     banghart 7827:     }
1.253     stredwic 7828: 
                   7829:     if($udom) {
                   7830:         if($uname) {
1.955     raeburn  7831:             $listing = &reply('ls3:'.&escape('/'.$uri).':'.$getpropath.':'
1.956     raeburn  7832:                               .$getuserdir.':'.&escape($dirRoot)
1.955     raeburn  7833:                               .':'.&escape($uname).':'.&escape($udom),
                   7834:                               &homeserver($uname,$udom));
                   7835:             if ($listing eq 'unknown_cmd') {
                   7836:                 $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   7837:                                   &homeserver($uname,$udom));
                   7838:             } else {
                   7839:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   7840:             }
1.605     matthew  7841:             if ($listing eq 'unknown_cmd') {
1.800     albertel 7842:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   7843: 				  &homeserver($uname,$udom));
1.605     matthew  7844:                 @listing_results = split(/:/,$listing);
                   7845:             } else {
                   7846:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   7847:             }
                   7848:             return @listing_results;
1.955     raeburn  7849:         } elsif(!$alternateRoot) {
1.800     albertel 7850:             my %allusers;
1.841     albertel 7851: 	    my %servers = &get_servers($udom,'library');
1.955     raeburn  7852:  	    foreach my $tryserver (keys(%servers)) {
                   7853:                 $listing = &reply('ls3:'.&escape("/res/$udom").':::::'.
                   7854:                                   &escape($udom),$tryserver);
                   7855:                 if ($listing eq 'unknown_cmd') {
                   7856: 		    $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   7857: 				      $udom, $tryserver);
                   7858:                 } else {
                   7859:                     @listing_results = map { &unescape($_); } split(/:/,$listing);
                   7860:                 }
1.841     albertel 7861: 		if ($listing eq 'unknown_cmd') {
                   7862: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   7863: 				      $udom, $tryserver);
                   7864: 		    @listing_results = split(/:/,$listing);
                   7865: 		} else {
                   7866: 		    @listing_results =
                   7867: 			map { &unescape($_); } split(/:/,$listing);
                   7868: 		}
                   7869: 		if ($listing_results[0] ne 'no_such_dir' && 
                   7870: 		    $listing_results[0] ne 'empty'       &&
                   7871: 		    $listing_results[0] ne 'con_lost') {
                   7872: 		    foreach my $line (@listing_results) {
                   7873: 			my ($entry) = split(/&/,$line,2);
                   7874: 			$allusers{$entry} = 1;
                   7875: 		    }
                   7876: 		}
1.253     stredwic 7877:             }
                   7878:             my $alluserstr='';
1.800     albertel 7879:             foreach my $user (sort(keys(%allusers))) {
                   7880:                 $alluserstr.=$user.'&user:';
1.253     stredwic 7881:             }
                   7882:             $alluserstr=~s/:$//;
                   7883:             return split(/:/,$alluserstr);
                   7884:         } else {
1.800     albertel 7885:             return ('missing user name');
1.253     stredwic 7886:         }
1.955     raeburn  7887:     } elsif(!defined($getpropath)) {
1.841     albertel 7888:         my @all_domains = sort(&all_domains());
1.955     raeburn  7889:         foreach my $domain (@all_domains) {
                   7890:             $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   7891:         }
                   7892:         return @all_domains;
                   7893:     } else {
1.800     albertel 7894:         return ('missing domain');
1.275     stredwic 7895:     }
                   7896: }
                   7897: 
                   7898: # --------------------------------------------- GetFileTimestamp
                   7899: # This function utilizes dirlist and returns the date stamp for
                   7900: # when it was last modified.  It will also return an error of -1
                   7901: # if an error occurs
                   7902: 
                   7903: sub GetFileTimestamp {
1.955     raeburn  7904:     my ($studentDomain,$studentName,$filename,$getuserdir)=@_;
1.807     albertel 7905:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   7906:     $studentName   = &LONCAPA::clean_username($studentName);
1.955     raeburn  7907:     my ($fileStat) = 
                   7908:         &Apache::lonnet::dirlist($filename,$studentDomain,$studentName, 
                   7909:                                  undef,$getuserdir);
1.275     stredwic 7910:     my @stats = split('&', $fileStat);
                   7911:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  7912:         # @stats contains first the filename, then the stat output
                   7913:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 7914:     } else {
                   7915:         return -1;
1.253     stredwic 7916:     }
1.26      www      7917: }
                   7918: 
1.712     albertel 7919: sub stat_file {
                   7920:     my ($uri) = @_;
1.787     albertel 7921:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 7922: 
1.955     raeburn  7923:     my ($udom,$uname,$file);
1.712     albertel 7924:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   7925: 	($udom,$uname,$file) =
1.811     albertel 7926: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 7927: 	$file = 'userfiles/'.$file;
                   7928:     }
                   7929:     if ($uri =~ m-^/res/-) {
                   7930: 	($udom,$uname) = 
1.807     albertel 7931: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 7932: 	$file = $uri;
                   7933:     }
                   7934: 
                   7935:     if (!$udom || !$uname || !$file) {
                   7936: 	# unable to handle the uri
                   7937: 	return ();
                   7938:     }
1.956     raeburn  7939:     my $getpropath;
                   7940:     if ($file =~ /^userfiles\//) {
                   7941:         $getpropath = 1;
                   7942:     }
1.955     raeburn  7943:     my ($result) = &dirlist($file,$udom,$uname,$getpropath);
1.712     albertel 7944:     my @stats = split('&', $result);
1.721     banghart 7945:     
1.712     albertel 7946:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   7947: 	shift(@stats); #filename is first
                   7948: 	return @stats;
                   7949:     }
                   7950:     return ();
                   7951: }
                   7952: 
1.26      www      7953: # -------------------------------------------------------- Value of a Condition
                   7954: 
1.713     albertel 7955: # gets the value of a specific preevaluated condition
                   7956: #    stored in the string  $env{user.state.<cid>}
                   7957: # or looks up a condition reference in the bighash and if if hasn't
                   7958: # already been evaluated recurses into docondval to get the value of
                   7959: # the condition, then memoizing it to 
                   7960: #   $env{user.state.<cid>.<condition>}
1.40      www      7961: sub directcondval {
                   7962:     my $number=shift;
1.620     albertel 7963:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 7964: 	&Apache::lonuserstate::evalstate();
                   7965:     }
1.713     albertel 7966:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   7967: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   7968:     } elsif ($number =~ /^_/) {
                   7969: 	my $sub_condition;
                   7970: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   7971: 		&GDBM_READER(),0640)) {
                   7972: 	    $sub_condition=$bighash{'conditions'.$number};
                   7973: 	    untie(%bighash);
                   7974: 	}
                   7975: 	my $value = &docondval($sub_condition);
1.949     raeburn  7976: 	&appenv({'user.state.'.$env{'request.course.id'}.".$number" => $value});
1.713     albertel 7977: 	return $value;
                   7978:     }
1.620     albertel 7979:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   7980:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      7981:     } else {
                   7982:        return 2;
                   7983:     }
                   7984: }
                   7985: 
1.713     albertel 7986: # get the collection of conditions for this resource
1.26      www      7987: sub condval {
                   7988:     my $condidx=shift;
1.54      www      7989:     my $allpathcond='';
1.713     albertel 7990:     foreach my $cond (split(/\|/,$condidx)) {
                   7991: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   7992: 	    $allpathcond.=
                   7993: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   7994: 	}
1.191     harris41 7995:     }
1.54      www      7996:     $allpathcond=~s/\|$//;
1.713     albertel 7997:     return &docondval($allpathcond);
                   7998: }
                   7999: 
                   8000: #evaluates an expression of conditions
                   8001: sub docondval {
                   8002:     my ($allpathcond) = @_;
                   8003:     my $result=0;
                   8004:     if ($env{'request.course.id'}
                   8005: 	&& defined($allpathcond)) {
                   8006: 	my $operand='|';
                   8007: 	my @stack;
                   8008: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   8009: 	    if ($chunk eq '(') {
                   8010: 		push @stack,($operand,$result);
                   8011: 	    } elsif ($chunk eq ')') {
                   8012: 		my $before=pop @stack;
                   8013: 		if (pop @stack eq '&') {
                   8014: 		    $result=$result>$before?$before:$result;
                   8015: 		} else {
                   8016: 		    $result=$result>$before?$result:$before;
                   8017: 		}
                   8018: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   8019: 		$operand=$chunk;
                   8020: 	    } else {
                   8021: 		my $new=directcondval($chunk);
                   8022: 		if ($operand eq '&') {
                   8023: 		    $result=$result>$new?$new:$result;
                   8024: 		} else {
                   8025: 		    $result=$result>$new?$result:$new;
                   8026: 		}
                   8027: 	    }
                   8028: 	}
1.26      www      8029:     }
                   8030:     return $result;
1.421     albertel 8031: }
                   8032: 
                   8033: # ---------------------------------------------------- Devalidate courseresdata
                   8034: 
                   8035: sub devalidatecourseresdata {
                   8036:     my ($coursenum,$coursedomain)=@_;
                   8037:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8038:     &devalidate_cache_new('courseres',$hashid);
1.28      www      8039: }
                   8040: 
1.763     www      8041: 
1.200     www      8042: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     8043: #
                   8044: #  Parameters:
                   8045: #      $coursenum    - Number of the course.
                   8046: #      $coursedomain - Domain at which the course was created.
                   8047: #  Returns:
                   8048: #     A hash of the course parameters along (I think) with timestamps
                   8049: #     and version info.
1.877     foxr     8050: 
1.624     albertel 8051: sub get_courseresdata {
                   8052:     my ($coursenum,$coursedomain)=@_;
1.200     www      8053:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   8054:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 8055:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 8056:     my %dumpreply;
1.417     albertel 8057:     unless (defined($cached)) {
1.624     albertel 8058: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 8059: 	$result=\%dumpreply;
1.251     albertel 8060: 	my ($tmp) = keys(%dumpreply);
                   8061: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 8062: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 8063: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   8064: 	    return $tmp;
1.416     albertel 8065: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 8066: 	    $result=undef;
1.599     albertel 8067: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 8068: 	}
                   8069:     }
1.624     albertel 8070:     return $result;
                   8071: }
                   8072: 
1.633     albertel 8073: sub devalidateuserresdata {
                   8074:     my ($uname,$udom)=@_;
                   8075:     my $hashid="$udom:$uname";
                   8076:     &devalidate_cache_new('userres',$hashid);
                   8077: }
                   8078: 
1.624     albertel 8079: sub get_userresdata {
                   8080:     my ($uname,$udom)=@_;
                   8081:     #most student don\'t have any data set, check if there is some data
                   8082:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   8083: 
                   8084:     my $hashid="$udom:$uname";
                   8085:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   8086:     if (!defined($cached)) {
                   8087: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   8088: 	$result=\%resourcedata;
                   8089: 	&do_cache_new('userres',$hashid,$result,600);
                   8090:     }
                   8091:     my ($tmp)=keys(%$result);
                   8092:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   8093: 	return $result;
                   8094:     }
                   8095:     #error 2 occurs when the .db doesn't exist
                   8096:     if ($tmp!~/error: 2 /) {
1.672     albertel 8097: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 8098: 		 " Trying to get resource data for ".
                   8099: 		 $uname." at ".$udom.": ".
                   8100: 		 $tmp."</font>");
                   8101:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 8102: 	#&EXT_cache_set($udom,$uname);
                   8103: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 8104: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 8105:     }
                   8106:     return $tmp;
                   8107: }
1.879     foxr     8108: #----------------------------------------------- resdata - return resource data
                   8109: #  Purpose:
                   8110: #    Return resource data for either users or for a course.
                   8111: #  Parameters:
                   8112: #     $name      - Course/user name.
                   8113: #     $domain    - Name of the domain the user/course is registered on.
                   8114: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   8115: #     @which     - Array of names of resources desired.
                   8116: #  Returns:
                   8117: #     The value of the first reasource in @which that is found in the
                   8118: #     resource hash.
                   8119: #  Exceptional Conditions:
                   8120: #     If the $type passed in is not valid (not the string 'course' or 
                   8121: #     'user', an undefined  reference is returned.
                   8122: #     If none of the resources are found, an undef is returned
1.624     albertel 8123: sub resdata {
                   8124:     my ($name,$domain,$type,@which)=@_;
                   8125:     my $result;
                   8126:     if ($type eq 'course') {
                   8127: 	$result=&get_courseresdata($name,$domain);
                   8128:     } elsif ($type eq 'user') {
                   8129: 	$result=&get_userresdata($name,$domain);
                   8130:     }
                   8131:     if (!ref($result)) { return $result; }    
1.251     albertel 8132:     foreach my $item (@which) {
1.927     albertel 8133: 	if (defined($result->{$item->[0]})) {
                   8134: 	    return [$result->{$item->[0]},$item->[1]];
1.251     albertel 8135: 	}
1.250     albertel 8136:     }
1.291     albertel 8137:     return undef;
1.200     www      8138: }
                   8139: 
1.379     matthew  8140: #
                   8141: # EXT resource caching routines
                   8142: #
                   8143: 
                   8144: sub clear_EXT_cache_status {
1.383     albertel 8145:     &delenv('cache.EXT.');
1.379     matthew  8146: }
                   8147: 
                   8148: sub EXT_cache_status {
                   8149:     my ($target_domain,$target_user) = @_;
1.383     albertel 8150:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 8151:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  8152:         # We know already the user has no data
                   8153:         return 1;
                   8154:     } else {
                   8155:         return 0;
                   8156:     }
                   8157: }
                   8158: 
                   8159: sub EXT_cache_set {
                   8160:     my ($target_domain,$target_user) = @_;
1.383     albertel 8161:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.949     raeburn  8162:     #&appenv({$cachename => time});
1.379     matthew  8163: }
                   8164: 
1.28      www      8165: # --------------------------------------------------------- Value of a Variable
1.58      www      8166: sub EXT {
1.715     albertel 8167: 
1.395     albertel 8168:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      8169:     unless ($varname) { return ''; }
1.218     albertel 8170:     #get real user name/domain, courseid and symb
                   8171:     my $courseid;
1.359     albertel 8172:     my $publicuser;
1.427     www      8173:     if ($symbparm) {
                   8174: 	$symbparm=&get_symb_from_alias($symbparm);
                   8175:     }
1.218     albertel 8176:     if (!($uname && $udom)) {
1.790     albertel 8177:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 8178:       if (!$symbparm) {	$symbparm=$cursymb; }
                   8179:     } else {
1.620     albertel 8180: 	$courseid=$env{'request.course.id'};
1.218     albertel 8181:     }
1.48      www      8182:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   8183:     my $rest;
1.320     albertel 8184:     if (defined($therest[0])) {
1.48      www      8185:        $rest=join('.',@therest);
                   8186:     } else {
                   8187:        $rest='';
                   8188:     }
1.320     albertel 8189: 
1.57      www      8190:     my $qualifierrest=$qualifier;
                   8191:     if ($rest) { $qualifierrest.='.'.$rest; }
                   8192:     my $spacequalifierrest=$space;
                   8193:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      8194:     if ($realm eq 'user') {
1.48      www      8195: # --------------------------------------------------------------- user.resource
                   8196: 	if ($space eq 'resource') {
1.651     albertel 8197: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   8198: 		  || defined($Apache::lonhomework::parsing_a_task))
                   8199: 		 &&
1.744     albertel 8200: 		 ($symbparm eq &symbread()) ) {	
                   8201: 		# if we are in the middle of processing the resource the
                   8202: 		# get the value we are planning on committing
                   8203:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   8204:                     return $Apache::lonhomework::results{$qualifierrest};
                   8205:                 } else {
                   8206:                     return $Apache::lonhomework::history{$qualifierrest};
                   8207:                 }
1.335     albertel 8208: 	    } else {
1.359     albertel 8209: 		my %restored;
1.620     albertel 8210: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 8211: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   8212: 		} else {
                   8213: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   8214: 		}
1.335     albertel 8215: 		return $restored{$qualifierrest};
                   8216: 	    }
1.48      www      8217: # ----------------------------------------------------------------- user.access
                   8218:         } elsif ($space eq 'access') {
1.218     albertel 8219: 	    # FIXME - not supporting calls for a specific user
1.48      www      8220:             return &allowed($qualifier,$rest);
                   8221: # ------------------------------------------ user.preferences, user.environment
                   8222:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 8223: 	    if (($uname eq $env{'user.name'}) &&
                   8224: 		($udom eq $env{'user.domain'})) {
                   8225: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 8226: 	    } else {
1.359     albertel 8227: 		my %returnhash;
                   8228: 		if (!$publicuser) {
                   8229: 		    %returnhash=&userenvironment($udom,$uname,
                   8230: 						 $qualifierrest);
                   8231: 		}
1.218     albertel 8232: 		return $returnhash{$qualifierrest};
                   8233: 	    }
1.48      www      8234: # ----------------------------------------------------------------- user.course
                   8235:         } elsif ($space eq 'course') {
1.218     albertel 8236: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8237:             return $env{join('.',('request.course',$qualifier))};
1.48      www      8238: # ------------------------------------------------------------------- user.role
                   8239:         } elsif ($space eq 'role') {
1.218     albertel 8240: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 8241:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      8242:             if ($qualifier eq 'value') {
                   8243: 		return $role;
                   8244:             } elsif ($qualifier eq 'extent') {
                   8245:                 return $where;
                   8246:             }
                   8247: # ----------------------------------------------------------------- user.domain
                   8248:         } elsif ($space eq 'domain') {
1.218     albertel 8249:             return $udom;
1.48      www      8250: # ------------------------------------------------------------------- user.name
                   8251:         } elsif ($space eq 'name') {
1.218     albertel 8252:             return $uname;
1.48      www      8253: # ---------------------------------------------------- Any other user namespace
1.29      www      8254:         } else {
1.359     albertel 8255: 	    my %reply;
                   8256: 	    if (!$publicuser) {
                   8257: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   8258: 	    }
                   8259: 	    return $reply{$qualifierrest};
1.48      www      8260:         }
1.236     www      8261:     } elsif ($realm eq 'query') {
                   8262: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 8263:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   8264: 						[$spacequalifierrest]);
1.620     albertel 8265: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      8266:    } elsif ($realm eq 'request') {
1.48      www      8267: # ------------------------------------------------------------- request.browser
                   8268:         if ($space eq 'browser') {
1.430     www      8269: 	    if ($qualifier eq 'textremote') {
1.676     albertel 8270: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      8271: 		    return 1;
                   8272: 		} else {
                   8273: 		    return 0;
                   8274: 		}
                   8275: 	    } else {
1.620     albertel 8276: 		return $env{'browser.'.$qualifier};
1.430     www      8277: 	    }
1.57      www      8278: # ------------------------------------------------------------ request.filename
                   8279:         } else {
1.620     albertel 8280:             return $env{'request.'.$spacequalifierrest};
1.29      www      8281:         }
1.28      www      8282:     } elsif ($realm eq 'course') {
1.48      www      8283: # ---------------------------------------------------------- course.description
1.620     albertel 8284:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      8285:     } elsif ($realm eq 'resource') {
1.165     www      8286: 
1.620     albertel 8287: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 8288: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   8289: 	}
1.693     albertel 8290: 
                   8291: 	if ($space eq 'title') {
                   8292: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   8293: 	    return &gettitle($symbparm);
                   8294: 	}
                   8295: 	
                   8296: 	if ($space eq 'map') {
                   8297: 	    my ($map) = &decode_symb($symbparm);
                   8298: 	    return &symbread($map);
                   8299: 	}
1.905     albertel 8300: 	if ($space eq 'filename') {
                   8301: 	    if ($symbparm) {
                   8302: 		return &clutter((&decode_symb($symbparm))[2]);
                   8303: 	    }
                   8304: 	    return &hreflocation('',$env{'request.filename'});
                   8305: 	}
1.693     albertel 8306: 
                   8307: 	my ($section, $group, @groups);
1.593     albertel 8308: 	my ($courselevelm,$courselevel);
1.539     albertel 8309: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8310: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      8311: 
1.218     albertel 8312: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      8313: 
1.60      www      8314: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 8315: 	    my $symbp=$symbparm;
1.735     albertel 8316: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 8317: 
                   8318: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   8319: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   8320: 
1.620     albertel 8321: 	    if (($env{'user.name'} eq $uname) &&
                   8322: 		($env{'user.domain'} eq $udom)) {
                   8323: 		$section=$env{'request.course.sec'};
1.733     raeburn  8324:                 @groups = split(/:/,$env{'request.course.groups'});  
                   8325:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 8326: 	    } else {
1.539     albertel 8327: 		if (! defined($usection)) {
1.551     albertel 8328: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 8329: 		} else {
                   8330: 		    $section = $usection;
                   8331: 		}
1.733     raeburn  8332:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 8333: 	    }
                   8334: 
                   8335: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   8336: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   8337: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   8338: 
1.593     albertel 8339: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 8340: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 8341: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      8342: 
1.60      www      8343: # ----------------------------------------------------------- first, check user
1.624     albertel 8344: 
                   8345: 	    my $userreply=&resdata($uname,$udom,'user',
1.927     albertel 8346: 				       ([$courselevelr,'resource'],
                   8347: 					[$courselevelm,'map'     ],
                   8348: 					[$courselevel, 'course'  ]));
1.931     albertel 8349: 	    if (defined($userreply)) { return &get_reply($userreply); }
1.95      www      8350: 
1.594     albertel 8351: # ------------------------------------------------ second, check some of course
1.684     raeburn  8352:             my $coursereply;
1.691     raeburn  8353:             if (@groups > 0) {
                   8354:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   8355:                                        $mapparm,$spacequalifierrest);
1.927     albertel 8356:                 if (defined($coursereply)) { return &get_reply($coursereply); }
1.684     raeburn  8357:             }
1.96      www      8358: 
1.684     raeburn  8359: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.927     albertel 8360: 				  $env{'course.'.$courseid.'.domain'},
                   8361: 				  'course',
                   8362: 				  ([$seclevelr,   'resource'],
                   8363: 				   [$seclevelm,   'map'     ],
                   8364: 				   [$seclevel,    'course'  ],
                   8365: 				   [$courselevelr,'resource']));
                   8366: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.200     www      8367: 
1.60      www      8368: # ------------------------------------------------------ third, check map parms
1.218     albertel 8369: 	    my %parmhash=();
                   8370: 	    my $thisparm='';
                   8371: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 8372: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 8373: 		    &GDBM_READER(),0640)) {
1.218     albertel 8374: 		$thisparm=$parmhash{$symbparm};
                   8375: 		untie(%parmhash);
                   8376: 	    }
1.927     albertel 8377: 	    if ($thisparm) { return &get_reply([$thisparm,'resource']); }
1.218     albertel 8378: 	}
1.594     albertel 8379: # ------------------------------------------ fourth, look in resource metadata
1.71      www      8380: 
1.218     albertel 8381: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 8382: 	my $filename;
                   8383: 	if (!$symbparm) { $symbparm=&symbread(); }
                   8384: 	if ($symbparm) {
1.409     www      8385: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 8386: 	} else {
1.620     albertel 8387: 	    $filename=$env{'request.filename'};
1.282     albertel 8388: 	}
                   8389: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.927     albertel 8390: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.282     albertel 8391: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.927     albertel 8392: 	if (defined($metadata)) { return &get_reply([$metadata,'resource']); }
1.142     www      8393: 
1.927     albertel 8394: # ---------------------------------------------- fourth, look in rest of course
1.593     albertel 8395: 	if ($symbparm && defined($courseid) && 
1.620     albertel 8396: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 8397: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   8398: 				     $env{'course.'.$courseid.'.domain'},
                   8399: 				     'course',
1.927     albertel 8400: 				     ([$courselevelm,'map'   ],
                   8401: 				      [$courselevel, 'course']));
                   8402: 	    if (defined($coursereply)) { return &get_reply($coursereply); }
1.593     albertel 8403: 	}
1.145     www      8404: # ------------------------------------------------------------------ Cascade up
1.218     albertel 8405: 	unless ($space eq '0') {
1.336     albertel 8406: 	    my @parts=split(/_/,$space);
                   8407: 	    my $id=pop(@parts);
                   8408: 	    my $part=join('_',@parts);
                   8409: 	    if ($part eq '') { $part='0'; }
1.927     albertel 8410: 	    my @partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 8411: 				 $symbparm,$udom,$uname,$section,1);
1.938     raeburn  8412: 	    if (defined($partgeneral[0])) { return &get_reply(\@partgeneral); }
1.218     albertel 8413: 	}
1.395     albertel 8414: 	if ($recurse) { return undef; }
                   8415: 	my $pack_def=&packages_tab_default($filename,$varname);
1.927     albertel 8416: 	if (defined($pack_def)) { return &get_reply([$pack_def,'resource']); }
1.48      www      8417: # ---------------------------------------------------- Any other user namespace
                   8418:     } elsif ($realm eq 'environment') {
                   8419: # ----------------------------------------------------------------- environment
1.620     albertel 8420: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   8421: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 8422: 	} else {
1.770     albertel 8423: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   8424: 		return '';
                   8425: 	    }
1.219     albertel 8426: 	    my %returnhash=&userenvironment($udom,$uname,
                   8427: 					    $spacequalifierrest);
                   8428: 	    return $returnhash{$spacequalifierrest};
                   8429: 	}
1.28      www      8430:     } elsif ($realm eq 'system') {
1.48      www      8431: # ----------------------------------------------------------------- system.time
                   8432: 	if ($space eq 'time') {
                   8433: 	    return time;
                   8434:         }
1.696     albertel 8435:     } elsif ($realm eq 'server') {
                   8436: # ----------------------------------------------------------------- system.time
                   8437: 	if ($space eq 'name') {
                   8438: 	    return $ENV{'SERVER_NAME'};
                   8439:         }
1.28      www      8440:     }
1.48      www      8441:     return '';
1.61      www      8442: }
                   8443: 
1.927     albertel 8444: sub get_reply {
                   8445:     my ($reply_value) = @_;
1.940     raeburn  8446:     if (ref($reply_value) eq 'ARRAY') {
                   8447:         if (wantarray) {
                   8448: 	    return @$reply_value;
                   8449:         }
                   8450:         return $reply_value->[0];
                   8451:     } else {
                   8452:         return $reply_value;
1.927     albertel 8453:     }
                   8454: }
                   8455: 
1.691     raeburn  8456: sub check_group_parms {
                   8457:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   8458:     my @groupitems = ();
                   8459:     my $resultitem;
1.927     albertel 8460:     my @levels = ([$symbparm,'resource'],[$mapparm,'map'],[$what,'course']);
1.691     raeburn  8461:     foreach my $group (@{$groups}) {
                   8462:         foreach my $level (@levels) {
1.927     albertel 8463:              my $item = $courseid.'.['.$group.'].'.$level->[0];
                   8464:              push(@groupitems,[$item,$level->[1]]);
1.691     raeburn  8465:         }
                   8466:     }
                   8467:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   8468:                             $env{'course.'.$courseid.'.domain'},
                   8469:                                      'course',@groupitems);
                   8470:     return $coursereply;
                   8471: }
                   8472: 
                   8473: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  8474:     my ($courseid,@groups) = @_;
                   8475:     @groups = sort(@groups);
1.691     raeburn  8476:     return @groups;
                   8477: }
                   8478: 
1.395     albertel 8479: sub packages_tab_default {
                   8480:     my ($uri,$varname)=@_;
                   8481:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 8482: 
                   8483:     my (@extension,@specifics,$do_default);
                   8484:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 8485: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 8486: 	if ($pack_type eq 'default') {
                   8487: 	    $do_default=1;
                   8488: 	} elsif ($pack_type eq 'extension') {
                   8489: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 8490: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 8491: 	    # only look at packages defaults for packages that this id is
1.738     albertel 8492: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   8493: 	}
                   8494:     }
                   8495:     # first look for a package that matches the requested part id
                   8496:     foreach my $package (@specifics) {
                   8497: 	my (undef,$pack_type,$pack_part)=@{$package};
                   8498: 	next if ($pack_part ne $part);
                   8499: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8500: 	    return $packagetab{"$pack_type&$name&default"};
                   8501: 	}
                   8502:     }
                   8503:     # look for any possible matching non extension_ package
                   8504:     foreach my $package (@specifics) {
                   8505: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 8506: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8507: 	    return $packagetab{"$pack_type&$name&default"};
                   8508: 	}
1.585     albertel 8509: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 8510: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   8511: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 8512: 	}
                   8513:     }
1.738     albertel 8514:     # look for any posible extension_ match
                   8515:     foreach my $package (@extension) {
                   8516: 	my ($package,$pack_type)=@{$package};
                   8517: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   8518: 	    return $packagetab{"$pack_type&$name&default"};
                   8519: 	}
                   8520: 	if (defined($packagetab{$package."&$name&default"})) {
                   8521: 	    return $packagetab{$package."&$name&default"};
                   8522: 	}
                   8523:     }
                   8524:     # look for a global default setting
                   8525:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   8526: 	return $packagetab{"default&$name&default"};
                   8527:     }
1.395     albertel 8528:     return undef;
                   8529: }
                   8530: 
1.334     albertel 8531: sub add_prefix_and_part {
                   8532:     my ($prefix,$part)=@_;
                   8533:     my $keyroot;
                   8534:     if (defined($prefix) && $prefix !~ /^__/) {
                   8535: 	# prefix that has a part already
                   8536: 	$keyroot=$prefix;
                   8537:     } elsif (defined($prefix)) {
                   8538: 	# prefix that is missing a part
                   8539: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   8540:     } else {
                   8541: 	# no prefix at all
                   8542: 	if (defined($part)) { $keyroot='_'.$part; }
                   8543:     }
                   8544:     return $keyroot;
                   8545: }
                   8546: 
1.71      www      8547: # ---------------------------------------------------------------- Get metadata
                   8548: 
1.599     albertel 8549: my %metaentry;
1.1070    www      8550: my %importedpartids;
1.71      www      8551: sub metadata {
1.176     www      8552:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      8553:     $uri=&declutter($uri);
1.288     albertel 8554:     # if it is a non metadata possible uri return quickly
1.529     albertel 8555:     if (($uri eq '') || 
                   8556: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 8557: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.1108    raeburn  8558:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ m{^/*uploaded/.+\.sequence$})) {
1.924     albertel 8559: 	return undef;
                   8560:     }
                   8561:     if (($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) 
                   8562: 	&& &Apache::lonxml::get_state('target') =~ /^(|meta)$/) {
1.468     albertel 8563: 	return undef;
1.288     albertel 8564:     }
1.73      www      8565:     my $filename=$uri;
                   8566:     $uri=~s/\.meta$//;
1.172     www      8567: #
                   8568: # Is the metadata already cached?
1.177     www      8569: # Look at timestamp of caching
1.172     www      8570: # Everything is cached by the main uri, libraries are never directly cached
                   8571: #
1.428     albertel 8572:     if (!defined($liburi)) {
1.599     albertel 8573: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 8574: 	if (defined($cached)) { return $result->{':'.$what}; }
                   8575:     }
                   8576:     {
1.1069    www      8577: # Imported parts would go here
1.1070    www      8578:         my %importedids=();
                   8579:         my @origfileimportpartids=();
1.1069    www      8580:         my $importedparts=0;
1.172     www      8581: #
                   8582: # Is this a recursive call for a library?
                   8583: #
1.599     albertel 8584: #	if (! exists($metacache{$uri})) {
                   8585: #	    $metacache{$uri}={};
                   8586: #	}
1.924     albertel 8587: 	my $cachetime = 60*60;
1.171     www      8588:         if ($liburi) {
                   8589: 	    $liburi=&declutter($liburi);
                   8590:             $filename=$liburi;
1.401     bowersj2 8591:         } else {
1.599     albertel 8592: 	    &devalidate_cache_new('meta',$uri);
                   8593: 	    undef(%metaentry);
1.401     bowersj2 8594: 	}
1.140     www      8595:         my %metathesekeys=();
1.73      www      8596:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 8597: 	my $metastring;
1.924     albertel 8598: 	if ($uri =~ /^~/ || $uri =~ m{home/$match_username/public_html/}) {
1.929     albertel 8599: 	    my $which = &hreflocation('','/'.($liburi || $uri));
1.924     albertel 8600: 	    $metastring = 
1.929     albertel 8601: 		&Apache::lonnet::ssi_body($which,
1.924     albertel 8602: 					  ('grade_target' => 'meta'));
                   8603: 	    $cachetime = 1; # only want this cached in the child not long term
1.1108    raeburn  8604: 	} elsif (($uri !~ m -^(editupload)/-) && 
                   8605:                  ($uri !~ m{^/*uploaded/$match_domain/$match_courseid/docs/})) {
1.543     albertel 8606: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 8607: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 8608: 	    $metastring=&getfile($file);
1.489     albertel 8609: 	}
1.208     albertel 8610:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      8611:         my $token;
1.140     www      8612:         undef %metathesekeys;
1.71      www      8613:         while ($token=$parser->get_token) {
1.339     albertel 8614: 	    if ($token->[0] eq 'S') {
                   8615: 		if (defined($token->[2]->{'package'})) {
1.172     www      8616: #
                   8617: # This is a package - get package info
                   8618: #
1.339     albertel 8619: 		    my $package=$token->[2]->{'package'};
                   8620: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   8621: 		    if (defined($token->[2]->{'id'})) { 
                   8622: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   8623: 		    }
1.599     albertel 8624: 		    if ($metaentry{':packages'}) {
                   8625: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 8626: 		    } else {
1.599     albertel 8627: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 8628: 		    }
1.736     albertel 8629: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 8630: 			my $part=$keyroot;
                   8631: 			$part=~s/^\_//;
1.736     albertel 8632: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   8633: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   8634: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 8635: 			    # ignore package.tab specified default values
                   8636:                             # here &package_tab_default() will fetch those
                   8637: 			    if ($subp eq 'default') { next; }
1.736     albertel 8638: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 8639: 			    my $unikey;
                   8640: 			    if ($pack =~ /_0$/) {
                   8641: 				$unikey='parameter_0_'.$name;
                   8642: 				$part=0;
                   8643: 			    } else {
                   8644: 				$unikey='parameter'.$keyroot.'_'.$name;
                   8645: 			    }
1.339     albertel 8646: 			    if ($subp eq 'display') {
                   8647: 				$value.=' [Part: '.$part.']';
                   8648: 			    }
1.599     albertel 8649: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 8650: 			    $metathesekeys{$unikey}=1;
1.599     albertel 8651: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   8652: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 8653: 			    }
1.599     albertel 8654: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   8655: 				$metaentry{':'.$unikey}=
                   8656: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 8657: 			    }
1.339     albertel 8658: 			}
                   8659: 		    }
                   8660: 		} else {
1.172     www      8661: #
                   8662: # This is not a package - some other kind of start tag
1.339     albertel 8663: #
                   8664: 		    my $entry=$token->[1];
1.1068    www      8665: 		    my $unikey='';
1.175     www      8666: 
1.339     albertel 8667: 		    if ($entry eq 'import') {
1.175     www      8668: #
                   8669: # Importing a library here
1.339     albertel 8670: #
1.1067    www      8671:                         my $location=$parser->get_text('/import');
                   8672:                         my $dir=$filename;
                   8673:                         $dir=~s|[^/]*$||;
                   8674:                         $location=&filelocation($dir,$location);
1.1069    www      8675:                        
1.1068    www      8676:                         my $importmode=$token->[2]->{'importmode'};
                   8677:                         if ($importmode eq 'problem') {
1.1069    www      8678: # Import as problem/response
1.1068    www      8679:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   8680:                         } elsif ($importmode eq 'part') {
                   8681: # Import as part(s)
1.1069    www      8682:                            $importedparts=1;
                   8683: # We need to get the original file and the imported file to get the part order correct
                   8684: # Good news: we do not need to worry about nested libraries, since parts cannot be nested
                   8685: # Load and inspect original file
1.1070    www      8686:                            if ($#origfileimportpartids<0) {
                   8687:                               undef(%importedpartids);
                   8688:                               my $origfilelocation=$perlvar{'lonDocRoot'}.&clutter($uri);
                   8689:                               my $origfile=&getfile($origfilelocation);
                   8690:                               @origfileimportpartids=($origfile=~/<(part|import)[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   8691:                            }
                   8692: 
1.1069    www      8693: # Load and inspect imported file
                   8694:                            my $impfile=&getfile($location);
                   8695:                            my @impfilepartids=($impfile=~/<part[^>]*id\s*=\s*[\"\']([^\"\']+)[\"\'][^>]*>/gs);
                   8696:                            if ($#impfilepartids>=0) {
                   8697: # This problem had parts
1.1070    www      8698:                                $importedpartids{$token->[2]->{'id'}}=join(',',@impfilepartids);
1.1069    www      8699:                            } else {
                   8700: # Importing by turning a single problem into a problem part
                   8701: # It gets the import-tags ID as part-ID
                   8702:                                $unikey=&add_prefix_and_part($prefix,$token->[2]->{'id'});
1.1070    www      8703:                                $importedpartids{$token->[2]->{'id'}}=$token->[2]->{'id'};
1.1069    www      8704:                            }
1.1068    www      8705:                         } else {
                   8706: # Normal import
                   8707:                            $unikey=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   8708:                            if (defined($token->[2]->{'id'})) {
                   8709:                               $unikey.='_'.$token->[2]->{'id'};
                   8710:                            }
1.1067    www      8711:                         }
                   8712: 
1.339     albertel 8713: 			if ($depthcount<20) {
1.736     albertel 8714: 			    my $metadata = 
                   8715: 				&metadata($uri,'keys', $location,$unikey,
                   8716: 					  $depthcount+1);
                   8717: 			    foreach my $meta (split(',',$metadata)) {
                   8718: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   8719: 				$metathesekeys{$meta}=1;
1.339     albertel 8720: 			    }
1.1068    www      8721: 			
                   8722:                         }
1.1067    www      8723: 		    } else {
                   8724: #
                   8725: # Not importing, some other kind of non-package, non-library start tag
                   8726: # 
                   8727:                         $unikey=$entry.&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   8728:                         if (defined($token->[2]->{'id'})) {
                   8729:                             $unikey.='_'.$token->[2]->{'id'};
                   8730:                         }
1.339     albertel 8731: 			if (defined($token->[2]->{'name'})) { 
                   8732: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   8733: 			}
                   8734: 			$metathesekeys{$unikey}=1;
1.736     albertel 8735: 			foreach my $param (@{$token->[3]}) {
                   8736: 			    $metaentry{':'.$unikey.'.'.$param} =
                   8737: 				$token->[2]->{$param};
1.339     albertel 8738: 			}
                   8739: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 8740: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 8741: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   8742: 		 # only ws inside the tag, and not in default, so use default
                   8743: 		 # as value
1.599     albertel 8744: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 8745: 			} elsif ( $internaltext =~ /\S/ ) {
                   8746: 		  # something interesting inside the tag
                   8747: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 8748: 			} else {
1.908     albertel 8749: 		  # no interesting values, don't set a default
1.339     albertel 8750: 			}
1.172     www      8751: # end of not-a-package not-a-library import
1.339     albertel 8752: 		    }
1.172     www      8753: # end of not-a-package start tag
1.339     albertel 8754: 		}
1.172     www      8755: # the next is the end of "start tag"
1.339     albertel 8756: 	    }
                   8757: 	}
1.483     albertel 8758: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 8759: 	$extension = lc($extension);
                   8760: 	if ($extension eq 'htm') { $extension='html'; }
                   8761: 
1.737     albertel 8762: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 8763: 	    #no specific packages #how's our extension
                   8764: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 8765: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 8766: 					 \%metathesekeys);
                   8767: 	}
1.883     albertel 8768: 
                   8769: 	if (!exists($metaentry{':packages'})
                   8770: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 8771: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 8772: 		#no specific packages well let's get default then
                   8773: 		if ($key!~/^default&/) { next; }
1.488     albertel 8774: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 8775: 					     \%metathesekeys);
                   8776: 	    }
                   8777: 	}
1.338     www      8778: # are there custom rights to evaluate
1.599     albertel 8779: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 8780: 
1.338     www      8781:     #
                   8782:     # Importing a rights file here
1.339     albertel 8783:     #
                   8784: 	    unless ($depthcount) {
1.599     albertel 8785: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 8786: 		my $dir=$filename;
                   8787: 		$dir=~s|[^/]*$||;
                   8788: 		$location=&filelocation($dir,$location);
1.736     albertel 8789: 		my $rights_metadata =
                   8790: 		    &metadata($uri,'keys',$location,'_rights',
                   8791: 			      $depthcount+1);
                   8792: 		foreach my $rights (split(',',$rights_metadata)) {
                   8793: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   8794: 		    $metathesekeys{$rights}=1;
1.339     albertel 8795: 		}
                   8796: 	    }
                   8797: 	}
1.737     albertel 8798: 	# uniqifiy package listing
                   8799: 	my %seen;
                   8800: 	my @uniq_packages =
                   8801: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   8802: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   8803: 
1.1070    www      8804:         if ($importedparts) {
                   8805: # We had imported parts and need to rebuild partorder
                   8806:            $metaentry{':partorder'}='';
                   8807:            $metathesekeys{'partorder'}=1;
                   8808:            for (my $index=0;$index<$#origfileimportpartids;$index+=2) {
                   8809:                if ($origfileimportpartids[$index] eq 'part') {
                   8810: # original part, part of the problem
                   8811:                   $metaentry{':partorder'}.=','.$origfileimportpartids[$index+1];
                   8812:                } else {
                   8813: # we have imported parts at this position
                   8814:                   $metaentry{':partorder'}.=','.$importedpartids{$origfileimportpartids[$index+1]};
                   8815:                }
                   8816:            }
                   8817:            $metaentry{':partorder'}=~s/^\,//;
                   8818:         }
                   8819: 
1.737     albertel 8820: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 8821: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   8822: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.924     albertel 8823: 	&do_cache_new('meta',$uri,\%metaentry,$cachetime);
1.177     www      8824: # this is the end of "was not already recently cached
1.71      www      8825:     }
1.599     albertel 8826:     return $metaentry{':'.$what};
1.261     albertel 8827: }
                   8828: 
1.488     albertel 8829: sub metadata_create_package_def {
1.483     albertel 8830:     my ($uri,$key,$package,$metathesekeys)=@_;
                   8831:     my ($pack,$name,$subp)=split(/\&/,$key);
                   8832:     if ($subp eq 'default') { next; }
                   8833:     
1.599     albertel 8834:     if (defined($metaentry{':packages'})) {
                   8835: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 8836:     } else {
1.599     albertel 8837: 	$metaentry{':packages'}=$package;
1.483     albertel 8838:     }
                   8839:     my $value=$packagetab{$key};
                   8840:     my $unikey;
                   8841:     $unikey='parameter_0_'.$name;
1.599     albertel 8842:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 8843:     $$metathesekeys{$unikey}=1;
1.599     albertel 8844:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   8845: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 8846:     }
1.599     albertel 8847:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   8848: 	$metaentry{':'.$unikey}=
                   8849: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 8850:     }
                   8851: }
                   8852: 
1.261     albertel 8853: sub metadata_generate_part0 {
                   8854:     my ($metadata,$metacache,$uri) = @_;
                   8855:     my %allnames;
1.737     albertel 8856:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 8857: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 8858: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   8859: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 8860: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 8861: 	    $allnames{$name}=$part;
                   8862: 	  }
                   8863: 	}
                   8864:     }
                   8865:     foreach my $name (keys(%allnames)) {
                   8866:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 8867:       my $key=":parameter_0_$name";
1.261     albertel 8868:       $$metacache{"$key.part"}='0';
                   8869:       $$metacache{"$key.name"}=$name;
1.428     albertel 8870:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 8871: 					   $allnames{$name}.'_'.$name.
                   8872: 					   '.type'};
1.428     albertel 8873:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 8874: 			     '.display'};
1.644     www      8875:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 8876:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 8877:       $$metacache{"$key.display"}=$olddis;
                   8878:     }
1.71      www      8879: }
                   8880: 
1.764     albertel 8881: # ------------------------------------------------------ Devalidate title cache
                   8882: 
                   8883: sub devalidate_title_cache {
                   8884:     my ($url)=@_;
                   8885:     if (!$env{'request.course.id'}) { return; }
                   8886:     my $symb=&symbread($url);
                   8887:     if (!$symb) { return; }
                   8888:     my $key=$env{'request.course.id'}."\0".$symb;
                   8889:     &devalidate_cache_new('title',$key);
                   8890: }
                   8891: 
1.1014    droeschl 8892: # ------------------------------------------------- Get the title of a course
                   8893: 
                   8894: sub current_course_title {
                   8895:     return $env{ 'course.' . $env{'request.course.id'} . '.description' };
                   8896: }
1.301     www      8897: # ------------------------------------------------- Get the title of a resource
                   8898: 
                   8899: sub gettitle {
                   8900:     my $urlsymb=shift;
                   8901:     my $symb=&symbread($urlsymb);
1.534     albertel 8902:     if ($symb) {
1.620     albertel 8903: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 8904: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 8905: 	if (defined($cached)) { 
                   8906: 	    return $result;
                   8907: 	}
1.534     albertel 8908: 	my ($map,$resid,$url)=&decode_symb($symb);
                   8909: 	my $title='';
1.907     albertel 8910: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   8911: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   8912: 	} else {
                   8913: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   8914: 		    &GDBM_READER(),0640)) {
                   8915: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   8916: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   8917: 		untie(%bighash);
                   8918: 	    }
1.534     albertel 8919: 	}
                   8920: 	$title=~s/\&colon\;/\:/gs;
                   8921: 	if ($title) {
1.599     albertel 8922: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 8923: 	}
                   8924: 	$urlsymb=$url;
                   8925:     }
                   8926:     my $title=&metadata($urlsymb,'title');
                   8927:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   8928:     return $title;
1.301     www      8929: }
1.613     albertel 8930: 
1.614     albertel 8931: sub get_slot {
                   8932:     my ($which,$cnum,$cdom)=@_;
                   8933:     if (!$cnum || !$cdom) {
1.790     albertel 8934: 	(undef,my $courseid)=&whichuser();
1.620     albertel 8935: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   8936: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 8937:     }
1.703     albertel 8938:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   8939:     my %slotinfo;
                   8940:     if (exists($remembered{$key})) {
                   8941: 	$slotinfo{$which} = $remembered{$key};
                   8942:     } else {
                   8943: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   8944: 	&Apache::lonhomework::showhash(%slotinfo);
                   8945: 	my ($tmp)=keys(%slotinfo);
                   8946: 	if ($tmp=~/^error:/) { return (); }
                   8947: 	$remembered{$key} = $slotinfo{$which};
                   8948:     }
1.616     albertel 8949:     if (ref($slotinfo{$which}) eq 'HASH') {
                   8950: 	return %{$slotinfo{$which}};
                   8951:     }
                   8952:     return $slotinfo{$which};
1.614     albertel 8953: }
1.31      www      8954: # ------------------------------------------------- Update symbolic store links
                   8955: 
                   8956: sub symblist {
                   8957:     my ($mapname,%newhash)=@_;
1.438     www      8958:     $mapname=&deversion(&declutter($mapname));
1.31      www      8959:     my %hash;
1.620     albertel 8960:     if (($env{'request.course.fn'}) && (%newhash)) {
                   8961:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 8962:                       &GDBM_WRCREAT(),0640)) {
1.1000    raeburn  8963: 	    foreach my $url (keys(%newhash)) {
1.711     albertel 8964: 		next if ($url eq 'last_known'
                   8965: 			 && $env{'form.no_update_last_known'});
                   8966: 		$hash{declutter($url)}=&encode_symb($mapname,
                   8967: 						    $newhash{$url}->[1],
                   8968: 						    $newhash{$url}->[0]);
1.191     harris41 8969:             }
1.31      www      8970:             if (untie(%hash)) {
                   8971: 		return 'ok';
                   8972:             }
                   8973:         }
                   8974:     }
                   8975:     return 'error';
1.212     www      8976: }
                   8977: 
                   8978: # --------------------------------------------------------------- Verify a symb
                   8979: 
                   8980: sub symbverify {
1.510     www      8981:     my ($symb,$thisurl)=@_;
                   8982:     my $thisfn=$thisurl;
1.439     www      8983:     $thisfn=&declutter($thisfn);
1.215     www      8984: # direct jump to resource in page or to a sequence - will construct own symbs
                   8985:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   8986: # check URL part
1.409     www      8987:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      8988: 
1.431     www      8989:     unless ($url eq $thisfn) { return 0; }
1.213     www      8990: 
1.216     www      8991:     $symb=&symbclean($symb);
1.510     www      8992:     $thisurl=&deversion($thisurl);
1.439     www      8993:     $thisfn=&deversion($thisfn);
1.213     www      8994: 
                   8995:     my %bighash;
                   8996:     my $okay=0;
1.431     www      8997: 
1.620     albertel 8998:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 8999:                             &GDBM_READER(),0640)) {
1.1032    raeburn  9000:         if (($thisurl =~ m{^/adm/wrapper/ext/}) || ($thisurl =~ m{^ext/})) {
                   9001:             $thisurl =~ s/\?.+$//;
                   9002:         }
1.510     www      9003:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.1102    raeburn  9004:         unless ($ids) {
                   9005:             my $idkey = 'ids_'.($thisurl =~ m{^/}? '' : '/').$thisurl;  
                   9006:             $ids=$bighash{$idkey};
1.216     www      9007:         }
                   9008:         if ($ids) {
                   9009: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 9010: 	    foreach my $id (split(/\,/,$ids)) {
                   9011: 	       my ($mapid,$resid)=split(/\./,$id);
1.1032    raeburn  9012:                if ($thisfn =~ m{^/adm/wrapper/ext/}) {
                   9013:                    $symb =~ s/\?.+$//;
                   9014:                }
1.216     www      9015:                if (
                   9016:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   9017:    eq $symb) { 
1.620     albertel 9018: 		   if (($env{'request.role.adv'}) ||
1.1101    raeburn  9019: 		       ($bighash{'encrypted_'.$id} eq $env{'request.enc'}) ||
                   9020:                        ($thisurl eq '/adm/navmaps')) {
1.582     albertel 9021: 		       $okay=1; 
                   9022: 		   }
                   9023: 	       }
1.216     www      9024: 	   }
                   9025:         }
1.213     www      9026: 	untie(%bighash);
                   9027:     }
                   9028:     return $okay;
1.31      www      9029: }
                   9030: 
1.210     www      9031: # --------------------------------------------------------------- Clean-up symb
                   9032: 
                   9033: sub symbclean {
                   9034:     my $symb=shift;
1.568     albertel 9035:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      9036: # remove version from map
                   9037:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      9038: 
1.210     www      9039: # remove version from URL
                   9040:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      9041: 
1.507     www      9042: # remove wrapper
                   9043: 
1.510     www      9044:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 9045:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      9046:     return $symb;
1.409     www      9047: }
                   9048: 
                   9049: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 9050: 
                   9051: sub encode_symb {
                   9052:     my ($map,$resid,$url)=@_;
                   9053:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   9054: }
1.409     www      9055: 
                   9056: sub decode_symb {
1.568     albertel 9057:     my $symb=shift;
                   9058:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   9059:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      9060:     return (&fixversion($map),$resid,&fixversion($url));
                   9061: }
                   9062: 
                   9063: sub fixversion {
                   9064:     my $fn=shift;
1.609     banghart 9065:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      9066:     my %bighash;
                   9067:     my $uri=&clutter($fn);
1.620     albertel 9068:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      9069: # is this cached?
1.599     albertel 9070:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      9071:     if (defined($cached)) { return $result; }
                   9072: # unfortunately not cached, or expired
1.620     albertel 9073:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      9074: 	    &GDBM_READER(),0640)) {
                   9075:  	if ($bighash{'version_'.$uri}) {
                   9076:  	    my $version=$bighash{'version_'.$uri};
1.444     www      9077:  	    unless (($version eq 'mostrecent') || 
                   9078: 		    ($version==&getversion($uri))) {
1.440     www      9079:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   9080:  	    }
                   9081:  	}
                   9082:  	untie %bighash;
1.413     www      9083:     }
1.599     albertel 9084:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      9085: }
                   9086: 
                   9087: sub deversion {
                   9088:     my $url=shift;
                   9089:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   9090:     return $url;
1.210     www      9091: }
                   9092: 
1.31      www      9093: # ------------------------------------------------------ Return symb list entry
                   9094: 
                   9095: sub symbread {
1.249     www      9096:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 9097:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 9098:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      9099: # no filename provided? try from environment
1.44      www      9100:     unless ($thisfn) {
1.620     albertel 9101:         if ($env{'request.symb'}) {
                   9102: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 9103: 	}
1.620     albertel 9104: 	$thisfn=$env{'request.filename'};
1.44      www      9105:     }
1.569     albertel 9106:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      9107: # is that filename actually a symb? Verify, clean, and return
                   9108:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 9109: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 9110: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 9111: 	}
1.242     www      9112:     }
1.44      www      9113:     $thisfn=declutter($thisfn);
1.31      www      9114:     my %hash;
1.37      www      9115:     my %bighash;
                   9116:     my $syval='';
1.620     albertel 9117:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  9118:         my $targetfn = $thisfn;
1.609     banghart 9119:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  9120:             $targetfn = 'adm/wrapper/'.$thisfn;
                   9121:         }
1.687     albertel 9122: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   9123: 	    $targetfn=$1;
                   9124: 	}
1.620     albertel 9125:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 9126:                       &GDBM_READER(),0640)) {
1.481     raeburn  9127: 	    $syval=$hash{$targetfn};
1.37      www      9128:             untie(%hash);
                   9129:         }
                   9130: # ---------------------------------------------------------- There was an entry
                   9131:         if ($syval) {
1.601     albertel 9132: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 9133: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.949     raeburn  9134: 		    #&appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9135: 		    #return $env{$cache_str}='';
1.601     albertel 9136: 		#}    
                   9137: 		#$syval.=$1;
                   9138: 	    #}
1.37      www      9139:         } else {
                   9140: # ------------------------------------------------------- Was not in symb table
1.620     albertel 9141:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 9142:                             &GDBM_READER(),0640)) {
1.37      www      9143: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      9144:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      9145:               unless ($ids) { 
                   9146:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      9147:               }
                   9148:               unless ($ids) {
                   9149: # alias?
                   9150: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      9151:               }
1.37      www      9152:               if ($ids) {
                   9153: # ------------------------------------------------------------------- Has ID(s)
                   9154:                  my @possibilities=split(/\,/,$ids);
1.39      www      9155:                  if ($#possibilities==0) {
                   9156: # ----------------------------------------------- There is only one possibility
1.37      www      9157: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 9158: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9159: 						    $resid,$thisfn);
1.249     www      9160:                  } elsif (!$donotrecurse) {
1.39      www      9161: # ------------------------------------------ There is more than one possibility
                   9162:                      my $realpossible=0;
1.800     albertel 9163:                      foreach my $id (@possibilities) {
                   9164: 			 my $file=$bighash{'src_'.$id};
1.39      www      9165:                          if (&allowed('bre',$file)) {
1.800     albertel 9166:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      9167:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   9168: 				$realpossible++;
1.626     albertel 9169:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   9170: 						    $resid,$thisfn);
1.39      www      9171:                             }
                   9172: 			 }
1.191     harris41 9173:                      }
1.39      www      9174: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      9175:                  } else {
                   9176:                      $syval='';
1.37      www      9177:                  }
                   9178: 	      }
                   9179:               untie(%bighash)
1.481     raeburn  9180:            }
1.31      www      9181:         }
1.62      www      9182:         if ($syval) {
1.620     albertel 9183: 	    return $env{$cache_str}=$syval;
1.62      www      9184:         }
1.31      www      9185:     }
1.949     raeburn  9186:     &appenv({'request.ambiguous' => $thisfn});
1.620     albertel 9187:     return $env{$cache_str}='';
1.31      www      9188: }
                   9189: 
                   9190: # ---------------------------------------------------------- Return random seed
                   9191: 
1.32      www      9192: sub numval {
                   9193:     my $txt=shift;
                   9194:     $txt=~tr/A-J/0-9/;
                   9195:     $txt=~tr/a-j/0-9/;
                   9196:     $txt=~tr/K-T/0-9/;
                   9197:     $txt=~tr/k-t/0-9/;
                   9198:     $txt=~tr/U-Z/0-5/;
                   9199:     $txt=~tr/u-z/0-5/;
                   9200:     $txt=~s/\D//g;
1.564     albertel 9201:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      9202:     return int($txt);
1.368     albertel 9203: }
                   9204: 
1.484     albertel 9205: sub numval2 {
                   9206:     my $txt=shift;
                   9207:     $txt=~tr/A-J/0-9/;
                   9208:     $txt=~tr/a-j/0-9/;
                   9209:     $txt=~tr/K-T/0-9/;
                   9210:     $txt=~tr/k-t/0-9/;
                   9211:     $txt=~tr/U-Z/0-5/;
                   9212:     $txt=~tr/u-z/0-5/;
                   9213:     $txt=~s/\D//g;
                   9214:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9215:     my $total;
                   9216:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 9217:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 9218:     return int($total);
                   9219: }
                   9220: 
1.575     albertel 9221: sub numval3 {
                   9222:     use integer;
                   9223:     my $txt=shift;
                   9224:     $txt=~tr/A-J/0-9/;
                   9225:     $txt=~tr/a-j/0-9/;
                   9226:     $txt=~tr/K-T/0-9/;
                   9227:     $txt=~tr/k-t/0-9/;
                   9228:     $txt=~tr/U-Z/0-5/;
                   9229:     $txt=~tr/u-z/0-5/;
                   9230:     $txt=~s/\D//g;
                   9231:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   9232:     my $total;
                   9233:     foreach my $val (@txts) { $total+=$val; }
                   9234:     if ($_64bit) { $total=(($total<<32)>>32); }
                   9235:     return $total;
                   9236: }
                   9237: 
1.675     albertel 9238: sub digest {
                   9239:     my ($data)=@_;
                   9240:     my $digest=&Digest::MD5::md5($data);
                   9241:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   9242:     my ($e,$f);
                   9243:     {
                   9244:         use integer;
                   9245:         $e=($a+$b);
                   9246:         $f=($c+$d);
                   9247:         if ($_64bit) {
                   9248:             $e=(($e<<32)>>32);
                   9249:             $f=(($f<<32)>>32);
                   9250:         }
                   9251:     }
                   9252:     if (wantarray) {
                   9253: 	return ($e,$f);
                   9254:     } else {
                   9255: 	my $g;
                   9256: 	{
                   9257: 	    use integer;
                   9258: 	    $g=($e+$f);
                   9259: 	    if ($_64bit) {
                   9260: 		$g=(($g<<32)>>32);
                   9261: 	    }
                   9262: 	}
                   9263: 	return $g;
                   9264:     }
                   9265: }
                   9266: 
1.368     albertel 9267: sub latest_rnd_algorithm_id {
1.675     albertel 9268:     return '64bit5';
1.366     albertel 9269: }
1.32      www      9270: 
1.503     albertel 9271: sub get_rand_alg {
                   9272:     my ($courseid)=@_;
1.790     albertel 9273:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 9274:     if ($courseid) {
1.620     albertel 9275: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 9276:     }
                   9277:     return &latest_rnd_algorithm_id();
                   9278: }
                   9279: 
1.562     albertel 9280: sub validCODE {
                   9281:     my ($CODE)=@_;
                   9282:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   9283:     return 0;
                   9284: }
                   9285: 
1.491     albertel 9286: sub getCODE {
1.620     albertel 9287:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 9288:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   9289: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   9290: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 9291: 	return $Apache::lonhomework::history{'resource.CODE'};
                   9292:     }
                   9293:     return undef;
                   9294: }
                   9295: 
1.31      www      9296: sub rndseed {
1.155     albertel 9297:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 9298:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 9299:     if (!defined($symb)) {
1.366     albertel 9300: 	unless ($symb=$wsymb) { return time; }
                   9301:     }
                   9302:     if (!$courseid) { $courseid=$wcourseid; }
                   9303:     if (!$domain) { $domain=$wdomain; }
                   9304:     if (!$username) { $username=$wusername }
1.503     albertel 9305:     my $which=&get_rand_alg();
1.1110    www      9306: 
1.491     albertel 9307:     if (defined(&getCODE())) {
1.675     albertel 9308: 	if ($which eq '64bit5') {
                   9309: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   9310: 	} elsif ($which eq '64bit4') {
1.575     albertel 9311: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   9312: 	} else {
                   9313: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   9314: 	}
1.675     albertel 9315:     } elsif ($which eq '64bit5') {
                   9316: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 9317:     } elsif ($which eq '64bit4') {
                   9318: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 9319:     } elsif ($which eq '64bit3') {
                   9320: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 9321:     } elsif ($which eq '64bit2') {
                   9322: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 9323:     } elsif ($which eq '64bit') {
                   9324: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   9325:     }
                   9326:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   9327: }
                   9328: 
                   9329: sub rndseed_32bit {
                   9330:     my ($symb,$courseid,$domain,$username)=@_;
                   9331:     {
                   9332: 	use integer;
                   9333: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   9334: 	my $symbseed=numval($symb) << 22;
                   9335: 	my $namechck=unpack("%32C*",$username) << 17;
                   9336: 	my $nameseed=numval($username) << 12;
                   9337: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   9338: 	my $courseseed=unpack("%32C*",$courseid);
                   9339: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 9340: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9341: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9342: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 9343: 	return $num;
                   9344:     }
                   9345: }
                   9346: 
                   9347: sub rndseed_64bit {
                   9348:     my ($symb,$courseid,$domain,$username)=@_;
                   9349:     {
                   9350: 	use integer;
                   9351: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   9352: 	my $symbseed=numval($symb) << 10;
                   9353: 	my $namechck=unpack("%32S*",$username);
                   9354: 	
                   9355: 	my $nameseed=numval($username) << 21;
                   9356: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   9357: 	my $courseseed=unpack("%32S*",$courseid);
                   9358: 	
                   9359: 	my $num1=$symbchck+$symbseed+$namechck;
                   9360: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9361: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9362: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 9363: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 9364: 	return "$num1,$num2";
1.155     albertel 9365:     }
1.366     albertel 9366: }
                   9367: 
1.443     albertel 9368: sub rndseed_64bit2 {
                   9369:     my ($symb,$courseid,$domain,$username)=@_;
                   9370:     {
                   9371: 	use integer;
                   9372: 	# strings need to be an even # of cahracters long, it it is odd the
                   9373:         # last characters gets thrown away
                   9374: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9375: 	my $symbseed=numval($symb) << 10;
                   9376: 	my $namechck=unpack("%32S*",$username.' ');
                   9377: 	
                   9378: 	my $nameseed=numval($username) << 21;
1.501     albertel 9379: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9380: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9381: 	
                   9382: 	my $num1=$symbchck+$symbseed+$namechck;
                   9383: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9384: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9385: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 9386: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 9387: 	return "$num1,$num2";
                   9388:     }
                   9389: }
                   9390: 
                   9391: sub rndseed_64bit3 {
                   9392:     my ($symb,$courseid,$domain,$username)=@_;
                   9393:     {
                   9394: 	use integer;
                   9395: 	# strings need to be an even # of cahracters long, it it is odd the
                   9396:         # last characters gets thrown away
                   9397: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9398: 	my $symbseed=numval2($symb) << 10;
                   9399: 	my $namechck=unpack("%32S*",$username.' ');
                   9400: 	
                   9401: 	my $nameseed=numval2($username) << 21;
1.443     albertel 9402: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9403: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9404: 	
                   9405: 	my $num1=$symbchck+$symbseed+$namechck;
                   9406: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9407: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9408: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 9409: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9410: 	
1.503     albertel 9411: 	return "$num1:$num2";
1.443     albertel 9412:     }
                   9413: }
                   9414: 
1.575     albertel 9415: sub rndseed_64bit4 {
                   9416:     my ($symb,$courseid,$domain,$username)=@_;
                   9417:     {
                   9418: 	use integer;
                   9419: 	# strings need to be an even # of cahracters long, it it is odd the
                   9420:         # last characters gets thrown away
                   9421: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   9422: 	my $symbseed=numval3($symb) << 10;
                   9423: 	my $namechck=unpack("%32S*",$username.' ');
                   9424: 	
                   9425: 	my $nameseed=numval3($username) << 21;
                   9426: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   9427: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9428: 	
                   9429: 	my $num1=$symbchck+$symbseed+$namechck;
                   9430: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 9431: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   9432: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 9433: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.1110    www      9434: 	
1.575     albertel 9435: 	return "$num1:$num2";
                   9436:     }
                   9437: }
                   9438: 
1.675     albertel 9439: sub rndseed_64bit5 {
                   9440:     my ($symb,$courseid,$domain,$username)=@_;
                   9441:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   9442:     return "$num1:$num2";
                   9443: }
                   9444: 
1.366     albertel 9445: sub rndseed_CODE_64bit {
                   9446:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 9447:     {
1.366     albertel 9448: 	use integer;
1.443     albertel 9449: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 9450: 	my $symbseed=numval2($symb);
1.491     albertel 9451: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   9452: 	my $CODEseed=numval(&getCODE());
1.443     albertel 9453: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 9454: 	my $num1=$symbseed+$CODEchck;
                   9455: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 9456: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   9457: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 9458: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   9459: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 9460: 	return "$num1:$num2";
1.366     albertel 9461:     }
                   9462: }
                   9463: 
1.575     albertel 9464: sub rndseed_CODE_64bit4 {
                   9465:     my ($symb,$courseid,$domain,$username)=@_;
                   9466:     {
                   9467: 	use integer;
                   9468: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   9469: 	my $symbseed=numval3($symb);
                   9470: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   9471: 	my $CODEseed=numval3(&getCODE());
                   9472: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   9473: 	my $num1=$symbseed+$CODEchck;
                   9474: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 9475: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   9476: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 9477: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   9478: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   9479: 	return "$num1:$num2";
                   9480:     }
                   9481: }
                   9482: 
1.675     albertel 9483: sub rndseed_CODE_64bit5 {
                   9484:     my ($symb,$courseid,$domain,$username)=@_;
                   9485:     my $code = &getCODE();
                   9486:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   9487:     return "$num1:$num2";
                   9488: }
                   9489: 
1.366     albertel 9490: sub setup_random_from_rndseed {
                   9491:     my ($rndseed)=@_;
1.503     albertel 9492:     if ($rndseed =~/([,:])/) {
                   9493: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 9494: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   9495:     } else {
                   9496: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 9497:     }
1.36      albertel 9498: }
                   9499: 
1.474     albertel 9500: sub latest_receipt_algorithm_id {
1.835     albertel 9501:     return 'receipt3';
1.474     albertel 9502: }
                   9503: 
1.480     www      9504: sub recunique {
                   9505:     my $fucourseid=shift;
                   9506:     my $unique;
1.835     albertel 9507:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   9508: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 9509: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      9510:     } else {
                   9511: 	$unique=$perlvar{'lonReceipt'};
                   9512:     }
                   9513:     return unpack("%32C*",$unique);
                   9514: }
                   9515: 
                   9516: sub recprefix {
                   9517:     my $fucourseid=shift;
                   9518:     my $prefix;
1.835     albertel 9519:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   9520: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 9521: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      9522:     } else {
                   9523: 	$prefix=$perlvar{'lonHostID'};
                   9524:     }
                   9525:     return unpack("%32C*",$prefix);
                   9526: }
                   9527: 
1.76      www      9528: sub ireceipt {
1.474     albertel 9529:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 9530: 
                   9531:     my $return =&recprefix($fucourseid).'-';
                   9532: 
                   9533:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   9534: 	$env{'request.state'} eq 'construct') {
                   9535: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   9536: 	return $return;
                   9537:     }
                   9538: 
1.76      www      9539:     my $cuname=unpack("%32C*",$funame);
                   9540:     my $cudom=unpack("%32C*",$fudom);
                   9541:     my $cucourseid=unpack("%32C*",$fucourseid);
                   9542:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      9543:     my $cunique=&recunique($fucourseid);
1.474     albertel 9544:     my $cpart=unpack("%32S*",$part);
1.835     albertel 9545:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   9546: 
1.790     albertel 9547: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 9548: 			       
                   9549: 	$return.= ($cunique%$cuname+
                   9550: 		   $cunique%$cudom+
                   9551: 		   $cusymb%$cuname+
                   9552: 		   $cusymb%$cudom+
                   9553: 		   $cucourseid%$cuname+
                   9554: 		   $cucourseid%$cudom+
                   9555: 		   $cpart%$cuname+
                   9556: 		   $cpart%$cudom);
                   9557:     } else {
                   9558: 	$return.= ($cunique%$cuname+
                   9559: 		   $cunique%$cudom+
                   9560: 		   $cusymb%$cuname+
                   9561: 		   $cusymb%$cudom+
                   9562: 		   $cucourseid%$cuname+
                   9563: 		   $cucourseid%$cudom);
                   9564:     }
                   9565:     return $return;
1.76      www      9566: }
                   9567: 
                   9568: sub receipt {
1.474     albertel 9569:     my ($part)=@_;
1.790     albertel 9570:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 9571:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      9572: }
1.260     ng       9573: 
1.790     albertel 9574: sub whichuser {
                   9575:     my ($passedsymb)=@_;
                   9576:     my ($symb,$courseid,$domain,$name,$publicuser);
                   9577:     if (defined($env{'form.grade_symb'})) {
                   9578: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   9579: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   9580: 	if (!$allowed &&
                   9581: 	    exists($env{'request.course.sec'}) &&
                   9582: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   9583: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   9584: 			      '/'.$env{'request.course.sec'});
                   9585: 	}
                   9586: 	if ($allowed) {
                   9587: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   9588: 	    $courseid=$tmp_courseid;
                   9589: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   9590: 	    ($name)=&get_env_multiple('form.grade_username');
                   9591: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   9592: 	}
                   9593:     }
                   9594:     if (!$passedsymb) {
                   9595: 	$symb=&symbread();
                   9596:     } else {
                   9597: 	$symb=$passedsymb;
                   9598:     }
                   9599:     $courseid=$env{'request.course.id'};
                   9600:     $domain=$env{'user.domain'};
                   9601:     $name=$env{'user.name'};
                   9602:     if ($name eq 'public' && $domain eq 'public') {
                   9603: 	if (!defined($env{'form.username'})) {
                   9604: 	    $env{'form.username'}.=time.rand(10000000);
                   9605: 	}
                   9606: 	$name.=$env{'form.username'};
                   9607:     }
                   9608:     return ($symb,$courseid,$domain,$name,$publicuser);
                   9609: 
                   9610: }
                   9611: 
1.36      albertel 9612: # ------------------------------------------------------------ Serves up a file
1.472     albertel 9613: # returns either the contents of the file or 
                   9614: # -1 if the file doesn't exist
1.481     raeburn  9615: #
                   9616: # if the target is a file that was uploaded via DOCS, 
                   9617: # a check will be made to see if a current copy exists on the local server,
                   9618: # if it does this will be served, otherwise a copy will be retrieved from
                   9619: # the home server for the course and stored in /home/httpd/html/userfiles on
                   9620: # the local server.   
1.472     albertel 9621: 
1.36      albertel 9622: sub getfile {
1.538     albertel 9623:     my ($file) = @_;
1.609     banghart 9624:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 9625:     &repcopy($file);
                   9626:     return &readfile($file);
                   9627: }
                   9628: 
                   9629: sub repcopy_userfile {
                   9630:     my ($file)=@_;
1.609     banghart 9631:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 9632:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 9633:     my ($cdom,$cnum,$filename) = 
1.811     albertel 9634: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 9635:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   9636:     if (-e "$file") {
1.828     www      9637: # we already have a local copy, check it out
1.538     albertel 9638: 	my @fileinfo = stat($file);
1.828     www      9639: 	my $rtncode;
                   9640: 	my $info;
1.538     albertel 9641: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 9642: 	if ($lwpresp ne 'ok') {
1.828     www      9643: # there is no such file anymore, even though we had a local copy
1.482     albertel 9644: 	    if ($rtncode eq '404') {
1.538     albertel 9645: 		unlink($file);
1.482     albertel 9646: 	    }
                   9647: 	    return -1;
                   9648: 	}
                   9649: 	if ($info < $fileinfo[9]) {
1.828     www      9650: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  9651: 	    return 'ok';
1.828     www      9652: 	} else {
                   9653: # the file is outdated, get rid of it
                   9654: 	    unlink($file);
1.482     albertel 9655: 	}
1.828     www      9656:     }
                   9657: # one way or the other, at this point, we don't have the file
                   9658: # construct the correct path for the file
                   9659:     my @parts = ($cdom,$cnum); 
                   9660:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   9661: 	push @parts, split(/\//,$1);
                   9662:     }
                   9663:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   9664:     foreach my $part (@parts) {
                   9665: 	$path .= '/'.$part;
                   9666: 	if (!-e $path) {
                   9667: 	    mkdir($path,0770);
1.482     albertel 9668: 	}
                   9669:     }
1.828     www      9670: # now the path exists for sure
                   9671: # get a user agent
                   9672:     my $ua=new LWP::UserAgent;
                   9673:     my $transferfile=$file.'.in.transfer';
                   9674: # FIXME: this should flock
                   9675:     if (-e $transferfile) { return 'ok'; }
                   9676:     my $request;
                   9677:     $uri=~s/^\///;
1.980     raeburn  9678:     my $homeserver = &homeserver($cnum,$cdom);
                   9679:     my $protocol = $protocol{$homeserver};
                   9680:     $protocol = 'http' if ($protocol ne 'https');
                   9681:     $request=new HTTP::Request('GET',$protocol.'://'.&hostname($homeserver).'/raw/'.$uri);
1.828     www      9682:     my $response=$ua->request($request,$transferfile);
                   9683: # did it work?
                   9684:     if ($response->is_error()) {
                   9685: 	unlink($transferfile);
                   9686: 	&logthis("Userfile repcopy failed for $uri");
                   9687: 	return -1;
                   9688:     }
                   9689: # worked, rename the transfer file
                   9690:     rename($transferfile,$file);
1.607     raeburn  9691:     return 'ok';
1.481     raeburn  9692: }
                   9693: 
1.517     albertel 9694: sub tokenwrapper {
                   9695:     my $uri=shift;
1.980     raeburn  9696:     $uri=~s|^https?\://([^/]+)||;
1.552     albertel 9697:     $uri=~s|^/||;
1.620     albertel 9698:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 9699:     my $token=$1;
1.552     albertel 9700:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   9701:     if ($udom && $uname && $file) {
                   9702: 	$file=~s|(\?\.*)*$||;
1.949     raeburn  9703:         &appenv({"userfile.$udom/$uname/$file" => $env{'request.course.id'}});
1.980     raeburn  9704:         my $homeserver = &homeserver($uname,$udom);
                   9705:         my $protocol = $protocol{$homeserver};
                   9706:         $protocol = 'http' if ($protocol ne 'https');
                   9707:         return $protocol.'://'.&hostname($homeserver).'/'.$uri.
1.517     albertel 9708:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   9709:                                '&tokenissued='.$perlvar{'lonHostID'};
                   9710:     } else {
                   9711:         return '/adm/notfound.html';
                   9712:     }
                   9713: }
                   9714: 
1.828     www      9715: # call with reqtype HEAD: get last modification time
                   9716: # call with reqtype GET: get the file contents
                   9717: # Do not call this with reqtype GET for large files! It loads everything into memory
                   9718: #
1.481     raeburn  9719: sub getuploaded {
                   9720:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   9721:     $uri=~s/^\///;
1.980     raeburn  9722:     my $homeserver = &homeserver($cnum,$cdom);
                   9723:     my $protocol = $protocol{$homeserver};
                   9724:     $protocol = 'http' if ($protocol ne 'https');
                   9725:     $uri = $protocol.'://'.&hostname($homeserver).'/raw/'.$uri;
1.481     raeburn  9726:     my $ua=new LWP::UserAgent;
                   9727:     my $request=new HTTP::Request($reqtype,$uri);
                   9728:     my $response=$ua->request($request);
                   9729:     $$rtncode = $response->code;
1.482     albertel 9730:     if (! $response->is_success()) {
                   9731: 	return 'failed';
                   9732:     }      
                   9733:     if ($reqtype eq 'HEAD') {
1.486     www      9734: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 9735:     } elsif ($reqtype eq 'GET') {
                   9736: 	$$info = $response->content;
1.472     albertel 9737:     }
1.482     albertel 9738:     return 'ok';
1.36      albertel 9739: }
                   9740: 
1.481     raeburn  9741: sub readfile {
                   9742:     my $file = shift;
                   9743:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   9744:     my $fh;
                   9745:     open($fh,"<$file");
                   9746:     my $a='';
1.800     albertel 9747:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  9748:     return $a;
                   9749: }
                   9750: 
1.36      albertel 9751: sub filelocation {
1.590     banghart 9752:     my ($dir,$file) = @_;
                   9753:     my $location;
                   9754:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 9755: 
                   9756:     if ($file =~ m-^/adm/-) {
                   9757: 	$file=~s-^/adm/wrapper/-/-;
                   9758: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   9759:     }
1.882     albertel 9760: 
1.590     banghart 9761:     if ($file=~m:^/~:) { # is a contruction space reference
                   9762:         $location = $file;
                   9763:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 9764:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 9765: 	# is a correct contruction space reference
                   9766:         $location = $file;
1.956     raeburn  9767:     } elsif ($file =~ m-^\Q$Apache::lonnet::perlvar{'lonTabDir'}\E/-) {
                   9768:         $location = $file;
1.609     banghart 9769:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 9770:         my ($udom,$uname,$filename)=
1.811     albertel 9771:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 9772:         my $home=&homeserver($uname,$udom);
                   9773:         my $is_me=0;
                   9774:         my @ids=&current_machine_ids();
                   9775:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   9776:         if ($is_me) {
1.955     raeburn  9777:   	    $location=&propath($udom,$uname).'/userfiles/'.$filename;
1.590     banghart 9778:         } else {
                   9779:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   9780:   	      $udom.'/'.$uname.'/'.$filename;
                   9781:         }
1.882     albertel 9782:     } elsif ($file =~ m-^/adm/-) {
                   9783: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 9784:     } else {
                   9785:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   9786:         $file=~s:^/res/:/:;
                   9787:         if ( !( $file =~ m:^/:) ) {
                   9788:             $location = $dir. '/'.$file;
                   9789:         } else {
                   9790:             $location = '/home/httpd/html/res'.$file;
                   9791:         }
1.59      albertel 9792:     }
1.590     banghart 9793:     $location=~s://+:/:g; # remove duplicate /
1.930     albertel 9794:     while ($location=~m{/\.\./}) {
                   9795: 	if ($location =~ m{/[^/]+/\.\./}) {
                   9796: 	    $location=~ s{/[^/]+/\.\./}{/}g;
                   9797: 	} else {
                   9798: 	    $location=~ s{/\.\./}{/}g;
                   9799: 	}
                   9800:     } #remove dir/..
1.590     banghart 9801:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   9802:     return $location;
1.46      www      9803: }
1.36      albertel 9804: 
1.46      www      9805: sub hreflocation {
                   9806:     my ($dir,$file)=@_;
1.980     raeburn  9807:     unless (($file=~m-^https?\://-i) || ($file=~m-^/-)) {
1.666     albertel 9808: 	$file=filelocation($dir,$file);
1.700     albertel 9809:     } elsif ($file=~m-^/adm/-) {
                   9810: 	$file=~s-^/adm/wrapper/-/-;
                   9811: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 9812:     }
                   9813:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   9814: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 9815:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   9816: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 9817:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 9818: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 9819: 	    -/uploaded/$1/$2/-x;
1.46      www      9820:     }
1.913     albertel 9821:     if ($file=~ m{^/userfiles/}) {
                   9822: 	$file =~ s{^/userfiles/}{/uploaded/};
                   9823:     }
1.462     albertel 9824:     return $file;
1.465     albertel 9825: }
                   9826: 
                   9827: sub current_machine_domains {
1.853     albertel 9828:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   9829: }
                   9830: 
                   9831: sub machine_domains {
                   9832:     my ($hostname) = @_;
1.465     albertel 9833:     my @domains;
1.838     albertel 9834:     my %hostname = &all_hostnames();
1.465     albertel 9835:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  9836: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 9837: 	if ($hostname eq $name) {
1.844     albertel 9838: 	    push(@domains,&host_domain($id));
1.465     albertel 9839: 	}
                   9840:     }
                   9841:     return @domains;
                   9842: }
                   9843: 
                   9844: sub current_machine_ids {
1.853     albertel 9845:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   9846: }
                   9847: 
                   9848: sub machine_ids {
                   9849:     my ($hostname) = @_;
                   9850:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 9851:     my @ids;
1.888     albertel 9852:     my %name_to_host = &all_names();
1.889     albertel 9853:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   9854: 	return @{ $name_to_host{$hostname} };
                   9855:     }
                   9856:     return;
1.31      www      9857: }
                   9858: 
1.824     raeburn  9859: sub additional_machine_domains {
                   9860:     my @domains;
                   9861:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   9862:     while( my $line = <$fh>) {
                   9863:         $line =~ s/\s//g;
                   9864:         push(@domains,$line);
                   9865:     }
                   9866:     return @domains;
                   9867: }
                   9868: 
                   9869: sub default_login_domain {
                   9870:     my $domain = $perlvar{'lonDefDomain'};
                   9871:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   9872:     foreach my $posdom (&current_machine_domains(),
                   9873:                         &additional_machine_domains()) {
                   9874:         if (lc($posdom) eq lc($testdomain)) {
                   9875:             $domain=$posdom;
                   9876:             last;
                   9877:         }
                   9878:     }
                   9879:     return $domain;
                   9880: }
                   9881: 
1.31      www      9882: # ------------------------------------------------------------- Declutters URLs
                   9883: 
                   9884: sub declutter {
                   9885:     my $thisfn=shift;
1.569     albertel 9886:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 9887:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      9888:     $thisfn=~s/^\///;
1.697     albertel 9889:     $thisfn=~s|^adm/wrapper/||;
                   9890:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      9891:     $thisfn=~s/^res\///;
1.1032    raeburn  9892:     unless (($thisfn =~ /^ext/) || ($thisfn =~ /\.(page|sequence)___\d+___ext/)) {
                   9893:         $thisfn=~s/\?.+$//;
                   9894:     }
1.268     www      9895:     return $thisfn;
                   9896: }
                   9897: 
                   9898: # ------------------------------------------------------------- Clutter up URLs
                   9899: 
                   9900: sub clutter {
                   9901:     my $thisfn='/'.&declutter(shift);
1.887     albertel 9902:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 9903: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      9904:        $thisfn='/res'.$thisfn; 
                   9905:     }
1.1031    raeburn  9906:     if ($thisfn !~m|^/adm|) {
                   9907: 	if ($thisfn =~ m|^/ext/|) {
1.694     albertel 9908: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 9909: 	} else {
                   9910: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   9911: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 9912: 	    if ($embstyle eq 'ssi'
                   9913: 		|| ($embstyle eq 'hdn')
                   9914: 		|| ($embstyle eq 'rat')
                   9915: 		|| ($embstyle eq 'prv')
                   9916: 		|| ($embstyle eq 'ign')) {
                   9917: 		#do nothing with these
                   9918: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 9919: 		|| ($embstyle eq 'emb')
                   9920: 		|| ($embstyle eq 'wrp')) {
                   9921: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 9922: 	    } elsif ($embstyle eq 'unk'
                   9923: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 9924: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 9925: 	    } else {
1.718     www      9926: #		&logthis("Got a blank emb style");
1.695     albertel 9927: 	    }
1.694     albertel 9928: 	}
                   9929:     }
1.31      www      9930:     return $thisfn;
1.12      www      9931: }
                   9932: 
1.787     albertel 9933: sub clutter_with_no_wrapper {
                   9934:     my $uri = &clutter(shift);
                   9935:     if ($uri =~ m-^/adm/-) {
                   9936: 	$uri =~ s-^/adm/wrapper/-/-;
                   9937: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   9938:     }
                   9939:     return $uri;
                   9940: }
                   9941: 
1.557     albertel 9942: sub freeze_escape {
                   9943:     my ($value)=@_;
                   9944:     if (ref($value)) {
                   9945: 	$value=&nfreeze($value);
                   9946: 	return '__FROZEN__'.&escape($value);
                   9947:     }
                   9948:     return &escape($value);
                   9949: }
                   9950: 
1.11      www      9951: 
1.557     albertel 9952: sub thaw_unescape {
                   9953:     my ($value)=@_;
                   9954:     if ($value =~ /^__FROZEN__/) {
                   9955: 	substr($value,0,10,undef);
                   9956: 	$value=&unescape($value);
                   9957: 	return &thaw($value);
                   9958:     }
                   9959:     return &unescape($value);
                   9960: }
                   9961: 
1.436     albertel 9962: sub correct_line_ends {
                   9963:     my ($result)=@_;
                   9964:     $$result =~s/\r\n/\n/mg;
                   9965:     $$result =~s/\r/\n/mg;
1.415     albertel 9966: }
1.1       albertel 9967: # ================================================================ Main Program
                   9968: 
1.184     www      9969: sub goodbye {
1.204     albertel 9970:    &logthis("Starting Shut down");
1.443     albertel 9971: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 9972:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 9973: #converted
1.599     albertel 9974: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 9975:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   9976: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   9977: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 9978: #1.1 only
1.870     albertel 9979: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   9980: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   9981: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   9982: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   9983:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 9984:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   9985:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      9986:    &flushcourselogs();
                   9987:    &logthis("Shutting down");
                   9988: }
                   9989: 
1.852     albertel 9990: sub get_dns {
1.869     albertel 9991:     my ($url,$func,$ignore_cache) = @_;
                   9992:     if (!$ignore_cache) {
                   9993: 	my ($content,$cached)=
                   9994: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   9995: 	if ($cached) {
                   9996: 	    &$func($content);
                   9997: 	    return;
                   9998: 	}
                   9999:     }
                   10000: 
                   10001:     my %alldns;
1.852     albertel 10002:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10003:     foreach my $dns (<$config>) {
                   10004: 	next if ($dns !~ /^\^(\S*)/x);
1.979     raeburn  10005:         my $line = $1;
                   10006:         my ($host,$protocol) = split(/:/,$line);
                   10007:         if ($protocol ne 'https') {
                   10008:             $protocol = 'http';
                   10009:         }
                   10010: 	$alldns{$host} = $protocol;
1.869     albertel 10011:     }
                   10012:     while (%alldns) {
                   10013: 	my ($dns) = keys(%alldns);
1.852     albertel 10014: 	my $ua=new LWP::UserAgent;
1.979     raeburn  10015: 	my $request=new HTTP::Request('GET',"$alldns{$dns}://$dns$url");
1.852     albertel 10016: 	my $response=$ua->request($request);
1.979     raeburn  10017:         delete($alldns{$dns});
1.852     albertel 10018: 	next if ($response->is_error());
                   10019: 	my @content = split("\n",$response->content);
1.869     albertel 10020: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 10021: 	&$func(\@content);
1.869     albertel 10022: 	return;
1.852     albertel 10023:     }
                   10024:     close($config);
1.871     albertel 10025:     my $which = (split('/',$url))[3];
                   10026:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   10027:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 10028:     my @content = <$config>;
                   10029:     &$func(\@content);
                   10030:     return;
1.852     albertel 10031: }
1.327     albertel 10032: # ------------------------------------------------------------ Read domain file
                   10033: {
1.852     albertel 10034:     my $loaded;
1.846     albertel 10035:     my %domain;
                   10036: 
1.852     albertel 10037:     sub parse_domain_tab {
                   10038: 	my ($lines) = @_;
                   10039: 	foreach my $line (@$lines) {
                   10040: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      10041: 
1.846     albertel 10042: 	    chomp($line);
1.852     albertel 10043: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 10044: 	    my %this_domain;
                   10045: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   10046: 			       'lang_def', 'city', 'longi', 'lati',
                   10047: 			       'primary') {
                   10048: 		$this_domain{$field} = shift(@elements);
                   10049: 	    }
                   10050: 	    $domain{$name} = \%this_domain;
1.852     albertel 10051: 	}
                   10052:     }
1.864     albertel 10053: 
                   10054:     sub reset_domain_info {
                   10055: 	undef($loaded);
                   10056: 	undef(%domain);
                   10057:     }
                   10058: 
1.852     albertel 10059:     sub load_domain_tab {
1.869     albertel 10060: 	my ($ignore_cache) = @_;
                   10061: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 10062: 	my $fh;
                   10063: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   10064: 	    my @lines = <$fh>;
                   10065: 	    &parse_domain_tab(\@lines);
1.448     albertel 10066: 	}
1.852     albertel 10067: 	close($fh);
                   10068: 	$loaded = 1;
1.327     albertel 10069:     }
1.846     albertel 10070: 
                   10071:     sub domain {
1.852     albertel 10072: 	&load_domain_tab() if (!$loaded);
                   10073: 
1.846     albertel 10074: 	my ($name,$what) = @_;
                   10075: 	return if ( !exists($domain{$name}) );
                   10076: 
                   10077: 	if (!$what) {
                   10078: 	    return $domain{$name}{'description'};
                   10079: 	}
                   10080: 	return $domain{$name}{$what};
                   10081:     }
1.974     raeburn  10082: 
                   10083:     sub domain_info {
                   10084:         &load_domain_tab() if (!$loaded);
                   10085:         return %domain;
                   10086:     }
                   10087: 
1.327     albertel 10088: }
                   10089: 
                   10090: 
1.1       albertel 10091: # ------------------------------------------------------------- Read hosts file
                   10092: {
1.838     albertel 10093:     my %hostname;
1.844     albertel 10094:     my %hostdom;
1.845     albertel 10095:     my %libserv;
1.852     albertel 10096:     my $loaded;
1.888     albertel 10097:     my %name_to_host;
1.1074    raeburn  10098:     my %internetdom;
1.1107    raeburn  10099:     my %LC_dns_serv;
1.852     albertel 10100: 
                   10101:     sub parse_hosts_tab {
                   10102: 	my ($file) = @_;
                   10103: 	foreach my $configline (@$file) {
                   10104: 	    next if ($configline =~ /^(\#|\s*$ )/x);
1.1107    raeburn  10105:             chomp($configline);
                   10106: 	    if ($configline =~ /^\^/) {
                   10107:                 if ($configline =~ /^\^([\w.\-]+)/) {
                   10108:                     $LC_dns_serv{$1} = 1;
                   10109:                 }
                   10110:                 next;
                   10111:             }
1.1074    raeburn  10112: 	    my ($id,$domain,$role,$name,$protocol,$intdom)=split(/:/,$configline);
1.852     albertel 10113: 	    $name=~s/\s//g;
                   10114: 	    if ($id && $domain && $role && $name) {
                   10115: 		$hostname{$id}=$name;
1.888     albertel 10116: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 10117: 		$hostdom{$id}=$domain;
                   10118: 		if ($role eq 'library') { $libserv{$id}=$name; }
1.969     raeburn  10119:                 if (defined($protocol)) {
                   10120:                     if ($protocol eq 'https') {
                   10121:                         $protocol{$id} = $protocol;
                   10122:                     } else {
                   10123:                         $protocol{$id} = 'http'; 
                   10124:                     }
1.968     raeburn  10125:                 } else {
1.969     raeburn  10126:                     $protocol{$id} = 'http';
1.968     raeburn  10127:                 }
1.1074    raeburn  10128:                 if (defined($intdom)) {
                   10129:                     $internetdom{$id} = $intdom;
                   10130:                 }
1.852     albertel 10131: 	    }
                   10132: 	}
                   10133:     }
1.864     albertel 10134:     
                   10135:     sub reset_hosts_info {
1.897     albertel 10136: 	&purge_remembered();
1.864     albertel 10137: 	&reset_domain_info();
                   10138: 	&reset_hosts_ip_info();
1.892     albertel 10139: 	undef(%name_to_host);
1.864     albertel 10140: 	undef(%hostname);
                   10141: 	undef(%hostdom);
                   10142: 	undef(%libserv);
                   10143: 	undef($loaded);
                   10144:     }
1.1       albertel 10145: 
1.852     albertel 10146:     sub load_hosts_tab {
1.869     albertel 10147: 	my ($ignore_cache) = @_;
                   10148: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 10149: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   10150: 	my @config = <$config>;
                   10151: 	&parse_hosts_tab(\@config);
                   10152: 	close($config);
                   10153: 	$loaded=1;
1.1       albertel 10154:     }
1.852     albertel 10155: 
1.838     albertel 10156:     sub hostname {
1.852     albertel 10157: 	&load_hosts_tab() if (!$loaded);
                   10158: 
1.838     albertel 10159: 	my ($lonid) = @_;
                   10160: 	return $hostname{$lonid};
                   10161:     }
1.845     albertel 10162: 
1.838     albertel 10163:     sub all_hostnames {
1.852     albertel 10164: 	&load_hosts_tab() if (!$loaded);
                   10165: 
1.838     albertel 10166: 	return %hostname;
                   10167:     }
1.845     albertel 10168: 
1.888     albertel 10169:     sub all_names {
                   10170: 	&load_hosts_tab() if (!$loaded);
                   10171: 
                   10172: 	return %name_to_host;
                   10173:     }
                   10174: 
1.974     raeburn  10175:     sub all_host_domain {
                   10176:         &load_hosts_tab() if (!$loaded);
                   10177:         return %hostdom;
                   10178:     }
                   10179: 
1.845     albertel 10180:     sub is_library {
1.852     albertel 10181: 	&load_hosts_tab() if (!$loaded);
                   10182: 
1.845     albertel 10183: 	return exists($libserv{$_[0]});
                   10184:     }
                   10185: 
                   10186:     sub all_library {
1.852     albertel 10187: 	&load_hosts_tab() if (!$loaded);
                   10188: 
1.845     albertel 10189: 	return %libserv;
                   10190:     }
                   10191: 
1.1062    droeschl 10192:     sub unique_library {
                   10193: 	#2x reverse removes all hostnames that appear more than once
                   10194:         my %unique = reverse &all_library();
                   10195:         return reverse %unique;
                   10196:     }
                   10197: 
1.841     albertel 10198:     sub get_servers {
1.852     albertel 10199: 	&load_hosts_tab() if (!$loaded);
                   10200: 
1.841     albertel 10201: 	my ($domain,$type) = @_;
                   10202: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   10203: 	                                          : %hostname;
                   10204: 	my %result;
1.842     albertel 10205: 	if (ref($domain) eq 'ARRAY') {
                   10206: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 10207: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 10208: 		    $result{$host} = $hostname;
                   10209: 		}
                   10210: 	    }
                   10211: 	} else {
                   10212: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   10213: 		if ($hostdom{$host} eq $domain) {
                   10214: 		    $result{$host} = $hostname;
                   10215: 		}
1.841     albertel 10216: 	    }
                   10217: 	}
                   10218: 	return %result;
                   10219:     }
1.845     albertel 10220: 
1.1062    droeschl 10221:     sub get_unique_servers {
                   10222:         my %unique = reverse &get_servers(@_);
                   10223: 	return reverse %unique;
                   10224:     }
                   10225: 
1.844     albertel 10226:     sub host_domain {
1.852     albertel 10227: 	&load_hosts_tab() if (!$loaded);
                   10228: 
1.844     albertel 10229: 	my ($lonid) = @_;
                   10230: 	return $hostdom{$lonid};
                   10231:     }
                   10232: 
1.841     albertel 10233:     sub all_domains {
1.852     albertel 10234: 	&load_hosts_tab() if (!$loaded);
                   10235: 
1.841     albertel 10236: 	my %seen;
                   10237: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   10238: 	return @uniq;
                   10239:     }
1.1074    raeburn  10240: 
                   10241:     sub internet_dom {
                   10242:         &load_hosts_tab() if (!$loaded);
                   10243: 
                   10244:         my ($lonid) = @_;
                   10245:         return $internetdom{$lonid};
                   10246:     }
1.1107    raeburn  10247: 
                   10248:     sub is_LC_dns {
                   10249:         &load_hosts_tab() if (!$loaded);
                   10250: 
                   10251:         my ($hostname) = @_;
                   10252:         return exists($LC_dns_serv{$hostname});
                   10253:     }
                   10254: 
1.1       albertel 10255: }
                   10256: 
1.847     albertel 10257: { 
                   10258:     my %iphost;
1.856     albertel 10259:     my %name_to_ip;
                   10260:     my %lonid_to_ip;
1.869     albertel 10261: 
1.847     albertel 10262:     sub get_hosts_from_ip {
                   10263: 	my ($ip) = @_;
                   10264: 	my %iphosts = &get_iphost();
                   10265: 	if (ref($iphosts{$ip})) {
                   10266: 	    return @{$iphosts{$ip}};
                   10267: 	}
                   10268: 	return;
1.839     albertel 10269:     }
1.864     albertel 10270:     
                   10271:     sub reset_hosts_ip_info {
                   10272: 	undef(%iphost);
                   10273: 	undef(%name_to_ip);
                   10274: 	undef(%lonid_to_ip);
                   10275:     }
1.856     albertel 10276: 
                   10277:     sub get_host_ip {
                   10278: 	my ($lonid) = @_;
                   10279: 	if (exists($lonid_to_ip{$lonid})) {
                   10280: 	    return $lonid_to_ip{$lonid};
                   10281: 	}
                   10282: 	my $name=&hostname($lonid);
                   10283:    	my $ip = gethostbyname($name);
                   10284: 	return if (!$ip || length($ip) ne 4);
                   10285: 	$ip=inet_ntoa($ip);
                   10286: 	$name_to_ip{$name}   = $ip;
                   10287: 	$lonid_to_ip{$lonid} = $ip;
                   10288: 	return $ip;
                   10289:     }
1.847     albertel 10290:     
                   10291:     sub get_iphost {
1.869     albertel 10292: 	my ($ignore_cache) = @_;
1.894     albertel 10293: 
1.869     albertel 10294: 	if (!$ignore_cache) {
                   10295: 	    if (%iphost) {
                   10296: 		return %iphost;
                   10297: 	    }
                   10298: 	    my ($ip_info,$cached)=
                   10299: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   10300: 	    if ($cached) {
                   10301: 		%iphost      = %{$ip_info->[0]};
                   10302: 		%name_to_ip  = %{$ip_info->[1]};
                   10303: 		%lonid_to_ip = %{$ip_info->[2]};
                   10304: 		return %iphost;
                   10305: 	    }
                   10306: 	}
1.894     albertel 10307: 
                   10308: 	# get yesterday's info for fallback
                   10309: 	my %old_name_to_ip;
                   10310: 	my ($ip_info,$cached)=
                   10311: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   10312: 	if ($cached) {
                   10313: 	    %old_name_to_ip = %{$ip_info->[1]};
                   10314: 	}
                   10315: 
1.888     albertel 10316: 	my %name_to_host = &all_names();
                   10317: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 10318: 	    my $ip;
                   10319: 	    if (!exists($name_to_ip{$name})) {
                   10320: 		$ip = gethostbyname($name);
                   10321: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 10322: 		    if (defined($old_name_to_ip{$name})) {
                   10323: 			$ip = $old_name_to_ip{$name};
                   10324: 			&logthis("Can't find $name defaulting to old $ip");
                   10325: 		    } else {
                   10326: 			&logthis("Name $name no IP found");
                   10327: 			next;
                   10328: 		    }
                   10329: 		} else {
                   10330: 		    $ip=inet_ntoa($ip);
1.847     albertel 10331: 		}
                   10332: 		$name_to_ip{$name} = $ip;
                   10333: 	    } else {
                   10334: 		$ip = $name_to_ip{$name};
1.653     albertel 10335: 	    }
1.888     albertel 10336: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   10337: 		$lonid_to_ip{$id} = $ip;
                   10338: 	    }
                   10339: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 10340: 	}
1.869     albertel 10341: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   10342: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 10343: 				      48*60*60);
1.869     albertel 10344: 
1.847     albertel 10345: 	return %iphost;
1.598     albertel 10346:     }
                   10347: 
1.992     raeburn  10348:     #
                   10349:     #  Given a DNS returns the loncapa host name for that DNS 
                   10350:     # 
                   10351:     sub host_from_dns {
                   10352:         my ($dns) = @_;
                   10353:         my @hosts;
                   10354:         my $ip;
                   10355: 
1.993     raeburn  10356:         if (exists($name_to_ip{$dns})) {
1.992     raeburn  10357:             $ip = $name_to_ip{$dns};
                   10358:         }
                   10359:         if (!$ip) {
                   10360:             $ip = gethostbyname($dns); # Initial translation to IP is in net order.
                   10361:             if (length($ip) == 4) { 
                   10362: 	        $ip   = &IO::Socket::inet_ntoa($ip);
                   10363:             }
                   10364:         }
                   10365:         if ($ip) {
                   10366: 	    @hosts = get_hosts_from_ip($ip);
                   10367: 	    return $hosts[0];
                   10368:         }
                   10369:         return undef;
1.986     foxr     10370:     }
1.992     raeburn  10371: 
1.1074    raeburn  10372:     sub get_internet_names {
                   10373:         my ($lonid) = @_;
                   10374:         return if ($lonid eq '');
                   10375:         my ($idnref,$cached)=
                   10376:             &Apache::lonnet::is_cached_new('internetnames',$lonid);
                   10377:         if ($cached) {
                   10378:             return $idnref;
                   10379:         }
                   10380:         my $ip = &get_host_ip($lonid);
                   10381:         my @hosts = &get_hosts_from_ip($ip);
                   10382:         my %iphost = &get_iphost();
                   10383:         my (@idns,%seen);
                   10384:         foreach my $id (@hosts) {
                   10385:             my $dom = &host_domain($id);
                   10386:             my $prim_id = &domain($dom,'primary');
                   10387:             my $prim_ip = &get_host_ip($prim_id);
                   10388:             next if ($seen{$prim_ip});
                   10389:             if (ref($iphost{$prim_ip}) eq 'ARRAY') {
                   10390:                 foreach my $id (@{$iphost{$prim_ip}}) {
                   10391:                     my $intdom = &internet_dom($id);
                   10392:                     unless (grep(/^\Q$intdom\E$/,@idns)) {
                   10393:                         push(@idns,$intdom);
                   10394:                     }
                   10395:                 }
                   10396:             }
                   10397:             $seen{$prim_ip} = 1;
                   10398:         }
                   10399:         return &Apache::lonnet::do_cache_new('internetnames',$lonid,\@idns,12*60*60);
                   10400:     }
                   10401: 
1.986     foxr     10402: }
                   10403: 
1.1079    raeburn  10404: sub all_loncaparevs {
                   10405:     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);
                   10406: }
                   10407: 
1.862     albertel 10408: BEGIN {
                   10409: 
                   10410: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   10411:     unless ($readit) {
                   10412: {
                   10413:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   10414:     %perlvar = (%perlvar,%{$configvars});
                   10415: }
                   10416: 
                   10417: 
1.1       albertel 10418: # ------------------------------------------------------ Read spare server file
                   10419: {
1.448     albertel 10420:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 10421: 
                   10422:     while (my $configline=<$config>) {
                   10423:        chomp($configline);
1.284     matthew  10424:        if ($configline) {
1.784     albertel 10425: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 10426: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 10427: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 10428:        }
                   10429:     }
1.448     albertel 10430:     close($config);
1.1       albertel 10431: }
1.11      www      10432: # ------------------------------------------------------------ Read permissions
                   10433: {
1.448     albertel 10434:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      10435: 
                   10436:     while (my $configline=<$config>) {
1.448     albertel 10437: 	chomp($configline);
                   10438: 	if ($configline) {
                   10439: 	    my ($role,$perm)=split(/ /,$configline);
                   10440: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   10441: 	}
1.11      www      10442:     }
1.448     albertel 10443:     close($config);
1.11      www      10444: }
                   10445: 
                   10446: # -------------------------------------------- Read plain texts for permissions
                   10447: {
1.448     albertel 10448:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      10449: 
                   10450:     while (my $configline=<$config>) {
1.448     albertel 10451: 	chomp($configline);
                   10452: 	if ($configline) {
1.742     raeburn  10453: 	    my ($short,@plain)=split(/:/,$configline);
                   10454:             %{$prp{$short}} = ();
                   10455: 	    if (@plain > 0) {
                   10456:                 $prp{$short}{'std'} = $plain[0];
                   10457:                 for (my $i=1; $i<@plain; $i++) {
                   10458:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   10459:                 }
                   10460:             }
1.448     albertel 10461: 	}
1.135     www      10462:     }
1.448     albertel 10463:     close($config);
1.135     www      10464: }
                   10465: 
                   10466: # ---------------------------------------------------------- Read package table
                   10467: {
1.448     albertel 10468:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      10469: 
                   10470:     while (my $configline=<$config>) {
1.483     albertel 10471: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 10472: 	chomp($configline);
                   10473: 	my ($short,$plain)=split(/:/,$configline);
                   10474: 	my ($pack,$name)=split(/\&/,$short);
                   10475: 	if ($plain ne '') {
                   10476: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   10477: 	    $packagetab{$short}=$plain; 
                   10478: 	}
1.11      www      10479:     }
1.448     albertel 10480:     close($config);
1.329     matthew  10481: }
                   10482: 
1.1073    raeburn  10483: # ---------------------------------------------------------- Read loncaparev table
                   10484: {
                   10485:     if (-e "$perlvar{'lonTabDir'}/loncaparevs.tab") {
                   10486:         if (open(my $config,"<$perlvar{'lonTabDir'}/loncaparevs.tab")) {
                   10487:             while (my $configline=<$config>) {
                   10488:                 chomp($configline);
                   10489:                 my ($hostid,$loncaparev)=split(/:/,$configline);
                   10490:                 $loncaparevs{$hostid}=$loncaparev;
                   10491:             }
                   10492:             close($config);
                   10493:         }
                   10494:     }
                   10495: }
                   10496: 
1.1074    raeburn  10497: # ---------------------------------------------------------- Read serverhostID table
                   10498: {
                   10499:     if (-e "$perlvar{'lonTabDir'}/serverhomeIDs.tab") {
                   10500:         if (open(my $config,"<$perlvar{'lonTabDir'}/serverhomeIDs.tab")) {
                   10501:             while (my $configline=<$config>) {
                   10502:                 chomp($configline);
                   10503:                 my ($name,$id)=split(/:/,$configline);
                   10504:                 $serverhomeIDs{$name}=$id;
                   10505:             }
                   10506:             close($config);
                   10507:         }
                   10508:     }
                   10509: }
                   10510: 
1.1079    raeburn  10511: {
                   10512:     my $file = $Apache::lonnet::perlvar{'lonTabDir'}.'/releaseslist.xml';
                   10513:     if (-e $file) {
                   10514:         my $parser = HTML::LCParser->new($file);
                   10515:         while (my $token = $parser->get_token()) {
                   10516:             if ($token->[0] eq 'S') {
                   10517:                 my $item = $token->[1];
                   10518:                 my $name = $token->[2]{'name'};
                   10519:                 my $value = $token->[2]{'value'};
                   10520:                 if ($item ne '' && $name ne '' && $value ne '') {
                   10521:                     my $release = $parser->get_text();
                   10522:                     $release =~ s/(^\s*|\s*$ )//gx;
                   10523:                     $needsrelease{$item.':'.$name.':'.$value} = $release;
                   10524:                 }
                   10525:             }
                   10526:         }
                   10527:     }
1.1073    raeburn  10528: }
                   10529: 
1.329     matthew  10530: # ------------- set up temporary directory
                   10531: {
                   10532:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   10533: 
1.11      www      10534: }
                   10535: 
1.794     albertel 10536: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   10537: 				'compress_threshold'=> 20_000,
                   10538:  			        });
1.185     www      10539: 
1.281     www      10540: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      10541: $dumpcount=0;
1.958     www      10542: $locknum=0;
1.22      www      10543: 
1.163     harris41 10544: &logtouch();
1.672     albertel 10545: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      10546: $readit=1;
1.564     albertel 10547:     {
                   10548: 	use integer;
                   10549: 	my $test=(2**32)+1;
1.568     albertel 10550: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 10551: 	&logthis(" Detected 64bit platform ($_64bit)");
                   10552:     }
1.195     www      10553: }
1.1       albertel 10554: }
1.179     www      10555: 
1.1       albertel 10556: 1;
1.191     harris41 10557: __END__
                   10558: 
1.243     albertel 10559: =pod
                   10560: 
1.191     harris41 10561: =head1 NAME
                   10562: 
1.243     albertel 10563: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 10564: 
                   10565: =head1 SYNOPSIS
                   10566: 
1.243     albertel 10567: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 10568: 
                   10569:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   10570: 
1.243     albertel 10571: Common parameters:
                   10572: 
                   10573: =over 4
                   10574: 
                   10575: =item *
                   10576: 
                   10577: $uname : an internal username (if $cname expecting a course Id specifically)
                   10578: 
                   10579: =item *
                   10580: 
                   10581: $udom : a domain (if $cdom expecting a course's domain specifically)
                   10582: 
                   10583: =item *
                   10584: 
                   10585: $symb : a resource instance identifier
                   10586: 
                   10587: =item *
                   10588: 
                   10589: $namespace : the name of a .db file that contains the data needed or
                   10590: being set.
                   10591: 
                   10592: =back
                   10593: 
1.394     bowersj2 10594: =head1 OVERVIEW
1.191     harris41 10595: 
1.394     bowersj2 10596: lonnet provides subroutines which interact with the
                   10597: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   10598: about classes, users, and resources.
1.243     albertel 10599: 
                   10600: For many of these objects you can also use this to store data about
                   10601: them or modify them in various ways.
1.191     harris41 10602: 
1.394     bowersj2 10603: =head2 Symbs
1.191     harris41 10604: 
1.394     bowersj2 10605: To identify a specific instance of a resource, LON-CAPA uses symbols
                   10606: or "symbs"X<symb>. These identifiers are built from the URL of the
                   10607: map, the resource number of the resource in the map, and the URL of
                   10608: the resource itself. The latter is somewhat redundant, but might help
                   10609: if maps change.
                   10610: 
                   10611: An example is
                   10612: 
                   10613:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   10614: 
                   10615: The respective map entry is
                   10616: 
                   10617:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   10618:   title="Problem 2">
                   10619:  </resource>
                   10620: 
                   10621: Symbs are used by the random number generator, as well as to store and
                   10622: restore data specific to a certain instance of for example a problem.
                   10623: 
                   10624: =head2 Storing And Retrieving Data
                   10625: 
                   10626: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   10627: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   10628: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   10629: is is the non-critical message twin of cstore. These functions are for
                   10630: handlers to store a perl hash to a user's permanent data space in an
                   10631: easy manner, and to retrieve it again on another call. It is expected
                   10632: that a handler would use this once at the beginning to retrieve data,
                   10633: and then again once at the end to send only the new data back.
                   10634: 
                   10635: The data is stored in the user's data directory on the user's
                   10636: homeserver under the ID of the course.
                   10637: 
                   10638: The hash that is returned by restore will have all of the previous
                   10639: value for all of the elements of the hash.
                   10640: 
                   10641: Example:
                   10642: 
                   10643:  #creating a hash
                   10644:  my %hash;
                   10645:  $hash{'foo'}='bar';
                   10646: 
                   10647:  #storing it
                   10648:  &Apache::lonnet::cstore(\%hash);
                   10649: 
                   10650:  #changing a value
                   10651:  $hash{'foo'}='notbar';
                   10652: 
                   10653:  #adding a new value
                   10654:  $hash{'bar'}='foo';
                   10655:  &Apache::lonnet::cstore(\%hash);
                   10656: 
                   10657:  #retrieving the hash
                   10658:  my %history=&Apache::lonnet::restore();
                   10659: 
                   10660:  #print the hash
                   10661:  foreach my $key (sort(keys(%history))) {
                   10662:    print("\%history{$key} = $history{$key}");
                   10663:  }
                   10664: 
                   10665: Will print out:
1.191     harris41 10666: 
1.394     bowersj2 10667:  %history{1:foo} = bar
                   10668:  %history{1:keys} = foo:timestamp
                   10669:  %history{1:timestamp} = 990455579
                   10670:  %history{2:bar} = foo
                   10671:  %history{2:foo} = notbar
                   10672:  %history{2:keys} = foo:bar:timestamp
                   10673:  %history{2:timestamp} = 990455580
                   10674:  %history{bar} = foo
                   10675:  %history{foo} = notbar
                   10676:  %history{timestamp} = 990455580
                   10677:  %history{version} = 2
                   10678: 
                   10679: Note that the special hash entries C<keys>, C<version> and
                   10680: C<timestamp> were added to the hash. C<version> will be equal to the
                   10681: total number of versions of the data that have been stored. The
                   10682: C<timestamp> attribute will be the UNIX time the hash was
                   10683: stored. C<keys> is available in every historical section to list which
                   10684: keys were added or changed at a specific historical revision of a
                   10685: hash.
                   10686: 
                   10687: B<Warning>: do not store the hash that restore returns directly. This
                   10688: will cause a mess since it will restore the historical keys as if the
                   10689: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 10690: 
1.394     bowersj2 10691: Calling convention:
1.191     harris41 10692: 
1.394     bowersj2 10693:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   10694:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 10695: 
1.394     bowersj2 10696: For more detailed information, see lonnet specific documentation.
1.191     harris41 10697: 
1.394     bowersj2 10698: =head1 RETURN MESSAGES
1.191     harris41 10699: 
1.394     bowersj2 10700: =over 4
1.191     harris41 10701: 
1.394     bowersj2 10702: =item * B<con_lost>: unable to contact remote host
1.191     harris41 10703: 
1.394     bowersj2 10704: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   10705: when the connection is brought back up
1.191     harris41 10706: 
1.394     bowersj2 10707: =item * B<con_failed>: unable to contact remote host and unable to save message
                   10708: for later delivery
1.191     harris41 10709: 
1.967     bisitz   10710: =item * B<error:>: an error a occurred, a description of the error follows the :
1.191     harris41 10711: 
1.394     bowersj2 10712: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 10713: that was requested
1.191     harris41 10714: 
1.243     albertel 10715: =back
1.191     harris41 10716: 
1.243     albertel 10717: =head1 PUBLIC SUBROUTINES
1.191     harris41 10718: 
1.243     albertel 10719: =head2 Session Environment Functions
1.191     harris41 10720: 
1.243     albertel 10721: =over 4
1.191     harris41 10722: 
1.394     bowersj2 10723: =item * 
                   10724: X<appenv()>
1.949     raeburn  10725: B<appenv($hashref,$rolesarrayref)>: the value of %{$hashref} is written to
1.394     bowersj2 10726: the user envirnoment file, and will be restored for each access this
1.620     albertel 10727: user makes during this session, also modifies the %env for the current
1.949     raeburn  10728: process. Optional rolesarrayref - if defined contains a reference to an array
                   10729: of roles which are exempt from the restriction on modifying user.role entries 
                   10730: in the user's environment.db and in %env.    
1.191     harris41 10731: 
                   10732: =item *
1.394     bowersj2 10733: X<delenv()>
1.987     raeburn  10734: B<delenv($delthis,$regexp)>: removes all items from the session
                   10735: environment file that begin with $delthis. If the 
                   10736: optional second arg - $regexp - is true, $delthis is treated as a 
                   10737: regular expression, otherwise \Q$delthis\E is used. 
                   10738: The values are also deleted from the current processes %env.
1.191     harris41 10739: 
1.795     albertel 10740: =item * get_env_multiple($name) 
                   10741: 
                   10742: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   10743: values may be defined and end up as an array ref.
                   10744: 
                   10745: returns an array of values
                   10746: 
1.243     albertel 10747: =back
                   10748: 
                   10749: =head2 User Information
1.191     harris41 10750: 
1.243     albertel 10751: =over 4
1.191     harris41 10752: 
                   10753: =item *
1.394     bowersj2 10754: X<queryauthenticate()>
                   10755: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 10756: authentication scheme
                   10757: 
                   10758: =item *
1.394     bowersj2 10759: X<authenticate()>
1.1073    raeburn  10760: B<authenticate($uname,$upass,$udom,$checkdefauth,$clientcancheckhost)>: try to
1.394     bowersj2 10761: authenticate user from domain's lib servers (first use the current
                   10762: one). C<$upass> should be the users password.
1.1073    raeburn  10763: $checkdefauth is optional (value is 1 if a check should be made to
                   10764:    authenticate user using default authentication method, and allow
                   10765:    account creation if username does not have account in the domain).
                   10766: $clientcancheckhost is optional (value is 1 if checking whether the
                   10767:    server can host will occur on the client side in lonauth.pm).   
1.191     harris41 10768: 
                   10769: =item *
1.394     bowersj2 10770: X<homeserver()>
                   10771: B<homeserver($uname,$udom)>: find the server which has
                   10772: the user's directory and files (there must be only one), this caches
                   10773: the answer, and also caches if there is a borken connection.
1.191     harris41 10774: 
                   10775: =item *
1.394     bowersj2 10776: X<idget()>
                   10777: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   10778: (IDs are a unique resource in a domain, there must be only 1 ID per
                   10779: username, and only 1 username per ID in a specific domain) (returns
                   10780: hash: id=>name,id=>name)
1.191     harris41 10781: 
                   10782: =item *
1.394     bowersj2 10783: X<idrget()>
                   10784: B<idrget($udom,@unames)>: find the IDs behind a list of
                   10785: usernames (returns hash: name=>id,name=>id)
1.191     harris41 10786: 
                   10787: =item *
1.394     bowersj2 10788: X<idput()>
                   10789: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 10790: 
                   10791: =item *
1.394     bowersj2 10792: X<rolesinit()>
                   10793: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 10794: 
                   10795: =item *
1.551     albertel 10796: X<getsection()>
                   10797: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 10798: course $cname, return section name/number or '' for "not in course"
                   10799: and '-1' for "no section"
                   10800: 
                   10801: =item *
1.394     bowersj2 10802: X<userenvironment()>
                   10803: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 10804: passed in @what from the requested user's environment, returns a hash
                   10805: 
1.858     raeburn  10806: =item * 
                   10807: X<userlog_query()>
1.859     albertel 10808: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   10809: activity.log file. %filters defines filters applied when parsing the
                   10810: log file. These can be start or end timestamps, or the type of action
                   10811: - log to look for Login or Logout events, check for Checkin or
                   10812: Checkout, role for role selection. The response is in the form
                   10813: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   10814: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  10815: 
1.243     albertel 10816: =back
                   10817: 
                   10818: =head2 User Roles
                   10819: 
                   10820: =over 4
                   10821: 
                   10822: =item *
                   10823: 
1.810     raeburn  10824: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 10825:  F: full access
                   10826:  U,I,K: authentication modes (cxx only)
                   10827:  '': forbidden
                   10828:  1: user needs to choose course
                   10829:  2: browse allowed
1.766     albertel 10830:  A: passphrase authentication needed
1.243     albertel 10831: 
                   10832: =item *
                   10833: 
                   10834: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   10835: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   10836: and course level
                   10837: 
                   10838: =item *
                   10839: 
1.988     raeburn  10840: plaintext($short,$type,$cid,$forcedefault) : return value in %prp hash 
                   10841: (rolesplain.tab); plain text explanation of a user role term.
1.1008    raeburn  10842: $type is Course (default) or Community.
1.988     raeburn  10843: If $forcedefault evaluates to true, text returned will be default 
                   10844: text for $type. Otherwise, if this is a course, the text returned 
                   10845: will be a custom name for the role (if defined in the course's 
                   10846: environment).  If no custom name is defined the default is returned.
                   10847:    
1.832     raeburn  10848: =item *
                   10849: 
1.935     raeburn  10850: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms,$withsec) :
1.858     raeburn  10851: All arguments are optional. Returns a hash of a roles, either for
                   10852: co-author/assistant author roles for a user's Construction Space
1.906     albertel 10853: (default), or if $context is 'userroles', roles for the user himself,
1.933     raeburn  10854: In the hash, keys are set to colon-separated $uname,$udom,$role, and
                   10855: (optionally) if $withsec is true, a fourth colon-separated item - $section.
                   10856: For each key, value is set to colon-separated start and end times for
                   10857: the role.  If no username and domain are specified, will default to
1.934     raeburn  10858: current user/domain. Types, roles, and roledoms are references to arrays
1.858     raeburn  10859: of role statuses (active, future or previous), roles 
                   10860: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   10861: to restrict the list of roles reported. If no array ref is 
                   10862: provided for types, will default to return only active roles.
1.834     albertel 10863: 
1.243     albertel 10864: =back
                   10865: 
                   10866: =head2 User Modification
                   10867: 
                   10868: =over 4
                   10869: 
                   10870: =item *
                   10871: 
1.957     raeburn  10872: assignrole($udom,$uname,$url,$role,$end,$start,$deleteflag,$selfenroll,$context) : assign role; give a role to a
1.243     albertel 10873: user for the level given by URL.  Optional start and end dates (leave empty
                   10874: string or zero for "no date")
1.191     harris41 10875: 
                   10876: =item *
                   10877: 
1.243     albertel 10878: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   10879: change a users, password, possible return values are: ok,
                   10880: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   10881: refused
1.191     harris41 10882: 
                   10883: =item *
                   10884: 
1.243     albertel 10885: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 10886: 
                   10887: =item *
                   10888: 
1.1058    raeburn  10889: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last, $gene,
                   10890:            $forceid,$desiredhome,$email,$inststatus,$candelete) :
                   10891: 
                   10892: will update user information (firstname,middlename,lastname,generation,
                   10893: permanentemail), and if forceid is true, student/employee ID also.
                   10894: A user's institutional affiliation(s) can also be updated.
                   10895: User information fields will not be overwritten with empty entries 
                   10896: unless the field is included in the $candelete array reference.
                   10897: This array is included when a single user is modified via "Manage Users",
                   10898: or when Autoupdate.pl is run by cron in a domain.
1.191     harris41 10899: 
                   10900: =item *
                   10901: 
1.286     matthew  10902: modifystudent
                   10903: 
1.957     raeburn  10904: modify a student's enrollment and identification information.
1.286     matthew  10905: The course id is resolved based on the current users environment.  
                   10906: This means the envoking user must be a course coordinator or otherwise
                   10907: associated with a course.
                   10908: 
1.297     matthew  10909: This call is essentially a wrapper for lonnet::modifyuser and
                   10910: lonnet::modify_student_enrollment
1.286     matthew  10911: 
                   10912: Inputs: 
                   10913: 
                   10914: =over 4
                   10915: 
1.957     raeburn  10916: =item B<$udom> Student's loncapa domain
1.286     matthew  10917: 
1.957     raeburn  10918: =item B<$uname> Student's loncapa login name
1.286     matthew  10919: 
1.964     bisitz   10920: =item B<$uid> Student/Employee ID
1.286     matthew  10921: 
1.957     raeburn  10922: =item B<$umode> Student's authentication mode
1.286     matthew  10923: 
1.957     raeburn  10924: =item B<$upass> Student's password
1.286     matthew  10925: 
1.957     raeburn  10926: =item B<$first> Student's first name
1.286     matthew  10927: 
1.957     raeburn  10928: =item B<$middle> Student's middle name
1.286     matthew  10929: 
1.957     raeburn  10930: =item B<$last> Student's last name
1.286     matthew  10931: 
1.957     raeburn  10932: =item B<$gene> Student's generation
1.286     matthew  10933: 
1.957     raeburn  10934: =item B<$usec> Student's section in course
1.286     matthew  10935: 
                   10936: =item B<$end> Unix time of the roles expiration
                   10937: 
                   10938: =item B<$start> Unix time of the roles start date
                   10939: 
                   10940: =item B<$forceid> If defined, allow $uid to be changed
                   10941: 
                   10942: =item B<$desiredhome> server to use as home server for student
                   10943: 
1.957     raeburn  10944: =item B<$email> Student's permanent e-mail address
                   10945: 
                   10946: =item B<$type> Type of enrollment (auto or manual)
                   10947: 
1.963     raeburn  10948: =item B<$locktype> boolean - enrollment type locked to prevent Autoenroll.pl changing manual to auto    
                   10949: 
                   10950: =item B<$cid> courseID - needed if a course role is assigned by a user whose current role is DC
1.957     raeburn  10951: 
1.963     raeburn  10952: =item B<$selfenroll> boolean - 1 if user role change occurred via self-enrollment
1.957     raeburn  10953: 
1.963     raeburn  10954: =item B<$context> role change context (shown in User Management Logs display in a course)
1.957     raeburn  10955: 
1.963     raeburn  10956: =item B<$inststatus> institutional status of user - : separated string of escaped status types  
1.957     raeburn  10957: 
1.286     matthew  10958: =back
1.297     matthew  10959: 
                   10960: =item *
                   10961: 
                   10962: modify_student_enrollment
                   10963: 
                   10964: Change a students enrollment status in a class.  The environment variable
                   10965: 'role.request.course' must be defined for this function to proceed.
                   10966: 
                   10967: Inputs:
                   10968: 
                   10969: =over 4
                   10970: 
                   10971: =item $udom, students domain
                   10972: 
                   10973: =item $uname, students name
                   10974: 
                   10975: =item $uid, students user id
                   10976: 
                   10977: =item $first, students first name
                   10978: 
                   10979: =item $middle
                   10980: 
                   10981: =item $last
                   10982: 
                   10983: =item $gene
                   10984: 
                   10985: =item $usec
                   10986: 
                   10987: =item $end
                   10988: 
                   10989: =item $start
                   10990: 
1.957     raeburn  10991: =item $type
                   10992: 
                   10993: =item $locktype
                   10994: 
                   10995: =item $cid
                   10996: 
                   10997: =item $selfenroll
                   10998: 
                   10999: =item $context
                   11000: 
1.297     matthew  11001: =back
                   11002: 
1.191     harris41 11003: 
                   11004: =item *
                   11005: 
1.243     albertel 11006: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   11007: custom role; give a custom role to a user for the level given by URL.  Specify
                   11008: name and domain of role author, and role name
1.191     harris41 11009: 
                   11010: =item *
                   11011: 
1.243     albertel 11012: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 11013: 
                   11014: =item *
                   11015: 
1.243     albertel 11016: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   11017: 
                   11018: =back
                   11019: 
                   11020: =head2 Course Infomation
                   11021: 
                   11022: =over 4
1.191     harris41 11023: 
                   11024: =item *
                   11025: 
1.631     albertel 11026: coursedescription($courseid) : returns a hash of information about the
                   11027: specified course id, including all environment settings for the
                   11028: course, the description of the course will be in the hash under the
                   11029: key 'description'
1.191     harris41 11030: 
                   11031: =item *
                   11032: 
1.624     albertel 11033: resdata($name,$domain,$type,@which) : request for current parameter
                   11034: setting for a specific $type, where $type is either 'course' or 'user',
                   11035: @what should be a list of parameters to ask about. This routine caches
                   11036: answers for 5 minutes.
1.243     albertel 11037: 
1.877     foxr     11038: =item *
                   11039: 
                   11040: get_courseresdata($courseid, $domain) : dump the entire course resource
                   11041: data base, returning a hash that is keyed by the resource name and has
                   11042: values that are the resource value.  I believe that the timestamps and
                   11043: versions are also returned.
                   11044: 
                   11045: 
1.243     albertel 11046: =back
                   11047: 
                   11048: =head2 Course Modification
                   11049: 
                   11050: =over 4
1.191     harris41 11051: 
                   11052: =item *
                   11053: 
1.243     albertel 11054: writecoursepref($courseid,%prefs) : write preferences (environment
                   11055: database) for a course
1.191     harris41 11056: 
                   11057: =item *
                   11058: 
1.1011    raeburn  11059: createcourse($udom,$description,$url,$course_server,$nonstandard,$inst_code,$course_owner,$crstype,$cnum) : make course
                   11060: 
                   11061: =item *
                   11062: 
1.1038    raeburn  11063: generate_coursenum($udom,$crstype) : get a unique (unused) course number in domain $udom for course type $crstype (Course or Community).
1.243     albertel 11064: 
                   11065: =back
                   11066: 
                   11067: =head2 Resource Subroutines
                   11068: 
                   11069: =over 4
1.191     harris41 11070: 
                   11071: =item *
                   11072: 
1.243     albertel 11073: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 11074: 
                   11075: =item *
                   11076: 
1.243     albertel 11077: repcopy($filename) : subscribes to the requested file, and attempts to
                   11078: replicate from the owning library server, Might return
1.607     raeburn  11079: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   11080: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 11081: resource. Expects the local filesystem pathname
                   11082: (/home/httpd/html/res/....)
                   11083: 
                   11084: =back
                   11085: 
                   11086: =head2 Resource Information
                   11087: 
                   11088: =over 4
1.191     harris41 11089: 
                   11090: =item *
                   11091: 
1.243     albertel 11092: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   11093: a vairety of different possible values, $varname should be a request
                   11094: string, and the other parameters can be used to specify who and what
                   11095: one is asking about.
                   11096: 
                   11097: Possible values for $varname are environment.lastname (or other item
                   11098: from the envirnment hash), user.name (or someother aspect about the
                   11099: user), resource.0.maxtries (or some other part and parameter of a
                   11100: resource)
1.204     albertel 11101: 
                   11102: =item *
                   11103: 
1.243     albertel 11104: directcondval($number) : get current value of a condition; reads from a state
                   11105: string
1.204     albertel 11106: 
                   11107: =item *
                   11108: 
1.243     albertel 11109: condval($condidx) : value of condition index based on state
1.204     albertel 11110: 
                   11111: =item *
                   11112: 
1.243     albertel 11113: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   11114: resource's metadata, $what should be either a specific key, or either
                   11115: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   11116: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   11117: 
                   11118: this function automatically caches all requests
1.191     harris41 11119: 
                   11120: =item *
                   11121: 
1.243     albertel 11122: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   11123: network of library servers; returns file handle of where SQL and regex results
                   11124: will be stored for query
1.191     harris41 11125: 
                   11126: =item *
                   11127: 
1.243     albertel 11128: symbread($filename) : return symbolic list entry (filename argument optional);
                   11129: returns the data handle
1.191     harris41 11130: 
                   11131: =item *
                   11132: 
1.243     albertel 11133: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 11134: a possible symb for the URL in $thisfn, and if is an encryypted
                   11135: resource that the user accessed using /enc/ returns a 1 on success, 0
                   11136: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 11137: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 11138: 
1.191     harris41 11139: 
                   11140: =item *
                   11141: 
1.243     albertel 11142: symbclean($symb) : removes versions numbers from a symb, returns the
                   11143: cleaned symb
1.191     harris41 11144: 
                   11145: =item *
                   11146: 
1.243     albertel 11147: is_on_map($uri) : checks if the $uri is somewhere on the current
                   11148: course map, user must be in a course for it to work.
1.191     harris41 11149: 
                   11150: =item *
                   11151: 
1.243     albertel 11152: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 11153: 
                   11154: =item *
                   11155: 
1.243     albertel 11156: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   11157: a random seed, all arguments are optional, if they aren't sent it uses the
                   11158: environment to derive them. Note: if symb isn't sent and it can't get one
                   11159: from &symbread it will use the current time as its return value
1.191     harris41 11160: 
                   11161: =item *
                   11162: 
1.243     albertel 11163: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   11164: unfakeable, receipt
1.191     harris41 11165: 
                   11166: =item *
                   11167: 
1.620     albertel 11168: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 11169: 
                   11170: =item *
                   11171: 
1.243     albertel 11172: countacc($url) : count the number of accesses to a given URL
1.191     harris41 11173: 
                   11174: =item *
                   11175: 
1.243     albertel 11176: 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 11177: 
                   11178: =item *
                   11179: 
1.243     albertel 11180: 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 11181: 
                   11182: =item *
                   11183: 
1.243     albertel 11184: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 11185: 
                   11186: =item *
                   11187: 
1.243     albertel 11188: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   11189: forcing spreadsheet to reevaluate the resource scores next time.
                   11190: 
                   11191: =back
                   11192: 
                   11193: =head2 Storing/Retreiving Data
                   11194: 
                   11195: =over 4
1.191     harris41 11196: 
                   11197: =item *
                   11198: 
1.243     albertel 11199: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   11200: for this url; hashref needs to be given and should be a \%hashname; the
                   11201: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 11202: be derived from the env
1.191     harris41 11203: 
                   11204: =item *
                   11205: 
1.243     albertel 11206: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   11207: uses critical subroutine
1.191     harris41 11208: 
                   11209: =item *
                   11210: 
1.243     albertel 11211: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   11212: all args are optional
1.191     harris41 11213: 
                   11214: =item *
                   11215: 
1.717     albertel 11216: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   11217: dumps the complete (or key matching regexp) namespace into a hash
                   11218: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   11219: normally &store()ed into
                   11220: 
                   11221: $range should be either an integer '100' (give me the first 100
                   11222:                                            matching records)
                   11223:               or be  two integers sperated by a - with no spaces
                   11224:                  '30-50' (give me the 30th through the 50th matching
                   11225:                           records)
                   11226: 
                   11227: 
                   11228: =item *
                   11229: 
                   11230: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   11231: replaces a &store() version of data with a replacement set of data
                   11232: for a particular resource in a namespace passed in the $storehash hash 
                   11233: reference
                   11234: 
                   11235: =item *
                   11236: 
1.243     albertel 11237: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   11238: works very similar to store/cstore, but all data is stored in a
                   11239: temporary location and can be reset using tmpreset, $storehash should
                   11240: be a hash reference, returns nothing on success
1.191     harris41 11241: 
                   11242: =item *
                   11243: 
1.243     albertel 11244: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   11245: similar to restore, but all data is stored in a temporary location and
                   11246: can be reset using tmpreset. Returns a hash of values on success,
                   11247: error string otherwise.
1.191     harris41 11248: 
                   11249: =item *
                   11250: 
1.243     albertel 11251: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   11252: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 11253: 
                   11254: =item *
                   11255: 
1.243     albertel 11256: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11257: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 11258: 
                   11259: =item *
                   11260: 
1.243     albertel 11261: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   11262: namesp ($udom and $uname are optional)
1.191     harris41 11263: 
                   11264: =item *
                   11265: 
1.702     albertel 11266: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 11267: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 11268: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  11269: 
1.702     albertel 11270: $range should be either an integer '100' (give me the first 100
                   11271:                                            matching records)
                   11272:               or be  two integers sperated by a - with no spaces
                   11273:                  '30-50' (give me the 30th through the 50th matching
                   11274:                           records)
1.449     matthew  11275: =item *
                   11276: 
                   11277: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   11278: $store can be a scalar, an array reference, or if the amount to be 
                   11279: incremented is > 1, a hash reference.
                   11280: 
                   11281: ($udom and $uname are optional)
1.191     harris41 11282: 
                   11283: =item *
                   11284: 
1.243     albertel 11285: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   11286: ($udom and $uname are optional)
1.191     harris41 11287: 
                   11288: =item *
                   11289: 
1.243     albertel 11290: cput($namespace,$storehash,$udom,$uname) : critical put
                   11291: ($udom and $uname are optional)
1.191     harris41 11292: 
                   11293: =item *
                   11294: 
1.748     albertel 11295: newput($namespace,$storehash,$udom,$uname) :
                   11296: 
                   11297: Attempts to store the items in the $storehash, but only if they don't
                   11298: currently exist, if this succeeds you can be certain that you have 
                   11299: successfully created a new key value pair in the $namespace db.
                   11300: 
                   11301: 
                   11302: Args:
                   11303:  $namespace: name of database to store values to
                   11304:  $storehash: hashref to store to the db
                   11305:  $udom: (optional) domain of user containing the db
                   11306:  $uname: (optional) name of user caontaining the db
                   11307: 
                   11308: Returns:
                   11309:  'ok' -> succeeded in storing all keys of $storehash
                   11310:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   11311:                         least <key> already existed in the db (other
                   11312:                         requested keys may also already exist)
1.967     bisitz   11313:  'error: <msg>' -> unable to tie the DB or other error occurred
1.748     albertel 11314:  'con_lost' -> unable to contact request server
                   11315:  'refused' -> action was not allowed by remote machine
                   11316: 
                   11317: 
                   11318: =item *
                   11319: 
1.243     albertel 11320: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   11321: reference filled in from namesp (encrypts the return communication)
                   11322: ($udom and $uname are optional)
1.191     harris41 11323: 
                   11324: =item *
                   11325: 
1.243     albertel 11326: log($udom,$name,$home,$message) : write to permanent log for user; use
                   11327: critical subroutine
                   11328: 
1.806     raeburn  11329: =item *
                   11330: 
1.860     raeburn  11331: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   11332: array reference filled in from namespace found in domain level on either
                   11333: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  11334: 
                   11335: =item *
                   11336: 
1.860     raeburn  11337: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   11338: domain level either on specified domain server ($uhome) or primary domain 
                   11339: server ($udom and $uhome are optional)
1.806     raeburn  11340: 
1.943     raeburn  11341: =item * 
                   11342: 
                   11343: get_domain_defaults($target_domain) : returns hash with defaults for
                   11344: authentication and language in the domain. Keys are: auth_def, auth_arg_def,
                   11345: lang_def; corresponsing values are authentication type (internal, krb4, krb5,
                   11346: or localauth), initial password or a kerberos realm, language (e.g., en-us).
                   11347: Values are retrieved from cache (if current), or from domain's configuration.db
                   11348: (if available), or lastly from values in lonTabs/dns_domain,tab, 
                   11349: or lonTabs/domain.tab. 
                   11350: 
                   11351: %domdefaults = &get_auth_defaults($target_domain);
                   11352: 
1.243     albertel 11353: =back
                   11354: 
                   11355: =head2 Network Status Functions
                   11356: 
                   11357: =over 4
1.191     harris41 11358: 
                   11359: =item *
                   11360: 
                   11361: dirlist($uri) : return directory list based on URI
                   11362: 
                   11363: =item *
                   11364: 
1.243     albertel 11365: spareserver() : find server with least workload from spare.tab
                   11366: 
1.986     foxr     11367: 
                   11368: =item *
                   11369: 
                   11370: host_from_dns($dns) : Returns the loncapa hostname corresponding to a DNS name or undef
                   11371: if there is no corresponding loncapa host.
                   11372: 
1.243     albertel 11373: =back
                   11374: 
1.986     foxr     11375: 
1.243     albertel 11376: =head2 Apache Request
                   11377: 
                   11378: =over 4
1.191     harris41 11379: 
                   11380: =item *
                   11381: 
1.243     albertel 11382: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   11383: localhost, posts hash
                   11384: 
                   11385: =back
                   11386: 
                   11387: =head2 Data to String to Data
                   11388: 
                   11389: =over 4
1.191     harris41 11390: 
                   11391: =item *
                   11392: 
1.243     albertel 11393: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   11394: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 11395: 
                   11396: =item *
                   11397: 
1.243     albertel 11398: hashref2str($hashref) : convert a hashref into a string complete with
                   11399: escaping and '=' and '&' separators, supports elements that are
                   11400: arrayrefs and hashrefs
1.191     harris41 11401: 
                   11402: =item *
                   11403: 
1.243     albertel 11404: arrayref2str($arrayref) : convert an arrayref into a string complete
                   11405: with escaping and '&' separators, supports elements that are arrayrefs
                   11406: and hashrefs
1.191     harris41 11407: 
                   11408: =item *
                   11409: 
1.243     albertel 11410: str2hash($string) : convert string to hash using unescaping and
                   11411: splitting on '=' and '&', supports elements that are arrayrefs and
                   11412: hashrefs
1.191     harris41 11413: 
                   11414: =item *
                   11415: 
1.243     albertel 11416: str2array($string) : convert string to hash using unescaping and
                   11417: splitting on '&', supports elements that are arrayrefs and hashrefs
                   11418: 
                   11419: =back
                   11420: 
                   11421: =head2 Logging Routines
                   11422: 
                   11423: =over 4
                   11424: 
                   11425: These routines allow one to make log messages in the lonnet.log and
                   11426: lonnet.perm logfiles.
1.191     harris41 11427: 
                   11428: =item *
                   11429: 
1.243     albertel 11430: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 11431: 
                   11432: =item *
                   11433: 
1.243     albertel 11434: logthis() : append message to the normal lonnet.log file, it gets
                   11435: preiodically rolled over and deleted.
1.191     harris41 11436: 
                   11437: =item *
                   11438: 
1.243     albertel 11439: logperm() : append a permanent message to lonnet.perm.log, this log
                   11440: file never gets deleted by any automated portion of the system, only
                   11441: messages of critical importance should go in here.
                   11442: 
                   11443: =back
                   11444: 
                   11445: =head2 General File Helper Routines
                   11446: 
                   11447: =over 4
1.191     harris41 11448: 
                   11449: =item *
                   11450: 
1.481     raeburn  11451: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   11452: (a) files in /uploaded
                   11453:   (i) If a local copy of the file exists - 
                   11454:       compares modification date of local copy with last-modified date for 
                   11455:       definitive version stored on home server for course. If local copy is 
                   11456:       stale, requests a new version from the home server and stores it. 
                   11457:       If the original has been removed from the home server, then local copy 
                   11458:       is unlinked.
                   11459:   (ii) If local copy does not exist -
                   11460:       requests the file from the home server and stores it. 
                   11461:   
                   11462:   If $caller is 'uploadrep':  
                   11463:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   11464:     for request for files originally uploaded via DOCS. 
                   11465:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   11466:   
                   11467:   Otherwise:
                   11468:      This indicates a call from the content generation phase of the request.
                   11469:      -  returns the entire contents of the file or -1.
                   11470:      
                   11471: (b) files in /res
                   11472:    - returns the entire contents of a file or -1; 
                   11473:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 11474: 
1.712     albertel 11475: 
                   11476: =item *
                   11477: 
                   11478: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   11479:                   reference
                   11480: 
                   11481: returns either a stat() list of data about the file or an empty list
                   11482: if the file doesn't exist or couldn't find out about it (connection
                   11483: problems or user unknown)
                   11484: 
1.191     harris41 11485: =item *
                   11486: 
1.243     albertel 11487: filelocation($dir,$file) : returns file system location of a file
                   11488: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   11489: directory that relative $file lookups are to looked in ($dir of /a/dir
                   11490: and a file of ../bob will become /a/bob)
1.191     harris41 11491: 
                   11492: =item *
                   11493: 
                   11494: hreflocation($dir,$file) : returns file system location or a URL; same as
                   11495: filelocation except for hrefs
                   11496: 
                   11497: =item *
                   11498: 
                   11499: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   11500: 
1.243     albertel 11501: =back
                   11502: 
1.608     albertel 11503: =head2 Usererfile file routines (/uploaded*)
                   11504: 
                   11505: =over 4
                   11506: 
                   11507: =item *
                   11508: 
                   11509: userfileupload(): main rotine for putting a file in a user or course's
                   11510:                   filespace, arguments are,
                   11511: 
1.620     albertel 11512:  formname - required - this is the name of the element in $env where the
1.608     albertel 11513:            filename, and the contents of the file to create/modifed exist
1.620     albertel 11514:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   11515:            contents of the file is located in $env{'form.'.$formname}
1.1090    raeburn  11516:  context - if coursedoc, store the file in the course of the active role
                   11517:              of the current user; 
                   11518:            if 'existingfile': store in 'overwrites' in /home/httpd/perl/tmp
                   11519:            if 'canceloverwrite': delete file in tmp/overwrites directory
1.608     albertel 11520:  subdir - required - subdirectory to put the file in under ../userfiles/
                   11521:          if undefined, it will be placed in "unknown"
                   11522: 
                   11523:  (This routine calls clean_filename() to remove any dangerous
                   11524:  characters from the filename, and then calls finuserfileupload() to
                   11525:  complete the transaction)
                   11526: 
                   11527:  returns either the url of the uploaded file (/uploaded/....) if successful
                   11528:  and /adm/notfound.html if unsuccessful
                   11529: 
                   11530: =item *
                   11531: 
                   11532: clean_filename(): routine for cleaing a filename up for storage in
                   11533:                  userfile space, argument is:
                   11534: 
                   11535:  filename - proposed filename
                   11536: 
                   11537: returns: the new clean filename
                   11538: 
                   11539: =item *
                   11540: 
1.1090    raeburn  11541: finishuserfileupload(): routine that creates and sends the file to
1.608     albertel 11542: userspace, probably shouldn't be called directly
                   11543: 
                   11544:   docuname: username or courseid of destination for the file
                   11545:   docudom: domain of user/course of destination for the file
                   11546:   formname: same as for userfileupload()
1.1090    raeburn  11547:   fname: filename (including subdirectories) for the file
                   11548:   parser: if 'parse', will parse (html) file to extract references to objects, links etc.
                   11549:   allfiles: reference to hash used to store objects found by parser
                   11550:   codebase: reference to hash used for codebases of java objects found by parser
                   11551:   thumbwidth: width (pixels) of thumbnail to be created for uploaded image
                   11552:   thumbheight: height (pixels) of thumbnail to be created for uploaded image
                   11553:   resizewidth: width to be used to resize image using resizeImage from ImageMagick
                   11554:   resizeheight: height to be used to resize image using resizeImage from ImageMagick
                   11555:   context: if 'overwrite', will move the uploaded file from its temporary location to
                   11556:             userfiles to facilitate overwriting a previously uploaded file with same name.
1.1095    raeburn  11557:   mimetype: reference to scalar to accommodate mime type determined
                   11558:             from File::MMagic if $parser = parse.
1.608     albertel 11559: 
                   11560:  returns either the url of the uploaded file (/uploaded/....) if successful
1.1090    raeburn  11561:  and /adm/notfound.html if unsuccessful (or an error message if context 
                   11562:  was 'overwrite').
                   11563:  
1.608     albertel 11564: 
                   11565: =item *
                   11566: 
                   11567: renameuserfile(): renames an existing userfile to a new name
                   11568: 
                   11569:   Args:
                   11570:    docuname: username or courseid of destination for the file
                   11571:    docudom: domain of user/course of destination for the file
                   11572:    old: current file name (including any subdirs under userfiles)
                   11573:    new: desired file name (including any subdirs under userfiles)
                   11574: 
                   11575: =item *
                   11576: 
                   11577: mkdiruserfile(): creates a directory is a userfiles dir
                   11578: 
                   11579:   Args:
                   11580:    docuname: username or courseid of destination for the file
                   11581:    docudom: domain of user/course of destination for the file
                   11582:    dir: dir to create (including any subdirs under userfiles)
                   11583: 
                   11584: =item *
                   11585: 
                   11586: removeuserfile(): removes a file that exists in userfiles
                   11587: 
                   11588:   Args:
                   11589:    docuname: username or courseid of destination for the file
                   11590:    docudom: domain of user/course of destination for the file
                   11591:    fname: filname to delete (including any subdirs under userfiles)
                   11592: 
                   11593: =item *
                   11594: 
                   11595: removeuploadedurl(): convience function for removeuserfile()
                   11596: 
                   11597:   Args:
                   11598:    url:  a full /uploaded/... url to delete
                   11599: 
1.747     albertel 11600: =item * 
                   11601: 
                   11602: get_portfile_permissions():
                   11603:   Args:
                   11604:     domain: domain of user or course contain the portfolio files
                   11605:     user: name of user or num of course contain the portfolio files
                   11606:   Returns:
                   11607:     hashref of a dump of the proper file_permissions.db
                   11608:    
                   11609: 
                   11610: =item * 
                   11611: 
                   11612: get_access_controls():
                   11613: 
                   11614: Args:
                   11615:   current_permissions: the hash ref returned from get_portfile_permissions()
                   11616:   group: (optional) the group you want the files associated with
                   11617:   file: (optional) the file you want access info on
                   11618: 
                   11619: Returns:
1.749     raeburn  11620:     a hash (keys are file names) of hashes containing
                   11621:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   11622:         values are XML containing access control settings (see below) 
1.747     albertel 11623: 
                   11624: Internal notes:
                   11625: 
1.749     raeburn  11626:  access controls are stored in file_permissions.db as key=value pairs.
                   11627:     key -> path to file/file_name\0uniqueID:scope_end_start
                   11628:         where scope -> public,guest,course,group,domains or users.
                   11629:               end -> UNIX time for end of access (0 -> no end date)
                   11630:               start -> UNIX time for start of access
                   11631: 
                   11632:     value -> XML description of access control
                   11633:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   11634:             <start></start>
                   11635:             <end></end>
                   11636: 
                   11637:             <password></password>  for scope type = guest
                   11638: 
                   11639:             <domain></domain>     for scope type = course or group
                   11640:             <number></number>
                   11641:             <roles id="">
                   11642:              <role></role>
                   11643:              <access></access>
                   11644:              <section></section>
                   11645:              <group></group>
                   11646:             </roles>
                   11647: 
                   11648:             <dom></dom>         for scope type = domains
                   11649: 
                   11650:             <users>             for scope type = users
                   11651:              <user>
                   11652:               <uname></uname>
                   11653:               <udom></udom>
                   11654:              </user>
                   11655:             </users>
                   11656:            </scope> 
                   11657:               
                   11658:  Access data is also aggregated for each file in an additional key=value pair:
                   11659:  key -> path to file/file_name\0accesscontrol 
                   11660:  value -> reference to hash
                   11661:           hash contains key = value pairs
                   11662:           where key = uniqueID:scope_end_start
                   11663:                 value = UNIX time record was last updated
                   11664: 
                   11665:           Used to improve speed of look-ups of access controls for each file.  
                   11666:  
                   11667:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   11668: 
                   11669: modify_access_controls():
                   11670: 
                   11671: Modifies access controls for a portfolio file
                   11672: Args
                   11673: 1. file name
                   11674: 2. reference to hash of required changes,
                   11675: 3. domain
                   11676: 4. username
                   11677:   where domain,username are the domain of the portfolio owner 
                   11678:   (either a user or a course) 
                   11679: 
                   11680: Returns:
                   11681: 1. result of additions or updates ('ok' or 'error', with error message). 
                   11682: 2. result of deletions ('ok' or 'error', with error message).
                   11683: 3. reference to hash of any new or updated access controls.
                   11684: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   11685:    key = integer (inbound ID)
                   11686:    value = uniqueID  
1.747     albertel 11687: 
1.608     albertel 11688: =back
                   11689: 
1.243     albertel 11690: =head2 HTTP Helper Routines
                   11691: 
                   11692: =over 4
                   11693: 
1.191     harris41 11694: =item *
                   11695: 
                   11696: escape() : unpack non-word characters into CGI-compatible hex codes
                   11697: 
                   11698: =item *
                   11699: 
                   11700: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   11701: 
1.243     albertel 11702: =back
                   11703: 
                   11704: =head1 PRIVATE SUBROUTINES
                   11705: 
                   11706: =head2 Underlying communication routines (Shouldn't call)
                   11707: 
                   11708: =over 4
                   11709: 
                   11710: =item *
                   11711: 
                   11712: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   11713: 
                   11714: =item *
                   11715: 
                   11716: reply() : uses subreply to send a message to remote machine, logs all failures
                   11717: 
                   11718: =item *
                   11719: 
                   11720: critical() : passes a critical message to another server; if cannot
                   11721: get through then place message in connection buffer directory and
                   11722: returns con_delayed, if incapable of saving message, returns
                   11723: con_failed
                   11724: 
                   11725: =item *
                   11726: 
                   11727: reconlonc() : tries to reconnect lonc client processes.
                   11728: 
                   11729: =back
                   11730: 
                   11731: =head2 Resource Access Logging
                   11732: 
                   11733: =over 4
                   11734: 
                   11735: =item *
                   11736: 
                   11737: flushcourselogs() : flush (save) buffer logs and access logs
                   11738: 
                   11739: =item *
                   11740: 
                   11741: courselog($what) : save message for course in hash
                   11742: 
                   11743: =item *
                   11744: 
                   11745: courseacclog($what) : save message for course using &courselog().  Perform
                   11746: special processing for specific resource types (problems, exams, quizzes, etc).
                   11747: 
1.191     harris41 11748: =item *
                   11749: 
                   11750: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   11751: as a PerlChildExitHandler
1.243     albertel 11752: 
                   11753: =back
                   11754: 
                   11755: =head2 Other
                   11756: 
                   11757: =over 4
                   11758: 
                   11759: =item *
                   11760: 
                   11761: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 11762: 
                   11763: =back
                   11764: 
                   11765: =cut
1.877     foxr     11766: 

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