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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.896   ! albertel    4: # $Id: lonnet.pm,v 1.895 2007/06/25 23:08:55 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.344     www       859: # --------------------------------------------------- Assign a key to a student
                    860: 
                    861: sub assign_access_key {
1.364     www       862: #
                    863: # a valid key looks like uname:udom#comments
                    864: # comments are being appended
                    865: #
1.498     www       866:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                    867:     $kdom=
1.620     albertel  868:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www       869:     $knum=
1.620     albertel  870:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www       871:     $cdom=
1.620     albertel  872:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       873:     $cnum=
1.620     albertel  874:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    875:     $udom=$env{'user.name'} unless (defined($udom));
                    876:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www       877:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www       878:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel  879:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www       880:                                                   # assigned to this person
                    881:                                                   # - this should not happen,
1.345     www       882:                                                   # unless something went wrong
                    883:                                                   # the first time around
                    884: # ready to assign
1.364     www       885:         $logentry=$1.'; '.$logentry;
1.496     www       886:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www       887:                                                  $kdom,$knum) eq 'ok') {
1.345     www       888: # key now belongs to user
1.346     www       889: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www       890:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
                    891:                 &appenv('environment.'.$envkey => $ckey);
                    892:                 return 'ok';
                    893:             } else {
                    894:                 return 
                    895:   'error: Count not permanently assign key, will need to be re-entered later.';
                    896: 	    }
                    897:         } else {
                    898:             return 'error: Could not assign key, try again later.';
                    899:         }
1.364     www       900:     } elsif (!$existing{$ckey}) {
1.345     www       901: # the key does not exist
                    902: 	return 'error: The key does not exist';
                    903:     } else {
                    904: # the key is somebody else's
                    905: 	return 'error: The key is already in use';
                    906:     }
1.344     www       907: }
                    908: 
1.364     www       909: # ------------------------------------------ put an additional comment on a key
                    910: 
                    911: sub comment_access_key {
                    912: #
                    913: # a valid key looks like uname:udom#comments
                    914: # comments are being appended
                    915: #
                    916:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                    917:     $cdom=
1.620     albertel  918:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www       919:     $cnum=
1.620     albertel  920:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www       921:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                    922:     if ($existing{$ckey}) {
                    923:         $existing{$ckey}.='; '.$logentry;
                    924: # ready to assign
1.367     www       925:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www       926:                                                  $cdom,$cnum) eq 'ok') {
                    927: 	    return 'ok';
                    928:         } else {
                    929: 	    return 'error: Count not store comment.';
                    930:         }
                    931:     } else {
                    932: # the key does not exist
                    933: 	return 'error: The key does not exist';
                    934:     }
                    935: }
                    936: 
1.344     www       937: # ------------------------------------------------------ Generate a set of keys
                    938: 
                    939: sub generate_access_keys {
1.364     www       940:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www       941:     $cdom=
1.620     albertel  942:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       943:     $cnum=
1.620     albertel  944:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www       945:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www       946:     unless (($cdom) && ($cnum)) { return 0; }
                    947:     if ($number>10000) { return 0; }
                    948:     sleep(2); # make sure don't get same seed twice
                    949:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                    950:     my $total=0;
                    951:     for (my $i=1;$i<=$number;$i++) {
                    952:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                    953:                   sprintf("%lx",int(100000*rand)).'-'.
                    954:                   sprintf("%lx",int(100000*rand));
                    955:        $newkey=~s/1/g/g; # folks mix up 1 and l
                    956:        $newkey=~s/0/h/g; # and also 0 and O
                    957:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                    958:        if ($existing{$newkey}) {
                    959:            $i--;
                    960:        } else {
1.364     www       961: 	  if (&put('accesskeys',
                    962:               { $newkey => '# generated '.localtime().
1.620     albertel  963:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www       964:                            '; '.$logentry },
                    965: 		   $cdom,$cnum) eq 'ok') {
1.344     www       966:               $total++;
                    967: 	  }
                    968:        }
                    969:     }
1.620     albertel  970:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www       971:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                    972:     return $total;
                    973: }
                    974: 
                    975: # ------------------------------------------------------- Validate an accesskey
                    976: 
                    977: sub validate_access_key {
                    978:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                    979:     $cdom=
1.620     albertel  980:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       981:     $cnum=
1.620     albertel  982:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    983:     $udom=$env{'user.domain'} unless (defined($udom));
                    984:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www       985:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel  986:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www       987: }
                    988: 
                    989: # ------------------------------------- Find the section of student in a course
1.652     albertel  990: sub devalidate_getsection_cache {
                    991:     my ($udom,$unam,$courseid)=@_;
                    992:     my $hashid="$udom:$unam:$courseid";
                    993:     &devalidate_cache_new('getsection',$hashid);
                    994: }
1.298     matthew   995: 
1.815     albertel  996: sub courseid_to_courseurl {
                    997:     my ($courseid) = @_;
                    998:     #already url style courseid
                    999:     return $courseid if ($courseid =~ m{^/});
                   1000: 
                   1001:     if (exists($env{'course.'.$courseid.'.num'})) {
                   1002: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   1003: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   1004: 	return "/$cdom/$cnum";
                   1005:     }
                   1006: 
                   1007:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   1008:     if (exists($courseinfo{'num'})) {
                   1009: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   1010:     }
                   1011: 
                   1012:     return undef;
                   1013: }
                   1014: 
1.298     matthew  1015: sub getsection {
                   1016:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 1017:     my $cachetime=1800;
1.551     albertel 1018: 
                   1019:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 1020:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 1021:     if (defined($cached)) { return $result; }
                   1022: 
1.298     matthew  1023:     my %Pending; 
                   1024:     my %Expired;
                   1025:     #
                   1026:     # Each role can either have not started yet (pending), be active, 
                   1027:     #    or have expired.
                   1028:     #
                   1029:     # If there is an active role, we are done.
                   1030:     #
                   1031:     # If there is more than one role which has not started yet, 
                   1032:     #     choose the one which will start sooner
                   1033:     # If there is one role which has not started yet, return it.
                   1034:     #
                   1035:     # If there is more than one expired role, choose the one which ended last.
                   1036:     # If there is a role which has expired, return it.
                   1037:     #
1.815     albertel 1038:     $courseid = &courseid_to_courseurl($courseid);
1.817     raeburn  1039:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
                   1040:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1041:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1042:         my $section=$1;
                   1043:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1044:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1045:         my $now=time;
1.548     albertel 1046:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1047:             $Expired{$end}=$section;
                   1048:             next;
                   1049:         }
1.548     albertel 1050:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1051:             $Pending{$start}=$section;
                   1052:             next;
                   1053:         }
1.599     albertel 1054:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1055:     }
                   1056:     #
                   1057:     # Presumedly there will be few matching roles from the above
                   1058:     # loop and the sorting time will be negligible.
                   1059:     if (scalar(keys(%Pending))) {
                   1060:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1061:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1062:     } 
                   1063:     if (scalar(keys(%Expired))) {
                   1064:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1065:         my $time = pop(@sorted);
1.599     albertel 1066:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1067:     }
1.599     albertel 1068:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1069: }
1.70      www      1070: 
1.599     albertel 1071: sub save_cache {
                   1072:     &purge_remembered();
1.722     albertel 1073:     #&Apache::loncommon::validate_page();
1.620     albertel 1074:     undef(%env);
1.780     albertel 1075:     undef($env_loaded);
1.599     albertel 1076: }
1.452     albertel 1077: 
1.599     albertel 1078: my $to_remember=-1;
                   1079: my %remembered;
                   1080: my %accessed;
                   1081: my $kicks=0;
                   1082: my $hits=0;
1.849     albertel 1083: sub make_key {
                   1084:     my ($name,$id) = @_;
1.872     albertel 1085:     if (length($id) > 65 
                   1086: 	&& length(&escape($id)) > 200) {
                   1087: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   1088:     }
1.849     albertel 1089:     return &escape($name.':'.$id);
                   1090: }
                   1091: 
1.599     albertel 1092: sub devalidate_cache_new {
                   1093:     my ($name,$id,$debug) = @_;
                   1094:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1095:     $id=&make_key($name,$id);
1.599     albertel 1096:     $memcache->delete($id);
                   1097:     delete($remembered{$id});
                   1098:     delete($accessed{$id});
                   1099: }
                   1100: 
                   1101: sub is_cached_new {
                   1102:     my ($name,$id,$debug) = @_;
1.849     albertel 1103:     $id=&make_key($name,$id);
1.599     albertel 1104:     if (exists($remembered{$id})) {
                   1105: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1106: 	$accessed{$id}=[&gettimeofday()];
                   1107: 	$hits++;
                   1108: 	return ($remembered{$id},1);
                   1109:     }
                   1110:     my $value = $memcache->get($id);
                   1111:     if (!(defined($value))) {
                   1112: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1113: 	return (undef,undef);
1.416     albertel 1114:     }
1.599     albertel 1115:     if ($value eq '__undef__') {
                   1116: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1117: 	$value=undef;
                   1118:     }
                   1119:     &make_room($id,$value,$debug);
                   1120:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1121:     return ($value,1);
                   1122: }
                   1123: 
                   1124: sub do_cache_new {
                   1125:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1126:     $id=&make_key($name,$id);
1.599     albertel 1127:     my $setvalue=$value;
                   1128:     if (!defined($setvalue)) {
                   1129: 	$setvalue='__undef__';
                   1130:     }
1.623     albertel 1131:     if (!defined($time) ) {
                   1132: 	$time=600;
                   1133:     }
1.599     albertel 1134:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.872     albertel 1135:     if (!($memcache->set($id,$setvalue,$time))) {
                   1136: 	&logthis("caching of id -> $id  failed");
                   1137:     }
1.600     albertel 1138:     # need to make a copy of $value
                   1139:     #&make_room($id,$value,$debug);
1.599     albertel 1140:     return $value;
                   1141: }
                   1142: 
                   1143: sub make_room {
                   1144:     my ($id,$value,$debug)=@_;
                   1145:     $remembered{$id}=$value;
                   1146:     if ($to_remember<0) { return; }
                   1147:     $accessed{$id}=[&gettimeofday()];
                   1148:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1149:     my $to_kick;
                   1150:     my $max_time=0;
                   1151:     foreach my $other (keys(%accessed)) {
                   1152: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1153: 	    $to_kick=$other;
                   1154: 	    $max_time=&tv_interval($accessed{$other});
                   1155: 	}
                   1156:     }
                   1157:     delete($remembered{$to_kick});
                   1158:     delete($accessed{$to_kick});
                   1159:     $kicks++;
                   1160:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1161:     return;
                   1162: }
                   1163: 
1.599     albertel 1164: sub purge_remembered {
1.604     albertel 1165:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1166:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1167:     undef(%remembered);
                   1168:     undef(%accessed);
1.428     albertel 1169: }
1.70      www      1170: # ------------------------------------- Read an entry from a user's environment
                   1171: 
                   1172: sub userenvironment {
                   1173:     my ($udom,$unam,@what)=@_;
                   1174:     my %returnhash=();
                   1175:     my @answer=split(/\&/,
                   1176:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1177:                       &homeserver($unam,$udom)));
                   1178:     my $i;
                   1179:     for ($i=0;$i<=$#what;$i++) {
                   1180: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1181:     }
                   1182:     return %returnhash;
1.1       albertel 1183: }
                   1184: 
1.617     albertel 1185: # ---------------------------------------------------------- Get a studentphoto
                   1186: sub studentphoto {
                   1187:     my ($udom,$unam,$ext) = @_;
                   1188:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1189:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1190:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1191:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1192:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1193:             } else {
                   1194:                 my ($result,$perm_reqd)=
1.707     albertel 1195: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1196:                 if ($result eq 'ok') {
                   1197:                     if (!($perm_reqd eq 'yes')) {
                   1198:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1199:                     }
                   1200:                 }
                   1201:             }
                   1202:         }
                   1203:     } else {
                   1204:         my ($result,$perm_reqd) = 
1.707     albertel 1205: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1206:         if ($result eq 'ok') {
                   1207:             if (!($perm_reqd eq 'yes')) {
                   1208:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1209:             }
                   1210:         }
                   1211:     }
                   1212:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1213: }
                   1214: 
                   1215: sub retrievestudentphoto {
                   1216:     my ($udom,$unam,$ext,$type) = @_;
                   1217:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1218:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1219:     if ($ret eq 'ok') {
                   1220:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1221:         if ($type eq 'thumbnail') {
                   1222:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1223:         }
                   1224:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1225:         return $tokenurl;
                   1226:     } else {
                   1227:         if ($type eq 'thumbnail') {
                   1228:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1229:         } else { 
                   1230:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1231:         }
1.617     albertel 1232:     }
                   1233: }
                   1234: 
1.263     www      1235: # -------------------------------------------------------------------- New chat
                   1236: 
                   1237: sub chatsend {
1.724     raeburn  1238:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1239:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1240:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1241:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1242:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1243: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1244: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1245: }
                   1246: 
                   1247: # ------------------------------------------ Find current version of a resource
                   1248: 
                   1249: sub getversion {
                   1250:     my $fname=&clutter(shift);
                   1251:     unless ($fname=~/^\/res\//) { return -1; }
                   1252:     return &currentversion(&filelocation('',$fname));
                   1253: }
                   1254: 
                   1255: sub currentversion {
                   1256:     my $fname=shift;
1.599     albertel 1257:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      1258:     if (defined($cached)) { return $result; }
1.292     www      1259:     my $author=$fname;
                   1260:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1261:     my ($udom,$uname)=split(/\//,$author);
                   1262:     my $home=homeserver($uname,$udom);
                   1263:     if ($home eq 'no_host') { 
                   1264:         return -1; 
                   1265:     }
                   1266:     my $answer=reply("currentversion:$fname",$home);
                   1267:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1268: 	return -1;
                   1269:     }
1.599     albertel 1270:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      1271: }
                   1272: 
1.1       albertel 1273: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1274: 
1.1       albertel 1275: sub subscribe {
                   1276:     my $fname=shift;
1.761     raeburn  1277:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1278:     $fname=~s/[\n\r]//g;
1.1       albertel 1279:     my $author=$fname;
                   1280:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1281:     my ($udom,$uname)=split(/\//,$author);
                   1282:     my $home=homeserver($uname,$udom);
1.335     albertel 1283:     if ($home eq 'no_host') {
                   1284:         return 'not_found';
1.1       albertel 1285:     }
                   1286:     my $answer=reply("sub:$fname",$home);
1.64      www      1287:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1288: 	$answer.=' by '.$home;
                   1289:     }
1.1       albertel 1290:     return $answer;
                   1291: }
                   1292:     
1.8       www      1293: # -------------------------------------------------------------- Replicate file
                   1294: 
                   1295: sub repcopy {
                   1296:     my $filename=shift;
1.23      www      1297:     $filename=~s/\/+/\//g;
1.607     raeburn  1298:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   1299:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 1300:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 1301: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 1302: 	return &repcopy_userfile($filename);
                   1303:     }
1.532     albertel 1304:     $filename=~s/[\n\r]//g;
1.8       www      1305:     my $transname="$filename.in.transfer";
1.828     www      1306: # FIXME: this should flock
1.607     raeburn  1307:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      1308:     my $remoteurl=subscribe($filename);
1.64      www      1309:     if ($remoteurl =~ /^con_lost by/) {
                   1310: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1311:            return 'unavailable';
1.8       www      1312:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1313: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  1314: 	   return 'not_found';
1.64      www      1315:     } elsif ($remoteurl =~ /^rejected by/) {
                   1316: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1317:            return 'forbidden';
1.20      www      1318:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  1319:            return 'ok';
1.8       www      1320:     } else {
1.290     www      1321:         my $author=$filename;
                   1322:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1323:         my ($udom,$uname)=split(/\//,$author);
                   1324:         my $home=homeserver($uname,$udom);
                   1325:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1326:            my @parts=split(/\//,$filename);
                   1327:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1328:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1329:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  1330: 	       return 'bad_request';
1.8       www      1331:            }
                   1332:            my $count;
                   1333:            for ($count=5;$count<$#parts;$count++) {
                   1334:                $path.="/$parts[$count]";
                   1335:                if ((-e $path)!=1) {
                   1336: 		   mkdir($path,0777);
                   1337:                }
                   1338:            }
                   1339:            my $ua=new LWP::UserAgent;
                   1340:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1341:            my $response=$ua->request($request,$transname);
                   1342:            if ($response->is_error()) {
                   1343: 	       unlink($transname);
                   1344:                my $message=$response->status_line;
1.672     albertel 1345:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      1346:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  1347:                return 'unavailable';
1.8       www      1348:            } else {
1.16      www      1349: 	       if ($remoteurl!~/\.meta$/) {
                   1350:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1351:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1352:                   if ($mresponse->is_error()) {
                   1353: 		      unlink($filename.'.meta');
                   1354:                       &logthis(
1.672     albertel 1355:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      1356:                   }
                   1357: 	       }
1.8       www      1358:                rename($transname,$filename);
1.607     raeburn  1359:                return 'ok';
1.8       www      1360:            }
1.290     www      1361:        }
1.8       www      1362:     }
1.330     www      1363: }
                   1364: 
                   1365: # ------------------------------------------------ Get server side include body
                   1366: sub ssi_body {
1.381     albertel 1367:     my ($filelink,%form)=@_;
1.606     matthew  1368:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   1369:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   1370:     }
1.330     www      1371:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381     albertel 1372:                                      &ssi($filelink,%form));
1.778     albertel 1373:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 1374:     $output=~s/^.*?\<body[^\>]*\>//si;
                   1375:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330     www      1376:     return $output;
1.8       www      1377: }
                   1378: 
1.15      www      1379: # --------------------------------------------------------- Server Side Include
                   1380: 
1.782     albertel 1381: sub absolute_url {
                   1382:     my ($host_name) = @_;
                   1383:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   1384:     if ($host_name eq '') {
                   1385: 	$host_name = $ENV{'SERVER_NAME'};
                   1386:     }
                   1387:     return $protocol.$host_name;
                   1388: }
                   1389: 
1.15      www      1390: sub ssi {
                   1391: 
1.23      www      1392:     my ($fn,%form)=@_;
1.15      www      1393: 
                   1394:     my $ua=new LWP::UserAgent;
1.23      www      1395:     
                   1396:     my $request;
1.711     albertel 1397: 
                   1398:     $form{'no_update_last_known'}=1;
1.895     albertel 1399:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      1400:     if (%form) {
1.782     albertel 1401:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201     albertel 1402:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1403:     } else {
1.782     albertel 1404:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      1405:     }
                   1406: 
1.15      www      1407:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1408:     my $response=$ua->request($request);
                   1409: 
1.324     www      1410:     return $response->content;
                   1411: }
                   1412: 
                   1413: sub externalssi {
                   1414:     my ($url)=@_;
                   1415:     my $ua=new LWP::UserAgent;
                   1416:     my $request=new HTTP::Request('GET',$url);
                   1417:     my $response=$ua->request($request);
1.15      www      1418:     return $response->content;
                   1419: }
1.254     www      1420: 
1.492     albertel 1421: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1422: 
                   1423: sub allowuploaded {
                   1424:     my ($srcurl,$url)=@_;
                   1425:     $url=&clutter(&declutter($url));
                   1426:     my $dir=$url;
                   1427:     $dir=~s/\/[^\/]+$//;
                   1428:     my %httpref=();
                   1429:     my $httpurl=&hreflocation('',$url);
                   1430:     $httpref{'httpref.'.$httpurl}=$srcurl;
                   1431:     &Apache::lonnet::appenv(%httpref);
1.254     www      1432: }
1.477     raeburn  1433: 
1.478     albertel 1434: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 1435: # input: action, courseID, current domain, intended
1.637     raeburn  1436: #        path to file, source of file, instruction to parse file for objects,
                   1437: #        ref to hash for embedded objects,
                   1438: #        ref to hash for codebase of java objects.
                   1439: #
1.485     raeburn  1440: # output: url to file (if action was uploaddoc), 
                   1441: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1442: #
1.478     albertel 1443: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1444: # course.
1.477     raeburn  1445: #
1.478     albertel 1446: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1447: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1448: #          course's home server.
1.477     raeburn  1449: #
1.478     albertel 1450: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1451: #          be copied from $source (current location) to 
                   1452: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1453: #         and will then be copied to
                   1454: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1455: #         course's home server.
1.485     raeburn  1456: #
1.481     raeburn  1457: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 1458: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1459: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1460: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1461: #         in course's home server.
1.637     raeburn  1462: #
1.477     raeburn  1463: 
                   1464: sub process_coursefile {
1.638     albertel 1465:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477     raeburn  1466:     my $fetchresult;
1.638     albertel 1467:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  1468:     if ($action eq 'propagate') {
1.638     albertel 1469:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1470: 			     $home);
1.481     raeburn  1471:     } else {
1.477     raeburn  1472:         my $fpath = '';
                   1473:         my $fname = $file;
1.478     albertel 1474:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1475:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  1476:         my $filepath = &build_filepath($fpath);
1.481     raeburn  1477:         if ($action eq 'copy') {
                   1478:             if ($source eq '') {
                   1479:                 $fetchresult = 'no source file';
                   1480:                 return $fetchresult;
                   1481:             } else {
                   1482:                 my $destination = $filepath.'/'.$fname;
                   1483:                 rename($source,$destination);
                   1484:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1485:                                  $home);
1.481     raeburn  1486:             }
                   1487:         } elsif ($action eq 'uploaddoc') {
                   1488:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 1489:             print $fh $env{'form.'.$source};
1.481     raeburn  1490:             close($fh);
1.637     raeburn  1491:             if ($parser eq 'parse') {
                   1492:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
                   1493:                 unless ($parse_result eq 'ok') {
                   1494:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   1495:                 }
                   1496:             }
1.477     raeburn  1497:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1498:                                  $home);
1.481     raeburn  1499:             if ($fetchresult eq 'ok') {
                   1500:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1501:             } else {
                   1502:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1503:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  1504:                 return '/adm/notfound.html';
                   1505:             }
1.477     raeburn  1506:         }
                   1507:     }
1.485     raeburn  1508:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1509:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1510:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  1511:     }
                   1512:     return $fetchresult;
                   1513: }
                   1514: 
1.637     raeburn  1515: sub build_filepath {
                   1516:     my ($fpath) = @_;
                   1517:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1518:     unless ($fpath eq '') {
                   1519:         my @parts=split('/',$fpath);
                   1520:         foreach my $part (@parts) {
                   1521:             $filepath.= '/'.$part;
                   1522:             if ((-e $filepath)!=1) {
                   1523:                 mkdir($filepath,0777);
                   1524:             }
                   1525:         }
                   1526:     }
                   1527:     return $filepath;
                   1528: }
                   1529: 
                   1530: sub store_edited_file {
1.638     albertel 1531:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  1532:     my $file = $primary_url;
                   1533:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   1534:     my $fpath = '';
                   1535:     my $fname = $file;
                   1536:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   1537:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1538:     my $filepath = &build_filepath($fpath);
                   1539:     open(my $fh,'>'.$filepath.'/'.$fname);
                   1540:     print $fh $content;
                   1541:     close($fh);
1.638     albertel 1542:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  1543:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1544: 			  $home);
1.637     raeburn  1545:     if ($$fetchresult eq 'ok') {
                   1546:         return '/uploaded/'.$fpath.'/'.$fname;
                   1547:     } else {
1.638     albertel 1548:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1549: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  1550:         return '/adm/notfound.html';
                   1551:     }
                   1552: }
                   1553: 
1.531     albertel 1554: sub clean_filename {
1.831     albertel 1555:     my ($fname,$args)=@_;
1.315     www      1556: # Replace Windows backslashes by forward slashes
1.257     www      1557:     $fname=~s/\\/\//g;
1.831     albertel 1558:     if (!$args->{'keep_path'}) {
                   1559:         # Get rid of everything but the actual filename
                   1560: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   1561:     }
1.315     www      1562: # Replace spaces by underscores
                   1563:     $fname=~s/\s+/\_/g;
                   1564: # Replace all other weird characters by nothing
1.831     albertel 1565:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 1566: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1567: # numbers
                   1568:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1569:     return $fname;
                   1570: }
                   1571: 
1.608     albertel 1572: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 1573: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719     banghart 1574: #                    the desired filenam is in $env{"form.$formname.filename"}
1.686     albertel 1575: #        $coursedoc - if true up to the current course
                   1576: #                     if false
                   1577: #        $subdir - directory in userfile to store the file into
1.858     raeburn  1578: #        $parser - instruction to parse file for objects ($parser = parse)    
                   1579: #        $allfiles - reference to hash for embedded objects
                   1580: #        $codebase - reference to hash for codebase of java objects
                   1581: #        $desuname - username for permanent storage of uploaded file
                   1582: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  1583: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   1584: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858     raeburn  1585: # 
1.686     albertel 1586: # output: url of file in userspace, or error: <message> 
                   1587: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 1588: 
                   1589: 
1.531     albertel 1590: sub userfileupload {
1.860     raeburn  1591:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
                   1592:         $destudom,$thumbwidth,$thumbheight)=@_;
1.531     albertel 1593:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 1594:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 1595:     $fname=&clean_filename($fname);
1.315     www      1596: # See if there is anything left
1.257     www      1597:     unless ($fname) { return 'error: no uploaded file'; }
1.620     albertel 1598:     chop($env{'form.'.$formname});
1.523     raeburn  1599:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   1600:         my $now = time;
                   1601:         my $filepath = 'tmp/helprequests/'.$now;
                   1602:         my @parts=split(/\//,$filepath);
                   1603:         my $fullpath = $perlvar{'lonDaemons'};
                   1604:         for (my $i=0;$i<@parts;$i++) {
                   1605:             $fullpath .= '/'.$parts[$i];
                   1606:             if ((-e $fullpath)!=1) {
                   1607:                 mkdir($fullpath,0777);
                   1608:             }
                   1609:         }
                   1610:         open(my $fh,'>'.$fullpath.'/'.$fname);
1.620     albertel 1611:         print $fh $env{'form.'.$formname};
1.523     raeburn  1612:         close($fh);
1.741     raeburn  1613:         return $fullpath.'/'.$fname;
                   1614:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
                   1615:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   1616:                        '_'.$env{'user.domain'}.'/pending';
                   1617:         my @parts=split(/\//,$filepath);
                   1618:         my $fullpath = $perlvar{'lonDaemons'};
                   1619:         for (my $i=0;$i<@parts;$i++) {
                   1620:             $fullpath .= '/'.$parts[$i];
                   1621:             if ((-e $fullpath)!=1) {
                   1622:                 mkdir($fullpath,0777);
                   1623:             }
                   1624:         }
                   1625:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   1626:         print $fh $env{'form.'.$formname};
                   1627:         close($fh);
                   1628:         return $fullpath.'/'.$fname;
1.523     raeburn  1629:     }
1.719     banghart 1630:     
1.258     www      1631: # Create the directory if not present
1.493     albertel 1632:     $fname="$subdir/$fname";
1.259     www      1633:     if ($coursedoc) {
1.638     albertel 1634: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1635: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  1636:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 1637:             return &finishuserfileupload($docuname,$docudom,
                   1638: 					 $formname,$fname,$parser,$allfiles,
1.860     raeburn  1639: 					 $codebase,$thumbwidth,$thumbheight);
1.481     raeburn  1640:         } else {
1.620     albertel 1641:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 1642:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   1643: 				       $fname,$formname,$parser,
                   1644: 				       $allfiles,$codebase);
1.481     raeburn  1645:         }
1.719     banghart 1646:     } elsif (defined($destuname)) {
                   1647:         my $docuname=$destuname;
                   1648:         my $docudom=$destudom;
1.860     raeburn  1649: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1650: 				     $parser,$allfiles,$codebase,
                   1651:                                      $thumbwidth,$thumbheight);
1.719     banghart 1652:         
1.259     www      1653:     } else {
1.638     albertel 1654:         my $docuname=$env{'user.name'};
                   1655:         my $docudom=$env{'user.domain'};
1.714     raeburn  1656:         if (exists($env{'form.group'})) {
                   1657:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1658:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1659:         }
1.860     raeburn  1660: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1661: 				     $parser,$allfiles,$codebase,
                   1662:                                      $thumbwidth,$thumbheight);
1.259     www      1663:     }
1.271     www      1664: }
                   1665: 
                   1666: sub finishuserfileupload {
1.860     raeburn  1667:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
                   1668:         $thumbwidth,$thumbheight) = @_;
1.477     raeburn  1669:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      1670:     my $filepath=$perlvar{'lonDocRoot'};
1.860     raeburn  1671:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 1672:     $file=$fname;
                   1673:     if ($fname=~m|/|) {
                   1674:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   1675: 	$path.=$fnamepath.'/';
                   1676:     }
1.259     www      1677:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      1678:     my $count;
                   1679:     for ($count=4;$count<=$#parts;$count++) {
                   1680:         $filepath.="/$parts[$count]";
                   1681:         if ((-e $filepath)!=1) {
                   1682: 	    mkdir($filepath,0777);
                   1683:         }
                   1684:     }
                   1685: # Save the file
                   1686:     {
1.701     albertel 1687: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   1688: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   1689: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   1690: 	    return '/adm/notfound.html';
                   1691: 	}
                   1692: 	if (!print FH ($env{'form.'.$formname})) {
                   1693: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   1694: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   1695: 	    return '/adm/notfound.html';
                   1696: 	}
1.570     albertel 1697: 	close(FH);
1.258     www      1698:     }
1.637     raeburn  1699:     if ($parser eq 'parse') {
1.638     albertel 1700:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
                   1701: 						   $codebase);
1.637     raeburn  1702:         unless ($parse_result eq 'ok') {
1.638     albertel 1703:             &logthis('Failed to parse '.$filepath.$file.
                   1704: 		     ' for embedded media: '.$parse_result); 
1.637     raeburn  1705:         }
                   1706:     }
1.860     raeburn  1707:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   1708:         my $input = $filepath.'/'.$file;
                   1709:         my $output = $filepath.'/'.'tn-'.$file;
                   1710:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   1711:         system("convert -sample $thumbsize $input $output");
                   1712:         if (-e $filepath.'/'.'tn-'.$file) {
                   1713:             $fetchthumb  = 1; 
                   1714:         }
                   1715:     }
1.858     raeburn  1716:  
1.259     www      1717: # Notify homeserver to grep it
                   1718: #
1.638     albertel 1719:     my $docuhome=&homeserver($docuname,$docudom);
1.494     albertel 1720:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      1721:     if ($fetchresult eq 'ok') {
1.860     raeburn  1722:         if ($fetchthumb) {
                   1723:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   1724:             if ($thumbresult ne 'ok') {
                   1725:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   1726:                          $docuhome.': '.$thumbresult);
                   1727:             }
                   1728:         }
1.259     www      1729: #
1.258     www      1730: # Return the URL to it
1.494     albertel 1731:         return '/uploaded/'.$path.$file;
1.263     www      1732:     } else {
1.494     albertel 1733:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   1734: 		 ': '.$fetchresult);
1.263     www      1735:         return '/adm/notfound.html';
1.858     raeburn  1736:     }
1.493     albertel 1737: }
                   1738: 
1.637     raeburn  1739: sub extract_embedded_items {
1.648     raeburn  1740:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637     raeburn  1741:     my @state = ();
                   1742:     my %javafiles = (
                   1743:                       codebase => '',
                   1744:                       code => '',
                   1745:                       archive => ''
                   1746:                     );
                   1747:     my %mediafiles = (
                   1748:                       src => '',
                   1749:                       movie => '',
                   1750:                      );
1.648     raeburn  1751:     my $p;
                   1752:     if ($content) {
                   1753:         $p = HTML::LCParser->new($content);
                   1754:     } else {
                   1755:         $p = HTML::LCParser->new($filepath.'/'.$file);
                   1756:     }
1.641     albertel 1757:     while (my $t=$p->get_token()) {
1.640     albertel 1758: 	if ($t->[0] eq 'S') {
                   1759: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 1760: 	    push(@state, $tagname);
1.648     raeburn  1761:             if (lc($tagname) eq 'allow') {
                   1762:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   1763:             }
1.640     albertel 1764: 	    if (lc($tagname) eq 'img') {
                   1765: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   1766: 	    }
1.886     albertel 1767: 	    if (lc($tagname) eq 'a') {
                   1768: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   1769: 	    }
1.645     raeburn  1770:             if (lc($tagname) eq 'script') {
                   1771:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   1772:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   1773:                 } else {
                   1774:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   1775:                 }
                   1776:             }
                   1777:             if (lc($tagname) eq 'link') {
                   1778:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   1779:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   1780:                 }
                   1781:             }
1.640     albertel 1782: 	    if (lc($tagname) eq 'object' ||
                   1783: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   1784: 		foreach my $item (keys(%javafiles)) {
                   1785: 		    $javafiles{$item} = '';
                   1786: 		}
                   1787: 	    }
                   1788: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   1789: 		my $name = lc($attr->{'name'});
                   1790: 		foreach my $item (keys(%javafiles)) {
                   1791: 		    if ($name eq $item) {
                   1792: 			$javafiles{$item} = $attr->{'value'};
                   1793: 			last;
                   1794: 		    }
                   1795: 		}
                   1796: 		foreach my $item (keys(%mediafiles)) {
                   1797: 		    if ($name eq $item) {
                   1798: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   1799: 			last;
                   1800: 		    }
                   1801: 		}
                   1802: 	    }
                   1803: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   1804: 		foreach my $item (keys(%javafiles)) {
                   1805: 		    if ($attr->{$item}) {
                   1806: 			$javafiles{$item} = $attr->{$item};
                   1807: 			last;
                   1808: 		    }
                   1809: 		}
                   1810: 		foreach my $item (keys(%mediafiles)) {
                   1811: 		    if ($attr->{$item}) {
                   1812: 			&add_filetype($allfiles,$attr->{$item},$item);
                   1813: 			last;
                   1814: 		    }
                   1815: 		}
                   1816: 	    }
                   1817: 	} elsif ($t->[0] eq 'E') {
                   1818: 	    my ($tagname) = ($t->[1]);
                   1819: 	    if ($javafiles{'codebase'} ne '') {
                   1820: 		$javafiles{'codebase'} .= '/';
                   1821: 	    }  
                   1822: 	    if (lc($tagname) eq 'applet' ||
                   1823: 		lc($tagname) eq 'object' ||
                   1824: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   1825: 		) {
                   1826: 		foreach my $item (keys(%javafiles)) {
                   1827: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   1828: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   1829: 			&add_filetype($allfiles,$file,$item);
                   1830: 		    }
                   1831: 		}
                   1832: 	    } 
                   1833: 	    pop @state;
                   1834: 	}
                   1835:     }
1.637     raeburn  1836:     return 'ok';
                   1837: }
                   1838: 
1.639     albertel 1839: sub add_filetype {
                   1840:     my ($allfiles,$file,$type)=@_;
                   1841:     if (exists($allfiles->{$file})) {
                   1842: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   1843: 	    push(@{$allfiles->{$file}}, &escape($type));
                   1844: 	}
                   1845:     } else {
                   1846: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  1847:     }
                   1848: }
                   1849: 
1.493     albertel 1850: sub removeuploadedurl {
                   1851:     my ($url)=@_;
                   1852:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613     albertel 1853:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 1854: }
                   1855: 
                   1856: sub removeuserfile {
                   1857:     my ($docuname,$docudom,$fname)=@_;
                   1858:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1859:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
                   1860:     if ($result eq 'ok') {
                   1861:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   1862:             my $metafile = $fname.'.meta';
                   1863:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 1864: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
                   1865:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1866:             my $sqlresult = 
1.823     albertel 1867:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1868:                                         'portfolio_metadata',$group,
                   1869:                                         'delete');
1.798     raeburn  1870:         }
                   1871:     }
                   1872:     return $result;
1.257     www      1873: }
1.15      www      1874: 
1.530     albertel 1875: sub mkdiruserfile {
                   1876:     my ($docuname,$docudom,$dir)=@_;
                   1877:     my $home=&homeserver($docuname,$docudom);
                   1878:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   1879: }
                   1880: 
1.531     albertel 1881: sub renameuserfile {
                   1882:     my ($docuname,$docudom,$old,$new)=@_;
                   1883:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1884:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   1885:                         &escape("$old").':'.&escape("$new"),$home);
                   1886:     if ($result eq 'ok') {
                   1887:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   1888:             my $oldmeta = $old.'.meta';
                   1889:             my $newmeta = $new.'.meta';
                   1890:             my $metaresult = 
                   1891:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 1892: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   1893:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1894:             my $sqlresult = 
1.823     albertel 1895:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1896:                                         'portfolio_metadata',$group,
                   1897:                                         'delete');
1.798     raeburn  1898:         }
                   1899:     }
                   1900:     return $result;
1.531     albertel 1901: }
                   1902: 
1.14      www      1903: # ------------------------------------------------------------------------- Log
                   1904: 
                   1905: sub log {
                   1906:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      1907:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      1908: }
                   1909: 
                   1910: # ------------------------------------------------------------------ Course Log
1.352     www      1911: #
                   1912: # This routine flushes several buffers of non-mission-critical nature
                   1913: #
1.157     www      1914: 
                   1915: sub flushcourselogs {
1.352     www      1916:     &logthis('Flushing log buffers');
                   1917: #
                   1918: # course logs
                   1919: # This is a log of all transactions in a course, which can be used
                   1920: # for data mining purposes
                   1921: #
                   1922: # It also collects the courseid database, which lists last transaction
                   1923: # times and course titles for all courseids
                   1924: #
                   1925:     my %courseidbuffer=();
1.800     albertel 1926:     foreach my $crsid (keys %courselogs) {
1.352     www      1927:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      1928: 		          &escape($courselogs{$crsid}),
                   1929: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      1930: 	    delete $courselogs{$crsid};
                   1931:         } else {
                   1932:             &logthis('Failed to flush log buffer for '.$crsid);
                   1933:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 1934:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      1935:                         " exceeded maximum size, deleting.</font>");
                   1936:                delete $courselogs{$crsid};
                   1937:             }
