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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.910.2.1! albertel    4: # $Id: lonnet.pm,v 1.910 2007/09/05 17:37:51 albertel 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.1       albertel   30: package Apache::lonnet;
                     31: 
                     32: use strict;
1.8       www        33: use LWP::UserAgent();
1.486     www        34: use HTTP::Date;
                     35: # use Date::Parse;
1.871     albertel   36: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
                     37:             $_64bit %env);
                     38: 
                     39: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     40:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     41:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
                     42:     %courseownerbuf, %coursetypebuf);
1.403     www        43: 
1.1       albertel   44: use IO::Socket;
1.31      www        45: use GDBM_File;
1.208     albertel   46: use HTML::LCParser;
1.88      www        47: use Fcntl qw(:flock);
1.870     albertel   48: use Storable qw(thaw nfreeze);
1.539     albertel   49: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   50: use Cache::Memcached;
1.676     albertel   51: use Digest::MD5;
1.790     albertel   52: use Math::Random;
1.807     albertel   53: use LONCAPA qw(:DEFAULT :match);
1.740     www        54: use LONCAPA::Configuration;
1.676     albertel   55: 
1.195     www        56: my $readit;
1.550     foxr       57: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel   58: 
1.619     albertel   59: require Exporter;
                     60: 
                     61: our @ISA = qw (Exporter);
                     62: our @EXPORT = qw(%env);
                     63: 
1.449     matthew    64: =pod
                     65: 
                     66: =head1 Package Variables
                     67: 
                     68: These are largely undocumented, so if you decipher one please note it here.
                     69: 
                     70: =over 4
                     71: 
                     72: =item $processmarker
                     73: 
                     74: Contains the time this process was started and this servers host id.
                     75: 
                     76: =item $dumpcount
                     77: 
                     78: Counts the number of times a message log flush has been attempted (regardless
                     79: of success) by this process.  Used as part of the filename when messages are
                     80: delayed.
                     81: 
                     82: =back
                     83: 
                     84: =cut
                     85: 
                     86: 
1.1       albertel   87: # --------------------------------------------------------------------- Logging
1.729     www        88: {
                     89:     my $logid;
                     90:     sub instructor_log {
                     91: 	my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
                     92: 	$logid++;
                     93: 	my $id=time().'00000'.$$.'00000'.$logid;
                     94: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www        95: 				    { $id => {
                     96: 					'exe_uname' => $env{'user.name'},
                     97: 					'exe_udom'  => $env{'user.domain'},
                     98: 					'exe_time'  => time(),
                     99: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    100: 					'delflag'   => $delflag,
                    101: 					'logentry'  => $storehash,
                    102: 					'uname'     => $uname,
                    103: 					'udom'      => $udom,
                    104: 				    }
                    105: 				  },
1.729     www       106: 				    $env{'course.'.$env{'request.course.id'}.'.domain'},
                    107: 				    $env{'course.'.$env{'request.course.id'}.'.num'}
                    108: 				    );
                    109:     }
                    110: }
1.1       albertel  111: 
1.163     harris41  112: sub logtouch {
                    113:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  114:     unless (-e "$execdir/logs/lonnet.log") {	
                    115: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  116: 	close $fh;
                    117:     }
                    118:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    119:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    120: }
                    121: 
1.1       albertel  122: sub logthis {
                    123:     my $message=shift;
                    124:     my $execdir=$perlvar{'lonDaemons'};
                    125:     my $now=time;
                    126:     my $local=localtime($now);
1.448     albertel  127:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
                    128: 	print $fh "$local ($$): $message\n";
                    129: 	close($fh);
                    130:     }
1.1       albertel  131:     return 1;
                    132: }
                    133: 
                    134: sub logperm {
                    135:     my $message=shift;
                    136:     my $execdir=$perlvar{'lonDaemons'};
                    137:     my $now=time;
                    138:     my $local=localtime($now);
1.448     albertel  139:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    140: 	print $fh "$now:$message:$local\n";
                    141: 	close($fh);
                    142:     }
1.1       albertel  143:     return 1;
                    144: }
                    145: 
1.850     albertel  146: sub create_connection {
1.853     albertel  147:     my ($hostname,$lonid) = @_;
1.851     albertel  148:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  149: 				     Type    => SOCK_STREAM,
                    150: 				     Timeout => 10);
                    151:     return 0 if (!$client);
1.890     albertel  152:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  153:     my $result = <$client>;
                    154:     chomp($result);
                    155:     return 1 if ($result eq 'done');
                    156:     return 0;
                    157: }
                    158: 
                    159: 
1.1       albertel  160: # -------------------------------------------------- Non-critical communication
                    161: sub subreply {
                    162:     my ($cmd,$server)=@_;
1.838     albertel  163:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      164:     #
                    165:     #  With loncnew process trimming, there's a timing hole between lonc server
                    166:     #  process exit and the master server picking up the listen on the AF_UNIX
                    167:     #  socket.  In that time interval, a lock file will exist:
                    168: 
                    169:     my $lockfile=$peerfile.".lock";
                    170:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    171: 	sleep(1);
                    172:     }
                    173:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      174:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      175:     #
1.550     foxr      176:     #   We'll give the connection a few tries before abandoning it.  If
                    177:     #   connection is not possible, we'll con_lost back to the client.
                    178:     #   
                    179:     my $client;
                    180:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    181: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    182: 				      Type    => SOCK_STREAM,
                    183: 				      Timeout => 10);
1.869     albertel  184: 	if ($client) {
1.550     foxr      185: 	    last;		# Connected!
1.850     albertel  186: 	} else {
1.853     albertel  187: 	    &create_connection(&hostname($server),$server);
1.550     foxr      188: 	}
1.850     albertel  189:         sleep(1);		# Try again later if failed connection.
1.550     foxr      190:     }
                    191:     my $answer;
                    192:     if ($client) {
1.704     albertel  193: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      194: 	$answer=<$client>;
                    195: 	if (!$answer) { $answer="con_lost"; }
                    196: 	chomp($answer);
                    197:     } else {
                    198: 	$answer = 'con_lost';	# Failed connection.
                    199:     }
1.1       albertel  200:     return $answer;
                    201: }
                    202: 
                    203: sub reply {
                    204:     my ($cmd,$server)=@_;
1.838     albertel  205:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  206:     my $answer=subreply($cmd,$server);
1.65      www       207:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  208:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       209:                 " $cmd to $server returned $answer</font>");
                    210:     }
1.1       albertel  211:     return $answer;
                    212: }
                    213: 
                    214: # ----------------------------------------------------------- Send USR1 to lonc
                    215: 
                    216: sub reconlonc {
1.891     albertel  217:     my ($lonid) = @_;
                    218:     my $hostname = &hostname($lonid);
                    219:     if ($lonid) {
                    220: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    221: 	if ($hostname && -e $peerfile) {
                    222: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    223: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    224: 					     Type    => SOCK_STREAM,
                    225: 					     Timeout => 10);
                    226: 	    if ($client) {
                    227: 		print $client ("reset_retries\n");
                    228: 		my $answer=<$client>;
                    229: 		#reset just this one.
                    230: 	    }
                    231: 	}
                    232: 	return;
                    233:     }
                    234: 
1.836     www       235:     &logthis("Trying to reconnect lonc");
1.1       albertel  236:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  237:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  238: 	my $loncpid=<$fh>;
                    239:         chomp($loncpid);
                    240:         if (kill 0 => $loncpid) {
                    241: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    242:             kill USR1 => $loncpid;
                    243:             sleep 1;
1.836     www       244:          } else {
1.12      www       245: 	    &logthis(
1.672     albertel  246:                "<font color=\"blue\">WARNING:".
1.12      www       247:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  248:         }
                    249:     } else {
1.836     www       250: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  251:     }
                    252: }
                    253: 
                    254: # ------------------------------------------------------ Critical communication
1.12      www       255: 
1.1       albertel  256: sub critical {
                    257:     my ($cmd,$server)=@_;
1.838     albertel  258:     unless (&hostname($server)) {
1.672     albertel  259:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       260:                " Critical message to unknown server ($server)</font>");
                    261:         return 'no_such_host';
                    262:     }
1.1       albertel  263:     my $answer=reply($cmd,$server);
                    264:     if ($answer eq 'con_lost') {
                    265: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  266: 	my $answer=reply($cmd,$server);
1.1       albertel  267:         if ($answer eq 'con_lost') {
                    268:             my $now=time;
                    269:             my $middlename=$cmd;
1.5       www       270:             $middlename=substr($middlename,0,16);
1.1       albertel  271:             $middlename=~s/\W//g;
                    272:             my $dfilename=
1.305     www       273:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    274:             $dumpcount++;
1.1       albertel  275:             {
1.448     albertel  276: 		my $dfh;
                    277: 		if (open($dfh,">$dfilename")) {
                    278: 		    print $dfh "$cmd\n"; 
                    279: 		    close($dfh);
                    280: 		}
1.1       albertel  281:             }
                    282:             sleep 2;
                    283:             my $wcmd='';
                    284:             {
1.448     albertel  285: 		my $dfh;
                    286: 		if (open($dfh,"<$dfilename")) {
                    287: 		    $wcmd=<$dfh>; 
                    288: 		    close($dfh);
                    289: 		}
1.1       albertel  290:             }
                    291:             chomp($wcmd);
1.7       www       292:             if ($wcmd eq $cmd) {
1.672     albertel  293: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       294:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  295:                 &logperm("D:$server:$cmd");
                    296: 	        return 'con_delayed';
                    297:             } else {
1.672     albertel  298:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       299:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  300:                 &logperm("F:$server:$cmd");
                    301:                 return 'con_failed';
                    302:             }
                    303:         }
                    304:     }
                    305:     return $answer;
1.405     albertel  306: }
                    307: 
1.755     albertel  308: # ------------------------------------------- check if return value is an error
                    309: 
                    310: sub error {
                    311:     my ($result) = @_;
1.756     albertel  312:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  313: 	if ($2 == 2) { return undef; }
                    314: 	return $1;
                    315:     }
                    316:     return undef;
                    317: }
                    318: 
1.783     albertel  319: sub convert_and_load_session_env {
                    320:     my ($lonidsdir,$handle)=@_;
                    321:     my @profile;
                    322:     {
                    323: 	open(my $idf,"$lonidsdir/$handle.id");
                    324: 	flock($idf,LOCK_SH);
                    325: 	@profile=<$idf>;
                    326: 	close($idf);
                    327:     }
                    328:     my %temp_env;
                    329:     foreach my $line (@profile) {
1.786     albertel  330: 	if ($line !~ m/=/) {
                    331: 	    return 0;
                    332: 	}
1.783     albertel  333: 	chomp($line);
                    334: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    335: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    336:     }
                    337:     unlink("$lonidsdir/$handle.id");
                    338:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    339: 	    0640)) {
                    340: 	%disk_env = %temp_env;
                    341: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    342: 	untie(%disk_env);
                    343:     }
1.786     albertel  344:     return 1;
1.783     albertel  345: }
                    346: 
1.374     www       347: # ------------------------------------------- Transfer profile into environment
1.780     albertel  348: my $env_loaded;
                    349: sub transfer_profile_to_env {
1.788     albertel  350:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    351:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       352: 
1.720     albertel  353:     if (!defined($lonidsdir)) {
                    354: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    355:     }
                    356:     if (!defined($handle)) {
                    357:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    358:     }
                    359: 
1.786     albertel  360:     my $convert;
                    361:     {
                    362:     	open(my $idf,"$lonidsdir/$handle.id");
                    363: 	flock($idf,LOCK_SH);
                    364: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    365: 		&GDBM_READER(),0640)) {
                    366: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    367: 	    untie(%disk_env);
                    368: 	} else {
                    369: 	    $convert = 1;
                    370: 	}
                    371:     }
                    372:     if ($convert) {
                    373: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    374: 	    &logthis("Failed to load session, or convert session.");
                    375: 	}
1.374     www       376:     }
1.783     albertel  377: 
1.786     albertel  378:     my %remove;
1.783     albertel  379:     while ( my $envname = each(%env) ) {
1.433     matthew   380:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    381:             if ($time < time-300) {
1.783     albertel  382:                 $remove{$key}++;
1.433     matthew   383:             }
                    384:         }
                    385:     }
1.783     albertel  386: 
1.619     albertel  387:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  388:     $env_loaded=1;
1.783     albertel  389:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   390:         &delenv($expired_key);
1.374     www       391:     }
1.1       albertel  392: }
                    393: 
1.830     albertel  394: sub timed_flock {
                    395:     my ($file,$lock_type) = @_;
                    396:     my $failed=0;
                    397:     eval {
                    398: 	local $SIG{__DIE__}='DEFAULT';
                    399: 	local $SIG{ALRM}=sub {
                    400: 	    $failed=1;
                    401: 	    die("failed lock");
                    402: 	};
                    403: 	alarm(13);
                    404: 	flock($file,$lock_type);
                    405: 	alarm(0);
                    406:     };
                    407:     if ($failed) {
                    408: 	return undef;
                    409:     } else {
                    410: 	return 1;
                    411:     }
                    412: }
                    413: 
1.5       www       414: # ---------------------------------------------------------- Append Environment
                    415: 
                    416: sub appenv {
1.6       www       417:     my %newenv=@_;
1.692     albertel  418:     foreach my $key (keys(%newenv)) {
                    419: 	if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
1.672     albertel  420:             &logthis("<font color=\"blue\">WARNING: ".
1.692     albertel  421:                 "Attempt to modify environment ".$key." to ".$newenv{$key}
1.151     www       422:                 .'</font>');
1.692     albertel  423: 	    delete($newenv{$key});
1.35      www       424:         } else {
1.692     albertel  425:             $env{$key}=$newenv{$key};
1.35      www       426:         }
1.191     harris41  427:     }
1.830     albertel  428:     open(my $env_file,$env{'user.environment'});
                    429:     if (&timed_flock($env_file,LOCK_EX)
                    430: 	&&
                    431: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    432: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  433: 	while (my ($key,$value) = each(%newenv)) {
                    434: 	    $disk_env{$key} = $value;
1.448     albertel  435: 	}
1.783     albertel  436: 	untie(%disk_env);
1.56      www       437:     }
                    438:     return 'ok';
                    439: }
                    440: # ----------------------------------------------------- Delete from Environment
                    441: 
                    442: sub delenv {
                    443:     my $delthis=shift;
                    444:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672     albertel  445:         &logthis("<font color=\"blue\">WARNING: ".
1.56      www       446:                 "Attempt to delete from environment ".$delthis);
                    447:         return 'error';
                    448:     }
1.830     albertel  449:     open(my $env_file,$env{'user.environment'});
                    450:     if (&timed_flock($env_file,LOCK_EX)
                    451: 	&&
                    452: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    453: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  454: 	foreach my $key (keys(%disk_env)) {
                    455: 	    if ($key=~/^$delthis/) { 
1.619     albertel  456:                 delete($env{$key});
1.783     albertel  457:                 delete($disk_env{$key});
1.473     matthew   458:             }
1.448     albertel  459: 	}
1.783     albertel  460: 	untie(%disk_env);
1.5       www       461:     }
                    462:     return 'ok';
1.369     albertel  463: }
                    464: 
1.790     albertel  465: sub get_env_multiple {
                    466:     my ($name) = @_;
                    467:     my @values;
                    468:     if (defined($env{$name})) {
                    469:         # exists is it an array
                    470:         if (ref($env{$name})) {
                    471:             @values=@{ $env{$name} };
                    472:         } else {
                    473:             $values[0]=$env{$name};
                    474:         }
                    475:     }
                    476:     return(@values);
                    477: }
                    478: 
1.369     albertel  479: # ------------------------------------------ Find out current server userload
                    480: # there is a copy in lond
                    481: sub userload {
                    482:     my $numusers=0;
                    483:     {
                    484: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    485: 	my $filename;
                    486: 	my $curtime=time;
                    487: 	while ($filename=readdir(LONIDS)) {
                    488: 	    if ($filename eq '.' || $filename eq '..') {next;}
1.404     albertel  489: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  490: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  491: 	}
                    492: 	closedir(LONIDS);
                    493:     }
                    494:     my $userloadpercent=0;
                    495:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    496:     if ($maxuserload) {
1.371     albertel  497: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  498:     }
1.372     albertel  499:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  500:     return $userloadpercent;
1.283     www       501: }
                    502: 
                    503: # ------------------------------------------ Fight off request when overloaded
                    504: 
                    505: sub overloaderror {
                    506:     my ($r,$checkserver)=@_;
                    507:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    508:     my $loadavg;
                    509:     if ($checkserver eq $perlvar{'lonHostID'}) {
1.448     albertel  510:        open(my $loadfile,'/proc/loadavg');
1.283     www       511:        $loadavg=<$loadfile>;
                    512:        $loadavg =~ s/\s.*//g;
1.285     matthew   513:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448     albertel  514:        close($loadfile);
1.283     www       515:     } else {
                    516:        $loadavg=&reply('load',$checkserver);
                    517:     }
1.285     matthew   518:     my $overload=$loadavg-100;
1.283     www       519:     if ($overload>0) {
1.285     matthew   520: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       521:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554     www       522:         return 413;
1.283     www       523:     }    
                    524:     return '';
1.5       www       525: }
1.1       albertel  526: 
                    527: # ------------------------------ Find server with least workload from spare.tab
1.11      www       528: 
1.1       albertel  529: sub spareserver {
1.670     albertel  530:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784     albertel  531:     my $spare_server;
1.370     albertel  532:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  533:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    534:                                                      :  $userloadpercent;
                    535:     
                    536:     foreach my $try_server (@{ $spareid{'primary'} }) {
                    537: 	($spare_server, $lowest_load) =
                    538: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
                    539:     }
                    540: 
                    541:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    542: 
                    543:     if (!$found_server) {
                    544: 	foreach my $try_server (@{ $spareid{'default'} }) {
                    545: 	    ($spare_server, $lowest_load) =
                    546: 		&compare_server_load($try_server, $spare_server, $lowest_load);
                    547: 	}
                    548:     }
                    549: 
                    550:     if (!$want_server_name) {
1.838     albertel  551: 	$spare_server="http://".&hostname($spare_server);
1.784     albertel  552:     }
                    553:     return $spare_server;
                    554: }
                    555: 
                    556: sub compare_server_load {
                    557:     my ($try_server, $spare_server, $lowest_load) = @_;
                    558: 
                    559:     my $loadans     = &reply('load',    $try_server);
                    560:     my $userloadans = &reply('userload',$try_server);
                    561: 
                    562:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
                    563: 	next; #didn't get a number from the server
                    564:     }
                    565: 
                    566:     my $load;
                    567:     if ($loadans =~ /\d/) {
                    568: 	if ($userloadans =~ /\d/) {
                    569: 	    #both are numbers, pick the bigger one
                    570: 	    $load = ($loadans > $userloadans) ? $loadans 
                    571: 		                              : $userloadans;
1.411     albertel  572: 	} else {
1.784     albertel  573: 	    $load = $loadans;
1.411     albertel  574: 	}
1.784     albertel  575:     } else {
                    576: 	$load = $userloadans;
                    577:     }
                    578: 
                    579:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    580: 	$spare_server = $try_server;
                    581: 	$lowest_load  = $load;
1.370     albertel  582:     }
1.784     albertel  583:     return ($spare_server,$lowest_load);
1.202     matthew   584: }
                    585: # --------------------------------------------- Try to change a user's password
                    586: 
                    587: sub changepass {
1.799     raeburn   588:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   589:     $currentpass = &escape($currentpass);
                    590:     $newpass     = &escape($newpass);
1.799     raeburn   591:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202     matthew   592: 		       $server);
                    593:     if (! $answer) {
                    594: 	&logthis("No reply on password change request to $server ".
                    595: 		 "by $uname in domain $udom.");
                    596:     } elsif ($answer =~ "^ok") {
                    597:         &logthis("$uname in $udom successfully changed their password ".
                    598: 		 "on $server.");
                    599:     } elsif ($answer =~ "^pwchange_failure") {
                    600: 	&logthis("$uname in $udom was unable to change their password ".
                    601: 		 "on $server.  The action was blocked by either lcpasswd ".
                    602: 		 "or pwchange");
                    603:     } elsif ($answer =~ "^non_authorized") {
                    604:         &logthis("$uname in $udom did not get their password correct when ".
                    605: 		 "attempting to change it on $server.");
                    606:     } elsif ($answer =~ "^auth_mode_error") {
                    607:         &logthis("$uname in $udom attempted to change their password despite ".
                    608: 		 "not being locally or internally authenticated on $server.");
                    609:     } elsif ($answer =~ "^unknown_user") {
                    610:         &logthis("$uname in $udom attempted to change their password ".
                    611: 		 "on $server but were unable to because $server is not ".
                    612: 		 "their home server.");
                    613:     } elsif ($answer =~ "^refused") {
                    614: 	&logthis("$server refused to change $uname in $udom password because ".
                    615: 		 "it was sent an unencrypted request to change the password.");
                    616:     }
                    617:     return $answer;
1.1       albertel  618: }
                    619: 
1.169     harris41  620: # ----------------------- Try to determine user's current authentication scheme
                    621: 
                    622: sub queryauthenticate {
                    623:     my ($uname,$udom)=@_;
1.456     albertel  624:     my $uhome=&homeserver($uname,$udom);
                    625:     if (!$uhome) {
                    626: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    627: 	return 'no_host';
                    628:     }
                    629:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    630:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    631: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  632:     }
1.456     albertel  633:     return $answer;
1.169     harris41  634: }
                    635: 
1.1       albertel  636: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       637: 
1.1       albertel  638: sub authenticate {
                    639:     my ($uname,$upass,$udom)=@_;
1.807     albertel  640:     $upass=&escape($upass);
                    641:     $uname= &LONCAPA::clean_username($uname);
1.836     www       642:     my $uhome=&homeserver($uname,$udom,1);
                    643:     if ((!$uhome) || ($uhome eq 'no_host')) {
                    644: # Maybe the machine was offline and only re-appeared again recently?
                    645:         &reconlonc();
                    646: # One more
                    647: 	my $uhome=&homeserver($uname,$udom,1);
                    648: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                    649: 	    &logthis("User $uname at $udom is unknown in authenticate");
                    650: 	}
1.471     albertel  651: 	return 'no_host';
1.1       albertel  652:     }
1.471     albertel  653:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
                    654:     if ($answer eq 'authorized') {
                    655: 	&logthis("User $uname at $udom authorized by $uhome"); 
                    656: 	return $uhome; 
                    657:     }
                    658:     if ($answer eq 'non_authorized') {
                    659: 	&logthis("User $uname at $udom rejected by $uhome");
                    660: 	return 'no_host'; 
1.9       www       661:     }
1.471     albertel  662:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  663:     return 'no_host';
                    664: }
                    665: 
                    666: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       667: 
1.599     albertel  668: my %homecache;
1.1       albertel  669: sub homeserver {
1.230     stredwic  670:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  671:     my $index="$uname:$udom";
1.426     albertel  672: 
1.599     albertel  673:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel  674: 
                    675:     my %servers = &get_servers($udom,'library');
                    676:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic  677:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  678: 		 exists($badServerCache{$tryserver}));
1.841     albertel  679: 
                    680: 	my $answer=reply("home:$udom:$uname",$tryserver);
                    681: 	if ($answer eq 'found') {
                    682: 	    delete($badServerCache{$tryserver}); 
                    683: 	    return $homecache{$index}=$tryserver;
                    684: 	} elsif ($answer eq 'no_host') {
                    685: 	    $badServerCache{$tryserver}=1;
                    686: 	}
1.1       albertel  687:     }    
                    688:     return 'no_host';
1.70      www       689: }
                    690: 
                    691: # ------------------------------------- Find the usernames behind a list of IDs
                    692: 
                    693: sub idget {
                    694:     my ($udom,@ids)=@_;
                    695:     my %returnhash=();
                    696:     
1.841     albertel  697:     my %servers = &get_servers($udom,'library');
                    698:     foreach my $tryserver (keys(%servers)) {
                    699: 	my $idlist=join('&',@ids);
                    700: 	$idlist=~tr/A-Z/a-z/; 
                    701: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    702: 	my @answer=();
                    703: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                    704: 	    @answer=split(/\&/,$reply);
                    705: 	}                    ;
                    706: 	my $i;
                    707: 	for ($i=0;$i<=$#ids;$i++) {
                    708: 	    if ($answer[$i]) {
                    709: 		$returnhash{$ids[$i]}=$answer[$i];
                    710: 	    } 
                    711: 	}
                    712:     } 
1.70      www       713:     return %returnhash;
                    714: }
                    715: 
                    716: # ------------------------------------- Find the IDs behind a list of usernames
                    717: 
                    718: sub idrget {
                    719:     my ($udom,@unames)=@_;
                    720:     my %returnhash=();
1.800     albertel  721:     foreach my $uname (@unames) {
                    722:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41  723:     }
1.70      www       724:     return %returnhash;
                    725: }
                    726: 
                    727: # ------------------------------- Store away a list of names and associated IDs
                    728: 
                    729: sub idput {
                    730:     my ($udom,%ids)=@_;
                    731:     my %servers=();
1.800     albertel  732:     foreach my $uname (keys(%ids)) {
                    733: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                    734:         my $uhom=&homeserver($uname,$udom);
1.70      www       735:         if ($uhom ne 'no_host') {
1.800     albertel  736:             my $id=&escape($ids{$uname});
1.70      www       737:             $id=~tr/A-Z/a-z/;
1.800     albertel  738:             my $esc_unam=&escape($uname);
1.70      www       739: 	    if ($servers{$uhom}) {
1.800     albertel  740: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www       741:             } else {
1.800     albertel  742:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www       743:             }
                    744:         }
1.191     harris41  745:     }
1.800     albertel  746:     foreach my $server (keys(%servers)) {
                    747:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41  748:     }
1.344     www       749: }
                    750: 
1.806     raeburn   751: # ------------------------------------------- get items from domain db files   
                    752: 
                    753: sub get_dom {
1.860     raeburn   754:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn   755:     my $items='';
                    756:     foreach my $item (@$storearr) {
                    757:         $items.=&escape($item).'&';
                    758:     }
                    759:     $items=~s/\&$//;
1.860     raeburn   760:     if (!$udom) {
                    761:         $udom=$env{'user.domain'};
                    762:         if (defined(&domain($udom,'primary'))) {
                    763:             $uhome=&domain($udom,'primary');
                    764:         } else {
1.874     albertel  765:             undef($uhome);
1.860     raeburn   766:         }
                    767:     } else {
                    768:         if (!$uhome) {
                    769:             if (defined(&domain($udom,'primary'))) {
                    770:                 $uhome=&domain($udom,'primary');
                    771:             }
                    772:         }
                    773:     }
                    774:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   775:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn   776:         my %returnhash;
1.875     albertel  777:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn   778:             return %returnhash;
                    779:         }
1.806     raeburn   780:         my @pairs=split(/\&/,$rep);
                    781:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                    782:             return @pairs;
                    783:         }
                    784:         my $i=0;
                    785:         foreach my $item (@$storearr) {
                    786:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                    787:             $i++;
                    788:         }
                    789:         return %returnhash;
                    790:     } else {
1.880     banghart  791:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn   792:     }
                    793: }
                    794: 
                    795: # -------------------------------------------- put items in domain db files 
                    796: 
                    797: sub put_dom {
1.860     raeburn   798:     my ($namespace,$storehash,$udom,$uhome)=@_;
                    799:     if (!$udom) {
                    800:         $udom=$env{'user.domain'};
                    801:         if (defined(&domain($udom,'primary'))) {
                    802:             $uhome=&domain($udom,'primary');
                    803:         } else {
1.874     albertel  804:             undef($uhome);
1.860     raeburn   805:         }
                    806:     } else {
                    807:         if (!$uhome) {
                    808:             if (defined(&domain($udom,'primary'))) {
                    809:                 $uhome=&domain($udom,'primary');
                    810:             }
                    811:         }
                    812:     } 
                    813:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   814:         my $items='';
                    815:         foreach my $item (keys(%$storehash)) {
                    816:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                    817:         }
                    818:         $items=~s/\&$//;
                    819:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                    820:     } else {
1.860     raeburn   821:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn   822:     }
                    823: }
                    824: 
1.837     raeburn   825: sub retrieve_inst_usertypes {
                    826:     my ($udom) = @_;
                    827:     my (%returnhash,@order);
1.846     albertel  828:     if (defined(&domain($udom,'primary'))) {
                    829:         my $uhome=&domain($udom,'primary');
1.837     raeburn   830:         my $rep=&reply("inst_usertypes:$udom",$uhome);
                    831:         my ($hashitems,$orderitems) = split(/:/,$rep); 
                    832:         my @pairs=split(/\&/,$hashitems);
                    833:         foreach my $item (@pairs) {
                    834:             my ($key,$value)=split(/=/,$item,2);
                    835:             $key = &unescape($key);
                    836:             next if ($key =~ /^error: 2 /);
                    837:             $returnhash{$key}=&thaw_unescape($value);
                    838:         }
                    839:         my @esc_order = split(/\&/,$orderitems);
                    840:         foreach my $item (@esc_order) {
                    841:             push(@order,&unescape($item));
                    842:         }
                    843:     } else {
                    844:         &logthis("get_dom failed - no primary domain server for $udom");
                    845:     }
                    846:     return (\%returnhash,\@order);
                    847: }
                    848: 
1.868     raeburn   849: sub is_domainimage {
                    850:     my ($url) = @_;
                    851:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                    852:         if (&domain($1) ne '') {
                    853:             return '1';
                    854:         }
                    855:     }
                    856:     return;
                    857: }
                    858: 
1.899     raeburn   859: sub inst_directory_query {
                    860:     my ($srch) = @_;
                    861:     my $udom = $srch->{'srchdomain'};
                    862:     my %results;
                    863:     my $homeserver = &domain($udom,'primary');
1.909     raeburn   864:     my $outcome;
1.899     raeburn   865:     if ($homeserver ne '') {
1.904     albertel  866: 	my $queryid=&reply("querysend:instdirsearch:".
                    867: 			   &escape($srch->{'srchby'}).':'.
                    868: 			   &escape($srch->{'srchterm'}).':'.
                    869: 			   &escape($srch->{'srchtype'}),$homeserver);
                    870: 	my $host=&hostname($homeserver);
                    871: 	if ($queryid !~/^\Q$host\E\_/) {
                    872: 	    &logthis('instituional directory search invalid queryid: '.$queryid.' for host: '.$homeserver.'in domain '.$udom);
                    873: 	    return;
                    874: 	}
                    875: 	my $response = &get_query_reply($queryid);
                    876: 	my $maxtries = 5;
                    877: 	my $tries = 1;
                    878: 	while (($response=~/^timeout/) && ($tries < $maxtries)) {
                    879: 	    $response = &get_query_reply($queryid);
                    880: 	    $tries ++;
                    881: 	}
                    882: 
                    883:         if (!&error($response) && $response ne 'refused') {
1.909     raeburn   884:             if ($response eq 'unavailable') {
                    885:                 $outcome = $response;
                    886:             } else {
                    887:                 $outcome = 'ok';
                    888:                 my @matches = split(/\n/,$response);
                    889:                 foreach my $match (@matches) {
                    890:                     my ($key,$value) = split(/=/,$match);
                    891:                     $results{&unescape($key).':'.$udom} = &thaw_unescape($value);
                    892:                 }
1.899     raeburn   893:             }
                    894:         }
                    895:     }
1.909     raeburn   896:     return ($outcome,%results);
1.899     raeburn   897: }
                    898: 
                    899: sub usersearch {
                    900:     my ($srch) = @_;
                    901:     my $dom = $srch->{'srchdomain'};
                    902:     my %results;
                    903:     my %libserv = &all_library();
                    904:     my $query = 'usersearch';
                    905:     foreach my $tryserver (keys(%libserv)) {
                    906:         if (&host_domain($tryserver) eq $dom) {
                    907:             my $host=&hostname($tryserver);
                    908:             my $queryid=
                    909:                 &reply("querysend:".&escape($query).':'.&escape($dom).':'.
                    910:                        &escape($srch->{'srchby'}).'%%'.
                    911:                        &escape($srch->{'srchtype'}).':'.
                    912:                        &escape($srch->{'srchterm'}),$tryserver);
                    913:             if ($queryid !~/^\Q$host\E\_/) {
                    914:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
1.902     raeburn   915:                 next;
1.899     raeburn   916:             }
                    917:             my $reply = &get_query_reply($queryid);
                    918:             my $maxtries = 1;
                    919:             my $tries = 1;
                    920:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                    921:                 $reply = &get_query_reply($queryid);
                    922:                 $tries ++;
                    923:             }
                    924:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                    925:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                    926:             } else {
1.900     albertel  927:                 my @matches = split(/&/,$reply);
1.899     raeburn   928:                 foreach my $match (@matches) {
                    929:                     my @items = split(/:/,$match);
                    930:                     my ($uname,$udom,%userhash);
                    931:                     foreach my $entry (@items) {
                    932:                         my ($key,$value) = split(/=/,$entry);
                    933:                         $key = &unescape($key);
                    934:                         $value = &unescape($value);
                    935:                         $userhash{$key} = $value;
                    936:                         if ($key eq 'username') {
                    937:                             $uname = $value;
                    938:                         } elsif ($key eq 'domain') {
                    939:                             $udom = $value;
                    940:                         } 
                    941:                     }
                    942:                     $results{$uname.':'.$udom} = \%userhash;
                    943:                 }
                    944:             }
                    945:         }
                    946:     }
                    947:     return %results;
                    948: }
                    949: 
1.344     www       950: # --------------------------------------------------- Assign a key to a student
                    951: 
                    952: sub assign_access_key {
1.364     www       953: #
                    954: # a valid key looks like uname:udom#comments
                    955: # comments are being appended
                    956: #
1.498     www       957:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                    958:     $kdom=
1.620     albertel  959:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www       960:     $knum=
1.620     albertel  961:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www       962:     $cdom=
1.620     albertel  963:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       964:     $cnum=
1.620     albertel  965:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    966:     $udom=$env{'user.name'} unless (defined($udom));
                    967:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www       968:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www       969:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel  970:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www       971:                                                   # assigned to this person
                    972:                                                   # - this should not happen,
1.345     www       973:                                                   # unless something went wrong
                    974:                                                   # the first time around
                    975: # ready to assign
1.364     www       976:         $logentry=$1.'; '.$logentry;
1.496     www       977:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www       978:                                                  $kdom,$knum) eq 'ok') {
1.345     www       979: # key now belongs to user
1.346     www       980: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www       981:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
                    982:                 &appenv('environment.'.$envkey => $ckey);
                    983:                 return 'ok';
                    984:             } else {
                    985:                 return 
                    986:   'error: Count not permanently assign key, will need to be re-entered later.';
                    987: 	    }
                    988:         } else {
                    989:             return 'error: Could not assign key, try again later.';
                    990:         }
1.364     www       991:     } elsif (!$existing{$ckey}) {
1.345     www       992: # the key does not exist
                    993: 	return 'error: The key does not exist';
                    994:     } else {
                    995: # the key is somebody else's
                    996: 	return 'error: The key is already in use';
                    997:     }
1.344     www       998: }
                    999: 
1.364     www      1000: # ------------------------------------------ put an additional comment on a key
                   1001: 
                   1002: sub comment_access_key {
                   1003: #
                   1004: # a valid key looks like uname:udom#comments
                   1005: # comments are being appended
                   1006: #
                   1007:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                   1008:     $cdom=
1.620     albertel 1009:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www      1010:     $cnum=
1.620     albertel 1011:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www      1012:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                   1013:     if ($existing{$ckey}) {
                   1014:         $existing{$ckey}.='; '.$logentry;
                   1015: # ready to assign
1.367     www      1016:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www      1017:                                                  $cdom,$cnum) eq 'ok') {
                   1018: 	    return 'ok';
                   1019:         } else {
                   1020: 	    return 'error: Count not store comment.';
                   1021:         }
                   1022:     } else {
                   1023: # the key does not exist
                   1024: 	return 'error: The key does not exist';
                   1025:     }
                   1026: }
                   1027: 
1.344     www      1028: # ------------------------------------------------------ Generate a set of keys
                   1029: 
                   1030: sub generate_access_keys {
1.364     www      1031:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      1032:     $cdom=
1.620     albertel 1033:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1034:     $cnum=
1.620     albertel 1035:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      1036:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      1037:     unless (($cdom) && ($cnum)) { return 0; }
                   1038:     if ($number>10000) { return 0; }
                   1039:     sleep(2); # make sure don't get same seed twice
                   1040:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   1041:     my $total=0;
                   1042:     for (my $i=1;$i<=$number;$i++) {
                   1043:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   1044:                   sprintf("%lx",int(100000*rand)).'-'.
                   1045:                   sprintf("%lx",int(100000*rand));
                   1046:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   1047:        $newkey=~s/0/h/g; # and also 0 and O
                   1048:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   1049:        if ($existing{$newkey}) {
                   1050:            $i--;
                   1051:        } else {
1.364     www      1052: 	  if (&put('accesskeys',
                   1053:               { $newkey => '# generated '.localtime().
1.620     albertel 1054:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      1055:                            '; '.$logentry },
                   1056: 		   $cdom,$cnum) eq 'ok') {
1.344     www      1057:               $total++;
                   1058: 	  }
                   1059:        }
                   1060:     }
1.620     albertel 1061:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      1062:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   1063:     return $total;
                   1064: }
                   1065: 
                   1066: # ------------------------------------------------------- Validate an accesskey
                   1067: 
                   1068: sub validate_access_key {
                   1069:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   1070:     $cdom=
1.620     albertel 1071:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1072:     $cnum=
1.620     albertel 1073:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1074:     $udom=$env{'user.domain'} unless (defined($udom));
                   1075:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      1076:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 1077:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      1078: }
                   1079: 
                   1080: # ------------------------------------- Find the section of student in a course
1.652     albertel 1081: sub devalidate_getsection_cache {
                   1082:     my ($udom,$unam,$courseid)=@_;
                   1083:     my $hashid="$udom:$unam:$courseid";
                   1084:     &devalidate_cache_new('getsection',$hashid);
                   1085: }
1.298     matthew  1086: 
1.815     albertel 1087: sub courseid_to_courseurl {
                   1088:     my ($courseid) = @_;
                   1089:     #already url style courseid
                   1090:     return $courseid if ($courseid =~ m{^/});
                   1091: 
                   1092:     if (exists($env{'course.'.$courseid.'.num'})) {
                   1093: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   1094: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   1095: 	return "/$cdom/$cnum";
                   1096:     }
                   1097: 
                   1098:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   1099:     if (exists($courseinfo{'num'})) {
                   1100: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   1101:     }
                   1102: 
                   1103:     return undef;
                   1104: }
                   1105: 
1.298     matthew  1106: sub getsection {
                   1107:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 1108:     my $cachetime=1800;
1.551     albertel 1109: 
                   1110:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 1111:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 1112:     if (defined($cached)) { return $result; }
                   1113: 
1.298     matthew  1114:     my %Pending; 
                   1115:     my %Expired;
                   1116:     #
                   1117:     # Each role can either have not started yet (pending), be active, 
                   1118:     #    or have expired.
                   1119:     #
                   1120:     # If there is an active role, we are done.
                   1121:     #
                   1122:     # If there is more than one role which has not started yet, 
                   1123:     #     choose the one which will start sooner
                   1124:     # If there is one role which has not started yet, return it.
                   1125:     #
                   1126:     # If there is more than one expired role, choose the one which ended last.
                   1127:     # If there is a role which has expired, return it.
                   1128:     #
1.815     albertel 1129:     $courseid = &courseid_to_courseurl($courseid);
1.817     raeburn  1130:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
                   1131:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1132:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1133:         my $section=$1;
                   1134:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1135:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1136:         my $now=time;
1.548     albertel 1137:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1138:             $Expired{$end}=$section;
                   1139:             next;
                   1140:         }
1.548     albertel 1141:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1142:             $Pending{$start}=$section;
                   1143:             next;
                   1144:         }
1.599     albertel 1145:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1146:     }
                   1147:     #
                   1148:     # Presumedly there will be few matching roles from the above
                   1149:     # loop and the sorting time will be negligible.
                   1150:     if (scalar(keys(%Pending))) {
                   1151:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1152:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1153:     } 
                   1154:     if (scalar(keys(%Expired))) {
                   1155:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1156:         my $time = pop(@sorted);
1.599     albertel 1157:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1158:     }
1.599     albertel 1159:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1160: }
1.70      www      1161: 
1.599     albertel 1162: sub save_cache {
                   1163:     &purge_remembered();
1.722     albertel 1164:     #&Apache::loncommon::validate_page();
1.620     albertel 1165:     undef(%env);
1.780     albertel 1166:     undef($env_loaded);
1.599     albertel 1167: }
1.452     albertel 1168: 
1.599     albertel 1169: my $to_remember=-1;
                   1170: my %remembered;
                   1171: my %accessed;
                   1172: my $kicks=0;
                   1173: my $hits=0;
1.849     albertel 1174: sub make_key {
                   1175:     my ($name,$id) = @_;
1.872     albertel 1176:     if (length($id) > 65 
                   1177: 	&& length(&escape($id)) > 200) {
                   1178: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   1179:     }
1.849     albertel 1180:     return &escape($name.':'.$id);
                   1181: }
                   1182: 
1.599     albertel 1183: sub devalidate_cache_new {
                   1184:     my ($name,$id,$debug) = @_;
                   1185:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1186:     $id=&make_key($name,$id);
1.599     albertel 1187:     $memcache->delete($id);
                   1188:     delete($remembered{$id});
                   1189:     delete($accessed{$id});
                   1190: }
                   1191: 
                   1192: sub is_cached_new {
                   1193:     my ($name,$id,$debug) = @_;
1.849     albertel 1194:     $id=&make_key($name,$id);
1.599     albertel 1195:     if (exists($remembered{$id})) {
                   1196: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1197: 	$accessed{$id}=[&gettimeofday()];
                   1198: 	$hits++;
                   1199: 	return ($remembered{$id},1);
                   1200:     }
                   1201:     my $value = $memcache->get($id);
                   1202:     if (!(defined($value))) {
                   1203: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1204: 	return (undef,undef);
1.416     albertel 1205:     }
1.599     albertel 1206:     if ($value eq '__undef__') {
                   1207: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1208: 	$value=undef;
                   1209:     }
                   1210:     &make_room($id,$value,$debug);
                   1211:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1212:     return ($value,1);
                   1213: }
                   1214: 
                   1215: sub do_cache_new {
                   1216:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1217:     $id=&make_key($name,$id);
1.599     albertel 1218:     my $setvalue=$value;
                   1219:     if (!defined($setvalue)) {
                   1220: 	$setvalue='__undef__';
                   1221:     }
1.623     albertel 1222:     if (!defined($time) ) {
                   1223: 	$time=600;
                   1224:     }
1.599     albertel 1225:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.910     albertel 1226:     my $result = $memcache->set($id,$setvalue,$time);
                   1227:     if (! $result) {
1.872     albertel 1228: 	&logthis("caching of id -> $id  failed");
1.910     albertel 1229: 	$memcache->disconnect_all();
1.872     albertel 1230:     }
1.600     albertel 1231:     # need to make a copy of $value
                   1232:     #&make_room($id,$value,$debug);
1.599     albertel 1233:     return $value;
                   1234: }
                   1235: 
                   1236: sub make_room {
                   1237:     my ($id,$value,$debug)=@_;
                   1238:     $remembered{$id}=$value;
                   1239:     if ($to_remember<0) { return; }
                   1240:     $accessed{$id}=[&gettimeofday()];
                   1241:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1242:     my $to_kick;
                   1243:     my $max_time=0;
                   1244:     foreach my $other (keys(%accessed)) {
                   1245: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1246: 	    $to_kick=$other;
                   1247: 	    $max_time=&tv_interval($accessed{$other});
                   1248: 	}
                   1249:     }
                   1250:     delete($remembered{$to_kick});
                   1251:     delete($accessed{$to_kick});
                   1252:     $kicks++;
                   1253:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1254:     return;
                   1255: }
                   1256: 
1.599     albertel 1257: sub purge_remembered {
1.604     albertel 1258:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1259:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1260:     undef(%remembered);
                   1261:     undef(%accessed);
1.428     albertel 1262: }
1.70      www      1263: # ------------------------------------- Read an entry from a user's environment
                   1264: 
                   1265: sub userenvironment {
                   1266:     my ($udom,$unam,@what)=@_;
                   1267:     my %returnhash=();
                   1268:     my @answer=split(/\&/,
                   1269:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1270:                       &homeserver($unam,$udom)));
                   1271:     my $i;
                   1272:     for ($i=0;$i<=$#what;$i++) {
                   1273: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1274:     }
                   1275:     return %returnhash;
1.1       albertel 1276: }
                   1277: 
1.617     albertel 1278: # ---------------------------------------------------------- Get a studentphoto
                   1279: sub studentphoto {
                   1280:     my ($udom,$unam,$ext) = @_;
                   1281:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1282:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1283:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1284:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1285:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1286:             } else {
                   1287:                 my ($result,$perm_reqd)=
1.707     albertel 1288: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1289:                 if ($result eq 'ok') {
                   1290:                     if (!($perm_reqd eq 'yes')) {
                   1291:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1292:                     }
                   1293:                 }
                   1294:             }
                   1295:         }
                   1296:     } else {
                   1297:         my ($result,$perm_reqd) = 
1.707     albertel 1298: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1299:         if ($result eq 'ok') {
                   1300:             if (!($perm_reqd eq 'yes')) {
                   1301:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1302:             }
                   1303:         }
                   1304:     }
                   1305:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1306: }
                   1307: 
                   1308: sub retrievestudentphoto {
                   1309:     my ($udom,$unam,$ext,$type) = @_;
                   1310:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1311:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1312:     if ($ret eq 'ok') {
                   1313:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1314:         if ($type eq 'thumbnail') {
                   1315:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1316:         }
                   1317:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1318:         return $tokenurl;
                   1319:     } else {
                   1320:         if ($type eq 'thumbnail') {
                   1321:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1322:         } else { 
                   1323:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1324:         }
1.617     albertel 1325:     }
                   1326: }
                   1327: 
1.263     www      1328: # -------------------------------------------------------------------- New chat
                   1329: 
                   1330: sub chatsend {
1.724     raeburn  1331:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1332:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1333:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1334:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1335:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1336: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1337: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1338: }
                   1339: 
                   1340: # ------------------------------------------ Find current version of a resource
                   1341: 
                   1342: sub getversion {
                   1343:     my $fname=&clutter(shift);
                   1344:     unless ($fname=~/^\/res\//) { return -1; }
                   1345:     return &currentversion(&filelocation('',$fname));
                   1346: }
                   1347: 
                   1348: sub currentversion {
                   1349:     my $fname=shift;
1.599     albertel 1350:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      1351:     if (defined($cached)) { return $result; }
1.292     www      1352:     my $author=$fname;
                   1353:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1354:     my ($udom,$uname)=split(/\//,$author);
                   1355:     my $home=homeserver($uname,$udom);
                   1356:     if ($home eq 'no_host') { 
                   1357:         return -1; 
                   1358:     }
                   1359:     my $answer=reply("currentversion:$fname",$home);
                   1360:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1361: 	return -1;
                   1362:     }
1.599     albertel 1363:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      1364: }
                   1365: 
1.1       albertel 1366: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1367: 
1.1       albertel 1368: sub subscribe {
                   1369:     my $fname=shift;
1.761     raeburn  1370:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1371:     $fname=~s/[\n\r]//g;
1.1       albertel 1372:     my $author=$fname;
                   1373:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1374:     my ($udom,$uname)=split(/\//,$author);
                   1375:     my $home=homeserver($uname,$udom);
1.335     albertel 1376:     if ($home eq 'no_host') {
                   1377:         return 'not_found';
1.1       albertel 1378:     }
                   1379:     my $answer=reply("sub:$fname",$home);
1.64      www      1380:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1381: 	$answer.=' by '.$home;
                   1382:     }
1.1       albertel 1383:     return $answer;
                   1384: }
                   1385:     
1.8       www      1386: # -------------------------------------------------------------- Replicate file
                   1387: 
                   1388: sub repcopy {
                   1389:     my $filename=shift;
1.23      www      1390:     $filename=~s/\/+/\//g;
1.607     raeburn  1391:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   1392:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 1393:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 1394: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 1395: 	return &repcopy_userfile($filename);
                   1396:     }
1.532     albertel 1397:     $filename=~s/[\n\r]//g;
1.8       www      1398:     my $transname="$filename.in.transfer";
1.828     www      1399: # FIXME: this should flock
1.607     raeburn  1400:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      1401:     my $remoteurl=subscribe($filename);
1.64      www      1402:     if ($remoteurl =~ /^con_lost by/) {
                   1403: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1404:            return 'unavailable';
1.8       www      1405:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1406: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  1407: 	   return 'not_found';
1.64      www      1408:     } elsif ($remoteurl =~ /^rejected by/) {
                   1409: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1410:            return 'forbidden';
1.20      www      1411:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  1412:            return 'ok';
1.8       www      1413:     } else {
1.290     www      1414:         my $author=$filename;
                   1415:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1416:         my ($udom,$uname)=split(/\//,$author);
                   1417:         my $home=homeserver($uname,$udom);
                   1418:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1419:            my @parts=split(/\//,$filename);
                   1420:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1421:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1422:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  1423: 	       return 'bad_request';
1.8       www      1424:            }
                   1425:            my $count;
                   1426:            for ($count=5;$count<$#parts;$count++) {
                   1427:                $path.="/$parts[$count]";
                   1428:                if ((-e $path)!=1) {
                   1429: 		   mkdir($path,0777);
                   1430:                }
                   1431:            }
                   1432:            my $ua=new LWP::UserAgent;
                   1433:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1434:            my $response=$ua->request($request,$transname);
                   1435:            if ($response->is_error()) {
                   1436: 	       unlink($transname);
                   1437:                my $message=$response->status_line;
1.672     albertel 1438:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      1439:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  1440:                return 'unavailable';
1.8       www      1441:            } else {
1.16      www      1442: 	       if ($remoteurl!~/\.meta$/) {
                   1443:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1444:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1445:                   if ($mresponse->is_error()) {
                   1446: 		      unlink($filename.'.meta');
                   1447:                       &logthis(
1.672     albertel 1448:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      1449:                   }
                   1450: 	       }
1.8       www      1451:                rename($transname,$filename);
1.607     raeburn  1452:                return 'ok';
1.8       www      1453:            }
1.290     www      1454:        }
1.8       www      1455:     }
1.330     www      1456: }
                   1457: 
                   1458: # ------------------------------------------------ Get server side include body
                   1459: sub ssi_body {
1.381     albertel 1460:     my ($filelink,%form)=@_;
1.606     matthew  1461:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   1462:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   1463:     }
1.330     www      1464:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381     albertel 1465:                                      &ssi($filelink,%form));
1.778     albertel 1466:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 1467:     $output=~s/^.*?\<body[^\>]*\>//si;
                   1468:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330     www      1469:     return $output;
1.8       www      1470: }
                   1471: 
1.15      www      1472: # --------------------------------------------------------- Server Side Include
                   1473: 
1.782     albertel 1474: sub absolute_url {
                   1475:     my ($host_name) = @_;
                   1476:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   1477:     if ($host_name eq '') {
                   1478: 	$host_name = $ENV{'SERVER_NAME'};
                   1479:     }
                   1480:     return $protocol.$host_name;
                   1481: }
                   1482: 
1.15      www      1483: sub ssi {
                   1484: 
1.23      www      1485:     my ($fn,%form)=@_;
1.15      www      1486: 
                   1487:     my $ua=new LWP::UserAgent;
1.23      www      1488:     
                   1489:     my $request;
1.711     albertel 1490: 
                   1491:     $form{'no_update_last_known'}=1;
1.895     albertel 1492:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      1493:     if (%form) {
1.782     albertel 1494:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201     albertel 1495:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1496:     } else {
1.782     albertel 1497:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      1498:     }
                   1499: 
1.15      www      1500:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1501:     my $response=$ua->request($request);
                   1502: 
1.324     www      1503:     return $response->content;
                   1504: }
                   1505: 
                   1506: sub externalssi {
                   1507:     my ($url)=@_;
                   1508:     my $ua=new LWP::UserAgent;
                   1509:     my $request=new HTTP::Request('GET',$url);
                   1510:     my $response=$ua->request($request);
1.15      www      1511:     return $response->content;
                   1512: }
1.254     www      1513: 
1.492     albertel 1514: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1515: 
                   1516: sub allowuploaded {
                   1517:     my ($srcurl,$url)=@_;
                   1518:     $url=&clutter(&declutter($url));
                   1519:     my $dir=$url;
                   1520:     $dir=~s/\/[^\/]+$//;
                   1521:     my %httpref=();
                   1522:     my $httpurl=&hreflocation('',$url);
                   1523:     $httpref{'httpref.'.$httpurl}=$srcurl;
                   1524:     &Apache::lonnet::appenv(%httpref);
1.254     www      1525: }
1.477     raeburn  1526: 
1.478     albertel 1527: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 1528: # input: action, courseID, current domain, intended
1.637     raeburn  1529: #        path to file, source of file, instruction to parse file for objects,
                   1530: #        ref to hash for embedded objects,
                   1531: #        ref to hash for codebase of java objects.
                   1532: #
1.485     raeburn  1533: # output: url to file (if action was uploaddoc), 
                   1534: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1535: #
1.478     albertel 1536: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1537: # course.
1.477     raeburn  1538: #
1.478     albertel 1539: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1540: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1541: #          course's home server.
1.477     raeburn  1542: #
1.478     albertel 1543: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1544: #          be copied from $source (current location) to 
                   1545: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1546: #         and will then be copied to
                   1547: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1548: #         course's home server.
1.485     raeburn  1549: #
1.481     raeburn  1550: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 1551: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1552: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1553: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1554: #         in course's home server.
1.637     raeburn  1555: #
1.477     raeburn  1556: 
                   1557: sub process_coursefile {
1.638     albertel 1558:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477     raeburn  1559:     my $fetchresult;
1.638     albertel 1560:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  1561:     if ($action eq 'propagate') {
1.638     albertel 1562:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1563: 			     $home);
1.481     raeburn  1564:     } else {
1.477     raeburn  1565:         my $fpath = '';
                   1566:         my $fname = $file;
1.478     albertel 1567:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1568:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  1569:         my $filepath = &build_filepath($fpath);
1.481     raeburn  1570:         if ($action eq 'copy') {
                   1571:             if ($source eq '') {
                   1572:                 $fetchresult = 'no source file';
                   1573:                 return $fetchresult;
                   1574:             } else {
                   1575:                 my $destination = $filepath.'/'.$fname;
                   1576:                 rename($source,$destination);
                   1577:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1578:                                  $home);
1.481     raeburn  1579:             }
                   1580:         } elsif ($action eq 'uploaddoc') {
                   1581:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 1582:             print $fh $env{'form.'.$source};
1.481     raeburn  1583:             close($fh);
1.637     raeburn  1584:             if ($parser eq 'parse') {
                   1585:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
                   1586:                 unless ($parse_result eq 'ok') {
                   1587:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   1588:                 }
                   1589:             }
1.477     raeburn  1590:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1591:                                  $home);
1.481     raeburn  1592:             if ($fetchresult eq 'ok') {
                   1593:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1594:             } else {
                   1595:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1596:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  1597:                 return '/adm/notfound.html';
                   1598:             }
1.477     raeburn  1599:         }
                   1600:     }
1.485     raeburn  1601:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1602:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1603:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  1604:     }
                   1605:     return $fetchresult;
                   1606: }
                   1607: 
1.637     raeburn  1608: sub build_filepath {
                   1609:     my ($fpath) = @_;
                   1610:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1611:     unless ($fpath eq '') {
                   1612:         my @parts=split('/',$fpath);
                   1613:         foreach my $part (@parts) {
                   1614:             $filepath.= '/'.$part;
                   1615:             if ((-e $filepath)!=1) {
                   1616:                 mkdir($filepath,0777);
                   1617:             }
                   1618:         }
                   1619:     }
                   1620:     return $filepath;
                   1621: }
                   1622: 
                   1623: sub store_edited_file {
1.638     albertel 1624:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  1625:     my $file = $primary_url;
                   1626:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   1627:     my $fpath = '';
                   1628:     my $fname = $file;
                   1629:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   1630:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1631:     my $filepath = &build_filepath($fpath);
                   1632:     open(my $fh,'>'.$filepath.'/'.$fname);
                   1633:     print $fh $content;
                   1634:     close($fh);
1.638     albertel 1635:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  1636:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1637: 			  $home);
1.637     raeburn  1638:     if ($$fetchresult eq 'ok') {
                   1639:         return '/uploaded/'.$fpath.'/'.$fname;
                   1640:     } else {
1.638     albertel 1641:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1642: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  1643:         return '/adm/notfound.html';
                   1644:     }
                   1645: }
                   1646: 
1.531     albertel 1647: sub clean_filename {
1.831     albertel 1648:     my ($fname,$args)=@_;
1.315     www      1649: # Replace Windows backslashes by forward slashes
1.257     www      1650:     $fname=~s/\\/\//g;
1.831     albertel 1651:     if (!$args->{'keep_path'}) {
                   1652:         # Get rid of everything but the actual filename
                   1653: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   1654:     }
1.315     www      1655: # Replace spaces by underscores
                   1656:     $fname=~s/\s+/\_/g;
                   1657: # Replace all other weird characters by nothing
1.831     albertel 1658:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 1659: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1660: # numbers
                   1661:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1662:     return $fname;
                   1663: }
                   1664: 
1.608     albertel 1665: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 1666: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719     banghart 1667: #                    the desired filenam is in $env{"form.$formname.filename"}
1.686     albertel 1668: #        $coursedoc - if true up to the current course
                   1669: #                     if false
                   1670: #        $subdir - directory in userfile to store the file into
1.858     raeburn  1671: #        $parser - instruction to parse file for objects ($parser = parse)    
                   1672: #        $allfiles - reference to hash for embedded objects
                   1673: #        $codebase - reference to hash for codebase of java objects
                   1674: #        $desuname - username for permanent storage of uploaded file
                   1675: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  1676: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   1677: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858     raeburn  1678: # 
1.686     albertel 1679: # output: url of file in userspace, or error: <message> 
                   1680: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 1681: 
                   1682: 
1.531     albertel 1683: sub userfileupload {
1.860     raeburn  1684:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
                   1685:         $destudom,$thumbwidth,$thumbheight)=@_;
1.531     albertel 1686:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 1687:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 1688:     $fname=&clean_filename($fname);
1.315     www      1689: # See if there is anything left
1.257     www      1690:     unless ($fname) { return 'error: no uploaded file'; }
1.620     albertel 1691:     chop($env{'form.'.$formname});
1.523     raeburn  1692:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   1693:         my $now = time;
                   1694:         my $filepath = 'tmp/helprequests/'.$now;
                   1695:         my @parts=split(/\//,$filepath);
                   1696:         my $fullpath = $perlvar{'lonDaemons'};
                   1697:         for (my $i=0;$i<@parts;$i++) {
                   1698:             $fullpath .= '/'.$parts[$i];
                   1699:             if ((-e $fullpath)!=1) {
                   1700:                 mkdir($fullpath,0777);
                   1701:             }
                   1702:         }
                   1703:         open(my $fh,'>'.$fullpath.'/'.$fname);
1.620     albertel 1704:         print $fh $env{'form.'.$formname};
1.523     raeburn  1705:         close($fh);
1.741     raeburn  1706:         return $fullpath.'/'.$fname;
                   1707:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
                   1708:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   1709:                        '_'.$env{'user.domain'}.'/pending';
                   1710:         my @parts=split(/\//,$filepath);
                   1711:         my $fullpath = $perlvar{'lonDaemons'};
                   1712:         for (my $i=0;$i<@parts;$i++) {
                   1713:             $fullpath .= '/'.$parts[$i];
                   1714:             if ((-e $fullpath)!=1) {
                   1715:                 mkdir($fullpath,0777);
                   1716:             }
                   1717:         }
                   1718:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   1719:         print $fh $env{'form.'.$formname};
                   1720:         close($fh);
                   1721:         return $fullpath.'/'.$fname;
1.523     raeburn  1722:     }
1.719     banghart 1723:     
1.258     www      1724: # Create the directory if not present
1.493     albertel 1725:     $fname="$subdir/$fname";
1.259     www      1726:     if ($coursedoc) {
1.638     albertel 1727: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1728: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  1729:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 1730:             return &finishuserfileupload($docuname,$docudom,
                   1731: 					 $formname,$fname,$parser,$allfiles,
1.860     raeburn  1732: 					 $codebase,$thumbwidth,$thumbheight);
1.481     raeburn  1733:         } else {
1.620     albertel 1734:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 1735:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   1736: 				       $fname,$formname,$parser,
                   1737: 				       $allfiles,$codebase);
1.481     raeburn  1738:         }
1.719     banghart 1739:     } elsif (defined($destuname)) {
                   1740:         my $docuname=$destuname;
                   1741:         my $docudom=$destudom;
1.860     raeburn  1742: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1743: 				     $parser,$allfiles,$codebase,
                   1744:                                      $thumbwidth,$thumbheight);
1.719     banghart 1745:         
1.259     www      1746:     } else {
1.638     albertel 1747:         my $docuname=$env{'user.name'};
                   1748:         my $docudom=$env{'user.domain'};
1.714     raeburn  1749:         if (exists($env{'form.group'})) {
                   1750:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1751:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1752:         }
1.860     raeburn  1753: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1754: 				     $parser,$allfiles,$codebase,
                   1755:                                      $thumbwidth,$thumbheight);
1.259     www      1756:     }
1.271     www      1757: }
                   1758: 
                   1759: sub finishuserfileupload {
1.860     raeburn  1760:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
                   1761:         $thumbwidth,$thumbheight) = @_;
1.477     raeburn  1762:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      1763:     my $filepath=$perlvar{'lonDocRoot'};
1.860     raeburn  1764:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 1765:     $file=$fname;
                   1766:     if ($fname=~m|/|) {
                   1767:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   1768: 	$path.=$fnamepath.'/';
                   1769:     }
1.259     www      1770:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      1771:     my $count;
                   1772:     for ($count=4;$count<=$#parts;$count++) {
                   1773:         $filepath.="/$parts[$count]";
                   1774:         if ((-e $filepath)!=1) {
                   1775: 	    mkdir($filepath,0777);
                   1776:         }
                   1777:     }
                   1778: # Save the file
                   1779:     {
1.701     albertel 1780: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   1781: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   1782: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   1783: 	    return '/adm/notfound.html';
                   1784: 	}
                   1785: 	if (!print FH ($env{'form.'.$formname})) {
                   1786: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   1787: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   1788: 	    return '/adm/notfound.html';
                   1789: 	}
1.570     albertel 1790: 	close(FH);
1.258     www      1791:     }
1.637     raeburn  1792:     if ($parser eq 'parse') {
1.638     albertel 1793:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
                   1794: 						   $codebase);
1.637     raeburn  1795:         unless ($parse_result eq 'ok') {
1.638     albertel 1796:             &logthis('Failed to parse '.$filepath.$file.
                   1797: 		     ' for embedded media: '.$parse_result); 
1.637     raeburn  1798:         }
                   1799:     }
1.860     raeburn  1800:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   1801:         my $input = $filepath.'/'.$file;
                   1802:         my $output = $filepath.'/'.'tn-'.$file;
                   1803:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   1804:         system("convert -sample $thumbsize $input $output");
                   1805:         if (-e $filepath.'/'.'tn-'.$file) {
                   1806:             $fetchthumb  = 1; 
                   1807:         }
                   1808:     }
1.858     raeburn  1809:  
1.259     www      1810: # Notify homeserver to grep it
                   1811: #
1.638     albertel 1812:     my $docuhome=&homeserver($docuname,$docudom);
1.494     albertel 1813:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      1814:     if ($fetchresult eq 'ok') {
1.860     raeburn  1815:         if ($fetchthumb) {
                   1816:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   1817:             if ($thumbresult ne 'ok') {
                   1818:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   1819:                          $docuhome.': '.$thumbresult);
                   1820:             }
                   1821:         }
1.259     www      1822: #
1.258     www      1823: # Return the URL to it
1.494     albertel 1824:         return '/uploaded/'.$path.$file;
1.263     www      1825:     } else {
1.494     albertel 1826:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   1827: 		 ': '.$fetchresult);
1.263     www      1828:         return '/adm/notfound.html';
1.858     raeburn  1829:     }
1.493     albertel 1830: }
                   1831: 
1.637     raeburn  1832: sub extract_embedded_items {
1.648     raeburn  1833:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637     raeburn  1834:     my @state = ();
                   1835:     my %javafiles = (
                   1836:                       codebase => '',
                   1837:                       code => '',
                   1838:                       archive => ''
                   1839:                     );
                   1840:     my %mediafiles = (
                   1841:                       src => '',
                   1842:                       movie => '',
                   1843:                      );
1.648     raeburn  1844:     my $p;
                   1845:     if ($content) {
                   1846:         $p = HTML::LCParser->new($content);
                   1847:     } else {
                   1848:         $p = HTML::LCParser->new($filepath.'/'.$file);
                   1849:     }
1.641     albertel 1850:     while (my $t=$p->get_token()) {
1.640     albertel 1851: 	if ($t->[0] eq 'S') {
                   1852: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 1853: 	    push(@state, $tagname);
1.648     raeburn  1854:             if (lc($tagname) eq 'allow') {
                   1855:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   1856:             }
1.640     albertel 1857: 	    if (lc($tagname) eq 'img') {
                   1858: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   1859: 	    }
1.886     albertel 1860: 	    if (lc($tagname) eq 'a') {
                   1861: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   1862: 	    }
1.645     raeburn  1863:             if (lc($tagname) eq 'script') {
                   1864:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   1865:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   1866:                 } else {
                   1867:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   1868:                 }
                   1869:             }
                   1870:             if (lc($tagname) eq 'link') {
                   1871:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   1872:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   1873:                 }
                   1874:             }
1.640     albertel 1875: 	    if (lc($tagname) eq 'object' ||
                   1876: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   1877: 		foreach my $item (keys(%javafiles)) {
                   1878: 		    $javafiles{$item} = '';
                   1879: 		}
                   1880: 	    }
                   1881: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   1882: 		my $name = lc($attr->{'name'});
                   1883: 		foreach my $item (keys(%javafiles)) {
                   1884: 		    if ($name eq $item) {
                   1885: 			$javafiles{$item} = $attr->{'value'};
                   1886: 			last;
                   1887: 		    }
                   1888: 		}
                   1889: 		foreach my $item (keys(%mediafiles)) {
                   1890: 		    if ($name eq $item) {
                   1891: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   1892: 			last;
                   1893: 		    }
                   1894: 		}
                   1895: 	    }
                   1896: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   1897: 		foreach my $item (keys(%javafiles)) {
                   1898: 		    if ($attr->{$item}) {
                   1899: 			$javafiles{$item} = $attr->{$item};
                   1900: 			last;
                   1901: 		    }
                   1902: 		}
                   1903: 		foreach my $item (keys(%mediafiles)) {
                   1904: 		    if ($attr->{$item}) {
                   1905: 			&add_filetype($allfiles,$attr->{$item},$item);
                   1906: 			last;
                   1907: 		    }
                   1908: 		}
                   1909: 	    }
                   1910: 	} elsif ($t->[0] eq 'E') {
                   1911: 	    my ($tagname) = ($t->[1]);
                   1912: 	    if ($javafiles{'codebase'} ne '') {
                   1913: 		$javafiles{'codebase'} .= '/';
                   1914: 	    }  
                   1915: 	    if (lc($tagname) eq 'applet' ||
                   1916: 		lc($tagname) eq 'object' ||
                   1917: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   1918: 		) {
                   1919: 		foreach my $item (keys(%javafiles)) {
                   1920: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   1921: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   1922: 			&add_filetype($allfiles,$file,$item);
                   1923: 		    }
                   1924: 		}
                   1925: 	    } 
                   1926: 	    pop @state;
                   1927: 	}
                   1928:     }
1.637     raeburn  1929:     return 'ok';
                   1930: }
                   1931: 
1.639     albertel 1932: sub add_filetype {
                   1933:     my ($allfiles,$file,$type)=@_;
                   1934:     if (exists($allfiles->{$file})) {
                   1935: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   1936: 	    push(@{$allfiles->{$file}}, &escape($type));
                   1937: 	}
                   1938:     } else {
                   1939: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  1940:     }
                   1941: }
                   1942: 
1.493     albertel 1943: sub removeuploadedurl {
                   1944:     my ($url)=@_;
                   1945:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613     albertel 1946:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 1947: }
                   1948: 
                   1949: sub removeuserfile {
                   1950:     my ($docuname,$docudom,$fname)=@_;
                   1951:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1952:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
                   1953:     if ($result eq 'ok') {
                   1954:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   1955:             my $metafile = $fname.'.meta';
                   1956:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 1957: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
                   1958:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1959:             my $sqlresult = 
1.823     albertel 1960:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1961:                                         'portfolio_metadata',$group,
                   1962:                                         'delete');
1.798     raeburn  1963:         }
                   1964:     }
                   1965:     return $result;
1.257     www      1966: }
1.15      www      1967: 
1.530     albertel 1968: sub mkdiruserfile {
                   1969:     my ($docuname,$docudom,$dir)=@_;
                   1970:     my $home=&homeserver($docuname,$docudom);
                   1971:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   1972: }
                   1973: 
1.531     albertel 1974: sub renameuserfile {
                   1975:     my ($docuname,$docudom,$old,$new)=@_;
                   1976:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1977:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   1978:                         &escape("$old").':'.&escape("$new"),$home);
                   1979:     if ($result eq 'ok') {
                   1980:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   1981:             my $oldmeta = $old.'.meta';
                   1982:             my $newmeta = $new.'.meta';
                   1983:             my $metaresult = 
                   1984:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 1985: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   1986:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1987:             my $sqlresult = 
1.823     albertel 1988:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1989:                                         'portfolio_metadata',$group,
                   1990:                                         'delete');
1.798     raeburn  1991:         }
                   1992:     }
                   1993:     return $result;
1.531     albertel 1994: }
                   1995: 
1.14      www      1996: # ------------------------------------------------------------------------- Log
                   1997: 
                   1998: sub log {
                   1999:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      2000:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      2001: }
                   2002: 
                   2003: # ------------------------------------------------------------------ Course Log
1.352     www      2004: #
                   2005: # This routine flushes several buffers of non-mission-critical nature
                   2006: #
1.157     www      2007: 
                   2008: sub flushcourselogs {
1.352     www      2009:     &logthis('Flushing log buffers');
                   2010: #
                   2011: # course logs
                   2012: # This is a log of all transactions in a course, which can be used
                   2013: # for data mining purposes
                   2014: #
                   2015: # It also collects the courseid database, which lists last transaction
                   2016: # times and course titles for all courseids
                   2017: #
                   2018:     my %courseidbuffer=();
1.800     albertel 2019:     foreach my $crsid (keys %courselogs) {
1.352     www      2020:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      2021: 		          &escape($courselogs{$crsid}),
                   2022: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      2023: 	    delete $courselogs{$crsid};
                   2024:         } else {
                   2025:             &logthis('Failed to flush log buffer for '.$crsid);
                   2026:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 2027:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      2028:                         " exceeded maximum size, deleting.</font>");
                   2029:                delete $courselogs{$crsid};
                   2030:             }
1.352     www      2031:         }
                   2032:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
                   2033:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516     raeburn  2034: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  2035:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.352     www      2036:         } else {
                   2037:            $courseidbuffer{$coursehombuf{$crsid}}=
1.516     raeburn  2038: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  2039:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.571     raeburn  2040:         }
1.191     harris41 2041:     }
1.352     www      2042: #
                   2043: # Write course id database (reverse lookup) to homeserver of courses 
                   2044: # Is used in pickcourse
                   2045: #
1.840     albertel 2046:     foreach my $crs_home (keys(%courseidbuffer)) {
1.844     albertel 2047:         &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1.840     albertel 2048: 		     $crs_home);
1.352     www      2049:     }
                   2050: #
                   2051: # File accesses
                   2052: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   2053: #
1.449     matthew  2054:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  2055:         if ($entry =~ /___count$/) {
                   2056:             my ($dom,$name);
1.807     albertel 2057:             ($dom,$name,undef)=
1.811     albertel 2058: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  2059:             if (! defined($dom) || $dom eq '' || 
                   2060:                 ! defined($name) || $name eq '') {
1.620     albertel 2061:                 my $cid = $env{'request.course.id'};
                   2062:                 $dom  = $env{'request.'.$cid.'.domain'};
                   2063:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  2064:             }
1.450     matthew  2065:             my $value = $accesshash{$entry};
                   2066:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   2067:             my %temphash=($url => $value);
1.449     matthew  2068:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   2069:             if ($result eq 'ok') {
                   2070:                 delete $accesshash{$entry};
                   2071:             } elsif ($result eq 'unknown_cmd') {
                   2072:                 # Target server has old code running on it.
1.450     matthew  2073:                 my %temphash=($entry => $value);
1.449     matthew  2074:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2075:                     delete $accesshash{$entry};
                   2076:                 }
                   2077:             }
                   2078:         } else {
1.811     albertel 2079:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  2080:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  2081:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2082:                 delete $accesshash{$entry};
                   2083:             }
1.185     www      2084:         }
1.191     harris41 2085:     }
1.352     www      2086: #
                   2087: # Roles
                   2088: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   2089: #
1.800     albertel 2090:     foreach my $entry (keys(%userrolehash)) {
1.351     www      2091:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      2092: 	    split(/\:/,$entry);
                   2093:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      2094:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      2095:                 $rudom,$runame) eq 'ok') {
                   2096: 	    delete $userrolehash{$entry};
                   2097:         }
                   2098:     }
1.662     raeburn  2099: #
                   2100: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   2101: #
                   2102:     my %domrolebuffer = ();
                   2103:     foreach my $entry (keys %domainrolehash) {
1.901     albertel 2104:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split(/:/,$entry);
1.662     raeburn  2105:         if ($domrolebuffer{$rudom}) {
                   2106:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   2107:                       '='.&escape($domainrolehash{$entry});
                   2108:         } else {
                   2109:             $domrolebuffer{$rudom}.=&escape($entry).
                   2110:                       '='.&escape($domainrolehash{$entry});
                   2111:         }
                   2112:         delete $domainrolehash{$entry};
                   2113:     }
                   2114:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 2115: 	my %servers = &get_servers($dom,'library');
                   2116: 	foreach my $tryserver (keys(%servers)) {
                   2117: 	    unless (&reply('domroleput:'.$dom.':'.
                   2118: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   2119: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   2120: 	    }
1.662     raeburn  2121:         }
                   2122:     }
1.186     www      2123:     $dumpcount++;
1.157     www      2124: }
                   2125: 
                   2126: sub courselog {
                   2127:     my $what=shift;
1.158     www      2128:     $what=time.':'.$what;
1.620     albertel 2129:     unless ($env{'request.course.id'}) { return ''; }
                   2130:     $coursedombuf{$env{'request.course.id'}}=
                   2131:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2132:     $coursenumbuf{$env{'request.course.id'}}=
                   2133:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2134:     $coursehombuf{$env{'request.course.id'}}=
                   2135:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2136:     $coursedescrbuf{$env{'request.course.id'}}=
                   2137:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2138:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2139:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2140:     $courseownerbuf{$env{'request.course.id'}}=
                   2141:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2142:     $coursetypebuf{$env{'request.course.id'}}=
                   2143:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2144:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2145: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2146:     } else {
1.620     albertel 2147: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2148:     }
1.620     albertel 2149:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2150: 	&flushcourselogs();
                   2151:     }
1.158     www      2152: }
                   2153: 
                   2154: sub courseacclog {
                   2155:     my $fnsymb=shift;
1.620     albertel 2156:     unless ($env{'request.course.id'}) { return ''; }
                   2157:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2158:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2159:         $what.=':POST';
1.583     matthew  2160:         # FIXME: Probably ought to escape things....
1.800     albertel 2161: 	foreach my $key (keys(%env)) {
                   2162:             if ($key=~/^form\.(.*)/) {
                   2163: 		$what.=':'.$1.'='.$env{$key};
1.158     www      2164:             }
1.191     harris41 2165:         }
1.583     matthew  2166:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2167:         # FIXME: We should not be depending on a form parameter that someone
                   2168:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2169:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2170:             $what.= ':POST';
                   2171:             # FIXME: Probably ought to escape things....
                   2172:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2173:                                  'crsdiscuss') {
1.620     albertel 2174:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2175:             }
                   2176:         }
1.158     www      2177:     }
                   2178:     &courselog($what);
1.149     www      2179: }
                   2180: 
1.185     www      2181: sub countacc {
                   2182:     my $url=&declutter(shift);
1.458     matthew  2183:     return if (! defined($url) || $url eq '');
1.620     albertel 2184:     unless ($env{'request.course.id'}) { return ''; }
                   2185:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2186:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2187:     $accesshash{$key}++;
1.185     www      2188: }
1.349     www      2189: 
1.361     www      2190: sub linklog {
                   2191:     my ($from,$to)=@_;
                   2192:     $from=&declutter($from);
                   2193:     $to=&declutter($to);
                   2194:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   2195:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   2196: }
                   2197:   
1.349     www      2198: sub userrolelog {
                   2199:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  2200:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  2201:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  2202:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
                   2203:         ($trole=~/^ta/)) {
1.350     www      2204:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2205:        $userrolehash
                   2206:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      2207:                     =$tend.':'.$tstart;
1.662     raeburn  2208:     }
1.898     albertel 2209:     if (($env{'request.role'} =~ /dc\./) &&
                   2210: 	(($trole=~/^au/) || ($trole=~/^in/) ||
                   2211: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
                   2212: 	 ($trole=~/^cr/) || ($trole=~/^ta/))) {
                   2213:        $userrolehash
                   2214:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   2215:                     =$tend.':'.$tstart;
                   2216:     }
1.662     raeburn  2217:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   2218:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   2219:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   2220:         ($trole=~/^sc/)) {
                   2221:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2222:        $domainrolehash
                   2223:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   2224:                     = $tend.':'.$tstart;
                   2225:     }
1.351     www      2226: }
                   2227: 
                   2228: sub get_course_adv_roles {
                   2229:     my $cid=shift;
1.620     albertel 2230:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      2231:     my %coursehash=&coursedescription($cid);
1.470     www      2232:     my %nothide=();
1.800     albertel 2233:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2234: 	$nothide{join(':',split(/[\@\:]/,$user))}=1;
1.470     www      2235:     }
1.351     www      2236:     my %returnhash=();
                   2237:     my %dumphash=
                   2238:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   2239:     my $now=time;
1.800     albertel 2240:     foreach my $entry (keys %dumphash) {
                   2241: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      2242:         if (($tstart) && ($tstart<0)) { next; }
                   2243:         if (($tend) && ($tend<$now)) { next; }
                   2244:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 2245:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 2246: 	if ($username eq '' || $domain eq '') { next; }
1.470     www      2247: 	if ((&privileged($username,$domain)) && 
                   2248: 	    (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 2249: 	if ($role eq 'cr') { next; }
1.351     www      2250:         my $key=&plaintext($role);
                   2251:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
                   2252:         if ($returnhash{$key}) {
                   2253: 	    $returnhash{$key}.=','.$username.':'.$domain;
                   2254:         } else {
                   2255:             $returnhash{$key}=$username.':'.$domain;
                   2256:         }
1.400     www      2257:      }
                   2258:     return %returnhash;
                   2259: }
                   2260: 
                   2261: sub get_my_roles {
1.858     raeburn  2262:     my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
1.620     albertel 2263:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   2264:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.858     raeburn  2265:     my %dumphash;
                   2266:     if ($context eq 'userroles') { 
                   2267:         %dumphash = &dump('roles',$udom,$uname);
                   2268:     } else {
                   2269:         %dumphash=
1.400     www      2270:             &dump('nohist_userroles',$udom,$uname);
1.858     raeburn  2271:     }
1.400     www      2272:     my %returnhash=();
                   2273:     my $now=time;
1.800     albertel 2274:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  2275:         my ($role,$tend,$tstart);
                   2276:         if ($context eq 'userroles') {
                   2277: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   2278:         } else {
                   2279:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   2280:         }
1.400     www      2281:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  2282:         my $status = 'active';
                   2283:         if (($tend) && ($tend<$now)) {
                   2284:             $status = 'previous';
                   2285:         } 
                   2286:         if (($tstart) && ($now<$tstart)) {
                   2287:             $status = 'future';
                   2288:         }
                   2289:         if (ref($types) eq 'ARRAY') {
                   2290:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   2291:                 next;
                   2292:             } 
                   2293:         } else {
                   2294:             if ($status ne 'active') {
                   2295:                 next;
                   2296:             }
                   2297:         }
1.867     raeburn  2298:         my ($rolecode,$username,$domain,$section,$area);
                   2299:         if ($context eq 'userroles') {
                   2300:             ($area,$rolecode) = split(/_/,$entry);
                   2301:             (undef,$domain,$username,$section) = split(/\//,$area);
                   2302:         } else {
                   2303:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   2304:         }
1.832     raeburn  2305:         if (ref($roledoms) eq 'ARRAY') {
                   2306:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   2307:                 next;
                   2308:             }
                   2309:         }
                   2310:         if (ref($roles) eq 'ARRAY') {
                   2311:             if (!grep(/^\Q$role\E$/,@{$roles})) {
                   2312:                 next;
                   2313:             }
1.867     raeburn  2314:         }
1.400     www      2315: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.832     raeburn  2316:     }
1.373     www      2317:     return %returnhash;
1.399     www      2318: }
                   2319: 
                   2320: # ----------------------------------------------------- Frontpage Announcements
                   2321: #
                   2322: #
                   2323: 
                   2324: sub postannounce {
                   2325:     my ($server,$text)=@_;
1.844     albertel 2326:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      2327:     unless ($text=~/\w/) { $text=''; }
                   2328:     return &reply('setannounce:'.&escape($text),$server);
                   2329: }
                   2330: 
                   2331: sub getannounce {
1.448     albertel 2332: 
                   2333:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      2334: 	my $announcement='';
1.800     albertel 2335: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 2336: 	close($fh);
1.399     www      2337: 	if ($announcement=~/\w/) { 
                   2338: 	    return 
                   2339:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 2340:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      2341: 	} else {
                   2342: 	    return '';
                   2343: 	}
                   2344:     } else {
                   2345: 	return '';
                   2346:     }
1.351     www      2347: }
1.353     www      2348: 
                   2349: # ---------------------------------------------------------- Course ID routines
                   2350: # Deal with domain's nohist_courseid.db files
                   2351: #
                   2352: 
                   2353: sub courseidput {
                   2354:     my ($domain,$what,$coursehome)=@_;
                   2355:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   2356: }
                   2357: 
                   2358: sub courseiddump {
1.791     raeburn  2359:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
1.353     www      2360:     my %returnhash=();
1.355     www      2361:     unless ($domfilter) { $domfilter=''; }
1.845     albertel 2362:     my %libserv = &all_library();
                   2363:     foreach my $tryserver (keys(%libserv)) {
                   2364:         if ( (  $hostidflag == 1 
                   2365: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   2366: 	     || (!defined($hostidflag)) ) {
                   2367: 
                   2368: 	    if ($domfilter eq ''
                   2369: 		|| (&host_domain($tryserver) eq $domfilter)) {
1.800     albertel 2370: 	        foreach my $line (
1.844     albertel 2371:                  split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
1.571     raeburn  2372: 			       $sincefilter.':'.&escape($descfilter).':'.
1.791     raeburn  2373:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
1.354     www      2374:                                $tryserver))) {
1.800     albertel 2375: 		    my ($key,$value)=split(/\=/,$line,2);
1.506     raeburn  2376:                     if (($key) && ($value)) {
1.516     raeburn  2377: 		        $returnhash{&unescape($key)}=$value;
1.506     raeburn  2378:                     }
1.353     www      2379:                 }
                   2380:             }
                   2381:         }
                   2382:     }
                   2383:     return %returnhash;
                   2384: }
                   2385: 
1.658     raeburn  2386: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  2387: 
                   2388: sub dcmailput {
1.685     raeburn  2389:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  2390:     my $status = &Apache::lonnet::critical(
1.740     www      2391:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   2392:        &escape($message),$server);
1.662     raeburn  2393:     return $status;
                   2394: }
                   2395: 
1.658     raeburn  2396: sub dcmaildump {
                   2397:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  2398:     my %returnhash=();
1.846     albertel 2399: 
                   2400:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  2401:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   2402:                                                          &escape($enddate).':';
                   2403: 	my @esc_senders=map { &escape($_)} @$senders;
                   2404: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 2405: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 2406:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  2407:             if (($key) && ($value)) {
                   2408:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  2409:             }
                   2410:         }
                   2411:     }
                   2412:     return %returnhash;
                   2413: }
1.662     raeburn  2414: # ---------------------------------------------------------- Domain roles
                   2415: 
                   2416: sub get_domain_roles {
                   2417:     my ($dom,$roles,$startdate,$enddate)=@_;
                   2418:     if (undef($startdate) || $startdate eq '') {
                   2419:         $startdate = '.';
                   2420:     }
                   2421:     if (undef($enddate) || $enddate eq '') {
                   2422:         $enddate = '.';
                   2423:     }
                   2424:     my $rolelist = join(':',@{$roles});
                   2425:     my %personnel = ();
1.841     albertel 2426: 
                   2427:     my %servers = &get_servers($dom,'library');
                   2428:     foreach my $tryserver (keys(%servers)) {
                   2429: 	%{$personnel{$tryserver}}=();
                   2430: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   2431: 					    &escape($startdate).':'.
                   2432: 					    &escape($enddate).':'.
                   2433: 					    &escape($rolelist), $tryserver))) {
                   2434: 	    my ($key,$value) = split(/\=/,$line,2);
                   2435: 	    if (($key) && ($value)) {
                   2436: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   2437: 	    }
                   2438: 	}
1.662     raeburn  2439:     }
                   2440:     return %personnel;
                   2441: }
1.658     raeburn  2442: 
1.149     www      2443: # ----------------------------------------------------------- Check out an item
                   2444: 
1.504     albertel 2445: sub get_first_access {
                   2446:     my ($type,$argsymb)=@_;
1.790     albertel 2447:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2448:     if ($argsymb) { $symb=$argsymb; }
                   2449:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2450:     if ($type eq 'map') {
                   2451: 	$res=&symbread($map);
                   2452:     } else {
                   2453: 	$res=$symb;
                   2454:     }
                   2455:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   2456:     return $times{"$courseid\0$res"};
1.504     albertel 2457: }
                   2458: 
                   2459: sub set_first_access {
                   2460:     my ($type)=@_;
1.790     albertel 2461:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2462:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2463:     if ($type eq 'map') {
                   2464: 	$res=&symbread($map);
                   2465:     } else {
                   2466: 	$res=$symb;
                   2467:     }
                   2468:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 2469:     if (!$firstaccess) {
1.588     albertel 2470: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 2471:     }
                   2472:     return 'already_set';
1.504     albertel 2473: }
                   2474: 
1.149     www      2475: sub checkout {
                   2476:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   2477:     my $now=time;
                   2478:     my $lonhost=$perlvar{'lonHostID'};
                   2479:     my $infostr=&escape(
1.234     www      2480:                  'CHECKOUTTOKEN&'.
1.149     www      2481:                  $tuname.'&'.
                   2482:                  $tudom.'&'.
                   2483:                  $tcrsid.'&'.
                   2484:                  $symb.'&'.
                   2485: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   2486:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      2487:     if ($token=~/^error\:/) { 
1.672     albertel 2488:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2489:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2490:                  "</font>");
                   2491:         return ''; 
                   2492:     }
                   2493: 
1.149     www      2494:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   2495:     $token=~tr/a-z/A-Z/;
                   2496: 
1.153     www      2497:     my %infohash=('resource.0.outtoken' => $token,
                   2498:                   'resource.0.checkouttime' => $now,
                   2499:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      2500: 
                   2501:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2502:        return '';
1.151     www      2503:     } else {
1.672     albertel 2504:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2505:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2506:                  "</font>");
1.149     www      2507:     }    
                   2508: 
                   2509:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2510:                          &escape('Checkout '.$infostr.' - '.
                   2511:                                                  $token)) ne 'ok') {
                   2512: 	return '';
1.151     www      2513:     } else {
1.672     albertel 2514:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2515:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2516:                  "</font>");
1.149     www      2517:     }
1.151     www      2518:     return $token;
1.149     www      2519: }
                   2520: 
                   2521: # ------------------------------------------------------------ Check in an item
                   2522: 
                   2523: sub checkin {
                   2524:     my $token=shift;
1.150     www      2525:     my $now=time;
                   2526:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   2527:     $lonhost=~tr/A-Z/a-z/;
1.838     albertel 2528:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150     www      2529:     $dtoken=~s/\W/\_/g;
1.234     www      2530:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      2531:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   2532: 
1.154     www      2533:     unless (($tuname) && ($tudom)) {
                   2534:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   2535:         return '';
                   2536:     }
                   2537:     
                   2538:     unless (&allowed('mgr',$tcrsid)) {
                   2539:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620     albertel 2540:                  $env{'user.name'}.' - '.$env{'user.domain'});
1.154     www      2541:         return '';
                   2542:     }
                   2543: 
1.153     www      2544:     my %infohash=('resource.0.intoken' => $token,
                   2545:                   'resource.0.checkintime' => $now,
                   2546:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      2547: 
                   2548:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2549:        return '';
                   2550:     }    
                   2551: 
                   2552:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2553:                          &escape('Checkin - '.$token)) ne 'ok') {
                   2554: 	return '';
                   2555:     }
                   2556: 
                   2557:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      2558: }
                   2559: 
                   2560: # --------------------------------------------- Set Expire Date for Spreadsheet
                   2561: 
                   2562: sub expirespread {
                   2563:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 2564:     my $cid=$env{'request.course.id'}; 
1.110     www      2565:     if ($cid) {
                   2566:        my $now=time;
                   2567:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 2568:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   2569:                             $env{'course.'.$cid.'.num'}.
1.110     www      2570: 	        	    ':nohist_expirationdates:'.
                   2571:                             &escape($key).'='.$now,
1.620     albertel 2572:                             $env{'course.'.$cid.'.home'})
1.110     www      2573:     }
                   2574:     return 'ok';
1.14      www      2575: }
                   2576: 
1.109     www      2577: # ----------------------------------------------------- Devalidate Spreadsheets
                   2578: 
                   2579: sub devalidate {
1.325     www      2580:     my ($symb,$uname,$udom)=@_;
1.620     albertel 2581:     my $cid=$env{'request.course.id'}; 
1.109     www      2582:     if ($cid) {
1.391     matthew  2583:         # delete the stored spreadsheets for
                   2584:         # - the student level sheet of this user in course's homespace
                   2585:         # - the assessment level sheet for this resource 
                   2586:         #   for this user in user's homespace
1.553     albertel 2587: 	# - current conditional state info
1.325     www      2588: 	my $key=$uname.':'.$udom.':';
1.109     www      2589:         my $status=
1.299     matthew  2590: 	    &del('nohist_calculatedsheets',
1.391     matthew  2591: 		 [$key.'studentcalc:'],
1.620     albertel 2592: 		 $env{'course.'.$cid.'.domain'},
                   2593: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 2594: 		.' '.
                   2595: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  2596: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      2597:         unless ($status eq 'ok ok') {
                   2598:            &logthis('Could not devalidate spreadsheet '.
1.325     www      2599:                     $uname.' at '.$udom.' for '.
1.109     www      2600: 		    $symb.': '.$status);
1.133     albertel 2601:         }
1.553     albertel 2602: 	&delenv('user.state.'.$cid);
1.109     www      2603:     }
                   2604: }
                   2605: 
1.265     albertel 2606: sub get_scalar {
                   2607:     my ($string,$end) = @_;
                   2608:     my $value;
                   2609:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   2610: 	$value = $1;
                   2611:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   2612: 	$value = $1;
                   2613:     }
                   2614:     return &unescape($value);
                   2615: }
                   2616: 
                   2617: sub array2str {
                   2618:   my (@array) = @_;
                   2619:   my $result=&arrayref2str(\@array);
                   2620:   $result=~s/^__ARRAY_REF__//;
                   2621:   $result=~s/__END_ARRAY_REF__$//;
                   2622:   return $result;
                   2623: }
                   2624: 
1.204     albertel 2625: sub arrayref2str {
                   2626:   my ($arrayref) = @_;
1.265     albertel 2627:   my $result='__ARRAY_REF__';
1.204     albertel 2628:   foreach my $elem (@$arrayref) {
1.265     albertel 2629:     if(ref($elem) eq 'ARRAY') {
                   2630:       $result.=&arrayref2str($elem).'&';
                   2631:     } elsif(ref($elem) eq 'HASH') {
                   2632:       $result.=&hashref2str($elem).'&';
                   2633:     } elsif(ref($elem)) {
                   2634:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 2635:     } else {
                   2636:       $result.=&escape($elem).'&';
                   2637:     }
                   2638:   }
                   2639:   $result=~s/\&$//;
1.265     albertel 2640:   $result .= '__END_ARRAY_REF__';
1.204     albertel 2641:   return $result;
                   2642: }
                   2643: 
1.168     albertel 2644: sub hash2str {
1.204     albertel 2645:   my (%hash) = @_;
                   2646:   my $result=&hashref2str(\%hash);
1.265     albertel 2647:   $result=~s/^__HASH_REF__//;
                   2648:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 2649:   return $result;
                   2650: }
                   2651: 
                   2652: sub hashref2str {
                   2653:   my ($hashref)=@_;
1.265     albertel 2654:   my $result='__HASH_REF__';
1.800     albertel 2655:   foreach my $key (sort(keys(%$hashref))) {
                   2656:     if (ref($key) eq 'ARRAY') {
                   2657:       $result.=&arrayref2str($key).'=';
                   2658:     } elsif (ref($key) eq 'HASH') {
                   2659:       $result.=&hashref2str($key).'=';
                   2660:     } elsif (ref($key)) {
1.265     albertel 2661:       $result.='=';
1.800     albertel 2662:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 2663:     } else {
1.800     albertel 2664: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 2665:     }
                   2666: 
1.800     albertel 2667:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   2668:       $result.=&arrayref2str($hashref->{$key}).'&';
                   2669:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   2670:       $result.=&hashref2str($hashref->{$key}).'&';
                   2671:     } elsif(ref($hashref->{$key})) {
1.265     albertel 2672:        $result.='&';
1.800     albertel 2673:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 2674:     } else {
1.800     albertel 2675:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 2676:     }
                   2677:   }
1.168     albertel 2678:   $result=~s/\&$//;
1.265     albertel 2679:   $result .= '__END_HASH_REF__';
1.168     albertel 2680:   return $result;
                   2681: }
                   2682: 
                   2683: sub str2hash {
1.265     albertel 2684:     my ($string)=@_;
                   2685:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   2686:     return %$hash;
                   2687: }
                   2688: 
                   2689: sub str2hashref {
1.168     albertel 2690:   my ($string) = @_;
1.265     albertel 2691: 
                   2692:   my %hash;
                   2693: 
                   2694:   if($string !~ /^__HASH_REF__/) {
                   2695:       if (! ($string eq '' || !defined($string))) {
                   2696: 	  $hash{'error'}='Not hash reference';
                   2697:       }
                   2698:       return (\%hash, $string);
                   2699:   }
                   2700: 
                   2701:   $string =~ s/^__HASH_REF__//;
                   2702: 
                   2703:   while($string !~ /^__END_HASH_REF__/) {
                   2704:       #key
                   2705:       my $key='';
                   2706:       if($string =~ /^__HASH_REF__/) {
                   2707:           ($key, $string)=&str2hashref($string);
                   2708:           if(defined($key->{'error'})) {
                   2709:               $hash{'error'}='Bad data';
                   2710:               return (\%hash, $string);
                   2711:           }
                   2712:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2713:           ($key, $string)=&str2arrayref($string);
                   2714:           if($key->[0] eq 'Array reference error') {
                   2715:               $hash{'error'}='Bad data';
                   2716:               return (\%hash, $string);
                   2717:           }
                   2718:       } else {
                   2719:           $string =~ s/^(.*?)=//;
1.267     albertel 2720: 	  $key=&unescape($1);
1.265     albertel 2721:       }
                   2722:       $string =~ s/^=//;
                   2723: 
                   2724:       #value
                   2725:       my $value='';
                   2726:       if($string =~ /^__HASH_REF__/) {
                   2727:           ($value, $string)=&str2hashref($string);
                   2728:           if(defined($value->{'error'})) {
                   2729:               $hash{'error'}='Bad data';
                   2730:               return (\%hash, $string);
                   2731:           }
                   2732:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2733:           ($value, $string)=&str2arrayref($string);
                   2734:           if($value->[0] eq 'Array reference error') {
                   2735:               $hash{'error'}='Bad data';
                   2736:               return (\%hash, $string);
                   2737:           }
                   2738:       } else {
                   2739: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   2740:       }
                   2741:       $string =~ s/^&//;
                   2742: 
                   2743:       $hash{$key}=$value;
1.204     albertel 2744:   }
1.265     albertel 2745: 
                   2746:   $string =~ s/^__END_HASH_REF__//;
                   2747: 
                   2748:   return (\%hash, $string);
1.204     albertel 2749: }
                   2750: 
                   2751: sub str2array {
1.265     albertel 2752:     my ($string)=@_;
                   2753:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   2754:     return @$array;
                   2755: }
                   2756: 
                   2757: sub str2arrayref {
1.204     albertel 2758:   my ($string) = @_;
1.265     albertel 2759:   my @array;
                   2760: 
                   2761:   if($string !~ /^__ARRAY_REF__/) {
                   2762:       if (! ($string eq '' || !defined($string))) {
                   2763: 	  $array[0]='Array reference error';
                   2764:       }
                   2765:       return (\@array, $string);
                   2766:   }
                   2767: 
                   2768:   $string =~ s/^__ARRAY_REF__//;
                   2769: 
                   2770:   while($string !~ /^__END_ARRAY_REF__/) {
                   2771:       my $value='';
                   2772:       if($string =~ /^__HASH_REF__/) {
                   2773:           ($value, $string)=&str2hashref($string);
                   2774:           if(defined($value->{'error'})) {
                   2775:               $array[0] ='Array reference error';
                   2776:               return (\@array, $string);
                   2777:           }
                   2778:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2779:           ($value, $string)=&str2arrayref($string);
                   2780:           if($value->[0] eq 'Array reference error') {
                   2781:               $array[0] ='Array reference error';
                   2782:               return (\@array, $string);
                   2783:           }
                   2784:       } else {
                   2785: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   2786:       }
                   2787:       $string =~ s/^&//;
                   2788: 
                   2789:       push(@array, $value);
1.191     harris41 2790:   }
1.265     albertel 2791: 
                   2792:   $string =~ s/^__END_ARRAY_REF__//;
                   2793: 
                   2794:   return (\@array, $string);
1.168     albertel 2795: }
                   2796: 
1.167     albertel 2797: # -------------------------------------------------------------------Temp Store
                   2798: 
1.168     albertel 2799: sub tmpreset {
                   2800:   my ($symb,$namespace,$domain,$stuname) = @_;
                   2801:   if (!$symb) {
                   2802:     $symb=&symbread();
1.620     albertel 2803:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2804:   }
                   2805:   $symb=escape($symb);
                   2806: 
1.620     albertel 2807:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 2808:   $namespace=~s/\//\_/g;
                   2809:   $namespace=~s/\W//g;
                   2810: 
1.620     albertel 2811:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2812:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2813:   if ($domain eq 'public' && $stuname eq 'public') {
                   2814:       $stuname=$ENV{'REMOTE_ADDR'};
                   2815:   }
1.168     albertel 2816:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2817:   my %hash;
                   2818:   if (tie(%hash,'GDBM_File',
                   2819: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2820: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2821:     foreach my $key (keys %hash) {
1.180     albertel 2822:       if ($key=~ /:$symb/) {
1.168     albertel 2823: 	delete($hash{$key});
                   2824:       }
                   2825:     }
                   2826:   }
                   2827: }
                   2828: 
1.167     albertel 2829: sub tmpstore {
1.168     albertel 2830:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2831: 
                   2832:   if (!$symb) {
                   2833:     $symb=&symbread();
1.620     albertel 2834:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2835:   }
                   2836:   $symb=escape($symb);
                   2837: 
                   2838:   if (!$namespace) {
                   2839:     # I don't think we would ever want to store this for a course.
                   2840:     # it seems this will only be used if we don't have a course.
1.620     albertel 2841:     #$namespace=$env{'request.course.id'};
1.168     albertel 2842:     #if (!$namespace) {
1.620     albertel 2843:       $namespace=$env{'request.state'};
1.168     albertel 2844:     #}
                   2845:   }
                   2846:   $namespace=~s/\//\_/g;
                   2847:   $namespace=~s/\W//g;
1.620     albertel 2848:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2849:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2850:   if ($domain eq 'public' && $stuname eq 'public') {
                   2851:       $stuname=$ENV{'REMOTE_ADDR'};
                   2852:   }
1.168     albertel 2853:   my $now=time;
                   2854:   my %hash;
                   2855:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2856:   if (tie(%hash,'GDBM_File',
                   2857: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2858: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2859:     $hash{"version:$symb"}++;
                   2860:     my $version=$hash{"version:$symb"};
                   2861:     my $allkeys=''; 
                   2862:     foreach my $key (keys(%$storehash)) {
                   2863:       $allkeys.=$key.':';
1.591     albertel 2864:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 2865:     }
                   2866:     $hash{"$version:$symb:timestamp"}=$now;
                   2867:     $allkeys.='timestamp';
                   2868:     $hash{"$version:keys:$symb"}=$allkeys;
                   2869:     if (untie(%hash)) {
                   2870:       return 'ok';
                   2871:     } else {
                   2872:       return "error:$!";
                   2873:     }
                   2874:   } else {
                   2875:     return "error:$!";
                   2876:   }
                   2877: }
1.167     albertel 2878: 
1.168     albertel 2879: # -----------------------------------------------------------------Temp Restore
1.167     albertel 2880: 
1.168     albertel 2881: sub tmprestore {
                   2882:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 2883: 
1.168     albertel 2884:   if (!$symb) {
                   2885:     $symb=&symbread();
1.620     albertel 2886:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2887:   }
                   2888:   $symb=escape($symb);
                   2889: 
1.620     albertel 2890:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 2891: 
1.620     albertel 2892:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2893:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2894:   if ($domain eq 'public' && $stuname eq 'public') {
                   2895:       $stuname=$ENV{'REMOTE_ADDR'};
                   2896:   }
1.168     albertel 2897:   my %returnhash;
                   2898:   $namespace=~s/\//\_/g;
                   2899:   $namespace=~s/\W//g;
                   2900:   my %hash;
                   2901:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2902:   if (tie(%hash,'GDBM_File',
                   2903: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2904: 	  &GDBM_READER(),0640)) {
1.168     albertel 2905:     my $version=$hash{"version:$symb"};
                   2906:     $returnhash{'version'}=$version;
                   2907:     my $scope;
                   2908:     for ($scope=1;$scope<=$version;$scope++) {
                   2909:       my $vkeys=$hash{"$scope:keys:$symb"};
                   2910:       my @keys=split(/:/,$vkeys);
                   2911:       my $key;
                   2912:       $returnhash{"$scope:keys"}=$vkeys;
                   2913:       foreach $key (@keys) {
1.591     albertel 2914: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   2915: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 2916:       }
                   2917:     }
1.168     albertel 2918:     if (!(untie(%hash))) {
                   2919:       return "error:$!";
                   2920:     }
                   2921:   } else {
                   2922:     return "error:$!";
                   2923:   }
                   2924:   return %returnhash;
1.167     albertel 2925: }
                   2926: 
1.9       www      2927: # ----------------------------------------------------------------------- Store
                   2928: 
                   2929: sub store {
1.124     www      2930:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2931:     my $home='';
                   2932: 
1.168     albertel 2933:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2934: 
1.213     www      2935:     $symb=&symbclean($symb);
1.122     albertel 2936:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2937: 
1.620     albertel 2938:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2939:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2940: 
                   2941:     &devalidate($symb,$stuname,$domain);
1.109     www      2942: 
                   2943:     $symb=escape($symb);
1.187     www      2944:     if (!$namespace) { 
1.620     albertel 2945:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2946:           return ''; 
                   2947:        } 
                   2948:     }
1.620     albertel 2949:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2950: 
                   2951:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2952:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2953: 
1.12      www      2954:     my $namevalue='';
1.800     albertel 2955:     foreach my $key (keys(%$storehash)) {
                   2956:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2957:     }
1.12      www      2958:     $namevalue=~s/\&$//;
1.187     www      2959:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      2960:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      2961: }
                   2962: 
1.47      www      2963: # -------------------------------------------------------------- Critical Store
                   2964: 
                   2965: sub cstore {
1.124     www      2966:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2967:     my $home='';
                   2968: 
1.168     albertel 2969:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2970: 
1.213     www      2971:     $symb=&symbclean($symb);
1.122     albertel 2972:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2973: 
1.620     albertel 2974:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2975:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2976: 
                   2977:     &devalidate($symb,$stuname,$domain);
1.109     www      2978: 
                   2979:     $symb=escape($symb);
1.187     www      2980:     if (!$namespace) { 
1.620     albertel 2981:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2982:           return ''; 
                   2983:        } 
                   2984:     }
1.620     albertel 2985:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2986: 
                   2987:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2988:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 2989: 
1.47      www      2990:     my $namevalue='';
1.800     albertel 2991:     foreach my $key (keys(%$storehash)) {
                   2992:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2993:     }
1.47      www      2994:     $namevalue=~s/\&$//;
1.187     www      2995:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      2996:     return critical
                   2997:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      2998: }
                   2999: 
1.9       www      3000: # --------------------------------------------------------------------- Restore
                   3001: 
                   3002: sub restore {
1.124     www      3003:     my ($symb,$namespace,$domain,$stuname) = @_;
                   3004:     my $home='';
                   3005: 
1.168     albertel 3006:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      3007: 
1.122     albertel 3008:     if (!$symb) {
                   3009:       unless ($symb=escape(&symbread())) { return ''; }
                   3010:     } else {
1.213     www      3011:       $symb=&escape(&symbclean($symb));
1.122     albertel 3012:     }
1.188     www      3013:     if (!$namespace) { 
1.620     albertel 3014:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      3015:           return ''; 
                   3016:        } 
                   3017:     }
1.620     albertel 3018:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3019:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   3020:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 3021:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   3022: 
1.12      www      3023:     my %returnhash=();
1.800     albertel 3024:     foreach my $line (split(/\&/,$answer)) {
                   3025: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 3026:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 3027:     }
1.75      www      3028:     my $version;
                   3029:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 3030:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   3031:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 3032:        }
1.75      www      3033:     }
1.13      www      3034:     return %returnhash;
1.34      www      3035: }
                   3036: 
                   3037: # ---------------------------------------------------------- Course Description
                   3038: 
                   3039: sub coursedescription {
1.731     albertel 3040:     my ($courseid,$args)=@_;
1.34      www      3041:     $courseid=~s/^\///;
1.49      www      3042:     $courseid=~s/\_/\//g;
1.34      www      3043:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 3044:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 3045:     my $normalid=$cdomain.'_'.$cnum;
                   3046:     # need to always cache even if we get errors otherwise we keep 
                   3047:     # trying and trying and trying to get the course description.
                   3048:     my %envhash=();
                   3049:     my %returnhash=();
1.731     albertel 3050:     
                   3051:     my $expiretime=600;
                   3052:     if ($env{'request.course.id'} eq $normalid) {
                   3053: 	$expiretime=120;
                   3054:     }
                   3055: 
                   3056:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   3057:     if (!$args->{'freshen_cache'}
                   3058: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   3059: 	foreach my $key (keys(%env)) {
                   3060: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   3061: 	    my ($setting) = $1;
                   3062: 	    $returnhash{$setting} = $env{$key};
                   3063: 	}
                   3064: 	return %returnhash;
                   3065:     }
                   3066: 
                   3067:     # get the data agin
                   3068:     if (!$args->{'one_time'}) {
                   3069: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   3070:     }
1.811     albertel 3071: 
1.34      www      3072:     if ($chome ne 'no_host') {
1.302     albertel 3073:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 3074:        if (!exists($returnhash{'con_lost'})) {
                   3075:            $returnhash{'home'}= $chome;
                   3076: 	   $returnhash{'domain'} = $cdomain;
                   3077: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  3078:            if (!defined($returnhash{'type'})) {
                   3079:                $returnhash{'type'} = 'Course';
                   3080:            }
1.130     albertel 3081:            while (my ($name,$value) = each %returnhash) {
1.53      www      3082:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 3083:            }
1.270     www      3084:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      3085:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 3086: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      3087:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   3088:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   3089:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      3090:        }
                   3091:     }
1.731     albertel 3092:     if (!$args->{'one_time'}) {
                   3093: 	&appenv(%envhash);
                   3094:     }
1.302     albertel 3095:     return %returnhash;
1.461     www      3096: }
                   3097: 
                   3098: # -------------------------------------------------See if a user is privileged
                   3099: 
                   3100: sub privileged {
                   3101:     my ($username,$domain)=@_;
                   3102:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   3103: 			&homeserver($username,$domain));
                   3104:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   3105:     my $now=time;
                   3106:     if ($rolesdump ne '') {
1.800     albertel 3107:         foreach my $entry (split(/&/,$rolesdump)) {
                   3108: 	    if ($entry!~/^rolesdef_/) {
                   3109: 		my ($area,$role)=split(/=/,$entry);
1.461     www      3110: 		$area=~s/\_\w\w$//;
                   3111: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   3112: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   3113: 		    my $active=1;
                   3114: 		    if ($tend) {
                   3115: 			if ($tend<$now) { $active=0; }
                   3116: 		    }
                   3117: 		    if ($tstart) {
                   3118: 			if ($tstart>$now) { $active=0; }
                   3119: 		    }
                   3120: 		    if ($active) { return 1; }
                   3121: 		}
                   3122: 	    }
                   3123: 	}
                   3124:     }
                   3125:     return 0;
1.9       www      3126: }
1.1       albertel 3127: 
1.103     harris41 3128: # -------------------------------------------------------- Get user privileges
1.11      www      3129: 
                   3130: sub rolesinit {
                   3131:     my ($domain,$username,$authhost)=@_;
                   3132:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      3133:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      3134:     my %allroles=();
1.678     raeburn  3135:     my %allgroups=();   
1.11      www      3136:     my $now=time;
1.743     albertel 3137:     my %userroles = ('user.login.time' => $now);
1.678     raeburn  3138:     my $group_privs;
1.11      www      3139: 
                   3140:     if ($rolesdump ne '') {
1.800     albertel 3141:         foreach my $entry (split(/&/,$rolesdump)) {
                   3142: 	  if ($entry!~/^rolesdef_/) {
                   3143:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 3144: 	    $area=~s/\_\w\w$//;
1.678     raeburn  3145:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 3146: 	    if ($role=~/^cr/) { 
1.807     albertel 3147: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   3148: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 3149: 		    ($tend,$tstart)=split('_',$trest);
                   3150: 		} else {
                   3151: 		    $trole=$role;
                   3152: 		}
1.678     raeburn  3153:             } elsif ($role =~ m|^gr/|) {
                   3154:                 ($trole,$tend,$tstart) = split(/_/,$role);
                   3155:                 ($trole,$group_privs) = split(/\//,$trole);
                   3156:                 $group_privs = &unescape($group_privs);
1.587     albertel 3157: 	    } else {
                   3158: 		($trole,$tend,$tstart)=split(/_/,$role);
                   3159: 	    }
1.743     albertel 3160: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   3161: 					 $username);
                   3162: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  3163:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   3164:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      3165:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 3166: 		my $spec=$trole.'.'.$area;
                   3167: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3168: 		if ($trole =~ /^cr\//) {
1.567     raeburn  3169:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  3170:                 } elsif ($trole eq 'gr') {
                   3171:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 3172: 		} else {
1.567     raeburn  3173:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 3174: 		}
1.12      www      3175:             }
1.662     raeburn  3176:           }
1.191     harris41 3177:         }
1.743     albertel 3178:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   3179:         $userroles{'user.adv'}    = $adv;
                   3180: 	$userroles{'user.author'} = $author;
1.620     albertel 3181:         $env{'user.adv'}=$adv;
1.11      www      3182:     }
1.743     albertel 3183:     return \%userroles;  
1.11      www      3184: }
                   3185: 
1.567     raeburn  3186: sub set_arearole {
                   3187:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   3188: # log the associated role with the area
                   3189:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 3190:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  3191: }
                   3192: 
                   3193: sub custom_roleprivs {
                   3194:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   3195:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   3196:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 3197:     if (&hostname($homsvr) ne '') {
1.567     raeburn  3198:         my ($rdummy,$roledef)=
                   3199:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   3200:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   3201:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   3202:             if (defined($syspriv)) {
                   3203:                 $$allroles{'cm./'}.=':'.$syspriv;
                   3204:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   3205:             }
                   3206:             if ($tdomain ne '') {
                   3207:                 if (defined($dompriv)) {
                   3208:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   3209:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   3210:                 }
                   3211:                 if (($trest ne '') && (defined($coursepriv))) {
                   3212:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   3213:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   3214:                 }
                   3215:             }
                   3216:         }
                   3217:     }
                   3218: }
                   3219: 
1.678     raeburn  3220: sub group_roleprivs {
                   3221:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   3222:     my $access = 1;
                   3223:     my $now = time;
                   3224:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   3225:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   3226:     if ($access) {
1.811     albertel 3227:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  3228:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   3229:     }
                   3230: }
1.567     raeburn  3231: 
                   3232: sub standard_roleprivs {
                   3233:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   3234:     if (defined($pr{$trole.':s'})) {
                   3235:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   3236:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   3237:     }
                   3238:     if ($tdomain ne '') {
                   3239:         if (defined($pr{$trole.':d'})) {
                   3240:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3241:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3242:         }
                   3243:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   3244:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   3245:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   3246:         }
                   3247:     }
                   3248: }
                   3249: 
                   3250: sub set_userprivs {
1.678     raeburn  3251:     my ($userroles,$allroles,$allgroups) = @_; 
1.567     raeburn  3252:     my $author=0;
                   3253:     my $adv=0;
1.678     raeburn  3254:     my %grouproles = ();
                   3255:     if (keys(%{$allgroups}) > 0) {
                   3256:         foreach my $role (keys %{$allroles}) {
1.681     raeburn  3257:             my ($trole,$area,$sec,$extendedarea);
1.881     raeburn  3258:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
1.678     raeburn  3259:                 $trole = $1;
                   3260:                 $area = $2;
1.681     raeburn  3261:                 $sec = $3;
                   3262:                 $extendedarea = $area.$sec;
                   3263:                 if (exists($$allgroups{$area})) {
                   3264:                     foreach my $group (keys(%{$$allgroups{$area}})) {
                   3265:                         my $spec = $trole.'.'.$extendedarea;
                   3266:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
                   3267:                                                 $$allgroups{$area}{$group};
1.678     raeburn  3268:                     }
                   3269:                 }
                   3270:             }
                   3271:         }
                   3272:     }
1.800     albertel 3273:     foreach my $group (keys(%grouproles)) {
                   3274:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  3275:     }
1.800     albertel 3276:     foreach my $role (keys(%{$allroles})) {
                   3277:         my %thesepriv;
                   3278:         if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
                   3279:         foreach my $item (split(/:/,$$allroles{$role})) {
                   3280:             if ($item ne '') {
                   3281:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  3282:                 if ($restrictions eq '') {
                   3283:                     $thesepriv{$privilege}='F';
                   3284:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   3285:                     $thesepriv{$privilege}.=$restrictions;
                   3286:                 }
                   3287:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   3288:             }
                   3289:         }
                   3290:         my $thesestr='';
1.800     albertel 3291:         foreach my $priv (keys(%thesepriv)) {
                   3292: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   3293: 	}
                   3294:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  3295:     }
                   3296:     return ($author,$adv);
                   3297: }
                   3298: 
1.12      www      3299: # --------------------------------------------------------------- get interface
                   3300: 
                   3301: sub get {
1.131     albertel 3302:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3303:    my $items='';
1.800     albertel 3304:    foreach my $item (@$storearr) {
                   3305:        $items.=&escape($item).'&';
1.191     harris41 3306:    }
1.12      www      3307:    $items=~s/\&$//;
1.620     albertel 3308:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3309:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 3310:    my $uhome=&homeserver($uname,$udomain);
                   3311: 
1.133     albertel 3312:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3313:    my @pairs=split(/\&/,$rep);
1.273     albertel 3314:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   3315:      return @pairs;
                   3316:    }
1.15      www      3317:    my %returnhash=();
1.42      www      3318:    my $i=0;
1.800     albertel 3319:    foreach my $item (@$storearr) {
                   3320:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3321:       $i++;
1.191     harris41 3322:    }
1.15      www      3323:    return %returnhash;
1.27      www      3324: }
                   3325: 
                   3326: # --------------------------------------------------------------- del interface
                   3327: 
                   3328: sub del {
1.133     albertel 3329:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      3330:    my $items='';
1.800     albertel 3331:    foreach my $item (@$storearr) {
                   3332:        $items.=&escape($item).'&';
1.191     harris41 3333:    }
1.27      www      3334:    $items=~s/\&$//;
1.620     albertel 3335:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3336:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3337:    my $uhome=&homeserver($uname,$udomain);
                   3338: 
                   3339:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3340: }
                   3341: 
                   3342: # -------------------------------------------------------------- dump interface
                   3343: 
                   3344: sub dump {
1.755     albertel 3345:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
                   3346:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3347:     if (!$uname) { $uname=$env{'user.name'}; }
                   3348:     my $uhome=&homeserver($uname,$udomain);
                   3349:     if ($regexp) {
                   3350: 	$regexp=&escape($regexp);
                   3351:     } else {
                   3352: 	$regexp='.';
                   3353:     }
                   3354:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3355:     my @pairs=split(/\&/,$rep);
                   3356:     my %returnhash=();
                   3357:     foreach my $item (@pairs) {
                   3358: 	my ($key,$value)=split(/=/,$item,2);
                   3359: 	$key = &unescape($key);
                   3360: 	next if ($key =~ /^error: 2 /);
                   3361: 	$returnhash{$key}=&thaw_unescape($value);
                   3362:     }
                   3363:     return %returnhash;
1.407     www      3364: }
                   3365: 
1.717     albertel 3366: # --------------------------------------------------------- dumpstore interface
                   3367: 
                   3368: sub dumpstore {
                   3369:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 3370:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3371:    if (!$uname) { $uname=$env{'user.name'}; }
                   3372:    my $uhome=&homeserver($uname,$udomain);
                   3373:    if ($regexp) {
                   3374:        $regexp=&escape($regexp);
                   3375:    } else {
                   3376:        $regexp='.';
                   3377:    }
                   3378:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3379:    my @pairs=split(/\&/,$rep);
                   3380:    my %returnhash=();
                   3381:    foreach my $item (@pairs) {
                   3382:        my ($key,$value)=split(/=/,$item,2);
                   3383:        next if ($key =~ /^error: 2 /);
                   3384:        $returnhash{$key}=&thaw_unescape($value);
                   3385:    }
                   3386:    return %returnhash;
1.717     albertel 3387: }
                   3388: 
1.407     www      3389: # -------------------------------------------------------------- keys interface
                   3390: 
                   3391: sub getkeys {
                   3392:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 3393:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3394:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      3395:    my $uhome=&homeserver($uname,$udomain);
                   3396:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   3397:    my @keyarray=();
1.800     albertel 3398:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  3399:       next if ($key =~ /^error: 2 /);
1.800     albertel 3400:       push(@keyarray,&unescape($key));
1.407     www      3401:    }
                   3402:    return @keyarray;
1.318     matthew  3403: }
                   3404: 
1.319     matthew  3405: # --------------------------------------------------------------- currentdump
                   3406: sub currentdump {
1.328     matthew  3407:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 3408:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   3409:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   3410:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  3411:    my $uhome = &homeserver($sname,$sdom);
                   3412:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  3413:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  3414:    #
1.318     matthew  3415:    my %returnhash=();
1.319     matthew  3416:    #
                   3417:    if ($rep eq "unknown_cmd") { 
                   3418:        # an old lond will not know currentdump
                   3419:        # Do a dump and make it look like a currentdump
1.822     albertel 3420:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  3421:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   3422:        my %hash = @tmp;
                   3423:        @tmp=();
1.424     matthew  3424:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  3425:    } else {
                   3426:        my @pairs=split(/\&/,$rep);
1.800     albertel 3427:        foreach my $pair (@pairs) {
                   3428:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  3429:            my ($symb,$param) = split(/:/,$key);
                   3430:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 3431:                                                         &thaw_unescape($value);
1.319     matthew  3432:        }
1.191     harris41 3433:    }
1.12      www      3434:    return %returnhash;
1.424     matthew  3435: }
                   3436: 
                   3437: sub convert_dump_to_currentdump{
                   3438:     my %hash = %{shift()};
                   3439:     my %returnhash;
                   3440:     # Code ripped from lond, essentially.  The only difference
                   3441:     # here is the unescaping done by lonnet::dump().  Conceivably
                   3442:     # we might run in to problems with parameter names =~ /^v\./
                   3443:     while (my ($key,$value) = each(%hash)) {
                   3444:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 3445: 	$symb  = &unescape($symb);
                   3446: 	$param = &unescape($param);
1.424     matthew  3447:         next if ($v eq 'version' || $symb eq 'keys');
                   3448:         next if (exists($returnhash{$symb}) &&
                   3449:                  exists($returnhash{$symb}->{$param}) &&
                   3450:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   3451:         $returnhash{$symb}->{$param}=$value;
                   3452:         $returnhash{$symb}->{'v.'.$param}=$v;
                   3453:     }
                   3454:     #
                   3455:     # Remove all of the keys in the hashes which keep track of
                   3456:     # the version of the parameter.
                   3457:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   3458:         # use a foreach because we are going to delete from the hash.
                   3459:         foreach my $key (keys(%$param_hash)) {
                   3460:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   3461:         }
                   3462:     }
                   3463:     return \%returnhash;
1.12      www      3464: }
                   3465: 
1.627     albertel 3466: # ------------------------------------------------------ critical inc interface
                   3467: 
                   3468: sub cinc {
                   3469:     return &inc(@_,'critical');
                   3470: }
                   3471: 
1.449     matthew  3472: # --------------------------------------------------------------- inc interface
                   3473: 
                   3474: sub inc {
1.627     albertel 3475:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 3476:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3477:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  3478:     my $uhome=&homeserver($uname,$udomain);
                   3479:     my $items='';
                   3480:     if (! ref($store)) {
                   3481:         # got a single value, so use that instead
                   3482:         $items = &escape($store).'=&';
                   3483:     } elsif (ref($store) eq 'SCALAR') {
                   3484:         $items = &escape($$store).'=&';        
                   3485:     } elsif (ref($store) eq 'ARRAY') {
                   3486:         $items = join('=&',map {&escape($_);} @{$store});
                   3487:     } elsif (ref($store) eq 'HASH') {
                   3488:         while (my($key,$value) = each(%{$store})) {
                   3489:             $items.= &escape($key).'='.&escape($value).'&';
                   3490:         }
                   3491:     }
                   3492:     $items=~s/\&$//;
1.627     albertel 3493:     if ($critical) {
                   3494: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3495:     } else {
                   3496: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3497:     }
1.449     matthew  3498: }
                   3499: 
1.12      www      3500: # --------------------------------------------------------------- put interface
                   3501: 
                   3502: sub put {
1.134     albertel 3503:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3504:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3505:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3506:    my $uhome=&homeserver($uname,$udomain);
1.12      www      3507:    my $items='';
1.800     albertel 3508:    foreach my $item (keys(%$storehash)) {
                   3509:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3510:    }
1.12      www      3511:    $items=~s/\&$//;
1.134     albertel 3512:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      3513: }
                   3514: 
1.631     albertel 3515: # ------------------------------------------------------------ newput interface
                   3516: 
                   3517: sub newput {
                   3518:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   3519:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3520:    if (!$uname) { $uname=$env{'user.name'}; }
                   3521:    my $uhome=&homeserver($uname,$udomain);
                   3522:    my $items='';
                   3523:    foreach my $key (keys(%$storehash)) {
                   3524:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   3525:    }
                   3526:    $items=~s/\&$//;
                   3527:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   3528: }
                   3529: 
                   3530: # ---------------------------------------------------------  putstore interface
                   3531: 
1.524     raeburn  3532: sub putstore {
1.715     albertel 3533:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 3534:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3535:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  3536:    my $uhome=&homeserver($uname,$udomain);
                   3537:    my $items='';
1.715     albertel 3538:    foreach my $key (keys(%$storehash)) {
                   3539:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  3540:    }
1.715     albertel 3541:    $items=~s/\&$//;
1.716     albertel 3542:    my $esc_symb=&escape($symb);
                   3543:    my $esc_v=&escape($version);
1.715     albertel 3544:    my $reply =
1.716     albertel 3545:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 3546: 	      $uhome);
                   3547:    if ($reply eq 'unknown_cmd') {
1.716     albertel 3548:        # gfall back to way things use to be done
1.715     albertel 3549:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   3550: 			    $uname);
1.524     raeburn  3551:    }
1.715     albertel 3552:    return $reply;
                   3553: }
                   3554: 
                   3555: sub old_putstore {
1.716     albertel 3556:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   3557:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3558:     if (!$uname) { $uname=$env{'user.name'}; }
                   3559:     my $uhome=&homeserver($uname,$udomain);
                   3560:     my %newstorehash;
1.800     albertel 3561:     foreach my $item (keys(%$storehash)) {
                   3562: 	my $key = $version.':'.&escape($symb).':'.$item;
                   3563: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 3564:     }
                   3565:     my $items='';
                   3566:     my %allitems = ();
1.800     albertel 3567:     foreach my $item (keys(%newstorehash)) {
                   3568: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 3569: 	    my $key = $1.':keys:'.$2;
                   3570: 	    $allitems{$key} .= $3.':';
                   3571: 	}
1.800     albertel 3572: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 3573:     }
1.800     albertel 3574:     foreach my $item (keys(%allitems)) {
                   3575: 	$allitems{$item} =~ s/\:$//;
                   3576: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 3577:     }
                   3578:     $items=~s/\&$//;
                   3579:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  3580: }
                   3581: 
1.47      www      3582: # ------------------------------------------------------ critical put interface
                   3583: 
                   3584: sub cput {
1.134     albertel 3585:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3586:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3587:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3588:    my $uhome=&homeserver($uname,$udomain);
1.47      www      3589:    my $items='';
1.800     albertel 3590:    foreach my $item (keys(%$storehash)) {
                   3591:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3592:    }
1.47      www      3593:    $items=~s/\&$//;
1.134     albertel 3594:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3595: }
                   3596: 
                   3597: # -------------------------------------------------------------- eget interface
                   3598: 
                   3599: sub eget {
1.133     albertel 3600:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3601:    my $items='';
1.800     albertel 3602:    foreach my $item (@$storearr) {
                   3603:        $items.=&escape($item).'&';
1.191     harris41 3604:    }
1.12      www      3605:    $items=~s/\&$//;
1.620     albertel 3606:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3607:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3608:    my $uhome=&homeserver($uname,$udomain);
                   3609:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3610:    my @pairs=split(/\&/,$rep);
                   3611:    my %returnhash=();
1.42      www      3612:    my $i=0;
1.800     albertel 3613:    foreach my $item (@$storearr) {
                   3614:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3615:       $i++;
1.191     harris41 3616:    }
1.12      www      3617:    return %returnhash;
                   3618: }
                   3619: 
1.667     albertel 3620: # ------------------------------------------------------------ tmpput interface
                   3621: sub tmpput {
1.802     raeburn  3622:     my ($storehash,$server,$context)=@_;
1.667     albertel 3623:     my $items='';
1.800     albertel 3624:     foreach my $item (keys(%$storehash)) {
                   3625: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 3626:     }
                   3627:     $items=~s/\&$//;
1.802     raeburn  3628:     if (defined($context)) {
                   3629:         $items .= ':'.&escape($context);
                   3630:     }
1.667     albertel 3631:     return &reply("tmpput:$items",$server);
                   3632: }
                   3633: 
                   3634: # ------------------------------------------------------------ tmpget interface
                   3635: sub tmpget {
1.688     albertel 3636:     my ($token,$server)=@_;
                   3637:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3638:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 3639:     my %returnhash;
                   3640:     foreach my $item (split(/\&/,$rep)) {
                   3641: 	my ($key,$value)=split(/=/,$item);
                   3642: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   3643:     }
                   3644:     return %returnhash;
                   3645: }
                   3646: 
1.688     albertel 3647: # ------------------------------------------------------------ tmpget interface
                   3648: sub tmpdel {
                   3649:     my ($token,$server)=@_;
                   3650:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3651:     return &reply("tmpdel:$token",$server);
                   3652: }
                   3653: 
1.765     albertel 3654: # -------------------------------------------------- portfolio access checking
                   3655: 
                   3656: sub portfolio_access {
1.766     albertel 3657:     my ($requrl) = @_;
1.765     albertel 3658:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   3659:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  3660:     if ($result) {
                   3661:         my %setters;
                   3662:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3663:             my ($startblock,$endblock) =
                   3664:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   3665:             if ($startblock && $endblock) {
                   3666:                 return 'B';
                   3667:             }
                   3668:         } else {
                   3669:             my ($startblock,$endblock) =
                   3670:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   3671:             if ($startblock && $endblock) {
                   3672:                 return 'B';
                   3673:             }
                   3674:         }
                   3675:     }
1.765     albertel 3676:     if ($result eq 'ok') {
1.766     albertel 3677:        return 'F';
1.765     albertel 3678:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 3679:        return 'A';
1.765     albertel 3680:     }
1.766     albertel 3681:     return '';
1.765     albertel 3682: }
                   3683: 
                   3684: sub get_portfolio_access {
1.767     albertel 3685:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   3686: 
                   3687:     if (!ref($access_hash)) {
                   3688: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   3689: 	my %access_controls = &get_access_controls($current_perms,$group,
                   3690: 						   $file_name);
                   3691: 	$access_hash = $access_controls{$file_name};
                   3692:     }
                   3693: 
1.765     albertel 3694:     my ($public,$guest,@domains,@users,@courses,@groups);
                   3695:     my $now = time;
                   3696:     if (ref($access_hash) eq 'HASH') {
                   3697:         foreach my $key (keys(%{$access_hash})) {
                   3698:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   3699:             if ($start > $now) {
                   3700:                 next;
                   3701:             }
                   3702:             if ($end && $end<$now) {
                   3703:                 next;
                   3704:             }
                   3705:             if ($scope eq 'public') {
                   3706:                 $public = $key;
                   3707:                 last;
                   3708:             } elsif ($scope eq 'guest') {
                   3709:                 $guest = $key;
                   3710:             } elsif ($scope eq 'domains') {
                   3711:                 push(@domains,$key);
                   3712:             } elsif ($scope eq 'users') {
                   3713:                 push(@users,$key);
                   3714:             } elsif ($scope eq 'course') {
                   3715:                 push(@courses,$key);
                   3716:             } elsif ($scope eq 'group') {
                   3717:                 push(@groups,$key);
                   3718:             }
                   3719:         }
                   3720:         if ($public) {
                   3721:             return 'ok';
                   3722:         }
                   3723:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3724:             if ($guest) {
                   3725:                 return $guest;
                   3726:             }
                   3727:         } else {
                   3728:             if (@domains > 0) {
                   3729:                 foreach my $domkey (@domains) {
                   3730:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   3731:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   3732:                             return 'ok';
                   3733:                         }
                   3734:                     }
                   3735:                 }
                   3736:             }
                   3737:             if (@users > 0) {
                   3738:                 foreach my $userkey (@users) {
1.865     raeburn  3739:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   3740:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   3741:                             if (ref($item) eq 'HASH') {
                   3742:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   3743:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   3744:                                     return 'ok';
                   3745:                                 }
                   3746:                             }
                   3747:                         }
                   3748:                     } 
1.765     albertel 3749:                 }
                   3750:             }
                   3751:             my %roleshash;
                   3752:             my @courses_and_groups = @courses;
                   3753:             push(@courses_and_groups,@groups); 
                   3754:             if (@courses_and_groups > 0) {
                   3755:                 my (%allgroups,%allroles); 
                   3756:                 my ($start,$end,$role,$sec,$group);
                   3757:                 foreach my $envkey (%env) {
1.811     albertel 3758:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3759:                         my $cid = $2.'_'.$3; 
                   3760:                         if ($1 eq 'gr') {
                   3761:                             $group = $4;
                   3762:                             $allgroups{$cid}{$group} = $env{$envkey};
                   3763:                         } else {
                   3764:                             if ($4 eq '') {
                   3765:                                 $sec = 'none';
                   3766:                             } else {
                   3767:                                 $sec = $4;
                   3768:                             }
                   3769:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3770:                         }
1.811     albertel 3771:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3772:                         my $cid = $2.'_'.$3;
                   3773:                         if ($4 eq '') {
                   3774:                             $sec = 'none';
                   3775:                         } else {
                   3776:                             $sec = $4;
                   3777:                         }
                   3778:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3779:                     }
                   3780:                 }
                   3781:                 if (keys(%allroles) == 0) {
                   3782:                     return;
                   3783:                 }
                   3784:                 foreach my $key (@courses_and_groups) {
                   3785:                     my %content = %{$$access_hash{$key}};
                   3786:                     my $cnum = $content{'number'};
                   3787:                     my $cdom = $content{'domain'};
                   3788:                     my $cid = $cdom.'_'.$cnum;
                   3789:                     if (!exists($allroles{$cid})) {
                   3790:                         next;
                   3791:                     }    
                   3792:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   3793:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   3794:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   3795:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   3796:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   3797:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   3798:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   3799:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   3800:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   3801:                                         if (grep/^all$/,@sections) {
                   3802:                                             return 'ok';
                   3803:                                         } else {
                   3804:                                             if (grep/^$sec$/,@sections) {
                   3805:                                                 return 'ok';
                   3806:                                             }
                   3807:                                         }
                   3808:                                     }
                   3809:                                 }
                   3810:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   3811:                                     if (grep/^none$/,@groups) {
                   3812:                                         return 'ok';
                   3813:                                     }
                   3814:                                 } else {
                   3815:                                     if (grep/^all$/,@groups) {
                   3816:                                         return 'ok';
                   3817:                                     } 
                   3818:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   3819:                                         if (grep/^$group$/,@groups) {
                   3820:                                             return 'ok';
                   3821:                                         }
                   3822:                                     }
                   3823:                                 } 
                   3824:                             }
                   3825:                         }
                   3826:                     }
                   3827:                 }
                   3828:             }
                   3829:             if ($guest) {
                   3830:                 return $guest;
                   3831:             }
                   3832:         }
                   3833:     }
                   3834:     return;
                   3835: }
                   3836: 
                   3837: sub course_group_datechecker {
                   3838:     my ($dates,$now,$status) = @_;
                   3839:     my ($start,$end) = split(/\./,$dates);
                   3840:     if (!$start && !$end) {
                   3841:         return 'ok';
                   3842:     }
                   3843:     if (grep/^active$/,@{$status}) {
                   3844:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   3845:             return 'ok';
                   3846:         }
                   3847:     }
                   3848:     if (grep/^previous$/,@{$status}) {
                   3849:         if ($end > $now ) {
                   3850:             return 'ok';
                   3851:         }
                   3852:     }
                   3853:     if (grep/^future$/,@{$status}) {
                   3854:         if ($start > $now) {
                   3855:             return 'ok';
                   3856:         }
                   3857:     }
                   3858:     return; 
                   3859: }
                   3860: 
                   3861: sub parse_portfolio_url {
                   3862:     my ($url) = @_;
                   3863: 
                   3864:     my ($type,$udom,$unum,$group,$file_name);
                   3865:     
1.823     albertel 3866:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 3867: 	$type = 1;
                   3868:         $udom = $1;
                   3869:         $unum = $2;
                   3870:         $file_name = $3;
1.823     albertel 3871:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 3872: 	$type = 2;
                   3873:         $udom = $1;
                   3874:         $unum = $2;
                   3875:         $group = $3;
                   3876:         $file_name = $3.'/'.$4;
                   3877:     }
                   3878:     if (wantarray) {
                   3879: 	return ($type,$udom,$unum,$file_name,$group);
                   3880:     }
                   3881:     return $type;
                   3882: }
                   3883: 
                   3884: sub is_portfolio_url {
                   3885:     my ($url) = @_;
                   3886:     return scalar(&parse_portfolio_url($url));
                   3887: }
                   3888: 
1.798     raeburn  3889: sub is_portfolio_file {
                   3890:     my ($file) = @_;
1.820     raeburn  3891:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  3892:         return 1;
                   3893:     }
                   3894:     return;
                   3895: }
                   3896: 
                   3897: 
1.341     www      3898: # ---------------------------------------------- Custom access rule evaluation
                   3899: 
                   3900: sub customaccess {
                   3901:     my ($priv,$uri)=@_;
1.807     albertel 3902:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      3903:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 3904:     $udom = &LONCAPA::clean_domain($udom);
                   3905:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      3906:     my $access=0;
1.800     albertel 3907:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 3908: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   3909: 	if ($type eq 'user') {
                   3910: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 3911: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 3912: 		if ($tdom) {
                   3913: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   3914: 		}
1.896     albertel 3915: 		if ($tuname) {
                   3916: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 3917: 		}
                   3918: 		$access=($effect eq 'allow');
                   3919: 		last;
                   3920: 	    }
                   3921: 	} else {
                   3922: 	    if ($role) {
                   3923: 		if ($role ne $urole) { next; }
                   3924: 	    }
                   3925: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   3926: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   3927: 		if ($tdom) {
                   3928: 		    if ($tdom ne $udom) { next; }
                   3929: 		}
                   3930: 		if ($tcrs) {
                   3931: 		    if ($tcrs ne $ucrs) { next; }
                   3932: 		}
                   3933: 		if ($tsec) {
                   3934: 		    if ($tsec ne $usec) { next; }
                   3935: 		}
                   3936: 		$access=($effect eq 'allow');
                   3937: 		last;
                   3938: 	    }
                   3939: 	    if ($realm eq '' && $role eq '') {
                   3940: 		$access=($effect eq 'allow');
                   3941: 	    }
1.402     bowersj2 3942: 	}
1.341     www      3943:     }
                   3944:     return $access;
                   3945: }
                   3946: 
1.103     harris41 3947: # ------------------------------------------------- Check for a user privilege
1.12      www      3948: 
                   3949: sub allowed {
1.810     raeburn  3950:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 3951:     my $ver_orguri=$uri;
1.439     www      3952:     $uri=&deversion($uri);
1.152     www      3953:     my $orguri=$uri;
1.52      www      3954:     $uri=&declutter($uri);
1.809     raeburn  3955: 
1.810     raeburn  3956:     if ($priv eq 'evb') {
                   3957: # Evade communication block restrictions for specified role in a course
                   3958:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   3959:             return $1;
                   3960:         } else {
                   3961:             return;
                   3962:         }
                   3963:     }
                   3964: 
1.620     albertel 3965:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      3966: # Free bre access to adm and meta resources
1.775     albertel 3967:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 3968: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   3969: 	&& ($priv eq 'bre')) {
1.14      www      3970: 	return 'F';
1.159     www      3971:     }
                   3972: 
1.545     banghart 3973: # Free bre access to user's own portfolio contents
1.714     raeburn  3974:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  3975:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  3976: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  3977:         my %setters;
                   3978:         my ($startblock,$endblock) = 
                   3979:             &Apache::loncommon::blockcheck(\%setters,'port');
                   3980:         if ($startblock && $endblock) {
                   3981:             return 'B';
                   3982:         } else {
                   3983:             return 'F';
                   3984:         }
1.545     banghart 3985:     }
                   3986: 
1.762     raeburn  3987: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  3988:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   3989:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   3990:         if (exists($env{'request.course.id'})) {
                   3991:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3992:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3993:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   3994:                 my $courseprivid=$env{'request.course.id'};
                   3995:                 $courseprivid=~s/\_/\//;
                   3996:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   3997:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   3998:                     return $1; 
1.762     raeburn  3999:                 } else {
                   4000:                     if ($env{'request.course.sec'}) {
                   4001:                         $courseprivid.='/'.$env{'request.course.sec'};
                   4002:                     }
                   4003:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   4004:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   4005:                         return $2;
                   4006:                     }
1.714     raeburn  4007:                 }
                   4008:             }
                   4009:         }
                   4010:     }
                   4011: 
1.159     www      4012: # Free bre to public access
                   4013: 
                   4014:     if ($priv eq 'bre') {
1.238     www      4015:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 4016: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      4017:            return 'F'; 
                   4018:         }
1.238     www      4019:         if ($copyright eq 'priv') {
                   4020:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4021: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      4022: 		return '';
                   4023:             }
                   4024:         }
                   4025:         if ($copyright eq 'domain') {
                   4026:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4027: 	    unless (($env{'user.domain'} eq $1) ||
                   4028:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      4029: 		return '';
                   4030:             }
1.262     matthew  4031:         }
1.620     albertel 4032:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  4033:             # Library role, so allow browsing of resources in this domain.
                   4034:             return 'F';
1.238     www      4035:         }
1.341     www      4036:         if ($copyright eq 'custom') {
                   4037: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   4038:         }
1.14      www      4039:     }
1.264     matthew  4040:     # Domain coordinator is trying to create a course
1.620     albertel 4041:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  4042:         # uri is the requested domain in this case.
                   4043:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  4044:         # a role of dc for the domain in question.
1.620     albertel 4045:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  4046:     }
1.29      www      4047: 
1.52      www      4048:     my $thisallowed='';
                   4049:     my $statecond=0;
                   4050:     my $courseprivid='';
                   4051: 
                   4052: # Course
                   4053: 
1.620     albertel 4054:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4055:        $thisallowed.=$1;
                   4056:     }
1.29      www      4057: 
1.52      www      4058: # Domain
                   4059: 
1.620     albertel 4060:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 4061:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4062:        $thisallowed.=$1;
                   4063:     }
1.52      www      4064: 
                   4065: # Course: uri itself is a course
1.66      www      4066:     my $courseuri=$uri;
                   4067:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      4068:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      4069: 
1.620     albertel 4070:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 4071:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4072:        $thisallowed.=$1;
                   4073:     }
1.29      www      4074: 
1.665     albertel 4075: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 4076: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 4077:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 4078: 	$thisallowed='';
1.671     raeburn  4079:         my ($match)=&is_on_map($uri);
                   4080:         if ($match) {
                   4081:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   4082:                   =~/\Q$priv\E\&([^\:]*)/) {
                   4083:                 $thisallowed.=$1;
                   4084:             }
                   4085:         } else {
1.705     albertel 4086:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  4087:             if ($refuri) {
                   4088:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  4089:                     $thisallowed='F';
1.671     raeburn  4090:                 } else {
                   4091:                     $refuri=&declutter($refuri);
                   4092:                     my ($match) = &is_on_map($refuri);
                   4093:                     if ($match) {
                   4094:                         $thisallowed='F';
                   4095:                     }
1.669     raeburn  4096:                 }
1.671     raeburn  4097:             }
                   4098:         }
1.314     www      4099:     }
1.492     albertel 4100: 
1.766     albertel 4101:     if ($priv eq 'bre'
                   4102: 	&& $thisallowed ne 'F' 
                   4103: 	&& $thisallowed ne '2'
                   4104: 	&& &is_portfolio_url($uri)) {
                   4105: 	$thisallowed = &portfolio_access($uri);
                   4106:     }
                   4107:     
1.52      www      4108: # Full access at system, domain or course-wide level? Exit.
1.29      www      4109: 
                   4110:     if ($thisallowed=~/F/) {
                   4111: 	return 'F';
                   4112:     }
                   4113: 
1.52      www      4114: # If this is generating or modifying users, exit with special codes
1.29      www      4115: 
1.643     www      4116:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   4117: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 4118: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      4119: # no author name given, so this just checks on the general right to make a co-author in this domain
                   4120: 	    unless ($auname) { return $thisallowed; }
                   4121: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 4122: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   4123: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   4124: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   4125: 	}
1.52      www      4126: 	return $thisallowed;
                   4127:     }
                   4128: #
1.103     harris41 4129: # Gathered so far: system, domain and course wide privileges
1.52      www      4130: #
                   4131: # Course: See if uri or referer is an individual resource that is part of 
                   4132: # the course
                   4133: 
1.620     albertel 4134:     if ($env{'request.course.id'}) {
1.232     www      4135: 
1.620     albertel 4136:        $courseprivid=$env{'request.course.id'};
                   4137:        if ($env{'request.course.sec'}) {
                   4138:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      4139:        }
                   4140:        $courseprivid=~s/\_/\//;
                   4141:        my $checkreferer=1;
1.232     www      4142:        my ($match,$cond)=&is_on_map($uri);
                   4143:        if ($match) {
                   4144:            $statecond=$cond;
1.620     albertel 4145:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4146:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4147:                $thisallowed.=$1;
                   4148:                $checkreferer=0;
                   4149:            }
1.29      www      4150:        }
1.83      www      4151:        
1.148     www      4152:        if ($checkreferer) {
1.620     albertel 4153: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      4154:             unless ($refuri) {
1.800     albertel 4155:                 foreach my $key (keys(%env)) {
                   4156: 		    if ($key=~/^httpref\..*\*/) {
                   4157: 			my $pattern=$key;
1.156     www      4158:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      4159:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   4160:                         $pattern=~s/\//\\\//g;
1.152     www      4161:                         if ($orguri=~/$pattern/) {
1.800     albertel 4162: 			    $refuri=$env{$key};
1.148     www      4163:                         }
                   4164:                     }
1.191     harris41 4165:                 }
1.148     www      4166:             }
1.232     www      4167: 
1.148     www      4168:          if ($refuri) { 
1.152     www      4169: 	  $refuri=&declutter($refuri);
1.232     www      4170:           my ($match,$cond)=&is_on_map($refuri);
                   4171:             if ($match) {
                   4172:               my $refstatecond=$cond;
1.620     albertel 4173:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4174:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4175:                   $thisallowed.=$1;
1.53      www      4176:                   $uri=$refuri;
                   4177:                   $statecond=$refstatecond;
1.52      www      4178:               }
                   4179:           }
1.148     www      4180:         }
1.29      www      4181:        }
1.52      www      4182:    }
1.29      www      4183: 
1.52      www      4184: #
1.103     harris41 4185: # Gathered now: all privileges that could apply, and condition number
1.52      www      4186: # 
                   4187: #
                   4188: # Full or no access?
                   4189: #
1.29      www      4190: 
1.52      www      4191:     if ($thisallowed=~/F/) {
                   4192: 	return 'F';
                   4193:     }
1.29      www      4194: 
1.52      www      4195:     unless ($thisallowed) {
                   4196:         return '';
                   4197:     }
1.29      www      4198: 
1.52      www      4199: # Restrictions exist, deal with them
                   4200: #
                   4201: #   C:according to course preferences
                   4202: #   R:according to resource settings
                   4203: #   L:unless locked
                   4204: #   X:according to user session state
                   4205: #
                   4206: 
                   4207: # Possibly locked functionality, check all courses
1.54      www      4208: # Locks might take effect only after 10 minutes cache expiration for other
                   4209: # courses, and 2 minutes for current course
1.52      www      4210: 
                   4211:     my $envkey;
                   4212:     if ($thisallowed=~/L/) {
1.620     albertel 4213:         foreach $envkey (keys %env) {
1.54      www      4214:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   4215:                my $courseid=$2;
                   4216:                my $roleid=$1.'.'.$2;
1.92      www      4217:                $courseid=~s/^\///;
1.54      www      4218:                my $expiretime=600;
1.620     albertel 4219:                if ($env{'request.role'} eq $roleid) {
1.54      www      4220: 		  $expiretime=120;
                   4221:                }
                   4222: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   4223:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 4224:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 4225: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      4226:                }
1.620     albertel 4227:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4228:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   4229: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   4230:                        &log($env{'user.domain'},$env{'user.name'},
                   4231:                             $env{'user.home'},
1.57      www      4232:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      4233:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4234:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4235: 		       return '';
                   4236:                    }
                   4237:                }
1.620     albertel 4238:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4239:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   4240: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   4241:                        &log($env{'user.domain'},$env{'user.name'},
                   4242:                             $env{'user.home'},
1.57      www      4243:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      4244:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4245:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4246: 		       return '';
                   4247:                    }
                   4248:                }
                   4249: 	   }
1.29      www      4250:        }
1.52      www      4251:     }
                   4252:    
                   4253: #
                   4254: # Rest of the restrictions depend on selected course
                   4255: #
                   4256: 
1.620     albertel 4257:     unless ($env{'request.course.id'}) {
1.766     albertel 4258: 	if ($thisallowed eq 'A') {
                   4259: 	    return 'A';
1.814     raeburn  4260:         } elsif ($thisallowed eq 'B') {
                   4261:             return 'B';
1.766     albertel 4262: 	} else {
                   4263: 	    return '1';
                   4264: 	}
1.52      www      4265:     }
1.29      www      4266: 
1.52      www      4267: #
                   4268: # Now user is definitely in a course
                   4269: #
1.53      www      4270: 
                   4271: 
                   4272: # Course preferences
                   4273: 
                   4274:    if ($thisallowed=~/C/) {
1.620     albertel 4275:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   4276:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   4277:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 4278: 	   =~/\Q$rolecode\E/) {
1.689     albertel 4279: 	   if ($priv ne 'pch') { 
                   4280: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4281: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   4282: 			$env{'request.course.id'});
                   4283: 	   }
1.237     www      4284:            return '';
                   4285:        }
                   4286: 
1.620     albertel 4287:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 4288: 	   =~/\Q$unamedom\E/) {
1.689     albertel 4289: 	   if ($priv ne 'pch') { 
                   4290: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   4291: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   4292: 			$env{'request.course.id'});
                   4293: 	   }
1.54      www      4294:            return '';
                   4295:        }
1.53      www      4296:    }
                   4297: 
                   4298: # Resource preferences
                   4299: 
                   4300:    if ($thisallowed=~/R/) {
1.620     albertel 4301:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 4302:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689     albertel 4303: 	   if ($priv ne 'pch') { 
                   4304: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4305: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   4306: 	   }
                   4307: 	   return '';
1.54      www      4308:        }
1.53      www      4309:    }
1.30      www      4310: 
1.246     www      4311: # Restricted by state or randomout?
1.30      www      4312: 
1.52      www      4313:    if ($thisallowed=~/X/) {
1.620     albertel 4314:       if ($env{'acc.randomout'}) {
1.579     albertel 4315: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 4316:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      4317:             return ''; 
                   4318:          }
1.247     www      4319:       }
                   4320:       if (&condval($statecond)) {
1.52      www      4321: 	 return '2';
                   4322:       } else {
                   4323:          return '';
                   4324:       }
                   4325:    }
1.30      www      4326: 
1.766     albertel 4327:     if ($thisallowed eq 'A') {
                   4328: 	return 'A';
1.814     raeburn  4329:     } elsif ($thisallowed eq 'B') {
                   4330:         return 'B';
1.766     albertel 4331:     }
1.52      www      4332:    return 'F';
1.232     www      4333: }
                   4334: 
1.710     albertel 4335: sub split_uri_for_cond {
                   4336:     my $uri=&deversion(&declutter(shift));
                   4337:     my @uriparts=split(/\//,$uri);
                   4338:     my $filename=pop(@uriparts);
                   4339:     my $pathname=join('/',@uriparts);
                   4340:     return ($pathname,$filename);
                   4341: }
1.232     www      4342: # --------------------------------------------------- Is a resource on the map?
                   4343: 
                   4344: sub is_on_map {
1.710     albertel 4345:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 4346:     #Trying to find the conditional for the file
1.620     albertel 4347:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 4348: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      4349:     if ($match) {
1.289     bowersj2 4350: 	return (1,$1);
                   4351:     } else {
1.434     www      4352: 	return (0,0);
1.289     bowersj2 4353:     }
1.12      www      4354: }
                   4355: 
1.427     www      4356: # --------------------------------------------------------- Get symb from alias
                   4357: 
                   4358: sub get_symb_from_alias {
                   4359:     my $symb=shift;
                   4360:     my ($map,$resid,$url)=&decode_symb($symb);
                   4361: # Already is a symb
                   4362:     if ($url) { return $symb; }
                   4363: # Must be an alias
                   4364:     my $aliassymb='';
                   4365:     my %bighash;
1.620     albertel 4366:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      4367:                             &GDBM_READER(),0640)) {
                   4368:         my $rid=$bighash{'mapalias_'.$symb};
                   4369: 	if ($rid) {
                   4370: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 4371: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   4372: 				    $resid,$bighash{'src_'.$rid});
1.427     www      4373: 	}
                   4374:         untie %bighash;
                   4375:     }
                   4376:     return $aliassymb;
                   4377: }
                   4378: 
1.12      www      4379: # ----------------------------------------------------------------- Define Role
                   4380: 
                   4381: sub definerole {
                   4382:   if (allowed('mcr','/')) {
                   4383:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 4384:     foreach my $role (split(':',$sysrole)) {
                   4385: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4386:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   4387:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   4388: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4389:                return "refused:s:$crole&$cqual"; 
                   4390:             }
                   4391:         }
1.191     harris41 4392:     }
1.800     albertel 4393:     foreach my $role (split(':',$domrole)) {
                   4394: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4395:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   4396:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   4397: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      4398:                return "refused:d:$crole&$cqual"; 
                   4399:             }
                   4400:         }
1.191     harris41 4401:     }
1.800     albertel 4402:     foreach my $role (split(':',$courole)) {
                   4403: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4404:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   4405:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   4406: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4407:                return "refused:c:$crole&$cqual"; 
                   4408:             }
                   4409:         }
1.191     harris41 4410:     }
1.620     albertel 4411:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   4412:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4413: 	        "rolesdef_$rolename=".
                   4414:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 4415:     return reply($command,$env{'user.home'});
1.12      www      4416:   } else {
                   4417:     return 'refused';
                   4418:   }
1.105     harris41 4419: }
                   4420: 
                   4421: # ---------------- Make a metadata query against the network of library servers
                   4422: 
                   4423: sub metadata_query {
1.244     matthew  4424:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 4425:     my %rhash;
1.845     albertel 4426:     my %libserv = &all_library();
1.244     matthew  4427:     my @server_list = (defined($server_array) ? @$server_array
                   4428:                                               : keys(%libserv) );
                   4429:     for my $server (@server_list) {
1.118     harris41 4430: 	unless ($custom or $customshow) {
                   4431: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   4432: 	    $rhash{$server}=$reply;
                   4433: 	}
                   4434: 	else {
                   4435: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   4436: 			     &escape($custom).':'.&escape($customshow),
                   4437: 			     $server);
                   4438: 	    $rhash{$server}=$reply;
                   4439: 	}
1.112     harris41 4440:     }
1.118     harris41 4441:     return \%rhash;
1.240     www      4442: }
                   4443: 
                   4444: # ----------------------------------------- Send log queries and wait for reply
                   4445: 
                   4446: sub log_query {
                   4447:     my ($uname,$udom,$query,%filters)=@_;
                   4448:     my $uhome=&homeserver($uname,$udom);
                   4449:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 4450:     my $uhost=&hostname($uhome);
1.800     albertel 4451:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      4452:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   4453:                        $uhome);
1.479     albertel 4454:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      4455:     return get_query_reply($queryid);
                   4456: }
                   4457: 
1.818     raeburn  4458: # -------------------------- Update MySQL table for portfolio file
                   4459: 
                   4460: sub update_portfolio_table {
1.821     raeburn  4461:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818     raeburn  4462:     my $homeserver = &homeserver($uname,$udom);
                   4463:     my $queryid=
1.821     raeburn  4464:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   4465:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  4466:     my $reply = &get_query_reply($queryid);
                   4467:     return $reply;
                   4468: }
                   4469: 
1.899     raeburn  4470: # -------------------------- Update MySQL allusers table
                   4471: 
                   4472: sub update_allusers_table {
                   4473:     my ($uname,$udom,$names) = @_;
                   4474:     my $homeserver = &homeserver($uname,$udom);
                   4475:     my $queryid=
                   4476:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   4477:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   4478:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   4479:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   4480:                'generation='.&escape($names->{'generation'}).'%%'.
                   4481:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   4482:                'id='.&escape($names->{'id'}),$homeserver);
                   4483:     my $reply = &get_query_reply($queryid);
                   4484:     return $reply;
                   4485: }
                   4486: 
1.508     raeburn  4487: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  4488: 
                   4489: sub fetch_enrollment_query {
1.511     raeburn  4490:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  4491:     my $homeserver;
1.547     raeburn  4492:     my $maxtries = 1;
1.508     raeburn  4493:     if ($context eq 'automated') {
                   4494:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  4495:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  4496:     } else {
                   4497:         $homeserver = &homeserver($cnum,$dom);
                   4498:     }
1.838     albertel 4499:     my $host=&hostname($homeserver);
1.506     raeburn  4500:     my $cmd = '';
1.800     albertel 4501:     foreach my $affiliate (keys %{$affiliatesref}) {
                   4502:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  4503:     }
                   4504:     $cmd =~ s/%%$//;
                   4505:     $cmd = &escape($cmd);
                   4506:     my $query = 'fetchenrollment';
1.620     albertel 4507:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  4508:     unless ($queryid=~/^\Q$host\E\_/) { 
                   4509:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   4510:         return 'error: '.$queryid;
                   4511:     }
1.506     raeburn  4512:     my $reply = &get_query_reply($queryid);
1.547     raeburn  4513:     my $tries = 1;
                   4514:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4515:         $reply = &get_query_reply($queryid);
                   4516:         $tries ++;
                   4517:     }
1.526     raeburn  4518:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 4519:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  4520:     } else {
1.901     albertel 4521:         my @responses = split(/:/,$reply);
1.515     raeburn  4522:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 4523:             foreach my $line (@responses) {
                   4524:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  4525:                 $$replyref{$key} = $value;
                   4526:             }
                   4527:         } else {
1.506     raeburn  4528:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 4529:             foreach my $line (@responses) {
                   4530:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  4531:                 $$replyref{$key} = $value;
                   4532:                 if ($value > 0) {
1.800     albertel 4533:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   4534:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  4535:                         my $destname = $pathname.'/'.$filename;
                   4536:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  4537:                         if ($xml_classlist =~ /^error/) {
                   4538:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   4539:                         } else {
1.506     raeburn  4540:                             if ( open(FILE,">$destname") ) {
                   4541:                                 print FILE &unescape($xml_classlist);
                   4542:                                 close(FILE);
1.526     raeburn  4543:                             } else {
                   4544:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  4545:                             }
                   4546:                         }
                   4547:                     }
                   4548:                 }
                   4549:             }
                   4550:         }
                   4551:         return 'ok';
                   4552:     }
                   4553:     return 'error';
                   4554: }
                   4555: 
1.242     www      4556: sub get_query_reply {
                   4557:     my $queryid=shift;
1.240     www      4558:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   4559:     my $reply='';
                   4560:     for (1..100) {
                   4561: 	sleep 2;
                   4562:         if (-e $replyfile.'.end') {
1.448     albertel 4563: 	    if (open(my $fh,$replyfile)) {
1.904     albertel 4564: 		$reply = join('',<$fh>);
                   4565: 		close($fh);
1.240     www      4566: 	   } else { return 'error: reply_file_error'; }
1.242     www      4567:            return &unescape($reply);
                   4568: 	}
1.240     www      4569:     }
1.242     www      4570:     return 'timeout:'.$queryid;
1.240     www      4571: }
                   4572: 
                   4573: sub courselog_query {
1.241     www      4574: #
                   4575: # possible filters:
                   4576: # url: url or symb
                   4577: # username
                   4578: # domain
                   4579: # action: view, submit, grade
                   4580: # start: timestamp
                   4581: # end: timestamp
                   4582: #
1.240     www      4583:     my (%filters)=@_;
1.620     albertel 4584:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      4585:     if ($filters{'url'}) {
                   4586: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   4587:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   4588:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   4589:     }
1.620     albertel 4590:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4591:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      4592:     return &log_query($cname,$cdom,'courselog',%filters);
                   4593: }
                   4594: 
                   4595: sub userlog_query {
1.858     raeburn  4596: #
                   4597: # possible filters:
                   4598: # action: log check role
                   4599: # start: timestamp
                   4600: # end: timestamp
                   4601: #
1.240     www      4602:     my ($uname,$udom,%filters)=@_;
                   4603:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      4604: }
                   4605: 
1.506     raeburn  4606: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   4607: 
                   4608: sub auto_run {
1.508     raeburn  4609:     my ($cnum,$cdom) = @_;
1.876     raeburn  4610:     my $response = 0;
                   4611:     my $settings;
                   4612:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   4613:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4614:         $settings = $domconfig{'autoenroll'};
                   4615:         if ($settings->{'run'} eq '1') {
                   4616:             $response = 1;
                   4617:         }
                   4618:     } else {
                   4619:         my $homeserver = &homeserver($cnum,$cdom);
                   4620:         $response = &reply('autorun:'.$cdom,$homeserver);
                   4621:     }
1.506     raeburn  4622:     return $response;
                   4623: }
1.776     albertel 4624: 
1.506     raeburn  4625: sub auto_get_sections {
1.508     raeburn  4626:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   4627:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  4628:     my @secs = ();
1.511     raeburn  4629:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  4630:     unless ($response eq 'refused') {
1.901     albertel 4631:         @secs = split(/:/,$response);
1.506     raeburn  4632:     }
                   4633:     return @secs;
                   4634: }
1.776     albertel 4635: 
1.506     raeburn  4636: sub auto_new_course {
1.508     raeburn  4637:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   4638:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  4639:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  4640:     return $response;
                   4641: }
1.776     albertel 4642: 
1.506     raeburn  4643: sub auto_validate_courseID {
1.508     raeburn  4644:     my ($cnum,$cdom,$inst_course_id) = @_;
                   4645:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  4646:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  4647:     return $response;
                   4648: }
1.776     albertel 4649: 
1.506     raeburn  4650: sub auto_create_password {
1.873     raeburn  4651:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   4652:     my ($homeserver,$response);
1.506     raeburn  4653:     my $create_passwd = 0;
                   4654:     my $authchk = '';
1.873     raeburn  4655:     if ($udom =~ /^$match_domain$/) {
                   4656:         $homeserver = &domain($udom,'primary');
                   4657:     }
                   4658:     if ($homeserver eq '') {
                   4659:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   4660:             $homeserver = &homeserver($cnum,$cdom);
                   4661:         }
                   4662:     }
                   4663:     if ($homeserver eq '') {
                   4664:         $authchk = 'nodomain';
1.506     raeburn  4665:     } else {
1.873     raeburn  4666:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   4667:         if ($response eq 'refused') {
                   4668:             $authchk = 'refused';
                   4669:         } else {
1.901     albertel 4670:             ($authparam,$create_passwd,$authchk) = split(/:/,$response);
1.873     raeburn  4671:         }
1.506     raeburn  4672:     }
                   4673:     return ($authparam,$create_passwd,$authchk);
                   4674: }
                   4675: 
1.706     raeburn  4676: sub auto_photo_permission {
                   4677:     my ($cnum,$cdom,$students) = @_;
                   4678:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 4679:     my ($outcome,$perm_reqd,$conditions) = 
                   4680: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 4681:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4682: 	return (undef,undef);
                   4683:     }
1.706     raeburn  4684:     return ($outcome,$perm_reqd,$conditions);
                   4685: }
                   4686: 
                   4687: sub auto_checkphotos {
                   4688:     my ($uname,$udom,$pid) = @_;
                   4689:     my $homeserver = &homeserver($uname,$udom);
                   4690:     my ($result,$resulttype);
                   4691:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 4692: 				   &escape($uname).':'.&escape($pid),
                   4693: 				   $homeserver));
1.709     albertel 4694:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4695: 	return (undef,undef);
                   4696:     }
1.706     raeburn  4697:     if ($outcome) {
                   4698:         ($result,$resulttype) = split(/:/,$outcome);
                   4699:     } 
                   4700:     return ($result,$resulttype);
                   4701: }
                   4702: 
                   4703: sub auto_photochoice {
                   4704:     my ($cnum,$cdom) = @_;
                   4705:     my $homeserver = &homeserver($cnum,$cdom);
                   4706:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 4707: 						       &escape($cdom),
                   4708: 						       $homeserver)));
1.709     albertel 4709:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4710: 	return (undef,undef);
                   4711:     }
1.706     raeburn  4712:     return ($update,$comment);
                   4713: }
                   4714: 
                   4715: sub auto_photoupdate {
                   4716:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   4717:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 4718:     my $host=&hostname($homeserver);
1.706     raeburn  4719:     my $cmd = '';
                   4720:     my $maxtries = 1;
1.800     albertel 4721:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   4722:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  4723:     }
                   4724:     $cmd =~ s/%%$//;
                   4725:     $cmd = &escape($cmd);
                   4726:     my $query = 'institutionalphotos';
                   4727:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   4728:     unless ($queryid=~/^\Q$host\E\_/) {
                   4729:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   4730:         return 'error: '.$queryid;
                   4731:     }
                   4732:     my $reply = &get_query_reply($queryid);
                   4733:     my $tries = 1;
                   4734:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4735:         $reply = &get_query_reply($queryid);
                   4736:         $tries ++;
                   4737:     }
                   4738:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   4739:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   4740:     } else {
                   4741:         my @responses = split(/:/,$reply);
                   4742:         my $outcome = shift(@responses); 
                   4743:         foreach my $item (@responses) {
                   4744:             my ($key,$value) = split(/=/,$item);
                   4745:             $$photo{$key} = $value;
                   4746:         }
                   4747:         return $outcome;
                   4748:     }
                   4749:     return 'error';
                   4750: }
                   4751: 
1.521     raeburn  4752: sub auto_instcode_format {
1.793     albertel 4753:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   4754: 	$cat_order) = @_;
1.521     raeburn  4755:     my $courses = '';
1.772     raeburn  4756:     my @homeservers;
1.521     raeburn  4757:     if ($caller eq 'global') {
1.841     albertel 4758: 	my %servers = &get_servers($codedom,'library');
                   4759: 	foreach my $tryserver (keys(%servers)) {
                   4760: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4761: 		push(@homeservers,$tryserver);
                   4762: 	    }
1.584     raeburn  4763:         }
1.521     raeburn  4764:     } else {
1.772     raeburn  4765:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  4766:     }
1.793     albertel 4767:     foreach my $code (keys(%{$instcodes})) {
                   4768:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  4769:     }
                   4770:     chop($courses);
1.772     raeburn  4771:     my $ok_response = 0;
                   4772:     my $response;
                   4773:     while (@homeservers > 0 && $ok_response == 0) {
                   4774:         my $server = shift(@homeservers); 
                   4775:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   4776:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   4777:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.901     albertel 4778: 		split(/:/,$response);
1.772     raeburn  4779:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   4780:             push(@{$codetitles},&str2array($codetitles_str));
                   4781:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   4782:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   4783:             $ok_response = 1;
                   4784:         }
                   4785:     }
                   4786:     if ($ok_response) {
1.521     raeburn  4787:         return 'ok';
1.772     raeburn  4788:     } else {
                   4789:         return $response;
1.521     raeburn  4790:     }
                   4791: }
                   4792: 
1.792     raeburn  4793: sub auto_instcode_defaults {
                   4794:     my ($domain,$returnhash,$code_order) = @_;
                   4795:     my @homeservers;
1.841     albertel 4796: 
                   4797:     my %servers = &get_servers($domain,'library');
                   4798:     foreach my $tryserver (keys(%servers)) {
                   4799: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4800: 	    push(@homeservers,$tryserver);
                   4801: 	}
1.792     raeburn  4802:     }
1.841     albertel 4803: 
1.792     raeburn  4804:     my $response;
1.841     albertel 4805:     foreach my $server (@homeservers) {
1.792     raeburn  4806:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 4807:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   4808: 	
                   4809: 	foreach my $pair (split(/\&/,$response)) {
                   4810: 	    my ($name,$value)=split(/\=/,$pair);
                   4811: 	    if ($name eq 'code_order') {
                   4812: 		@{$code_order} = split(/\&/,&unescape($value));
                   4813: 	    } else {
                   4814: 		$returnhash->{&unescape($name)}=&unescape($value);
                   4815: 	    }
                   4816: 	}
                   4817: 	return 'ok';
1.792     raeburn  4818:     }
1.841     albertel 4819: 
                   4820:     return $response;
1.792     raeburn  4821: } 
                   4822: 
1.777     albertel 4823: sub auto_validate_class_sec {
1.773     raeburn  4824:     my ($cdom,$cnum,$owner,$inst_class) = @_;
                   4825:     my $homeserver = &homeserver($cnum,$cdom);
                   4826:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.774     banghart 4827:                         &escape($owner).':'.$cdom,$homeserver);
1.773     raeburn  4828:     return $response;
                   4829: }
                   4830: 
1.679     raeburn  4831: # ------------------------------------------------------- Course Group routines
                   4832: 
                   4833: sub get_coursegroups {
1.809     raeburn  4834:     my ($cdom,$cnum,$group,$namespace) = @_;
                   4835:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  4836: }
                   4837: 
1.679     raeburn  4838: sub modify_coursegroup {
                   4839:     my ($cdom,$cnum,$groupsettings) = @_;
                   4840:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   4841: }
                   4842: 
1.809     raeburn  4843: sub toggle_coursegroup_status {
                   4844:     my ($cdom,$cnum,$group,$action) = @_;
                   4845:     my ($from_namespace,$to_namespace);
                   4846:     if ($action eq 'delete') {
                   4847:         $from_namespace = 'coursegroups';
                   4848:         $to_namespace = 'deleted_groups';
                   4849:     } else {
                   4850:         $from_namespace = 'deleted_groups';
                   4851:         $to_namespace = 'coursegroups';
                   4852:     }
                   4853:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  4854:     if (my $tmp = &error(%curr_group)) {
                   4855:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   4856:         return ('read error',$tmp);
                   4857:     } else {
                   4858:         my %savedsettings = %curr_group; 
1.809     raeburn  4859:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  4860:         my $deloutcome;
                   4861:         if ($result eq 'ok') {
1.809     raeburn  4862:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  4863:         } else {
                   4864:             return ('write error',$result);
                   4865:         }
                   4866:         if ($deloutcome eq 'ok') {
                   4867:             return 'ok';
                   4868:         } else {
                   4869:             return ('delete error',$deloutcome);
                   4870:         }
                   4871:     }
                   4872: }
                   4873: 
1.679     raeburn  4874: sub modify_group_roles {
                   4875:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
                   4876:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   4877:     my $role = 'gr/'.&escape($userprivs);
                   4878:     my ($uname,$udom) = split(/:/,$user);
                   4879:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684     raeburn  4880:     if ($result eq 'ok') {
                   4881:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   4882:     }
1.679     raeburn  4883:     return $result;
                   4884: }
                   4885: 
                   4886: sub modify_coursegroup_membership {
                   4887:     my ($cdom,$cnum,$membership) = @_;
                   4888:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   4889:     return $result;
                   4890: }
                   4891: 
1.682     raeburn  4892: sub get_active_groups {
                   4893:     my ($udom,$uname,$cdom,$cnum) = @_;
                   4894:     my $now = time;
                   4895:     my %groups = ();
                   4896:     foreach my $key (keys(%env)) {
1.811     albertel 4897:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  4898:             my ($start,$end) = split(/\./,$env{$key});
                   4899:             if (($end!=0) && ($end<$now)) { next; }
                   4900:             if (($start!=0) && ($start>$now)) { next; }
                   4901:             if ($1 eq $cdom && $2 eq $cnum) {
                   4902:                 $groups{$3} = $env{$key} ;
                   4903:             }
                   4904:         }
                   4905:     }
                   4906:     return %groups;
                   4907: }
                   4908: 
1.683     raeburn  4909: sub get_group_membership {
                   4910:     my ($cdom,$cnum,$group) = @_;
                   4911:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   4912: }
                   4913: 
                   4914: sub get_users_groups {
                   4915:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  4916:     my @usersgroups;
1.683     raeburn  4917:     my $cachetime=1800;
                   4918: 
                   4919:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  4920:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   4921:     if (defined($cached)) {
1.734     albertel 4922:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  4923:     } else {  
                   4924:         $grouplist = '';
1.816     raeburn  4925:         my $courseurl = &courseid_to_courseurl($courseid);
                   4926:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  4927:         my $access_end = $env{'course.'.$courseid.
                   4928:                               '.default_enrollment_end_date'};
                   4929:         my $now = time;
                   4930:         foreach my $key (keys(%roleshash)) {
                   4931:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   4932:                 my $group = $1;
                   4933:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   4934:                     my $start = $2;
                   4935:                     my $end = $1;
                   4936:                     if ($start == -1) { next; } # deleted from group
                   4937:                     if (($start!=0) && ($start>$now)) { next; }
                   4938:                     if (($end!=0) && ($end<$now)) {
                   4939:                         if ($access_end && $access_end < $now) {
                   4940:                             if ($access_end - $end < 86400) {
                   4941:                                 push(@usersgroups,$group);
1.733     raeburn  4942:                             }
                   4943:                         }
1.817     raeburn  4944:                         next;
1.733     raeburn  4945:                     }
1.817     raeburn  4946:                     push(@usersgroups,$group);
1.683     raeburn  4947:                 }
                   4948:             }
                   4949:         }
1.817     raeburn  4950:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   4951:         $grouplist = join(':',@usersgroups);
                   4952:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  4953:     }
1.733     raeburn  4954:     return @usersgroups;
1.683     raeburn  4955: }
                   4956: 
                   4957: sub devalidate_getgroups_cache {
                   4958:     my ($udom,$uname,$cdom,$cnum)=@_;
                   4959:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 4960: 
1.683     raeburn  4961:     my $hashid="$udom:$uname:$courseid";
                   4962:     &devalidate_cache_new('getgroups',$hashid);
                   4963: }
                   4964: 
1.12      www      4965: # ------------------------------------------------------------------ Plain Text
                   4966: 
                   4967: sub plaintext {
1.742     raeburn  4968:     my ($short,$type,$cid) = @_;
1.758     albertel 4969:     if ($short =~ /^cr/) {
                   4970: 	return (split('/',$short))[-1];
                   4971:     }
1.742     raeburn  4972:     if (!defined($cid)) {
                   4973:         $cid = $env{'request.course.id'};
                   4974:     }
                   4975:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
                   4976:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
                   4977:                                           '.plaintext'});
                   4978:     }
                   4979:     my %rolenames = (
                   4980:                       Course => 'std',
                   4981:                       Group => 'alt1',
                   4982:                     );
                   4983:     if (defined($type) && 
                   4984:          defined($rolenames{$type}) && 
                   4985:          defined($prp{$short}{$rolenames{$type}})) {
                   4986:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
                   4987:     } else {
                   4988:         return &Apache::lonlocal::mt($prp{$short}{'std'});
                   4989:     }
1.12      www      4990: }
                   4991: 
                   4992: # ----------------------------------------------------------------- Assign Role
                   4993: 
                   4994: sub assignrole {
1.357     www      4995:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21      www      4996:     my $mrole;
                   4997:     if ($role =~ /^cr\//) {
1.393     www      4998:         my $cwosec=$url;
1.811     albertel 4999:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      5000: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      5001:            &logthis('Refused custom assignrole: '.
                   5002:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 5003: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      5004:            return 'refused'; 
                   5005:         }
1.21      www      5006:         $mrole='cr';
1.678     raeburn  5007:     } elsif ($role =~ /^gr\//) {
                   5008:         my $cwogrp=$url;
1.811     albertel 5009:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  5010:         unless (&allowed('mdg',$cwogrp)) {
                   5011:             &logthis('Refused group assignrole: '.
                   5012:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   5013:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   5014:             return 'refused';
                   5015:         }
                   5016:         $mrole='gr';
1.21      www      5017:     } else {
1.82      www      5018:         my $cwosec=$url;
1.811     albertel 5019:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.373     www      5020:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
1.104     www      5021:            &logthis('Refused assignrole: '.
                   5022:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 5023: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      5024:            return 'refused'; 
                   5025:         }
1.21      www      5026:         $mrole=$role;
                   5027:     }
1.620     albertel 5028:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      5029:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      5030:     if ($end) { $command.='_'.$end; }
1.21      www      5031:     if ($start) {
                   5032: 	if ($end) { 
1.81      www      5033:            $command.='_'.$start; 
1.21      www      5034:         } else {
1.81      www      5035:            $command.='_0_'.$start;
1.21      www      5036:         }
                   5037:     }
1.739     raeburn  5038:     my $origstart = $start;
                   5039:     my $origend = $end;
1.357     www      5040: # actually delete
                   5041:     if ($deleteflag) {
1.373     www      5042: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      5043: # modify command to delete the role
1.620     albertel 5044:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      5045:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 5046: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      5047: # set start and finish to negative values for userrolelog
                   5048:            $start=-1;
                   5049:            $end=-1;
                   5050:         }
                   5051:     }
                   5052: # send command
1.349     www      5053:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      5054: # log new user role if status is ok
1.349     www      5055:     if ($answer eq 'ok') {
1.663     raeburn  5056: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  5057: # for course roles, perform group memberships changes triggered by role change.
                   5058:         unless ($role =~ /^gr/) {
                   5059:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   5060:                                              $origstart);
                   5061:         }
1.349     www      5062:     }
                   5063:     return $answer;
1.169     harris41 5064: }
                   5065: 
                   5066: # -------------------------------------------------- Modify user authentication
1.197     www      5067: # Overrides without validation
                   5068: 
1.169     harris41 5069: sub modifyuserauth {
                   5070:     my ($udom,$uname,$umode,$upass)=@_;
                   5071:     my $uhome=&homeserver($uname,$udom);
1.197     www      5072:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   5073:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 5074:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5075:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 5076:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   5077: 		     &escape($upass),$uhome);
1.620     albertel 5078:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      5079:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   5080:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   5081:     &log($udom,,$uname,$uhome,
1.620     albertel 5082:         'Authentication changed by '.$env{'user.domain'}.', '.
                   5083:                                      $env{'user.name'}.', '.$umode.
1.197     www      5084:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 5085:     unless ($reply eq 'ok') {
1.197     www      5086:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 5087: 	return 'error: '.$reply;
                   5088:     }   
1.170     harris41 5089:     return 'ok';
1.80      www      5090: }
                   5091: 
1.81      www      5092: # --------------------------------------------------------------- Modify a user
1.80      www      5093: 
1.81      www      5094: sub modifyuser {
1.206     matthew  5095:     my ($udom,    $uname, $uid,
                   5096:         $umode,   $upass, $first,
                   5097:         $middle,  $last,  $gene,
1.387     www      5098:         $forceid, $desiredhome, $email)=@_;
1.807     albertel 5099:     $udom= &LONCAPA::clean_domain($udom);
                   5100:     $uname=&LONCAPA::clean_username($uname);
1.81      www      5101:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5102:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  5103: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   5104:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   5105:                                      ' desiredhome not specified'). 
1.620     albertel 5106:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5107:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 5108:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      5109: # ----------------------------------------------------------------- Create User
1.406     albertel 5110:     if (($uhome eq 'no_host') && 
                   5111: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      5112:         my $unhome='';
1.844     albertel 5113:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  5114:             $unhome = $desiredhome;
1.620     albertel 5115: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   5116: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  5117:         } else { # load balancing routine for determining $unhome
1.81      www      5118:             my $loadm=10000000;
1.841     albertel 5119: 	    my %servers = &get_servers($udom,'library');
                   5120: 	    foreach my $tryserver (keys(%servers)) {
                   5121: 		my $answer=reply('load',$tryserver);
                   5122: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   5123: 		    $loadm=$answer;
                   5124: 		    $unhome=$tryserver;
                   5125: 		}
1.80      www      5126: 	    }
                   5127:         }
                   5128:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  5129: 	    return 'error: unable to find a home server for '.$uname.
                   5130:                    ' in domain '.$udom;
1.80      www      5131:         }
                   5132:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   5133:                          &escape($upass),$unhome);
                   5134: 	unless ($reply eq 'ok') {
                   5135:             return 'error: '.$reply;
                   5136:         }   
1.230     stredwic 5137:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      5138:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  5139: 	    return 'error: unable verify users home machine.';
1.80      www      5140:         }
1.209     matthew  5141:     }   # End of creation of new user
1.80      www      5142: # ---------------------------------------------------------------------- Add ID
                   5143:     if ($uid) {
                   5144:        $uid=~tr/A-Z/a-z/;
                   5145:        my %uidhash=&idrget($udom,$uname);
1.196     www      5146:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   5147:          && (!$forceid)) {
1.80      www      5148: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  5149: 	      return 'error: user id "'.$uid.'" does not match '.
                   5150:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      5151:           }
                   5152:        } else {
                   5153: 	  &idput($udom,($uname => $uid));
                   5154:        }
                   5155:     }
                   5156: # -------------------------------------------------------------- Add names, etc
1.313     matthew  5157:     my @tmp=&get('environment',
1.899     raeburn  5158: 		   ['firstname','middlename','lastname','generation','id',
                   5159:                     'permanentemail'],
1.134     albertel 5160: 		   $udom,$uname);
1.313     matthew  5161:     my %names;
                   5162:     if ($tmp[0] =~ m/^error:.*/) { 
                   5163:         %names=(); 
                   5164:     } else {
                   5165:         %names = @tmp;
                   5166:     }
1.388     www      5167: #
                   5168: # Make sure to not trash student environment if instructor does not bother
                   5169: # to supply name and email information
                   5170: #
                   5171:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  5172:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      5173:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  5174:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      5175:     if ($email) {
                   5176:        $email=~s/[^\w\@\.\-\,]//gs;
                   5177:        if ($email=~/\@/) { $names{'notification'} = $email;
                   5178: 			   $names{'critnotification'} = $email;
                   5179: 			   $names{'permanentemail'} = $email; }
                   5180:     }
1.899     raeburn  5181:     if ($uid) { $names{'id'}  = $uid; }
1.134     albertel 5182:     my $reply = &put('environment', \%names, $udom,$uname);
                   5183:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.899     raeburn  5184:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
1.680     www      5185:     &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81      www      5186:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5187:              $umode.', '.$first.', '.$middle.', '.
                   5188: 	     $last.', '.$gene.' by '.
1.620     albertel 5189:              $env{'user.name'}.' at '.$env{'user.domain'});
1.134     albertel 5190:     return 'ok';
1.80      www      5191: }
                   5192: 
1.81      www      5193: # -------------------------------------------------------------- Modify student
1.80      www      5194: 
1.81      www      5195: sub modifystudent {
                   5196:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515     raeburn  5197:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455     albertel 5198:     if (!$cid) {
1.620     albertel 5199: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5200: 	    return 'not_in_class';
                   5201: 	}
1.80      www      5202:     }
                   5203: # --------------------------------------------------------------- Make the user
1.81      www      5204:     my $reply=&modifyuser
1.209     matthew  5205: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      5206:          $desiredhome,$email);
1.80      www      5207:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  5208:     # This will cause &modify_student_enrollment to get the uid from the
                   5209:     # students environment
                   5210:     $uid = undef if (!$forceid);
1.455     albertel 5211:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515     raeburn  5212: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
1.297     matthew  5213:     return $reply;
                   5214: }
                   5215: 
                   5216: sub modify_student_enrollment {
1.515     raeburn  5217:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455     albertel 5218:     my ($cdom,$cnum,$chome);
                   5219:     if (!$cid) {
1.620     albertel 5220: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5221: 	    return 'not_in_class';
                   5222: 	}
1.620     albertel 5223: 	$cdom=$env{'course.'.$cid.'.domain'};
                   5224: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 5225:     } else {
                   5226: 	($cdom,$cnum)=split(/_/,$cid);
                   5227:     }
1.620     albertel 5228:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 5229:     if (!$chome) {
1.457     raeburn  5230: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  5231:     }
1.455     albertel 5232:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  5233:     # Make sure the user exists
1.81      www      5234:     my $uhome=&homeserver($uname,$udom);
                   5235:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5236: 	return 'error: no such user';
                   5237:     }
1.297     matthew  5238:     # Get student data if we were not given enough information
                   5239:     if (!defined($first)  || $first  eq '' || 
                   5240:         !defined($last)   || $last   eq '' || 
                   5241:         !defined($uid)    || $uid    eq '' || 
                   5242:         !defined($middle) || $middle eq '' || 
                   5243:         !defined($gene)   || $gene   eq '') {
1.294     matthew  5244:         # They did not supply us with enough data to enroll the student, so
                   5245:         # we need to pick up more information.
1.297     matthew  5246:         my %tmp = &get('environment',
1.294     matthew  5247:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  5248:                        ,$udom,$uname);
                   5249: 
1.800     albertel 5250:         #foreach my $key (keys(%tmp)) {
                   5251:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 5252:         #}
1.294     matthew  5253:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   5254:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   5255:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  5256:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  5257:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   5258:     }
1.556     albertel 5259:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 5260:     my $reply=cput('classlist',
                   5261: 		   {"$uname:$udom" => 
1.515     raeburn  5262: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 5263: 		   $cdom,$cnum);
1.81      www      5264:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   5265: 	return 'error: '.$reply;
1.652     albertel 5266:     } else {
                   5267: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      5268:     }
1.297     matthew  5269:     # Add student role to user
1.83      www      5270:     my $uurl='/'.$cid;
1.81      www      5271:     $uurl=~s/\_/\//g;
                   5272:     if ($usec) {
                   5273: 	$uurl.='/'.$usec;
                   5274:     }
                   5275:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      5276: }
                   5277: 
1.556     albertel 5278: sub format_name {
                   5279:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   5280:     my $name;
                   5281:     if ($first ne 'lastname') {
                   5282: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   5283:     } else {
                   5284: 	if ($lastname=~/\S/) {
                   5285: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   5286: 	    $name=~s/\s+,/,/;
                   5287: 	} else {
                   5288: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   5289: 	}
                   5290:     }
                   5291:     $name=~s/^\s+//;
                   5292:     $name=~s/\s+$//;
                   5293:     $name=~s/\s+/ /g;
                   5294:     return $name;
                   5295: }
                   5296: 
1.84      www      5297: # ------------------------------------------------- Write to course preferences
                   5298: 
                   5299: sub writecoursepref {
                   5300:     my ($courseid,%prefs)=@_;
                   5301:     $courseid=~s/^\///;
                   5302:     $courseid=~s/\_/\//g;
                   5303:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   5304:     my $chome=homeserver($cnum,$cdomain);
                   5305:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   5306: 	return 'error: no such course';
                   5307:     }
                   5308:     my $cstring='';
1.800     albertel 5309:     foreach my $pref (keys(%prefs)) {
                   5310: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 5311:     }
1.84      www      5312:     $cstring=~s/\&$//;
                   5313:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   5314: }
                   5315: 
                   5316: # ---------------------------------------------------------- Make/modify course
                   5317: 
                   5318: sub createcourse {
1.741     raeburn  5319:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
                   5320:         $course_owner,$crstype)=@_;
1.84      www      5321:     $url=&declutter($url);
                   5322:     my $cid='';
1.264     matthew  5323:     unless (&allowed('ccc',$udom)) {
1.84      www      5324:         return 'refused';
                   5325:     }
                   5326: # ------------------------------------------------------------------- Create ID
1.674     www      5327:    my $uname=int(1+rand(9)).
                   5328:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   5329:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84      www      5330:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   5331: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 5332:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      5333:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5334:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   5335:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 5336:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      5337:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5338:            return 'error: unable to generate unique course-ID';
                   5339:        } 
                   5340:    }
1.264     matthew  5341: # ------------------------------------------------ Check supplied server name
1.620     albertel 5342:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845     albertel 5343:     if (! &is_library($course_server)) {
1.264     matthew  5344:         return 'error:bad server name '.$course_server;
                   5345:     }
1.84      www      5346: # ------------------------------------------------------------- Make the course
                   5347:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  5348:                       $course_server);
1.84      www      5349:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 5350:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      5351:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5352: 	return 'error: no such course';
                   5353:     }
1.271     www      5354: # ----------------------------------------------------------------- Course made
1.516     raeburn  5355: # log existence
                   5356:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.741     raeburn  5357:                  ':'.&escape($inst_code).':'.&escape($course_owner).':'.
                   5358:                   &escape($crstype),$uhome);
1.358     www      5359:     &flushcourselogs();
                   5360: # set toplevel url
1.271     www      5361:     my $topurl=$url;
                   5362:     unless ($nonstandard) {
                   5363: # ------------------------------------------ For standard courses, make top url
                   5364:         my $mapurl=&clutter($url);
1.278     www      5365:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 5366:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      5367: <map>
                   5368: <resource id="1" type="start"></resource>
                   5369: <resource id="2" src="$mapurl"></resource>
                   5370: <resource id="3" type="finish"></resource>
                   5371: <link index="1" from="1" to="2"></link>
                   5372: <link index="2" from="2" to="3"></link>
                   5373: </map>
                   5374: ENDINITMAP
                   5375:         $topurl=&declutter(
1.638     albertel 5376:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      5377:                           );
                   5378:     }
                   5379: # ----------------------------------------------------------- Write preferences
1.84      www      5380:     &writecoursepref($udom.'_'.$uname,
                   5381:                      ('description' => $description,
1.271     www      5382:                       'url'         => $topurl));
1.84      www      5383:     return '/'.$udom.'/'.$uname;
                   5384: }
                   5385: 
1.813     albertel 5386: sub is_course {
                   5387:     my ($cdom,$cnum) = @_;
                   5388:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
                   5389: 				undef,'.');
                   5390:     if (exists($courses{$cdom.'_'.$cnum})) {
                   5391:         return 1;
                   5392:     }
                   5393:     return 0;
                   5394: }
                   5395: 
1.21      www      5396: # ---------------------------------------------------------- Assign Custom Role
                   5397: 
                   5398: sub assigncustomrole {
1.357     www      5399:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21      www      5400:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357     www      5401:                        $end,$start,$deleteflag);
1.21      www      5402: }
                   5403: 
                   5404: # ----------------------------------------------------------------- Revoke Role
                   5405: 
                   5406: sub revokerole {
1.357     www      5407:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21      www      5408:     my $now=time;
1.357     www      5409:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21      www      5410: }
                   5411: 
                   5412: # ---------------------------------------------------------- Revoke Custom Role
                   5413: 
                   5414: sub revokecustomrole {
1.357     www      5415:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21      www      5416:     my $now=time;
1.357     www      5417:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
                   5418:            $deleteflag);
1.17      www      5419: }
                   5420: 
1.533     banghart 5421: # ------------------------------------------------------------ Disk usage
1.535     albertel 5422: sub diskusage {
1.533     banghart 5423:     my ($udom,$uname,$directoryRoot)=@_;
                   5424:     $directoryRoot =~ s/\/$//;
1.535     albertel 5425:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514     albertel 5426:     return $listing;
1.512     banghart 5427: }
                   5428: 
1.566     banghart 5429: sub is_locked {
                   5430:     my ($file_name, $domain, $user) = @_;
                   5431:     my @check;
                   5432:     my $is_locked;
                   5433:     push @check, $file_name;
1.613     albertel 5434:     my %locked = &get('file_permissions',\@check,
1.620     albertel 5435: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 5436:     my ($tmp)=keys(%locked);
                   5437:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  5438:     
1.566     banghart 5439:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  5440:         $is_locked = 'false';
                   5441:         foreach my $entry (@{$locked{$file_name}}) {
                   5442:            if (ref($entry) eq 'ARRAY') { 
1.746     raeburn  5443:                $is_locked = 'true';
                   5444:                last;
1.745     raeburn  5445:            }
                   5446:        }
1.566     banghart 5447:     } else {
                   5448:         $is_locked = 'false';
                   5449:     }
                   5450: }
                   5451: 
1.759     albertel 5452: sub declutter_portfile {
                   5453:     my ($file) = @_;
1.833     albertel 5454:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 5455:     return $file;
                   5456: }
                   5457: 
1.559     banghart 5458: # ------------------------------------------------------------- Mark as Read Only
                   5459: 
                   5460: sub mark_as_readonly {
                   5461:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 5462:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5463:     my ($tmp)=keys(%current_permissions);
                   5464:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 5465:     foreach my $file (@{$files}) {
1.759     albertel 5466: 	$file = &declutter_portfile($file);
1.561     banghart 5467:         push(@{$current_permissions{$file}},$what);
1.559     banghart 5468:     }
1.613     albertel 5469:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5470:     return;
                   5471: }
                   5472: 
1.572     banghart 5473: # ------------------------------------------------------------Save Selected Files
                   5474: 
                   5475: sub save_selected_files {
                   5476:     my ($user, $path, @files) = @_;
                   5477:     my $filename = $user."savedfiles";
1.573     banghart 5478:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 5479:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 5480:     foreach my $file (@files) {
1.620     albertel 5481:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 5482:     }
                   5483:     foreach my $file (@other_files) {
1.574     banghart 5484:         print (OUT $file."\n");
1.572     banghart 5485:     }
1.574     banghart 5486:     close (OUT);
1.572     banghart 5487:     return 'ok';
                   5488: }
                   5489: 
1.574     banghart 5490: sub clear_selected_files {
                   5491:     my ($user) = @_;
                   5492:     my $filename = $user."savedfiles";
                   5493:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5494:     print (OUT undef);
                   5495:     close (OUT);
                   5496:     return ("ok");    
                   5497: }
                   5498: 
1.572     banghart 5499: sub files_in_path {
                   5500:     my ($user, $path) = @_;
                   5501:     my $filename = $user."savedfiles";
                   5502:     my %return_files;
1.574     banghart 5503:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5504:     while (my $line_in = <IN>) {
1.574     banghart 5505:         chomp ($line_in);
                   5506:         my @paths_and_file = split (m!/!, $line_in);
                   5507:         my $file_part = pop (@paths_and_file);
                   5508:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 5509:         $path_part.='/';
                   5510:         my $path_and_file = $path_part.$file_part;
                   5511:         if ($path_part eq $path) {
                   5512:             $return_files{$file_part}= 'selected';
                   5513:         }
                   5514:     }
1.574     banghart 5515:     close (IN);
                   5516:     return (\%return_files);
1.572     banghart 5517: }
                   5518: 
                   5519: # called in portfolio select mode, to show files selected NOT in current directory
                   5520: sub files_not_in_path {
                   5521:     my ($user, $path) = @_;
                   5522:     my $filename = $user."savedfiles";
                   5523:     my @return_files;
                   5524:     my $path_part;
1.800     albertel 5525:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5526:     while (my $line = <IN>) {
1.572     banghart 5527:         #ok, I know it's clunky, but I want it to work
1.800     albertel 5528:         my @paths_and_file = split(m|/|, $line);
                   5529:         my $file_part = pop(@paths_and_file);
                   5530:         chomp($file_part);
                   5531:         my $path_part = join('/', @paths_and_file);
1.572     banghart 5532:         $path_part .= '/';
                   5533:         my $path_and_file = $path_part.$file_part;
                   5534:         if ($path_part ne $path) {
1.800     albertel 5535:             push(@return_files, ($path_and_file));
1.572     banghart 5536:         }
                   5537:     }
1.800     albertel 5538:     close(OUT);
1.574     banghart 5539:     return (@return_files);
1.572     banghart 5540: }
                   5541: 
1.745     raeburn  5542: #----------------------------------------------Get portfolio file permissions
1.629     banghart 5543: 
1.745     raeburn  5544: sub get_portfile_permissions {
                   5545:     my ($domain,$user) = @_;
1.613     albertel 5546:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5547:     my ($tmp)=keys(%current_permissions);
                   5548:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5549:     return \%current_permissions;
                   5550: }
                   5551: 
                   5552: #---------------------------------------------Get portfolio file access controls
                   5553: 
1.749     raeburn  5554: sub get_access_controls {
1.745     raeburn  5555:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 5556:     my %access;
                   5557:     my $real_file = $file;
                   5558:     $file =~ s/\.meta$//;
1.745     raeburn  5559:     if (defined($file)) {
1.749     raeburn  5560:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   5561:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 5562:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  5563:             }
                   5564:         }
1.745     raeburn  5565:     } else {
1.749     raeburn  5566:         foreach my $key (keys(%{$current_permissions})) {
                   5567:             if ($key =~ /\0accesscontrol$/) {
                   5568:                 if (defined($group)) {
                   5569:                     if ($key !~ m-^\Q$group\E/-) {
                   5570:                         next;
                   5571:                     }
                   5572:                 }
                   5573:                 my ($fullpath) = split(/\0/,$key);
                   5574:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   5575:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   5576:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   5577:                     }
                   5578:                 }
                   5579:             }
                   5580:         }
                   5581:     }
                   5582:     return %access;
                   5583: }
                   5584: 
                   5585: sub modify_access_controls {
                   5586:     my ($file_name,$changes,$domain,$user)=@_;
                   5587:     my ($outcome,$deloutcome);
                   5588:     my %store_permissions;
                   5589:     my %new_values;
                   5590:     my %new_control;
                   5591:     my %translation;
                   5592:     my @deletions = ();
                   5593:     my $now = time;
                   5594:     if (exists($$changes{'activate'})) {
                   5595:         if (ref($$changes{'activate'}) eq 'HASH') {
                   5596:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   5597:             my $numnew = scalar(@newitems);
                   5598:             for (my $i=0; $i<$numnew; $i++) {
                   5599:                 my $newkey = $newitems[$i];
                   5600:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  5601:                 if ($newkey =~ /^\d+:/) { 
                   5602:                     $newkey =~ s/^(\d+)/$newid/;
                   5603:                     $translation{$1} = $newid;
                   5604:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   5605:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   5606:                     $translation{$1} = $newid;
                   5607:                 }
1.749     raeburn  5608:                 $new_values{$file_name."\0".$newkey} = 
                   5609:                                           $$changes{'activate'}{$newitems[$i]};
                   5610:                 $new_control{$newkey} = $now;
                   5611:             }
                   5612:         }
                   5613:     }
                   5614:     my %todelete;
                   5615:     my %changed_items;
                   5616:     foreach my $action ('delete','update') {
                   5617:         if (exists($$changes{$action})) {
                   5618:             if (ref($$changes{$action}) eq 'HASH') {
                   5619:                 foreach my $key (keys(%{$$changes{$action}})) {
                   5620:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   5621:                     if ($action eq 'delete') { 
                   5622:                         $todelete{$itemnum} = 1;
                   5623:                     } else {
                   5624:                         $changed_items{$itemnum} = $key;
                   5625:                     }
                   5626:                 }
1.745     raeburn  5627:             }
                   5628:         }
1.749     raeburn  5629:     }
                   5630:     # get lock on access controls for file.
                   5631:     my $lockhash = {
                   5632:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   5633:                                                        ':'.$env{'user.domain'},
                   5634:                    }; 
                   5635:     my $tries = 0;
                   5636:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5637:    
                   5638:     while (($gotlock ne 'ok') && $tries <3) {
                   5639:         $tries ++;
                   5640:         sleep 1;
                   5641:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5642:     }
                   5643:     if ($gotlock eq 'ok') {
                   5644:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   5645:         my ($tmp)=keys(%curr_permissions);
                   5646:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   5647:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   5648:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   5649:             if (ref($curr_controls) eq 'HASH') {
                   5650:                 foreach my $control_item (keys(%{$curr_controls})) {
                   5651:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   5652:                     if (defined($todelete{$itemnum})) {
                   5653:                         push(@deletions,$file_name."\0".$control_item);
                   5654:                     } else {
                   5655:                         if (defined($changed_items{$itemnum})) {
                   5656:                             $new_control{$changed_items{$itemnum}} = $now;
                   5657:                             push(@deletions,$file_name."\0".$control_item);
                   5658:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   5659:                         } else {
                   5660:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   5661:                         }
                   5662:                     }
1.745     raeburn  5663:                 }
                   5664:             }
                   5665:         }
1.749     raeburn  5666:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   5667:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   5668:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   5669:         #  remove lock
                   5670:         my @del_lock = ($file_name."\0".'locked_access_records');
                   5671:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  5672:         my ($file,$group);
                   5673:         if (&is_course($domain,$user)) {
                   5674:             ($group,$file) = split(/\//,$file_name,2);
                   5675:         } else {
                   5676:             $file = $file_name;
                   5677:         }
                   5678:         my $sqlresult =
                   5679:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
                   5680:                                     $group);
1.749     raeburn  5681:     } else {
                   5682:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  5683:     }
1.749     raeburn  5684:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  5685: }
                   5686: 
1.827     raeburn  5687: sub make_public_indefinitely {
                   5688:     my ($requrl) = @_;
                   5689:     my $now = time;
                   5690:     my $action = 'activate';
                   5691:     my $aclnum = 0;
                   5692:     if (&is_portfolio_url($requrl)) {
                   5693:         my (undef,$udom,$unum,$file_name,$group) =
                   5694:             &parse_portfolio_url($requrl);
                   5695:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   5696:         my %access_controls = &get_access_controls($current_perms,
                   5697:                                                    $group,$file_name);
                   5698:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   5699:             my ($num,$scope,$end,$start) = 
                   5700:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5701:             if ($scope eq 'public') {
                   5702:                 if ($start <= $now && $end == 0) {
                   5703:                     $action = 'none';
                   5704:                 } else {
                   5705:                     $action = 'update';
                   5706:                     $aclnum = $num;
                   5707:                 }
                   5708:                 last;
                   5709:             }
                   5710:         }
                   5711:         if ($action eq 'none') {
                   5712:              return 'ok';
                   5713:         } else {
                   5714:             my %changes;
                   5715:             my $newend = 0;
                   5716:             my $newstart = $now;
                   5717:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   5718:             $changes{$action}{$newkey} = {
                   5719:                 type => 'public',
                   5720:                 time => {
                   5721:                     start => $newstart,
                   5722:                     end   => $newend,
                   5723:                 },
                   5724:             };
                   5725:             my ($outcome,$deloutcome,$new_values,$translation) =
                   5726:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   5727:             return $outcome;
                   5728:         }
                   5729:     } else {
                   5730:         return 'invalid';
                   5731:     }
                   5732: }
                   5733: 
1.745     raeburn  5734: #------------------------------------------------------Get Marked as Read Only
                   5735: 
                   5736: sub get_marked_as_readonly {
                   5737:     my ($domain,$user,$what,$group) = @_;
                   5738:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 5739:     my @readonly_files;
1.629     banghart 5740:     my $cmp1=$what;
                   5741:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  5742:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5743:         if (defined($group)) {
                   5744:             if ($file_name !~ m-^\Q$group\E/-) {
                   5745:                 next;
                   5746:             }
                   5747:         }
1.561     banghart 5748:         if (ref($value) eq "ARRAY"){
                   5749:             foreach my $stored_what (@{$value}) {
1.629     banghart 5750:                 my $cmp2=$stored_what;
1.759     albertel 5751:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  5752:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  5753:                 }
1.629     banghart 5754:                 if ($cmp1 eq $cmp2) {
1.561     banghart 5755:                     push(@readonly_files, $file_name);
1.745     raeburn  5756:                     last;
1.563     banghart 5757:                 } elsif (!defined($what)) {
                   5758:                     push(@readonly_files, $file_name);
1.745     raeburn  5759:                     last;
1.561     banghart 5760:                 }
                   5761:             }
1.745     raeburn  5762:         }
1.561     banghart 5763:     }
                   5764:     return @readonly_files;
                   5765: }
1.577     banghart 5766: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 5767: 
1.577     banghart 5768: sub get_marked_as_readonly_hash {
1.745     raeburn  5769:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 5770:     my %readonly_files;
1.745     raeburn  5771:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5772:         if (defined($group)) {
                   5773:             if ($file_name !~ m-^\Q$group\E/-) {
                   5774:                 next;
                   5775:             }
                   5776:         }
1.577     banghart 5777:         if (ref($value) eq "ARRAY"){
                   5778:             foreach my $stored_what (@{$value}) {
1.745     raeburn  5779:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 5780:                     foreach my $lock_descriptor(@{$stored_what}) {
                   5781:                         if ($lock_descriptor eq 'graded') {
                   5782:                             $readonly_files{$file_name} = 'graded';
                   5783:                         } elsif ($lock_descriptor eq 'handback') {
                   5784:                             $readonly_files{$file_name} = 'handback';
                   5785:                         } else {
                   5786:                             if (!exists($readonly_files{$file_name})) {
                   5787:                                 $readonly_files{$file_name} = 'locked';
                   5788:                             }
                   5789:                         }
1.745     raeburn  5790:                     }
1.750     banghart 5791:                 } 
1.577     banghart 5792:             }
                   5793:         } 
                   5794:     }
                   5795:     return %readonly_files;
                   5796: }
1.559     banghart 5797: # ------------------------------------------------------------ Unmark as Read Only
                   5798: 
                   5799: sub unmark_as_readonly {
1.629     banghart 5800:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   5801:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  5802:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 5803:     $file_name = &declutter_portfile($file_name);
1.634     albertel 5804:     my $symb_crs = $what;
                   5805:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  5806:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 5807:     my ($tmp)=keys(%current_permissions);
                   5808:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5809:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 5810:     foreach my $file (@readonly_files) {
1.759     albertel 5811: 	my $clean_file = &declutter_portfile($file);
                   5812: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 5813: 	my $current_locks = $current_permissions{$file};
1.563     banghart 5814:         my @new_locks;
                   5815:         my @del_keys;
                   5816:         if (ref($current_locks) eq "ARRAY"){
                   5817:             foreach my $locker (@{$current_locks}) {
1.632     albertel 5818:                 my $compare=$locker;
1.749     raeburn  5819:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  5820:                     $compare=join('',@{$locker});
1.746     raeburn  5821:                     if ($compare ne $symb_crs) {
                   5822:                         push(@new_locks, $locker);
                   5823:                     }
1.563     banghart 5824:                 }
                   5825:             }
1.650     albertel 5826:             if (scalar(@new_locks) > 0) {
1.563     banghart 5827:                 $current_permissions{$file} = \@new_locks;
                   5828:             } else {
                   5829:                 push(@del_keys, $file);
1.613     albertel 5830:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 5831:                 delete($current_permissions{$file});
1.563     banghart 5832:             }
                   5833:         }
1.561     banghart 5834:     }
1.613     albertel 5835:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5836:     return;
                   5837: }
1.512     banghart 5838: 
1.17      www      5839: # ------------------------------------------------------------ Directory lister
                   5840: 
                   5841: sub dirlist {
1.253     stredwic 5842:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   5843: 
1.18      www      5844:     $uri=~s/^\///;
                   5845:     $uri=~s/\/$//;
1.253     stredwic 5846:     my ($udom, $uname);
                   5847:     (undef,$udom,$uname)=split(/\//,$uri);
                   5848:     if(defined($userdomain)) {
                   5849:         $udom = $userdomain;
                   5850:     }
                   5851:     if(defined($username)) {
                   5852:         $uname = $username;
                   5853:     }
                   5854: 
                   5855:     my $dirRoot = $perlvar{'lonDocRoot'};
                   5856:     if(defined($alternateDirectoryRoot)) {
                   5857:         $dirRoot = $alternateDirectoryRoot;
                   5858:         $dirRoot =~ s/\/$//;
1.751     banghart 5859:     }
1.253     stredwic 5860: 
                   5861:     if($udom) {
                   5862:         if($uname) {
1.800     albertel 5863:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   5864: 				 &homeserver($uname,$udom));
1.605     matthew  5865:             my @listing_results;
                   5866:             if ($listing eq 'unknown_cmd') {
1.800     albertel 5867:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   5868: 				  &homeserver($uname,$udom));
1.605     matthew  5869:                 @listing_results = split(/:/,$listing);
                   5870:             } else {
                   5871:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   5872:             }
                   5873:             return @listing_results;
1.253     stredwic 5874:         } elsif(!defined($alternateDirectoryRoot)) {
1.800     albertel 5875:             my %allusers;
1.841     albertel 5876: 	    my %servers = &get_servers($udom,'library');
                   5877: 	    foreach my $tryserver (keys(%servers)) {
                   5878: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5879: 				     $udom, $tryserver);
                   5880: 		my @listing_results;
                   5881: 		if ($listing eq 'unknown_cmd') {
                   5882: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5883: 				      $udom, $tryserver);
                   5884: 		    @listing_results = split(/:/,$listing);
                   5885: 		} else {
                   5886: 		    @listing_results =
                   5887: 			map { &unescape($_); } split(/:/,$listing);
                   5888: 		}
                   5889: 		if ($listing_results[0] ne 'no_such_dir' && 
                   5890: 		    $listing_results[0] ne 'empty'       &&
                   5891: 		    $listing_results[0] ne 'con_lost') {
                   5892: 		    foreach my $line (@listing_results) {
                   5893: 			my ($entry) = split(/&/,$line,2);
                   5894: 			$allusers{$entry} = 1;
                   5895: 		    }
                   5896: 		}
1.253     stredwic 5897:             }
                   5898:             my $alluserstr='';
1.800     albertel 5899:             foreach my $user (sort(keys(%allusers))) {
                   5900:                 $alluserstr.=$user.'&user:';
1.253     stredwic 5901:             }
                   5902:             $alluserstr=~s/:$//;
                   5903:             return split(/:/,$alluserstr);
                   5904:         } else {
1.800     albertel 5905:             return ('missing user name');
1.253     stredwic 5906:         }
                   5907:     } elsif(!defined($alternateDirectoryRoot)) {
1.841     albertel 5908:         my @all_domains = sort(&all_domains());
                   5909:          foreach my $domain (@all_domains) {
                   5910:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   5911:          }
                   5912:          return @all_domains;
                   5913:      } else {
1.800     albertel 5914:         return ('missing domain');
1.275     stredwic 5915:     }
                   5916: }
                   5917: 
                   5918: # --------------------------------------------- GetFileTimestamp
                   5919: # This function utilizes dirlist and returns the date stamp for
                   5920: # when it was last modified.  It will also return an error of -1
                   5921: # if an error occurs
                   5922: 
1.410     matthew  5923: ##
                   5924: ## FIXME: This subroutine assumes its caller knows something about the
                   5925: ## directory structure of the home server for the student ($root).
                   5926: ## Not a good assumption to make.  Since this is for looking up files
                   5927: ## in user directories, the full path should be constructed by lond, not
                   5928: ## whatever machine we request data from.
                   5929: ##
1.275     stredwic 5930: sub GetFileTimestamp {
                   5931:     my ($studentDomain,$studentName,$filename,$root)=@_;
1.807     albertel 5932:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   5933:     $studentName   = &LONCAPA::clean_username($studentName);
1.275     stredwic 5934:     my $subdir=$studentName.'__';
                   5935:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   5936:     my $proname="$studentDomain/$subdir/$studentName";
                   5937:     $proname .= '/'.$filename;
1.375     matthew  5938:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
                   5939:                                               $studentName, $root);
1.275     stredwic 5940:     my @stats = split('&', $fileStat);
                   5941:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  5942:         # @stats contains first the filename, then the stat output
                   5943:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 5944:     } else {
                   5945:         return -1;
1.253     stredwic 5946:     }
1.26      www      5947: }
                   5948: 
1.712     albertel 5949: sub stat_file {
                   5950:     my ($uri) = @_;
1.787     albertel 5951:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 5952: 
1.712     albertel 5953:     my ($udom,$uname,$file,$dir);
                   5954:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   5955: 	($udom,$uname,$file) =
1.811     albertel 5956: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 5957: 	$file = 'userfiles/'.$file;
1.740     www      5958: 	$dir = &propath($udom,$uname);
1.712     albertel 5959:     }
                   5960:     if ($uri =~ m-^/res/-) {
                   5961: 	($udom,$uname) = 
1.807     albertel 5962: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 5963: 	$file = $uri;
                   5964:     }
                   5965: 
                   5966:     if (!$udom || !$uname || !$file) {
                   5967: 	# unable to handle the uri
                   5968: 	return ();
                   5969:     }
                   5970: 
                   5971:     my ($result) = &dirlist($file,$udom,$uname,$dir);
                   5972:     my @stats = split('&', $result);
1.721     banghart 5973:     
1.712     albertel 5974:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   5975: 	shift(@stats); #filename is first
                   5976: 	return @stats;
                   5977:     }
                   5978:     return ();
                   5979: }
                   5980: 
1.26      www      5981: # -------------------------------------------------------- Value of a Condition
                   5982: 
1.713     albertel 5983: # gets the value of a specific preevaluated condition
                   5984: #    stored in the string  $env{user.state.<cid>}
                   5985: # or looks up a condition reference in the bighash and if if hasn't
                   5986: # already been evaluated recurses into docondval to get the value of
                   5987: # the condition, then memoizing it to 
                   5988: #   $env{user.state.<cid>.<condition>}
1.40      www      5989: sub directcondval {
                   5990:     my $number=shift;
1.620     albertel 5991:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 5992: 	&Apache::lonuserstate::evalstate();
                   5993:     }
1.713     albertel 5994:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   5995: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   5996:     } elsif ($number =~ /^_/) {
                   5997: 	my $sub_condition;
                   5998: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   5999: 		&GDBM_READER(),0640)) {
                   6000: 	    $sub_condition=$bighash{'conditions'.$number};
                   6001: 	    untie(%bighash);
                   6002: 	}
                   6003: 	my $value = &docondval($sub_condition);
                   6004: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
                   6005: 	return $value;
                   6006:     }
1.620     albertel 6007:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   6008:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      6009:     } else {
                   6010:        return 2;
                   6011:     }
                   6012: }
                   6013: 
1.713     albertel 6014: # get the collection of conditions for this resource
1.26      www      6015: sub condval {
                   6016:     my $condidx=shift;
1.54      www      6017:     my $allpathcond='';
1.713     albertel 6018:     foreach my $cond (split(/\|/,$condidx)) {
                   6019: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   6020: 	    $allpathcond.=
                   6021: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   6022: 	}
1.191     harris41 6023:     }
1.54      www      6024:     $allpathcond=~s/\|$//;
1.713     albertel 6025:     return &docondval($allpathcond);
                   6026: }
                   6027: 
                   6028: #evaluates an expression of conditions
                   6029: sub docondval {
                   6030:     my ($allpathcond) = @_;
                   6031:     my $result=0;
                   6032:     if ($env{'request.course.id'}
                   6033: 	&& defined($allpathcond)) {
                   6034: 	my $operand='|';
                   6035: 	my @stack;
                   6036: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   6037: 	    if ($chunk eq '(') {
                   6038: 		push @stack,($operand,$result);
                   6039: 	    } elsif ($chunk eq ')') {
                   6040: 		my $before=pop @stack;
                   6041: 		if (pop @stack eq '&') {
                   6042: 		    $result=$result>$before?$before:$result;
                   6043: 		} else {
                   6044: 		    $result=$result>$before?$result:$before;
                   6045: 		}
                   6046: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   6047: 		$operand=$chunk;
                   6048: 	    } else {
                   6049: 		my $new=directcondval($chunk);
                   6050: 		if ($operand eq '&') {
                   6051: 		    $result=$result>$new?$new:$result;
                   6052: 		} else {
                   6053: 		    $result=$result>$new?$result:$new;
                   6054: 		}
                   6055: 	    }
                   6056: 	}
1.26      www      6057:     }
                   6058:     return $result;
1.421     albertel 6059: }
                   6060: 
                   6061: # ---------------------------------------------------- Devalidate courseresdata
                   6062: 
                   6063: sub devalidatecourseresdata {
                   6064:     my ($coursenum,$coursedomain)=@_;
                   6065:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6066:     &devalidate_cache_new('courseres',$hashid);
1.28      www      6067: }
                   6068: 
1.763     www      6069: 
1.200     www      6070: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     6071: #
                   6072: #  Parameters:
                   6073: #      $coursenum    - Number of the course.
                   6074: #      $coursedomain - Domain at which the course was created.
                   6075: #  Returns:
                   6076: #     A hash of the course parameters along (I think) with timestamps
                   6077: #     and version info.
1.877     foxr     6078: 
1.624     albertel 6079: sub get_courseresdata {
                   6080:     my ($coursenum,$coursedomain)=@_;
1.200     www      6081:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   6082:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6083:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 6084:     my %dumpreply;
1.417     albertel 6085:     unless (defined($cached)) {
1.624     albertel 6086: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 6087: 	$result=\%dumpreply;
1.251     albertel 6088: 	my ($tmp) = keys(%dumpreply);
                   6089: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 6090: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 6091: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   6092: 	    return $tmp;
1.416     albertel 6093: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 6094: 	    $result=undef;
1.599     albertel 6095: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 6096: 	}
                   6097:     }
1.624     albertel 6098:     return $result;
                   6099: }
                   6100: 
1.633     albertel 6101: sub devalidateuserresdata {
                   6102:     my ($uname,$udom)=@_;
                   6103:     my $hashid="$udom:$uname";
                   6104:     &devalidate_cache_new('userres',$hashid);
                   6105: }
                   6106: 
1.624     albertel 6107: sub get_userresdata {
                   6108:     my ($uname,$udom)=@_;
                   6109:     #most student don\'t have any data set, check if there is some data
                   6110:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   6111: 
                   6112:     my $hashid="$udom:$uname";
                   6113:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   6114:     if (!defined($cached)) {
                   6115: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   6116: 	$result=\%resourcedata;
                   6117: 	&do_cache_new('userres',$hashid,$result,600);
                   6118:     }
                   6119:     my ($tmp)=keys(%$result);
                   6120:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   6121: 	return $result;
                   6122:     }
                   6123:     #error 2 occurs when the .db doesn't exist
                   6124:     if ($tmp!~/error: 2 /) {
1.672     albertel 6125: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 6126: 		 " Trying to get resource data for ".
                   6127: 		 $uname." at ".$udom.": ".
                   6128: 		 $tmp."</font>");
                   6129:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 6130: 	#&EXT_cache_set($udom,$uname);
                   6131: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 6132: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 6133:     }
                   6134:     return $tmp;
                   6135: }
1.879     foxr     6136: #----------------------------------------------- resdata - return resource data
                   6137: #  Purpose:
                   6138: #    Return resource data for either users or for a course.
                   6139: #  Parameters:
                   6140: #     $name      - Course/user name.
                   6141: #     $domain    - Name of the domain the user/course is registered on.
                   6142: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   6143: #     @which     - Array of names of resources desired.
                   6144: #  Returns:
                   6145: #     The value of the first reasource in @which that is found in the
                   6146: #     resource hash.
                   6147: #  Exceptional Conditions:
                   6148: #     If the $type passed in is not valid (not the string 'course' or 
                   6149: #     'user', an undefined  reference is returned.
                   6150: #     If none of the resources are found, an undef is returned
1.624     albertel 6151: sub resdata {
                   6152:     my ($name,$domain,$type,@which)=@_;
                   6153:     my $result;
                   6154:     if ($type eq 'course') {
                   6155: 	$result=&get_courseresdata($name,$domain);
                   6156:     } elsif ($type eq 'user') {
                   6157: 	$result=&get_userresdata($name,$domain);
                   6158:     }
                   6159:     if (!ref($result)) { return $result; }    
1.251     albertel 6160:     foreach my $item (@which) {
1.417     albertel 6161: 	if (defined($result->{$item})) {
                   6162: 	    return $result->{$item};
1.251     albertel 6163: 	}
1.250     albertel 6164:     }
1.291     albertel 6165:     return undef;
1.200     www      6166: }
                   6167: 
1.379     matthew  6168: #
                   6169: # EXT resource caching routines
                   6170: #
                   6171: 
                   6172: sub clear_EXT_cache_status {
1.383     albertel 6173:     &delenv('cache.EXT.');
1.379     matthew  6174: }
                   6175: 
                   6176: sub EXT_cache_status {
                   6177:     my ($target_domain,$target_user) = @_;
1.383     albertel 6178:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 6179:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  6180:         # We know already the user has no data
                   6181:         return 1;
                   6182:     } else {
                   6183:         return 0;
                   6184:     }
                   6185: }
                   6186: 
                   6187: sub EXT_cache_set {
                   6188:     my ($target_domain,$target_user) = @_;
1.383     albertel 6189:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633     albertel 6190:     #&appenv($cachename => time);
1.379     matthew  6191: }
                   6192: 
1.28      www      6193: # --------------------------------------------------------- Value of a Variable
1.58      www      6194: sub EXT {
1.715     albertel 6195: 
1.395     albertel 6196:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      6197:     unless ($varname) { return ''; }
1.218     albertel 6198:     #get real user name/domain, courseid and symb
                   6199:     my $courseid;
1.359     albertel 6200:     my $publicuser;
1.427     www      6201:     if ($symbparm) {
                   6202: 	$symbparm=&get_symb_from_alias($symbparm);
                   6203:     }
1.218     albertel 6204:     if (!($uname && $udom)) {
1.790     albertel 6205:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 6206:       if (!$symbparm) {	$symbparm=$cursymb; }
                   6207:     } else {
1.620     albertel 6208: 	$courseid=$env{'request.course.id'};
1.218     albertel 6209:     }
1.48      www      6210:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   6211:     my $rest;
1.320     albertel 6212:     if (defined($therest[0])) {
1.48      www      6213:        $rest=join('.',@therest);
                   6214:     } else {
                   6215:        $rest='';
                   6216:     }
1.320     albertel 6217: 
1.57      www      6218:     my $qualifierrest=$qualifier;
                   6219:     if ($rest) { $qualifierrest.='.'.$rest; }
                   6220:     my $spacequalifierrest=$space;
                   6221:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      6222:     if ($realm eq 'user') {
1.48      www      6223: # --------------------------------------------------------------- user.resource
                   6224: 	if ($space eq 'resource') {
1.651     albertel 6225: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   6226: 		  || defined($Apache::lonhomework::parsing_a_task))
                   6227: 		 &&
1.744     albertel 6228: 		 ($symbparm eq &symbread()) ) {	
                   6229: 		# if we are in the middle of processing the resource the
                   6230: 		# get the value we are planning on committing
                   6231:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   6232:                     return $Apache::lonhomework::results{$qualifierrest};
                   6233:                 } else {
                   6234:                     return $Apache::lonhomework::history{$qualifierrest};
                   6235:                 }
1.335     albertel 6236: 	    } else {
1.359     albertel 6237: 		my %restored;
1.620     albertel 6238: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 6239: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   6240: 		} else {
                   6241: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   6242: 		}
1.335     albertel 6243: 		return $restored{$qualifierrest};
                   6244: 	    }
1.48      www      6245: # ----------------------------------------------------------------- user.access
                   6246:         } elsif ($space eq 'access') {
1.218     albertel 6247: 	    # FIXME - not supporting calls for a specific user
1.48      www      6248:             return &allowed($qualifier,$rest);
                   6249: # ------------------------------------------ user.preferences, user.environment
                   6250:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 6251: 	    if (($uname eq $env{'user.name'}) &&
                   6252: 		($udom eq $env{'user.domain'})) {
                   6253: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 6254: 	    } else {
1.359     albertel 6255: 		my %returnhash;
                   6256: 		if (!$publicuser) {
                   6257: 		    %returnhash=&userenvironment($udom,$uname,
                   6258: 						 $qualifierrest);
                   6259: 		}
1.218     albertel 6260: 		return $returnhash{$qualifierrest};
                   6261: 	    }
1.48      www      6262: # ----------------------------------------------------------------- user.course
                   6263:         } elsif ($space eq 'course') {
1.218     albertel 6264: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6265:             return $env{join('.',('request.course',$qualifier))};
1.48      www      6266: # ------------------------------------------------------------------- user.role
                   6267:         } elsif ($space eq 'role') {
1.218     albertel 6268: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6269:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      6270:             if ($qualifier eq 'value') {
                   6271: 		return $role;
                   6272:             } elsif ($qualifier eq 'extent') {
                   6273:                 return $where;
                   6274:             }
                   6275: # ----------------------------------------------------------------- user.domain
                   6276:         } elsif ($space eq 'domain') {
1.218     albertel 6277:             return $udom;
1.48      www      6278: # ------------------------------------------------------------------- user.name
                   6279:         } elsif ($space eq 'name') {
1.218     albertel 6280:             return $uname;
1.48      www      6281: # ---------------------------------------------------- Any other user namespace
1.29      www      6282:         } else {
1.359     albertel 6283: 	    my %reply;
                   6284: 	    if (!$publicuser) {
                   6285: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   6286: 	    }
                   6287: 	    return $reply{$qualifierrest};
1.48      www      6288:         }
1.236     www      6289:     } elsif ($realm eq 'query') {
                   6290: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 6291:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   6292: 						[$spacequalifierrest]);
1.620     albertel 6293: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      6294:    } elsif ($realm eq 'request') {
1.48      www      6295: # ------------------------------------------------------------- request.browser
                   6296:         if ($space eq 'browser') {
1.430     www      6297: 	    if ($qualifier eq 'textremote') {
1.676     albertel 6298: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      6299: 		    return 1;
                   6300: 		} else {
                   6301: 		    return 0;
                   6302: 		}
                   6303: 	    } else {
1.620     albertel 6304: 		return $env{'browser.'.$qualifier};
1.430     www      6305: 	    }
1.57      www      6306: # ------------------------------------------------------------ request.filename
                   6307:         } else {
1.620     albertel 6308:             return $env{'request.'.$spacequalifierrest};
1.29      www      6309:         }
1.28      www      6310:     } elsif ($realm eq 'course') {
1.48      www      6311: # ---------------------------------------------------------- course.description
1.620     albertel 6312:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      6313:     } elsif ($realm eq 'resource') {
1.165     www      6314: 
1.620     albertel 6315: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 6316: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   6317: 	}
1.693     albertel 6318: 
                   6319: 	if ($space eq 'title') {
                   6320: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   6321: 	    return &gettitle($symbparm);
                   6322: 	}
                   6323: 	
                   6324: 	if ($space eq 'map') {
                   6325: 	    my ($map) = &decode_symb($symbparm);
                   6326: 	    return &symbread($map);
                   6327: 	}
1.905     albertel 6328: 	if ($space eq 'filename') {
                   6329: 	    if ($symbparm) {
                   6330: 		return &clutter((&decode_symb($symbparm))[2]);
                   6331: 	    }
                   6332: 	    return &hreflocation('',$env{'request.filename'});
                   6333: 	}
1.693     albertel 6334: 
                   6335: 	my ($section, $group, @groups);
1.593     albertel 6336: 	my ($courselevelm,$courselevel);
1.539     albertel 6337: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6338: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      6339: 
1.218     albertel 6340: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      6341: 
1.60      www      6342: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 6343: 	    my $symbp=$symbparm;
1.735     albertel 6344: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 6345: 
                   6346: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   6347: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   6348: 
1.620     albertel 6349: 	    if (($env{'user.name'} eq $uname) &&
                   6350: 		($env{'user.domain'} eq $udom)) {
                   6351: 		$section=$env{'request.course.sec'};
1.733     raeburn  6352:                 @groups = split(/:/,$env{'request.course.groups'});  
                   6353:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 6354: 	    } else {
1.539     albertel 6355: 		if (! defined($usection)) {
1.551     albertel 6356: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 6357: 		} else {
                   6358: 		    $section = $usection;
                   6359: 		}
1.733     raeburn  6360:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 6361: 	    }
                   6362: 
                   6363: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   6364: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   6365: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   6366: 
1.593     albertel 6367: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 6368: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 6369: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      6370: 
1.60      www      6371: # ----------------------------------------------------------- first, check user
1.624     albertel 6372: 
                   6373: 	    my $userreply=&resdata($uname,$udom,'user',
                   6374: 				       ($courselevelr,$courselevelm,
                   6375: 					$courselevel));
                   6376: 	    if (defined($userreply)) { return $userreply; }
1.95      www      6377: 
1.594     albertel 6378: # ------------------------------------------------ second, check some of course
1.684     raeburn  6379:             my $coursereply;
1.691     raeburn  6380:             if (@groups > 0) {
                   6381:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   6382:                                        $mapparm,$spacequalifierrest);
1.684     raeburn  6383:                 if (defined($coursereply)) { return $coursereply; }
                   6384:             }
1.96      www      6385: 
1.684     raeburn  6386: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624     albertel 6387: 				     $env{'course.'.$courseid.'.domain'},
                   6388: 				     'course',
                   6389: 				     ($seclevelr,$seclevelm,$seclevel,
                   6390: 				      $courselevelr));
1.287     albertel 6391: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      6392: 
1.60      www      6393: # ------------------------------------------------------ third, check map parms
1.218     albertel 6394: 	    my %parmhash=();
                   6395: 	    my $thisparm='';
                   6396: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 6397: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 6398: 		    &GDBM_READER(),0640)) {
1.218     albertel 6399: 		$thisparm=$parmhash{$symbparm};
                   6400: 		untie(%parmhash);
                   6401: 	    }
                   6402: 	    if ($thisparm) { return $thisparm; }
                   6403: 	}
1.594     albertel 6404: # ------------------------------------------ fourth, look in resource metadata
1.71      www      6405: 
1.218     albertel 6406: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 6407: 	my $filename;
                   6408: 	if (!$symbparm) { $symbparm=&symbread(); }
                   6409: 	if ($symbparm) {
1.409     www      6410: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 6411: 	} else {
1.620     albertel 6412: 	    $filename=$env{'request.filename'};
1.282     albertel 6413: 	}
                   6414: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 6415: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 6416: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 6417: 	if (defined($metadata)) { return $metadata; }
1.142     www      6418: 
1.594     albertel 6419: # ---------------------------------------------- fourth, look in rest pf course
1.593     albertel 6420: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6421: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 6422: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   6423: 				     $env{'course.'.$courseid.'.domain'},
                   6424: 				     'course',
                   6425: 				     ($courselevelm,$courselevel));
1.593     albertel 6426: 	    if (defined($coursereply)) { return $coursereply; }
                   6427: 	}
1.145     www      6428: # ------------------------------------------------------------------ Cascade up
1.218     albertel 6429: 	unless ($space eq '0') {
1.336     albertel 6430: 	    my @parts=split(/_/,$space);
                   6431: 	    my $id=pop(@parts);
                   6432: 	    my $part=join('_',@parts);
                   6433: 	    if ($part eq '') { $part='0'; }
                   6434: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 6435: 				 $symbparm,$udom,$uname,$section,1);
1.337     albertel 6436: 	    if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 6437: 	}
1.395     albertel 6438: 	if ($recurse) { return undef; }
                   6439: 	my $pack_def=&packages_tab_default($filename,$varname);
                   6440: 	if (defined($pack_def)) { return $pack_def; }
1.71      www      6441: 
1.48      www      6442: # ---------------------------------------------------- Any other user namespace
                   6443:     } elsif ($realm eq 'environment') {
                   6444: # ----------------------------------------------------------------- environment
1.620     albertel 6445: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   6446: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 6447: 	} else {
1.770     albertel 6448: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   6449: 		return '';
                   6450: 	    }
1.219     albertel 6451: 	    my %returnhash=&userenvironment($udom,$uname,
                   6452: 					    $spacequalifierrest);
                   6453: 	    return $returnhash{$spacequalifierrest};
                   6454: 	}
1.28      www      6455:     } elsif ($realm eq 'system') {
1.48      www      6456: # ----------------------------------------------------------------- system.time
                   6457: 	if ($space eq 'time') {
                   6458: 	    return time;
                   6459:         }
1.696     albertel 6460:     } elsif ($realm eq 'server') {
                   6461: # ----------------------------------------------------------------- system.time
                   6462: 	if ($space eq 'name') {
                   6463: 	    return $ENV{'SERVER_NAME'};
                   6464:         }
1.28      www      6465:     }
1.48      www      6466:     return '';
1.61      www      6467: }
                   6468: 
1.691     raeburn  6469: sub check_group_parms {
                   6470:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   6471:     my @groupitems = ();
                   6472:     my $resultitem;
                   6473:     my @levels = ($symbparm,$mapparm,$what);
                   6474:     foreach my $group (@{$groups}) {
                   6475:         foreach my $level (@levels) {
                   6476:              my $item = $courseid.'.['.$group.'].'.$level;
                   6477:              push(@groupitems,$item);
                   6478:         }
                   6479:     }
                   6480:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   6481:                             $env{'course.'.$courseid.'.domain'},
                   6482:                                      'course',@groupitems);
                   6483:     return $coursereply;
                   6484: }
                   6485: 
                   6486: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  6487:     my ($courseid,@groups) = @_;
                   6488:     @groups = sort(@groups);
1.691     raeburn  6489:     return @groups;
                   6490: }
                   6491: 
1.395     albertel 6492: sub packages_tab_default {
                   6493:     my ($uri,$varname)=@_;
                   6494:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 6495: 
                   6496:     my (@extension,@specifics,$do_default);
                   6497:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 6498: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 6499: 	if ($pack_type eq 'default') {
                   6500: 	    $do_default=1;
                   6501: 	} elsif ($pack_type eq 'extension') {
                   6502: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 6503: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 6504: 	    # only look at packages defaults for packages that this id is
1.738     albertel 6505: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   6506: 	}
                   6507:     }
                   6508:     # first look for a package that matches the requested part id
                   6509:     foreach my $package (@specifics) {
                   6510: 	my (undef,$pack_type,$pack_part)=@{$package};
                   6511: 	next if ($pack_part ne $part);
                   6512: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6513: 	    return $packagetab{"$pack_type&$name&default"};
                   6514: 	}
                   6515:     }
                   6516:     # look for any possible matching non extension_ package
                   6517:     foreach my $package (@specifics) {
                   6518: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 6519: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6520: 	    return $packagetab{"$pack_type&$name&default"};
                   6521: 	}
1.585     albertel 6522: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 6523: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   6524: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 6525: 	}
                   6526:     }
1.738     albertel 6527:     # look for any posible extension_ match
                   6528:     foreach my $package (@extension) {
                   6529: 	my ($package,$pack_type)=@{$package};
                   6530: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6531: 	    return $packagetab{"$pack_type&$name&default"};
                   6532: 	}
                   6533: 	if (defined($packagetab{$package."&$name&default"})) {
                   6534: 	    return $packagetab{$package."&$name&default"};
                   6535: 	}
                   6536:     }
                   6537:     # look for a global default setting
                   6538:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   6539: 	return $packagetab{"default&$name&default"};
                   6540:     }
1.395     albertel 6541:     return undef;
                   6542: }
                   6543: 
1.334     albertel 6544: sub add_prefix_and_part {
                   6545:     my ($prefix,$part)=@_;
                   6546:     my $keyroot;
                   6547:     if (defined($prefix) && $prefix !~ /^__/) {
                   6548: 	# prefix that has a part already
                   6549: 	$keyroot=$prefix;
                   6550:     } elsif (defined($prefix)) {
                   6551: 	# prefix that is missing a part
                   6552: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   6553:     } else {
                   6554: 	# no prefix at all
                   6555: 	if (defined($part)) { $keyroot='_'.$part; }
                   6556:     }
                   6557:     return $keyroot;
                   6558: }
                   6559: 
1.71      www      6560: # ---------------------------------------------------------------- Get metadata
                   6561: 
1.599     albertel 6562: my %metaentry;
1.71      www      6563: sub metadata {
1.176     www      6564:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      6565:     $uri=&declutter($uri);
1.288     albertel 6566:     # if it is a non metadata possible uri return quickly
1.529     albertel 6567:     if (($uri eq '') || 
                   6568: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 6569: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423     albertel 6570:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.807     albertel 6571: 	($uri =~ m|home/$match_username/public_html/|)) {
1.468     albertel 6572: 	return undef;
1.288     albertel 6573:     }
1.73      www      6574:     my $filename=$uri;
                   6575:     $uri=~s/\.meta$//;
1.172     www      6576: #
                   6577: # Is the metadata already cached?
1.177     www      6578: # Look at timestamp of caching
1.172     www      6579: # Everything is cached by the main uri, libraries are never directly cached
                   6580: #
1.428     albertel 6581:     if (!defined($liburi)) {
1.599     albertel 6582: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 6583: 	if (defined($cached)) { return $result->{':'.$what}; }
                   6584:     }
                   6585:     {
1.172     www      6586: #
                   6587: # Is this a recursive call for a library?
                   6588: #
1.599     albertel 6589: #	if (! exists($metacache{$uri})) {
                   6590: #	    $metacache{$uri}={};
                   6591: #	}
1.171     www      6592:         if ($liburi) {
                   6593: 	    $liburi=&declutter($liburi);
                   6594:             $filename=$liburi;
1.401     bowersj2 6595:         } else {
1.599     albertel 6596: 	    &devalidate_cache_new('meta',$uri);
                   6597: 	    undef(%metaentry);
1.401     bowersj2 6598: 	}
1.140     www      6599:         my %metathesekeys=();
1.73      www      6600:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 6601: 	my $metastring;
1.768     albertel 6602: 	if ($uri !~ m -^(editupload)/-) {
1.543     albertel 6603: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 6604: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 6605: 	    $metastring=&getfile($file);
1.489     albertel 6606: 	}
1.208     albertel 6607:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      6608:         my $token;
1.140     www      6609:         undef %metathesekeys;
1.71      www      6610:         while ($token=$parser->get_token) {
1.339     albertel 6611: 	    if ($token->[0] eq 'S') {
                   6612: 		if (defined($token->[2]->{'package'})) {
1.172     www      6613: #
                   6614: # This is a package - get package info
                   6615: #
1.339     albertel 6616: 		    my $package=$token->[2]->{'package'};
                   6617: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6618: 		    if (defined($token->[2]->{'id'})) { 
                   6619: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   6620: 		    }
1.599     albertel 6621: 		    if ($metaentry{':packages'}) {
                   6622: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 6623: 		    } else {
1.599     albertel 6624: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 6625: 		    }
1.736     albertel 6626: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 6627: 			my $part=$keyroot;
                   6628: 			$part=~s/^\_//;
1.736     albertel 6629: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   6630: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   6631: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 6632: 			    # ignore package.tab specified default values
                   6633:                             # here &package_tab_default() will fetch those
                   6634: 			    if ($subp eq 'default') { next; }
1.736     albertel 6635: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 6636: 			    my $unikey;
                   6637: 			    if ($pack =~ /_0$/) {
                   6638: 				$unikey='parameter_0_'.$name;
                   6639: 				$part=0;
                   6640: 			    } else {
                   6641: 				$unikey='parameter'.$keyroot.'_'.$name;
                   6642: 			    }
1.339     albertel 6643: 			    if ($subp eq 'display') {
                   6644: 				$value.=' [Part: '.$part.']';
                   6645: 			    }
1.599     albertel 6646: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 6647: 			    $metathesekeys{$unikey}=1;
1.599     albertel 6648: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6649: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 6650: 			    }
1.599     albertel 6651: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   6652: 				$metaentry{':'.$unikey}=
                   6653: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 6654: 			    }
1.339     albertel 6655: 			}
                   6656: 		    }
                   6657: 		} else {
1.172     www      6658: #
                   6659: # This is not a package - some other kind of start tag
1.339     albertel 6660: #
                   6661: 		    my $entry=$token->[1];
                   6662: 		    my $unikey;
                   6663: 		    if ($entry eq 'import') {
                   6664: 			$unikey='';
                   6665: 		    } else {
                   6666: 			$unikey=$entry;
                   6667: 		    }
                   6668: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6669: 
                   6670: 		    if (defined($token->[2]->{'id'})) { 
                   6671: 			$unikey.='_'.$token->[2]->{'id'}; 
                   6672: 		    }
1.175     www      6673: 
1.339     albertel 6674: 		    if ($entry eq 'import') {
1.175     www      6675: #
                   6676: # Importing a library here
1.339     albertel 6677: #
                   6678: 			if ($depthcount<20) {
                   6679: 			    my $location=$parser->get_text('/import');
                   6680: 			    my $dir=$filename;
                   6681: 			    $dir=~s|[^/]*$||;
                   6682: 			    $location=&filelocation($dir,$location);
1.736     albertel 6683: 			    my $metadata = 
                   6684: 				&metadata($uri,'keys', $location,$unikey,
                   6685: 					  $depthcount+1);
                   6686: 			    foreach my $meta (split(',',$metadata)) {
                   6687: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   6688: 				$metathesekeys{$meta}=1;
1.339     albertel 6689: 			    }
                   6690: 			}
                   6691: 		    } else { 
                   6692: 			
                   6693: 			if (defined($token->[2]->{'name'})) { 
                   6694: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   6695: 			}
                   6696: 			$metathesekeys{$unikey}=1;
1.736     albertel 6697: 			foreach my $param (@{$token->[3]}) {
                   6698: 			    $metaentry{':'.$unikey.'.'.$param} =
                   6699: 				$token->[2]->{$param};
1.339     albertel 6700: 			}
                   6701: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 6702: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 6703: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   6704: 		 # only ws inside the tag, and not in default, so use default
                   6705: 		 # as value
1.599     albertel 6706: 			    $metaentry{':'.$unikey}=$default;
1.908     albertel 6707: 			} elsif ( $internaltext =~ /\S/ ) {
                   6708: 		  # something interesting inside the tag
                   6709: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 6710: 			} else {
1.908     albertel 6711: 		  # no interesting values, don't set a default
1.339     albertel 6712: 			}
1.172     www      6713: # end of not-a-package not-a-library import
1.339     albertel 6714: 		    }
1.172     www      6715: # end of not-a-package start tag
1.339     albertel 6716: 		}
1.172     www      6717: # the next is the end of "start tag"
1.339     albertel 6718: 	    }
                   6719: 	}
1.483     albertel 6720: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 6721: 	$extension = lc($extension);
                   6722: 	if ($extension eq 'htm') { $extension='html'; }
                   6723: 
1.737     albertel 6724: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 6725: 	    #no specific packages #how's our extension
                   6726: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 6727: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 6728: 					 \%metathesekeys);
                   6729: 	}
1.883     albertel 6730: 
                   6731: 	if (!exists($metaentry{':packages'})
                   6732: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 6733: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 6734: 		#no specific packages well let's get default then
                   6735: 		if ($key!~/^default&/) { next; }
1.488     albertel 6736: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 6737: 					     \%metathesekeys);
                   6738: 	    }
                   6739: 	}
1.338     www      6740: # are there custom rights to evaluate
1.599     albertel 6741: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 6742: 
1.338     www      6743:     #
                   6744:     # Importing a rights file here
1.339     albertel 6745:     #
                   6746: 	    unless ($depthcount) {
1.599     albertel 6747: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 6748: 		my $dir=$filename;
                   6749: 		$dir=~s|[^/]*$||;
                   6750: 		$location=&filelocation($dir,$location);
1.736     albertel 6751: 		my $rights_metadata =
                   6752: 		    &metadata($uri,'keys',$location,'_rights',
                   6753: 			      $depthcount+1);
                   6754: 		foreach my $rights (split(',',$rights_metadata)) {
                   6755: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   6756: 		    $metathesekeys{$rights}=1;
1.339     albertel 6757: 		}
                   6758: 	    }
                   6759: 	}
1.737     albertel 6760: 	# uniqifiy package listing
                   6761: 	my %seen;
                   6762: 	my @uniq_packages =
                   6763: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   6764: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   6765: 
                   6766: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 6767: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   6768: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699     albertel 6769: 	&do_cache_new('meta',$uri,\%metaentry,60*60);
1.177     www      6770: # this is the end of "was not already recently cached
1.71      www      6771:     }
1.599     albertel 6772:     return $metaentry{':'.$what};
1.261     albertel 6773: }
                   6774: 
1.488     albertel 6775: sub metadata_create_package_def {
1.483     albertel 6776:     my ($uri,$key,$package,$metathesekeys)=@_;
                   6777:     my ($pack,$name,$subp)=split(/\&/,$key);
                   6778:     if ($subp eq 'default') { next; }
                   6779:     
1.599     albertel 6780:     if (defined($metaentry{':packages'})) {
                   6781: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 6782:     } else {
1.599     albertel 6783: 	$metaentry{':packages'}=$package;
1.483     albertel 6784:     }
                   6785:     my $value=$packagetab{$key};
                   6786:     my $unikey;
                   6787:     $unikey='parameter_0_'.$name;
1.599     albertel 6788:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 6789:     $$metathesekeys{$unikey}=1;
1.599     albertel 6790:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6791: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 6792:     }
1.599     albertel 6793:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   6794: 	$metaentry{':'.$unikey}=
                   6795: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 6796:     }
                   6797: }
                   6798: 
1.261     albertel 6799: sub metadata_generate_part0 {
                   6800:     my ($metadata,$metacache,$uri) = @_;
                   6801:     my %allnames;
1.737     albertel 6802:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 6803: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 6804: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   6805: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 6806: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 6807: 	    $allnames{$name}=$part;
                   6808: 	  }
                   6809: 	}
                   6810:     }
                   6811:     foreach my $name (keys(%allnames)) {
                   6812:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 6813:       my $key=":parameter_0_$name";
1.261     albertel 6814:       $$metacache{"$key.part"}='0';
                   6815:       $$metacache{"$key.name"}=$name;
1.428     albertel 6816:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 6817: 					   $allnames{$name}.'_'.$name.
                   6818: 					   '.type'};
1.428     albertel 6819:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 6820: 			     '.display'};
1.644     www      6821:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 6822:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 6823:       $$metacache{"$key.display"}=$olddis;
                   6824:     }
1.71      www      6825: }
                   6826: 
1.764     albertel 6827: # ------------------------------------------------------ Devalidate title cache
                   6828: 
                   6829: sub devalidate_title_cache {
                   6830:     my ($url)=@_;
                   6831:     if (!$env{'request.course.id'}) { return; }
                   6832:     my $symb=&symbread($url);
                   6833:     if (!$symb) { return; }
                   6834:     my $key=$env{'request.course.id'}."\0".$symb;
                   6835:     &devalidate_cache_new('title',$key);
                   6836: }
                   6837: 
1.301     www      6838: # ------------------------------------------------- Get the title of a resource
                   6839: 
                   6840: sub gettitle {
                   6841:     my $urlsymb=shift;
                   6842:     my $symb=&symbread($urlsymb);
1.534     albertel 6843:     if ($symb) {
1.620     albertel 6844: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 6845: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 6846: 	if (defined($cached)) { 
                   6847: 	    return $result;
                   6848: 	}
1.534     albertel 6849: 	my ($map,$resid,$url)=&decode_symb($symb);
                   6850: 	my $title='';
1.907     albertel 6851: 	if (!$map && $resid == 0 && $url =~/default\.sequence$/) {
                   6852: 	    $title = $env{'course.'.$env{'request.course.id'}.'.description'};
                   6853: 	} else {
                   6854: 	    if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   6855: 		    &GDBM_READER(),0640)) {
                   6856: 		my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   6857: 		$title=$bighash{'title_'.$mapid.'.'.$resid};
                   6858: 		untie(%bighash);
                   6859: 	    }
1.534     albertel 6860: 	}
                   6861: 	$title=~s/\&colon\;/\:/gs;
                   6862: 	if ($title) {
1.599     albertel 6863: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 6864: 	}
                   6865: 	$urlsymb=$url;
                   6866:     }
                   6867:     my $title=&metadata($urlsymb,'title');
                   6868:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   6869:     return $title;
1.301     www      6870: }
1.613     albertel 6871: 
1.614     albertel 6872: sub get_slot {
                   6873:     my ($which,$cnum,$cdom)=@_;
                   6874:     if (!$cnum || !$cdom) {
1.790     albertel 6875: 	(undef,my $courseid)=&whichuser();
1.620     albertel 6876: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   6877: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 6878:     }
1.703     albertel 6879:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   6880:     my %slotinfo;
                   6881:     if (exists($remembered{$key})) {
                   6882: 	$slotinfo{$which} = $remembered{$key};
                   6883:     } else {
                   6884: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   6885: 	&Apache::lonhomework::showhash(%slotinfo);
                   6886: 	my ($tmp)=keys(%slotinfo);
                   6887: 	if ($tmp=~/^error:/) { return (); }
                   6888: 	$remembered{$key} = $slotinfo{$which};
                   6889:     }
1.616     albertel 6890:     if (ref($slotinfo{$which}) eq 'HASH') {
                   6891: 	return %{$slotinfo{$which}};
                   6892:     }
                   6893:     return $slotinfo{$which};
1.614     albertel 6894: }
1.31      www      6895: # ------------------------------------------------- Update symbolic store links
                   6896: 
                   6897: sub symblist {
                   6898:     my ($mapname,%newhash)=@_;
1.438     www      6899:     $mapname=&deversion(&declutter($mapname));
1.31      www      6900:     my %hash;
1.620     albertel 6901:     if (($env{'request.course.fn'}) && (%newhash)) {
                   6902:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6903:                       &GDBM_WRCREAT(),0640)) {
1.711     albertel 6904: 	    foreach my $url (keys %newhash) {
                   6905: 		next if ($url eq 'last_known'
                   6906: 			 && $env{'form.no_update_last_known'});
                   6907: 		$hash{declutter($url)}=&encode_symb($mapname,
                   6908: 						    $newhash{$url}->[1],
                   6909: 						    $newhash{$url}->[0]);
1.191     harris41 6910:             }
1.31      www      6911:             if (untie(%hash)) {
                   6912: 		return 'ok';
                   6913:             }
                   6914:         }
                   6915:     }
                   6916:     return 'error';
1.212     www      6917: }
                   6918: 
                   6919: # --------------------------------------------------------------- Verify a symb
                   6920: 
                   6921: sub symbverify {
1.510     www      6922:     my ($symb,$thisurl)=@_;
                   6923:     my $thisfn=$thisurl;
1.439     www      6924:     $thisfn=&declutter($thisfn);
1.215     www      6925: # direct jump to resource in page or to a sequence - will construct own symbs
                   6926:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   6927: # check URL part
1.409     www      6928:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      6929: 
1.431     www      6930:     unless ($url eq $thisfn) { return 0; }
1.213     www      6931: 
1.216     www      6932:     $symb=&symbclean($symb);
1.510     www      6933:     $thisurl=&deversion($thisurl);
1.439     www      6934:     $thisfn=&deversion($thisfn);
1.213     www      6935: 
                   6936:     my %bighash;
                   6937:     my $okay=0;
1.431     www      6938: 
1.620     albertel 6939:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6940:                             &GDBM_READER(),0640)) {
1.510     www      6941:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      6942:         unless ($ids) { 
1.510     www      6943:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      6944:         }
                   6945:         if ($ids) {
                   6946: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 6947: 	    foreach my $id (split(/\,/,$ids)) {
                   6948: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      6949:                if (
                   6950:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   6951:    eq $symb) { 
1.620     albertel 6952: 		   if (($env{'request.role.adv'}) ||
1.800     albertel 6953: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582     albertel 6954: 		       $okay=1; 
                   6955: 		   }
                   6956: 	       }
1.216     www      6957: 	   }
                   6958:         }
1.213     www      6959: 	untie(%bighash);
                   6960:     }
                   6961:     return $okay;
1.31      www      6962: }
                   6963: 
1.210     www      6964: # --------------------------------------------------------------- Clean-up symb
                   6965: 
                   6966: sub symbclean {
                   6967:     my $symb=shift;
1.568     albertel 6968:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      6969: # remove version from map
                   6970:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      6971: 
1.210     www      6972: # remove version from URL
                   6973:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      6974: 
1.507     www      6975: # remove wrapper
                   6976: 
1.510     www      6977:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 6978:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      6979:     return $symb;
1.409     www      6980: }
                   6981: 
                   6982: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 6983: 
                   6984: sub encode_symb {
                   6985:     my ($map,$resid,$url)=@_;
                   6986:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   6987: }
1.409     www      6988: 
                   6989: sub decode_symb {
1.568     albertel 6990:     my $symb=shift;
                   6991:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   6992:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      6993:     return (&fixversion($map),$resid,&fixversion($url));
                   6994: }
                   6995: 
                   6996: sub fixversion {
                   6997:     my $fn=shift;
1.609     banghart 6998:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      6999:     my %bighash;
                   7000:     my $uri=&clutter($fn);
1.620     albertel 7001:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      7002: # is this cached?
1.599     albertel 7003:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      7004:     if (defined($cached)) { return $result; }
                   7005: # unfortunately not cached, or expired
1.620     albertel 7006:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      7007: 	    &GDBM_READER(),0640)) {
                   7008:  	if ($bighash{'version_'.$uri}) {
                   7009:  	    my $version=$bighash{'version_'.$uri};
1.444     www      7010:  	    unless (($version eq 'mostrecent') || 
                   7011: 		    ($version==&getversion($uri))) {
1.440     www      7012:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   7013:  	    }
                   7014:  	}
                   7015:  	untie %bighash;
1.413     www      7016:     }
1.599     albertel 7017:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      7018: }
                   7019: 
                   7020: sub deversion {
                   7021:     my $url=shift;
                   7022:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   7023:     return $url;
1.210     www      7024: }
                   7025: 
1.31      www      7026: # ------------------------------------------------------ Return symb list entry
                   7027: 
                   7028: sub symbread {
1.249     www      7029:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 7030:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 7031:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      7032: # no filename provided? try from environment
1.44      www      7033:     unless ($thisfn) {
1.620     albertel 7034:         if ($env{'request.symb'}) {
                   7035: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 7036: 	}
1.620     albertel 7037: 	$thisfn=$env{'request.filename'};
1.44      www      7038:     }
1.569     albertel 7039:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      7040: # is that filename actually a symb? Verify, clean, and return
                   7041:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 7042: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 7043: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 7044: 	}
1.242     www      7045:     }
1.44      www      7046:     $thisfn=declutter($thisfn);
1.31      www      7047:     my %hash;
1.37      www      7048:     my %bighash;
                   7049:     my $syval='';
1.620     albertel 7050:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  7051:         my $targetfn = $thisfn;
1.609     banghart 7052:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  7053:             $targetfn = 'adm/wrapper/'.$thisfn;
                   7054:         }
1.687     albertel 7055: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   7056: 	    $targetfn=$1;
                   7057: 	}
1.620     albertel 7058:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 7059:                       &GDBM_READER(),0640)) {
1.481     raeburn  7060: 	    $syval=$hash{$targetfn};
1.37      www      7061:             untie(%hash);
                   7062:         }
                   7063: # ---------------------------------------------------------- There was an entry
                   7064:         if ($syval) {
1.601     albertel 7065: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 7066: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601     albertel 7067: 		    #&appenv('request.ambiguous' => $thisfn);
1.620     albertel 7068: 		    #return $env{$cache_str}='';
1.601     albertel 7069: 		#}    
                   7070: 		#$syval.=$1;
                   7071: 	    #}
1.37      www      7072:         } else {
                   7073: # ------------------------------------------------------- Was not in symb table
1.620     albertel 7074:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 7075:                             &GDBM_READER(),0640)) {
1.37      www      7076: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      7077:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      7078:               unless ($ids) { 
                   7079:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      7080:               }
                   7081:               unless ($ids) {
                   7082: # alias?
                   7083: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      7084:               }
1.37      www      7085:               if ($ids) {
                   7086: # ------------------------------------------------------------------- Has ID(s)
                   7087:                  my @possibilities=split(/\,/,$ids);
1.39      www      7088:                  if ($#possibilities==0) {
                   7089: # ----------------------------------------------- There is only one possibility
1.37      www      7090: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 7091: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7092: 						    $resid,$thisfn);
1.249     www      7093:                  } elsif (!$donotrecurse) {
1.39      www      7094: # ------------------------------------------ There is more than one possibility
                   7095:                      my $realpossible=0;
1.800     albertel 7096:                      foreach my $id (@possibilities) {
                   7097: 			 my $file=$bighash{'src_'.$id};
1.39      www      7098:                          if (&allowed('bre',$file)) {
1.800     albertel 7099:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      7100:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   7101: 				$realpossible++;
1.626     albertel 7102:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7103: 						    $resid,$thisfn);
1.39      www      7104:                             }
                   7105: 			 }
1.191     harris41 7106:                      }
1.39      www      7107: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      7108:                  } else {
                   7109:                      $syval='';
1.37      www      7110:                  }
                   7111: 	      }
                   7112:               untie(%bighash)
1.481     raeburn  7113:            }
1.31      www      7114:         }
1.62      www      7115:         if ($syval) {
1.620     albertel 7116: 	    return $env{$cache_str}=$syval;
1.62      www      7117:         }
1.31      www      7118:     }
1.44      www      7119:     &appenv('request.ambiguous' => $thisfn);
1.620     albertel 7120:     return $env{$cache_str}='';
1.31      www      7121: }
                   7122: 
                   7123: # ---------------------------------------------------------- Return random seed
                   7124: 
1.32      www      7125: sub numval {
                   7126:     my $txt=shift;
                   7127:     $txt=~tr/A-J/0-9/;
                   7128:     $txt=~tr/a-j/0-9/;
                   7129:     $txt=~tr/K-T/0-9/;
                   7130:     $txt=~tr/k-t/0-9/;
                   7131:     $txt=~tr/U-Z/0-5/;
                   7132:     $txt=~tr/u-z/0-5/;
                   7133:     $txt=~s/\D//g;
1.564     albertel 7134:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      7135:     return int($txt);
1.368     albertel 7136: }
                   7137: 
1.484     albertel 7138: sub numval2 {
                   7139:     my $txt=shift;
                   7140:     $txt=~tr/A-J/0-9/;
                   7141:     $txt=~tr/a-j/0-9/;
                   7142:     $txt=~tr/K-T/0-9/;
                   7143:     $txt=~tr/k-t/0-9/;
                   7144:     $txt=~tr/U-Z/0-5/;
                   7145:     $txt=~tr/u-z/0-5/;
                   7146:     $txt=~s/\D//g;
                   7147:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7148:     my $total;
                   7149:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 7150:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 7151:     return int($total);
                   7152: }
                   7153: 
1.575     albertel 7154: sub numval3 {
                   7155:     use integer;
                   7156:     my $txt=shift;
                   7157:     $txt=~tr/A-J/0-9/;
                   7158:     $txt=~tr/a-j/0-9/;
                   7159:     $txt=~tr/K-T/0-9/;
                   7160:     $txt=~tr/k-t/0-9/;
                   7161:     $txt=~tr/U-Z/0-5/;
                   7162:     $txt=~tr/u-z/0-5/;
                   7163:     $txt=~s/\D//g;
                   7164:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7165:     my $total;
                   7166:     foreach my $val (@txts) { $total+=$val; }
                   7167:     if ($_64bit) { $total=(($total<<32)>>32); }
                   7168:     return $total;
                   7169: }
                   7170: 
1.675     albertel 7171: sub digest {
                   7172:     my ($data)=@_;
                   7173:     my $digest=&Digest::MD5::md5($data);
                   7174:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   7175:     my ($e,$f);
                   7176:     {
                   7177:         use integer;
                   7178:         $e=($a+$b);
                   7179:         $f=($c+$d);
                   7180:         if ($_64bit) {
                   7181:             $e=(($e<<32)>>32);
                   7182:             $f=(($f<<32)>>32);
                   7183:         }
                   7184:     }
                   7185:     if (wantarray) {
                   7186: 	return ($e,$f);
                   7187:     } else {
                   7188: 	my $g;
                   7189: 	{
                   7190: 	    use integer;
                   7191: 	    $g=($e+$f);
                   7192: 	    if ($_64bit) {
                   7193: 		$g=(($g<<32)>>32);
                   7194: 	    }
                   7195: 	}
                   7196: 	return $g;
                   7197:     }
                   7198: }
                   7199: 
1.368     albertel 7200: sub latest_rnd_algorithm_id {
1.675     albertel 7201:     return '64bit5';
1.366     albertel 7202: }
1.32      www      7203: 
1.503     albertel 7204: sub get_rand_alg {
                   7205:     my ($courseid)=@_;
1.790     albertel 7206:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 7207:     if ($courseid) {
1.620     albertel 7208: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 7209:     }
                   7210:     return &latest_rnd_algorithm_id();
                   7211: }
                   7212: 
1.562     albertel 7213: sub validCODE {
                   7214:     my ($CODE)=@_;
                   7215:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   7216:     return 0;
                   7217: }
                   7218: 
1.491     albertel 7219: sub getCODE {
1.620     albertel 7220:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 7221:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   7222: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   7223: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 7224: 	return $Apache::lonhomework::history{'resource.CODE'};
                   7225:     }
                   7226:     return undef;
                   7227: }
                   7228: 
1.31      www      7229: sub rndseed {
1.155     albertel 7230:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 7231:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 7232:     if (!defined($symb)) {
1.366     albertel 7233: 	unless ($symb=$wsymb) { return time; }
                   7234:     }
                   7235:     if (!$courseid) { $courseid=$wcourseid; }
                   7236:     if (!$domain) { $domain=$wdomain; }
                   7237:     if (!$username) { $username=$wusername }
1.503     albertel 7238:     my $which=&get_rand_alg();
1.803     albertel 7239: 
1.491     albertel 7240:     if (defined(&getCODE())) {
1.675     albertel 7241: 	if ($which eq '64bit5') {
                   7242: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   7243: 	} elsif ($which eq '64bit4') {
1.575     albertel 7244: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   7245: 	} else {
                   7246: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   7247: 	}
1.675     albertel 7248:     } elsif ($which eq '64bit5') {
                   7249: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 7250:     } elsif ($which eq '64bit4') {
                   7251: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 7252:     } elsif ($which eq '64bit3') {
                   7253: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 7254:     } elsif ($which eq '64bit2') {
                   7255: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 7256:     } elsif ($which eq '64bit') {
                   7257: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   7258:     }
                   7259:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   7260: }
                   7261: 
                   7262: sub rndseed_32bit {
                   7263:     my ($symb,$courseid,$domain,$username)=@_;
                   7264:     {
                   7265: 	use integer;
                   7266: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   7267: 	my $symbseed=numval($symb) << 22;
                   7268: 	my $namechck=unpack("%32C*",$username) << 17;
                   7269: 	my $nameseed=numval($username) << 12;
                   7270: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   7271: 	my $courseseed=unpack("%32C*",$courseid);
                   7272: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 7273: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7274: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7275: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 7276: 	return $num;
                   7277:     }
                   7278: }
                   7279: 
                   7280: sub rndseed_64bit {
                   7281:     my ($symb,$courseid,$domain,$username)=@_;
                   7282:     {
                   7283: 	use integer;
                   7284: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   7285: 	my $symbseed=numval($symb) << 10;
                   7286: 	my $namechck=unpack("%32S*",$username);
                   7287: 	
                   7288: 	my $nameseed=numval($username) << 21;
                   7289: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   7290: 	my $courseseed=unpack("%32S*",$courseid);
                   7291: 	
                   7292: 	my $num1=$symbchck+$symbseed+$namechck;
                   7293: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7294: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7295: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7296: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 7297: 	return "$num1,$num2";
1.155     albertel 7298:     }
1.366     albertel 7299: }
                   7300: 
1.443     albertel 7301: sub rndseed_64bit2 {
                   7302:     my ($symb,$courseid,$domain,$username)=@_;
                   7303:     {
                   7304: 	use integer;
                   7305: 	# strings need to be an even # of cahracters long, it it is odd the
                   7306:         # last characters gets thrown away
                   7307: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7308: 	my $symbseed=numval($symb) << 10;
                   7309: 	my $namechck=unpack("%32S*",$username.' ');
                   7310: 	
                   7311: 	my $nameseed=numval($username) << 21;
1.501     albertel 7312: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7313: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7314: 	
                   7315: 	my $num1=$symbchck+$symbseed+$namechck;
                   7316: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7317: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7318: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 7319: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 7320: 	return "$num1,$num2";
                   7321:     }
                   7322: }
                   7323: 
                   7324: sub rndseed_64bit3 {
                   7325:     my ($symb,$courseid,$domain,$username)=@_;
                   7326:     {
                   7327: 	use integer;
                   7328: 	# strings need to be an even # of cahracters long, it it is odd the
                   7329:         # last characters gets thrown away
                   7330: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7331: 	my $symbseed=numval2($symb) << 10;
                   7332: 	my $namechck=unpack("%32S*",$username.' ');
                   7333: 	
                   7334: 	my $nameseed=numval2($username) << 21;
1.443     albertel 7335: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7336: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7337: 	
                   7338: 	my $num1=$symbchck+$symbseed+$namechck;
                   7339: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7340: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7341: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 7342: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7343: 	
1.503     albertel 7344: 	return "$num1:$num2";
1.443     albertel 7345:     }
                   7346: }
                   7347: 
1.575     albertel 7348: sub rndseed_64bit4 {
                   7349:     my ($symb,$courseid,$domain,$username)=@_;
                   7350:     {
                   7351: 	use integer;
                   7352: 	# strings need to be an even # of cahracters long, it it is odd the
                   7353:         # last characters gets thrown away
                   7354: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7355: 	my $symbseed=numval3($symb) << 10;
                   7356: 	my $namechck=unpack("%32S*",$username.' ');
                   7357: 	
                   7358: 	my $nameseed=numval3($username) << 21;
                   7359: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7360: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7361: 	
                   7362: 	my $num1=$symbchck+$symbseed+$namechck;
                   7363: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7364: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7365: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 7366: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7367: 	
                   7368: 	return "$num1:$num2";
                   7369:     }
                   7370: }
                   7371: 
1.675     albertel 7372: sub rndseed_64bit5 {
                   7373:     my ($symb,$courseid,$domain,$username)=@_;
                   7374:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   7375:     return "$num1:$num2";
                   7376: }
                   7377: 
1.366     albertel 7378: sub rndseed_CODE_64bit {
                   7379:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 7380:     {
1.366     albertel 7381: 	use integer;
1.443     albertel 7382: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 7383: 	my $symbseed=numval2($symb);
1.491     albertel 7384: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7385: 	my $CODEseed=numval(&getCODE());
1.443     albertel 7386: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 7387: 	my $num1=$symbseed+$CODEchck;
                   7388: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7389: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7390: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 7391: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7392: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 7393: 	return "$num1:$num2";
1.366     albertel 7394:     }
                   7395: }
                   7396: 
1.575     albertel 7397: sub rndseed_CODE_64bit4 {
                   7398:     my ($symb,$courseid,$domain,$username)=@_;
                   7399:     {
                   7400: 	use integer;
                   7401: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   7402: 	my $symbseed=numval3($symb);
                   7403: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7404: 	my $CODEseed=numval3(&getCODE());
                   7405: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7406: 	my $num1=$symbseed+$CODEchck;
                   7407: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7408: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7409: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 7410: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7411: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   7412: 	return "$num1:$num2";
                   7413:     }
                   7414: }
                   7415: 
1.675     albertel 7416: sub rndseed_CODE_64bit5 {
                   7417:     my ($symb,$courseid,$domain,$username)=@_;
                   7418:     my $code = &getCODE();
                   7419:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   7420:     return "$num1:$num2";
                   7421: }
                   7422: 
1.366     albertel 7423: sub setup_random_from_rndseed {
                   7424:     my ($rndseed)=@_;
1.503     albertel 7425:     if ($rndseed =~/([,:])/) {
                   7426: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 7427: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   7428:     } else {
                   7429: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 7430:     }
1.36      albertel 7431: }
                   7432: 
1.474     albertel 7433: sub latest_receipt_algorithm_id {
1.835     albertel 7434:     return 'receipt3';
1.474     albertel 7435: }
                   7436: 
1.480     www      7437: sub recunique {
                   7438:     my $fucourseid=shift;
                   7439:     my $unique;
1.835     albertel 7440:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   7441: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7442: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      7443:     } else {
                   7444: 	$unique=$perlvar{'lonReceipt'};
                   7445:     }
                   7446:     return unpack("%32C*",$unique);
                   7447: }
                   7448: 
                   7449: sub recprefix {
                   7450:     my $fucourseid=shift;
                   7451:     my $prefix;
1.835     albertel 7452:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   7453: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7454: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      7455:     } else {
                   7456: 	$prefix=$perlvar{'lonHostID'};
                   7457:     }
                   7458:     return unpack("%32C*",$prefix);
                   7459: }
                   7460: 
1.76      www      7461: sub ireceipt {
1.474     albertel 7462:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 7463: 
                   7464:     my $return =&recprefix($fucourseid).'-';
                   7465: 
                   7466:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   7467: 	$env{'request.state'} eq 'construct') {
                   7468: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   7469: 	return $return;
                   7470:     }
                   7471: 
1.76      www      7472:     my $cuname=unpack("%32C*",$funame);
                   7473:     my $cudom=unpack("%32C*",$fudom);
                   7474:     my $cucourseid=unpack("%32C*",$fucourseid);
                   7475:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      7476:     my $cunique=&recunique($fucourseid);
1.474     albertel 7477:     my $cpart=unpack("%32S*",$part);
1.835     albertel 7478:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   7479: 
1.790     albertel 7480: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 7481: 			       
                   7482: 	$return.= ($cunique%$cuname+
                   7483: 		   $cunique%$cudom+
                   7484: 		   $cusymb%$cuname+
                   7485: 		   $cusymb%$cudom+
                   7486: 		   $cucourseid%$cuname+
                   7487: 		   $cucourseid%$cudom+
                   7488: 		   $cpart%$cuname+
                   7489: 		   $cpart%$cudom);
                   7490:     } else {
                   7491: 	$return.= ($cunique%$cuname+
                   7492: 		   $cunique%$cudom+
                   7493: 		   $cusymb%$cuname+
                   7494: 		   $cusymb%$cudom+
                   7495: 		   $cucourseid%$cuname+
                   7496: 		   $cucourseid%$cudom);
                   7497:     }
                   7498:     return $return;
1.76      www      7499: }
                   7500: 
                   7501: sub receipt {
1.474     albertel 7502:     my ($part)=@_;
1.790     albertel 7503:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 7504:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      7505: }
1.260     ng       7506: 
1.790     albertel 7507: sub whichuser {
                   7508:     my ($passedsymb)=@_;
                   7509:     my ($symb,$courseid,$domain,$name,$publicuser);
                   7510:     if (defined($env{'form.grade_symb'})) {
                   7511: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   7512: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   7513: 	if (!$allowed &&
                   7514: 	    exists($env{'request.course.sec'}) &&
                   7515: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   7516: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   7517: 			      '/'.$env{'request.course.sec'});
                   7518: 	}
                   7519: 	if ($allowed) {
                   7520: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   7521: 	    $courseid=$tmp_courseid;
                   7522: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   7523: 	    ($name)=&get_env_multiple('form.grade_username');
                   7524: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   7525: 	}
                   7526:     }
                   7527:     if (!$passedsymb) {
                   7528: 	$symb=&symbread();
                   7529:     } else {
                   7530: 	$symb=$passedsymb;
                   7531:     }
                   7532:     $courseid=$env{'request.course.id'};
                   7533:     $domain=$env{'user.domain'};
                   7534:     $name=$env{'user.name'};
                   7535:     if ($name eq 'public' && $domain eq 'public') {
                   7536: 	if (!defined($env{'form.username'})) {
                   7537: 	    $env{'form.username'}.=time.rand(10000000);
                   7538: 	}
                   7539: 	$name.=$env{'form.username'};
                   7540:     }
                   7541:     return ($symb,$courseid,$domain,$name,$publicuser);
                   7542: 
                   7543: }
                   7544: 
1.36      albertel 7545: # ------------------------------------------------------------ Serves up a file
1.472     albertel 7546: # returns either the contents of the file or 
                   7547: # -1 if the file doesn't exist
1.481     raeburn  7548: #
                   7549: # if the target is a file that was uploaded via DOCS, 
                   7550: # a check will be made to see if a current copy exists on the local server,
                   7551: # if it does this will be served, otherwise a copy will be retrieved from
                   7552: # the home server for the course and stored in /home/httpd/html/userfiles on
                   7553: # the local server.   
1.472     albertel 7554: 
1.36      albertel 7555: sub getfile {
1.538     albertel 7556:     my ($file) = @_;
1.609     banghart 7557:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 7558:     &repcopy($file);
                   7559:     return &readfile($file);
                   7560: }
                   7561: 
                   7562: sub repcopy_userfile {
                   7563:     my ($file)=@_;
1.609     banghart 7564:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 7565:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 7566:     my ($cdom,$cnum,$filename) = 
1.811     albertel 7567: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 7568:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   7569:     if (-e "$file") {
1.828     www      7570: # we already have a local copy, check it out
1.538     albertel 7571: 	my @fileinfo = stat($file);
1.828     www      7572: 	my $rtncode;
                   7573: 	my $info;
1.538     albertel 7574: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 7575: 	if ($lwpresp ne 'ok') {
1.828     www      7576: # there is no such file anymore, even though we had a local copy
1.482     albertel 7577: 	    if ($rtncode eq '404') {
1.538     albertel 7578: 		unlink($file);
1.482     albertel 7579: 	    }
                   7580: 	    return -1;
                   7581: 	}
                   7582: 	if ($info < $fileinfo[9]) {
1.828     www      7583: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  7584: 	    return 'ok';
1.828     www      7585: 	} else {
                   7586: # the file is outdated, get rid of it
                   7587: 	    unlink($file);
1.482     albertel 7588: 	}
1.828     www      7589:     }
                   7590: # one way or the other, at this point, we don't have the file
                   7591: # construct the correct path for the file
                   7592:     my @parts = ($cdom,$cnum); 
                   7593:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   7594: 	push @parts, split(/\//,$1);
                   7595:     }
                   7596:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   7597:     foreach my $part (@parts) {
                   7598: 	$path .= '/'.$part;
                   7599: 	if (!-e $path) {
                   7600: 	    mkdir($path,0770);
1.482     albertel 7601: 	}
                   7602:     }
1.828     www      7603: # now the path exists for sure
                   7604: # get a user agent
                   7605:     my $ua=new LWP::UserAgent;
                   7606:     my $transferfile=$file.'.in.transfer';
                   7607: # FIXME: this should flock
                   7608:     if (-e $transferfile) { return 'ok'; }
                   7609:     my $request;
                   7610:     $uri=~s/^\///;
1.838     albertel 7611:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828     www      7612:     my $response=$ua->request($request,$transferfile);
                   7613: # did it work?
                   7614:     if ($response->is_error()) {
                   7615: 	unlink($transferfile);
                   7616: 	&logthis("Userfile repcopy failed for $uri");
                   7617: 	return -1;
                   7618:     }
                   7619: # worked, rename the transfer file
                   7620:     rename($transferfile,$file);
1.607     raeburn  7621:     return 'ok';
1.481     raeburn  7622: }
                   7623: 
1.517     albertel 7624: sub tokenwrapper {
                   7625:     my $uri=shift;
1.552     albertel 7626:     $uri=~s|^http\://([^/]+)||;
                   7627:     $uri=~s|^/||;
1.620     albertel 7628:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 7629:     my $token=$1;
1.552     albertel 7630:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   7631:     if ($udom && $uname && $file) {
                   7632: 	$file=~s|(\?\.*)*$||;
1.620     albertel 7633:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.838     albertel 7634:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517     albertel 7635:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   7636:                                '&tokenissued='.$perlvar{'lonHostID'};
                   7637:     } else {
                   7638:         return '/adm/notfound.html';
                   7639:     }
                   7640: }
                   7641: 
1.828     www      7642: # call with reqtype HEAD: get last modification time
                   7643: # call with reqtype GET: get the file contents
                   7644: # Do not call this with reqtype GET for large files! It loads everything into memory
                   7645: #
1.481     raeburn  7646: sub getuploaded {
                   7647:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   7648:     $uri=~s/^\///;
1.838     albertel 7649:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481     raeburn  7650:     my $ua=new LWP::UserAgent;
                   7651:     my $request=new HTTP::Request($reqtype,$uri);
                   7652:     my $response=$ua->request($request);
                   7653:     $$rtncode = $response->code;
1.482     albertel 7654:     if (! $response->is_success()) {
                   7655: 	return 'failed';
                   7656:     }      
                   7657:     if ($reqtype eq 'HEAD') {
1.486     www      7658: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 7659:     } elsif ($reqtype eq 'GET') {
                   7660: 	$$info = $response->content;
1.472     albertel 7661:     }
1.482     albertel 7662:     return 'ok';
1.36      albertel 7663: }
                   7664: 
1.481     raeburn  7665: sub readfile {
                   7666:     my $file = shift;
                   7667:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   7668:     my $fh;
                   7669:     open($fh,"<$file");
                   7670:     my $a='';
1.800     albertel 7671:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  7672:     return $a;
                   7673: }
                   7674: 
1.36      albertel 7675: sub filelocation {
1.590     banghart 7676:     my ($dir,$file) = @_;
                   7677:     my $location;
                   7678:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 7679: 
                   7680:     if ($file =~ m-^/adm/-) {
                   7681: 	$file=~s-^/adm/wrapper/-/-;
                   7682: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   7683:     }
1.882     albertel 7684: 
1.590     banghart 7685:     if ($file=~m:^/~:) { # is a contruction space reference
                   7686:         $location = $file;
                   7687:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 7688:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 7689: 	# is a correct contruction space reference
                   7690:         $location = $file;
1.609     banghart 7691:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 7692:         my ($udom,$uname,$filename)=
1.811     albertel 7693:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 7694:         my $home=&homeserver($uname,$udom);
                   7695:         my $is_me=0;
                   7696:         my @ids=&current_machine_ids();
                   7697:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   7698:         if ($is_me) {
1.740     www      7699:   	    $location=&propath($udom,$uname).
1.590     banghart 7700:   	      '/userfiles/'.$filename;
                   7701:         } else {
                   7702:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   7703:   	      $udom.'/'.$uname.'/'.$filename;
                   7704:         }
1.882     albertel 7705:     } elsif ($file =~ m-^/adm/-) {
                   7706: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 7707:     } else {
                   7708:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   7709:         $file=~s:^/res/:/:;
                   7710:         if ( !( $file =~ m:^/:) ) {
                   7711:             $location = $dir. '/'.$file;
                   7712:         } else {
                   7713:             $location = '/home/httpd/html/res'.$file;
                   7714:         }
1.59      albertel 7715:     }
1.590     banghart 7716:     $location=~s://+:/:g; # remove duplicate /
                   7717:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
                   7718:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   7719:     return $location;
1.46      www      7720: }
1.36      albertel 7721: 
1.46      www      7722: sub hreflocation {
                   7723:     my ($dir,$file)=@_;
1.460     albertel 7724:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666     albertel 7725: 	$file=filelocation($dir,$file);
1.700     albertel 7726:     } elsif ($file=~m-^/adm/-) {
                   7727: 	$file=~s-^/adm/wrapper/-/-;
                   7728: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 7729:     }
                   7730:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   7731: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 7732:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   7733: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 7734:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 7735: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 7736: 	    -/uploaded/$1/$2/-x;
1.46      www      7737:     }
1.910.2.1! albertel 7738:     if ($file=~ m{^/userfiles/}) {
        !          7739: 	$file =~ s{^/userfiles/}{/uploaded/};
        !          7740:     }
1.462     albertel 7741:     return $file;
1.465     albertel 7742: }
                   7743: 
                   7744: sub current_machine_domains {
1.853     albertel 7745:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   7746: }
                   7747: 
                   7748: sub machine_domains {
                   7749:     my ($hostname) = @_;
1.465     albertel 7750:     my @domains;
1.838     albertel 7751:     my %hostname = &all_hostnames();
1.465     albertel 7752:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  7753: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 7754: 	if ($hostname eq $name) {
1.844     albertel 7755: 	    push(@domains,&host_domain($id));
1.465     albertel 7756: 	}
                   7757:     }
                   7758:     return @domains;
                   7759: }
                   7760: 
                   7761: sub current_machine_ids {
1.853     albertel 7762:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   7763: }
                   7764: 
                   7765: sub machine_ids {
                   7766:     my ($hostname) = @_;
                   7767:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 7768:     my @ids;
1.888     albertel 7769:     my %name_to_host = &all_names();
1.889     albertel 7770:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   7771: 	return @{ $name_to_host{$hostname} };
                   7772:     }
                   7773:     return;
1.31      www      7774: }
                   7775: 
1.824     raeburn  7776: sub additional_machine_domains {
                   7777:     my @domains;
                   7778:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   7779:     while( my $line = <$fh>) {
                   7780:         $line =~ s/\s//g;
                   7781:         push(@domains,$line);
                   7782:     }
                   7783:     return @domains;
                   7784: }
                   7785: 
                   7786: sub default_login_domain {
                   7787:     my $domain = $perlvar{'lonDefDomain'};
                   7788:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   7789:     foreach my $posdom (&current_machine_domains(),
                   7790:                         &additional_machine_domains()) {
                   7791:         if (lc($posdom) eq lc($testdomain)) {
                   7792:             $domain=$posdom;
                   7793:             last;
                   7794:         }
                   7795:     }
                   7796:     return $domain;
                   7797: }
                   7798: 
1.31      www      7799: # ------------------------------------------------------------- Declutters URLs
                   7800: 
                   7801: sub declutter {
                   7802:     my $thisfn=shift;
1.569     albertel 7803:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 7804:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      7805:     $thisfn=~s/^\///;
1.697     albertel 7806:     $thisfn=~s|^adm/wrapper/||;
                   7807:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      7808:     $thisfn=~s/^res\///;
1.235     www      7809:     $thisfn=~s/\?.+$//;
1.268     www      7810:     return $thisfn;
                   7811: }
                   7812: 
                   7813: # ------------------------------------------------------------- Clutter up URLs
                   7814: 
                   7815: sub clutter {
                   7816:     my $thisfn='/'.&declutter(shift);
1.887     albertel 7817:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 7818: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      7819:        $thisfn='/res'.$thisfn; 
                   7820:     }
1.694     albertel 7821:     if ($thisfn !~m|/adm|) {
1.695     albertel 7822: 	if ($thisfn =~ m|/ext/|) {
1.694     albertel 7823: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 7824: 	} else {
                   7825: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   7826: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 7827: 	    if ($embstyle eq 'ssi'
                   7828: 		|| ($embstyle eq 'hdn')
                   7829: 		|| ($embstyle eq 'rat')
                   7830: 		|| ($embstyle eq 'prv')
                   7831: 		|| ($embstyle eq 'ign')) {
                   7832: 		#do nothing with these
                   7833: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 7834: 		|| ($embstyle eq 'emb')
                   7835: 		|| ($embstyle eq 'wrp')) {
                   7836: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 7837: 	    } elsif ($embstyle eq 'unk'
                   7838: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 7839: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 7840: 	    } else {
1.718     www      7841: #		&logthis("Got a blank emb style");
1.695     albertel 7842: 	    }
1.694     albertel 7843: 	}
                   7844:     }
1.31      www      7845:     return $thisfn;
1.12      www      7846: }
                   7847: 
1.787     albertel 7848: sub clutter_with_no_wrapper {
                   7849:     my $uri = &clutter(shift);
                   7850:     if ($uri =~ m-^/adm/-) {
                   7851: 	$uri =~ s-^/adm/wrapper/-/-;
                   7852: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   7853:     }
                   7854:     return $uri;
                   7855: }
                   7856: 
1.557     albertel 7857: sub freeze_escape {
                   7858:     my ($value)=@_;
                   7859:     if (ref($value)) {
                   7860: 	$value=&nfreeze($value);
                   7861: 	return '__FROZEN__'.&escape($value);
                   7862:     }
                   7863:     return &escape($value);
                   7864: }
                   7865: 
1.11      www      7866: 
1.557     albertel 7867: sub thaw_unescape {
                   7868:     my ($value)=@_;
                   7869:     if ($value =~ /^__FROZEN__/) {
                   7870: 	substr($value,0,10,undef);
                   7871: 	$value=&unescape($value);
                   7872: 	return &thaw($value);
                   7873:     }
                   7874:     return &unescape($value);
                   7875: }
                   7876: 
1.436     albertel 7877: sub correct_line_ends {
                   7878:     my ($result)=@_;
                   7879:     $$result =~s/\r\n/\n/mg;
                   7880:     $$result =~s/\r/\n/mg;
1.415     albertel 7881: }
1.1       albertel 7882: # ================================================================ Main Program
                   7883: 
1.184     www      7884: sub goodbye {
1.204     albertel 7885:    &logthis("Starting Shut down");
1.443     albertel 7886: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 7887:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 7888: #converted
1.599     albertel 7889: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 7890:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   7891: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   7892: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 7893: #1.1 only
1.870     albertel 7894: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   7895: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   7896: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   7897: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   7898:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 7899:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   7900:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      7901:    &flushcourselogs();
                   7902:    &logthis("Shutting down");
                   7903: }
                   7904: 
1.852     albertel 7905: sub get_dns {
1.869     albertel 7906:     my ($url,$func,$ignore_cache) = @_;
                   7907:     if (!$ignore_cache) {
                   7908: 	my ($content,$cached)=
                   7909: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   7910: 	if ($cached) {
                   7911: 	    &$func($content);
                   7912: 	    return;
                   7913: 	}
                   7914:     }
                   7915: 
                   7916:     my %alldns;
1.852     albertel 7917:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7918:     foreach my $dns (<$config>) {
                   7919: 	next if ($dns !~ /^\^(\S*)/x);
1.869     albertel 7920: 	$alldns{$1} = 1;
                   7921:     }
                   7922:     while (%alldns) {
                   7923: 	my ($dns) = keys(%alldns);
                   7924: 	delete($alldns{$dns});
1.852     albertel 7925: 	my $ua=new LWP::UserAgent;
                   7926: 	my $request=new HTTP::Request('GET',"http://$dns$url");
                   7927: 	my $response=$ua->request($request);
                   7928: 	next if ($response->is_error());
                   7929: 	my @content = split("\n",$response->content);
1.869     albertel 7930: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 7931: 	&$func(\@content);
1.869     albertel 7932: 	return;
1.852     albertel 7933:     }
                   7934:     close($config);
1.871     albertel 7935:     my $which = (split('/',$url))[3];
                   7936:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   7937:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 7938:     my @content = <$config>;
                   7939:     &$func(\@content);
                   7940:     return;
1.852     albertel 7941: }
1.327     albertel 7942: # ------------------------------------------------------------ Read domain file
                   7943: {
1.852     albertel 7944:     my $loaded;
1.846     albertel 7945:     my %domain;
                   7946: 
1.852     albertel 7947:     sub parse_domain_tab {
                   7948: 	my ($lines) = @_;
                   7949: 	foreach my $line (@$lines) {
                   7950: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      7951: 
1.846     albertel 7952: 	    chomp($line);
1.852     albertel 7953: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 7954: 	    my %this_domain;
                   7955: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   7956: 			       'lang_def', 'city', 'longi', 'lati',
                   7957: 			       'primary') {
                   7958: 		$this_domain{$field} = shift(@elements);
                   7959: 	    }
                   7960: 	    $domain{$name} = \%this_domain;
1.852     albertel 7961: 	}
                   7962:     }
1.864     albertel 7963: 
                   7964:     sub reset_domain_info {
                   7965: 	undef($loaded);
                   7966: 	undef(%domain);
                   7967:     }
                   7968: 
1.852     albertel 7969:     sub load_domain_tab {
1.869     albertel 7970: 	my ($ignore_cache) = @_;
                   7971: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 7972: 	my $fh;
                   7973: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   7974: 	    my @lines = <$fh>;
                   7975: 	    &parse_domain_tab(\@lines);
1.448     albertel 7976: 	}
1.852     albertel 7977: 	close($fh);
                   7978: 	$loaded = 1;
1.327     albertel 7979:     }
1.846     albertel 7980: 
                   7981:     sub domain {
1.852     albertel 7982: 	&load_domain_tab() if (!$loaded);
                   7983: 
1.846     albertel 7984: 	my ($name,$what) = @_;
                   7985: 	return if ( !exists($domain{$name}) );
                   7986: 
                   7987: 	if (!$what) {
                   7988: 	    return $domain{$name}{'description'};
                   7989: 	}
                   7990: 	return $domain{$name}{$what};
                   7991:     }
1.327     albertel 7992: }
                   7993: 
                   7994: 
1.1       albertel 7995: # ------------------------------------------------------------- Read hosts file
                   7996: {
1.838     albertel 7997:     my %hostname;
1.844     albertel 7998:     my %hostdom;
1.845     albertel 7999:     my %libserv;
1.852     albertel 8000:     my $loaded;
1.888     albertel 8001:     my %name_to_host;
1.852     albertel 8002: 
                   8003:     sub parse_hosts_tab {
                   8004: 	my ($file) = @_;
                   8005: 	foreach my $configline (@$file) {
                   8006: 	    next if ($configline =~ /^(\#|\s*$ )/x);
                   8007: 	    next if ($configline =~ /^\^/);
                   8008: 	    chomp($configline);
                   8009: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
                   8010: 	    $name=~s/\s//g;
                   8011: 	    if ($id && $domain && $role && $name) {
                   8012: 		$hostname{$id}=$name;
1.888     albertel 8013: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 8014: 		$hostdom{$id}=$domain;
                   8015: 		if ($role eq 'library') { $libserv{$id}=$name; }
                   8016: 	    }
                   8017: 	}
                   8018:     }
1.864     albertel 8019:     
                   8020:     sub reset_hosts_info {
1.897     albertel 8021: 	&purge_remembered();
1.864     albertel 8022: 	&reset_domain_info();
                   8023: 	&reset_hosts_ip_info();
1.892     albertel 8024: 	undef(%name_to_host);
1.864     albertel 8025: 	undef(%hostname);
                   8026: 	undef(%hostdom);
                   8027: 	undef(%libserv);
                   8028: 	undef($loaded);
                   8029:     }
1.1       albertel 8030: 
1.852     albertel 8031:     sub load_hosts_tab {
1.869     albertel 8032: 	my ($ignore_cache) = @_;
                   8033: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 8034: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   8035: 	my @config = <$config>;
                   8036: 	&parse_hosts_tab(\@config);
                   8037: 	close($config);
                   8038: 	$loaded=1;
1.1       albertel 8039:     }
1.852     albertel 8040: 
1.838     albertel 8041:     sub hostname {
1.852     albertel 8042: 	&load_hosts_tab() if (!$loaded);
                   8043: 
1.838     albertel 8044: 	my ($lonid) = @_;
                   8045: 	return $hostname{$lonid};
                   8046:     }
1.845     albertel 8047: 
1.838     albertel 8048:     sub all_hostnames {
1.852     albertel 8049: 	&load_hosts_tab() if (!$loaded);
                   8050: 
1.838     albertel 8051: 	return %hostname;
                   8052:     }
1.845     albertel 8053: 
1.888     albertel 8054:     sub all_names {
                   8055: 	&load_hosts_tab() if (!$loaded);
                   8056: 
                   8057: 	return %name_to_host;
                   8058:     }
                   8059: 
1.845     albertel 8060:     sub is_library {
1.852     albertel 8061: 	&load_hosts_tab() if (!$loaded);
                   8062: 
1.845     albertel 8063: 	return exists($libserv{$_[0]});
                   8064:     }
                   8065: 
                   8066:     sub all_library {
1.852     albertel 8067: 	&load_hosts_tab() if (!$loaded);
                   8068: 
1.845     albertel 8069: 	return %libserv;
                   8070:     }
                   8071: 
1.841     albertel 8072:     sub get_servers {
1.852     albertel 8073: 	&load_hosts_tab() if (!$loaded);
                   8074: 
1.841     albertel 8075: 	my ($domain,$type) = @_;
                   8076: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   8077: 	                                          : %hostname;
                   8078: 	my %result;
1.842     albertel 8079: 	if (ref($domain) eq 'ARRAY') {
                   8080: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 8081: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 8082: 		    $result{$host} = $hostname;
                   8083: 		}
                   8084: 	    }
                   8085: 	} else {
                   8086: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   8087: 		if ($hostdom{$host} eq $domain) {
                   8088: 		    $result{$host} = $hostname;
                   8089: 		}
1.841     albertel 8090: 	    }
                   8091: 	}
                   8092: 	return %result;
                   8093:     }
1.845     albertel 8094: 
1.844     albertel 8095:     sub host_domain {
1.852     albertel 8096: 	&load_hosts_tab() if (!$loaded);
                   8097: 
1.844     albertel 8098: 	my ($lonid) = @_;
                   8099: 	return $hostdom{$lonid};
                   8100:     }
                   8101: 
1.841     albertel 8102:     sub all_domains {
1.852     albertel 8103: 	&load_hosts_tab() if (!$loaded);
                   8104: 
1.841     albertel 8105: 	my %seen;
                   8106: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   8107: 	return @uniq;
                   8108:     }
1.1       albertel 8109: }
                   8110: 
1.847     albertel 8111: { 
                   8112:     my %iphost;
1.856     albertel 8113:     my %name_to_ip;
                   8114:     my %lonid_to_ip;
1.869     albertel 8115: 
1.847     albertel 8116:     sub get_hosts_from_ip {
                   8117: 	my ($ip) = @_;
                   8118: 	my %iphosts = &get_iphost();
                   8119: 	if (ref($iphosts{$ip})) {
                   8120: 	    return @{$iphosts{$ip}};
                   8121: 	}
                   8122: 	return;
1.839     albertel 8123:     }
1.864     albertel 8124:     
                   8125:     sub reset_hosts_ip_info {
                   8126: 	undef(%iphost);
                   8127: 	undef(%name_to_ip);
                   8128: 	undef(%lonid_to_ip);
                   8129:     }
1.856     albertel 8130: 
                   8131:     sub get_host_ip {
                   8132: 	my ($lonid) = @_;
                   8133: 	if (exists($lonid_to_ip{$lonid})) {
                   8134: 	    return $lonid_to_ip{$lonid};
                   8135: 	}
                   8136: 	my $name=&hostname($lonid);
                   8137:    	my $ip = gethostbyname($name);
                   8138: 	return if (!$ip || length($ip) ne 4);
                   8139: 	$ip=inet_ntoa($ip);
                   8140: 	$name_to_ip{$name}   = $ip;
                   8141: 	$lonid_to_ip{$lonid} = $ip;
                   8142: 	return $ip;
                   8143:     }
1.847     albertel 8144:     
                   8145:     sub get_iphost {
1.869     albertel 8146: 	my ($ignore_cache) = @_;
1.894     albertel 8147: 
1.869     albertel 8148: 	if (!$ignore_cache) {
                   8149: 	    if (%iphost) {
                   8150: 		return %iphost;
                   8151: 	    }
                   8152: 	    my ($ip_info,$cached)=
                   8153: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   8154: 	    if ($cached) {
                   8155: 		%iphost      = %{$ip_info->[0]};
                   8156: 		%name_to_ip  = %{$ip_info->[1]};
                   8157: 		%lonid_to_ip = %{$ip_info->[2]};
                   8158: 		return %iphost;
                   8159: 	    }
                   8160: 	}
1.894     albertel 8161: 
                   8162: 	# get yesterday's info for fallback
                   8163: 	my %old_name_to_ip;
                   8164: 	my ($ip_info,$cached)=
                   8165: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   8166: 	if ($cached) {
                   8167: 	    %old_name_to_ip = %{$ip_info->[1]};
                   8168: 	}
                   8169: 
1.888     albertel 8170: 	my %name_to_host = &all_names();
                   8171: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 8172: 	    my $ip;
                   8173: 	    if (!exists($name_to_ip{$name})) {
                   8174: 		$ip = gethostbyname($name);
                   8175: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 8176: 		    if (defined($old_name_to_ip{$name})) {
                   8177: 			$ip = $old_name_to_ip{$name};
                   8178: 			&logthis("Can't find $name defaulting to old $ip");
                   8179: 		    } else {
                   8180: 			&logthis("Name $name no IP found");
                   8181: 			next;
                   8182: 		    }
                   8183: 		} else {
                   8184: 		    $ip=inet_ntoa($ip);
1.847     albertel 8185: 		}
                   8186: 		$name_to_ip{$name} = $ip;
                   8187: 	    } else {
                   8188: 		$ip = $name_to_ip{$name};
1.653     albertel 8189: 	    }
1.888     albertel 8190: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   8191: 		$lonid_to_ip{$id} = $ip;
                   8192: 	    }
                   8193: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 8194: 	}
1.869     albertel 8195: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   8196: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 8197: 				      48*60*60);
1.869     albertel 8198: 
1.847     albertel 8199: 	return %iphost;
1.598     albertel 8200:     }
                   8201: }
                   8202: 
1.862     albertel 8203: BEGIN {
                   8204: 
                   8205: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   8206:     unless ($readit) {
                   8207: {
                   8208:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   8209:     %perlvar = (%perlvar,%{$configvars});
                   8210: }
                   8211: 
                   8212: 
1.1       albertel 8213: # ------------------------------------------------------ Read spare server file
                   8214: {
1.448     albertel 8215:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 8216: 
                   8217:     while (my $configline=<$config>) {
                   8218:        chomp($configline);
1.284     matthew  8219:        if ($configline) {
1.784     albertel 8220: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 8221: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 8222: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 8223:        }
                   8224:     }
1.448     albertel 8225:     close($config);
1.1       albertel 8226: }
1.11      www      8227: # ------------------------------------------------------------ Read permissions
                   8228: {
1.448     albertel 8229:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      8230: 
                   8231:     while (my $configline=<$config>) {
1.448     albertel 8232: 	chomp($configline);
                   8233: 	if ($configline) {
                   8234: 	    my ($role,$perm)=split(/ /,$configline);
                   8235: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   8236: 	}
1.11      www      8237:     }
1.448     albertel 8238:     close($config);
1.11      www      8239: }
                   8240: 
                   8241: # -------------------------------------------- Read plain texts for permissions
                   8242: {
1.448     albertel 8243:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      8244: 
                   8245:     while (my $configline=<$config>) {
1.448     albertel 8246: 	chomp($configline);
                   8247: 	if ($configline) {
1.742     raeburn  8248: 	    my ($short,@plain)=split(/:/,$configline);
                   8249:             %{$prp{$short}} = ();
                   8250: 	    if (@plain > 0) {
                   8251:                 $prp{$short}{'std'} = $plain[0];
                   8252:                 for (my $i=1; $i<@plain; $i++) {
                   8253:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   8254:                 }
                   8255:             }
1.448     albertel 8256: 	}
1.135     www      8257:     }
1.448     albertel 8258:     close($config);
1.135     www      8259: }
                   8260: 
                   8261: # ---------------------------------------------------------- Read package table
                   8262: {
1.448     albertel 8263:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      8264: 
                   8265:     while (my $configline=<$config>) {
1.483     albertel 8266: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 8267: 	chomp($configline);
                   8268: 	my ($short,$plain)=split(/:/,$configline);
                   8269: 	my ($pack,$name)=split(/\&/,$short);
                   8270: 	if ($plain ne '') {
                   8271: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   8272: 	    $packagetab{$short}=$plain; 
                   8273: 	}
1.11      www      8274:     }
1.448     albertel 8275:     close($config);
1.329     matthew  8276: }
                   8277: 
                   8278: # ------------- set up temporary directory
                   8279: {
                   8280:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   8281: 
1.11      www      8282: }
                   8283: 
1.794     albertel 8284: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   8285: 				'compress_threshold'=> 20_000,
                   8286:  			        });
1.185     www      8287: 
1.281     www      8288: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      8289: $dumpcount=0;
1.22      www      8290: 
1.163     harris41 8291: &logtouch();
1.672     albertel 8292: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      8293: $readit=1;
1.564     albertel 8294:     {
                   8295: 	use integer;
                   8296: 	my $test=(2**32)+1;
1.568     albertel 8297: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 8298: 	&logthis(" Detected 64bit platform ($_64bit)");
                   8299:     }
1.195     www      8300: }
1.1       albertel 8301: }
1.179     www      8302: 
1.1       albertel 8303: 1;
1.191     harris41 8304: __END__
                   8305: 
1.243     albertel 8306: =pod
                   8307: 
1.191     harris41 8308: =head1 NAME
                   8309: 
1.243     albertel 8310: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 8311: 
                   8312: =head1 SYNOPSIS
                   8313: 
1.243     albertel 8314: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 8315: 
                   8316:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   8317: 
1.243     albertel 8318: Common parameters:
                   8319: 
                   8320: =over 4
                   8321: 
                   8322: =item *
                   8323: 
                   8324: $uname : an internal username (if $cname expecting a course Id specifically)
                   8325: 
                   8326: =item *
                   8327: 
                   8328: $udom : a domain (if $cdom expecting a course's domain specifically)
                   8329: 
                   8330: =item *
                   8331: 
                   8332: $symb : a resource instance identifier
                   8333: 
                   8334: =item *
                   8335: 
                   8336: $namespace : the name of a .db file that contains the data needed or
                   8337: being set.
                   8338: 
                   8339: =back
                   8340: 
1.394     bowersj2 8341: =head1 OVERVIEW
1.191     harris41 8342: 
1.394     bowersj2 8343: lonnet provides subroutines which interact with the
                   8344: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   8345: about classes, users, and resources.
1.243     albertel 8346: 
                   8347: For many of these objects you can also use this to store data about
                   8348: them or modify them in various ways.
1.191     harris41 8349: 
1.394     bowersj2 8350: =head2 Symbs
1.191     harris41 8351: 
1.394     bowersj2 8352: To identify a specific instance of a resource, LON-CAPA uses symbols
                   8353: or "symbs"X<symb>. These identifiers are built from the URL of the
                   8354: map, the resource number of the resource in the map, and the URL of
                   8355: the resource itself. The latter is somewhat redundant, but might help
                   8356: if maps change.
                   8357: 
                   8358: An example is
                   8359: 
                   8360:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   8361: 
                   8362: The respective map entry is
                   8363: 
                   8364:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   8365:   title="Problem 2">
                   8366:  </resource>
                   8367: 
                   8368: Symbs are used by the random number generator, as well as to store and
                   8369: restore data specific to a certain instance of for example a problem.
                   8370: 
                   8371: =head2 Storing And Retrieving Data
                   8372: 
                   8373: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   8374: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   8375: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   8376: is is the non-critical message twin of cstore. These functions are for
                   8377: handlers to store a perl hash to a user's permanent data space in an
                   8378: easy manner, and to retrieve it again on another call. It is expected
                   8379: that a handler would use this once at the beginning to retrieve data,
                   8380: and then again once at the end to send only the new data back.
                   8381: 
                   8382: The data is stored in the user's data directory on the user's
                   8383: homeserver under the ID of the course.
                   8384: 
                   8385: The hash that is returned by restore will have all of the previous
                   8386: value for all of the elements of the hash.
                   8387: 
                   8388: Example:
                   8389: 
                   8390:  #creating a hash
                   8391:  my %hash;
                   8392:  $hash{'foo'}='bar';
                   8393: 
                   8394:  #storing it
                   8395:  &Apache::lonnet::cstore(\%hash);
                   8396: 
                   8397:  #changing a value
                   8398:  $hash{'foo'}='notbar';
                   8399: 
                   8400:  #adding a new value
                   8401:  $hash{'bar'}='foo';
                   8402:  &Apache::lonnet::cstore(\%hash);
                   8403: 
                   8404:  #retrieving the hash
                   8405:  my %history=&Apache::lonnet::restore();
                   8406: 
                   8407:  #print the hash
                   8408:  foreach my $key (sort(keys(%history))) {
                   8409:    print("\%history{$key} = $history{$key}");
                   8410:  }
                   8411: 
                   8412: Will print out:
1.191     harris41 8413: 
1.394     bowersj2 8414:  %history{1:foo} = bar
                   8415:  %history{1:keys} = foo:timestamp
                   8416:  %history{1:timestamp} = 990455579
                   8417:  %history{2:bar} = foo
                   8418:  %history{2:foo} = notbar
                   8419:  %history{2:keys} = foo:bar:timestamp
                   8420:  %history{2:timestamp} = 990455580
                   8421:  %history{bar} = foo
                   8422:  %history{foo} = notbar
                   8423:  %history{timestamp} = 990455580
                   8424:  %history{version} = 2
                   8425: 
                   8426: Note that the special hash entries C<keys>, C<version> and
                   8427: C<timestamp> were added to the hash. C<version> will be equal to the
                   8428: total number of versions of the data that have been stored. The
                   8429: C<timestamp> attribute will be the UNIX time the hash was
                   8430: stored. C<keys> is available in every historical section to list which
                   8431: keys were added or changed at a specific historical revision of a
                   8432: hash.
                   8433: 
                   8434: B<Warning>: do not store the hash that restore returns directly. This
                   8435: will cause a mess since it will restore the historical keys as if the
                   8436: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 8437: 
1.394     bowersj2 8438: Calling convention:
1.191     harris41 8439: 
1.394     bowersj2 8440:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   8441:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 8442: 
1.394     bowersj2 8443: For more detailed information, see lonnet specific documentation.
1.191     harris41 8444: 
1.394     bowersj2 8445: =head1 RETURN MESSAGES
1.191     harris41 8446: 
1.394     bowersj2 8447: =over 4
1.191     harris41 8448: 
1.394     bowersj2 8449: =item * B<con_lost>: unable to contact remote host
1.191     harris41 8450: 
1.394     bowersj2 8451: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   8452: when the connection is brought back up
1.191     harris41 8453: 
1.394     bowersj2 8454: =item * B<con_failed>: unable to contact remote host and unable to save message
                   8455: for later delivery
1.191     harris41 8456: 
1.394     bowersj2 8457: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 8458: 
1.394     bowersj2 8459: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 8460: that was requested
1.191     harris41 8461: 
1.243     albertel 8462: =back
1.191     harris41 8463: 
1.243     albertel 8464: =head1 PUBLIC SUBROUTINES
1.191     harris41 8465: 
1.243     albertel 8466: =head2 Session Environment Functions
1.191     harris41 8467: 
1.243     albertel 8468: =over 4
1.191     harris41 8469: 
1.394     bowersj2 8470: =item * 
                   8471: X<appenv()>
                   8472: B<appenv(%hash)>: the value of %hash is written to
                   8473: the user envirnoment file, and will be restored for each access this
1.620     albertel 8474: user makes during this session, also modifies the %env for the current
1.394     bowersj2 8475: process
1.191     harris41 8476: 
                   8477: =item *
1.394     bowersj2 8478: X<delenv()>
                   8479: B<delenv($regexp)>: removes all items from the session
                   8480: environment file that matches the regular expression in $regexp. The
1.620     albertel 8481: values are also delted from the current processes %env.
1.191     harris41 8482: 
1.795     albertel 8483: =item * get_env_multiple($name) 
                   8484: 
                   8485: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   8486: values may be defined and end up as an array ref.
                   8487: 
                   8488: returns an array of values
                   8489: 
1.243     albertel 8490: =back
                   8491: 
                   8492: =head2 User Information
1.191     harris41 8493: 
1.243     albertel 8494: =over 4
1.191     harris41 8495: 
                   8496: =item *
1.394     bowersj2 8497: X<queryauthenticate()>
                   8498: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 8499: authentication scheme
                   8500: 
                   8501: =item *
1.394     bowersj2 8502: X<authenticate()>
                   8503: B<authenticate($uname,$upass,$udom)>: try to
                   8504: authenticate user from domain's lib servers (first use the current
                   8505: one). C<$upass> should be the users password.
1.191     harris41 8506: 
                   8507: =item *
1.394     bowersj2 8508: X<homeserver()>
                   8509: B<homeserver($uname,$udom)>: find the server which has
                   8510: the user's directory and files (there must be only one), this caches
                   8511: the answer, and also caches if there is a borken connection.
1.191     harris41 8512: 
                   8513: =item *
1.394     bowersj2 8514: X<idget()>
                   8515: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   8516: (IDs are a unique resource in a domain, there must be only 1 ID per
                   8517: username, and only 1 username per ID in a specific domain) (returns
                   8518: hash: id=>name,id=>name)
1.191     harris41 8519: 
                   8520: =item *
1.394     bowersj2 8521: X<idrget()>
                   8522: B<idrget($udom,@unames)>: find the IDs behind a list of
                   8523: usernames (returns hash: name=>id,name=>id)
1.191     harris41 8524: 
                   8525: =item *
1.394     bowersj2 8526: X<idput()>
                   8527: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 8528: 
                   8529: =item *
1.394     bowersj2 8530: X<rolesinit()>
                   8531: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 8532: 
                   8533: =item *
1.551     albertel 8534: X<getsection()>
                   8535: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 8536: course $cname, return section name/number or '' for "not in course"
                   8537: and '-1' for "no section"
                   8538: 
                   8539: =item *
1.394     bowersj2 8540: X<userenvironment()>
                   8541: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 8542: passed in @what from the requested user's environment, returns a hash
                   8543: 
1.858     raeburn  8544: =item * 
                   8545: X<userlog_query()>
1.859     albertel 8546: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   8547: activity.log file. %filters defines filters applied when parsing the
                   8548: log file. These can be start or end timestamps, or the type of action
                   8549: - log to look for Login or Logout events, check for Checkin or
                   8550: Checkout, role for role selection. The response is in the form
                   8551: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   8552: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  8553: 
1.243     albertel 8554: =back
                   8555: 
                   8556: =head2 User Roles
                   8557: 
                   8558: =over 4
                   8559: 
                   8560: =item *
                   8561: 
1.810     raeburn  8562: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 8563:  F: full access
                   8564:  U,I,K: authentication modes (cxx only)
                   8565:  '': forbidden
                   8566:  1: user needs to choose course
                   8567:  2: browse allowed
1.766     albertel 8568:  A: passphrase authentication needed
1.243     albertel 8569: 
                   8570: =item *
                   8571: 
                   8572: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   8573: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   8574: and course level
                   8575: 
                   8576: =item *
                   8577: 
                   8578: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   8579: explanation of a user role term
                   8580: 
1.832     raeburn  8581: =item *
                   8582: 
1.858     raeburn  8583: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
                   8584: All arguments are optional. Returns a hash of a roles, either for
                   8585: co-author/assistant author roles for a user's Construction Space
1.906     albertel 8586: (default), or if $context is 'userroles', roles for the user himself,
1.858     raeburn  8587: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
                   8588: and value is set to colon-separated start and end times for the role.
                   8589: If no username and domain are specified, will default to current
                   8590: user/domain. Types, roles, and roledoms are references to arrays,
                   8591: of role statuses (active, future or previous), roles 
                   8592: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   8593: to restrict the list of roles reported. If no array ref is 
                   8594: provided for types, will default to return only active roles.
1.834     albertel 8595: 
1.243     albertel 8596: =back
                   8597: 
                   8598: =head2 User Modification
                   8599: 
                   8600: =over 4
                   8601: 
                   8602: =item *
                   8603: 
                   8604: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   8605: user for the level given by URL.  Optional start and end dates (leave empty
                   8606: string or zero for "no date")
1.191     harris41 8607: 
                   8608: =item *
                   8609: 
1.243     albertel 8610: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   8611: change a users, password, possible return values are: ok,
                   8612: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   8613: refused
1.191     harris41 8614: 
                   8615: =item *
                   8616: 
1.243     albertel 8617: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 8618: 
                   8619: =item *
                   8620: 
1.243     albertel 8621: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   8622: modify user
1.191     harris41 8623: 
                   8624: =item *
                   8625: 
1.286     matthew  8626: modifystudent
                   8627: 
                   8628: modify a students enrollment and identification information.
                   8629: The course id is resolved based on the current users environment.  
                   8630: This means the envoking user must be a course coordinator or otherwise
                   8631: associated with a course.
                   8632: 
1.297     matthew  8633: This call is essentially a wrapper for lonnet::modifyuser and
                   8634: lonnet::modify_student_enrollment
1.286     matthew  8635: 
                   8636: Inputs: 
                   8637: 
                   8638: =over 4
                   8639: 
                   8640: =item B<$udom> Students loncapa domain
                   8641: 
                   8642: =item B<$uname> Students loncapa login name
                   8643: 
                   8644: =item B<$uid> Students id/student number
                   8645: 
                   8646: =item B<$umode> Students authentication mode
                   8647: 
                   8648: =item B<$upass> Students password
                   8649: 
                   8650: =item B<$first> Students first name
                   8651: 
                   8652: =item B<$middle> Students middle name
                   8653: 
                   8654: =item B<$last> Students last name
                   8655: 
                   8656: =item B<$gene> Students generation
                   8657: 
                   8658: =item B<$usec> Students section in course
                   8659: 
                   8660: =item B<$end> Unix time of the roles expiration
                   8661: 
                   8662: =item B<$start> Unix time of the roles start date
                   8663: 
                   8664: =item B<$forceid> If defined, allow $uid to be changed
                   8665: 
                   8666: =item B<$desiredhome> server to use as home server for student
                   8667: 
                   8668: =back
1.297     matthew  8669: 
                   8670: =item *
                   8671: 
                   8672: modify_student_enrollment
                   8673: 
                   8674: Change a students enrollment status in a class.  The environment variable
                   8675: 'role.request.course' must be defined for this function to proceed.
                   8676: 
                   8677: Inputs:
                   8678: 
                   8679: =over 4
                   8680: 
                   8681: =item $udom, students domain
                   8682: 
                   8683: =item $uname, students name
                   8684: 
                   8685: =item $uid, students user id
                   8686: 
                   8687: =item $first, students first name
                   8688: 
                   8689: =item $middle
                   8690: 
                   8691: =item $last
                   8692: 
                   8693: =item $gene
                   8694: 
                   8695: =item $usec
                   8696: 
                   8697: =item $end
                   8698: 
                   8699: =item $start
                   8700: 
                   8701: =back
                   8702: 
1.191     harris41 8703: 
                   8704: =item *
                   8705: 
1.243     albertel 8706: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   8707: custom role; give a custom role to a user for the level given by URL.  Specify
                   8708: name and domain of role author, and role name
1.191     harris41 8709: 
                   8710: =item *
                   8711: 
1.243     albertel 8712: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 8713: 
                   8714: =item *
                   8715: 
1.243     albertel 8716: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   8717: 
                   8718: =back
                   8719: 
                   8720: =head2 Course Infomation
                   8721: 
                   8722: =over 4
1.191     harris41 8723: 
                   8724: =item *
                   8725: 
1.631     albertel 8726: coursedescription($courseid) : returns a hash of information about the
                   8727: specified course id, including all environment settings for the
                   8728: course, the description of the course will be in the hash under the
                   8729: key 'description'
1.191     harris41 8730: 
                   8731: =item *
                   8732: 
1.624     albertel 8733: resdata($name,$domain,$type,@which) : request for current parameter
                   8734: setting for a specific $type, where $type is either 'course' or 'user',
                   8735: @what should be a list of parameters to ask about. This routine caches
                   8736: answers for 5 minutes.
1.243     albertel 8737: 
1.877     foxr     8738: =item *
                   8739: 
                   8740: get_courseresdata($courseid, $domain) : dump the entire course resource
                   8741: data base, returning a hash that is keyed by the resource name and has
                   8742: values that are the resource value.  I believe that the timestamps and
                   8743: versions are also returned.
                   8744: 
                   8745: 
1.243     albertel 8746: =back
                   8747: 
                   8748: =head2 Course Modification
                   8749: 
                   8750: =over 4
1.191     harris41 8751: 
                   8752: =item *
                   8753: 
1.243     albertel 8754: writecoursepref($courseid,%prefs) : write preferences (environment
                   8755: database) for a course
1.191     harris41 8756: 
                   8757: =item *
                   8758: 
1.243     albertel 8759: createcourse($udom,$description,$url) : make/modify course
                   8760: 
                   8761: =back
                   8762: 
                   8763: =head2 Resource Subroutines
                   8764: 
                   8765: =over 4
1.191     harris41 8766: 
                   8767: =item *
                   8768: 
1.243     albertel 8769: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 8770: 
                   8771: =item *
                   8772: 
1.243     albertel 8773: repcopy($filename) : subscribes to the requested file, and attempts to
                   8774: replicate from the owning library server, Might return
1.607     raeburn  8775: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   8776: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 8777: resource. Expects the local filesystem pathname
                   8778: (/home/httpd/html/res/....)
                   8779: 
                   8780: =back
                   8781: 
                   8782: =head2 Resource Information
                   8783: 
                   8784: =over 4
1.191     harris41 8785: 
                   8786: =item *
                   8787: 
1.243     albertel 8788: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   8789: a vairety of different possible values, $varname should be a request
                   8790: string, and the other parameters can be used to specify who and what
                   8791: one is asking about.
                   8792: 
                   8793: Possible values for $varname are environment.lastname (or other item
                   8794: from the envirnment hash), user.name (or someother aspect about the
                   8795: user), resource.0.maxtries (or some other part and parameter of a
                   8796: resource)
1.204     albertel 8797: 
                   8798: =item *
                   8799: 
1.243     albertel 8800: directcondval($number) : get current value of a condition; reads from a state
                   8801: string
1.204     albertel 8802: 
                   8803: =item *
                   8804: 
1.243     albertel 8805: condval($condidx) : value of condition index based on state
1.204     albertel 8806: 
                   8807: =item *
                   8808: 
1.243     albertel 8809: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   8810: resource's metadata, $what should be either a specific key, or either
                   8811: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   8812: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   8813: 
                   8814: this function automatically caches all requests
1.191     harris41 8815: 
                   8816: =item *
                   8817: 
1.243     albertel 8818: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   8819: network of library servers; returns file handle of where SQL and regex results
                   8820: will be stored for query
1.191     harris41 8821: 
                   8822: =item *
                   8823: 
1.243     albertel 8824: symbread($filename) : return symbolic list entry (filename argument optional);
                   8825: returns the data handle
1.191     harris41 8826: 
                   8827: =item *
                   8828: 
1.243     albertel 8829: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 8830: a possible symb for the URL in $thisfn, and if is an encryypted
                   8831: resource that the user accessed using /enc/ returns a 1 on success, 0
                   8832: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 8833: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 8834: 
1.191     harris41 8835: 
                   8836: =item *
                   8837: 
1.243     albertel 8838: symbclean($symb) : removes versions numbers from a symb, returns the
                   8839: cleaned symb
1.191     harris41 8840: 
                   8841: =item *
                   8842: 
1.243     albertel 8843: is_on_map($uri) : checks if the $uri is somewhere on the current
                   8844: course map, user must be in a course for it to work.
1.191     harris41 8845: 
                   8846: =item *
                   8847: 
1.243     albertel 8848: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 8849: 
                   8850: =item *
                   8851: 
1.243     albertel 8852: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   8853: a random seed, all arguments are optional, if they aren't sent it uses the
                   8854: environment to derive them. Note: if symb isn't sent and it can't get one
                   8855: from &symbread it will use the current time as its return value
1.191     harris41 8856: 
                   8857: =item *
                   8858: 
1.243     albertel 8859: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   8860: unfakeable, receipt
1.191     harris41 8861: 
                   8862: =item *
                   8863: 
1.620     albertel 8864: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 8865: 
                   8866: =item *
                   8867: 
1.243     albertel 8868: countacc($url) : count the number of accesses to a given URL
1.191     harris41 8869: 
                   8870: =item *
                   8871: 
1.243     albertel 8872: 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 8873: 
                   8874: =item *
                   8875: 
1.243     albertel 8876: 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 8877: 
                   8878: =item *
                   8879: 
1.243     albertel 8880: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 8881: 
                   8882: =item *
                   8883: 
1.243     albertel 8884: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   8885: forcing spreadsheet to reevaluate the resource scores next time.
                   8886: 
                   8887: =back
                   8888: 
                   8889: =head2 Storing/Retreiving Data
                   8890: 
                   8891: =over 4
1.191     harris41 8892: 
                   8893: =item *
                   8894: 
1.243     albertel 8895: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   8896: for this url; hashref needs to be given and should be a \%hashname; the
                   8897: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 8898: be derived from the env
1.191     harris41 8899: 
                   8900: =item *
                   8901: 
1.243     albertel 8902: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   8903: uses critical subroutine
1.191     harris41 8904: 
                   8905: =item *
                   8906: 
1.243     albertel 8907: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   8908: all args are optional
1.191     harris41 8909: 
                   8910: =item *
                   8911: 
1.717     albertel 8912: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   8913: dumps the complete (or key matching regexp) namespace into a hash
                   8914: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   8915: normally &store()ed into
                   8916: 
                   8917: $range should be either an integer '100' (give me the first 100
                   8918:                                            matching records)
                   8919:               or be  two integers sperated by a - with no spaces
                   8920:                  '30-50' (give me the 30th through the 50th matching
                   8921:                           records)
                   8922: 
                   8923: 
                   8924: =item *
                   8925: 
                   8926: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   8927: replaces a &store() version of data with a replacement set of data
                   8928: for a particular resource in a namespace passed in the $storehash hash 
                   8929: reference
                   8930: 
                   8931: =item *
                   8932: 
1.243     albertel 8933: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   8934: works very similar to store/cstore, but all data is stored in a
                   8935: temporary location and can be reset using tmpreset, $storehash should
                   8936: be a hash reference, returns nothing on success
1.191     harris41 8937: 
                   8938: =item *
                   8939: 
1.243     albertel 8940: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   8941: similar to restore, but all data is stored in a temporary location and
                   8942: can be reset using tmpreset. Returns a hash of values on success,
                   8943: error string otherwise.
1.191     harris41 8944: 
                   8945: =item *
                   8946: 
1.243     albertel 8947: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   8948: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 8949: 
                   8950: =item *
                   8951: 
1.243     albertel 8952: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8953: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 8954: 
                   8955: =item *
                   8956: 
1.243     albertel 8957: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   8958: namesp ($udom and $uname are optional)
1.191     harris41 8959: 
                   8960: =item *
                   8961: 
1.702     albertel 8962: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 8963: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 8964: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  8965: 
1.702     albertel 8966: $range should be either an integer '100' (give me the first 100
                   8967:                                            matching records)
                   8968:               or be  two integers sperated by a - with no spaces
                   8969:                  '30-50' (give me the 30th through the 50th matching
                   8970:                           records)
1.449     matthew  8971: =item *
                   8972: 
                   8973: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   8974: $store can be a scalar, an array reference, or if the amount to be 
                   8975: incremented is > 1, a hash reference.
                   8976: 
                   8977: ($udom and $uname are optional)
1.191     harris41 8978: 
                   8979: =item *
                   8980: 
1.243     albertel 8981: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   8982: ($udom and $uname are optional)
1.191     harris41 8983: 
                   8984: =item *
                   8985: 
1.243     albertel 8986: cput($namespace,$storehash,$udom,$uname) : critical put
                   8987: ($udom and $uname are optional)
1.191     harris41 8988: 
                   8989: =item *
                   8990: 
1.748     albertel 8991: newput($namespace,$storehash,$udom,$uname) :
                   8992: 
                   8993: Attempts to store the items in the $storehash, but only if they don't
                   8994: currently exist, if this succeeds you can be certain that you have 
                   8995: successfully created a new key value pair in the $namespace db.
                   8996: 
                   8997: 
                   8998: Args:
                   8999:  $namespace: name of database to store values to
                   9000:  $storehash: hashref to store to the db
                   9001:  $udom: (optional) domain of user containing the db
                   9002:  $uname: (optional) name of user caontaining the db
                   9003: 
                   9004: Returns:
                   9005:  'ok' -> succeeded in storing all keys of $storehash
                   9006:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   9007:                         least <key> already existed in the db (other
                   9008:                         requested keys may also already exist)
                   9009:  'error: <msg>' -> unable to tie the DB or other erorr occured
                   9010:  'con_lost' -> unable to contact request server
                   9011:  'refused' -> action was not allowed by remote machine
                   9012: 
                   9013: 
                   9014: =item *
                   9015: 
1.243     albertel 9016: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   9017: reference filled in from namesp (encrypts the return communication)
                   9018: ($udom and $uname are optional)
1.191     harris41 9019: 
                   9020: =item *
                   9021: 
1.243     albertel 9022: log($udom,$name,$home,$message) : write to permanent log for user; use
                   9023: critical subroutine
                   9024: 
1.806     raeburn  9025: =item *
                   9026: 
1.860     raeburn  9027: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   9028: array reference filled in from namespace found in domain level on either
                   9029: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  9030: 
                   9031: =item *
                   9032: 
1.860     raeburn  9033: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   9034: domain level either on specified domain server ($uhome) or primary domain 
                   9035: server ($udom and $uhome are optional)
1.806     raeburn  9036: 
1.243     albertel 9037: =back
                   9038: 
                   9039: =head2 Network Status Functions
                   9040: 
                   9041: =over 4
1.191     harris41 9042: 
                   9043: =item *
                   9044: 
                   9045: dirlist($uri) : return directory list based on URI
                   9046: 
                   9047: =item *
                   9048: 
1.243     albertel 9049: spareserver() : find server with least workload from spare.tab
                   9050: 
                   9051: =back
                   9052: 
                   9053: =head2 Apache Request
                   9054: 
                   9055: =over 4
1.191     harris41 9056: 
                   9057: =item *
                   9058: 
1.243     albertel 9059: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   9060: localhost, posts hash
                   9061: 
                   9062: =back
                   9063: 
                   9064: =head2 Data to String to Data
                   9065: 
                   9066: =over 4
1.191     harris41 9067: 
                   9068: =item *
                   9069: 
1.243     albertel 9070: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   9071: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 9072: 
                   9073: =item *
                   9074: 
1.243     albertel 9075: hashref2str($hashref) : convert a hashref into a string complete with
                   9076: escaping and '=' and '&' separators, supports elements that are
                   9077: arrayrefs and hashrefs
1.191     harris41 9078: 
                   9079: =item *
                   9080: 
1.243     albertel 9081: arrayref2str($arrayref) : convert an arrayref into a string complete
                   9082: with escaping and '&' separators, supports elements that are arrayrefs
                   9083: and hashrefs
1.191     harris41 9084: 
                   9085: =item *
                   9086: 
1.243     albertel 9087: str2hash($string) : convert string to hash using unescaping and
                   9088: splitting on '=' and '&', supports elements that are arrayrefs and
                   9089: hashrefs
1.191     harris41 9090: 
                   9091: =item *
                   9092: 
1.243     albertel 9093: str2array($string) : convert string to hash using unescaping and
                   9094: splitting on '&', supports elements that are arrayrefs and hashrefs
                   9095: 
                   9096: =back
                   9097: 
                   9098: =head2 Logging Routines
                   9099: 
                   9100: =over 4
                   9101: 
                   9102: These routines allow one to make log messages in the lonnet.log and
                   9103: lonnet.perm logfiles.
1.191     harris41 9104: 
                   9105: =item *
                   9106: 
1.243     albertel 9107: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 9108: 
                   9109: =item *
                   9110: 
1.243     albertel 9111: logthis() : append message to the normal lonnet.log file, it gets
                   9112: preiodically rolled over and deleted.
1.191     harris41 9113: 
                   9114: =item *
                   9115: 
1.243     albertel 9116: logperm() : append a permanent message to lonnet.perm.log, this log
                   9117: file never gets deleted by any automated portion of the system, only
                   9118: messages of critical importance should go in here.
                   9119: 
                   9120: =back
                   9121: 
                   9122: =head2 General File Helper Routines
                   9123: 
                   9124: =over 4
1.191     harris41 9125: 
                   9126: =item *
                   9127: 
1.481     raeburn  9128: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   9129: (a) files in /uploaded
                   9130:   (i) If a local copy of the file exists - 
                   9131:       compares modification date of local copy with last-modified date for 
                   9132:       definitive version stored on home server for course. If local copy is 
                   9133:       stale, requests a new version from the home server and stores it. 
                   9134:       If the original has been removed from the home server, then local copy 
                   9135:       is unlinked.
                   9136:   (ii) If local copy does not exist -
                   9137:       requests the file from the home server and stores it. 
                   9138:   
                   9139:   If $caller is 'uploadrep':  
                   9140:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   9141:     for request for files originally uploaded via DOCS. 
                   9142:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   9143:   
                   9144:   Otherwise:
                   9145:      This indicates a call from the content generation phase of the request.
                   9146:      -  returns the entire contents of the file or -1.
                   9147:      
                   9148: (b) files in /res
                   9149:    - returns the entire contents of a file or -1; 
                   9150:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 9151: 
1.712     albertel 9152: 
                   9153: =item *
                   9154: 
                   9155: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   9156:                   reference
                   9157: 
                   9158: returns either a stat() list of data about the file or an empty list
                   9159: if the file doesn't exist or couldn't find out about it (connection
                   9160: problems or user unknown)
                   9161: 
1.191     harris41 9162: =item *
                   9163: 
1.243     albertel 9164: filelocation($dir,$file) : returns file system location of a file
                   9165: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   9166: directory that relative $file lookups are to looked in ($dir of /a/dir
                   9167: and a file of ../bob will become /a/bob)
1.191     harris41 9168: 
                   9169: =item *
                   9170: 
                   9171: hreflocation($dir,$file) : returns file system location or a URL; same as
                   9172: filelocation except for hrefs
                   9173: 
                   9174: =item *
                   9175: 
                   9176: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   9177: 
1.243     albertel 9178: =back
                   9179: 
1.608     albertel 9180: =head2 Usererfile file routines (/uploaded*)
                   9181: 
                   9182: =over 4
                   9183: 
                   9184: =item *
                   9185: 
                   9186: userfileupload(): main rotine for putting a file in a user or course's
                   9187:                   filespace, arguments are,
                   9188: 
1.620     albertel 9189:  formname - required - this is the name of the element in $env where the
1.608     albertel 9190:            filename, and the contents of the file to create/modifed exist
1.620     albertel 9191:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   9192:            contents of the file is located in $env{'form.'.$formname}
1.608     albertel 9193:  coursedoc - if true, store the file in the course of the active role
                   9194:              of the current user
                   9195:  subdir - required - subdirectory to put the file in under ../userfiles/
                   9196:          if undefined, it will be placed in "unknown"
                   9197: 
                   9198:  (This routine calls clean_filename() to remove any dangerous
                   9199:  characters from the filename, and then calls finuserfileupload() to
                   9200:  complete the transaction)
                   9201: 
                   9202:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9203:  and /adm/notfound.html if unsuccessful
                   9204: 
                   9205: =item *
                   9206: 
                   9207: clean_filename(): routine for cleaing a filename up for storage in
                   9208:                  userfile space, argument is:
                   9209: 
                   9210:  filename - proposed filename
                   9211: 
                   9212: returns: the new clean filename
                   9213: 
                   9214: =item *
                   9215: 
                   9216: finishuserfileupload(): routine that creaes and sends the file to
                   9217: userspace, probably shouldn't be called directly
                   9218: 
                   9219:   docuname: username or courseid of destination for the file
                   9220:   docudom: domain of user/course of destination for the file
                   9221:   formname: same as for userfileupload()
                   9222:   fname: filename (inculding subdirectories) for the file
                   9223: 
                   9224:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9225:  and /adm/notfound.html if unsuccessful
                   9226: 
                   9227: =item *
                   9228: 
                   9229: renameuserfile(): renames an existing userfile to a new name
                   9230: 
                   9231:   Args:
                   9232:    docuname: username or courseid of destination for the file
                   9233:    docudom: domain of user/course of destination for the file
                   9234:    old: current file name (including any subdirs under userfiles)
                   9235:    new: desired file name (including any subdirs under userfiles)
                   9236: 
                   9237: =item *
                   9238: 
                   9239: mkdiruserfile(): creates a directory is a userfiles dir
                   9240: 
                   9241:   Args:
                   9242:    docuname: username or courseid of destination for the file
                   9243:    docudom: domain of user/course of destination for the file
                   9244:    dir: dir to create (including any subdirs under userfiles)
                   9245: 
                   9246: =item *
                   9247: 
                   9248: removeuserfile(): removes a file that exists in userfiles
                   9249: 
                   9250:   Args:
                   9251:    docuname: username or courseid of destination for the file
                   9252:    docudom: domain of user/course of destination for the file
                   9253:    fname: filname to delete (including any subdirs under userfiles)
                   9254: 
                   9255: =item *
                   9256: 
                   9257: removeuploadedurl(): convience function for removeuserfile()
                   9258: 
                   9259:   Args:
                   9260:    url:  a full /uploaded/... url to delete
                   9261: 
1.747     albertel 9262: =item * 
                   9263: 
                   9264: get_portfile_permissions():
                   9265:   Args:
                   9266:     domain: domain of user or course contain the portfolio files
                   9267:     user: name of user or num of course contain the portfolio files
                   9268:   Returns:
                   9269:     hashref of a dump of the proper file_permissions.db
                   9270:    
                   9271: 
                   9272: =item * 
                   9273: 
                   9274: get_access_controls():
                   9275: 
                   9276: Args:
                   9277:   current_permissions: the hash ref returned from get_portfile_permissions()
                   9278:   group: (optional) the group you want the files associated with
                   9279:   file: (optional) the file you want access info on
                   9280: 
                   9281: Returns:
1.749     raeburn  9282:     a hash (keys are file names) of hashes containing
                   9283:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   9284:         values are XML containing access control settings (see below) 
1.747     albertel 9285: 
                   9286: Internal notes:
                   9287: 
1.749     raeburn  9288:  access controls are stored in file_permissions.db as key=value pairs.
                   9289:     key -> path to file/file_name\0uniqueID:scope_end_start
                   9290:         where scope -> public,guest,course,group,domains or users.
                   9291:               end -> UNIX time for end of access (0 -> no end date)
                   9292:               start -> UNIX time for start of access
                   9293: 
                   9294:     value -> XML description of access control
                   9295:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   9296:             <start></start>
                   9297:             <end></end>
                   9298: 
                   9299:             <password></password>  for scope type = guest
                   9300: 
                   9301:             <domain></domain>     for scope type = course or group
                   9302:             <number></number>
                   9303:             <roles id="">
                   9304:              <role></role>
                   9305:              <access></access>
                   9306:              <section></section>
                   9307:              <group></group>
                   9308:             </roles>
                   9309: 
                   9310:             <dom></dom>         for scope type = domains
                   9311: 
                   9312:             <users>             for scope type = users
                   9313:              <user>
                   9314:               <uname></uname>
                   9315:               <udom></udom>
                   9316:              </user>
                   9317:             </users>
                   9318:            </scope> 
                   9319:               
                   9320:  Access data is also aggregated for each file in an additional key=value pair:
                   9321:  key -> path to file/file_name\0accesscontrol 
                   9322:  value -> reference to hash
                   9323:           hash contains key = value pairs
                   9324:           where key = uniqueID:scope_end_start
                   9325:                 value = UNIX time record was last updated
                   9326: 
                   9327:           Used to improve speed of look-ups of access controls for each file.  
                   9328:  
                   9329:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   9330: 
                   9331: modify_access_controls():
                   9332: 
                   9333: Modifies access controls for a portfolio file
                   9334: Args
                   9335: 1. file name
                   9336: 2. reference to hash of required changes,
                   9337: 3. domain
                   9338: 4. username
                   9339:   where domain,username are the domain of the portfolio owner 
                   9340:   (either a user or a course) 
                   9341: 
                   9342: Returns:
                   9343: 1. result of additions or updates ('ok' or 'error', with error message). 
                   9344: 2. result of deletions ('ok' or 'error', with error message).
                   9345: 3. reference to hash of any new or updated access controls.
                   9346: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   9347:    key = integer (inbound ID)
                   9348:    value = uniqueID  
1.747     albertel 9349: 
1.608     albertel 9350: =back
                   9351: 
1.243     albertel 9352: =head2 HTTP Helper Routines
                   9353: 
                   9354: =over 4
                   9355: 
1.191     harris41 9356: =item *
                   9357: 
                   9358: escape() : unpack non-word characters into CGI-compatible hex codes
                   9359: 
                   9360: =item *
                   9361: 
                   9362: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   9363: 
1.243     albertel 9364: =back
                   9365: 
                   9366: =head1 PRIVATE SUBROUTINES
                   9367: 
                   9368: =head2 Underlying communication routines (Shouldn't call)
                   9369: 
                   9370: =over 4
                   9371: 
                   9372: =item *
                   9373: 
                   9374: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   9375: 
                   9376: =item *
                   9377: 
                   9378: reply() : uses subreply to send a message to remote machine, logs all failures
                   9379: 
                   9380: =item *
                   9381: 
                   9382: critical() : passes a critical message to another server; if cannot
                   9383: get through then place message in connection buffer directory and
                   9384: returns con_delayed, if incapable of saving message, returns
                   9385: con_failed
                   9386: 
                   9387: =item *
                   9388: 
                   9389: reconlonc() : tries to reconnect lonc client processes.
                   9390: 
                   9391: =back
                   9392: 
                   9393: =head2 Resource Access Logging
                   9394: 
                   9395: =over 4
                   9396: 
                   9397: =item *
                   9398: 
                   9399: flushcourselogs() : flush (save) buffer logs and access logs
                   9400: 
                   9401: =item *
                   9402: 
                   9403: courselog($what) : save message for course in hash
                   9404: 
                   9405: =item *
                   9406: 
                   9407: courseacclog($what) : save message for course using &courselog().  Perform
                   9408: special processing for specific resource types (problems, exams, quizzes, etc).
                   9409: 
1.191     harris41 9410: =item *
                   9411: 
                   9412: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   9413: as a PerlChildExitHandler
1.243     albertel 9414: 
                   9415: =back
                   9416: 
                   9417: =head2 Other
                   9418: 
                   9419: =over 4
                   9420: 
                   9421: =item *
                   9422: 
                   9423: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 9424: 
                   9425: =back
                   9426: 
                   9427: =cut
1.877     foxr     9428: 

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