1.352     www      1938:         }
                   1939:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
                   1940:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516     raeburn  1941: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  1942:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.352     www      1943:         } else {
                   1944:            $courseidbuffer{$coursehombuf{$crsid}}=
1.516     raeburn  1945: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  1946:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.571     raeburn  1947:         }
1.191     harris41 1948:     }
1.352     www      1949: #
                   1950: # Write course id database (reverse lookup) to homeserver of courses 
                   1951: # Is used in pickcourse
                   1952: #
1.840     albertel 1953:     foreach my $crs_home (keys(%courseidbuffer)) {
1.844     albertel 1954:         &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1.840     albertel 1955: 		     $crs_home);
1.352     www      1956:     }
                   1957: #
                   1958: # File accesses
                   1959: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   1960: #
1.449     matthew  1961:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  1962:         if ($entry =~ /___count$/) {
                   1963:             my ($dom,$name);
1.807     albertel 1964:             ($dom,$name,undef)=
1.811     albertel 1965: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  1966:             if (! defined($dom) || $dom eq '' || 
                   1967:                 ! defined($name) || $name eq '') {
1.620     albertel 1968:                 my $cid = $env{'request.course.id'};
                   1969:                 $dom  = $env{'request.'.$cid.'.domain'};
                   1970:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  1971:             }
1.450     matthew  1972:             my $value = $accesshash{$entry};
                   1973:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   1974:             my %temphash=($url => $value);
1.449     matthew  1975:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   1976:             if ($result eq 'ok') {
                   1977:                 delete $accesshash{$entry};
                   1978:             } elsif ($result eq 'unknown_cmd') {
                   1979:                 # Target server has old code running on it.
1.450     matthew  1980:                 my %temphash=($entry => $value);
1.449     matthew  1981:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1982:                     delete $accesshash{$entry};
                   1983:                 }
                   1984:             }
                   1985:         } else {
1.811     albertel 1986:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  1987:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  1988:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   1989:                 delete $accesshash{$entry};
                   1990:             }
1.185     www      1991:         }
1.191     harris41 1992:     }
1.352     www      1993: #
                   1994: # Roles
                   1995: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   1996: #
1.800     albertel 1997:     foreach my $entry (keys(%userrolehash)) {
1.351     www      1998:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      1999: 	    split(/\:/,$entry);
                   2000:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      2001:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      2002:                 $rudom,$runame) eq 'ok') {
                   2003: 	    delete $userrolehash{$entry};
                   2004:         }
                   2005:     }
1.662     raeburn  2006: #
                   2007: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   2008: #
                   2009:     my %domrolebuffer = ();
                   2010:     foreach my $entry (keys %domainrolehash) {
                   2011:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
                   2012:         if ($domrolebuffer{$rudom}) {
                   2013:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   2014:                       '='.&escape($domainrolehash{$entry});
                   2015:         } else {
                   2016:             $domrolebuffer{$rudom}.=&escape($entry).
                   2017:                       '='.&escape($domainrolehash{$entry});
                   2018:         }
                   2019:         delete $domainrolehash{$entry};
                   2020:     }
                   2021:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 2022: 	my %servers = &get_servers($dom,'library');
                   2023: 	foreach my $tryserver (keys(%servers)) {
                   2024: 	    unless (&reply('domroleput:'.$dom.':'.
                   2025: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   2026: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   2027: 	    }
1.662     raeburn  2028:         }
                   2029:     }
1.186     www      2030:     $dumpcount++;
1.157     www      2031: }
                   2032: 
                   2033: sub courselog {
                   2034:     my $what=shift;
1.158     www      2035:     $what=time.':'.$what;
1.620     albertel 2036:     unless ($env{'request.course.id'}) { return ''; }
                   2037:     $coursedombuf{$env{'request.course.id'}}=
                   2038:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2039:     $coursenumbuf{$env{'request.course.id'}}=
                   2040:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2041:     $coursehombuf{$env{'request.course.id'}}=
                   2042:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2043:     $coursedescrbuf{$env{'request.course.id'}}=
                   2044:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2045:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2046:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2047:     $courseownerbuf{$env{'request.course.id'}}=
                   2048:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2049:     $coursetypebuf{$env{'request.course.id'}}=
                   2050:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2051:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2052: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2053:     } else {
1.620     albertel 2054: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2055:     }
1.620     albertel 2056:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2057: 	&flushcourselogs();
                   2058:     }
1.158     www      2059: }
                   2060: 
                   2061: sub courseacclog {
                   2062:     my $fnsymb=shift;
1.620     albertel 2063:     unless ($env{'request.course.id'}) { return ''; }
                   2064:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2065:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2066:         $what.=':POST';
1.583     matthew  2067:         # FIXME: Probably ought to escape things....
1.800     albertel 2068: 	foreach my $key (keys(%env)) {
                   2069:             if ($key=~/^form\.(.*)/) {
                   2070: 		$what.=':'.$1.'='.$env{$key};
1.158     www      2071:             }
1.191     harris41 2072:         }
1.583     matthew  2073:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2074:         # FIXME: We should not be depending on a form parameter that someone
                   2075:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2076:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2077:             $what.= ':POST';
                   2078:             # FIXME: Probably ought to escape things....
                   2079:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2080:                                  'crsdiscuss') {
1.620     albertel 2081:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2082:             }
                   2083:         }
1.158     www      2084:     }
                   2085:     &courselog($what);
1.149     www      2086: }
                   2087: 
1.185     www      2088: sub countacc {
                   2089:     my $url=&declutter(shift);
1.458     matthew  2090:     return if (! defined($url) || $url eq '');
1.620     albertel 2091:     unless ($env{'request.course.id'}) { return ''; }
                   2092:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2093:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2094:     $accesshash{$key}++;
1.185     www      2095: }
1.349     www      2096: 
1.361     www      2097: sub linklog {
                   2098:     my ($from,$to)=@_;
                   2099:     $from=&declutter($from);
                   2100:     $to=&declutter($to);
                   2101:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   2102:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   2103: }
                   2104:   
1.349     www      2105: sub userrolelog {
                   2106:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  2107:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  2108:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  2109:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
                   2110:         ($trole=~/^ta/)) {
1.350     www      2111:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2112:        $userrolehash
                   2113:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      2114:                     =$tend.':'.$tstart;
1.662     raeburn  2115:     }
                   2116:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   2117:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   2118:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   2119:         ($trole=~/^sc/)) {
                   2120:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2121:        $domainrolehash
                   2122:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   2123:                     = $tend.':'.$tstart;
                   2124:     }
1.351     www      2125: }
                   2126: 
                   2127: sub get_course_adv_roles {
                   2128:     my $cid=shift;
1.620     albertel 2129:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      2130:     my %coursehash=&coursedescription($cid);
1.470     www      2131:     my %nothide=();
1.800     albertel 2132:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2133: 	$nothide{join(':',split(/[\@\:]/,$user))}=1;
1.470     www      2134:     }
1.351     www      2135:     my %returnhash=();
                   2136:     my %dumphash=
                   2137:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   2138:     my $now=time;
1.800     albertel 2139:     foreach my $entry (keys %dumphash) {
                   2140: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      2141:         if (($tstart) && ($tstart<0)) { next; }
                   2142:         if (($tend) && ($tend<$now)) { next; }
                   2143:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 2144:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 2145: 	if ($username eq '' || $domain eq '') { next; }
1.470     www      2146: 	if ((&privileged($username,$domain)) && 
                   2147: 	    (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 2148: 	if ($role eq 'cr') { next; }
1.351     www      2149:         my $key=&plaintext($role);
                   2150:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
                   2151:         if ($returnhash{$key}) {
                   2152: 	    $returnhash{$key}.=','.$username.':'.$domain;
                   2153:         } else {
                   2154:             $returnhash{$key}=$username.':'.$domain;
                   2155:         }
1.400     www      2156:      }
                   2157:     return %returnhash;
                   2158: }
                   2159: 
                   2160: sub get_my_roles {
1.858     raeburn  2161:     my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
1.620     albertel 2162:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   2163:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.858     raeburn  2164:     my %dumphash;
                   2165:     if ($context eq 'userroles') { 
                   2166:         %dumphash = &dump('roles',$udom,$uname);
                   2167:     } else {
                   2168:         %dumphash=
1.400     www      2169:             &dump('nohist_userroles',$udom,$uname);
1.858     raeburn  2170:     }
1.400     www      2171:     my %returnhash=();
                   2172:     my $now=time;
1.800     albertel 2173:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  2174:         my ($role,$tend,$tstart);
                   2175:         if ($context eq 'userroles') {
                   2176: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   2177:         } else {
                   2178:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   2179:         }
1.400     www      2180:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  2181:         my $status = 'active';
                   2182:         if (($tend) && ($tend<$now)) {
                   2183:             $status = 'previous';
                   2184:         } 
                   2185:         if (($tstart) && ($now<$tstart)) {
                   2186:             $status = 'future';
                   2187:         }
                   2188:         if (ref($types) eq 'ARRAY') {
                   2189:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   2190:                 next;
                   2191:             } 
                   2192:         } else {
                   2193:             if ($status ne 'active') {
                   2194:                 next;
                   2195:             }
                   2196:         }
1.867     raeburn  2197:         my ($rolecode,$username,$domain,$section,$area);
                   2198:         if ($context eq 'userroles') {
                   2199:             ($area,$rolecode) = split(/_/,$entry);
                   2200:             (undef,$domain,$username,$section) = split(/\//,$area);
                   2201:         } else {
                   2202:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   2203:         }
1.832     raeburn  2204:         if (ref($roledoms) eq 'ARRAY') {
                   2205:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   2206:                 next;
                   2207:             }
                   2208:         }
                   2209:         if (ref($roles) eq 'ARRAY') {
                   2210:             if (!grep(/^\Q$role\E$/,@{$roles})) {
                   2211:                 next;
                   2212:             }
1.867     raeburn  2213:         }
1.400     www      2214: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.832     raeburn  2215:     }
1.373     www      2216:     return %returnhash;
1.399     www      2217: }
                   2218: 
                   2219: # ----------------------------------------------------- Frontpage Announcements
                   2220: #
                   2221: #
                   2222: 
                   2223: sub postannounce {
                   2224:     my ($server,$text)=@_;
1.844     albertel 2225:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      2226:     unless ($text=~/\w/) { $text=''; }
                   2227:     return &reply('setannounce:'.&escape($text),$server);
                   2228: }
                   2229: 
                   2230: sub getannounce {
1.448     albertel 2231: 
                   2232:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      2233: 	my $announcement='';
1.800     albertel 2234: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 2235: 	close($fh);
1.399     www      2236: 	if ($announcement=~/\w/) { 
                   2237: 	    return 
                   2238:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 2239:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      2240: 	} else {
                   2241: 	    return '';
                   2242: 	}
                   2243:     } else {
                   2244: 	return '';
                   2245:     }
1.351     www      2246: }
1.353     www      2247: 
                   2248: # ---------------------------------------------------------- Course ID routines
                   2249: # Deal with domain's nohist_courseid.db files
                   2250: #
                   2251: 
                   2252: sub courseidput {
                   2253:     my ($domain,$what,$coursehome)=@_;
                   2254:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   2255: }
                   2256: 
                   2257: sub courseiddump {
1.791     raeburn  2258:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
1.353     www      2259:     my %returnhash=();
1.355     www      2260:     unless ($domfilter) { $domfilter=''; }
1.845     albertel 2261:     my %libserv = &all_library();
                   2262:     foreach my $tryserver (keys(%libserv)) {
                   2263:         if ( (  $hostidflag == 1 
                   2264: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   2265: 	     || (!defined($hostidflag)) ) {
                   2266: 
                   2267: 	    if ($domfilter eq ''
                   2268: 		|| (&host_domain($tryserver) eq $domfilter)) {
1.800     albertel 2269: 	        foreach my $line (
1.844     albertel 2270:                  split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
1.571     raeburn  2271: 			       $sincefilter.':'.&escape($descfilter).':'.
1.791     raeburn  2272:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
1.354     www      2273:                                $tryserver))) {
1.800     albertel 2274: 		    my ($key,$value)=split(/\=/,$line,2);
1.506     raeburn  2275:                     if (($key) && ($value)) {
1.516     raeburn  2276: 		        $returnhash{&unescape($key)}=$value;
1.506     raeburn  2277:                     }
1.353     www      2278:                 }
                   2279:             }
                   2280:         }
                   2281:     }
                   2282:     return %returnhash;
                   2283: }
                   2284: 
1.658     raeburn  2285: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  2286: 
                   2287: sub dcmailput {
1.685     raeburn  2288:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  2289:     my $status = &Apache::lonnet::critical(
1.740     www      2290:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   2291:        &escape($message),$server);
1.662     raeburn  2292:     return $status;
                   2293: }
                   2294: 
1.658     raeburn  2295: sub dcmaildump {
                   2296:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  2297:     my %returnhash=();
1.846     albertel 2298: 
                   2299:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  2300:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   2301:                                                          &escape($enddate).':';
                   2302: 	my @esc_senders=map { &escape($_)} @$senders;
                   2303: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 2304: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 2305:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  2306:             if (($key) && ($value)) {
                   2307:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  2308:             }
                   2309:         }
                   2310:     }
                   2311:     return %returnhash;
                   2312: }
1.662     raeburn  2313: # ---------------------------------------------------------- Domain roles
                   2314: 
                   2315: sub get_domain_roles {
                   2316:     my ($dom,$roles,$startdate,$enddate)=@_;
                   2317:     if (undef($startdate) || $startdate eq '') {
                   2318:         $startdate = '.';
                   2319:     }
                   2320:     if (undef($enddate) || $enddate eq '') {
                   2321:         $enddate = '.';
                   2322:     }
                   2323:     my $rolelist = join(':',@{$roles});
                   2324:     my %personnel = ();
1.841     albertel 2325: 
                   2326:     my %servers = &get_servers($dom,'library');
                   2327:     foreach my $tryserver (keys(%servers)) {
                   2328: 	%{$personnel{$tryserver}}=();
                   2329: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   2330: 					    &escape($startdate).':'.
                   2331: 					    &escape($enddate).':'.
                   2332: 					    &escape($rolelist), $tryserver))) {
                   2333: 	    my ($key,$value) = split(/\=/,$line,2);
                   2334: 	    if (($key) && ($value)) {
                   2335: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   2336: 	    }
                   2337: 	}
1.662     raeburn  2338:     }
                   2339:     return %personnel;
                   2340: }
1.658     raeburn  2341: 
1.149     www      2342: # ----------------------------------------------------------- Check out an item
                   2343: 
1.504     albertel 2344: sub get_first_access {
                   2345:     my ($type,$argsymb)=@_;
1.790     albertel 2346:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2347:     if ($argsymb) { $symb=$argsymb; }
                   2348:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2349:     if ($type eq 'map') {
                   2350: 	$res=&symbread($map);
                   2351:     } else {
                   2352: 	$res=$symb;
                   2353:     }
                   2354:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   2355:     return $times{"$courseid\0$res"};
1.504     albertel 2356: }
                   2357: 
                   2358: sub set_first_access {
                   2359:     my ($type)=@_;
1.790     albertel 2360:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2361:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2362:     if ($type eq 'map') {
                   2363: 	$res=&symbread($map);
                   2364:     } else {
                   2365: 	$res=$symb;
                   2366:     }
                   2367:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 2368:     if (!$firstaccess) {
1.588     albertel 2369: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 2370:     }
                   2371:     return 'already_set';
1.504     albertel 2372: }
                   2373: 
1.149     www      2374: sub checkout {
                   2375:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   2376:     my $now=time;
                   2377:     my $lonhost=$perlvar{'lonHostID'};
                   2378:     my $infostr=&escape(
1.234     www      2379:                  'CHECKOUTTOKEN&'.
1.149     www      2380:                  $tuname.'&'.
                   2381:                  $tudom.'&'.
                   2382:                  $tcrsid.'&'.
                   2383:                  $symb.'&'.
                   2384: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   2385:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      2386:     if ($token=~/^error\:/) { 
1.672     albertel 2387:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2388:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2389:                  "</font>");
                   2390:         return ''; 
                   2391:     }
                   2392: 
1.149     www      2393:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   2394:     $token=~tr/a-z/A-Z/;
                   2395: 
1.153     www      2396:     my %infohash=('resource.0.outtoken' => $token,
                   2397:                   'resource.0.checkouttime' => $now,
                   2398:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      2399: 
                   2400:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2401:        return '';
1.151     www      2402:     } else {
1.672     albertel 2403:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2404:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2405:                  "</font>");
1.149     www      2406:     }    
                   2407: 
                   2408:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2409:                          &escape('Checkout '.$infostr.' - '.
                   2410:                                                  $token)) ne 'ok') {
                   2411: 	return '';
1.151     www      2412:     } else {
1.672     albertel 2413:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2414:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2415:                  "</font>");
1.149     www      2416:     }
1.151     www      2417:     return $token;
1.149     www      2418: }
                   2419: 
                   2420: # ------------------------------------------------------------ Check in an item
                   2421: 
                   2422: sub checkin {
                   2423:     my $token=shift;
1.150     www      2424:     my $now=time;
                   2425:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   2426:     $lonhost=~tr/A-Z/a-z/;
1.838     albertel 2427:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150     www      2428:     $dtoken=~s/\W/\_/g;
1.234     www      2429:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      2430:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   2431: 
1.154     www      2432:     unless (($tuname) && ($tudom)) {
                   2433:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   2434:         return '';
                   2435:     }
                   2436:     
                   2437:     unless (&allowed('mgr',$tcrsid)) {
                   2438:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620     albertel 2439:                  $env{'user.name'}.' - '.$env{'user.domain'});
1.154     www      2440:         return '';
                   2441:     }
                   2442: 
1.153     www      2443:     my %infohash=('resource.0.intoken' => $token,
                   2444:                   'resource.0.checkintime' => $now,
                   2445:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      2446: 
                   2447:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2448:        return '';
                   2449:     }    
                   2450: 
                   2451:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2452:                          &escape('Checkin - '.$token)) ne 'ok') {
                   2453: 	return '';
                   2454:     }
                   2455: 
                   2456:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      2457: }
                   2458: 
                   2459: # --------------------------------------------- Set Expire Date for Spreadsheet
                   2460: 
                   2461: sub expirespread {
                   2462:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 2463:     my $cid=$env{'request.course.id'}; 
1.110     www      2464:     if ($cid) {
                   2465:        my $now=time;
                   2466:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 2467:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   2468:                             $env{'course.'.$cid.'.num'}.
1.110     www      2469: 	        	    ':nohist_expirationdates:'.
                   2470:                             &escape($key).'='.$now,
1.620     albertel 2471:                             $env{'course.'.$cid.'.home'})
1.110     www      2472:     }
                   2473:     return 'ok';
1.14      www      2474: }
                   2475: 
1.109     www      2476: # ----------------------------------------------------- Devalidate Spreadsheets
                   2477: 
                   2478: sub devalidate {
1.325     www      2479:     my ($symb,$uname,$udom)=@_;
1.620     albertel 2480:     my $cid=$env{'request.course.id'}; 
1.109     www      2481:     if ($cid) {
1.391     matthew  2482:         # delete the stored spreadsheets for
                   2483:         # - the student level sheet of this user in course's homespace
                   2484:         # - the assessment level sheet for this resource 
                   2485:         #   for this user in user's homespace
1.553     albertel 2486: 	# - current conditional state info
1.325     www      2487: 	my $key=$uname.':'.$udom.':';
1.109     www      2488:         my $status=
1.299     matthew  2489: 	    &del('nohist_calculatedsheets',
1.391     matthew  2490: 		 [$key.'studentcalc:'],
1.620     albertel 2491: 		 $env{'course.'.$cid.'.domain'},
                   2492: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 2493: 		.' '.
                   2494: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  2495: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      2496:         unless ($status eq 'ok ok') {
                   2497:            &logthis('Could not devalidate spreadsheet '.
1.325     www      2498:                     $uname.' at '.$udom.' for '.
1.109     www      2499: 		    $symb.': '.$status);
1.133     albertel 2500:         }
1.553     albertel 2501: 	&delenv('user.state.'.$cid);
1.109     www      2502:     }
                   2503: }
                   2504: 
1.265     albertel 2505: sub get_scalar {
                   2506:     my ($string,$end) = @_;
                   2507:     my $value;
                   2508:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   2509: 	$value = $1;
                   2510:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   2511: 	$value = $1;
                   2512:     }
                   2513:     return &unescape($value);
                   2514: }
                   2515: 
                   2516: sub array2str {
                   2517:   my (@array) = @_;
                   2518:   my $result=&arrayref2str(\@array);
                   2519:   $result=~s/^__ARRAY_REF__//;
                   2520:   $result=~s/__END_ARRAY_REF__$//;
                   2521:   return $result;
                   2522: }
                   2523: 
1.204     albertel 2524: sub arrayref2str {
                   2525:   my ($arrayref) = @_;
1.265     albertel 2526:   my $result='__ARRAY_REF__';
1.204     albertel 2527:   foreach my $elem (@$arrayref) {
1.265     albertel 2528:     if(ref($elem) eq 'ARRAY') {
                   2529:       $result.=&arrayref2str($elem).'&';
                   2530:     } elsif(ref($elem) eq 'HASH') {
                   2531:       $result.=&hashref2str($elem).'&';
                   2532:     } elsif(ref($elem)) {
                   2533:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 2534:     } else {
                   2535:       $result.=&escape($elem).'&';
                   2536:     }
                   2537:   }
                   2538:   $result=~s/\&$//;
1.265     albertel 2539:   $result .= '__END_ARRAY_REF__';
1.204     albertel 2540:   return $result;
                   2541: }
                   2542: 
1.168     albertel 2543: sub hash2str {
1.204     albertel 2544:   my (%hash) = @_;
                   2545:   my $result=&hashref2str(\%hash);
1.265     albertel 2546:   $result=~s/^__HASH_REF__//;
                   2547:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 2548:   return $result;
                   2549: }
                   2550: 
                   2551: sub hashref2str {
                   2552:   my ($hashref)=@_;
1.265     albertel 2553:   my $result='__HASH_REF__';
1.800     albertel 2554:   foreach my $key (sort(keys(%$hashref))) {
                   2555:     if (ref($key) eq 'ARRAY') {
                   2556:       $result.=&arrayref2str($key).'=';
                   2557:     } elsif (ref($key) eq 'HASH') {
                   2558:       $result.=&hashref2str($key).'=';
                   2559:     } elsif (ref($key)) {
1.265     albertel 2560:       $result.='=';
1.800     albertel 2561:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 2562:     } else {
1.800     albertel 2563: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 2564:     }
                   2565: 
1.800     albertel 2566:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   2567:       $result.=&arrayref2str($hashref->{$key}).'&';
                   2568:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   2569:       $result.=&hashref2str($hashref->{$key}).'&';
                   2570:     } elsif(ref($hashref->{$key})) {
1.265     albertel 2571:        $result.='&';
1.800     albertel 2572:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 2573:     } else {
1.800     albertel 2574:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 2575:     }
                   2576:   }
1.168     albertel 2577:   $result=~s/\&$//;
1.265     albertel 2578:   $result .= '__END_HASH_REF__';
1.168     albertel 2579:   return $result;
                   2580: }
                   2581: 
                   2582: sub str2hash {
1.265     albertel 2583:     my ($string)=@_;
                   2584:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   2585:     return %$hash;
                   2586: }
                   2587: 
                   2588: sub str2hashref {
1.168     albertel 2589:   my ($string) = @_;
1.265     albertel 2590: 
                   2591:   my %hash;
                   2592: 
                   2593:   if($string !~ /^__HASH_REF__/) {
                   2594:       if (! ($string eq '' || !defined($string))) {
                   2595: 	  $hash{'error'}='Not hash reference';
                   2596:       }
                   2597:       return (\%hash, $string);
                   2598:   }
                   2599: 
                   2600:   $string =~ s/^__HASH_REF__//;
                   2601: 
                   2602:   while($string !~ /^__END_HASH_REF__/) {
                   2603:       #key
                   2604:       my $key='';
                   2605:       if($string =~ /^__HASH_REF__/) {
                   2606:           ($key, $string)=&str2hashref($string);
                   2607:           if(defined($key->{'error'})) {
                   2608:               $hash{'error'}='Bad data';
                   2609:               return (\%hash, $string);
                   2610:           }
                   2611:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2612:           ($key, $string)=&str2arrayref($string);
                   2613:           if($key->[0] eq 'Array reference error') {
                   2614:               $hash{'error'}='Bad data';
                   2615:               return (\%hash, $string);
                   2616:           }
                   2617:       } else {
                   2618:           $string =~ s/^(.*?)=//;
1.267     albertel 2619: 	  $key=&unescape($1);
1.265     albertel 2620:       }
                   2621:       $string =~ s/^=//;
                   2622: 
                   2623:       #value
                   2624:       my $value='';
                   2625:       if($string =~ /^__HASH_REF__/) {
                   2626:           ($value, $string)=&str2hashref($string);
                   2627:           if(defined($value->{'error'})) {
                   2628:               $hash{'error'}='Bad data';
                   2629:               return (\%hash, $string);
                   2630:           }
                   2631:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2632:           ($value, $string)=&str2arrayref($string);
                   2633:           if($value->[0] eq 'Array reference error') {
                   2634:               $hash{'error'}='Bad data';
                   2635:               return (\%hash, $string);
                   2636:           }
                   2637:       } else {
                   2638: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   2639:       }
                   2640:       $string =~ s/^&//;
                   2641: 
                   2642:       $hash{$key}=$value;
1.204     albertel 2643:   }
1.265     albertel 2644: 
                   2645:   $string =~ s/^__END_HASH_REF__//;
                   2646: 
                   2647:   return (\%hash, $string);
1.204     albertel 2648: }
                   2649: 
                   2650: sub str2array {
1.265     albertel 2651:     my ($string)=@_;
                   2652:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   2653:     return @$array;
                   2654: }
                   2655: 
                   2656: sub str2arrayref {
1.204     albertel 2657:   my ($string) = @_;
1.265     albertel 2658:   my @array;
                   2659: 
                   2660:   if($string !~ /^__ARRAY_REF__/) {
                   2661:       if (! ($string eq '' || !defined($string))) {
                   2662: 	  $array[0]='Array reference error';
                   2663:       }
                   2664:       return (\@array, $string);
                   2665:   }
                   2666: 
                   2667:   $string =~ s/^__ARRAY_REF__//;
                   2668: 
                   2669:   while($string !~ /^__END_ARRAY_REF__/) {
                   2670:       my $value='';
                   2671:       if($string =~ /^__HASH_REF__/) {
                   2672:           ($value, $string)=&str2hashref($string);
                   2673:           if(defined($value->{'error'})) {
                   2674:               $array[0] ='Array reference error';
                   2675:               return (\@array, $string);
                   2676:           }
                   2677:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2678:           ($value, $string)=&str2arrayref($string);
                   2679:           if($value->[0] eq 'Array reference error') {
                   2680:               $array[0] ='Array reference error';
                   2681:               return (\@array, $string);
                   2682:           }
                   2683:       } else {
                   2684: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   2685:       }
                   2686:       $string =~ s/^&//;
                   2687: 
                   2688:       push(@array, $value);
1.191     harris41 2689:   }
1.265     albertel 2690: 
                   2691:   $string =~ s/^__END_ARRAY_REF__//;
                   2692: 
                   2693:   return (\@array, $string);
1.168     albertel 2694: }
                   2695: 
1.167     albertel 2696: # -------------------------------------------------------------------Temp Store
                   2697: 
1.168     albertel 2698: sub tmpreset {
                   2699:   my ($symb,$namespace,$domain,$stuname) = @_;
                   2700:   if (!$symb) {
                   2701:     $symb=&symbread();
1.620     albertel 2702:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2703:   }
                   2704:   $symb=escape($symb);
                   2705: 
1.620     albertel 2706:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 2707:   $namespace=~s/\//\_/g;
                   2708:   $namespace=~s/\W//g;
                   2709: 
1.620     albertel 2710:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2711:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2712:   if ($domain eq 'public' && $stuname eq 'public') {
                   2713:       $stuname=$ENV{'REMOTE_ADDR'};
                   2714:   }
1.168     albertel 2715:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2716:   my %hash;
                   2717:   if (tie(%hash,'GDBM_File',
                   2718: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2719: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2720:     foreach my $key (keys %hash) {
1.180     albertel 2721:       if ($key=~ /:$symb/) {
1.168     albertel 2722: 	delete($hash{$key});
                   2723:       }
                   2724:     }
                   2725:   }
                   2726: }
                   2727: 
1.167     albertel 2728: sub tmpstore {
1.168     albertel 2729:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2730: 
                   2731:   if (!$symb) {
                   2732:     $symb=&symbread();
1.620     albertel 2733:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2734:   }
                   2735:   $symb=escape($symb);
                   2736: 
                   2737:   if (!$namespace) {
                   2738:     # I don't think we would ever want to store this for a course.
                   2739:     # it seems this will only be used if we don't have a course.
1.620     albertel 2740:     #$namespace=$env{'request.course.id'};
1.168     albertel 2741:     #if (!$namespace) {
1.620     albertel 2742:       $namespace=$env{'request.state'};
1.168     albertel 2743:     #}
                   2744:   }
                   2745:   $namespace=~s/\//\_/g;
                   2746:   $namespace=~s/\W//g;
1.620     albertel 2747:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2748:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2749:   if ($domain eq 'public' && $stuname eq 'public') {
                   2750:       $stuname=$ENV{'REMOTE_ADDR'};
                   2751:   }
1.168     albertel 2752:   my $now=time;
                   2753:   my %hash;
                   2754:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2755:   if (tie(%hash,'GDBM_File',
                   2756: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2757: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2758:     $hash{"version:$symb"}++;
                   2759:     my $version=$hash{"version:$symb"};
                   2760:     my $allkeys=''; 
                   2761:     foreach my $key (keys(%$storehash)) {
                   2762:       $allkeys.=$key.':';
1.591     albertel 2763:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 2764:     }
                   2765:     $hash{"$version:$symb:timestamp"}=$now;
                   2766:     $allkeys.='timestamp';
                   2767:     $hash{"$version:keys:$symb"}=$allkeys;
                   2768:     if (untie(%hash)) {
                   2769:       return 'ok';
                   2770:     } else {
                   2771:       return "error:$!";
                   2772:     }
                   2773:   } else {
                   2774:     return "error:$!";
                   2775:   }
                   2776: }
1.167     albertel 2777: 
1.168     albertel 2778: # -----------------------------------------------------------------Temp Restore
1.167     albertel 2779: 
1.168     albertel 2780: sub tmprestore {
                   2781:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 2782: 
1.168     albertel 2783:   if (!$symb) {
                   2784:     $symb=&symbread();
1.620     albertel 2785:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2786:   }
                   2787:   $symb=escape($symb);
                   2788: 
1.620     albertel 2789:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 2790: 
1.620     albertel 2791:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2792:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2793:   if ($domain eq 'public' && $stuname eq 'public') {
                   2794:       $stuname=$ENV{'REMOTE_ADDR'};
                   2795:   }
1.168     albertel 2796:   my %returnhash;
                   2797:   $namespace=~s/\//\_/g;
                   2798:   $namespace=~s/\W//g;
                   2799:   my %hash;
                   2800:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2801:   if (tie(%hash,'GDBM_File',
                   2802: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2803: 	  &GDBM_READER(),0640)) {
1.168     albertel 2804:     my $version=$hash{"version:$symb"};
                   2805:     $returnhash{'version'}=$version;
                   2806:     my $scope;
                   2807:     for ($scope=1;$scope<=$version;$scope++) {
                   2808:       my $vkeys=$hash{"$scope:keys:$symb"};
                   2809:       my @keys=split(/:/,$vkeys);
                   2810:       my $key;
                   2811:       $returnhash{"$scope:keys"}=$vkeys;
                   2812:       foreach $key (@keys) {
1.591     albertel 2813: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   2814: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 2815:       }
                   2816:     }
1.168     albertel 2817:     if (!(untie(%hash))) {
                   2818:       return "error:$!";
                   2819:     }
                   2820:   } else {
                   2821:     return "error:$!";
                   2822:   }
                   2823:   return %returnhash;
1.167     albertel 2824: }
                   2825: 
1.9       www      2826: # ----------------------------------------------------------------------- Store
                   2827: 
                   2828: sub store {
1.124     www      2829:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2830:     my $home='';
                   2831: 
1.168     albertel 2832:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2833: 
1.213     www      2834:     $symb=&symbclean($symb);
1.122     albertel 2835:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2836: 
1.620     albertel 2837:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2838:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2839: 
                   2840:     &devalidate($symb,$stuname,$domain);
1.109     www      2841: 
                   2842:     $symb=escape($symb);
1.187     www      2843:     if (!$namespace) { 
1.620     albertel 2844:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2845:           return ''; 
                   2846:        } 
                   2847:     }
1.620     albertel 2848:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2849: 
                   2850:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2851:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2852: 
1.12      www      2853:     my $namevalue='';
1.800     albertel 2854:     foreach my $key (keys(%$storehash)) {
                   2855:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2856:     }
1.12      www      2857:     $namevalue=~s/\&$//;
1.187     www      2858:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      2859:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      2860: }
                   2861: 
1.47      www      2862: # -------------------------------------------------------------- Critical Store
                   2863: 
                   2864: sub cstore {
1.124     www      2865:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2866:     my $home='';
                   2867: 
1.168     albertel 2868:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2869: 
1.213     www      2870:     $symb=&symbclean($symb);
1.122     albertel 2871:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2872: 
1.620     albertel 2873:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2874:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2875: 
                   2876:     &devalidate($symb,$stuname,$domain);
1.109     www      2877: 
                   2878:     $symb=escape($symb);
1.187     www      2879:     if (!$namespace) { 
1.620     albertel 2880:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2881:           return ''; 
                   2882:        } 
                   2883:     }
1.620     albertel 2884:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2885: 
                   2886:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2887:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 2888: 
1.47      www      2889:     my $namevalue='';
1.800     albertel 2890:     foreach my $key (keys(%$storehash)) {
                   2891:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2892:     }
1.47      www      2893:     $namevalue=~s/\&$//;
1.187     www      2894:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      2895:     return critical
                   2896:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      2897: }
                   2898: 
1.9       www      2899: # --------------------------------------------------------------------- Restore
                   2900: 
                   2901: sub restore {
1.124     www      2902:     my ($symb,$namespace,$domain,$stuname) = @_;
                   2903:     my $home='';
                   2904: 
1.168     albertel 2905:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2906: 
1.122     albertel 2907:     if (!$symb) {
                   2908:       unless ($symb=escape(&symbread())) { return ''; }
                   2909:     } else {
1.213     www      2910:       $symb=&escape(&symbclean($symb));
1.122     albertel 2911:     }
1.188     www      2912:     if (!$namespace) { 
1.620     albertel 2913:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      2914:           return ''; 
                   2915:        } 
                   2916:     }
1.620     albertel 2917:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2918:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   2919:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 2920:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   2921: 
1.12      www      2922:     my %returnhash=();
1.800     albertel 2923:     foreach my $line (split(/\&/,$answer)) {
                   2924: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 2925:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 2926:     }
1.75      www      2927:     my $version;
                   2928:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 2929:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   2930:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 2931:        }
1.75      www      2932:     }
1.13      www      2933:     return %returnhash;
1.34      www      2934: }
                   2935: 
                   2936: # ---------------------------------------------------------- Course Description
                   2937: 
                   2938: sub coursedescription {
1.731     albertel 2939:     my ($courseid,$args)=@_;
1.34      www      2940:     $courseid=~s/^\///;
1.49      www      2941:     $courseid=~s/\_/\//g;
1.34      www      2942:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 2943:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 2944:     my $normalid=$cdomain.'_'.$cnum;
                   2945:     # need to always cache even if we get errors otherwise we keep 
                   2946:     # trying and trying and trying to get the course description.
                   2947:     my %envhash=();
                   2948:     my %returnhash=();
1.731     albertel 2949:     
                   2950:     my $expiretime=600;
                   2951:     if ($env{'request.course.id'} eq $normalid) {
                   2952: 	$expiretime=120;
                   2953:     }
                   2954: 
                   2955:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   2956:     if (!$args->{'freshen_cache'}
                   2957: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   2958: 	foreach my $key (keys(%env)) {
                   2959: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   2960: 	    my ($setting) = $1;
                   2961: 	    $returnhash{$setting} = $env{$key};
                   2962: 	}
                   2963: 	return %returnhash;
                   2964:     }
                   2965: 
                   2966:     # get the data agin
                   2967:     if (!$args->{'one_time'}) {
                   2968: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   2969:     }
1.811     albertel 2970: 
1.34      www      2971:     if ($chome ne 'no_host') {
1.302     albertel 2972:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 2973:        if (!exists($returnhash{'con_lost'})) {
                   2974:            $returnhash{'home'}= $chome;
                   2975: 	   $returnhash{'domain'} = $cdomain;
                   2976: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  2977:            if (!defined($returnhash{'type'})) {
                   2978:                $returnhash{'type'} = 'Course';
                   2979:            }
1.130     albertel 2980:            while (my ($name,$value) = each %returnhash) {
1.53      www      2981:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 2982:            }
1.270     www      2983:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      2984:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 2985: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      2986:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   2987:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   2988:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      2989:        }
                   2990:     }
1.731     albertel 2991:     if (!$args->{'one_time'}) {
                   2992: 	&appenv(%envhash);
                   2993:     }
1.302     albertel 2994:     return %returnhash;
1.461     www      2995: }
                   2996: 
                   2997: # -------------------------------------------------See if a user is privileged
                   2998: 
                   2999: sub privileged {
                   3000:     my ($username,$domain)=@_;
                   3001:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   3002: 			&homeserver($username,$domain));
                   3003:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   3004:     my $now=time;
                   3005:     if ($rolesdump ne '') {
1.800     albertel 3006:         foreach my $entry (split(/&/,$rolesdump)) {
                   3007: 	    if ($entry!~/^rolesdef_/) {
                   3008: 		my ($area,$role)=split(/=/,$entry);
1.461     www      3009: 		$area=~s/\_\w\w$//;
                   3010: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   3011: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   3012: 		    my $active=1;
                   3013: 		    if ($tend) {
                   3014: 			if ($tend<$now) { $active=0; }
                   3015: 		    }
                   3016: 		    if ($tstart) {
                   3017: 			if ($tstart>$now) { $active=0; }
                   3018: 		    }
                   3019: 		    if ($active) { return 1; }
                   3020: 		}
                   3021: 	    }
                   3022: 	}
                   3023:     }
                   3024:     return 0;
1.9       www      3025: }
1.1       albertel 3026: 
1.103     harris41 3027: # -------------------------------------------------------- Get user privileges
1.11      www      3028: 
                   3029: sub rolesinit {
                   3030:     my ($domain,$username,$authhost)=@_;
                   3031:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      3032:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      3033:     my %allroles=();
1.678     raeburn  3034:     my %allgroups=();   
1.11      www      3035:     my $now=time;
1.743     albertel 3036:     my %userroles = ('user.login.time' => $now);
1.678     raeburn  3037:     my $group_privs;
1.11      www      3038: 
                   3039:     if ($rolesdump ne '') {
1.800     albertel 3040:         foreach my $entry (split(/&/,$rolesdump)) {
                   3041: 	  if ($entry!~/^rolesdef_/) {
                   3042:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 3043: 	    $area=~s/\_\w\w$//;
1.678     raeburn  3044:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 3045: 	    if ($role=~/^cr/) { 
1.807     albertel 3046: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   3047: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 3048: 		    ($tend,$tstart)=split('_',$trest);
                   3049: 		} else {
                   3050: 		    $trole=$role;
                   3051: 		}
1.678     raeburn  3052:             } elsif ($role =~ m|^gr/|) {
                   3053:                 ($trole,$tend,$tstart) = split(/_/,$role);
                   3054:                 ($trole,$group_privs) = split(/\//,$trole);
                   3055:                 $group_privs = &unescape($group_privs);
1.587     albertel 3056: 	    } else {
                   3057: 		($trole,$tend,$tstart)=split(/_/,$role);
                   3058: 	    }
1.743     albertel 3059: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   3060: 					 $username);
                   3061: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  3062:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   3063:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      3064:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 3065: 		my $spec=$trole.'.'.$area;
                   3066: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3067: 		if ($trole =~ /^cr\//) {
1.567     raeburn  3068:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  3069:                 } elsif ($trole eq 'gr') {
                   3070:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 3071: 		} else {
1.567     raeburn  3072:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 3073: 		}
1.12      www      3074:             }
1.662     raeburn  3075:           }
1.191     harris41 3076:         }
1.743     albertel 3077:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   3078:         $userroles{'user.adv'}    = $adv;
                   3079: 	$userroles{'user.author'} = $author;
1.620     albertel 3080:         $env{'user.adv'}=$adv;
1.11      www      3081:     }
1.743     albertel 3082:     return \%userroles;  
1.11      www      3083: }
                   3084: 
1.567     raeburn  3085: sub set_arearole {
                   3086:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   3087: # log the associated role with the area
                   3088:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 3089:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  3090: }
                   3091: 
                   3092: sub custom_roleprivs {
                   3093:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   3094:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   3095:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 3096:     if (&hostname($homsvr) ne '') {
1.567     raeburn  3097:         my ($rdummy,$roledef)=
                   3098:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   3099:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   3100:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   3101:             if (defined($syspriv)) {
                   3102:                 $$allroles{'cm./'}.=':'.$syspriv;
                   3103:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   3104:             }
                   3105:             if ($tdomain ne '') {
                   3106:                 if (defined($dompriv)) {
                   3107:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   3108:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   3109:                 }
                   3110:                 if (($trest ne '') && (defined($coursepriv))) {
                   3111:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   3112:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   3113:                 }
                   3114:             }
                   3115:         }
                   3116:     }
                   3117: }
                   3118: 
1.678     raeburn  3119: sub group_roleprivs {
                   3120:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   3121:     my $access = 1;
                   3122:     my $now = time;
                   3123:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   3124:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   3125:     if ($access) {
1.811     albertel 3126:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  3127:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   3128:     }
                   3129: }
1.567     raeburn  3130: 
                   3131: sub standard_roleprivs {
                   3132:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   3133:     if (defined($pr{$trole.':s'})) {
                   3134:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   3135:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   3136:     }
                   3137:     if ($tdomain ne '') {
                   3138:         if (defined($pr{$trole.':d'})) {
                   3139:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3140:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3141:         }
                   3142:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   3143:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   3144:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   3145:         }
                   3146:     }
                   3147: }
                   3148: 
                   3149: sub set_userprivs {
1.678     raeburn  3150:     my ($userroles,$allroles,$allgroups) = @_; 
1.567     raeburn  3151:     my $author=0;
                   3152:     my $adv=0;
1.678     raeburn  3153:     my %grouproles = ();
                   3154:     if (keys(%{$allgroups}) > 0) {
                   3155:         foreach my $role (keys %{$allroles}) {
1.681     raeburn  3156:             my ($trole,$area,$sec,$extendedarea);
1.881     raeburn  3157:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
1.678     raeburn  3158:                 $trole = $1;
                   3159:                 $area = $2;
1.681     raeburn  3160:                 $sec = $3;
                   3161:                 $extendedarea = $area.$sec;
                   3162:                 if (exists($$allgroups{$area})) {
                   3163:                     foreach my $group (keys(%{$$allgroups{$area}})) {
                   3164:                         my $spec = $trole.'.'.$extendedarea;
                   3165:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
                   3166:                                                 $$allgroups{$area}{$group};
1.678     raeburn  3167:                     }
                   3168:                 }
                   3169:             }
                   3170:         }
                   3171:     }
1.800     albertel 3172:     foreach my $group (keys(%grouproles)) {
                   3173:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  3174:     }
1.800     albertel 3175:     foreach my $role (keys(%{$allroles})) {
                   3176:         my %thesepriv;
                   3177:         if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
                   3178:         foreach my $item (split(/:/,$$allroles{$role})) {
                   3179:             if ($item ne '') {
                   3180:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  3181:                 if ($restrictions eq '') {
                   3182:                     $thesepriv{$privilege}='F';
                   3183:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   3184:                     $thesepriv{$privilege}.=$restrictions;
                   3185:                 }
                   3186:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   3187:             }
                   3188:         }
                   3189:         my $thesestr='';
1.800     albertel 3190:         foreach my $priv (keys(%thesepriv)) {
                   3191: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   3192: 	}
                   3193:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  3194:     }
                   3195:     return ($author,$adv);
                   3196: }
                   3197: 
1.12      www      3198: # --------------------------------------------------------------- get interface
                   3199: 
                   3200: sub get {
1.131     albertel 3201:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3202:    my $items='';
1.800     albertel 3203:    foreach my $item (@$storearr) {
                   3204:        $items.=&escape($item).'&';
1.191     harris41 3205:    }
1.12      www      3206:    $items=~s/\&$//;
1.620     albertel 3207:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3208:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 3209:    my $uhome=&homeserver($uname,$udomain);
                   3210: 
1.133     albertel 3211:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3212:    my @pairs=split(/\&/,$rep);
1.273     albertel 3213:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   3214:      return @pairs;
                   3215:    }
1.15      www      3216:    my %returnhash=();
1.42      www      3217:    my $i=0;
1.800     albertel 3218:    foreach my $item (@$storearr) {
                   3219:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3220:       $i++;
1.191     harris41 3221:    }
1.15      www      3222:    return %returnhash;
1.27      www      3223: }
                   3224: 
                   3225: # --------------------------------------------------------------- del interface
                   3226: 
                   3227: sub del {
1.133     albertel 3228:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      3229:    my $items='';
1.800     albertel 3230:    foreach my $item (@$storearr) {
                   3231:        $items.=&escape($item).'&';
1.191     harris41 3232:    }
1.27      www      3233:    $items=~s/\&$//;
1.620     albertel 3234:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3235:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3236:    my $uhome=&homeserver($uname,$udomain);
                   3237: 
                   3238:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3239: }
                   3240: 
                   3241: # -------------------------------------------------------------- dump interface
                   3242: 
                   3243: sub dump {
1.755     albertel 3244:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
                   3245:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3246:     if (!$uname) { $uname=$env{'user.name'}; }
                   3247:     my $uhome=&homeserver($uname,$udomain);
                   3248:     if ($regexp) {
                   3249: 	$regexp=&escape($regexp);
                   3250:     } else {
                   3251: 	$regexp='.';
                   3252:     }
                   3253:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3254:     my @pairs=split(/\&/,$rep);
                   3255:     my %returnhash=();
                   3256:     foreach my $item (@pairs) {
                   3257: 	my ($key,$value)=split(/=/,$item,2);
                   3258: 	$key = &unescape($key);
                   3259: 	next if ($key =~ /^error: 2 /);
                   3260: 	$returnhash{$key}=&thaw_unescape($value);
                   3261:     }
                   3262:     return %returnhash;
1.407     www      3263: }
                   3264: 
1.717     albertel 3265: # --------------------------------------------------------- dumpstore interface
                   3266: 
                   3267: sub dumpstore {
                   3268:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 3269:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3270:    if (!$uname) { $uname=$env{'user.name'}; }
                   3271:    my $uhome=&homeserver($uname,$udomain);
                   3272:    if ($regexp) {
                   3273:        $regexp=&escape($regexp);
                   3274:    } else {
                   3275:        $regexp='.';
                   3276:    }
                   3277:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3278:    my @pairs=split(/\&/,$rep);
                   3279:    my %returnhash=();
                   3280:    foreach my $item (@pairs) {
                   3281:        my ($key,$value)=split(/=/,$item,2);
                   3282:        next if ($key =~ /^error: 2 /);
                   3283:        $returnhash{$key}=&thaw_unescape($value);
                   3284:    }
                   3285:    return %returnhash;
1.717     albertel 3286: }
                   3287: 
1.407     www      3288: # -------------------------------------------------------------- keys interface
                   3289: 
                   3290: sub getkeys {
                   3291:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 3292:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3293:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      3294:    my $uhome=&homeserver($uname,$udomain);
                   3295:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   3296:    my @keyarray=();
1.800     albertel 3297:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  3298:       next if ($key =~ /^error: 2 /);
1.800     albertel 3299:       push(@keyarray,&unescape($key));
1.407     www      3300:    }
                   3301:    return @keyarray;
1.318     matthew  3302: }
                   3303: 
1.319     matthew  3304: # --------------------------------------------------------------- currentdump
                   3305: sub currentdump {
1.328     matthew  3306:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 3307:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   3308:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   3309:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  3310:    my $uhome = &homeserver($sname,$sdom);
                   3311:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  3312:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  3313:    #
1.318     matthew  3314:    my %returnhash=();
1.319     matthew  3315:    #
                   3316:    if ($rep eq "unknown_cmd") { 
                   3317:        # an old lond will not know currentdump
                   3318:        # Do a dump and make it look like a currentdump
1.822     albertel 3319:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  3320:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   3321:        my %hash = @tmp;
                   3322:        @tmp=();
1.424     matthew  3323:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  3324:    } else {
                   3325:        my @pairs=split(/\&/,$rep);
1.800     albertel 3326:        foreach my $pair (@pairs) {
                   3327:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  3328:            my ($symb,$param) = split(/:/,$key);
                   3329:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 3330:                                                         &thaw_unescape($value);
1.319     matthew  3331:        }
1.191     harris41 3332:    }
1.12      www      3333:    return %returnhash;
1.424     matthew  3334: }
                   3335: 
                   3336: sub convert_dump_to_currentdump{
                   3337:     my %hash = %{shift()};
                   3338:     my %returnhash;
                   3339:     # Code ripped from lond, essentially.  The only difference
                   3340:     # here is the unescaping done by lonnet::dump().  Conceivably
                   3341:     # we might run in to problems with parameter names =~ /^v\./
                   3342:     while (my ($key,$value) = each(%hash)) {
                   3343:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 3344: 	$symb  = &unescape($symb);
                   3345: 	$param = &unescape($param);
1.424     matthew  3346:         next if ($v eq 'version' || $symb eq 'keys');
                   3347:         next if (exists($returnhash{$symb}) &&
                   3348:                  exists($returnhash{$symb}->{$param}) &&
                   3349:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   3350:         $returnhash{$symb}->{$param}=$value;
                   3351:         $returnhash{$symb}->{'v.'.$param}=$v;
                   3352:     }
                   3353:     #
                   3354:     # Remove all of the keys in the hashes which keep track of
                   3355:     # the version of the parameter.
                   3356:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   3357:         # use a foreach because we are going to delete from the hash.
                   3358:         foreach my $key (keys(%$param_hash)) {
                   3359:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   3360:         }
                   3361:     }
                   3362:     return \%returnhash;
1.12      www      3363: }
                   3364: 
1.627     albertel 3365: # ------------------------------------------------------ critical inc interface
                   3366: 
                   3367: sub cinc {
                   3368:     return &inc(@_,'critical');
                   3369: }
                   3370: 
1.449     matthew  3371: # --------------------------------------------------------------- inc interface
                   3372: 
                   3373: sub inc {
1.627     albertel 3374:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 3375:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3376:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  3377:     my $uhome=&homeserver($uname,$udomain);
                   3378:     my $items='';
                   3379:     if (! ref($store)) {
                   3380:         # got a single value, so use that instead
                   3381:         $items = &escape($store).'=&';
                   3382:     } elsif (ref($store) eq 'SCALAR') {
                   3383:         $items = &escape($$store).'=&';        
                   3384:     } elsif (ref($store) eq 'ARRAY') {
                   3385:         $items = join('=&',map {&escape($_);} @{$store});
                   3386:     } elsif (ref($store) eq 'HASH') {
                   3387:         while (my($key,$value) = each(%{$store})) {
                   3388:             $items.= &escape($key).'='.&escape($value).'&';
                   3389:         }
                   3390:     }
                   3391:     $items=~s/\&$//;
1.627     albertel 3392:     if ($critical) {
                   3393: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3394:     } else {
                   3395: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3396:     }
1.449     matthew  3397: }
                   3398: 
1.12      www      3399: # --------------------------------------------------------------- put interface
                   3400: 
                   3401: sub put {
1.134     albertel 3402:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3403:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3404:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3405:    my $uhome=&homeserver($uname,$udomain);
1.12      www      3406:    my $items='';
1.800     albertel 3407:    foreach my $item (keys(%$storehash)) {
                   3408:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3409:    }
1.12      www      3410:    $items=~s/\&$//;
1.134     albertel 3411:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      3412: }
                   3413: 
1.631     albertel 3414: # ------------------------------------------------------------ newput interface
                   3415: 
                   3416: sub newput {
                   3417:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   3418:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3419:    if (!$uname) { $uname=$env{'user.name'}; }
                   3420:    my $uhome=&homeserver($uname,$udomain);
                   3421:    my $items='';
                   3422:    foreach my $key (keys(%$storehash)) {
                   3423:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   3424:    }
                   3425:    $items=~s/\&$//;
                   3426:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   3427: }
                   3428: 
                   3429: # ---------------------------------------------------------  putstore interface
                   3430: 
1.524     raeburn  3431: sub putstore {
1.715     albertel 3432:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 3433:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3434:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  3435:    my $uhome=&homeserver($uname,$udomain);
                   3436:    my $items='';
1.715     albertel 3437:    foreach my $key (keys(%$storehash)) {
                   3438:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  3439:    }
1.715     albertel 3440:    $items=~s/\&$//;
1.716     albertel 3441:    my $esc_symb=&escape($symb);
                   3442:    my $esc_v=&escape($version);
1.715     albertel 3443:    my $reply =
1.716     albertel 3444:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 3445: 	      $uhome);
                   3446:    if ($reply eq 'unknown_cmd') {
1.716     albertel 3447:        # gfall back to way things use to be done
1.715     albertel 3448:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   3449: 			    $uname);
1.524     raeburn  3450:    }
1.715     albertel 3451:    return $reply;
                   3452: }
                   3453: 
                   3454: sub old_putstore {
1.716     albertel 3455:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   3456:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3457:     if (!$uname) { $uname=$env{'user.name'}; }
                   3458:     my $uhome=&homeserver($uname,$udomain);
                   3459:     my %newstorehash;
1.800     albertel 3460:     foreach my $item (keys(%$storehash)) {
                   3461: 	my $key = $version.':'.&escape($symb).':'.$item;
                   3462: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 3463:     }
                   3464:     my $items='';
                   3465:     my %allitems = ();
1.800     albertel 3466:     foreach my $item (keys(%newstorehash)) {
                   3467: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 3468: 	    my $key = $1.':keys:'.$2;
                   3469: 	    $allitems{$key} .= $3.':';
                   3470: 	}
1.800     albertel 3471: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 3472:     }
1.800     albertel 3473:     foreach my $item (keys(%allitems)) {
                   3474: 	$allitems{$item} =~ s/\:$//;
                   3475: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 3476:     }
                   3477:     $items=~s/\&$//;
                   3478:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  3479: }
                   3480: 
1.47      www      3481: # ------------------------------------------------------ critical put interface
                   3482: 
                   3483: sub cput {
1.134     albertel 3484:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3485:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3486:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3487:    my $uhome=&homeserver($uname,$udomain);
1.47      www      3488:    my $items='';
1.800     albertel 3489:    foreach my $item (keys(%$storehash)) {
                   3490:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3491:    }
1.47      www      3492:    $items=~s/\&$//;
1.134     albertel 3493:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3494: }
                   3495: 
                   3496: # -------------------------------------------------------------- eget interface
                   3497: 
                   3498: sub eget {
1.133     albertel 3499:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3500:    my $items='';
1.800     albertel 3501:    foreach my $item (@$storearr) {
                   3502:        $items.=&escape($item).'&';
1.191     harris41 3503:    }
1.12      www      3504:    $items=~s/\&$//;
1.620     albertel 3505:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3506:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3507:    my $uhome=&homeserver($uname,$udomain);
                   3508:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3509:    my @pairs=split(/\&/,$rep);
                   3510:    my %returnhash=();
1.42      www      3511:    my $i=0;
1.800     albertel 3512:    foreach my $item (@$storearr) {
                   3513:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3514:       $i++;
1.191     harris41 3515:    }
1.12      www      3516:    return %returnhash;
                   3517: }
                   3518: 
1.667     albertel 3519: # ------------------------------------------------------------ tmpput interface
                   3520: sub tmpput {
1.802     raeburn  3521:     my ($storehash,$server,$context)=@_;
1.667     albertel 3522:     my $items='';
1.800     albertel 3523:     foreach my $item (keys(%$storehash)) {
                   3524: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 3525:     }
                   3526:     $items=~s/\&$//;
1.802     raeburn  3527:     if (defined($context)) {
                   3528:         $items .= ':'.&escape($context);
                   3529:     }
1.667     albertel 3530:     return &reply("tmpput:$items",$server);
                   3531: }
                   3532: 
                   3533: # ------------------------------------------------------------ tmpget interface
                   3534: sub tmpget {
1.688     albertel 3535:     my ($token,$server)=@_;
                   3536:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3537:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 3538:     my %returnhash;
                   3539:     foreach my $item (split(/\&/,$rep)) {
                   3540: 	my ($key,$value)=split(/=/,$item);
                   3541: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   3542:     }
                   3543:     return %returnhash;
                   3544: }
                   3545: 
1.688     albertel 3546: # ------------------------------------------------------------ tmpget interface
                   3547: sub tmpdel {
                   3548:     my ($token,$server)=@_;
                   3549:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3550:     return &reply("tmpdel:$token",$server);
                   3551: }
                   3552: 
1.765     albertel 3553: # -------------------------------------------------- portfolio access checking
                   3554: 
                   3555: sub portfolio_access {
1.766     albertel 3556:     my ($requrl) = @_;
1.765     albertel 3557:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   3558:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  3559:     if ($result) {
                   3560:         my %setters;
                   3561:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3562:             my ($startblock,$endblock) =
                   3563:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   3564:             if ($startblock && $endblock) {
                   3565:                 return 'B';
                   3566:             }
                   3567:         } else {
                   3568:             my ($startblock,$endblock) =
                   3569:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   3570:             if ($startblock && $endblock) {
                   3571:                 return 'B';
                   3572:             }
                   3573:         }
                   3574:     }
1.765     albertel 3575:     if ($result eq 'ok') {
1.766     albertel 3576:        return 'F';
1.765     albertel 3577:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 3578:        return 'A';
1.765     albertel 3579:     }
1.766     albertel 3580:     return '';
1.765     albertel 3581: }
                   3582: 
                   3583: sub get_portfolio_access {
1.767     albertel 3584:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   3585: 
                   3586:     if (!ref($access_hash)) {
                   3587: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   3588: 	my %access_controls = &get_access_controls($current_perms,$group,
                   3589: 						   $file_name);
                   3590: 	$access_hash = $access_controls{$file_name};
                   3591:     }
                   3592: 
1.765     albertel 3593:     my ($public,$guest,@domains,@users,@courses,@groups);
                   3594:     my $now = time;
                   3595:     if (ref($access_hash) eq 'HASH') {
                   3596:         foreach my $key (keys(%{$access_hash})) {
                   3597:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   3598:             if ($start > $now) {
                   3599:                 next;
                   3600:             }
                   3601:             if ($end && $end<$now) {
                   3602:                 next;
                   3603:             }
                   3604:             if ($scope eq 'public') {
                   3605:                 $public = $key;
                   3606:                 last;
                   3607:             } elsif ($scope eq 'guest') {
                   3608:                 $guest = $key;
                   3609:             } elsif ($scope eq 'domains') {
                   3610:                 push(@domains,$key);
                   3611:             } elsif ($scope eq 'users') {
                   3612:                 push(@users,$key);
                   3613:             } elsif ($scope eq 'course') {
                   3614:                 push(@courses,$key);
                   3615:             } elsif ($scope eq 'group') {
                   3616:                 push(@groups,$key);
                   3617:             }
                   3618:         }
                   3619:         if ($public) {
                   3620:             return 'ok';
                   3621:         }
                   3622:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3623:             if ($guest) {
                   3624:                 return $guest;
                   3625:             }
                   3626:         } else {
                   3627:             if (@domains > 0) {
                   3628:                 foreach my $domkey (@domains) {
                   3629:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   3630:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   3631:                             return 'ok';
                   3632:                         }
                   3633:                     }
                   3634:                 }
                   3635:             }
                   3636:             if (@users > 0) {
                   3637:                 foreach my $userkey (@users) {
1.865     raeburn  3638:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   3639:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   3640:                             if (ref($item) eq 'HASH') {
                   3641:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   3642:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   3643:                                     return 'ok';
                   3644:                                 }
                   3645:                             }
                   3646:                         }
                   3647:                     } 
1.765     albertel 3648:                 }
                   3649:             }
                   3650:             my %roleshash;
                   3651:             my @courses_and_groups = @courses;
                   3652:             push(@courses_and_groups,@groups); 
                   3653:             if (@courses_and_groups > 0) {
                   3654:                 my (%allgroups,%allroles); 
                   3655:                 my ($start,$end,$role,$sec,$group);
                   3656:                 foreach my $envkey (%env) {
1.811     albertel 3657:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3658:                         my $cid = $2.'_'.$3; 
                   3659:                         if ($1 eq 'gr') {
                   3660:                             $group = $4;
                   3661:                             $allgroups{$cid}{$group} = $env{$envkey};
                   3662:                         } else {
                   3663:                             if ($4 eq '') {
                   3664:                                 $sec = 'none';
                   3665:                             } else {
                   3666:                                 $sec = $4;
                   3667:                             }
                   3668:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3669:                         }
1.811     albertel 3670:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3671:                         my $cid = $2.'_'.$3;
                   3672:                         if ($4 eq '') {
                   3673:                             $sec = 'none';
                   3674:                         } else {
                   3675:                             $sec = $4;
                   3676:                         }
                   3677:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3678:                     }
                   3679:                 }
                   3680:                 if (keys(%allroles) == 0) {
                   3681:                     return;
                   3682:                 }
                   3683:                 foreach my $key (@courses_and_groups) {
                   3684:                     my %content = %{$$access_hash{$key}};
                   3685:                     my $cnum = $content{'number'};
                   3686:                     my $cdom = $content{'domain'};
                   3687:                     my $cid = $cdom.'_'.$cnum;
                   3688:                     if (!exists($allroles{$cid})) {
                   3689:                         next;
                   3690:                     }    
                   3691:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   3692:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   3693:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   3694:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   3695:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   3696:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   3697:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   3698:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   3699:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   3700:                                         if (grep/^all$/,@sections) {
                   3701:                                             return 'ok';
                   3702:                                         } else {
                   3703:                                             if (grep/^$sec$/,@sections) {
                   3704:                                                 return 'ok';
                   3705:                                             }
                   3706:                                         }
                   3707:                                     }
                   3708:                                 }
                   3709:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   3710:                                     if (grep/^none$/,@groups) {
                   3711:                                         return 'ok';
                   3712:                                     }
                   3713:                                 } else {
                   3714:                                     if (grep/^all$/,@groups) {
                   3715:                                         return 'ok';
                   3716:                                     } 
                   3717:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   3718:                                         if (grep/^$group$/,@groups) {
                   3719:                                             return 'ok';
                   3720:                                         }
                   3721:                                     }
                   3722:                                 } 
                   3723:                             }
                   3724:                         }
                   3725:                     }
                   3726:                 }
                   3727:             }
                   3728:             if ($guest) {
                   3729:                 return $guest;
                   3730:             }
                   3731:         }
                   3732:     }
                   3733:     return;
                   3734: }
                   3735: 
                   3736: sub course_group_datechecker {
                   3737:     my ($dates,$now,$status) = @_;
                   3738:     my ($start,$end) = split(/\./,$dates);
                   3739:     if (!$start && !$end) {
                   3740:         return 'ok';
                   3741:     }
                   3742:     if (grep/^active$/,@{$status}) {
                   3743:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   3744:             return 'ok';
                   3745:         }
                   3746:     }
                   3747:     if (grep/^previous$/,@{$status}) {
                   3748:         if ($end > $now ) {
                   3749:             return 'ok';
                   3750:         }
                   3751:     }
                   3752:     if (grep/^future$/,@{$status}) {
                   3753:         if ($start > $now) {
                   3754:             return 'ok';
                   3755:         }
                   3756:     }
                   3757:     return; 
                   3758: }
                   3759: 
                   3760: sub parse_portfolio_url {
                   3761:     my ($url) = @_;
                   3762: 
                   3763:     my ($type,$udom,$unum,$group,$file_name);
                   3764:     
1.823     albertel 3765:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 3766: 	$type = 1;
                   3767:         $udom = $1;
                   3768:         $unum = $2;
                   3769:         $file_name = $3;
1.823     albertel 3770:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 3771: 	$type = 2;
                   3772:         $udom = $1;
                   3773:         $unum = $2;
                   3774:         $group = $3;
                   3775:         $file_name = $3.'/'.$4;
                   3776:     }
                   3777:     if (wantarray) {
                   3778: 	return ($type,$udom,$unum,$file_name,$group);
                   3779:     }
                   3780:     return $type;
                   3781: }
                   3782: 
                   3783: sub is_portfolio_url {
                   3784:     my ($url) = @_;
                   3785:     return scalar(&parse_portfolio_url($url));
                   3786: }
                   3787: 
1.798     raeburn  3788: sub is_portfolio_file {
                   3789:     my ($file) = @_;
1.820     raeburn  3790:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  3791:         return 1;
                   3792:     }
                   3793:     return;
                   3794: }
                   3795: 
                   3796: 
1.341     www      3797: # ---------------------------------------------- Custom access rule evaluation
                   3798: 
                   3799: sub customaccess {
                   3800:     my ($priv,$uri)=@_;
1.807     albertel 3801:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      3802:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 3803:     $udom = &LONCAPA::clean_domain($udom);
                   3804:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      3805:     my $access=0;
1.800     albertel 3806:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 3807: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   3808: 	if ($type eq 'user') {
                   3809: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896   ! albertel 3810: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 3811: 		if ($tdom) {
                   3812: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   3813: 		}
1.896   ! albertel 3814: 		if ($tuname) {
        !          3815: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 3816: 		}
                   3817: 		$access=($effect eq 'allow');
                   3818: 		last;
                   3819: 	    }
                   3820: 	} else {
                   3821: 	    if ($role) {
                   3822: 		if ($role ne $urole) { next; }
                   3823: 	    }
                   3824: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   3825: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   3826: 		if ($tdom) {
                   3827: 		    if ($tdom ne $udom) { next; }
                   3828: 		}
                   3829: 		if ($tcrs) {
                   3830: 		    if ($tcrs ne $ucrs) { next; }
                   3831: 		}
                   3832: 		if ($tsec) {
                   3833: 		    if ($tsec ne $usec) { next; }
                   3834: 		}
                   3835: 		$access=($effect eq 'allow');
                   3836: 		last;
                   3837: 	    }
                   3838: 	    if ($realm eq '' && $role eq '') {
                   3839: 		$access=($effect eq 'allow');
                   3840: 	    }
1.402     bowersj2 3841: 	}
1.341     www      3842:     }
                   3843:     return $access;
                   3844: }
                   3845: 
1.103     harris41 3846: # ------------------------------------------------- Check for a user privilege
1.12      www      3847: 
                   3848: sub allowed {
1.810     raeburn  3849:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 3850:     my $ver_orguri=$uri;
1.439     www      3851:     $uri=&deversion($uri);
1.152     www      3852:     my $orguri=$uri;
1.52      www      3853:     $uri=&declutter($uri);
1.809     raeburn  3854: 
1.810     raeburn  3855:     if ($priv eq 'evb') {
                   3856: # Evade communication block restrictions for specified role in a course
                   3857:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   3858:             return $1;
                   3859:         } else {
                   3860:             return;
                   3861:         }
                   3862:     }
                   3863: 
1.620     albertel 3864:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      3865: # Free bre access to adm and meta resources
1.775     albertel 3866:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 3867: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   3868: 	&& ($priv eq 'bre')) {
1.14      www      3869: 	return 'F';
1.159     www      3870:     }
                   3871: 
1.545     banghart 3872: # Free bre access to user's own portfolio contents
1.714     raeburn  3873:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  3874:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  3875: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  3876:         my %setters;
                   3877:         my ($startblock,$endblock) = 
                   3878:             &Apache::loncommon::blockcheck(\%setters,'port');
                   3879:         if ($startblock && $endblock) {
                   3880:             return 'B';
                   3881:         } else {
                   3882:             return 'F';
                   3883:         }
1.545     banghart 3884:     }
                   3885: 
1.762     raeburn  3886: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  3887:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   3888:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   3889:         if (exists($env{'request.course.id'})) {
                   3890:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3891:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3892:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   3893:                 my $courseprivid=$env{'request.course.id'};
                   3894:                 $courseprivid=~s/\_/\//;
                   3895:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   3896:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   3897:                     return $1; 
1.762     raeburn  3898:                 } else {
                   3899:                     if ($env{'request.course.sec'}) {
                   3900:                         $courseprivid.='/'.$env{'request.course.sec'};
                   3901:                     }
                   3902:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   3903:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   3904:                         return $2;
                   3905:                     }
1.714     raeburn  3906:                 }
                   3907:             }
                   3908:         }
                   3909:     }
                   3910: 
1.159     www      3911: # Free bre to public access
                   3912: 
                   3913:     if ($priv eq 'bre') {
1.238     www      3914:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 3915: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      3916:            return 'F'; 
                   3917:         }
1.238     www      3918:         if ($copyright eq 'priv') {
                   3919:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 3920: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      3921: 		return '';
                   3922:             }
                   3923:         }
                   3924:         if ($copyright eq 'domain') {
                   3925:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 3926: 	    unless (($env{'user.domain'} eq $1) ||
                   3927:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      3928: 		return '';
                   3929:             }
1.262     matthew  3930:         }
1.620     albertel 3931:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  3932:             # Library role, so allow browsing of resources in this domain.
                   3933:             return 'F';
1.238     www      3934:         }
1.341     www      3935:         if ($copyright eq 'custom') {
                   3936: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   3937:         }
1.14      www      3938:     }
1.264     matthew  3939:     # Domain coordinator is trying to create a course
1.620     albertel 3940:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  3941:         # uri is the requested domain in this case.
                   3942:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  3943:         # a role of dc for the domain in question.
1.620     albertel 3944:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  3945:     }
1.29      www      3946: 
1.52      www      3947:     my $thisallowed='';
                   3948:     my $statecond=0;
                   3949:     my $courseprivid='';
                   3950: 
                   3951: # Course
                   3952: 
1.620     albertel 3953:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      3954:        $thisallowed.=$1;
                   3955:     }
1.29      www      3956: 
1.52      www      3957: # Domain
                   3958: 
1.620     albertel 3959:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 3960:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      3961:        $thisallowed.=$1;
                   3962:     }
1.52      www      3963: 
                   3964: # Course: uri itself is a course
1.66      www      3965:     my $courseuri=$uri;
                   3966:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      3967:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      3968: 
1.620     albertel 3969:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 3970:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      3971:        $thisallowed.=$1;
                   3972:     }
1.29      www      3973: 
1.665     albertel 3974: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 3975: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 3976:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 3977: 	$thisallowed='';
1.671     raeburn  3978:         my ($match)=&is_on_map($uri);
                   3979:         if ($match) {
                   3980:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   3981:                   =~/\Q$priv\E\&([^\:]*)/) {
                   3982:                 $thisallowed.=$1;
                   3983:             }
                   3984:         } else {
1.705     albertel 3985:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  3986:             if ($refuri) {
                   3987:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  3988:                     $thisallowed='F';
1.671     raeburn  3989:                 } else {
                   3990:                     $refuri=&declutter($refuri);
                   3991:                     my ($match) = &is_on_map($refuri);
                   3992:                     if ($match) {
                   3993:                         $thisallowed='F';
                   3994:                     }
1.669     raeburn  3995:                 }
1.671     raeburn  3996:             }
                   3997:         }
1.314     www      3998:     }
1.492     albertel 3999: 
1.766     albertel 4000:     if ($priv eq 'bre'
                   4001: 	&& $thisallowed ne 'F' 
                   4002: 	&& $thisallowed ne '2'
                   4003: 	&& &is_portfolio_url($uri)) {
                   4004: 	$thisallowed = &portfolio_access($uri);
                   4005:     }
                   4006:     
1.52      www      4007: # Full access at system, domain or course-wide level? Exit.
1.29      www      4008: 
                   4009:     if ($thisallowed=~/F/) {
                   4010: 	return 'F';
                   4011:     }
                   4012: 
1.52      www      4013: # If this is generating or modifying users, exit with special codes
1.29      www      4014: 
1.643     www      4015:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   4016: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 4017: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      4018: # no author name given, so this just checks on the general right to make a co-author in this domain
                   4019: 	    unless ($auname) { return $thisallowed; }
                   4020: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 4021: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   4022: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   4023: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   4024: 	}
1.52      www      4025: 	return $thisallowed;
                   4026:     }
                   4027: #
1.103     harris41 4028: # Gathered so far: system, domain and course wide privileges
1.52      www      4029: #
                   4030: # Course: See if uri or referer is an individual resource that is part of 
                   4031: # the course
                   4032: 
1.620     albertel 4033:     if ($env{'request.course.id'}) {
1.232     www      4034: 
1.620     albertel 4035:        $courseprivid=$env{'request.course.id'};
                   4036:        if ($env{'request.course.sec'}) {
                   4037:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      4038:        }
                   4039:        $courseprivid=~s/\_/\//;
                   4040:        my $checkreferer=1;
1.232     www      4041:        my ($match,$cond)=&is_on_map($uri);
                   4042:        if ($match) {
                   4043:            $statecond=$cond;
1.620     albertel 4044:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4045:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4046:                $thisallowed.=$1;
                   4047:                $checkreferer=0;
                   4048:            }
1.29      www      4049:        }
1.83      www      4050:        
1.148     www      4051:        if ($checkreferer) {
1.620     albertel 4052: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      4053:             unless ($refuri) {
1.800     albertel 4054:                 foreach my $key (keys(%env)) {
                   4055: 		    if ($key=~/^httpref\..*\*/) {
                   4056: 			my $pattern=$key;
1.156     www      4057:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      4058:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   4059:                         $pattern=~s/\//\\\//g;
1.152     www      4060:                         if ($orguri=~/$pattern/) {
1.800     albertel 4061: 			    $refuri=$env{$key};
1.148     www      4062:                         }
                   4063:                     }
1.191     harris41 4064:                 }
1.148     www      4065:             }
1.232     www      4066: 
1.148     www      4067:          if ($refuri) { 
1.152     www      4068: 	  $refuri=&declutter($refuri);
1.232     www      4069:           my ($match,$cond)=&is_on_map($refuri);
                   4070:             if ($match) {
                   4071:               my $refstatecond=$cond;
1.620     albertel 4072:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4073:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4074:                   $thisallowed.=$1;
1.53      www      4075:                   $uri=$refuri;
                   4076:                   $statecond=$refstatecond;
1.52      www      4077:               }
                   4078:           }
1.148     www      4079:         }
1.29      www      4080:        }
1.52      www      4081:    }
1.29      www      4082: 
1.52      www      4083: #
1.103     harris41 4084: # Gathered now: all privileges that could apply, and condition number
1.52      www      4085: # 
                   4086: #
                   4087: # Full or no access?
                   4088: #
1.29      www      4089: 
1.52      www      4090:     if ($thisallowed=~/F/) {
                   4091: 	return 'F';
                   4092:     }
1.29      www      4093: 
1.52      www      4094:     unless ($thisallowed) {
                   4095:         return '';
                   4096:     }
1.29      www      4097: 
1.52      www      4098: # Restrictions exist, deal with them
                   4099: #
                   4100: #   C:according to course preferences
                   4101: #   R:according to resource settings
                   4102: #   L:unless locked
                   4103: #   X:according to user session state
                   4104: #
                   4105: 
                   4106: # Possibly locked functionality, check all courses
1.54      www      4107: # Locks might take effect only after 10 minutes cache expiration for other
                   4108: # courses, and 2 minutes for current course
1.52      www      4109: 
                   4110:     my $envkey;
                   4111:     if ($thisallowed=~/L/) {
1.620     albertel 4112:         foreach $envkey (keys %env) {
1.54      www      4113:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   4114:                my $courseid=$2;
                   4115:                my $roleid=$1.'.'.$2;
1.92      www      4116:                $courseid=~s/^\///;
1.54      www      4117:                my $expiretime=600;
1.620     albertel 4118:                if ($env{'request.role'} eq $roleid) {
1.54      www      4119: 		  $expiretime=120;
                   4120:                }
                   4121: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   4122:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 4123:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 4124: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      4125:                }
1.620     albertel 4126:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4127:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   4128: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   4129:                        &log($env{'user.domain'},$env{'user.name'},
                   4130:                             $env{'user.home'},
1.57      www      4131:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      4132:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4133:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4134: 		       return '';
                   4135:                    }
                   4136:                }
1.620     albertel 4137:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4138:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   4139: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   4140:                        &log($env{'user.domain'},$env{'user.name'},
                   4141:                             $env{'user.home'},
1.57      www      4142:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      4143:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4144:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4145: 		       return '';
                   4146:                    }
                   4147:                }
                   4148: 	   }
1.29      www      4149:        }
1.52      www      4150:     }
                   4151:    
                   4152: #
                   4153: # Rest of the restrictions depend on selected course
                   4154: #
                   4155: 
1.620     albertel 4156:     unless ($env{'request.course.id'}) {
1.766     albertel 4157: 	if ($thisallowed eq 'A') {
                   4158: 	    return 'A';
1.814     raeburn  4159:         } elsif ($thisallowed eq 'B') {
                   4160:             return 'B';
1.766     albertel 4161: 	} else {
                   4162: 	    return '1';
                   4163: 	}
1.52      www      4164:     }
1.29      www      4165: 
1.52      www      4166: #
                   4167: # Now user is definitely in a course
                   4168: #
1.53      www      4169: 
                   4170: 
                   4171: # Course preferences
                   4172: 
                   4173:    if ($thisallowed=~/C/) {
1.620     albertel 4174:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   4175:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   4176:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 4177: 	   =~/\Q$rolecode\E/) {
1.689     albertel 4178: 	   if ($priv ne 'pch') { 
                   4179: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4180: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   4181: 			$env{'request.course.id'});
                   4182: 	   }
1.237     www      4183:            return '';
                   4184:        }
                   4185: 
1.620     albertel 4186:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 4187: 	   =~/\Q$unamedom\E/) {
1.689     albertel 4188: 	   if ($priv ne 'pch') { 
                   4189: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   4190: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   4191: 			$env{'request.course.id'});
                   4192: 	   }
1.54      www      4193:            return '';
                   4194:        }
1.53      www      4195:    }
                   4196: 
                   4197: # Resource preferences
                   4198: 
                   4199:    if ($thisallowed=~/R/) {
1.620     albertel 4200:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 4201:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689     albertel 4202: 	   if ($priv ne 'pch') { 
                   4203: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4204: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   4205: 	   }
                   4206: 	   return '';
1.54      www      4207:        }
1.53      www      4208:    }
1.30      www      4209: 
1.246     www      4210: # Restricted by state or randomout?
1.30      www      4211: 
1.52      www      4212:    if ($thisallowed=~/X/) {
1.620     albertel 4213:       if ($env{'acc.randomout'}) {
1.579     albertel 4214: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 4215:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      4216:             return ''; 
                   4217:          }
1.247     www      4218:       }
                   4219:       if (&condval($statecond)) {
1.52      www      4220: 	 return '2';
                   4221:       } else {
                   4222:          return '';
                   4223:       }
                   4224:    }
1.30      www      4225: 
1.766     albertel 4226:     if ($thisallowed eq 'A') {
                   4227: 	return 'A';
1.814     raeburn  4228:     } elsif ($thisallowed eq 'B') {
                   4229:         return 'B';
1.766     albertel 4230:     }
1.52      www      4231:    return 'F';
1.232     www      4232: }
                   4233: 
1.710     albertel 4234: sub split_uri_for_cond {
                   4235:     my $uri=&deversion(&declutter(shift));
                   4236:     my @uriparts=split(/\//,$uri);
                   4237:     my $filename=pop(@uriparts);
                   4238:     my $pathname=join('/',@uriparts);
                   4239:     return ($pathname,$filename);
                   4240: }
1.232     www      4241: # --------------------------------------------------- Is a resource on the map?
                   4242: 
                   4243: sub is_on_map {
1.710     albertel 4244:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 4245:     #Trying to find the conditional for the file
1.620     albertel 4246:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 4247: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      4248:     if ($match) {
1.289     bowersj2 4249: 	return (1,$1);
                   4250:     } else {
1.434     www      4251: 	return (0,0);
1.289     bowersj2 4252:     }
1.12      www      4253: }
                   4254: 
1.427     www      4255: # --------------------------------------------------------- Get symb from alias
                   4256: 
                   4257: sub get_symb_from_alias {
                   4258:     my $symb=shift;
                   4259:     my ($map,$resid,$url)=&decode_symb($symb);
                   4260: # Already is a symb
                   4261:     if ($url) { return $symb; }
                   4262: # Must be an alias
                   4263:     my $aliassymb='';
                   4264:     my %bighash;
1.620     albertel 4265:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      4266:                             &GDBM_READER(),0640)) {
                   4267:         my $rid=$bighash{'mapalias_'.$symb};
                   4268: 	if ($rid) {
                   4269: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 4270: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   4271: 				    $resid,$bighash{'src_'.$rid});
1.427     www      4272: 	}
                   4273:         untie %bighash;
                   4274:     }
                   4275:     return $aliassymb;
                   4276: }
                   4277: 
1.12      www      4278: # ----------------------------------------------------------------- Define Role
                   4279: 
                   4280: sub definerole {
                   4281:   if (allowed('mcr','/')) {
                   4282:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 4283:     foreach my $role (split(':',$sysrole)) {
                   4284: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4285:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   4286:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   4287: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4288:                return "refused:s:$crole&$cqual"; 
                   4289:             }
                   4290:         }
1.191     harris41 4291:     }
1.800     albertel 4292:     foreach my $role (split(':',$domrole)) {
                   4293: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4294:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   4295:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   4296: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      4297:                return "refused:d:$crole&$cqual"; 
                   4298:             }
                   4299:         }
1.191     harris41 4300:     }
1.800     albertel 4301:     foreach my $role (split(':',$courole)) {
                   4302: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4303:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   4304:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   4305: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4306:                return "refused:c:$crole&$cqual"; 
                   4307:             }
                   4308:         }
1.191     harris41 4309:     }
1.620     albertel 4310:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   4311:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4312: 	        "rolesdef_$rolename=".
                   4313:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 4314:     return reply($command,$env{'user.home'});
1.12      www      4315:   } else {
                   4316:     return 'refused';
                   4317:   }
1.105     harris41 4318: }
                   4319: 
                   4320: # ---------------- Make a metadata query against the network of library servers
                   4321: 
                   4322: sub metadata_query {
1.244     matthew  4323:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 4324:     my %rhash;
1.845     albertel 4325:     my %libserv = &all_library();
1.244     matthew  4326:     my @server_list = (defined($server_array) ? @$server_array
                   4327:                                               : keys(%libserv) );
                   4328:     for my $server (@server_list) {
1.118     harris41 4329: 	unless ($custom or $customshow) {
                   4330: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   4331: 	    $rhash{$server}=$reply;
                   4332: 	}
                   4333: 	else {
                   4334: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   4335: 			     &escape($custom).':'.&escape($customshow),
                   4336: 			     $server);
                   4337: 	    $rhash{$server}=$reply;
                   4338: 	}
1.112     harris41 4339:     }
1.118     harris41 4340:     return \%rhash;
1.240     www      4341: }
                   4342: 
                   4343: # ----------------------------------------- Send log queries and wait for reply
                   4344: 
                   4345: sub log_query {
                   4346:     my ($uname,$udom,$query,%filters)=@_;
                   4347:     my $uhome=&homeserver($uname,$udom);
                   4348:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 4349:     my $uhost=&hostname($uhome);
1.800     albertel 4350:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      4351:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   4352:                        $uhome);
1.479     albertel 4353:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      4354:     return get_query_reply($queryid);
                   4355: }
                   4356: 
1.818     raeburn  4357: # -------------------------- Update MySQL table for portfolio file
                   4358: 
                   4359: sub update_portfolio_table {
1.821     raeburn  4360:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818     raeburn  4361:     my $homeserver = &homeserver($uname,$udom);
                   4362:     my $queryid=
1.821     raeburn  4363:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   4364:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  4365:     my $reply = &get_query_reply($queryid);
                   4366:     return $reply;
                   4367: }
                   4368: 
1.508     raeburn  4369: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  4370: 
                   4371: sub fetch_enrollment_query {
1.511     raeburn  4372:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  4373:     my $homeserver;
1.547     raeburn  4374:     my $maxtries = 1;
1.508     raeburn  4375:     if ($context eq 'automated') {
                   4376:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  4377:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  4378:     } else {
                   4379:         $homeserver = &homeserver($cnum,$dom);
                   4380:     }
1.838     albertel 4381:     my $host=&hostname($homeserver);
1.506     raeburn  4382:     my $cmd = '';
1.800     albertel 4383:     foreach my $affiliate (keys %{$affiliatesref}) {
                   4384:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  4385:     }
                   4386:     $cmd =~ s/%%$//;
                   4387:     $cmd = &escape($cmd);
                   4388:     my $query = 'fetchenrollment';
1.620     albertel 4389:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  4390:     unless ($queryid=~/^\Q$host\E\_/) { 
                   4391:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   4392:         return 'error: '.$queryid;
                   4393:     }
1.506     raeburn  4394:     my $reply = &get_query_reply($queryid);
1.547     raeburn  4395:     my $tries = 1;
                   4396:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4397:         $reply = &get_query_reply($queryid);
                   4398:         $tries ++;
                   4399:     }
1.526     raeburn  4400:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 4401:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  4402:     } else {
1.515     raeburn  4403:         my @responses = split/:/,$reply;
                   4404:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 4405:             foreach my $line (@responses) {
                   4406:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  4407:                 $$replyref{$key} = $value;
                   4408:             }
                   4409:         } else {
1.506     raeburn  4410:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 4411:             foreach my $line (@responses) {
                   4412:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  4413:                 $$replyref{$key} = $value;
                   4414:                 if ($value > 0) {
1.800     albertel 4415:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   4416:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  4417:                         my $destname = $pathname.'/'.$filename;
                   4418:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  4419:                         if ($xml_classlist =~ /^error/) {
                   4420:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   4421:                         } else {
1.506     raeburn  4422:                             if ( open(FILE,">$destname") ) {
                   4423:                                 print FILE &unescape($xml_classlist);
                   4424:                                 close(FILE);
1.526     raeburn  4425:                             } else {
                   4426:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  4427:                             }
                   4428:                         }
                   4429:                     }
                   4430:                 }
                   4431:             }
                   4432:         }
                   4433:         return 'ok';
                   4434:     }
                   4435:     return 'error';
                   4436: }
                   4437: 
1.242     www      4438: sub get_query_reply {
                   4439:     my $queryid=shift;
1.240     www      4440:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   4441:     my $reply='';
                   4442:     for (1..100) {
                   4443: 	sleep 2;
                   4444:         if (-e $replyfile.'.end') {
1.448     albertel 4445: 	    if (open(my $fh,$replyfile)) {
1.240     www      4446:                $reply.=<$fh>;
1.448     albertel 4447:                close($fh);
1.240     www      4448: 	   } else { return 'error: reply_file_error'; }
1.242     www      4449:            return &unescape($reply);
                   4450: 	}
1.240     www      4451:     }
1.242     www      4452:     return 'timeout:'.$queryid;
1.240     www      4453: }
                   4454: 
                   4455: sub courselog_query {
1.241     www      4456: #
                   4457: # possible filters:
                   4458: # url: url or symb
                   4459: # username
                   4460: # domain
                   4461: # action: view, submit, grade
                   4462: # start: timestamp
                   4463: # end: timestamp
                   4464: #
1.240     www      4465:     my (%filters)=@_;
1.620     albertel 4466:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      4467:     if ($filters{'url'}) {
                   4468: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   4469:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   4470:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   4471:     }
1.620     albertel 4472:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4473:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      4474:     return &log_query($cname,$cdom,'courselog',%filters);
                   4475: }
                   4476: 
                   4477: sub userlog_query {
1.858     raeburn  4478: #
                   4479: # possible filters:
                   4480: # action: log check role
                   4481: # start: timestamp
                   4482: # end: timestamp
                   4483: #
1.240     www      4484:     my ($uname,$udom,%filters)=@_;
                   4485:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      4486: }
                   4487: 
1.506     raeburn  4488: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   4489: 
                   4490: sub auto_run {
1.508     raeburn  4491:     my ($cnum,$cdom) = @_;
1.876     raeburn  4492:     my $response = 0;
                   4493:     my $settings;
                   4494:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   4495:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4496:         $settings = $domconfig{'autoenroll'};
                   4497:         if ($settings->{'run'} eq '1') {
                   4498:             $response = 1;
                   4499:         }
                   4500:     } else {
                   4501:         my $homeserver = &homeserver($cnum,$cdom);
                   4502:         $response = &reply('autorun:'.$cdom,$homeserver);
                   4503:     }
1.506     raeburn  4504:     return $response;
                   4505: }
1.776     albertel 4506: 
1.506     raeburn  4507: sub auto_get_sections {
1.508     raeburn  4508:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   4509:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  4510:     my @secs = ();
1.511     raeburn  4511:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  4512:     unless ($response eq 'refused') {
                   4513:         @secs = split/:/,$response;
                   4514:     }
                   4515:     return @secs;
                   4516: }
1.776     albertel 4517: 
1.506     raeburn  4518: sub auto_new_course {
1.508     raeburn  4519:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   4520:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  4521:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  4522:     return $response;
                   4523: }
1.776     albertel 4524: 
1.506     raeburn  4525: sub auto_validate_courseID {
1.508     raeburn  4526:     my ($cnum,$cdom,$inst_course_id) = @_;
                   4527:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  4528:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  4529:     return $response;
                   4530: }
1.776     albertel 4531: 
1.506     raeburn  4532: sub auto_create_password {
1.873     raeburn  4533:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   4534:     my ($homeserver,$response);
1.506     raeburn  4535:     my $create_passwd = 0;
                   4536:     my $authchk = '';
1.873     raeburn  4537:     if ($udom =~ /^$match_domain$/) {
                   4538:         $homeserver = &domain($udom,'primary');
                   4539:     }
                   4540:     if ($homeserver eq '') {
                   4541:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   4542:             $homeserver = &homeserver($cnum,$cdom);
                   4543:         }
                   4544:     }
                   4545:     if ($homeserver eq '') {
                   4546:         $authchk = 'nodomain';
1.506     raeburn  4547:     } else {
1.873     raeburn  4548:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   4549:         if ($response eq 'refused') {
                   4550:             $authchk = 'refused';
                   4551:         } else {
                   4552:             ($authparam,$create_passwd,$authchk) = split/:/,$response;
                   4553:         }
1.506     raeburn  4554:     }
                   4555:     return ($authparam,$create_passwd,$authchk);
                   4556: }
                   4557: 
1.706     raeburn  4558: sub auto_photo_permission {
                   4559:     my ($cnum,$cdom,$students) = @_;
                   4560:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 4561:     my ($outcome,$perm_reqd,$conditions) = 
                   4562: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 4563:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4564: 	return (undef,undef);
                   4565:     }
1.706     raeburn  4566:     return ($outcome,$perm_reqd,$conditions);
                   4567: }
                   4568: 
                   4569: sub auto_checkphotos {
                   4570:     my ($uname,$udom,$pid) = @_;
                   4571:     my $homeserver = &homeserver($uname,$udom);
                   4572:     my ($result,$resulttype);
                   4573:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 4574: 				   &escape($uname).':'.&escape($pid),
                   4575: 				   $homeserver));
1.709     albertel 4576:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4577: 	return (undef,undef);
                   4578:     }
1.706     raeburn  4579:     if ($outcome) {
                   4580:         ($result,$resulttype) = split(/:/,$outcome);
                   4581:     } 
                   4582:     return ($result,$resulttype);
                   4583: }
                   4584: 
                   4585: sub auto_photochoice {
                   4586:     my ($cnum,$cdom) = @_;
                   4587:     my $homeserver = &homeserver($cnum,$cdom);
                   4588:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 4589: 						       &escape($cdom),
                   4590: 						       $homeserver)));
1.709     albertel 4591:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4592: 	return (undef,undef);
                   4593:     }
1.706     raeburn  4594:     return ($update,$comment);
                   4595: }
                   4596: 
                   4597: sub auto_photoupdate {
                   4598:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   4599:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 4600:     my $host=&hostname($homeserver);
1.706     raeburn  4601:     my $cmd = '';
                   4602:     my $maxtries = 1;
1.800     albertel 4603:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   4604:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  4605:     }
                   4606:     $cmd =~ s/%%$//;
                   4607:     $cmd = &escape($cmd);
                   4608:     my $query = 'institutionalphotos';
                   4609:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   4610:     unless ($queryid=~/^\Q$host\E\_/) {
                   4611:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   4612:         return 'error: '.$queryid;
                   4613:     }
                   4614:     my $reply = &get_query_reply($queryid);
                   4615:     my $tries = 1;
                   4616:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4617:         $reply = &get_query_reply($queryid);
                   4618:         $tries ++;
                   4619:     }
                   4620:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   4621:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   4622:     } else {
                   4623:         my @responses = split(/:/,$reply);
                   4624:         my $outcome = shift(@responses); 
                   4625:         foreach my $item (@responses) {
                   4626:             my ($key,$value) = split(/=/,$item);
                   4627:             $$photo{$key} = $value;
                   4628:         }
                   4629:         return $outcome;
                   4630:     }
                   4631:     return 'error';
                   4632: }
                   4633: 
1.521     raeburn  4634: sub auto_instcode_format {
1.793     albertel 4635:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   4636: 	$cat_order) = @_;
1.521     raeburn  4637:     my $courses = '';
1.772     raeburn  4638:     my @homeservers;
1.521     raeburn  4639:     if ($caller eq 'global') {
1.841     albertel 4640: 	my %servers = &get_servers($codedom,'library');
                   4641: 	foreach my $tryserver (keys(%servers)) {
                   4642: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4643: 		push(@homeservers,$tryserver);
                   4644: 	    }
1.584     raeburn  4645:         }
1.521     raeburn  4646:     } else {
1.772     raeburn  4647:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  4648:     }
1.793     albertel 4649:     foreach my $code (keys(%{$instcodes})) {
                   4650:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  4651:     }
                   4652:     chop($courses);
1.772     raeburn  4653:     my $ok_response = 0;
                   4654:     my $response;
                   4655:     while (@homeservers > 0 && $ok_response == 0) {
                   4656:         my $server = shift(@homeservers); 
                   4657:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   4658:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   4659:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.793     albertel 4660: 		split/:/,$response;
1.772     raeburn  4661:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   4662:             push(@{$codetitles},&str2array($codetitles_str));
                   4663:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   4664:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   4665:             $ok_response = 1;
                   4666:         }
                   4667:     }
                   4668:     if ($ok_response) {
1.521     raeburn  4669:         return 'ok';
1.772     raeburn  4670:     } else {
                   4671:         return $response;
1.521     raeburn  4672:     }
                   4673: }
                   4674: 
1.792     raeburn  4675: sub auto_instcode_defaults {
                   4676:     my ($domain,$returnhash,$code_order) = @_;
                   4677:     my @homeservers;
1.841     albertel 4678: 
                   4679:     my %servers = &get_servers($domain,'library');
                   4680:     foreach my $tryserver (keys(%servers)) {
                   4681: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4682: 	    push(@homeservers,$tryserver);
                   4683: 	}
1.792     raeburn  4684:     }
1.841     albertel 4685: 
1.792     raeburn  4686:     my $response;
1.841     albertel 4687:     foreach my $server (@homeservers) {
1.792     raeburn  4688:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 4689:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   4690: 	
                   4691: 	foreach my $pair (split(/\&/,$response)) {
                   4692: 	    my ($name,$value)=split(/\=/,$pair);
                   4693: 	    if ($name eq 'code_order') {
                   4694: 		@{$code_order} = split(/\&/,&unescape($value));
                   4695: 	    } else {
                   4696: 		$returnhash->{&unescape($name)}=&unescape($value);
                   4697: 	    }
                   4698: 	}
                   4699: 	return 'ok';
1.792     raeburn  4700:     }
1.841     albertel 4701: 
                   4702:     return $response;
1.792     raeburn  4703: } 
                   4704: 
1.777     albertel 4705: sub auto_validate_class_sec {
1.773     raeburn  4706:     my ($cdom,$cnum,$owner,$inst_class) = @_;
                   4707:     my $homeserver = &homeserver($cnum,$cdom);
                   4708:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.774     banghart 4709:                         &escape($owner).':'.$cdom,$homeserver);
1.773     raeburn  4710:     return $response;
                   4711: }
                   4712: 
1.679     raeburn  4713: # ------------------------------------------------------- Course Group routines
                   4714: 
                   4715: sub get_coursegroups {
1.809     raeburn  4716:     my ($cdom,$cnum,$group,$namespace) = @_;
                   4717:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  4718: }
                   4719: 
1.679     raeburn  4720: sub modify_coursegroup {
                   4721:     my ($cdom,$cnum,$groupsettings) = @_;
                   4722:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   4723: }
                   4724: 
1.809     raeburn  4725: sub toggle_coursegroup_status {
                   4726:     my ($cdom,$cnum,$group,$action) = @_;
                   4727:     my ($from_namespace,$to_namespace);
                   4728:     if ($action eq 'delete') {
                   4729:         $from_namespace = 'coursegroups';
                   4730:         $to_namespace = 'deleted_groups';
                   4731:     } else {
                   4732:         $from_namespace = 'deleted_groups';
                   4733:         $to_namespace = 'coursegroups';
                   4734:     }
                   4735:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  4736:     if (my $tmp = &error(%curr_group)) {
                   4737:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   4738:         return ('read error',$tmp);
                   4739:     } else {
                   4740:         my %savedsettings = %curr_group; 
1.809     raeburn  4741:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  4742:         my $deloutcome;
                   4743:         if ($result eq 'ok') {
1.809     raeburn  4744:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  4745:         } else {
                   4746:             return ('write error',$result);
                   4747:         }
                   4748:         if ($deloutcome eq 'ok') {
                   4749:             return 'ok';
                   4750:         } else {
                   4751:             return ('delete error',$deloutcome);
                   4752:         }
                   4753:     }
                   4754: }
                   4755: 
1.679     raeburn  4756: sub modify_group_roles {
                   4757:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
                   4758:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   4759:     my $role = 'gr/'.&escape($userprivs);
                   4760:     my ($uname,$udom) = split(/:/,$user);
                   4761:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684     raeburn  4762:     if ($result eq 'ok') {
                   4763:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   4764:     }
1.679     raeburn  4765:     return $result;
                   4766: }
                   4767: 
                   4768: sub modify_coursegroup_membership {
                   4769:     my ($cdom,$cnum,$membership) = @_;
                   4770:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   4771:     return $result;
                   4772: }
                   4773: 
1.682     raeburn  4774: sub get_active_groups {
                   4775:     my ($udom,$uname,$cdom,$cnum) = @_;
                   4776:     my $now = time;
                   4777:     my %groups = ();
                   4778:     foreach my $key (keys(%env)) {
1.811     albertel 4779:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  4780:             my ($start,$end) = split(/\./,$env{$key});
                   4781:             if (($end!=0) && ($end<$now)) { next; }
                   4782:             if (($start!=0) && ($start>$now)) { next; }
                   4783:             if ($1 eq $cdom && $2 eq $cnum) {
                   4784:                 $groups{$3} = $env{$key} ;
                   4785:             }
                   4786:         }
                   4787:     }
                   4788:     return %groups;
                   4789: }
                   4790: 
1.683     raeburn  4791: sub get_group_membership {
                   4792:     my ($cdom,$cnum,$group) = @_;
                   4793:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   4794: }
                   4795: 
                   4796: sub get_users_groups {
                   4797:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  4798:     my @usersgroups;
1.683     raeburn  4799:     my $cachetime=1800;
                   4800: 
                   4801:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  4802:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   4803:     if (defined($cached)) {
1.734     albertel 4804:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  4805:     } else {  
                   4806:         $grouplist = '';
1.816     raeburn  4807:         my $courseurl = &courseid_to_courseurl($courseid);
                   4808:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  4809:         my $access_end = $env{'course.'.$courseid.
                   4810:                               '.default_enrollment_end_date'};
                   4811:         my $now = time;
                   4812:         foreach my $key (keys(%roleshash)) {
                   4813:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   4814:                 my $group = $1;
                   4815:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   4816:                     my $start = $2;
                   4817:                     my $end = $1;
                   4818:                     if ($start == -1) { next; } # deleted from group
                   4819:                     if (($start!=0) && ($start>$now)) { next; }
                   4820:                     if (($end!=0) && ($end<$now)) {
                   4821:                         if ($access_end && $access_end < $now) {
                   4822:                             if ($access_end - $end < 86400) {
                   4823:                                 push(@usersgroups,$group);
1.733     raeburn  4824:                             }
                   4825:                         }
1.817     raeburn  4826:                         next;
1.733     raeburn  4827:                     }
1.817     raeburn  4828:                     push(@usersgroups,$group);
1.683     raeburn  4829:                 }
                   4830:             }
                   4831:         }
1.817     raeburn  4832:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   4833:         $grouplist = join(':',@usersgroups);
                   4834:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  4835:     }
1.733     raeburn  4836:     return @usersgroups;
1.683     raeburn  4837: }
                   4838: 
                   4839: sub devalidate_getgroups_cache {
                   4840:     my ($udom,$uname,$cdom,$cnum)=@_;
                   4841:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 4842: 
1.683     raeburn  4843:     my $hashid="$udom:$uname:$courseid";
                   4844:     &devalidate_cache_new('getgroups',$hashid);
                   4845: }
                   4846: 
1.12      www      4847: # ------------------------------------------------------------------ Plain Text
                   4848: 
                   4849: sub plaintext {
1.742     raeburn  4850:     my ($short,$type,$cid) = @_;
1.758     albertel 4851:     if ($short =~ /^cr/) {
                   4852: 	return (split('/',$short))[-1];
                   4853:     }
1.742     raeburn  4854:     if (!defined($cid)) {
                   4855:         $cid = $env{'request.course.id'};
                   4856:     }
                   4857:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
                   4858:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
                   4859:                                           '.plaintext'});
                   4860:     }
                   4861:     my %rolenames = (
                   4862:                       Course => 'std',
                   4863:                       Group => 'alt1',
                   4864:                     );
                   4865:     if (defined($type) && 
                   4866:          defined($rolenames{$type}) && 
                   4867:          defined($prp{$short}{$rolenames{$type}})) {
                   4868:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
                   4869:     } else {
                   4870:         return &Apache::lonlocal::mt($prp{$short}{'std'});
                   4871:     }
1.12      www      4872: }
                   4873: 
                   4874: # ----------------------------------------------------------------- Assign Role
                   4875: 
                   4876: sub assignrole {
1.357     www      4877:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21      www      4878:     my $mrole;
                   4879:     if ($role =~ /^cr\//) {
1.393     www      4880:         my $cwosec=$url;
1.811     albertel 4881:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      4882: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      4883:            &logthis('Refused custom assignrole: '.
                   4884:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 4885: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      4886:            return 'refused'; 
                   4887:         }
1.21      www      4888:         $mrole='cr';
1.678     raeburn  4889:     } elsif ($role =~ /^gr\//) {
                   4890:         my $cwogrp=$url;
1.811     albertel 4891:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  4892:         unless (&allowed('mdg',$cwogrp)) {
                   4893:             &logthis('Refused group assignrole: '.
                   4894:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   4895:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   4896:             return 'refused';
                   4897:         }
                   4898:         $mrole='gr';
1.21      www      4899:     } else {
1.82      www      4900:         my $cwosec=$url;
1.811     albertel 4901:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.373     www      4902:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
1.104     www      4903:            &logthis('Refused assignrole: '.
                   4904:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 4905: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      4906:            return 'refused'; 
                   4907:         }
1.21      www      4908:         $mrole=$role;
                   4909:     }
1.620     albertel 4910:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4911:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      4912:     if ($end) { $command.='_'.$end; }
1.21      www      4913:     if ($start) {
                   4914: 	if ($end) { 
1.81      www      4915:            $command.='_'.$start; 
1.21      www      4916:         } else {
1.81      www      4917:            $command.='_0_'.$start;
1.21      www      4918:         }
                   4919:     }
1.739     raeburn  4920:     my $origstart = $start;
                   4921:     my $origend = $end;
1.357     www      4922: # actually delete
                   4923:     if ($deleteflag) {
1.373     www      4924: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      4925: # modify command to delete the role
1.620     albertel 4926:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      4927:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 4928: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      4929: # set start and finish to negative values for userrolelog
                   4930:            $start=-1;
                   4931:            $end=-1;
                   4932:         }
                   4933:     }
                   4934: # send command
1.349     www      4935:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      4936: # log new user role if status is ok
1.349     www      4937:     if ($answer eq 'ok') {
1.663     raeburn  4938: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  4939: # for course roles, perform group memberships changes triggered by role change.
                   4940:         unless ($role =~ /^gr/) {
                   4941:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   4942:                                              $origstart);
                   4943:         }
1.349     www      4944:     }
                   4945:     return $answer;
1.169     harris41 4946: }
                   4947: 
                   4948: # -------------------------------------------------- Modify user authentication
1.197     www      4949: # Overrides without validation
                   4950: 
1.169     harris41 4951: sub modifyuserauth {
                   4952:     my ($udom,$uname,$umode,$upass)=@_;
                   4953:     my $uhome=&homeserver($uname,$udom);
1.197     www      4954:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   4955:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 4956:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   4957:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 4958:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   4959: 		     &escape($upass),$uhome);
1.620     albertel 4960:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      4961:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   4962:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   4963:     &log($udom,,$uname,$uhome,
1.620     albertel 4964:         'Authentication changed by '.$env{'user.domain'}.', '.
                   4965:                                      $env{'user.name'}.', '.$umode.
1.197     www      4966:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 4967:     unless ($reply eq 'ok') {
1.197     www      4968:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 4969: 	return 'error: '.$reply;
                   4970:     }   
1.170     harris41 4971:     return 'ok';
1.80      www      4972: }
                   4973: 
1.81      www      4974: # --------------------------------------------------------------- Modify a user
1.80      www      4975: 
1.81      www      4976: sub modifyuser {
1.206     matthew  4977:     my ($udom,    $uname, $uid,
                   4978:         $umode,   $upass, $first,
                   4979:         $middle,  $last,  $gene,
1.387     www      4980:         $forceid, $desiredhome, $email)=@_;
1.807     albertel 4981:     $udom= &LONCAPA::clean_domain($udom);
                   4982:     $uname=&LONCAPA::clean_username($uname);
1.81      www      4983:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      4984:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  4985: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   4986:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   4987:                                      ' desiredhome not specified'). 
1.620     albertel 4988:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   4989:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 4990:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      4991: # ----------------------------------------------------------------- Create User
1.406     albertel 4992:     if (($uhome eq 'no_host') && 
                   4993: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      4994:         my $unhome='';
1.844     albertel 4995:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  4996:             $unhome = $desiredhome;
1.620     albertel 4997: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   4998: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  4999:         } else { # load balancing routine for determining $unhome
1.81      www      5000:             my $loadm=10000000;
1.841     albertel 5001: 	    my %servers = &get_servers($udom,'library');
                   5002: 	    foreach my $tryserver (keys(%servers)) {
                   5003: 		my $answer=reply('load',$tryserver);
                   5004: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   5005: 		    $loadm=$answer;
                   5006: 		    $unhome=$tryserver;
                   5007: 		}
1.80      www      5008: 	    }
                   5009:         }
                   5010:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  5011: 	    return 'error: unable to find a home server for '.$uname.
                   5012:                    ' in domain '.$udom;
1.80      www      5013:         }
                   5014:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   5015:                          &escape($upass),$unhome);
                   5016: 	unless ($reply eq 'ok') {
                   5017:             return 'error: '.$reply;
                   5018:         }   
1.230     stredwic 5019:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      5020:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  5021: 	    return 'error: unable verify users home machine.';
1.80      www      5022:         }
1.209     matthew  5023:     }   # End of creation of new user
1.80      www      5024: # ---------------------------------------------------------------------- Add ID
                   5025:     if ($uid) {
                   5026:        $uid=~tr/A-Z/a-z/;
                   5027:        my %uidhash=&idrget($udom,$uname);
1.196     www      5028:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   5029:          && (!$forceid)) {
1.80      www      5030: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  5031: 	      return 'error: user id "'.$uid.'" does not match '.
                   5032:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      5033:           }
                   5034:        } else {
                   5035: 	  &idput($udom,($uname => $uid));
                   5036:        }
                   5037:     }
                   5038: # -------------------------------------------------------------- Add names, etc
1.313     matthew  5039:     my @tmp=&get('environment',
1.134     albertel 5040: 		   ['firstname','middlename','lastname','generation'],
                   5041: 		   $udom,$uname);
1.313     matthew  5042:     my %names;
                   5043:     if ($tmp[0] =~ m/^error:.*/) { 
                   5044:         %names=(); 
                   5045:     } else {
                   5046:         %names = @tmp;
                   5047:     }
1.388     www      5048: #
                   5049: # Make sure to not trash student environment if instructor does not bother
                   5050: # to supply name and email information
                   5051: #
                   5052:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  5053:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      5054:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  5055:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      5056:     if ($email) {
                   5057:        $email=~s/[^\w\@\.\-\,]//gs;
                   5058:        if ($email=~/\@/) { $names{'notification'} = $email;
                   5059: 			   $names{'critnotification'} = $email;
                   5060: 			   $names{'permanentemail'} = $email; }
                   5061:     }
1.134     albertel 5062:     my $reply = &put('environment', \%names, $udom,$uname);
                   5063:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.680     www      5064:     &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81      www      5065:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5066:              $umode.', '.$first.', '.$middle.', '.
                   5067: 	     $last.', '.$gene.' by '.
1.620     albertel 5068:              $env{'user.name'}.' at '.$env{'user.domain'});
1.134     albertel 5069:     return 'ok';
1.80      www      5070: }
                   5071: 
1.81      www      5072: # -------------------------------------------------------------- Modify student
1.80      www      5073: 
1.81      www      5074: sub modifystudent {
                   5075:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515     raeburn  5076:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455     albertel 5077:     if (!$cid) {
1.620     albertel 5078: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5079: 	    return 'not_in_class';
                   5080: 	}
1.80      www      5081:     }
                   5082: # --------------------------------------------------------------- Make the user
1.81      www      5083:     my $reply=&modifyuser
1.209     matthew  5084: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      5085:          $desiredhome,$email);
1.80      www      5086:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  5087:     # This will cause &modify_student_enrollment to get the uid from the
                   5088:     # students environment
                   5089:     $uid = undef if (!$forceid);
1.455     albertel 5090:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515     raeburn  5091: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
1.297     matthew  5092:     return $reply;
                   5093: }
                   5094: 
                   5095: sub modify_student_enrollment {
1.515     raeburn  5096:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455     albertel 5097:     my ($cdom,$cnum,$chome);
                   5098:     if (!$cid) {
1.620     albertel 5099: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5100: 	    return 'not_in_class';
                   5101: 	}
1.620     albertel 5102: 	$cdom=$env{'course.'.$cid.'.domain'};
                   5103: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 5104:     } else {
                   5105: 	($cdom,$cnum)=split(/_/,$cid);
                   5106:     }
1.620     albertel 5107:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 5108:     if (!$chome) {
1.457     raeburn  5109: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  5110:     }
1.455     albertel 5111:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  5112:     # Make sure the user exists
1.81      www      5113:     my $uhome=&homeserver($uname,$udom);
                   5114:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5115: 	return 'error: no such user';
                   5116:     }
1.297     matthew  5117:     # Get student data if we were not given enough information
                   5118:     if (!defined($first)  || $first  eq '' || 
                   5119:         !defined($last)   || $last   eq '' || 
                   5120:         !defined($uid)    || $uid    eq '' || 
                   5121:         !defined($middle) || $middle eq '' || 
                   5122:         !defined($gene)   || $gene   eq '') {
1.294     matthew  5123:         # They did not supply us with enough data to enroll the student, so
                   5124:         # we need to pick up more information.
1.297     matthew  5125:         my %tmp = &get('environment',
1.294     matthew  5126:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  5127:                        ,$udom,$uname);
                   5128: 
1.800     albertel 5129:         #foreach my $key (keys(%tmp)) {
                   5130:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 5131:         #}
1.294     matthew  5132:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   5133:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   5134:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  5135:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  5136:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   5137:     }
1.556     albertel 5138:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 5139:     my $reply=cput('classlist',
                   5140: 		   {"$uname:$udom" => 
1.515     raeburn  5141: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 5142: 		   $cdom,$cnum);
1.81      www      5143:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   5144: 	return 'error: '.$reply;
1.652     albertel 5145:     } else {
                   5146: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      5147:     }
1.297     matthew  5148:     # Add student role to user
1.83      www      5149:     my $uurl='/'.$cid;
1.81      www      5150:     $uurl=~s/\_/\//g;
                   5151:     if ($usec) {
                   5152: 	$uurl.='/'.$usec;
                   5153:     }
                   5154:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      5155: }
                   5156: 
1.556     albertel 5157: sub format_name {
                   5158:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   5159:     my $name;
                   5160:     if ($first ne 'lastname') {
                   5161: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   5162:     } else {
                   5163: 	if ($lastname=~/\S/) {
                   5164: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   5165: 	    $name=~s/\s+,/,/;
                   5166: 	} else {
                   5167: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   5168: 	}
                   5169:     }
                   5170:     $name=~s/^\s+//;
                   5171:     $name=~s/\s+$//;
                   5172:     $name=~s/\s+/ /g;
                   5173:     return $name;
                   5174: }
                   5175: 
1.84      www      5176: # ------------------------------------------------- Write to course preferences
                   5177: 
                   5178: sub writecoursepref {
                   5179:     my ($courseid,%prefs)=@_;
                   5180:     $courseid=~s/^\///;
                   5181:     $courseid=~s/\_/\//g;
                   5182:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   5183:     my $chome=homeserver($cnum,$cdomain);
                   5184:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   5185: 	return 'error: no such course';
                   5186:     }
                   5187:     my $cstring='';
1.800     albertel 5188:     foreach my $pref (keys(%prefs)) {
                   5189: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 5190:     }
1.84      www      5191:     $cstring=~s/\&$//;
                   5192:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   5193: }
                   5194: 
                   5195: # ---------------------------------------------------------- Make/modify course
                   5196: 
                   5197: sub createcourse {
1.741     raeburn  5198:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
                   5199:         $course_owner,$crstype)=@_;
1.84      www      5200:     $url=&declutter($url);
                   5201:     my $cid='';
1.264     matthew  5202:     unless (&allowed('ccc',$udom)) {
1.84      www      5203:         return 'refused';
                   5204:     }
                   5205: # ------------------------------------------------------------------- Create ID
1.674     www      5206:    my $uname=int(1+rand(9)).
                   5207:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   5208:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84      www      5209:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   5210: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 5211:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      5212:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5213:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   5214:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 5215:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      5216:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5217:            return 'error: unable to generate unique course-ID';
                   5218:        } 
                   5219:    }
1.264     matthew  5220: # ------------------------------------------------ Check supplied server name
1.620     albertel 5221:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845     albertel 5222:     if (! &is_library($course_server)) {
1.264     matthew  5223:         return 'error:bad server name '.$course_server;
                   5224:     }
1.84      www      5225: # ------------------------------------------------------------- Make the course
                   5226:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  5227:                       $course_server);
1.84      www      5228:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 5229:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      5230:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5231: 	return 'error: no such course';
                   5232:     }
1.271     www      5233: # ----------------------------------------------------------------- Course made
1.516     raeburn  5234: # log existence
                   5235:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.741     raeburn  5236:                  ':'.&escape($inst_code).':'.&escape($course_owner).':'.
                   5237:                   &escape($crstype),$uhome);
1.358     www      5238:     &flushcourselogs();
                   5239: # set toplevel url
1.271     www      5240:     my $topurl=$url;
                   5241:     unless ($nonstandard) {
                   5242: # ------------------------------------------ For standard courses, make top url
                   5243:         my $mapurl=&clutter($url);
1.278     www      5244:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 5245:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      5246: <map>
                   5247: <resource id="1" type="start"></resource>
                   5248: <resource id="2" src="$mapurl"></resource>
                   5249: <resource id="3" type="finish"></resource>
                   5250: <link index="1" from="1" to="2"></link>
                   5251: <link index="2" from="2" to="3"></link>
                   5252: </map>
                   5253: ENDINITMAP
                   5254:         $topurl=&declutter(
1.638     albertel 5255:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      5256:                           );
                   5257:     }
                   5258: # ----------------------------------------------------------- Write preferences
1.84      www      5259:     &writecoursepref($udom.'_'.$uname,
                   5260:                      ('description' => $description,
1.271     www      5261:                       'url'         => $topurl));
1.84      www      5262:     return '/'.$udom.'/'.$uname;
                   5263: }
                   5264: 
1.813     albertel 5265: sub is_course {
                   5266:     my ($cdom,$cnum) = @_;
                   5267:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
                   5268: 				undef,'.');
                   5269:     if (exists($courses{$cdom.'_'.$cnum})) {
                   5270:         return 1;
                   5271:     }
                   5272:     return 0;
                   5273: }
                   5274: 
1.21      www      5275: # ---------------------------------------------------------- Assign Custom Role
                   5276: 
                   5277: sub assigncustomrole {
1.357     www      5278:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21      www      5279:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357     www      5280:                        $end,$start,$deleteflag);
1.21      www      5281: }
                   5282: 
                   5283: # ----------------------------------------------------------------- Revoke Role
                   5284: 
                   5285: sub revokerole {
1.357     www      5286:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21      www      5287:     my $now=time;
1.357     www      5288:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21      www      5289: }
                   5290: 
                   5291: # ---------------------------------------------------------- Revoke Custom Role
                   5292: 
                   5293: sub revokecustomrole {
1.357     www      5294:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21      www      5295:     my $now=time;
1.357     www      5296:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
                   5297:            $deleteflag);
1.17      www      5298: }
                   5299: 
1.533     banghart 5300: # ------------------------------------------------------------ Disk usage
1.535     albertel 5301: sub diskusage {
1.533     banghart 5302:     my ($udom,$uname,$directoryRoot)=@_;
                   5303:     $directoryRoot =~ s/\/$//;
1.535     albertel 5304:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514     albertel 5305:     return $listing;
1.512     banghart 5306: }
                   5307: 
1.566     banghart 5308: sub is_locked {
                   5309:     my ($file_name, $domain, $user) = @_;
                   5310:     my @check;
                   5311:     my $is_locked;
                   5312:     push @check, $file_name;
1.613     albertel 5313:     my %locked = &get('file_permissions',\@check,
1.620     albertel 5314: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 5315:     my ($tmp)=keys(%locked);
                   5316:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  5317:     
1.566     banghart 5318:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  5319:         $is_locked = 'false';
                   5320:         foreach my $entry (@{$locked{$file_name}}) {
                   5321:            if (ref($entry) eq 'ARRAY') { 
1.746     raeburn  5322:                $is_locked = 'true';
                   5323:                last;
1.745     raeburn  5324:            }
                   5325:        }
1.566     banghart 5326:     } else {
                   5327:         $is_locked = 'false';
                   5328:     }
                   5329: }
                   5330: 
1.759     albertel 5331: sub declutter_portfile {
                   5332:     my ($file) = @_;
1.833     albertel 5333:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 5334:     return $file;
                   5335: }
                   5336: 
1.559     banghart 5337: # ------------------------------------------------------------- Mark as Read Only
                   5338: 
                   5339: sub mark_as_readonly {
                   5340:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 5341:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5342:     my ($tmp)=keys(%current_permissions);
                   5343:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 5344:     foreach my $file (@{$files}) {
1.759     albertel 5345: 	$file = &declutter_portfile($file);
1.561     banghart 5346:         push(@{$current_permissions{$file}},$what);
1.559     banghart 5347:     }
1.613     albertel 5348:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5349:     return;
                   5350: }
                   5351: 
1.572     banghart 5352: # ------------------------------------------------------------Save Selected Files
                   5353: 
                   5354: sub save_selected_files {
                   5355:     my ($user, $path, @files) = @_;
                   5356:     my $filename = $user."savedfiles";
1.573     banghart 5357:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 5358:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 5359:     foreach my $file (@files) {
1.620     albertel 5360:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 5361:     }
                   5362:     foreach my $file (@other_files) {
1.574     banghart 5363:         print (OUT $file."\n");
1.572     banghart 5364:     }
1.574     banghart 5365:     close (OUT);
1.572     banghart 5366:     return 'ok';
                   5367: }
                   5368: 
1.574     banghart 5369: sub clear_selected_files {
                   5370:     my ($user) = @_;
                   5371:     my $filename = $user."savedfiles";
                   5372:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5373:     print (OUT undef);
                   5374:     close (OUT);
                   5375:     return ("ok");    
                   5376: }
                   5377: 
1.572     banghart 5378: sub files_in_path {
                   5379:     my ($user, $path) = @_;
                   5380:     my $filename = $user."savedfiles";
                   5381:     my %return_files;
1.574     banghart 5382:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5383:     while (my $line_in = <IN>) {
1.574     banghart 5384:         chomp ($line_in);
                   5385:         my @paths_and_file = split (m!/!, $line_in);
                   5386:         my $file_part = pop (@paths_and_file);
                   5387:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 5388:         $path_part.='/';
                   5389:         my $path_and_file = $path_part.$file_part;
                   5390:         if ($path_part eq $path) {
                   5391:             $return_files{$file_part}= 'selected';
                   5392:         }
                   5393:     }
1.574     banghart 5394:     close (IN);
                   5395:     return (\%return_files);
1.572     banghart 5396: }
                   5397: 
                   5398: # called in portfolio select mode, to show files selected NOT in current directory
                   5399: sub files_not_in_path {
                   5400:     my ($user, $path) = @_;
                   5401:     my $filename = $user."savedfiles";
                   5402:     my @return_files;
                   5403:     my $path_part;
1.800     albertel 5404:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5405:     while (my $line = <IN>) {
1.572     banghart 5406:         #ok, I know it's clunky, but I want it to work
1.800     albertel 5407:         my @paths_and_file = split(m|/|, $line);
                   5408:         my $file_part = pop(@paths_and_file);
                   5409:         chomp($file_part);
                   5410:         my $path_part = join('/', @paths_and_file);
1.572     banghart 5411:         $path_part .= '/';
                   5412:         my $path_and_file = $path_part.$file_part;
                   5413:         if ($path_part ne $path) {
1.800     albertel 5414:             push(@return_files, ($path_and_file));
1.572     banghart 5415:         }
                   5416:     }
1.800     albertel 5417:     close(OUT);
1.574     banghart 5418:     return (@return_files);
1.572     banghart 5419: }
                   5420: 
1.745     raeburn  5421: #----------------------------------------------Get portfolio file permissions
1.629     banghart 5422: 
1.745     raeburn  5423: sub get_portfile_permissions {
                   5424:     my ($domain,$user) = @_;
1.613     albertel 5425:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5426:     my ($tmp)=keys(%current_permissions);
                   5427:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5428:     return \%current_permissions;
                   5429: }
                   5430: 
                   5431: #---------------------------------------------Get portfolio file access controls
                   5432: 
1.749     raeburn  5433: sub get_access_controls {
1.745     raeburn  5434:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 5435:     my %access;
                   5436:     my $real_file = $file;
                   5437:     $file =~ s/\.meta$//;
1.745     raeburn  5438:     if (defined($file)) {
1.749     raeburn  5439:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   5440:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 5441:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  5442:             }
                   5443:         }
1.745     raeburn  5444:     } else {
1.749     raeburn  5445:         foreach my $key (keys(%{$current_permissions})) {
                   5446:             if ($key =~ /\0accesscontrol$/) {
                   5447:                 if (defined($group)) {
                   5448:                     if ($key !~ m-^\Q$group\E/-) {
                   5449:                         next;
                   5450:                     }
                   5451:                 }
                   5452:                 my ($fullpath) = split(/\0/,$key);
                   5453:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   5454:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   5455:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   5456:                     }
                   5457:                 }
                   5458:             }
                   5459:         }
                   5460:     }
                   5461:     return %access;
                   5462: }
                   5463: 
                   5464: sub modify_access_controls {
                   5465:     my ($file_name,$changes,$domain,$user)=@_;
                   5466:     my ($outcome,$deloutcome);
                   5467:     my %store_permissions;
                   5468:     my %new_values;
                   5469:     my %new_control;
                   5470:     my %translation;
                   5471:     my @deletions = ();
                   5472:     my $now = time;
                   5473:     if (exists($$changes{'activate'})) {
                   5474:         if (ref($$changes{'activate'}) eq 'HASH') {
                   5475:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   5476:             my $numnew = scalar(@newitems);
                   5477:             for (my $i=0; $i<$numnew; $i++) {
                   5478:                 my $newkey = $newitems[$i];
                   5479:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  5480:                 if ($newkey =~ /^\d+:/) { 
                   5481:                     $newkey =~ s/^(\d+)/$newid/;
                   5482:                     $translation{$1} = $newid;
                   5483:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   5484:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   5485:                     $translation{$1} = $newid;
                   5486:                 }
1.749     raeburn  5487:                 $new_values{$file_name."\0".$newkey} = 
                   5488:                                           $$changes{'activate'}{$newitems[$i]};
                   5489:                 $new_control{$newkey} = $now;
                   5490:             }
                   5491:         }
                   5492:     }
                   5493:     my %todelete;
                   5494:     my %changed_items;
                   5495:     foreach my $action ('delete','update') {
                   5496:         if (exists($$changes{$action})) {
                   5497:             if (ref($$changes{$action}) eq 'HASH') {
                   5498:                 foreach my $key (keys(%{$$changes{$action}})) {
                   5499:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   5500:                     if ($action eq 'delete') { 
                   5501:                         $todelete{$itemnum} = 1;
                   5502:                     } else {
                   5503:                         $changed_items{$itemnum} = $key;
                   5504:                     }
                   5505:                 }
1.745     raeburn  5506:             }
                   5507:         }
1.749     raeburn  5508:     }
                   5509:     # get lock on access controls for file.
                   5510:     my $lockhash = {
                   5511:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   5512:                                                        ':'.$env{'user.domain'},
                   5513:                    }; 
                   5514:     my $tries = 0;
                   5515:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5516:    
                   5517:     while (($gotlock ne 'ok') && $tries <3) {
                   5518:         $tries ++;
                   5519:         sleep 1;
                   5520:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5521:     }
                   5522:     if ($gotlock eq 'ok') {
                   5523:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   5524:         my ($tmp)=keys(%curr_permissions);
                   5525:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   5526:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   5527:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   5528:             if (ref($curr_controls) eq 'HASH') {
                   5529:                 foreach my $control_item (keys(%{$curr_controls})) {
                   5530:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   5531:                     if (defined($todelete{$itemnum})) {
                   5532:                         push(@deletions,$file_name."\0".$control_item);
                   5533:                     } else {
                   5534:                         if (defined($changed_items{$itemnum})) {
                   5535:                             $new_control{$changed_items{$itemnum}} = $now;
                   5536:                             push(@deletions,$file_name."\0".$control_item);
                   5537:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   5538:                         } else {
                   5539:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   5540:                         }
                   5541:                     }
1.745     raeburn  5542:                 }
                   5543:             }
                   5544:         }
1.749     raeburn  5545:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   5546:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   5547:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   5548:         #  remove lock
                   5549:         my @del_lock = ($file_name."\0".'locked_access_records');
                   5550:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  5551:         my ($file,$group);
                   5552:         if (&is_course($domain,$user)) {
                   5553:             ($group,$file) = split(/\//,$file_name,2);
                   5554:         } else {
                   5555:             $file = $file_name;
                   5556:         }
                   5557:         my $sqlresult =
                   5558:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
                   5559:                                     $group);
1.749     raeburn  5560:     } else {
                   5561:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  5562:     }
1.749     raeburn  5563:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  5564: }
                   5565: 
1.827     raeburn  5566: sub make_public_indefinitely {
                   5567:     my ($requrl) = @_;
                   5568:     my $now = time;
                   5569:     my $action = 'activate';
                   5570:     my $aclnum = 0;
                   5571:     if (&is_portfolio_url($requrl)) {
                   5572:         my (undef,$udom,$unum,$file_name,$group) =
                   5573:             &parse_portfolio_url($requrl);
                   5574:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   5575:         my %access_controls = &get_access_controls($current_perms,
                   5576:                                                    $group,$file_name);
                   5577:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   5578:             my ($num,$scope,$end,$start) = 
                   5579:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5580:             if ($scope eq 'public') {
                   5581:                 if ($start <= $now && $end == 0) {
                   5582:                     $action = 'none';
                   5583:                 } else {
                   5584:                     $action = 'update';
                   5585:                     $aclnum = $num;
                   5586:                 }
                   5587:                 last;
                   5588:             }
                   5589:         }
                   5590:         if ($action eq 'none') {
                   5591:              return 'ok';
                   5592:         } else {
                   5593:             my %changes;
                   5594:             my $newend = 0;
                   5595:             my $newstart = $now;
                   5596:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   5597:             $changes{$action}{$newkey} = {
                   5598:                 type => 'public',
                   5599:                 time => {
                   5600:                     start => $newstart,
                   5601:                     end   => $newend,
                   5602:                 },
                   5603:             };
                   5604:             my ($outcome,$deloutcome,$new_values,$translation) =
                   5605:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   5606:             return $outcome;
                   5607:         }
                   5608:     } else {
                   5609:         return 'invalid';
                   5610:     }
                   5611: }
                   5612: 
1.745     raeburn  5613: #------------------------------------------------------Get Marked as Read Only
                   5614: 
                   5615: sub get_marked_as_readonly {
                   5616:     my ($domain,$user,$what,$group) = @_;
                   5617:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 5618:     my @readonly_files;
1.629     banghart 5619:     my $cmp1=$what;
                   5620:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  5621:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5622:         if (defined($group)) {
                   5623:             if ($file_name !~ m-^\Q$group\E/-) {
                   5624:                 next;
                   5625:             }
                   5626:         }
1.561     banghart 5627:         if (ref($value) eq "ARRAY"){
                   5628:             foreach my $stored_what (@{$value}) {
1.629     banghart 5629:                 my $cmp2=$stored_what;
1.759     albertel 5630:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  5631:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  5632:                 }
1.629     banghart 5633:                 if ($cmp1 eq $cmp2) {
1.561     banghart 5634:                     push(@readonly_files, $file_name);
1.745     raeburn  5635:                     last;
1.563     banghart 5636:                 } elsif (!defined($what)) {
                   5637:                     push(@readonly_files, $file_name);
1.745     raeburn  5638:                     last;
1.561     banghart 5639:                 }
                   5640:             }
1.745     raeburn  5641:         }
1.561     banghart 5642:     }
                   5643:     return @readonly_files;
                   5644: }
1.577     banghart 5645: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 5646: 
1.577     banghart 5647: sub get_marked_as_readonly_hash {
1.745     raeburn  5648:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 5649:     my %readonly_files;
1.745     raeburn  5650:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5651:         if (defined($group)) {
                   5652:             if ($file_name !~ m-^\Q$group\E/-) {
                   5653:                 next;
                   5654:             }
                   5655:         }
1.577     banghart 5656:         if (ref($value) eq "ARRAY"){
                   5657:             foreach my $stored_what (@{$value}) {
1.745     raeburn  5658:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 5659:                     foreach my $lock_descriptor(@{$stored_what}) {
                   5660:                         if ($lock_descriptor eq 'graded') {
                   5661:                             $readonly_files{$file_name} = 'graded';
                   5662:                         } elsif ($lock_descriptor eq 'handback') {
                   5663:                             $readonly_files{$file_name} = 'handback';
                   5664:                         } else {
                   5665:                             if (!exists($readonly_files{$file_name})) {
                   5666:                                 $readonly_files{$file_name} = 'locked';
                   5667:                             }
                   5668:                         }
1.745     raeburn  5669:                     }
1.750     banghart 5670:                 } 
1.577     banghart 5671:             }
                   5672:         } 
                   5673:     }
                   5674:     return %readonly_files;
                   5675: }
1.559     banghart 5676: # ------------------------------------------------------------ Unmark as Read Only
                   5677: 
                   5678: sub unmark_as_readonly {
1.629     banghart 5679:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   5680:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  5681:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 5682:     $file_name = &declutter_portfile($file_name);
1.634     albertel 5683:     my $symb_crs = $what;
                   5684:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  5685:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 5686:     my ($tmp)=keys(%current_permissions);
                   5687:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5688:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 5689:     foreach my $file (@readonly_files) {
1.759     albertel 5690: 	my $clean_file = &declutter_portfile($file);
                   5691: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 5692: 	my $current_locks = $current_permissions{$file};
1.563     banghart 5693:         my @new_locks;
                   5694:         my @del_keys;
                   5695:         if (ref($current_locks) eq "ARRAY"){
                   5696:             foreach my $locker (@{$current_locks}) {
1.632     albertel 5697:                 my $compare=$locker;
1.749     raeburn  5698:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  5699:                     $compare=join('',@{$locker});
1.746     raeburn  5700:                     if ($compare ne $symb_crs) {
                   5701:                         push(@new_locks, $locker);
                   5702:                     }
1.563     banghart 5703:                 }
                   5704:             }
1.650     albertel 5705:             if (scalar(@new_locks) > 0) {
1.563     banghart 5706:                 $current_permissions{$file} = \@new_locks;
                   5707:             } else {
                   5708:                 push(@del_keys, $file);
1.613     albertel 5709:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 5710:                 delete($current_permissions{$file});
1.563     banghart 5711:             }
                   5712:         }
1.561     banghart 5713:     }
1.613     albertel 5714:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5715:     return;
                   5716: }
1.512     banghart 5717: 
1.17      www      5718: # ------------------------------------------------------------ Directory lister
                   5719: 
                   5720: sub dirlist {
1.253     stredwic 5721:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   5722: 
1.18      www      5723:     $uri=~s/^\///;
                   5724:     $uri=~s/\/$//;
1.253     stredwic 5725:     my ($udom, $uname);
                   5726:     (undef,$udom,$uname)=split(/\//,$uri);
                   5727:     if(defined($userdomain)) {
                   5728:         $udom = $userdomain;
                   5729:     }
                   5730:     if(defined($username)) {
                   5731:         $uname = $username;
                   5732:     }
                   5733: 
                   5734:     my $dirRoot = $perlvar{'lonDocRoot'};
                   5735:     if(defined($alternateDirectoryRoot)) {
                   5736:         $dirRoot = $alternateDirectoryRoot;
                   5737:         $dirRoot =~ s/\/$//;
1.751     banghart 5738:     }
1.253     stredwic 5739: 
                   5740:     if($udom) {
                   5741:         if($uname) {
1.800     albertel 5742:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   5743: 				 &homeserver($uname,$udom));
1.605     matthew  5744:             my @listing_results;
                   5745:             if ($listing eq 'unknown_cmd') {
1.800     albertel 5746:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   5747: 				  &homeserver($uname,$udom));
1.605     matthew  5748:                 @listing_results = split(/:/,$listing);
                   5749:             } else {
                   5750:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   5751:             }
                   5752:             return @listing_results;
1.253     stredwic 5753:         } elsif(!defined($alternateDirectoryRoot)) {
1.800     albertel 5754:             my %allusers;
1.841     albertel 5755: 	    my %servers = &get_servers($udom,'library');
                   5756: 	    foreach my $tryserver (keys(%servers)) {
                   5757: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5758: 				     $udom, $tryserver);
                   5759: 		my @listing_results;
                   5760: 		if ($listing eq 'unknown_cmd') {
                   5761: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5762: 				      $udom, $tryserver);
                   5763: 		    @listing_results = split(/:/,$listing);
                   5764: 		} else {
                   5765: 		    @listing_results =
                   5766: 			map { &unescape($_); } split(/:/,$listing);
                   5767: 		}
                   5768: 		if ($listing_results[0] ne 'no_such_dir' && 
                   5769: 		    $listing_results[0] ne 'empty'       &&
                   5770: 		    $listing_results[0] ne 'con_lost') {
                   5771: 		    foreach my $line (@listing_results) {
                   5772: 			my ($entry) = split(/&/,$line,2);
                   5773: 			$allusers{$entry} = 1;
                   5774: 		    }
                   5775: 		}
1.253     stredwic 5776:             }
                   5777:             my $alluserstr='';
1.800     albertel 5778:             foreach my $user (sort(keys(%allusers))) {
                   5779:                 $alluserstr.=$user.'&user:';
1.253     stredwic 5780:             }
                   5781:             $alluserstr=~s/:$//;
                   5782:             return split(/:/,$alluserstr);
                   5783:         } else {
1.800     albertel 5784:             return ('missing user name');
1.253     stredwic 5785:         }
                   5786:     } elsif(!defined($alternateDirectoryRoot)) {
1.841     albertel 5787:         my @all_domains = sort(&all_domains());
                   5788:          foreach my $domain (@all_domains) {
                   5789:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   5790:          }
                   5791:          return @all_domains;
                   5792:      } else {
1.800     albertel 5793:         return ('missing domain');
1.275     stredwic 5794:     }
                   5795: }
                   5796: 
                   5797: # --------------------------------------------- GetFileTimestamp
                   5798: # This function utilizes dirlist and returns the date stamp for
                   5799: # when it was last modified.  It will also return an error of -1
                   5800: # if an error occurs
                   5801: 
1.410     matthew  5802: ##
                   5803: ## FIXME: This subroutine assumes its caller knows something about the
                   5804: ## directory structure of the home server for the student ($root).
                   5805: ## Not a good assumption to make.  Since this is for looking up files
                   5806: ## in user directories, the full path should be constructed by lond, not
                   5807: ## whatever machine we request data from.
                   5808: ##
1.275     stredwic 5809: sub GetFileTimestamp {
                   5810:     my ($studentDomain,$studentName,$filename,$root)=@_;
1.807     albertel 5811:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   5812:     $studentName   = &LONCAPA::clean_username($studentName);
1.275     stredwic 5813:     my $subdir=$studentName.'__';
                   5814:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   5815:     my $proname="$studentDomain/$subdir/$studentName";
                   5816:     $proname .= '/'.$filename;
1.375     matthew  5817:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
                   5818:                                               $studentName, $root);
1.275     stredwic 5819:     my @stats = split('&', $fileStat);
                   5820:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  5821:         # @stats contains first the filename, then the stat output
                   5822:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 5823:     } else {
                   5824:         return -1;
1.253     stredwic 5825:     }
1.26      www      5826: }
                   5827: 
1.712     albertel 5828: sub stat_file {
                   5829:     my ($uri) = @_;
1.787     albertel 5830:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 5831: 
1.712     albertel 5832:     my ($udom,$uname,$file,$dir);
                   5833:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   5834: 	($udom,$uname,$file) =
1.811     albertel 5835: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 5836: 	$file = 'userfiles/'.$file;
1.740     www      5837: 	$dir = &propath($udom,$uname);
1.712     albertel 5838:     }
                   5839:     if ($uri =~ m-^/res/-) {
                   5840: 	($udom,$uname) = 
1.807     albertel 5841: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 5842: 	$file = $uri;
                   5843:     }
                   5844: 
                   5845:     if (!$udom || !$uname || !$file) {
                   5846: 	# unable to handle the uri
                   5847: 	return ();
                   5848:     }
                   5849: 
                   5850:     my ($result) = &dirlist($file,$udom,$uname,$dir);
                   5851:     my @stats = split('&', $result);
1.721     banghart 5852:     
1.712     albertel 5853:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   5854: 	shift(@stats); #filename is first
                   5855: 	return @stats;
                   5856:     }
                   5857:     return ();
                   5858: }
                   5859: 
1.26      www      5860: # -------------------------------------------------------- Value of a Condition
                   5861: 
1.713     albertel 5862: # gets the value of a specific preevaluated condition
                   5863: #    stored in the string  $env{user.state.<cid>}
                   5864: # or looks up a condition reference in the bighash and if if hasn't
                   5865: # already been evaluated recurses into docondval to get the value of
                   5866: # the condition, then memoizing it to 
                   5867: #   $env{user.state.<cid>.<condition>}
1.40      www      5868: sub directcondval {
                   5869:     my $number=shift;
1.620     albertel 5870:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 5871: 	&Apache::lonuserstate::evalstate();
                   5872:     }
1.713     albertel 5873:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   5874: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   5875:     } elsif ($number =~ /^_/) {
                   5876: 	my $sub_condition;
                   5877: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   5878: 		&GDBM_READER(),0640)) {
                   5879: 	    $sub_condition=$bighash{'conditions'.$number};
                   5880: 	    untie(%bighash);
                   5881: 	}
                   5882: 	my $value = &docondval($sub_condition);
                   5883: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
                   5884: 	return $value;
                   5885:     }
1.620     albertel 5886:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   5887:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      5888:     } else {
                   5889:        return 2;
                   5890:     }
                   5891: }
                   5892: 
1.713     albertel 5893: # get the collection of conditions for this resource
1.26      www      5894: sub condval {
                   5895:     my $condidx=shift;
1.54      www      5896:     my $allpathcond='';
1.713     albertel 5897:     foreach my $cond (split(/\|/,$condidx)) {
                   5898: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   5899: 	    $allpathcond.=
                   5900: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   5901: 	}
1.191     harris41 5902:     }
1.54      www      5903:     $allpathcond=~s/\|$//;
1.713     albertel 5904:     return &docondval($allpathcond);
                   5905: }
                   5906: 
                   5907: #evaluates an expression of conditions
                   5908: sub docondval {
                   5909:     my ($allpathcond) = @_;
                   5910:     my $result=0;
                   5911:     if ($env{'request.course.id'}
                   5912: 	&& defined($allpathcond)) {
                   5913: 	my $operand='|';
                   5914: 	my @stack;
                   5915: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   5916: 	    if ($chunk eq '(') {
                   5917: 		push @stack,($operand,$result);
                   5918: 	    } elsif ($chunk eq ')') {
                   5919: 		my $before=pop @stack;
                   5920: 		if (pop @stack eq '&') {
                   5921: 		    $result=$result>$before?$before:$result;
                   5922: 		} else {
                   5923: 		    $result=$result>$before?$result:$before;
                   5924: 		}
                   5925: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   5926: 		$operand=$chunk;
                   5927: 	    } else {
                   5928: 		my $new=directcondval($chunk);
                   5929: 		if ($operand eq '&') {
                   5930: 		    $result=$result>$new?$new:$result;
                   5931: 		} else {
                   5932: 		    $result=$result>$new?$result:$new;
                   5933: 		}
                   5934: 	    }
                   5935: 	}
1.26      www      5936:     }
                   5937:     return $result;
1.421     albertel 5938: }
                   5939: 
                   5940: # ---------------------------------------------------- Devalidate courseresdata
                   5941: 
                   5942: sub devalidatecourseresdata {
                   5943:     my ($coursenum,$coursedomain)=@_;
                   5944:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 5945:     &devalidate_cache_new('courseres',$hashid);
1.28      www      5946: }
                   5947: 
1.763     www      5948: 
1.200     www      5949: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     5950: #
                   5951: #  Parameters:
                   5952: #      $coursenum    - Number of the course.
                   5953: #      $coursedomain - Domain at which the course was created.
                   5954: #  Returns:
                   5955: #     A hash of the course parameters along (I think) with timestamps
                   5956: #     and version info.
1.877     foxr     5957: 
1.624     albertel 5958: sub get_courseresdata {
                   5959:     my ($coursenum,$coursedomain)=@_;
1.200     www      5960:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   5961:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 5962:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 5963:     my %dumpreply;
1.417     albertel 5964:     unless (defined($cached)) {
1.624     albertel 5965: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 5966: 	$result=\%dumpreply;
1.251     albertel 5967: 	my ($tmp) = keys(%dumpreply);
                   5968: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 5969: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 5970: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   5971: 	    return $tmp;
1.416     albertel 5972: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 5973: 	    $result=undef;
1.599     albertel 5974: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 5975: 	}
                   5976:     }
1.624     albertel 5977:     return $result;
                   5978: }
                   5979: 
1.633     albertel 5980: sub devalidateuserresdata {
                   5981:     my ($uname,$udom)=@_;
                   5982:     my $hashid="$udom:$uname";
                   5983:     &devalidate_cache_new('userres',$hashid);
                   5984: }
                   5985: 
1.624     albertel 5986: sub get_userresdata {
                   5987:     my ($uname,$udom)=@_;
                   5988:     #most student don\'t have any data set, check if there is some data
                   5989:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   5990: 
                   5991:     my $hashid="$udom:$uname";
                   5992:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   5993:     if (!defined($cached)) {
                   5994: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   5995: 	$result=\%resourcedata;
                   5996: 	&do_cache_new('userres',$hashid,$result,600);
                   5997:     }
                   5998:     my ($tmp)=keys(%$result);
                   5999:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   6000: 	return $result;
                   6001:     }
                   6002:     #error 2 occurs when the .db doesn't exist
                   6003:     if ($tmp!~/error: 2 /) {
1.672     albertel 6004: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 6005: 		 " Trying to get resource data for ".
                   6006: 		 $uname." at ".$udom.": ".
                   6007: 		 $tmp."</font>");
                   6008:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 6009: 	#&EXT_cache_set($udom,$uname);
                   6010: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 6011: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 6012:     }
                   6013:     return $tmp;
                   6014: }
1.879     foxr     6015: #----------------------------------------------- resdata - return resource data
                   6016: #  Purpose:
                   6017: #    Return resource data for either users or for a course.
                   6018: #  Parameters:
                   6019: #     $name      - Course/user name.
                   6020: #     $domain    - Name of the domain the user/course is registered on.
                   6021: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   6022: #     @which     - Array of names of resources desired.
                   6023: #  Returns:
                   6024: #     The value of the first reasource in @which that is found in the
                   6025: #     resource hash.
                   6026: #  Exceptional Conditions:
                   6027: #     If the $type passed in is not valid (not the string 'course' or 
                   6028: #     'user', an undefined  reference is returned.
                   6029: #     If none of the resources are found, an undef is returned
1.624     albertel 6030: sub resdata {
                   6031:     my ($name,$domain,$type,@which)=@_;
                   6032:     my $result;
                   6033:     if ($type eq 'course') {
                   6034: 	$result=&get_courseresdata($name,$domain);
                   6035:     } elsif ($type eq 'user') {
                   6036: 	$result=&get_userresdata($name,$domain);
                   6037:     }
                   6038:     if (!ref($result)) { return $result; }    
1.251     albertel 6039:     foreach my $item (@which) {
1.417     albertel 6040: 	if (defined($result->{$item})) {
                   6041: 	    return $result->{$item};
1.251     albertel 6042: 	}
1.250     albertel 6043:     }
1.291     albertel 6044:     return undef;
1.200     www      6045: }
                   6046: 
1.379     matthew  6047: #
                   6048: # EXT resource caching routines
                   6049: #
                   6050: 
                   6051: sub clear_EXT_cache_status {
1.383     albertel 6052:     &delenv('cache.EXT.');
1.379     matthew  6053: }
                   6054: 
                   6055: sub EXT_cache_status {
                   6056:     my ($target_domain,$target_user) = @_;
1.383     albertel 6057:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 6058:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  6059:         # We know already the user has no data
                   6060:         return 1;
                   6061:     } else {
                   6062:         return 0;
                   6063:     }
                   6064: }
                   6065: 
                   6066: sub EXT_cache_set {
                   6067:     my ($target_domain,$target_user) = @_;
1.383     albertel 6068:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633     albertel 6069:     #&appenv($cachename => time);
1.379     matthew  6070: }
                   6071: 
1.28      www      6072: # --------------------------------------------------------- Value of a Variable
1.58      www      6073: sub EXT {
1.715     albertel 6074: 
1.395     albertel 6075:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      6076:     unless ($varname) { return ''; }
1.218     albertel 6077:     #get real user name/domain, courseid and symb
                   6078:     my $courseid;
1.359     albertel 6079:     my $publicuser;
1.427     www      6080:     if ($symbparm) {
                   6081: 	$symbparm=&get_symb_from_alias($symbparm);
                   6082:     }
1.218     albertel 6083:     if (!($uname && $udom)) {
1.790     albertel 6084:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 6085:       if (!$symbparm) {	$symbparm=$cursymb; }
                   6086:     } else {
1.620     albertel 6087: 	$courseid=$env{'request.course.id'};
1.218     albertel 6088:     }
1.48      www      6089:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   6090:     my $rest;
1.320     albertel 6091:     if (defined($therest[0])) {
1.48      www      6092:        $rest=join('.',@therest);
                   6093:     } else {
                   6094:        $rest='';
                   6095:     }
1.320     albertel 6096: 
1.57      www      6097:     my $qualifierrest=$qualifier;
                   6098:     if ($rest) { $qualifierrest.='.'.$rest; }
                   6099:     my $spacequalifierrest=$space;
                   6100:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      6101:     if ($realm eq 'user') {
1.48      www      6102: # --------------------------------------------------------------- user.resource
                   6103: 	if ($space eq 'resource') {
1.651     albertel 6104: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   6105: 		  || defined($Apache::lonhomework::parsing_a_task))
                   6106: 		 &&
1.744     albertel 6107: 		 ($symbparm eq &symbread()) ) {	
                   6108: 		# if we are in the middle of processing the resource the
                   6109: 		# get the value we are planning on committing
                   6110:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   6111:                     return $Apache::lonhomework::results{$qualifierrest};
                   6112:                 } else {
                   6113:                     return $Apache::lonhomework::history{$qualifierrest};
                   6114:                 }
1.335     albertel 6115: 	    } else {
1.359     albertel 6116: 		my %restored;
1.620     albertel 6117: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 6118: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   6119: 		} else {
                   6120: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   6121: 		}
1.335     albertel 6122: 		return $restored{$qualifierrest};
                   6123: 	    }
1.48      www      6124: # ----------------------------------------------------------------- user.access
                   6125:         } elsif ($space eq 'access') {
1.218     albertel 6126: 	    # FIXME - not supporting calls for a specific user
1.48      www      6127:             return &allowed($qualifier,$rest);
                   6128: # ------------------------------------------ user.preferences, user.environment
                   6129:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 6130: 	    if (($uname eq $env{'user.name'}) &&
                   6131: 		($udom eq $env{'user.domain'})) {
                   6132: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 6133: 	    } else {
1.359     albertel 6134: 		my %returnhash;
                   6135: 		if (!$publicuser) {
                   6136: 		    %returnhash=&userenvironment($udom,$uname,
                   6137: 						 $qualifierrest);
                   6138: 		}
1.218     albertel 6139: 		return $returnhash{$qualifierrest};
                   6140: 	    }
1.48      www      6141: # ----------------------------------------------------------------- user.course
                   6142:         } elsif ($space eq 'course') {
1.218     albertel 6143: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6144:             return $env{join('.',('request.course',$qualifier))};
1.48      www      6145: # ------------------------------------------------------------------- user.role
                   6146:         } elsif ($space eq 'role') {
1.218     albertel 6147: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6148:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      6149:             if ($qualifier eq 'value') {
                   6150: 		return $role;
                   6151:             } elsif ($qualifier eq 'extent') {
                   6152:                 return $where;
                   6153:             }
                   6154: # ----------------------------------------------------------------- user.domain
                   6155:         } elsif ($space eq 'domain') {
1.218     albertel 6156:             return $udom;
1.48      www      6157: # ------------------------------------------------------------------- user.name
                   6158:         } elsif ($space eq 'name') {
1.218     albertel 6159:             return $uname;
1.48      www      6160: # ---------------------------------------------------- Any other user namespace
1.29      www      6161:         } else {
1.359     albertel 6162: 	    my %reply;
                   6163: 	    if (!$publicuser) {
                   6164: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   6165: 	    }
                   6166: 	    return $reply{$qualifierrest};
1.48      www      6167:         }
1.236     www      6168:     } elsif ($realm eq 'query') {
                   6169: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 6170:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   6171: 						[$spacequalifierrest]);
1.620     albertel 6172: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      6173:    } elsif ($realm eq 'request') {
1.48      www      6174: # ------------------------------------------------------------- request.browser
                   6175:         if ($space eq 'browser') {
1.430     www      6176: 	    if ($qualifier eq 'textremote') {
1.676     albertel 6177: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      6178: 		    return 1;
                   6179: 		} else {
                   6180: 		    return 0;
                   6181: 		}
                   6182: 	    } else {
1.620     albertel 6183: 		return $env{'browser.'.$qualifier};
1.430     www      6184: 	    }
1.57      www      6185: # ------------------------------------------------------------ request.filename
                   6186:         } else {
1.620     albertel 6187:             return $env{'request.'.$spacequalifierrest};
1.29      www      6188:         }
1.28      www      6189:     } elsif ($realm eq 'course') {
1.48      www      6190: # ---------------------------------------------------------- course.description
1.620     albertel 6191:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      6192:     } elsif ($realm eq 'resource') {
1.165     www      6193: 
1.620     albertel 6194: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 6195: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   6196: 	}
1.693     albertel 6197: 
                   6198: 	if ($space eq 'title') {
                   6199: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   6200: 	    return &gettitle($symbparm);
                   6201: 	}
                   6202: 	
                   6203: 	if ($space eq 'map') {
                   6204: 	    my ($map) = &decode_symb($symbparm);
                   6205: 	    return &symbread($map);
                   6206: 	}
                   6207: 
                   6208: 	my ($section, $group, @groups);
1.593     albertel 6209: 	my ($courselevelm,$courselevel);
1.539     albertel 6210: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6211: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      6212: 
1.218     albertel 6213: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      6214: 
1.60      www      6215: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 6216: 	    my $symbp=$symbparm;
1.735     albertel 6217: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 6218: 
                   6219: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   6220: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   6221: 
1.620     albertel 6222: 	    if (($env{'user.name'} eq $uname) &&
                   6223: 		($env{'user.domain'} eq $udom)) {
                   6224: 		$section=$env{'request.course.sec'};
1.733     raeburn  6225:                 @groups = split(/:/,$env{'request.course.groups'});  
                   6226:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 6227: 	    } else {
1.539     albertel 6228: 		if (! defined($usection)) {
1.551     albertel 6229: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 6230: 		} else {
                   6231: 		    $section = $usection;
                   6232: 		}
1.733     raeburn  6233:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 6234: 	    }
                   6235: 
                   6236: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   6237: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   6238: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   6239: 
1.593     albertel 6240: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 6241: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 6242: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      6243: 
1.60      www      6244: # ----------------------------------------------------------- first, check user
1.624     albertel 6245: 
                   6246: 	    my $userreply=&resdata($uname,$udom,'user',
                   6247: 				       ($courselevelr,$courselevelm,
                   6248: 					$courselevel));
                   6249: 	    if (defined($userreply)) { return $userreply; }
1.95      www      6250: 
1.594     albertel 6251: # ------------------------------------------------ second, check some of course
1.684     raeburn  6252:             my $coursereply;
1.691     raeburn  6253:             if (@groups > 0) {
                   6254:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   6255:                                        $mapparm,$spacequalifierrest);
1.684     raeburn  6256:                 if (defined($coursereply)) { return $coursereply; }
                   6257:             }
1.96      www      6258: 
1.684     raeburn  6259: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624     albertel 6260: 				     $env{'course.'.$courseid.'.domain'},
                   6261: 				     'course',
                   6262: 				     ($seclevelr,$seclevelm,$seclevel,
                   6263: 				      $courselevelr));
1.287     albertel 6264: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      6265: 
1.60      www      6266: # ------------------------------------------------------ third, check map parms
1.218     albertel 6267: 	    my %parmhash=();
                   6268: 	    my $thisparm='';
                   6269: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 6270: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 6271: 		    &GDBM_READER(),0640)) {
1.218     albertel 6272: 		$thisparm=$parmhash{$symbparm};
                   6273: 		untie(%parmhash);
                   6274: 	    }
                   6275: 	    if ($thisparm) { return $thisparm; }
                   6276: 	}
1.594     albertel 6277: # ------------------------------------------ fourth, look in resource metadata
1.71      www      6278: 
1.218     albertel 6279: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 6280: 	my $filename;
                   6281: 	if (!$symbparm) { $symbparm=&symbread(); }
                   6282: 	if ($symbparm) {
1.409     www      6283: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 6284: 	} else {
1.620     albertel 6285: 	    $filename=$env{'request.filename'};
1.282     albertel 6286: 	}
                   6287: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 6288: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 6289: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 6290: 	if (defined($metadata)) { return $metadata; }
1.142     www      6291: 
1.594     albertel 6292: # ---------------------------------------------- fourth, look in rest pf course
1.593     albertel 6293: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6294: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 6295: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   6296: 				     $env{'course.'.$courseid.'.domain'},
                   6297: 				     'course',
                   6298: 				     ($courselevelm,$courselevel));
1.593     albertel 6299: 	    if (defined($coursereply)) { return $coursereply; }
                   6300: 	}
1.145     www      6301: # ------------------------------------------------------------------ Cascade up
1.218     albertel 6302: 	unless ($space eq '0') {
1.336     albertel 6303: 	    my @parts=split(/_/,$space);
                   6304: 	    my $id=pop(@parts);
                   6305: 	    my $part=join('_',@parts);
                   6306: 	    if ($part eq '') { $part='0'; }
                   6307: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 6308: 				 $symbparm,$udom,$uname,$section,1);
1.337     albertel 6309: 	    if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 6310: 	}
1.395     albertel 6311: 	if ($recurse) { return undef; }
                   6312: 	my $pack_def=&packages_tab_default($filename,$varname);
                   6313: 	if (defined($pack_def)) { return $pack_def; }
1.71      www      6314: 
1.48      www      6315: # ---------------------------------------------------- Any other user namespace
                   6316:     } elsif ($realm eq 'environment') {
                   6317: # ----------------------------------------------------------------- environment
1.620     albertel 6318: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   6319: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 6320: 	} else {
1.770     albertel 6321: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   6322: 		return '';
                   6323: 	    }
1.219     albertel 6324: 	    my %returnhash=&userenvironment($udom,$uname,
                   6325: 					    $spacequalifierrest);
                   6326: 	    return $returnhash{$spacequalifierrest};
                   6327: 	}
1.28      www      6328:     } elsif ($realm eq 'system') {
1.48      www      6329: # ----------------------------------------------------------------- system.time
                   6330: 	if ($space eq 'time') {
                   6331: 	    return time;
                   6332:         }
1.696     albertel 6333:     } elsif ($realm eq 'server') {
                   6334: # ----------------------------------------------------------------- system.time
                   6335: 	if ($space eq 'name') {
                   6336: 	    return $ENV{'SERVER_NAME'};
                   6337:         }
1.28      www      6338:     }
1.48      www      6339:     return '';
1.61      www      6340: }
                   6341: 
1.691     raeburn  6342: sub check_group_parms {
                   6343:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   6344:     my @groupitems = ();
                   6345:     my $resultitem;
                   6346:     my @levels = ($symbparm,$mapparm,$what);
                   6347:     foreach my $group (@{$groups}) {
                   6348:         foreach my $level (@levels) {
                   6349:              my $item = $courseid.'.['.$group.'].'.$level;
                   6350:              push(@groupitems,$item);
                   6351:         }
                   6352:     }
                   6353:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   6354:                             $env{'course.'.$courseid.'.domain'},
                   6355:                                      'course',@groupitems);
                   6356:     return $coursereply;
                   6357: }
                   6358: 
                   6359: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  6360:     my ($courseid,@groups) = @_;
                   6361:     @groups = sort(@groups);
1.691     raeburn  6362:     return @groups;
                   6363: }
                   6364: 
1.395     albertel 6365: sub packages_tab_default {
                   6366:     my ($uri,$varname)=@_;
                   6367:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 6368: 
                   6369:     my (@extension,@specifics,$do_default);
                   6370:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 6371: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 6372: 	if ($pack_type eq 'default') {
                   6373: 	    $do_default=1;
                   6374: 	} elsif ($pack_type eq 'extension') {
                   6375: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 6376: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 6377: 	    # only look at packages defaults for packages that this id is
1.738     albertel 6378: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   6379: 	}
                   6380:     }
                   6381:     # first look for a package that matches the requested part id
                   6382:     foreach my $package (@specifics) {
                   6383: 	my (undef,$pack_type,$pack_part)=@{$package};
                   6384: 	next if ($pack_part ne $part);
                   6385: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6386: 	    return $packagetab{"$pack_type&$name&default"};
                   6387: 	}
                   6388:     }
                   6389:     # look for any possible matching non extension_ package
                   6390:     foreach my $package (@specifics) {
                   6391: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 6392: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6393: 	    return $packagetab{"$pack_type&$name&default"};
                   6394: 	}
1.585     albertel 6395: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 6396: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   6397: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 6398: 	}
                   6399:     }
1.738     albertel 6400:     # look for any posible extension_ match
                   6401:     foreach my $package (@extension) {
                   6402: 	my ($package,$pack_type)=@{$package};
                   6403: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6404: 	    return $packagetab{"$pack_type&$name&default"};
                   6405: 	}
                   6406: 	if (defined($packagetab{$package."&$name&default"})) {
                   6407: 	    return $packagetab{$package."&$name&default"};
                   6408: 	}
                   6409:     }
                   6410:     # look for a global default setting
                   6411:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   6412: 	return $packagetab{"default&$name&default"};
                   6413:     }
1.395     albertel 6414:     return undef;
                   6415: }
                   6416: 
1.334     albertel 6417: sub add_prefix_and_part {
                   6418:     my ($prefix,$part)=@_;
                   6419:     my $keyroot;
                   6420:     if (defined($prefix) && $prefix !~ /^__/) {
                   6421: 	# prefix that has a part already
                   6422: 	$keyroot=$prefix;
                   6423:     } elsif (defined($prefix)) {
                   6424: 	# prefix that is missing a part
                   6425: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   6426:     } else {
                   6427: 	# no prefix at all
                   6428: 	if (defined($part)) { $keyroot='_'.$part; }
                   6429:     }
                   6430:     return $keyroot;
                   6431: }
                   6432: 
1.71      www      6433: # ---------------------------------------------------------------- Get metadata
                   6434: 
1.599     albertel 6435: my %metaentry;
1.71      www      6436: sub metadata {
1.176     www      6437:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      6438:     $uri=&declutter($uri);
1.288     albertel 6439:     # if it is a non metadata possible uri return quickly
1.529     albertel 6440:     if (($uri eq '') || 
                   6441: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 6442: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423     albertel 6443:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.807     albertel 6444: 	($uri =~ m|home/$match_username/public_html/|)) {
1.468     albertel 6445: 	return undef;
1.288     albertel 6446:     }
1.73      www      6447:     my $filename=$uri;
                   6448:     $uri=~s/\.meta$//;
1.172     www      6449: #
                   6450: # Is the metadata already cached?
1.177     www      6451: # Look at timestamp of caching
1.172     www      6452: # Everything is cached by the main uri, libraries are never directly cached
                   6453: #
1.428     albertel 6454:     if (!defined($liburi)) {
1.599     albertel 6455: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 6456: 	if (defined($cached)) { return $result->{':'.$what}; }
                   6457:     }
                   6458:     {
1.172     www      6459: #
                   6460: # Is this a recursive call for a library?
                   6461: #
1.599     albertel 6462: #	if (! exists($metacache{$uri})) {
                   6463: #	    $metacache{$uri}={};
                   6464: #	}
1.171     www      6465:         if ($liburi) {
                   6466: 	    $liburi=&declutter($liburi);
                   6467:             $filename=$liburi;
1.401     bowersj2 6468:         } else {
1.599     albertel 6469: 	    &devalidate_cache_new('meta',$uri);
                   6470: 	    undef(%metaentry);
1.401     bowersj2 6471: 	}
1.140     www      6472:         my %metathesekeys=();
1.73      www      6473:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 6474: 	my $metastring;
1.768     albertel 6475: 	if ($uri !~ m -^(editupload)/-) {
1.543     albertel 6476: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 6477: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 6478: 	    $metastring=&getfile($file);
1.489     albertel 6479: 	}
1.208     albertel 6480:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      6481:         my $token;
1.140     www      6482:         undef %metathesekeys;
1.71      www      6483:         while ($token=$parser->get_token) {
1.339     albertel 6484: 	    if ($token->[0] eq 'S') {
                   6485: 		if (defined($token->[2]->{'package'})) {
1.172     www      6486: #
                   6487: # This is a package - get package info
                   6488: #
1.339     albertel 6489: 		    my $package=$token->[2]->{'package'};
                   6490: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6491: 		    if (defined($token->[2]->{'id'})) { 
                   6492: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   6493: 		    }
1.599     albertel 6494: 		    if ($metaentry{':packages'}) {
                   6495: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 6496: 		    } else {
1.599     albertel 6497: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 6498: 		    }
1.736     albertel 6499: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 6500: 			my $part=$keyroot;
                   6501: 			$part=~s/^\_//;
1.736     albertel 6502: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   6503: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   6504: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 6505: 			    # ignore package.tab specified default values
                   6506:                             # here &package_tab_default() will fetch those
                   6507: 			    if ($subp eq 'default') { next; }
1.736     albertel 6508: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 6509: 			    my $unikey;
                   6510: 			    if ($pack =~ /_0$/) {
                   6511: 				$unikey='parameter_0_'.$name;
                   6512: 				$part=0;
                   6513: 			    } else {
                   6514: 				$unikey='parameter'.$keyroot.'_'.$name;
                   6515: 			    }
1.339     albertel 6516: 			    if ($subp eq 'display') {
                   6517: 				$value.=' [Part: '.$part.']';
                   6518: 			    }
1.599     albertel 6519: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 6520: 			    $metathesekeys{$unikey}=1;
1.599     albertel 6521: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6522: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 6523: 			    }
1.599     albertel 6524: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   6525: 				$metaentry{':'.$unikey}=
                   6526: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 6527: 			    }
1.339     albertel 6528: 			}
                   6529: 		    }
                   6530: 		} else {
1.172     www      6531: #
                   6532: # This is not a package - some other kind of start tag
1.339     albertel 6533: #
                   6534: 		    my $entry=$token->[1];
                   6535: 		    my $unikey;
                   6536: 		    if ($entry eq 'import') {
                   6537: 			$unikey='';
                   6538: 		    } else {
                   6539: 			$unikey=$entry;
                   6540: 		    }
                   6541: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6542: 
                   6543: 		    if (defined($token->[2]->{'id'})) { 
                   6544: 			$unikey.='_'.$token->[2]->{'id'}; 
                   6545: 		    }
1.175     www      6546: 
1.339     albertel 6547: 		    if ($entry eq 'import') {
1.175     www      6548: #
                   6549: # Importing a library here
1.339     albertel 6550: #
                   6551: 			if ($depthcount<20) {
                   6552: 			    my $location=$parser->get_text('/import');
                   6553: 			    my $dir=$filename;
                   6554: 			    $dir=~s|[^/]*$||;
                   6555: 			    $location=&filelocation($dir,$location);
1.736     albertel 6556: 			    my $metadata = 
                   6557: 				&metadata($uri,'keys', $location,$unikey,
                   6558: 					  $depthcount+1);
                   6559: 			    foreach my $meta (split(',',$metadata)) {
                   6560: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   6561: 				$metathesekeys{$meta}=1;
1.339     albertel 6562: 			    }
                   6563: 			}
                   6564: 		    } else { 
                   6565: 			
                   6566: 			if (defined($token->[2]->{'name'})) { 
                   6567: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   6568: 			}
                   6569: 			$metathesekeys{$unikey}=1;
1.736     albertel 6570: 			foreach my $param (@{$token->[3]}) {
                   6571: 			    $metaentry{':'.$unikey.'.'.$param} =
                   6572: 				$token->[2]->{$param};
1.339     albertel 6573: 			}
                   6574: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 6575: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 6576: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   6577: 		 # only ws inside the tag, and not in default, so use default
                   6578: 		 # as value
1.599     albertel 6579: 			    $metaentry{':'.$unikey}=$default;
1.339     albertel 6580: 			} else {
1.321     albertel 6581: 		  # either something interesting inside the tag or default
                   6582:                   # uninteresting
1.599     albertel 6583: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 6584: 			}
1.172     www      6585: # end of not-a-package not-a-library import
1.339     albertel 6586: 		    }
1.172     www      6587: # end of not-a-package start tag
1.339     albertel 6588: 		}
1.172     www      6589: # the next is the end of "start tag"
1.339     albertel 6590: 	    }
                   6591: 	}
1.483     albertel 6592: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 6593: 	$extension = lc($extension);
                   6594: 	if ($extension eq 'htm') { $extension='html'; }
                   6595: 
1.737     albertel 6596: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 6597: 	    #no specific packages #how's our extension
                   6598: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 6599: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 6600: 					 \%metathesekeys);
                   6601: 	}
1.883     albertel 6602: 
                   6603: 	if (!exists($metaentry{':packages'})
                   6604: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 6605: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 6606: 		#no specific packages well let's get default then
                   6607: 		if ($key!~/^default&/) { next; }
1.488     albertel 6608: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 6609: 					     \%metathesekeys);
                   6610: 	    }
                   6611: 	}
1.338     www      6612: # are there custom rights to evaluate
1.599     albertel 6613: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 6614: 
1.338     www      6615:     #
                   6616:     # Importing a rights file here
1.339     albertel 6617:     #
                   6618: 	    unless ($depthcount) {
1.599     albertel 6619: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 6620: 		my $dir=$filename;
                   6621: 		$dir=~s|[^/]*$||;
                   6622: 		$location=&filelocation($dir,$location);
1.736     albertel 6623: 		my $rights_metadata =
                   6624: 		    &metadata($uri,'keys',$location,'_rights',
                   6625: 			      $depthcount+1);
                   6626: 		foreach my $rights (split(',',$rights_metadata)) {
                   6627: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   6628: 		    $metathesekeys{$rights}=1;
1.339     albertel 6629: 		}
                   6630: 	    }
                   6631: 	}
1.737     albertel 6632: 	# uniqifiy package listing
                   6633: 	my %seen;
                   6634: 	my @uniq_packages =
                   6635: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   6636: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   6637: 
                   6638: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 6639: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   6640: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699     albertel 6641: 	&do_cache_new('meta',$uri,\%metaentry,60*60);
1.177     www      6642: # this is the end of "was not already recently cached
1.71      www      6643:     }
1.599     albertel 6644:     return $metaentry{':'.$what};
1.261     albertel 6645: }
                   6646: 
1.488     albertel 6647: sub metadata_create_package_def {
1.483     albertel 6648:     my ($uri,$key,$package,$metathesekeys)=@_;
                   6649:     my ($pack,$name,$subp)=split(/\&/,$key);
                   6650:     if ($subp eq 'default') { next; }
                   6651:     
1.599     albertel 6652:     if (defined($metaentry{':packages'})) {
                   6653: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 6654:     } else {
1.599     albertel 6655: 	$metaentry{':packages'}=$package;
1.483     albertel 6656:     }
                   6657:     my $value=$packagetab{$key};
                   6658:     my $unikey;
                   6659:     $unikey='parameter_0_'.$name;
1.599     albertel 6660:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 6661:     $$metathesekeys{$unikey}=1;
1.599     albertel 6662:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6663: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 6664:     }
1.599     albertel 6665:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   6666: 	$metaentry{':'.$unikey}=
                   6667: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 6668:     }
                   6669: }
                   6670: 
1.261     albertel 6671: sub metadata_generate_part0 {
                   6672:     my ($metadata,$metacache,$uri) = @_;
                   6673:     my %allnames;
1.737     albertel 6674:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 6675: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 6676: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   6677: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 6678: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 6679: 	    $allnames{$name}=$part;
                   6680: 	  }
                   6681: 	}
                   6682:     }
                   6683:     foreach my $name (keys(%allnames)) {
                   6684:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 6685:       my $key=":parameter_0_$name";
1.261     albertel 6686:       $$metacache{"$key.part"}='0';
                   6687:       $$metacache{"$key.name"}=$name;
1.428     albertel 6688:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 6689: 					   $allnames{$name}.'_'.$name.
                   6690: 					   '.type'};
1.428     albertel 6691:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 6692: 			     '.display'};
1.644     www      6693:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 6694:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 6695:       $$metacache{"$key.display"}=$olddis;
                   6696:     }
1.71      www      6697: }
                   6698: 
1.764     albertel 6699: # ------------------------------------------------------ Devalidate title cache
                   6700: 
                   6701: sub devalidate_title_cache {
                   6702:     my ($url)=@_;
                   6703:     if (!$env{'request.course.id'}) { return; }
                   6704:     my $symb=&symbread($url);
                   6705:     if (!$symb) { return; }
                   6706:     my $key=$env{'request.course.id'}."\0".$symb;
                   6707:     &devalidate_cache_new('title',$key);
                   6708: }
                   6709: 
1.301     www      6710: # ------------------------------------------------- Get the title of a resource
                   6711: 
                   6712: sub gettitle {
                   6713:     my $urlsymb=shift;
                   6714:     my $symb=&symbread($urlsymb);
1.534     albertel 6715:     if ($symb) {
1.620     albertel 6716: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 6717: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 6718: 	if (defined($cached)) { 
                   6719: 	    return $result;
                   6720: 	}
1.534     albertel 6721: 	my ($map,$resid,$url)=&decode_symb($symb);
                   6722: 	my $title='';
                   6723: 	my %bighash;
1.620     albertel 6724: 	if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.534     albertel 6725: 		&GDBM_READER(),0640)) {
                   6726: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   6727: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
                   6728: 	    untie %bighash;
                   6729: 	}
                   6730: 	$title=~s/\&colon\;/\:/gs;
                   6731: 	if ($title) {
1.599     albertel 6732: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 6733: 	}
                   6734: 	$urlsymb=$url;
                   6735:     }
                   6736:     my $title=&metadata($urlsymb,'title');
                   6737:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   6738:     return $title;
1.301     www      6739: }
1.613     albertel 6740: 
1.614     albertel 6741: sub get_slot {
                   6742:     my ($which,$cnum,$cdom)=@_;
                   6743:     if (!$cnum || !$cdom) {
1.790     albertel 6744: 	(undef,my $courseid)=&whichuser();
1.620     albertel 6745: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   6746: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 6747:     }
1.703     albertel 6748:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   6749:     my %slotinfo;
                   6750:     if (exists($remembered{$key})) {
                   6751: 	$slotinfo{$which} = $remembered{$key};
                   6752:     } else {
                   6753: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   6754: 	&Apache::lonhomework::showhash(%slotinfo);
                   6755: 	my ($tmp)=keys(%slotinfo);
                   6756: 	if ($tmp=~/^error:/) { return (); }
                   6757: 	$remembered{$key} = $slotinfo{$which};
                   6758:     }
1.616     albertel 6759:     if (ref($slotinfo{$which}) eq 'HASH') {
                   6760: 	return %{$slotinfo{$which}};
                   6761:     }
                   6762:     return $slotinfo{$which};
1.614     albertel 6763: }
1.31      www      6764: # ------------------------------------------------- Update symbolic store links
                   6765: 
                   6766: sub symblist {
                   6767:     my ($mapname,%newhash)=@_;
1.438     www      6768:     $mapname=&deversion(&declutter($mapname));
1.31      www      6769:     my %hash;
1.620     albertel 6770:     if (($env{'request.course.fn'}) && (%newhash)) {
                   6771:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6772:                       &GDBM_WRCREAT(),0640)) {
1.711     albertel 6773: 	    foreach my $url (keys %newhash) {
                   6774: 		next if ($url eq 'last_known'
                   6775: 			 && $env{'form.no_update_last_known'});
                   6776: 		$hash{declutter($url)}=&encode_symb($mapname,
                   6777: 						    $newhash{$url}->[1],
                   6778: 						    $newhash{$url}->[0]);
1.191     harris41 6779:             }
1.31      www      6780:             if (untie(%hash)) {
                   6781: 		return 'ok';
                   6782:             }
                   6783:         }
                   6784:     }
                   6785:     return 'error';
1.212     www      6786: }
                   6787: 
                   6788: # --------------------------------------------------------------- Verify a symb
                   6789: 
                   6790: sub symbverify {
1.510     www      6791:     my ($symb,$thisurl)=@_;
                   6792:     my $thisfn=$thisurl;
1.439     www      6793:     $thisfn=&declutter($thisfn);
1.215     www      6794: # direct jump to resource in page or to a sequence - will construct own symbs
                   6795:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   6796: # check URL part
1.409     www      6797:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      6798: 
1.431     www      6799:     unless ($url eq $thisfn) { return 0; }
1.213     www      6800: 
1.216     www      6801:     $symb=&symbclean($symb);
1.510     www      6802:     $thisurl=&deversion($thisurl);
1.439     www      6803:     $thisfn=&deversion($thisfn);
1.213     www      6804: 
                   6805:     my %bighash;
                   6806:     my $okay=0;
1.431     www      6807: 
1.620     albertel 6808:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6809:                             &GDBM_READER(),0640)) {
1.510     www      6810:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      6811:         unless ($ids) { 
1.510     www      6812:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      6813:         }
                   6814:         if ($ids) {
                   6815: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 6816: 	    foreach my $id (split(/\,/,$ids)) {
                   6817: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      6818:                if (
                   6819:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   6820:    eq $symb) { 
1.620     albertel 6821: 		   if (($env{'request.role.adv'}) ||
1.800     albertel 6822: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582     albertel 6823: 		       $okay=1; 
                   6824: 		   }
                   6825: 	       }
1.216     www      6826: 	   }
                   6827:         }
1.213     www      6828: 	untie(%bighash);
                   6829:     }
                   6830:     return $okay;
1.31      www      6831: }
                   6832: 
1.210     www      6833: # --------------------------------------------------------------- Clean-up symb
                   6834: 
                   6835: sub symbclean {
                   6836:     my $symb=shift;
1.568     albertel 6837:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      6838: # remove version from map
                   6839:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      6840: 
1.210     www      6841: # remove version from URL
                   6842:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      6843: 
1.507     www      6844: # remove wrapper
                   6845: 
1.510     www      6846:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 6847:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      6848:     return $symb;
1.409     www      6849: }
                   6850: 
                   6851: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 6852: 
                   6853: sub encode_symb {
                   6854:     my ($map,$resid,$url)=@_;
                   6855:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   6856: }
1.409     www      6857: 
                   6858: sub decode_symb {
1.568     albertel 6859:     my $symb=shift;
                   6860:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   6861:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      6862:     return (&fixversion($map),$resid,&fixversion($url));
                   6863: }
                   6864: 
                   6865: sub fixversion {
                   6866:     my $fn=shift;
1.609     banghart 6867:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      6868:     my %bighash;
                   6869:     my $uri=&clutter($fn);
1.620     albertel 6870:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      6871: # is this cached?
1.599     albertel 6872:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      6873:     if (defined($cached)) { return $result; }
                   6874: # unfortunately not cached, or expired
1.620     albertel 6875:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      6876: 	    &GDBM_READER(),0640)) {
                   6877:  	if ($bighash{'version_'.$uri}) {
                   6878:  	    my $version=$bighash{'version_'.$uri};
1.444     www      6879:  	    unless (($version eq 'mostrecent') || 
                   6880: 		    ($version==&getversion($uri))) {
1.440     www      6881:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   6882:  	    }
                   6883:  	}
                   6884:  	untie %bighash;
1.413     www      6885:     }
1.599     albertel 6886:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      6887: }
                   6888: 
                   6889: sub deversion {
                   6890:     my $url=shift;
                   6891:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   6892:     return $url;
1.210     www      6893: }
                   6894: 
1.31      www      6895: # ------------------------------------------------------ Return symb list entry
                   6896: 
                   6897: sub symbread {
1.249     www      6898:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 6899:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 6900:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      6901: # no filename provided? try from environment
1.44      www      6902:     unless ($thisfn) {
1.620     albertel 6903:         if ($env{'request.symb'}) {
                   6904: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 6905: 	}
1.620     albertel 6906: 	$thisfn=$env{'request.filename'};
1.44      www      6907:     }
1.569     albertel 6908:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      6909: # is that filename actually a symb? Verify, clean, and return
                   6910:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 6911: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 6912: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 6913: 	}
1.242     www      6914:     }
1.44      www      6915:     $thisfn=declutter($thisfn);
1.31      www      6916:     my %hash;
1.37      www      6917:     my %bighash;
                   6918:     my $syval='';
1.620     albertel 6919:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  6920:         my $targetfn = $thisfn;
1.609     banghart 6921:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  6922:             $targetfn = 'adm/wrapper/'.$thisfn;
                   6923:         }
1.687     albertel 6924: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   6925: 	    $targetfn=$1;
                   6926: 	}
1.620     albertel 6927:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6928:                       &GDBM_READER(),0640)) {
1.481     raeburn  6929: 	    $syval=$hash{$targetfn};
1.37      www      6930:             untie(%hash);
                   6931:         }
                   6932: # ---------------------------------------------------------- There was an entry
                   6933:         if ($syval) {
1.601     albertel 6934: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 6935: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601     albertel 6936: 		    #&appenv('request.ambiguous' => $thisfn);
1.620     albertel 6937: 		    #return $env{$cache_str}='';
1.601     albertel 6938: 		#}    
                   6939: 		#$syval.=$1;
                   6940: 	    #}
1.37      www      6941:         } else {
                   6942: # ------------------------------------------------------- Was not in symb table
1.620     albertel 6943:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6944:                             &GDBM_READER(),0640)) {
1.37      www      6945: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      6946:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      6947:               unless ($ids) { 
                   6948:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      6949:               }
                   6950:               unless ($ids) {
                   6951: # alias?
                   6952: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      6953:               }
1.37      www      6954:               if ($ids) {
                   6955: # ------------------------------------------------------------------- Has ID(s)
                   6956:                  my @possibilities=split(/\,/,$ids);
1.39      www      6957:                  if ($#possibilities==0) {
                   6958: # ----------------------------------------------- There is only one possibility
1.37      www      6959: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 6960: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   6961: 						    $resid,$thisfn);
1.249     www      6962:                  } elsif (!$donotrecurse) {
1.39      www      6963: # ------------------------------------------ There is more than one possibility
                   6964:                      my $realpossible=0;
1.800     albertel 6965:                      foreach my $id (@possibilities) {
                   6966: 			 my $file=$bighash{'src_'.$id};
1.39      www      6967:                          if (&allowed('bre',$file)) {
1.800     albertel 6968:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      6969:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   6970: 				$realpossible++;
1.626     albertel 6971:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   6972: 						    $resid,$thisfn);
1.39      www      6973:                             }
                   6974: 			 }
1.191     harris41 6975:                      }
1.39      www      6976: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      6977:                  } else {
                   6978:                      $syval='';
1.37      www      6979:                  }
                   6980: 	      }
                   6981:               untie(%bighash)
1.481     raeburn  6982:            }
1.31      www      6983:         }
1.62      www      6984:         if ($syval) {
1.620     albertel 6985: 	    return $env{$cache_str}=$syval;
1.62      www      6986:         }
1.31      www      6987:     }
1.44      www      6988:     &appenv('request.ambiguous' => $thisfn);
1.620     albertel 6989:     return $env{$cache_str}='';
1.31      www      6990: }
                   6991: 
                   6992: # ---------------------------------------------------------- Return random seed
                   6993: 
1.32      www      6994: sub numval {
                   6995:     my $txt=shift;
                   6996:     $txt=~tr/A-J/0-9/;
                   6997:     $txt=~tr/a-j/0-9/;
                   6998:     $txt=~tr/K-T/0-9/;
                   6999:     $txt=~tr/k-t/0-9/;
                   7000:     $txt=~tr/U-Z/0-5/;
                   7001:     $txt=~tr/u-z/0-5/;
                   7002:     $txt=~s/\D//g;
1.564     albertel 7003:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      7004:     return int($txt);
1.368     albertel 7005: }
                   7006: 
1.484     albertel 7007: sub numval2 {
                   7008:     my $txt=shift;
                   7009:     $txt=~tr/A-J/0-9/;
                   7010:     $txt=~tr/a-j/0-9/;
                   7011:     $txt=~tr/K-T/0-9/;
                   7012:     $txt=~tr/k-t/0-9/;
                   7013:     $txt=~tr/U-Z/0-5/;
                   7014:     $txt=~tr/u-z/0-5/;
                   7015:     $txt=~s/\D//g;
                   7016:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7017:     my $total;
                   7018:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 7019:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 7020:     return int($total);
                   7021: }
                   7022: 
1.575     albertel 7023: sub numval3 {
                   7024:     use integer;
                   7025:     my $txt=shift;
                   7026:     $txt=~tr/A-J/0-9/;
                   7027:     $txt=~tr/a-j/0-9/;
                   7028:     $txt=~tr/K-T/0-9/;
                   7029:     $txt=~tr/k-t/0-9/;
                   7030:     $txt=~tr/U-Z/0-5/;
                   7031:     $txt=~tr/u-z/0-5/;
                   7032:     $txt=~s/\D//g;
                   7033:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7034:     my $total;
                   7035:     foreach my $val (@txts) { $total+=$val; }
                   7036:     if ($_64bit) { $total=(($total<<32)>>32); }
                   7037:     return $total;
                   7038: }
                   7039: 
1.675     albertel 7040: sub digest {
                   7041:     my ($data)=@_;
                   7042:     my $digest=&Digest::MD5::md5($data);
                   7043:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   7044:     my ($e,$f);
                   7045:     {
                   7046:         use integer;
                   7047:         $e=($a+$b);
                   7048:         $f=($c+$d);
                   7049:         if ($_64bit) {
                   7050:             $e=(($e<<32)>>32);
                   7051:             $f=(($f<<32)>>32);
                   7052:         }
                   7053:     }
                   7054:     if (wantarray) {
                   7055: 	return ($e,$f);
                   7056:     } else {
                   7057: 	my $g;
                   7058: 	{
                   7059: 	    use integer;
                   7060: 	    $g=($e+$f);
                   7061: 	    if ($_64bit) {
                   7062: 		$g=(($g<<32)>>32);
                   7063: 	    }
                   7064: 	}
                   7065: 	return $g;
                   7066:     }
                   7067: }
                   7068: 
1.368     albertel 7069: sub latest_rnd_algorithm_id {
1.675     albertel 7070:     return '64bit5';
1.366     albertel 7071: }
1.32      www      7072: 
1.503     albertel 7073: sub get_rand_alg {
                   7074:     my ($courseid)=@_;
1.790     albertel 7075:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 7076:     if ($courseid) {
1.620     albertel 7077: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 7078:     }
                   7079:     return &latest_rnd_algorithm_id();
                   7080: }
                   7081: 
1.562     albertel 7082: sub validCODE {
                   7083:     my ($CODE)=@_;
                   7084:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   7085:     return 0;
                   7086: }
                   7087: 
1.491     albertel 7088: sub getCODE {
1.620     albertel 7089:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 7090:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   7091: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   7092: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 7093: 	return $Apache::lonhomework::history{'resource.CODE'};
                   7094:     }
                   7095:     return undef;
                   7096: }
                   7097: 
1.31      www      7098: sub rndseed {
1.155     albertel 7099:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 7100:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896   ! albertel 7101:     if (!defined($symb)) {
1.366     albertel 7102: 	unless ($symb=$wsymb) { return time; }
                   7103:     }
                   7104:     if (!$courseid) { $courseid=$wcourseid; }
                   7105:     if (!$domain) { $domain=$wdomain; }
                   7106:     if (!$username) { $username=$wusername }
1.503     albertel 7107:     my $which=&get_rand_alg();
1.803     albertel 7108: 
1.491     albertel 7109:     if (defined(&getCODE())) {
1.675     albertel 7110: 	if ($which eq '64bit5') {
                   7111: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   7112: 	} elsif ($which eq '64bit4') {
1.575     albertel 7113: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   7114: 	} else {
                   7115: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   7116: 	}
1.675     albertel 7117:     } elsif ($which eq '64bit5') {
                   7118: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 7119:     } elsif ($which eq '64bit4') {
                   7120: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 7121:     } elsif ($which eq '64bit3') {
                   7122: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 7123:     } elsif ($which eq '64bit2') {
                   7124: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 7125:     } elsif ($which eq '64bit') {
                   7126: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   7127:     }
                   7128:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   7129: }
                   7130: 
                   7131: sub rndseed_32bit {
                   7132:     my ($symb,$courseid,$domain,$username)=@_;
                   7133:     {
                   7134: 	use integer;
                   7135: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   7136: 	my $symbseed=numval($symb) << 22;
                   7137: 	my $namechck=unpack("%32C*",$username) << 17;
                   7138: 	my $nameseed=numval($username) << 12;
                   7139: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   7140: 	my $courseseed=unpack("%32C*",$courseid);
                   7141: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 7142: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7143: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7144: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 7145: 	return $num;
                   7146:     }
                   7147: }
                   7148: 
                   7149: sub rndseed_64bit {
                   7150:     my ($symb,$courseid,$domain,$username)=@_;
                   7151:     {
                   7152: 	use integer;
                   7153: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   7154: 	my $symbseed=numval($symb) << 10;
                   7155: 	my $namechck=unpack("%32S*",$username);
                   7156: 	
                   7157: 	my $nameseed=numval($username) << 21;
                   7158: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   7159: 	my $courseseed=unpack("%32S*",$courseid);
                   7160: 	
                   7161: 	my $num1=$symbchck+$symbseed+$namechck;
                   7162: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7163: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7164: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7165: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 7166: 	return "$num1,$num2";
1.155     albertel 7167:     }
1.366     albertel 7168: }
                   7169: 
1.443     albertel 7170: sub rndseed_64bit2 {
                   7171:     my ($symb,$courseid,$domain,$username)=@_;
                   7172:     {
                   7173: 	use integer;
                   7174: 	# strings need to be an even # of cahracters long, it it is odd the
                   7175:         # last characters gets thrown away
                   7176: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7177: 	my $symbseed=numval($symb) << 10;
                   7178: 	my $namechck=unpack("%32S*",$username.' ');
                   7179: 	
                   7180: 	my $nameseed=numval($username) << 21;
1.501     albertel 7181: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7182: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7183: 	
                   7184: 	my $num1=$symbchck+$symbseed+$namechck;
                   7185: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7186: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7187: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 7188: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 7189: 	return "$num1,$num2";
                   7190:     }
                   7191: }
                   7192: 
                   7193: sub rndseed_64bit3 {
                   7194:     my ($symb,$courseid,$domain,$username)=@_;
                   7195:     {
                   7196: 	use integer;
                   7197: 	# strings need to be an even # of cahracters long, it it is odd the
                   7198:         # last characters gets thrown away
                   7199: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7200: 	my $symbseed=numval2($symb) << 10;
                   7201: 	my $namechck=unpack("%32S*",$username.' ');
                   7202: 	
                   7203: 	my $nameseed=numval2($username) << 21;
1.443     albertel 7204: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7205: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7206: 	
                   7207: 	my $num1=$symbchck+$symbseed+$namechck;
                   7208: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7209: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7210: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 7211: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7212: 	
1.503     albertel 7213: 	return "$num1:$num2";
1.443     albertel 7214:     }
                   7215: }
                   7216: 
1.575     albertel 7217: sub rndseed_64bit4 {
                   7218:     my ($symb,$courseid,$domain,$username)=@_;
                   7219:     {
                   7220: 	use integer;
                   7221: 	# strings need to be an even # of cahracters long, it it is odd the
                   7222:         # last characters gets thrown away
                   7223: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7224: 	my $symbseed=numval3($symb) << 10;
                   7225: 	my $namechck=unpack("%32S*",$username.' ');
                   7226: 	
                   7227: 	my $nameseed=numval3($username) << 21;
                   7228: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7229: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7230: 	
                   7231: 	my $num1=$symbchck+$symbseed+$namechck;
                   7232: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7233: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7234: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 7235: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7236: 	
                   7237: 	return "$num1:$num2";
                   7238:     }
                   7239: }
                   7240: 
1.675     albertel 7241: sub rndseed_64bit5 {
                   7242:     my ($symb,$courseid,$domain,$username)=@_;
                   7243:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   7244:     return "$num1:$num2";
                   7245: }
                   7246: 
1.366     albertel 7247: sub rndseed_CODE_64bit {
                   7248:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 7249:     {
1.366     albertel 7250: 	use integer;
1.443     albertel 7251: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 7252: 	my $symbseed=numval2($symb);
1.491     albertel 7253: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7254: 	my $CODEseed=numval(&getCODE());
1.443     albertel 7255: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 7256: 	my $num1=$symbseed+$CODEchck;
                   7257: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7258: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7259: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 7260: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7261: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 7262: 	return "$num1:$num2";
1.366     albertel 7263:     }
                   7264: }
                   7265: 
1.575     albertel 7266: sub rndseed_CODE_64bit4 {
                   7267:     my ($symb,$courseid,$domain,$username)=@_;
                   7268:     {
                   7269: 	use integer;
                   7270: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   7271: 	my $symbseed=numval3($symb);
                   7272: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7273: 	my $CODEseed=numval3(&getCODE());
                   7274: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7275: 	my $num1=$symbseed+$CODEchck;
                   7276: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7277: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7278: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 7279: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7280: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   7281: 	return "$num1:$num2";
                   7282:     }
                   7283: }
                   7284: 
1.675     albertel 7285: sub rndseed_CODE_64bit5 {
                   7286:     my ($symb,$courseid,$domain,$username)=@_;
                   7287:     my $code = &getCODE();
                   7288:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   7289:     return "$num1:$num2";
                   7290: }
                   7291: 
1.366     albertel 7292: sub setup_random_from_rndseed {
                   7293:     my ($rndseed)=@_;
1.503     albertel 7294:     if ($rndseed =~/([,:])/) {
                   7295: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 7296: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   7297:     } else {
                   7298: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 7299:     }
1.36      albertel 7300: }
                   7301: 
1.474     albertel 7302: sub latest_receipt_algorithm_id {
1.835     albertel 7303:     return 'receipt3';
1.474     albertel 7304: }
                   7305: 
1.480     www      7306: sub recunique {
                   7307:     my $fucourseid=shift;
                   7308:     my $unique;
1.835     albertel 7309:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   7310: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7311: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      7312:     } else {
                   7313: 	$unique=$perlvar{'lonReceipt'};
                   7314:     }
                   7315:     return unpack("%32C*",$unique);
                   7316: }
                   7317: 
                   7318: sub recprefix {
                   7319:     my $fucourseid=shift;
                   7320:     my $prefix;
1.835     albertel 7321:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   7322: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7323: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      7324:     } else {
                   7325: 	$prefix=$perlvar{'lonHostID'};
                   7326:     }
                   7327:     return unpack("%32C*",$prefix);
                   7328: }
                   7329: 
1.76      www      7330: sub ireceipt {
1.474     albertel 7331:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 7332: 
                   7333:     my $return =&recprefix($fucourseid).'-';
                   7334: 
                   7335:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   7336: 	$env{'request.state'} eq 'construct') {
                   7337: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   7338: 	return $return;
                   7339:     }
                   7340: 
1.76      www      7341:     my $cuname=unpack("%32C*",$funame);
                   7342:     my $cudom=unpack("%32C*",$fudom);
                   7343:     my $cucourseid=unpack("%32C*",$fucourseid);
                   7344:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      7345:     my $cunique=&recunique($fucourseid);
1.474     albertel 7346:     my $cpart=unpack("%32S*",$part);
1.835     albertel 7347:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   7348: 
1.790     albertel 7349: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 7350: 			       
                   7351: 	$return.= ($cunique%$cuname+
                   7352: 		   $cunique%$cudom+
                   7353: 		   $cusymb%$cuname+
                   7354: 		   $cusymb%$cudom+
                   7355: 		   $cucourseid%$cuname+
                   7356: 		   $cucourseid%$cudom+
                   7357: 		   $cpart%$cuname+
                   7358: 		   $cpart%$cudom);
                   7359:     } else {
                   7360: 	$return.= ($cunique%$cuname+
                   7361: 		   $cunique%$cudom+
                   7362: 		   $cusymb%$cuname+
                   7363: 		   $cusymb%$cudom+
                   7364: 		   $cucourseid%$cuname+
                   7365: 		   $cucourseid%$cudom);
                   7366:     }
                   7367:     return $return;
1.76      www      7368: }
                   7369: 
                   7370: sub receipt {
1.474     albertel 7371:     my ($part)=@_;
1.790     albertel 7372:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 7373:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      7374: }
1.260     ng       7375: 
1.790     albertel 7376: sub whichuser {
                   7377:     my ($passedsymb)=@_;
                   7378:     my ($symb,$courseid,$domain,$name,$publicuser);
                   7379:     if (defined($env{'form.grade_symb'})) {
                   7380: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   7381: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   7382: 	if (!$allowed &&
                   7383: 	    exists($env{'request.course.sec'}) &&
                   7384: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   7385: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   7386: 			      '/'.$env{'request.course.sec'});
                   7387: 	}
                   7388: 	if ($allowed) {
                   7389: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   7390: 	    $courseid=$tmp_courseid;
                   7391: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   7392: 	    ($name)=&get_env_multiple('form.grade_username');
                   7393: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   7394: 	}
                   7395:     }
                   7396:     if (!$passedsymb) {
                   7397: 	$symb=&symbread();
                   7398:     } else {
                   7399: 	$symb=$passedsymb;
                   7400:     }
                   7401:     $courseid=$env{'request.course.id'};
                   7402:     $domain=$env{'user.domain'};
                   7403:     $name=$env{'user.name'};
                   7404:     if ($name eq 'public' && $domain eq 'public') {
                   7405: 	if (!defined($env{'form.username'})) {
                   7406: 	    $env{'form.username'}.=time.rand(10000000);
                   7407: 	}
                   7408: 	$name.=$env{'form.username'};
                   7409:     }
                   7410:     return ($symb,$courseid,$domain,$name,$publicuser);
                   7411: 
                   7412: }
                   7413: 
1.36      albertel 7414: # ------------------------------------------------------------ Serves up a file
1.472     albertel 7415: # returns either the contents of the file or 
                   7416: # -1 if the file doesn't exist
1.481     raeburn  7417: #
                   7418: # if the target is a file that was uploaded via DOCS, 
                   7419: # a check will be made to see if a current copy exists on the local server,
                   7420: # if it does this will be served, otherwise a copy will be retrieved from
                   7421: # the home server for the course and stored in /home/httpd/html/userfiles on
                   7422: # the local server.   
1.472     albertel 7423: 
1.36      albertel 7424: sub getfile {
1.538     albertel 7425:     my ($file) = @_;
1.609     banghart 7426:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 7427:     &repcopy($file);
                   7428:     return &readfile($file);
                   7429: }
                   7430: 
                   7431: sub repcopy_userfile {
                   7432:     my ($file)=@_;
1.609     banghart 7433:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 7434:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 7435:     my ($cdom,$cnum,$filename) = 
1.811     albertel 7436: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 7437:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   7438:     if (-e "$file") {
1.828     www      7439: # we already have a local copy, check it out
1.538     albertel 7440: 	my @fileinfo = stat($file);
1.828     www      7441: 	my $rtncode;
                   7442: 	my $info;
1.538     albertel 7443: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 7444: 	if ($lwpresp ne 'ok') {
1.828     www      7445: # there is no such file anymore, even though we had a local copy
1.482     albertel 7446: 	    if ($rtncode eq '404') {
1.538     albertel 7447: 		unlink($file);
1.482     albertel 7448: 	    }
                   7449: 	    return -1;
                   7450: 	}
                   7451: 	if ($info < $fileinfo[9]) {
1.828     www      7452: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  7453: 	    return 'ok';
1.828     www      7454: 	} else {
                   7455: # the file is outdated, get rid of it
                   7456: 	    unlink($file);
1.482     albertel 7457: 	}
1.828     www      7458:     }
                   7459: # one way or the other, at this point, we don't have the file
                   7460: # construct the correct path for the file
                   7461:     my @parts = ($cdom,$cnum); 
                   7462:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   7463: 	push @parts, split(/\//,$1);
                   7464:     }
                   7465:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   7466:     foreach my $part (@parts) {
                   7467: 	$path .= '/'.$part;
                   7468: 	if (!-e $path) {
                   7469: 	    mkdir($path,0770);
1.482     albertel 7470: 	}
                   7471:     }
1.828     www      7472: # now the path exists for sure
                   7473: # get a user agent
                   7474:     my $ua=new LWP::UserAgent;
                   7475:     my $transferfile=$file.'.in.transfer';
                   7476: # FIXME: this should flock
                   7477:     if (-e $transferfile) { return 'ok'; }
                   7478:     my $request;
                   7479:     $uri=~s/^\///;
1.838     albertel 7480:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828     www      7481:     my $response=$ua->request($request,$transferfile);
                   7482: # did it work?
                   7483:     if ($response->is_error()) {
                   7484: 	unlink($transferfile);
                   7485: 	&logthis("Userfile repcopy failed for $uri");
                   7486: 	return -1;
                   7487:     }
                   7488: # worked, rename the transfer file
                   7489:     rename($transferfile,$file);
1.607     raeburn  7490:     return 'ok';
1.481     raeburn  7491: }
                   7492: 
1.517     albertel 7493: sub tokenwrapper {
                   7494:     my $uri=shift;
1.552     albertel 7495:     $uri=~s|^http\://([^/]+)||;
                   7496:     $uri=~s|^/||;
1.620     albertel 7497:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 7498:     my $token=$1;
1.552     albertel 7499:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   7500:     if ($udom && $uname && $file) {
                   7501: 	$file=~s|(\?\.*)*$||;
1.620     albertel 7502:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.838     albertel 7503:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517     albertel 7504:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   7505:                                '&tokenissued='.$perlvar{'lonHostID'};
                   7506:     } else {
                   7507:         return '/adm/notfound.html';
                   7508:     }
                   7509: }
                   7510: 
1.828     www      7511: # call with reqtype HEAD: get last modification time
                   7512: # call with reqtype GET: get the file contents
                   7513: # Do not call this with reqtype GET for large files! It loads everything into memory
                   7514: #
1.481     raeburn  7515: sub getuploaded {
                   7516:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   7517:     $uri=~s/^\///;
1.838     albertel 7518:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481     raeburn  7519:     my $ua=new LWP::UserAgent;
                   7520:     my $request=new HTTP::Request($reqtype,$uri);
                   7521:     my $response=$ua->request($request);
                   7522:     $$rtncode = $response->code;
1.482     albertel 7523:     if (! $response->is_success()) {
                   7524: 	return 'failed';
                   7525:     }      
                   7526:     if ($reqtype eq 'HEAD') {
1.486     www      7527: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 7528:     } elsif ($reqtype eq 'GET') {
                   7529: 	$$info = $response->content;
1.472     albertel 7530:     }
1.482     albertel 7531:     return 'ok';
1.36      albertel 7532: }
                   7533: 
1.481     raeburn  7534: sub readfile {
                   7535:     my $file = shift;
                   7536:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   7537:     my $fh;
                   7538:     open($fh,"<$file");
                   7539:     my $a='';
1.800     albertel 7540:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  7541:     return $a;
                   7542: }
                   7543: 
1.36      albertel 7544: sub filelocation {
1.590     banghart 7545:     my ($dir,$file) = @_;
                   7546:     my $location;
                   7547:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 7548: 
                   7549:     if ($file =~ m-^/adm/-) {
                   7550: 	$file=~s-^/adm/wrapper/-/-;
                   7551: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   7552:     }
1.882     albertel 7553: 
1.590     banghart 7554:     if ($file=~m:^/~:) { # is a contruction space reference
                   7555:         $location = $file;
                   7556:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 7557:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 7558: 	# is a correct contruction space reference
                   7559:         $location = $file;
1.609     banghart 7560:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 7561:         my ($udom,$uname,$filename)=
1.811     albertel 7562:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 7563:         my $home=&homeserver($uname,$udom);
                   7564:         my $is_me=0;
                   7565:         my @ids=&current_machine_ids();
                   7566:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   7567:         if ($is_me) {
1.740     www      7568:   	    $location=&propath($udom,$uname).
1.590     banghart 7569:   	      '/userfiles/'.$filename;
                   7570:         } else {
                   7571:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   7572:   	      $udom.'/'.$uname.'/'.$filename;
                   7573:         }
1.882     albertel 7574:     } elsif ($file =~ m-^/adm/-) {
                   7575: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 7576:     } else {
                   7577:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   7578:         $file=~s:^/res/:/:;
                   7579:         if ( !( $file =~ m:^/:) ) {
                   7580:             $location = $dir. '/'.$file;
                   7581:         } else {
                   7582:             $location = '/home/httpd/html/res'.$file;
                   7583:         }
1.59      albertel 7584:     }
1.590     banghart 7585:     $location=~s://+:/:g; # remove duplicate /
                   7586:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
                   7587:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   7588:     return $location;
1.46      www      7589: }
1.36      albertel 7590: 
1.46      www      7591: sub hreflocation {
                   7592:     my ($dir,$file)=@_;
1.460     albertel 7593:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666     albertel 7594: 	$file=filelocation($dir,$file);
1.700     albertel 7595:     } elsif ($file=~m-^/adm/-) {
                   7596: 	$file=~s-^/adm/wrapper/-/-;
                   7597: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 7598:     }
                   7599:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   7600: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 7601:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   7602: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 7603:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 7604: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 7605: 	    -/uploaded/$1/$2/-x;
1.46      www      7606:     }
1.462     albertel 7607:     return $file;
1.465     albertel 7608: }
                   7609: 
                   7610: sub current_machine_domains {
1.853     albertel 7611:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   7612: }
                   7613: 
                   7614: sub machine_domains {
                   7615:     my ($hostname) = @_;
1.465     albertel 7616:     my @domains;
1.838     albertel 7617:     my %hostname = &all_hostnames();
1.465     albertel 7618:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  7619: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 7620: 	if ($hostname eq $name) {
1.844     albertel 7621: 	    push(@domains,&host_domain($id));
1.465     albertel 7622: 	}
                   7623:     }
                   7624:     return @domains;
                   7625: }
                   7626: 
                   7627: sub current_machine_ids {
1.853     albertel 7628:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   7629: }
                   7630: 
                   7631: sub machine_ids {
                   7632:     my ($hostname) = @_;
                   7633:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 7634:     my @ids;
1.888     albertel 7635:     my %name_to_host = &all_names();
1.889     albertel 7636:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   7637: 	return @{ $name_to_host{$hostname} };
                   7638:     }
                   7639:     return;
1.31      www      7640: }
                   7641: 
1.824     raeburn  7642: sub additional_machine_domains {
                   7643:     my @domains;
                   7644:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   7645:     while( my $line = <$fh>) {
                   7646:         $line =~ s/\s//g;
                   7647:         push(@domains,$line);
                   7648:     }
                   7649:     return @domains;
                   7650: }
                   7651: 
                   7652: sub default_login_domain {
                   7653:     my $domain = $perlvar{'lonDefDomain'};
                   7654:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   7655:     foreach my $posdom (&current_machine_domains(),
                   7656:                         &additional_machine_domains()) {
                   7657:         if (lc($posdom) eq lc($testdomain)) {
                   7658:             $domain=$posdom;
                   7659:             last;
                   7660:         }
                   7661:     }
                   7662:     return $domain;
                   7663: }
                   7664: 
1.31      www      7665: # ------------------------------------------------------------- Declutters URLs
                   7666: 
                   7667: sub declutter {
                   7668:     my $thisfn=shift;
1.569     albertel 7669:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 7670:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      7671:     $thisfn=~s/^\///;
1.697     albertel 7672:     $thisfn=~s|^adm/wrapper/||;
                   7673:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      7674:     $thisfn=~s/^res\///;
1.235     www      7675:     $thisfn=~s/\?.+$//;
1.268     www      7676:     return $thisfn;
                   7677: }
                   7678: 
                   7679: # ------------------------------------------------------------- Clutter up URLs
                   7680: 
                   7681: sub clutter {
                   7682:     my $thisfn='/'.&declutter(shift);
1.887     albertel 7683:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 7684: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      7685:        $thisfn='/res'.$thisfn; 
                   7686:     }
1.694     albertel 7687:     if ($thisfn !~m|/adm|) {
1.695     albertel 7688: 	if ($thisfn =~ m|/ext/|) {
1.694     albertel 7689: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 7690: 	} else {
                   7691: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   7692: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 7693: 	    if ($embstyle eq 'ssi'
                   7694: 		|| ($embstyle eq 'hdn')
                   7695: 		|| ($embstyle eq 'rat')
                   7696: 		|| ($embstyle eq 'prv')
                   7697: 		|| ($embstyle eq 'ign')) {
                   7698: 		#do nothing with these
                   7699: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 7700: 		|| ($embstyle eq 'emb')
                   7701: 		|| ($embstyle eq 'wrp')) {
                   7702: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 7703: 	    } elsif ($embstyle eq 'unk'
                   7704: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 7705: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 7706: 	    } else {
1.718     www      7707: #		&logthis("Got a blank emb style");
1.695     albertel 7708: 	    }
1.694     albertel 7709: 	}
                   7710:     }
1.31      www      7711:     return $thisfn;
1.12      www      7712: }
                   7713: 
1.787     albertel 7714: sub clutter_with_no_wrapper {
                   7715:     my $uri = &clutter(shift);
                   7716:     if ($uri =~ m-^/adm/-) {
                   7717: 	$uri =~ s-^/adm/wrapper/-/-;
                   7718: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   7719:     }
                   7720:     return $uri;
                   7721: }
                   7722: 
1.557     albertel 7723: sub freeze_escape {
                   7724:     my ($value)=@_;
                   7725:     if (ref($value)) {
                   7726: 	$value=&nfreeze($value);
                   7727: 	return '__FROZEN__'.&escape($value);
                   7728:     }
                   7729:     return &escape($value);
                   7730: }
                   7731: 
1.11      www      7732: 
1.557     albertel 7733: sub thaw_unescape {
                   7734:     my ($value)=@_;
                   7735:     if ($value =~ /^__FROZEN__/) {
                   7736: 	substr($value,0,10,undef);
                   7737: 	$value=&unescape($value);
                   7738: 	return &thaw($value);
                   7739:     }
                   7740:     return &unescape($value);
                   7741: }
                   7742: 
1.436     albertel 7743: sub correct_line_ends {
                   7744:     my ($result)=@_;
                   7745:     $$result =~s/\r\n/\n/mg;
                   7746:     $$result =~s/\r/\n/mg;
1.415     albertel 7747: }
1.1       albertel 7748: # ================================================================ Main Program
                   7749: 
1.184     www      7750: sub goodbye {
1.204     albertel 7751:    &logthis("Starting Shut down");
1.443     albertel 7752: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 7753:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 7754: #converted
1.599     albertel 7755: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 7756:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   7757: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   7758: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 7759: #1.1 only
1.870     albertel 7760: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   7761: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   7762: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   7763: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   7764:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 7765:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   7766:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      7767:    &flushcourselogs();
                   7768:    &logthis("Shutting down");
                   7769: }
                   7770: 
1.852     albertel 7771: sub get_dns {
1.869     albertel 7772:     my ($url,$func,$ignore_cache) = @_;
                   7773:     if (!$ignore_cache) {
                   7774: 	my ($content,$cached)=
                   7775: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   7776: 	if ($cached) {
                   7777: 	    &$func($content);
                   7778: 	    return;
                   7779: 	}
                   7780:     }
                   7781: 
                   7782:     my %alldns;
1.852     albertel 7783:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7784:     foreach my $dns (<$config>) {
                   7785: 	next if ($dns !~ /^\^(\S*)/x);
1.869     albertel 7786: 	$alldns{$1} = 1;
                   7787:     }
                   7788:     while (%alldns) {
                   7789: 	my ($dns) = keys(%alldns);
                   7790: 	delete($alldns{$dns});
1.852     albertel 7791: 	my $ua=new LWP::UserAgent;
                   7792: 	my $request=new HTTP::Request('GET',"http://$dns$url");
                   7793: 	my $response=$ua->request($request);
                   7794: 	next if ($response->is_error());
                   7795: 	my @content = split("\n",$response->content);
1.869     albertel 7796: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 7797: 	&$func(\@content);
1.869     albertel 7798: 	return;
1.852     albertel 7799:     }
                   7800:     close($config);
1.871     albertel 7801:     my $which = (split('/',$url))[3];
                   7802:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   7803:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 7804:     my @content = <$config>;
                   7805:     &$func(\@content);
                   7806:     return;
1.852     albertel 7807: }
1.327     albertel 7808: # ------------------------------------------------------------ Read domain file
                   7809: {
1.852     albertel 7810:     my $loaded;
1.846     albertel 7811:     my %domain;
                   7812: 
1.852     albertel 7813:     sub parse_domain_tab {
                   7814: 	my ($lines) = @_;
                   7815: 	foreach my $line (@$lines) {
                   7816: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      7817: 
1.846     albertel 7818: 	    chomp($line);
1.852     albertel 7819: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 7820: 	    my %this_domain;
                   7821: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   7822: 			       'lang_def', 'city', 'longi', 'lati',
                   7823: 			       'primary') {
                   7824: 		$this_domain{$field} = shift(@elements);
                   7825: 	    }
                   7826: 	    $domain{$name} = \%this_domain;
1.852     albertel 7827: 	}
                   7828:     }
1.864     albertel 7829: 
                   7830:     sub reset_domain_info {
                   7831: 	undef($loaded);
                   7832: 	undef(%domain);
                   7833:     }
                   7834: 
1.852     albertel 7835:     sub load_domain_tab {
1.869     albertel 7836: 	my ($ignore_cache) = @_;
                   7837: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 7838: 	my $fh;
                   7839: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   7840: 	    my @lines = <$fh>;
                   7841: 	    &parse_domain_tab(\@lines);
1.448     albertel 7842: 	}
1.852     albertel 7843: 	close($fh);
                   7844: 	$loaded = 1;
1.327     albertel 7845:     }
1.846     albertel 7846: 
                   7847:     sub domain {
1.852     albertel 7848: 	&load_domain_tab() if (!$loaded);
                   7849: 
1.846     albertel 7850: 	my ($name,$what) = @_;
                   7851: 	return if ( !exists($domain{$name}) );
                   7852: 
                   7853: 	if (!$what) {
                   7854: 	    return $domain{$name}{'description'};
                   7855: 	}
                   7856: 	return $domain{$name}{$what};
                   7857:     }
1.327     albertel 7858: }
                   7859: 
                   7860: 
1.1       albertel 7861: # ------------------------------------------------------------- Read hosts file
                   7862: {
1.838     albertel 7863:     my %hostname;
1.844     albertel 7864:     my %hostdom;
1.845     albertel 7865:     my %libserv;
1.852     albertel 7866:     my $loaded;
1.888     albertel 7867:     my %name_to_host;
1.852     albertel 7868: 
                   7869:     sub parse_hosts_tab {
                   7870: 	my ($file) = @_;
                   7871: 	foreach my $configline (@$file) {
                   7872: 	    next if ($configline =~ /^(\#|\s*$ )/x);
                   7873: 	    next if ($configline =~ /^\^/);
                   7874: 	    chomp($configline);
                   7875: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
                   7876: 	    $name=~s/\s//g;
                   7877: 	    if ($id && $domain && $role && $name) {
                   7878: 		$hostname{$id}=$name;
1.888     albertel 7879: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 7880: 		$hostdom{$id}=$domain;
                   7881: 		if ($role eq 'library') { $libserv{$id}=$name; }
                   7882: 	    }
                   7883: 	}
                   7884:     }
1.864     albertel 7885:     
                   7886:     sub reset_hosts_info {
                   7887: 	&reset_domain_info();
                   7888: 	&reset_hosts_ip_info();
1.892     albertel 7889: 	undef(%name_to_host);
1.864     albertel 7890: 	undef(%hostname);
                   7891: 	undef(%hostdom);
                   7892: 	undef(%libserv);
                   7893: 	undef($loaded);
                   7894:     }
1.1       albertel 7895: 
1.852     albertel 7896:     sub load_hosts_tab {
1.869     albertel 7897: 	my ($ignore_cache) = @_;
                   7898: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 7899: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7900: 	my @config = <$config>;
                   7901: 	&parse_hosts_tab(\@config);
                   7902: 	close($config);
                   7903: 	$loaded=1;
1.1       albertel 7904:     }
1.852     albertel 7905: 
1.838     albertel 7906:     sub hostname {
1.852     albertel 7907: 	&load_hosts_tab() if (!$loaded);
                   7908: 
1.838     albertel 7909: 	my ($lonid) = @_;
                   7910: 	return $hostname{$lonid};
                   7911:     }
1.845     albertel 7912: 
1.838     albertel 7913:     sub all_hostnames {
1.852     albertel 7914: 	&load_hosts_tab() if (!$loaded);
                   7915: 
1.838     albertel 7916: 	return %hostname;
                   7917:     }
1.845     albertel 7918: 
1.888     albertel 7919:     sub all_names {
                   7920: 	&load_hosts_tab() if (!$loaded);
                   7921: 
                   7922: 	return %name_to_host;
                   7923:     }
                   7924: 
1.845     albertel 7925:     sub is_library {
1.852     albertel 7926: 	&load_hosts_tab() if (!$loaded);
                   7927: 
1.845     albertel 7928: 	return exists($libserv{$_[0]});
                   7929:     }
                   7930: 
                   7931:     sub all_library {
1.852     albertel 7932: 	&load_hosts_tab() if (!$loaded);
                   7933: 
1.845     albertel 7934: 	return %libserv;
                   7935:     }
                   7936: 
1.841     albertel 7937:     sub get_servers {
1.852     albertel 7938: 	&load_hosts_tab() if (!$loaded);
                   7939: 
1.841     albertel 7940: 	my ($domain,$type) = @_;
                   7941: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   7942: 	                                          : %hostname;
                   7943: 	my %result;
1.842     albertel 7944: 	if (ref($domain) eq 'ARRAY') {
                   7945: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 7946: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 7947: 		    $result{$host} = $hostname;
                   7948: 		}
                   7949: 	    }
                   7950: 	} else {
                   7951: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   7952: 		if ($hostdom{$host} eq $domain) {
                   7953: 		    $result{$host} = $hostname;
                   7954: 		}
1.841     albertel 7955: 	    }
                   7956: 	}
                   7957: 	return %result;
                   7958:     }
1.845     albertel 7959: 
1.844     albertel 7960:     sub host_domain {
1.852     albertel 7961: 	&load_hosts_tab() if (!$loaded);
                   7962: 
1.844     albertel 7963: 	my ($lonid) = @_;
                   7964: 	return $hostdom{$lonid};
                   7965:     }
                   7966: 
1.841     albertel 7967:     sub all_domains {
1.852     albertel 7968: 	&load_hosts_tab() if (!$loaded);
                   7969: 
1.841     albertel 7970: 	my %seen;
                   7971: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   7972: 	return @uniq;
                   7973:     }
1.1       albertel 7974: }
                   7975: 
1.847     albertel 7976: { 
                   7977:     my %iphost;
1.856     albertel 7978:     my %name_to_ip;
                   7979:     my %lonid_to_ip;
1.869     albertel 7980: 
1.847     albertel 7981:     sub get_hosts_from_ip {
                   7982: 	my ($ip) = @_;
                   7983: 	my %iphosts = &get_iphost();
                   7984: 	if (ref($iphosts{$ip})) {
                   7985: 	    return @{$iphosts{$ip}};
                   7986: 	}
                   7987: 	return;
1.839     albertel 7988:     }
1.864     albertel 7989:     
                   7990:     sub reset_hosts_ip_info {
                   7991: 	undef(%iphost);
                   7992: 	undef(%name_to_ip);
                   7993: 	undef(%lonid_to_ip);
                   7994:     }
1.856     albertel 7995: 
                   7996:     sub get_host_ip {
                   7997: 	my ($lonid) = @_;
                   7998: 	if (exists($lonid_to_ip{$lonid})) {
                   7999: 	    return $lonid_to_ip{$lonid};
                   8000: 	}
                   8001: 	my $name=&hostname($lonid);
                   8002:    	my $ip = gethostbyname($name);
                   8003: 	return if (!$ip || length($ip) ne 4);
                   8004: 	$ip=inet_ntoa($ip);
                   8005: 	$name_to_ip{$name}   = $ip;
                   8006: 	$lonid_to_ip{$lonid} = $ip;
                   8007: 	return $ip;
                   8008:     }
1.847     albertel 8009:     
                   8010:     sub get_iphost {
1.869     albertel 8011: 	my ($ignore_cache) = @_;
1.894     albertel 8012: 
1.869     albertel 8013: 	if (!$ignore_cache) {
                   8014: 	    if (%iphost) {
                   8015: 		return %iphost;
                   8016: 	    }
                   8017: 	    my ($ip_info,$cached)=
                   8018: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   8019: 	    if ($cached) {
                   8020: 		%iphost      = %{$ip_info->[0]};
                   8021: 		%name_to_ip  = %{$ip_info->[1]};
                   8022: 		%lonid_to_ip = %{$ip_info->[2]};
                   8023: 		return %iphost;
                   8024: 	    }
                   8025: 	}
1.894     albertel 8026: 
                   8027: 	# get yesterday's info for fallback
                   8028: 	my %old_name_to_ip;
                   8029: 	my ($ip_info,$cached)=
                   8030: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   8031: 	if ($cached) {
                   8032: 	    %old_name_to_ip = %{$ip_info->[1]};
                   8033: 	}
                   8034: 
1.888     albertel 8035: 	my %name_to_host = &all_names();
                   8036: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 8037: 	    my $ip;
                   8038: 	    if (!exists($name_to_ip{$name})) {
                   8039: 		$ip = gethostbyname($name);
                   8040: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 8041: 		    if (defined($old_name_to_ip{$name})) {
                   8042: 			$ip = $old_name_to_ip{$name};
                   8043: 			&logthis("Can't find $name defaulting to old $ip");
                   8044: 		    } else {
                   8045: 			&logthis("Name $name no IP found");
                   8046: 			next;
                   8047: 		    }
                   8048: 		} else {
                   8049: 		    $ip=inet_ntoa($ip);
1.847     albertel 8050: 		}
                   8051: 		$name_to_ip{$name} = $ip;
                   8052: 	    } else {
                   8053: 		$ip = $name_to_ip{$name};
1.653     albertel 8054: 	    }
1.888     albertel 8055: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   8056: 		$lonid_to_ip{$id} = $ip;
                   8057: 	    }
                   8058: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 8059: 	}
1.869     albertel 8060: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   8061: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 8062: 				      48*60*60);
1.869     albertel 8063: 
1.847     albertel 8064: 	return %iphost;
1.598     albertel 8065:     }
                   8066: }
                   8067: 
1.862     albertel 8068: BEGIN {
                   8069: 
                   8070: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   8071:     unless ($readit) {
                   8072: {
                   8073:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   8074:     %perlvar = (%perlvar,%{$configvars});
                   8075: }
                   8076: 
                   8077: 
1.1       albertel 8078: # ------------------------------------------------------ Read spare server file
                   8079: {
1.448     albertel 8080:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 8081: 
                   8082:     while (my $configline=<$config>) {
                   8083:        chomp($configline);
1.284     matthew  8084:        if ($configline) {
1.784     albertel 8085: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 8086: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 8087: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 8088:        }
                   8089:     }
1.448     albertel 8090:     close($config);
1.1       albertel 8091: }
1.11      www      8092: # ------------------------------------------------------------ Read permissions
                   8093: {
1.448     albertel 8094:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      8095: 
                   8096:     while (my $configline=<$config>) {
1.448     albertel 8097: 	chomp($configline);
                   8098: 	if ($configline) {
                   8099: 	    my ($role,$perm)=split(/ /,$configline);
                   8100: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   8101: 	}
1.11      www      8102:     }
1.448     albertel 8103:     close($config);
1.11      www      8104: }
                   8105: 
                   8106: # -------------------------------------------- Read plain texts for permissions
                   8107: {
1.448     albertel 8108:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      8109: 
                   8110:     while (my $configline=<$config>) {
1.448     albertel 8111: 	chomp($configline);
                   8112: 	if ($configline) {
1.742     raeburn  8113: 	    my ($short,@plain)=split(/:/,$configline);
                   8114:             %{$prp{$short}} = ();
                   8115: 	    if (@plain > 0) {
                   8116:                 $prp{$short}{'std'} = $plain[0];
                   8117:                 for (my $i=1; $i<@plain; $i++) {
                   8118:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   8119:                 }
                   8120:             }
1.448     albertel 8121: 	}
1.135     www      8122:     }
1.448     albertel 8123:     close($config);
1.135     www      8124: }
                   8125: 
                   8126: # ---------------------------------------------------------- Read package table
                   8127: {
1.448     albertel 8128:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      8129: 
                   8130:     while (my $configline=<$config>) {
1.483     albertel 8131: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 8132: 	chomp($configline);
                   8133: 	my ($short,$plain)=split(/:/,$configline);
                   8134: 	my ($pack,$name)=split(/\&/,$short);
                   8135: 	if ($plain ne '') {
                   8136: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   8137: 	    $packagetab{$short}=$plain; 
                   8138: 	}
1.11      www      8139:     }
1.448     albertel 8140:     close($config);
1.329     matthew  8141: }
                   8142: 
                   8143: # ------------- set up temporary directory
                   8144: {
                   8145:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   8146: 
1.11      www      8147: }
                   8148: 
1.794     albertel 8149: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   8150: 				'compress_threshold'=> 20_000,
                   8151:  			        });
1.185     www      8152: 
1.281     www      8153: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      8154: $dumpcount=0;
1.22      www      8155: 
1.163     harris41 8156: &logtouch();
1.672     albertel 8157: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      8158: $readit=1;
1.564     albertel 8159:     {
                   8160: 	use integer;
                   8161: 	my $test=(2**32)+1;
1.568     albertel 8162: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 8163: 	&logthis(" Detected 64bit platform ($_64bit)");
                   8164:     }
1.195     www      8165: }
1.1       albertel 8166: }
1.179     www      8167: 
1.1       albertel 8168: 1;
1.191     harris41 8169: __END__
                   8170: 
1.243     albertel 8171: =pod
                   8172: 
1.191     harris41 8173: =head1 NAME
                   8174: 
1.243     albertel 8175: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 8176: 
                   8177: =head1 SYNOPSIS
                   8178: 
1.243     albertel 8179: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 8180: 
                   8181:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   8182: 
1.243     albertel 8183: Common parameters:
                   8184: 
                   8185: =over 4
                   8186: 
                   8187: =item *
                   8188: 
                   8189: $uname : an internal username (if $cname expecting a course Id specifically)
                   8190: 
                   8191: =item *
                   8192: 
                   8193: $udom : a domain (if $cdom expecting a course's domain specifically)
                   8194: 
                   8195: =item *
                   8196: 
                   8197: $symb : a resource instance identifier
                   8198: 
                   8199: =item *
                   8200: 
                   8201: $namespace : the name of a .db file that contains the data needed or
                   8202: being set.
                   8203: 
                   8204: =back
                   8205: 
1.394     bowersj2 8206: =head1 OVERVIEW
1.191     harris41 8207: 
1.394     bowersj2 8208: lonnet provides subroutines which interact with the
                   8209: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   8210: about classes, users, and resources.
1.243     albertel 8211: 
                   8212: For many of these objects you can also use this to store data about
                   8213: them or modify them in various ways.
1.191     harris41 8214: 
1.394     bowersj2 8215: =head2 Symbs
1.191     harris41 8216: 
1.394     bowersj2 8217: To identify a specific instance of a resource, LON-CAPA uses symbols
                   8218: or "symbs"X<symb>. These identifiers are built from the URL of the
                   8219: map, the resource number of the resource in the map, and the URL of
                   8220: the resource itself. The latter is somewhat redundant, but might help
                   8221: if maps change.
                   8222: 
                   8223: An example is
                   8224: 
                   8225:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   8226: 
                   8227: The respective map entry is
                   8228: 
                   8229:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   8230:   title="Problem 2">
                   8231:  </resource>
                   8232: 
                   8233: Symbs are used by the random number generator, as well as to store and
                   8234: restore data specific to a certain instance of for example a problem.
                   8235: 
                   8236: =head2 Storing And Retrieving Data
                   8237: 
                   8238: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   8239: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   8240: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   8241: is is the non-critical message twin of cstore. These functions are for
                   8242: handlers to store a perl hash to a user's permanent data space in an
                   8243: easy manner, and to retrieve it again on another call. It is expected
                   8244: that a handler would use this once at the beginning to retrieve data,
                   8245: and then again once at the end to send only the new data back.
                   8246: 
                   8247: The data is stored in the user's data directory on the user's
                   8248: homeserver under the ID of the course.
                   8249: 
                   8250: The hash that is returned by restore will have all of the previous
                   8251: value for all of the elements of the hash.
                   8252: 
                   8253: Example:
                   8254: 
                   8255:  #creating a hash
                   8256:  my %hash;
                   8257:  $hash{'foo'}='bar';
                   8258: 
                   8259:  #storing it
                   8260:  &Apache::lonnet::cstore(\%hash);
                   8261: 
                   8262:  #changing a value
                   8263:  $hash{'foo'}='notbar';
                   8264: 
                   8265:  #adding a new value
                   8266:  $hash{'bar'}='foo';
                   8267:  &Apache::lonnet::cstore(\%hash);
                   8268: 
                   8269:  #retrieving the hash
                   8270:  my %history=&Apache::lonnet::restore();
                   8271: 
                   8272:  #print the hash
                   8273:  foreach my $key (sort(keys(%history))) {
                   8274:    print("\%history{$key} = $history{$key}");
                   8275:  }
                   8276: 
                   8277: Will print out:
1.191     harris41 8278: 
1.394     bowersj2 8279:  %history{1:foo} = bar
                   8280:  %history{1:keys} = foo:timestamp
                   8281:  %history{1:timestamp} = 990455579
                   8282:  %history{2:bar} = foo
                   8283:  %history{2:foo} = notbar
                   8284:  %history{2:keys} = foo:bar:timestamp
                   8285:  %history{2:timestamp} = 990455580
                   8286:  %history{bar} = foo
                   8287:  %history{foo} = notbar
                   8288:  %history{timestamp} = 990455580
                   8289:  %history{version} = 2
                   8290: 
                   8291: Note that the special hash entries C<keys>, C<version> and
                   8292: C<timestamp> were added to the hash. C<version> will be equal to the
                   8293: total number of versions of the data that have been stored. The
                   8294: C<timestamp> attribute will be the UNIX time the hash was
                   8295: stored. C<keys> is available in every historical section to list which
                   8296: keys were added or changed at a specific historical revision of a
                   8297: hash.
                   8298: 
                   8299: B<Warning>: do not store the hash that restore returns directly. This
                   8300: will cause a mess since it will restore the historical keys as if the
                   8301: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 8302: 
1.394     bowersj2 8303: Calling convention:
1.191     harris41 8304: 
1.394     bowersj2 8305:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   8306:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 8307: 
1.394     bowersj2 8308: For more detailed information, see lonnet specific documentation.
1.191     harris41 8309: 
1.394     bowersj2 8310: =head1 RETURN MESSAGES
1.191     harris41 8311: 
1.394     bowersj2 8312: =over 4
1.191     harris41 8313: 
1.394     bowersj2 8314: =item * B<con_lost>: unable to contact remote host
1.191     harris41 8315: 
1.394     bowersj2 8316: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   8317: when the connection is brought back up
1.191     harris41 8318: 
1.394     bowersj2 8319: =item * B<con_failed>: unable to contact remote host and unable to save message
                   8320: for later delivery
1.191     harris41 8321: 
1.394     bowersj2 8322: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 8323: 
1.394     bowersj2 8324: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 8325: that was requested
1.191     harris41 8326: 
1.243     albertel 8327: =back
1.191     harris41 8328: 
1.243     albertel 8329: =head1 PUBLIC SUBROUTINES
1.191     harris41 8330: 
1.243     albertel 8331: =head2 Session Environment Functions
1.191     harris41 8332: 
1.243     albertel 8333: =over 4
1.191     harris41 8334: 
1.394     bowersj2 8335: =item * 
                   8336: X<appenv()>
                   8337: B<appenv(%hash)>: the value of %hash is written to
                   8338: the user envirnoment file, and will be restored for each access this
1.620     albertel 8339: user makes during this session, also modifies the %env for the current
1.394     bowersj2 8340: process
1.191     harris41 8341: 
                   8342: =item *
1.394     bowersj2 8343: X<delenv()>
                   8344: B<delenv($regexp)>: removes all items from the session
                   8345: environment file that matches the regular expression in $regexp. The
1.620     albertel 8346: values are also delted from the current processes %env.
1.191     harris41 8347: 
1.795     albertel 8348: =item * get_env_multiple($name) 
                   8349: 
                   8350: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   8351: values may be defined and end up as an array ref.
                   8352: 
                   8353: returns an array of values
                   8354: 
1.243     albertel 8355: =back
                   8356: 
                   8357: =head2 User Information
1.191     harris41 8358: 
1.243     albertel 8359: =over 4
1.191     harris41 8360: 
                   8361: =item *
1.394     bowersj2 8362: X<queryauthenticate()>
                   8363: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 8364: authentication scheme
                   8365: 
                   8366: =item *
1.394     bowersj2 8367: X<authenticate()>
                   8368: B<authenticate($uname,$upass,$udom)>: try to
                   8369: authenticate user from domain's lib servers (first use the current
                   8370: one). C<$upass> should be the users password.
1.191     harris41 8371: 
                   8372: =item *
1.394     bowersj2 8373: X<homeserver()>
                   8374: B<homeserver($uname,$udom)>: find the server which has
                   8375: the user's directory and files (there must be only one), this caches
                   8376: the answer, and also caches if there is a borken connection.
1.191     harris41 8377: 
                   8378: =item *
1.394     bowersj2 8379: X<idget()>
                   8380: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   8381: (IDs are a unique resource in a domain, there must be only 1 ID per
                   8382: username, and only 1 username per ID in a specific domain) (returns
                   8383: hash: id=>name,id=>name)
1.191     harris41 8384: 
                   8385: =item *
1.394     bowersj2 8386: X<idrget()>
                   8387: B<idrget($udom,@unames)>: find the IDs behind a list of
                   8388: usernames (returns hash: name=>id,name=>id)
1.191     harris41 8389: 
                   8390: =item *
1.394     bowersj2 8391: X<idput()>
                   8392: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 8393: 
                   8394: =item *
1.394     bowersj2 8395: X<rolesinit()>
                   8396: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 8397: 
                   8398: =item *
1.551     albertel 8399: X<getsection()>
                   8400: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 8401: course $cname, return section name/number or '' for "not in course"
                   8402: and '-1' for "no section"
                   8403: 
                   8404: =item *
1.394     bowersj2 8405: X<userenvironment()>
                   8406: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 8407: passed in @what from the requested user's environment, returns a hash
                   8408: 
1.858     raeburn  8409: =item * 
                   8410: X<userlog_query()>
1.859     albertel 8411: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   8412: activity.log file. %filters defines filters applied when parsing the
                   8413: log file. These can be start or end timestamps, or the type of action
                   8414: - log to look for Login or Logout events, check for Checkin or
                   8415: Checkout, role for role selection. The response is in the form
                   8416: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   8417: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  8418: 
1.243     albertel 8419: =back
                   8420: 
                   8421: =head2 User Roles
                   8422: 
                   8423: =over 4
                   8424: 
                   8425: =item *
                   8426: 
1.810     raeburn  8427: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 8428:  F: full access
                   8429:  U,I,K: authentication modes (cxx only)
                   8430:  '': forbidden
                   8431:  1: user needs to choose course
                   8432:  2: browse allowed
1.766     albertel 8433:  A: passphrase authentication needed
1.243     albertel 8434: 
                   8435: =item *
                   8436: 
                   8437: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   8438: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   8439: and course level
                   8440: 
                   8441: =item *
                   8442: 
                   8443: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   8444: explanation of a user role term
                   8445: 
1.832     raeburn  8446: =item *
                   8447: 
1.858     raeburn  8448: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
                   8449: All arguments are optional. Returns a hash of a roles, either for
                   8450: co-author/assistant author roles for a user's Construction Space
                   8451: (default), or if $context is 'user', roles for the user himself,
                   8452: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
                   8453: and value is set to colon-separated start and end times for the role.
                   8454: If no username and domain are specified, will default to current
                   8455: user/domain. Types, roles, and roledoms are references to arrays,
                   8456: of role statuses (active, future or previous), roles 
                   8457: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   8458: to restrict the list of roles reported. If no array ref is 
                   8459: provided for types, will default to return only active roles.
1.834     albertel 8460: 
1.243     albertel 8461: =back
                   8462: 
                   8463: =head2 User Modification
                   8464: 
                   8465: =over 4
                   8466: 
                   8467: =item *
                   8468: 
                   8469: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   8470: user for the level given by URL.  Optional start and end dates (leave empty
                   8471: string or zero for "no date")
1.191     harris41 8472: 
                   8473: =item *
                   8474: 
1.243     albertel 8475: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   8476: change a users, password, possible return values are: ok,
                   8477: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   8478: refused
1.191     harris41 8479: 
                   8480: =item *
                   8481: 
1.243     albertel 8482: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 8483: 
                   8484: =item *
                   8485: 
1.243     albertel 8486: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   8487: modify user
1.191     harris41 8488: 
                   8489: =item *
                   8490: 
1.286     matthew  8491: modifystudent
                   8492: 
                   8493: modify a students enrollment and identification information.
                   8494: The course id is resolved based on the current users environment.  
                   8495: This means the envoking user must be a course coordinator or otherwise
                   8496: associated with a course.
                   8497: 
1.297     matthew  8498: This call is essentially a wrapper for lonnet::modifyuser and
                   8499: lonnet::modify_student_enrollment
1.286     matthew  8500: 
                   8501: Inputs: 
                   8502: 
                   8503: =over 4
                   8504: 
                   8505: =item B<$udom> Students loncapa domain
                   8506: 
                   8507: =item B<$uname> Students loncapa login name
                   8508: 
                   8509: =item B<$uid> Students id/student number
                   8510: 
                   8511: =item B<$umode> Students authentication mode
                   8512: 
                   8513: =item B<$upass> Students password
                   8514: 
                   8515: =item B<$first> Students first name
                   8516: 
                   8517: =item B<$middle> Students middle name
                   8518: 
                   8519: =item B<$last> Students last name
                   8520: 
                   8521: =item B<$gene> Students generation
                   8522: 
                   8523: =item B<$usec> Students section in course
                   8524: 
                   8525: =item B<$end> Unix time of the roles expiration
                   8526: 
                   8527: =item B<$start> Unix time of the roles start date
                   8528: 
                   8529: =item B<$forceid> If defined, allow $uid to be changed
                   8530: 
                   8531: =item B<$desiredhome> server to use as home server for student
                   8532: 
                   8533: =back
1.297     matthew  8534: 
                   8535: =item *
                   8536: 
                   8537: modify_student_enrollment
                   8538: 
                   8539: Change a students enrollment status in a class.  The environment variable
                   8540: 'role.request.course' must be defined for this function to proceed.
                   8541: 
                   8542: Inputs:
                   8543: 
                   8544: =over 4
                   8545: 
                   8546: =item $udom, students domain
                   8547: 
                   8548: =item $uname, students name
                   8549: 
                   8550: =item $uid, students user id
                   8551: 
                   8552: =item $first, students first name
                   8553: 
                   8554: =item $middle
                   8555: 
                   8556: =item $last
                   8557: 
                   8558: =item $gene
                   8559: 
                   8560: =item $usec
                   8561: 
                   8562: =item $end
                   8563: 
                   8564: =item $start
                   8565: 
                   8566: =back
                   8567: 
1.191     harris41 8568: 
                   8569: =item *
                   8570: 
1.243     albertel 8571: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   8572: custom role; give a custom role to a user for the level given by URL.  Specify
                   8573: name and domain of role author, and role name
1.191     harris41 8574: 
                   8575: =item *
                   8576: 
1.243     albertel 8577: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 8578: 
                   8579: =item *
                   8580: 
1.243     albertel 8581: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   8582: 
                   8583: =back
                   8584: 
                   8585: =head2 Course Infomation
                   8586: 
                   8587: =over 4
1.191     harris41 8588: 
                   8589: =item *
                   8590: 
1.631     albertel 8591: coursedescription($courseid) : returns a hash of information about the
                   8592: specified course id, including all environment settings for the
                   8593: course, the description of the course will be in the hash under the
                   8594: key 'description'
1.191     harris41 8595: 
                   8596: =item *
                   8597: 
1.624     albertel 8598: resdata($name,$domain,$type,@which) : request for current parameter
                   8599: setting for a specific $type, where $type is either 'course' or 'user',
                   8600: @what should be a list of parameters to ask about. This routine caches
                   8601: answers for 5 minutes.
1.243     albertel 8602: 
1.877     foxr     8603: =item *
                   8604: 
                   8605: get_courseresdata($courseid, $domain) : dump the entire course resource
                   8606: data base, returning a hash that is keyed by the resource name and has
                   8607: values that are the resource value.  I believe that the timestamps and
                   8608: versions are also returned.
                   8609: 
                   8610: 
1.243     albertel 8611: =back
                   8612: 
                   8613: =head2 Course Modification
                   8614: 
                   8615: =over 4
1.191     harris41 8616: 
                   8617: =item *
                   8618: 
1.243     albertel 8619: writecoursepref($courseid,%prefs) : write preferences (environment
                   8620: database) for a course
1.191     harris41 8621: 
                   8622: =item *
                   8623: 
1.243     albertel 8624: createcourse($udom,$description,$url) : make/modify course
                   8625: 
                   8626: =back
                   8627: 
                   8628: =head2 Resource Subroutines
                   8629: 
                   8630: =over 4
1.191     harris41 8631: 
                   8632: =item *
                   8633: 
1.243     albertel 8634: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 8635: 
                   8636: =item *
                   8637: 
1.243     albertel 8638: repcopy($filename) : subscribes to the requested file, and attempts to
                   8639: replicate from the owning library server, Might return
1.607     raeburn  8640: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   8641: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 8642: resource. Expects the local filesystem pathname
                   8643: (/home/httpd/html/res/....)
                   8644: 
                   8645: =back
                   8646: 
                   8647: =head2 Resource Information
                   8648: 
                   8649: =over 4
1.191     harris41 8650: 
                   8651: =item *
                   8652: 
1.243     albertel 8653: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   8654: a vairety of different possible values, $varname should be a request
                   8655: string, and the other parameters can be used to specify who and what
                   8656: one is asking about.
                   8657: 
                   8658: Possible values for $varname are environment.lastname (or other item
                   8659: from the envirnment hash), user.name (or someother aspect about the
                   8660: user), resource.0.maxtries (or some other part and parameter of a
                   8661: resource)
1.204     albertel 8662: 
                   8663: =item *
                   8664: 
1.243     albertel 8665: directcondval($number) : get current value of a condition; reads from a state
                   8666: string
1.204     albertel 8667: 
                   8668: =item *
                   8669: 
1.243     albertel 8670: condval($condidx) : value of condition index based on state
1.204     albertel 8671: 
                   8672: =item *
                   8673: 
1.243     albertel 8674: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   8675: resource's metadata, $what should be either a specific key, or either
                   8676: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   8677: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   8678: 
                   8679: this function automatically caches all requests
1.191     harris41 8680: 
                   8681: =item *
                   8682: 
1.243     albertel 8683: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   8684: network of library servers; returns file handle of where SQL and regex results
                   8685: will be stored for query
1.191     harris41 8686: 
                   8687: =item *
                   8688: 
1.243     albertel 8689: symbread($filename) : return symbolic list entry (filename argument optional);
                   8690: returns the data handle
1.191     harris41 8691: 
                   8692: =item *
                   8693: 
1.243     albertel 8694: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 8695: a possible symb for the URL in $thisfn, and if is an encryypted
                   8696: resource that the user accessed using /enc/ returns a 1 on success, 0
                   8697: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 8698: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 8699: 
1.191     harris41 8700: 
                   8701: =item *
                   8702: 
1.243     albertel 8703: symbclean($symb) : removes versions numbers from a symb, returns the
                   8704: cleaned symb
1.191     harris41 8705: 
                   8706: =item *
                   8707: 
1.243     albertel 8708: is_on_map($uri) : checks if the $uri is somewhere on the current
                   8709: course map, user must be in a course for it to work.
1.191     harris41 8710: 
                   8711: =item *
                   8712: 
1.243     albertel 8713: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 8714: 
                   8715: =item *
                   8716: 
1.243     albertel 8717: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   8718: a random seed, all arguments are optional, if they aren't sent it uses the
                   8719: environment to derive them. Note: if symb isn't sent and it can't get one
                   8720: from &symbread it will use the current time as its return value
1.191     harris41 8721: 
                   8722: =item *
                   8723: 
1.243     albertel 8724: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   8725: unfakeable, receipt
1.191     harris41 8726: 
                   8727: =item *
                   8728: 
1.620     albertel 8729: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 8730: 
                   8731: =item *
                   8732: 
1.243     albertel 8733: countacc($url) : count the number of accesses to a given URL
1.191     harris41 8734: 
                   8735: =item *
                   8736: 
1.243     albertel 8737: 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 8738: 
                   8739: =item *
                   8740: 
1.243     albertel 8741: 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 8742: 
                   8743: =item *
                   8744: 
1.243     albertel 8745: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 8746: 
                   8747: =item *
                   8748: 
1.243     albertel 8749: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   8750: forcing spreadsheet to reevaluate the resource scores next time.
                   8751: 
                   8752: =back
                   8753: 
                   8754: =head2 Storing/Retreiving Data
                   8755: 
                   8756: =over 4
1.191     harris41 8757: 
                   8758: =item *
                   8759: 
1.243     albertel 8760: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   8761: for this url; hashref needs to be given and should be a \%hashname; the
                   8762: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 8763: be derived from the env
1.191     harris41 8764: 
                   8765: =item *
                   8766: 
1.243     albertel 8767: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   8768: uses critical subroutine
1.191     harris41 8769: 
                   8770: =item *
                   8771: 
1.243     albertel 8772: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   8773: all args are optional
1.191     harris41 8774: 
                   8775: =item *
                   8776: 
1.717     albertel 8777: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   8778: dumps the complete (or key matching regexp) namespace into a hash
                   8779: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   8780: normally &store()ed into
                   8781: 
                   8782: $range should be either an integer '100' (give me the first 100
                   8783:                                            matching records)
                   8784:               or be  two integers sperated by a - with no spaces
                   8785:                  '30-50' (give me the 30th through the 50th matching
                   8786:                           records)
                   8787: 
                   8788: 
                   8789: =item *
                   8790: 
                   8791: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   8792: replaces a &store() version of data with a replacement set of data
                   8793: for a particular resource in a namespace passed in the $storehash hash 
                   8794: reference
                   8795: 
                   8796: =item *
                   8797: 
1.243     albertel 8798: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   8799: works very similar to store/cstore, but all data is stored in a
                   8800: temporary location and can be reset using tmpreset, $storehash should
                   8801: be a hash reference, returns nothing on success
1.191     harris41 8802: 
                   8803: =item *
                   8804: 
1.243     albertel 8805: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   8806: similar to restore, but all data is stored in a temporary location and
                   8807: can be reset using tmpreset. Returns a hash of values on success,
                   8808: error string otherwise.
1.191     harris41 8809: 
                   8810: =item *
                   8811: 
1.243     albertel 8812: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   8813: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 8814: 
                   8815: =item *
                   8816: 
1.243     albertel 8817: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8818: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 8819: 
                   8820: =item *
                   8821: 
1.243     albertel 8822: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   8823: namesp ($udom and $uname are optional)
1.191     harris41 8824: 
                   8825: =item *
                   8826: 
1.702     albertel 8827: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 8828: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 8829: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  8830: 
1.702     albertel 8831: $range should be either an integer '100' (give me the first 100
                   8832:                                            matching records)
                   8833:               or be  two integers sperated by a - with no spaces
                   8834:                  '30-50' (give me the 30th through the 50th matching
                   8835:                           records)
1.449     matthew  8836: =item *
                   8837: 
                   8838: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   8839: $store can be a scalar, an array reference, or if the amount to be 
                   8840: incremented is > 1, a hash reference.
                   8841: 
                   8842: ($udom and $uname are optional)
1.191     harris41 8843: 
                   8844: =item *
                   8845: 
1.243     albertel 8846: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   8847: ($udom and $uname are optional)
1.191     harris41 8848: 
                   8849: =item *
                   8850: 
1.243     albertel 8851: cput($namespace,$storehash,$udom,$uname) : critical put
                   8852: ($udom and $uname are optional)
1.191     harris41 8853: 
                   8854: =item *
                   8855: 
1.748     albertel 8856: newput($namespace,$storehash,$udom,$uname) :
                   8857: 
                   8858: Attempts to store the items in the $storehash, but only if they don't
                   8859: currently exist, if this succeeds you can be certain that you have 
                   8860: successfully created a new key value pair in the $namespace db.
                   8861: 
                   8862: 
                   8863: Args:
                   8864:  $namespace: name of database to store values to
                   8865:  $storehash: hashref to store to the db
                   8866:  $udom: (optional) domain of user containing the db
                   8867:  $uname: (optional) name of user caontaining the db
                   8868: 
                   8869: Returns:
                   8870:  'ok' -> succeeded in storing all keys of $storehash
                   8871:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   8872:                         least <key> already existed in the db (other
                   8873:                         requested keys may also already exist)
                   8874:  'error: <msg>' -> unable to tie the DB or other erorr occured
                   8875:  'con_lost' -> unable to contact request server
                   8876:  'refused' -> action was not allowed by remote machine
                   8877: 
                   8878: 
                   8879: =item *
                   8880: 
1.243     albertel 8881: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8882: reference filled in from namesp (encrypts the return communication)
                   8883: ($udom and $uname are optional)
1.191     harris41 8884: 
                   8885: =item *
                   8886: 
1.243     albertel 8887: log($udom,$name,$home,$message) : write to permanent log for user; use
                   8888: critical subroutine
                   8889: 
1.806     raeburn  8890: =item *
                   8891: 
1.860     raeburn  8892: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   8893: array reference filled in from namespace found in domain level on either
                   8894: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  8895: 
                   8896: =item *
                   8897: 
1.860     raeburn  8898: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   8899: domain level either on specified domain server ($uhome) or primary domain 
                   8900: server ($udom and $uhome are optional)
1.806     raeburn  8901: 
1.243     albertel 8902: =back
                   8903: 
                   8904: =head2 Network Status Functions
                   8905: 
                   8906: =over 4
1.191     harris41 8907: 
                   8908: =item *
                   8909: 
                   8910: dirlist($uri) : return directory list based on URI
                   8911: 
                   8912: =item *
                   8913: 
1.243     albertel 8914: spareserver() : find server with least workload from spare.tab
                   8915: 
                   8916: =back
                   8917: 
                   8918: =head2 Apache Request
                   8919: 
                   8920: =over 4
1.191     harris41 8921: 
                   8922: =item *
                   8923: 
1.243     albertel 8924: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   8925: localhost, posts hash
                   8926: 
                   8927: =back
                   8928: 
                   8929: =head2 Data to String to Data
                   8930: 
                   8931: =over 4
1.191     harris41 8932: 
                   8933: =item *
                   8934: 
1.243     albertel 8935: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   8936: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 8937: 
                   8938: =item *
                   8939: 
1.243     albertel 8940: hashref2str($hashref) : convert a hashref into a string complete with
                   8941: escaping and '=' and '&' separators, supports elements that are
                   8942: arrayrefs and hashrefs
1.191     harris41 8943: 
                   8944: =item *
                   8945: 
1.243     albertel 8946: arrayref2str($arrayref) : convert an arrayref into a string complete
                   8947: with escaping and '&' separators, supports elements that are arrayrefs
                   8948: and hashrefs
1.191     harris41 8949: 
                   8950: =item *
                   8951: 
1.243     albertel 8952: str2hash($string) : convert string to hash using unescaping and
                   8953: splitting on '=' and '&', supports elements that are arrayrefs and
                   8954: hashrefs
1.191     harris41 8955: 
                   8956: =item *
                   8957: 
1.243     albertel 8958: str2array($string) : convert string to hash using unescaping and
                   8959: splitting on '&', supports elements that are arrayrefs and hashrefs
                   8960: 
                   8961: =back
                   8962: 
                   8963: =head2 Logging Routines
                   8964: 
                   8965: =over 4
                   8966: 
                   8967: These routines allow one to make log messages in the lonnet.log and
                   8968: lonnet.perm logfiles.
1.191     harris41 8969: 
                   8970: =item *
                   8971: 
1.243     albertel 8972: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 8973: 
                   8974: =item *
                   8975: 
1.243     albertel 8976: logthis() : append message to the normal lonnet.log file, it gets
                   8977: preiodically rolled over and deleted.
1.191     harris41 8978: 
                   8979: =item *
                   8980: 
1.243     albertel 8981: logperm() : append a permanent message to lonnet.perm.log, this log
                   8982: file never gets deleted by any automated portion of the system, only
                   8983: messages of critical importance should go in here.
                   8984: 
                   8985: =back
                   8986: 
                   8987: =head2 General File Helper Routines
                   8988: 
                   8989: =over 4
1.191     harris41 8990: 
                   8991: =item *
                   8992: 
1.481     raeburn  8993: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   8994: (a) files in /uploaded
                   8995:   (i) If a local copy of the file exists - 
                   8996:       compares modification date of local copy with last-modified date for 
                   8997:       definitive version stored on home server for course. If local copy is 
                   8998:       stale, requests a new version from the home server and stores it. 
                   8999:       If the original has been removed from the home server, then local copy 
                   9000:       is unlinked.
                   9001:   (ii) If local copy does not exist -
                   9002:       requests the file from the home server and stores it. 
                   9003:   
                   9004:   If $caller is 'uploadrep':  
                   9005:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   9006:     for request for files originally uploaded via DOCS. 
                   9007:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   9008:   
                   9009:   Otherwise:
                   9010:      This indicates a call from the content generation phase of the request.
                   9011:      -  returns the entire contents of the file or -1.
                   9012:      
                   9013: (b) files in /res
                   9014:    - returns the entire contents of a file or -1; 
                   9015:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 9016: 
1.712     albertel 9017: 
                   9018: =item *
                   9019: 
                   9020: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   9021:                   reference
                   9022: 
                   9023: returns either a stat() list of data about the file or an empty list
                   9024: if the file doesn't exist or couldn't find out about it (connection
                   9025: problems or user unknown)
                   9026: 
1.191     harris41 9027: =item *
                   9028: 
1.243     albertel 9029: filelocation($dir,$file) : returns file system location of a file
                   9030: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   9031: directory that relative $file lookups are to looked in ($dir of /a/dir
                   9032: and a file of ../bob will become /a/bob)
1.191     harris41 9033: 
                   9034: =item *
                   9035: 
                   9036: hreflocation($dir,$file) : returns file system location or a URL; same as
                   9037: filelocation except for hrefs
                   9038: 
                   9039: =item *
                   9040: 
                   9041: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   9042: 
1.243     albertel 9043: =back
                   9044: 
1.608     albertel 9045: =head2 Usererfile file routines (/uploaded*)
                   9046: 
                   9047: =over 4
                   9048: 
                   9049: =item *
                   9050: 
                   9051: userfileupload(): main rotine for putting a file in a user or course's
                   9052:                   filespace, arguments are,
                   9053: 
1.620     albertel 9054:  formname - required - this is the name of the element in $env where the
1.608     albertel 9055:            filename, and the contents of the file to create/modifed exist
1.620     albertel 9056:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   9057:            contents of the file is located in $env{'form.'.$formname}
1.608     albertel 9058:  coursedoc - if true, store the file in the course of the active role
                   9059:              of the current user
                   9060:  subdir - required - subdirectory to put the file in under ../userfiles/
                   9061:          if undefined, it will be placed in "unknown"
                   9062: 
                   9063:  (This routine calls clean_filename() to remove any dangerous
                   9064:  characters from the filename, and then calls finuserfileupload() to
                   9065:  complete the transaction)
                   9066: 
                   9067:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9068:  and /adm/notfound.html if unsuccessful
                   9069: 
                   9070: =item *
                   9071: 
                   9072: clean_filename(): routine for cleaing a filename up for storage in
                   9073:                  userfile space, argument is:
                   9074: 
                   9075:  filename - proposed filename
                   9076: 
                   9077: returns: the new clean filename
                   9078: 
                   9079: =item *
                   9080: 
                   9081: finishuserfileupload(): routine that creaes and sends the file to
                   9082: userspace, probably shouldn't be called directly
                   9083: 
                   9084:   docuname: username or courseid of destination for the file
                   9085:   docudom: domain of user/course of destination for the file
                   9086:   formname: same as for userfileupload()
                   9087:   fname: filename (inculding subdirectories) for the file
                   9088: 
                   9089:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9090:  and /adm/notfound.html if unsuccessful
                   9091: 
                   9092: =item *
                   9093: 
                   9094: renameuserfile(): renames an existing userfile to a new name
                   9095: 
                   9096:   Args:
                   9097:    docuname: username or courseid of destination for the file
                   9098:    docudom: domain of user/course of destination for the file
                   9099:    old: current file name (including any subdirs under userfiles)
                   9100:    new: desired file name (including any subdirs under userfiles)
                   9101: 
                   9102: =item *
                   9103: 
                   9104: mkdiruserfile(): creates a directory is a userfiles dir
                   9105: 
                   9106:   Args:
                   9107:    docuname: username or courseid of destination for the file
                   9108:    docudom: domain of user/course of destination for the file
                   9109:    dir: dir to create (including any subdirs under userfiles)
                   9110: 
                   9111: =item *
                   9112: 
                   9113: removeuserfile(): removes a file that exists in userfiles
                   9114: 
                   9115:   Args:
                   9116:    docuname: username or courseid of destination for the file
                   9117:    docudom: domain of user/course of destination for the file
                   9118:    fname: filname to delete (including any subdirs under userfiles)
                   9119: 
                   9120: =item *
                   9121: 
                   9122: removeuploadedurl(): convience function for removeuserfile()
                   9123: 
                   9124:   Args:
                   9125:    url:  a full /uploaded/... url to delete
                   9126: 
1.747     albertel 9127: =item * 
                   9128: 
                   9129: get_portfile_permissions():
                   9130:   Args:
                   9131:     domain: domain of user or course contain the portfolio files
                   9132:     user: name of user or num of course contain the portfolio files
                   9133:   Returns:
                   9134:     hashref of a dump of the proper file_permissions.db
                   9135:    
                   9136: 
                   9137: =item * 
                   9138: 
                   9139: get_access_controls():
                   9140: 
                   9141: Args:
                   9142:   current_permissions: the hash ref returned from get_portfile_permissions()
                   9143:   group: (optional) the group you want the files associated with
                   9144:   file: (optional) the file you want access info on
                   9145: 
                   9146: Returns:
1.749     raeburn  9147:     a hash (keys are file names) of hashes containing
                   9148:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   9149:         values are XML containing access control settings (see below) 
1.747     albertel 9150: 
                   9151: Internal notes:
                   9152: 
1.749     raeburn  9153:  access controls are stored in file_permissions.db as key=value pairs.
                   9154:     key -> path to file/file_name\0uniqueID:scope_end_start
                   9155:         where scope -> public,guest,course,group,domains or users.
                   9156:               end -> UNIX time for end of access (0 -> no end date)
                   9157:               start -> UNIX time for start of access
                   9158: 
                   9159:     value -> XML description of access control
                   9160:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   9161:             <start></start>
                   9162:             <end></end>
                   9163: 
                   9164:             <password></password>  for scope type = guest
                   9165: 
                   9166:             <domain></domain>     for scope type = course or group
                   9167:             <number></number>
                   9168:             <roles id="">
                   9169:              <role></role>
                   9170:              <access></access>
                   9171:              <section></section>
                   9172:              <group></group>
                   9173:             </roles>
                   9174: 
                   9175:             <dom></dom>         for scope type = domains
                   9176: 
                   9177:             <users>             for scope type = users
                   9178:              <user>
                   9179:               <uname></uname>
                   9180:               <udom></udom>
                   9181:              </user>
                   9182:             </users>
                   9183:            </scope> 
                   9184:               
                   9185:  Access data is also aggregated for each file in an additional key=value pair:
                   9186:  key -> path to file/file_name\0accesscontrol 
                   9187:  value -> reference to hash
                   9188:           hash contains key = value pairs
                   9189:           where key = uniqueID:scope_end_start
                   9190:                 value = UNIX time record was last updated
                   9191: 
                   9192:           Used to improve speed of look-ups of access controls for each file.  
                   9193:  
                   9194:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   9195: 
                   9196: modify_access_controls():
                   9197: 
                   9198: Modifies access controls for a portfolio file
                   9199: Args
                   9200: 1. file name
                   9201: 2. reference to hash of required changes,
                   9202: 3. domain
                   9203: 4. username
                   9204:   where domain,username are the domain of the portfolio owner 
                   9205:   (either a user or a course) 
                   9206: 
                   9207: Returns:
                   9208: 1. result of additions or updates ('ok' or 'error', with error message). 
                   9209: 2. result of deletions ('ok' or 'error', with error message).
                   9210: 3. reference to hash of any new or updated access controls.
                   9211: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   9212:    key = integer (inbound ID)
                   9213:    value = uniqueID  
1.747     albertel 9214: 
1.608     albertel 9215: =back
                   9216: 
1.243     albertel 9217: =head2 HTTP Helper Routines
                   9218: 
                   9219: =over 4
                   9220: 
1.191     harris41 9221: =item *
                   9222: 
                   9223: escape() : unpack non-word characters into CGI-compatible hex codes
                   9224: 
                   9225: =item *
                   9226: 
                   9227: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   9228: 
1.243     albertel 9229: =back
                   9230: 
                   9231: =head1 PRIVATE SUBROUTINES
                   9232: 
                   9233: =head2 Underlying communication routines (Shouldn't call)
                   9234: 
                   9235: =over 4
                   9236: 
                   9237: =item *
                   9238: 
                   9239: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   9240: 
                   9241: =item *
                   9242: 
                   9243: reply() : uses subreply to send a message to remote machine, logs all failures
                   9244: 
                   9245: =item *
                   9246: 
                   9247: critical() : passes a critical message to another server; if cannot
                   9248: get through then place message in connection buffer directory and
                   9249: returns con_delayed, if incapable of saving message, returns
                   9250: con_failed
                   9251: 
                   9252: =item *
                   9253: 
                   9254: reconlonc() : tries to reconnect lonc client processes.
                   9255: 
                   9256: =back
                   9257: 
                   9258: =head2 Resource Access Logging
                   9259: 
                   9260: =over 4
                   9261: 
                   9262: =item *
                   9263: 
                   9264: flushcourselogs() : flush (save) buffer logs and access logs
                   9265: 
                   9266: =item *
                   9267: 
                   9268: courselog($what) : save message for course in hash
                   9269: 
                   9270: =item *
                   9271: 
                   9272: courseacclog($what) : save message for course using &courselog().  Perform
                   9273: special processing for specific resource types (problems, exams, quizzes, etc).
                   9274: 
1.191     harris41 9275: =item *
                   9276: 
                   9277: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   9278: as a PerlChildExitHandler
1.243     albertel 9279: 
                   9280: =back
                   9281: 
                   9282: =head2 Other
                   9283: 
                   9284: =over 4
                   9285: 
                   9286: =item *
                   9287: 
                   9288: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 9289: 
                   9290: =back
                   9291: 
                   9292: =cut
1.877     foxr     9293: 

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