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

1.1       albertel    1: # The LearningOnline Network
                      2: # TCP networking package
1.12      www         3: #
1.900   ! albertel    4: # $Id: lonnet.pm,v 1.899 2007/07/26 02:09:24 raeburn Exp $
1.178     www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.169     harris41   28: ###
                     29: 
1.1       albertel   30: package Apache::lonnet;
                     31: 
                     32: use strict;
1.8       www        33: use LWP::UserAgent();
1.486     www        34: use HTTP::Date;
                     35: # use Date::Parse;
1.871     albertel   36: use vars qw(%perlvar %spareid %pr %prp $memcache %packagetab $tmpdir
                     37:             $_64bit %env);
                     38: 
                     39: my (%badServerCache, $memcache, %courselogs, %accesshash, %domainrolehash,
                     40:     %userrolehash, $processmarker, $dumpcount, %coursedombuf,
                     41:     %coursenumbuf, %coursehombuf, %coursedescrbuf, %courseinstcodebuf,
                     42:     %courseownerbuf, %coursetypebuf);
1.403     www        43: 
1.1       albertel   44: use IO::Socket;
1.31      www        45: use GDBM_File;
1.208     albertel   46: use HTML::LCParser;
1.88      www        47: use Fcntl qw(:flock);
1.870     albertel   48: use Storable qw(thaw nfreeze);
1.539     albertel   49: use Time::HiRes qw( gettimeofday tv_interval );
1.599     albertel   50: use Cache::Memcached;
1.676     albertel   51: use Digest::MD5;
1.790     albertel   52: use Math::Random;
1.807     albertel   53: use LONCAPA qw(:DEFAULT :match);
1.740     www        54: use LONCAPA::Configuration;
1.676     albertel   55: 
1.195     www        56: my $readit;
1.550     foxr       57: my $max_connection_retries = 10;     # Or some such value.
1.1       albertel   58: 
1.619     albertel   59: require Exporter;
                     60: 
                     61: our @ISA = qw (Exporter);
                     62: our @EXPORT = qw(%env);
                     63: 
1.449     matthew    64: =pod
                     65: 
                     66: =head1 Package Variables
                     67: 
                     68: These are largely undocumented, so if you decipher one please note it here.
                     69: 
                     70: =over 4
                     71: 
                     72: =item $processmarker
                     73: 
                     74: Contains the time this process was started and this servers host id.
                     75: 
                     76: =item $dumpcount
                     77: 
                     78: Counts the number of times a message log flush has been attempted (regardless
                     79: of success) by this process.  Used as part of the filename when messages are
                     80: delayed.
                     81: 
                     82: =back
                     83: 
                     84: =cut
                     85: 
                     86: 
1.1       albertel   87: # --------------------------------------------------------------------- Logging
1.729     www        88: {
                     89:     my $logid;
                     90:     sub instructor_log {
                     91: 	my ($hash_name,$storehash,$delflag,$uname,$udom)=@_;
                     92: 	$logid++;
                     93: 	my $id=time().'00000'.$$.'00000'.$logid;
                     94: 	return &Apache::lonnet::put('nohist_'.$hash_name,
1.730     www        95: 				    { $id => {
                     96: 					'exe_uname' => $env{'user.name'},
                     97: 					'exe_udom'  => $env{'user.domain'},
                     98: 					'exe_time'  => time(),
                     99: 					'exe_ip'    => $ENV{'REMOTE_ADDR'},
                    100: 					'delflag'   => $delflag,
                    101: 					'logentry'  => $storehash,
                    102: 					'uname'     => $uname,
                    103: 					'udom'      => $udom,
                    104: 				    }
                    105: 				  },
1.729     www       106: 				    $env{'course.'.$env{'request.course.id'}.'.domain'},
                    107: 				    $env{'course.'.$env{'request.course.id'}.'.num'}
                    108: 				    );
                    109:     }
                    110: }
1.1       albertel  111: 
1.163     harris41  112: sub logtouch {
                    113:     my $execdir=$perlvar{'lonDaemons'};
1.448     albertel  114:     unless (-e "$execdir/logs/lonnet.log") {	
                    115: 	open(my $fh,">>$execdir/logs/lonnet.log");
1.163     harris41  116: 	close $fh;
                    117:     }
                    118:     my ($wwwuid,$wwwgid)=(getpwnam('www'))[2,3];
                    119:     chown($wwwuid,$wwwgid,$execdir.'/logs/lonnet.log');
                    120: }
                    121: 
1.1       albertel  122: sub logthis {
                    123:     my $message=shift;
                    124:     my $execdir=$perlvar{'lonDaemons'};
                    125:     my $now=time;
                    126:     my $local=localtime($now);
1.448     albertel  127:     if (open(my $fh,">>$execdir/logs/lonnet.log")) {
                    128: 	print $fh "$local ($$): $message\n";
                    129: 	close($fh);
                    130:     }
1.1       albertel  131:     return 1;
                    132: }
                    133: 
                    134: sub logperm {
                    135:     my $message=shift;
                    136:     my $execdir=$perlvar{'lonDaemons'};
                    137:     my $now=time;
                    138:     my $local=localtime($now);
1.448     albertel  139:     if (open(my $fh,">>$execdir/logs/lonnet.perm.log")) {
                    140: 	print $fh "$now:$message:$local\n";
                    141: 	close($fh);
                    142:     }
1.1       albertel  143:     return 1;
                    144: }
                    145: 
1.850     albertel  146: sub create_connection {
1.853     albertel  147:     my ($hostname,$lonid) = @_;
1.851     albertel  148:     my $client=IO::Socket::UNIX->new(Peer    => $perlvar{'lonSockCreate'},
1.850     albertel  149: 				     Type    => SOCK_STREAM,
                    150: 				     Timeout => 10);
                    151:     return 0 if (!$client);
1.890     albertel  152:     print $client (join(':',$hostname,$lonid,&machine_ids($hostname))."\n");
1.850     albertel  153:     my $result = <$client>;
                    154:     chomp($result);
                    155:     return 1 if ($result eq 'done');
                    156:     return 0;
                    157: }
                    158: 
                    159: 
1.1       albertel  160: # -------------------------------------------------- Non-critical communication
                    161: sub subreply {
                    162:     my ($cmd,$server)=@_;
1.838     albertel  163:     my $peerfile="$perlvar{'lonSockDir'}/".&hostname($server);
1.549     foxr      164:     #
                    165:     #  With loncnew process trimming, there's a timing hole between lonc server
                    166:     #  process exit and the master server picking up the listen on the AF_UNIX
                    167:     #  socket.  In that time interval, a lock file will exist:
                    168: 
                    169:     my $lockfile=$peerfile.".lock";
                    170:     while (-e $lockfile) {	# Need to wait for the lockfile to disappear.
                    171: 	sleep(1);
                    172:     }
                    173:     # At this point, either a loncnew parent is listening or an old lonc
1.550     foxr      174:     # or loncnew child is listening so we can connect or everything's dead.
1.549     foxr      175:     #
1.550     foxr      176:     #   We'll give the connection a few tries before abandoning it.  If
                    177:     #   connection is not possible, we'll con_lost back to the client.
                    178:     #   
                    179:     my $client;
                    180:     for (my $retries = 0; $retries < $max_connection_retries; $retries++) {
                    181: 	$client=IO::Socket::UNIX->new(Peer    =>"$peerfile",
                    182: 				      Type    => SOCK_STREAM,
                    183: 				      Timeout => 10);
1.869     albertel  184: 	if ($client) {
1.550     foxr      185: 	    last;		# Connected!
1.850     albertel  186: 	} else {
1.853     albertel  187: 	    &create_connection(&hostname($server),$server);
1.550     foxr      188: 	}
1.850     albertel  189:         sleep(1);		# Try again later if failed connection.
1.550     foxr      190:     }
                    191:     my $answer;
                    192:     if ($client) {
1.704     albertel  193: 	print $client "sethost:$server:$cmd\n";
1.550     foxr      194: 	$answer=<$client>;
                    195: 	if (!$answer) { $answer="con_lost"; }
                    196: 	chomp($answer);
                    197:     } else {
                    198: 	$answer = 'con_lost';	# Failed connection.
                    199:     }
1.1       albertel  200:     return $answer;
                    201: }
                    202: 
                    203: sub reply {
                    204:     my ($cmd,$server)=@_;
1.838     albertel  205:     unless (defined(&hostname($server))) { return 'no_such_host'; }
1.1       albertel  206:     my $answer=subreply($cmd,$server);
1.65      www       207:     if (($answer=~/^refused/) || ($answer=~/^rejected/)) {
1.672     albertel  208:        &logthis("<font color=\"blue\">WARNING:".
1.12      www       209:                 " $cmd to $server returned $answer</font>");
                    210:     }
1.1       albertel  211:     return $answer;
                    212: }
                    213: 
                    214: # ----------------------------------------------------------- Send USR1 to lonc
                    215: 
                    216: sub reconlonc {
1.891     albertel  217:     my ($lonid) = @_;
                    218:     my $hostname = &hostname($lonid);
                    219:     if ($lonid) {
                    220: 	my $peerfile="$perlvar{'lonSockDir'}/$hostname";
                    221: 	if ($hostname && -e $peerfile) {
                    222: 	    &logthis("Trying to reconnect lonc for $lonid ($hostname)");
                    223: 	    my $client=IO::Socket::UNIX->new(Peer    => $peerfile,
                    224: 					     Type    => SOCK_STREAM,
                    225: 					     Timeout => 10);
                    226: 	    if ($client) {
                    227: 		print $client ("reset_retries\n");
                    228: 		my $answer=<$client>;
                    229: 		#reset just this one.
                    230: 	    }
                    231: 	}
                    232: 	return;
                    233:     }
                    234: 
1.836     www       235:     &logthis("Trying to reconnect lonc");
1.1       albertel  236:     my $loncfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
1.448     albertel  237:     if (open(my $fh,"<$loncfile")) {
1.1       albertel  238: 	my $loncpid=<$fh>;
                    239:         chomp($loncpid);
                    240:         if (kill 0 => $loncpid) {
                    241: 	    &logthis("lonc at pid $loncpid responding, sending USR1");
                    242:             kill USR1 => $loncpid;
                    243:             sleep 1;
1.836     www       244:          } else {
1.12      www       245: 	    &logthis(
1.672     albertel  246:                "<font color=\"blue\">WARNING:".
1.12      www       247:                " lonc at pid $loncpid not responding, giving up</font>");
1.1       albertel  248:         }
                    249:     } else {
1.836     www       250: 	&logthis('<font color="blue">WARNING: lonc not running, giving up</font>');
1.1       albertel  251:     }
                    252: }
                    253: 
                    254: # ------------------------------------------------------ Critical communication
1.12      www       255: 
1.1       albertel  256: sub critical {
                    257:     my ($cmd,$server)=@_;
1.838     albertel  258:     unless (&hostname($server)) {
1.672     albertel  259:         &logthis("<font color=\"blue\">WARNING:".
1.89      www       260:                " Critical message to unknown server ($server)</font>");
                    261:         return 'no_such_host';
                    262:     }
1.1       albertel  263:     my $answer=reply($cmd,$server);
                    264:     if ($answer eq 'con_lost') {
                    265: 	&reconlonc("$perlvar{'lonSockDir'}/$server");
1.589     albertel  266: 	my $answer=reply($cmd,$server);
1.1       albertel  267:         if ($answer eq 'con_lost') {
                    268:             my $now=time;
                    269:             my $middlename=$cmd;
1.5       www       270:             $middlename=substr($middlename,0,16);
1.1       albertel  271:             $middlename=~s/\W//g;
                    272:             my $dfilename=
1.305     www       273:       "$perlvar{'lonSockDir'}/delayed/$now.$dumpcount.$$.$middlename.$server";
                    274:             $dumpcount++;
1.1       albertel  275:             {
1.448     albertel  276: 		my $dfh;
                    277: 		if (open($dfh,">$dfilename")) {
                    278: 		    print $dfh "$cmd\n"; 
                    279: 		    close($dfh);
                    280: 		}
1.1       albertel  281:             }
                    282:             sleep 2;
                    283:             my $wcmd='';
                    284:             {
1.448     albertel  285: 		my $dfh;
                    286: 		if (open($dfh,"<$dfilename")) {
                    287: 		    $wcmd=<$dfh>; 
                    288: 		    close($dfh);
                    289: 		}
1.1       albertel  290:             }
                    291:             chomp($wcmd);
1.7       www       292:             if ($wcmd eq $cmd) {
1.672     albertel  293: 		&logthis("<font color=\"blue\">WARNING: ".
1.12      www       294:                          "Connection buffer $dfilename: $cmd</font>");
1.1       albertel  295:                 &logperm("D:$server:$cmd");
                    296: 	        return 'con_delayed';
                    297:             } else {
1.672     albertel  298:                 &logthis("<font color=\"red\">CRITICAL:"
1.12      www       299:                         ." Critical connection failed: $server $cmd</font>");
1.1       albertel  300:                 &logperm("F:$server:$cmd");
                    301:                 return 'con_failed';
                    302:             }
                    303:         }
                    304:     }
                    305:     return $answer;
1.405     albertel  306: }
                    307: 
1.755     albertel  308: # ------------------------------------------- check if return value is an error
                    309: 
                    310: sub error {
                    311:     my ($result) = @_;
1.756     albertel  312:     if ($result =~ /^(con_lost|no_such_host|error: (\d+) (.*))/) {
1.755     albertel  313: 	if ($2 == 2) { return undef; }
                    314: 	return $1;
                    315:     }
                    316:     return undef;
                    317: }
                    318: 
1.783     albertel  319: sub convert_and_load_session_env {
                    320:     my ($lonidsdir,$handle)=@_;
                    321:     my @profile;
                    322:     {
                    323: 	open(my $idf,"$lonidsdir/$handle.id");
                    324: 	flock($idf,LOCK_SH);
                    325: 	@profile=<$idf>;
                    326: 	close($idf);
                    327:     }
                    328:     my %temp_env;
                    329:     foreach my $line (@profile) {
1.786     albertel  330: 	if ($line !~ m/=/) {
                    331: 	    return 0;
                    332: 	}
1.783     albertel  333: 	chomp($line);
                    334: 	my ($envname,$envvalue)=split(/=/,$line,2);
                    335: 	$temp_env{&unescape($envname)} = &unescape($envvalue);
                    336:     }
                    337:     unlink("$lonidsdir/$handle.id");
                    338:     if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",&GDBM_WRCREAT(),
                    339: 	    0640)) {
                    340: 	%disk_env = %temp_env;
                    341: 	@env{keys(%temp_env)} = @disk_env{keys(%temp_env)};
                    342: 	untie(%disk_env);
                    343:     }
1.786     albertel  344:     return 1;
1.783     albertel  345: }
                    346: 
1.374     www       347: # ------------------------------------------- Transfer profile into environment
1.780     albertel  348: my $env_loaded;
                    349: sub transfer_profile_to_env {
1.788     albertel  350:     my ($lonidsdir,$handle,$force_transfer) = @_;
                    351:     if (!$force_transfer && $env_loaded) { return; } 
1.374     www       352: 
1.720     albertel  353:     if (!defined($lonidsdir)) {
                    354: 	$lonidsdir = $perlvar{'lonIDsDir'};
                    355:     }
                    356:     if (!defined($handle)) {
                    357:         ($handle) = ($env{'user.environment'} =~m|/([^/]+)\.id$| );
                    358:     }
                    359: 
1.786     albertel  360:     my $convert;
                    361:     {
                    362:     	open(my $idf,"$lonidsdir/$handle.id");
                    363: 	flock($idf,LOCK_SH);
                    364: 	if (tie(my %disk_env,'GDBM_File',"$lonidsdir/$handle.id",
                    365: 		&GDBM_READER(),0640)) {
                    366: 	    @env{keys(%disk_env)} = @disk_env{keys(%disk_env)};
                    367: 	    untie(%disk_env);
                    368: 	} else {
                    369: 	    $convert = 1;
                    370: 	}
                    371:     }
                    372:     if ($convert) {
                    373: 	if (!&convert_and_load_session_env($lonidsdir,$handle)) {
                    374: 	    &logthis("Failed to load session, or convert session.");
                    375: 	}
1.374     www       376:     }
1.783     albertel  377: 
1.786     albertel  378:     my %remove;
1.783     albertel  379:     while ( my $envname = each(%env) ) {
1.433     matthew   380:         if (my ($key,$time) = ($envname =~ /^(cgi\.(\d+)_\d+\.)/)) {
                    381:             if ($time < time-300) {
1.783     albertel  382:                 $remove{$key}++;
1.433     matthew   383:             }
                    384:         }
                    385:     }
1.783     albertel  386: 
1.619     albertel  387:     $env{'user.environment'} = "$lonidsdir/$handle.id";
1.780     albertel  388:     $env_loaded=1;
1.783     albertel  389:     foreach my $expired_key (keys(%remove)) {
1.433     matthew   390:         &delenv($expired_key);
1.374     www       391:     }
1.1       albertel  392: }
                    393: 
1.830     albertel  394: sub timed_flock {
                    395:     my ($file,$lock_type) = @_;
                    396:     my $failed=0;
                    397:     eval {
                    398: 	local $SIG{__DIE__}='DEFAULT';
                    399: 	local $SIG{ALRM}=sub {
                    400: 	    $failed=1;
                    401: 	    die("failed lock");
                    402: 	};
                    403: 	alarm(13);
                    404: 	flock($file,$lock_type);
                    405: 	alarm(0);
                    406:     };
                    407:     if ($failed) {
                    408: 	return undef;
                    409:     } else {
                    410: 	return 1;
                    411:     }
                    412: }
                    413: 
1.5       www       414: # ---------------------------------------------------------- Append Environment
                    415: 
                    416: sub appenv {
1.6       www       417:     my %newenv=@_;
1.692     albertel  418:     foreach my $key (keys(%newenv)) {
                    419: 	if (($newenv{$key}=~/^user\.role/) || ($newenv{$key}=~/^user\.priv/)) {
1.672     albertel  420:             &logthis("<font color=\"blue\">WARNING: ".
1.692     albertel  421:                 "Attempt to modify environment ".$key." to ".$newenv{$key}
1.151     www       422:                 .'</font>');
1.692     albertel  423: 	    delete($newenv{$key});
1.35      www       424:         } else {
1.692     albertel  425:             $env{$key}=$newenv{$key};
1.35      www       426:         }
1.191     harris41  427:     }
1.830     albertel  428:     open(my $env_file,$env{'user.environment'});
                    429:     if (&timed_flock($env_file,LOCK_EX)
                    430: 	&&
                    431: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    432: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  433: 	while (my ($key,$value) = each(%newenv)) {
                    434: 	    $disk_env{$key} = $value;
1.448     albertel  435: 	}
1.783     albertel  436: 	untie(%disk_env);
1.56      www       437:     }
                    438:     return 'ok';
                    439: }
                    440: # ----------------------------------------------------- Delete from Environment
                    441: 
                    442: sub delenv {
                    443:     my $delthis=shift;
                    444:     if (($delthis=~/user\.role/) || ($delthis=~/user\.priv/)) {
1.672     albertel  445:         &logthis("<font color=\"blue\">WARNING: ".
1.56      www       446:                 "Attempt to delete from environment ".$delthis);
                    447:         return 'error';
                    448:     }
1.830     albertel  449:     open(my $env_file,$env{'user.environment'});
                    450:     if (&timed_flock($env_file,LOCK_EX)
                    451: 	&&
                    452: 	tie(my %disk_env,'GDBM_File',$env{'user.environment'},
                    453: 	    (&GDBM_WRITER()|&GDBM_NOLOCK()),0640)) {
1.783     albertel  454: 	foreach my $key (keys(%disk_env)) {
                    455: 	    if ($key=~/^$delthis/) { 
1.619     albertel  456:                 delete($env{$key});
1.783     albertel  457:                 delete($disk_env{$key});
1.473     matthew   458:             }
1.448     albertel  459: 	}
1.783     albertel  460: 	untie(%disk_env);
1.5       www       461:     }
                    462:     return 'ok';
1.369     albertel  463: }
                    464: 
1.790     albertel  465: sub get_env_multiple {
                    466:     my ($name) = @_;
                    467:     my @values;
                    468:     if (defined($env{$name})) {
                    469:         # exists is it an array
                    470:         if (ref($env{$name})) {
                    471:             @values=@{ $env{$name} };
                    472:         } else {
                    473:             $values[0]=$env{$name};
                    474:         }
                    475:     }
                    476:     return(@values);
                    477: }
                    478: 
1.369     albertel  479: # ------------------------------------------ Find out current server userload
                    480: # there is a copy in lond
                    481: sub userload {
                    482:     my $numusers=0;
                    483:     {
                    484: 	opendir(LONIDS,$perlvar{'lonIDsDir'});
                    485: 	my $filename;
                    486: 	my $curtime=time;
                    487: 	while ($filename=readdir(LONIDS)) {
                    488: 	    if ($filename eq '.' || $filename eq '..') {next;}
1.404     albertel  489: 	    my ($mtime)=(stat($perlvar{'lonIDsDir'}.'/'.$filename))[9];
1.437     albertel  490: 	    if ($curtime-$mtime < 1800) { $numusers++; }
1.369     albertel  491: 	}
                    492: 	closedir(LONIDS);
                    493:     }
                    494:     my $userloadpercent=0;
                    495:     my $maxuserload=$perlvar{'lonUserLoadLim'};
                    496:     if ($maxuserload) {
1.371     albertel  497: 	$userloadpercent=100*$numusers/$maxuserload;
1.369     albertel  498:     }
1.372     albertel  499:     $userloadpercent=sprintf("%.2f",$userloadpercent);
1.369     albertel  500:     return $userloadpercent;
1.283     www       501: }
                    502: 
                    503: # ------------------------------------------ Fight off request when overloaded
                    504: 
                    505: sub overloaderror {
                    506:     my ($r,$checkserver)=@_;
                    507:     unless ($checkserver) { $checkserver=$perlvar{'lonHostID'}; }
                    508:     my $loadavg;
                    509:     if ($checkserver eq $perlvar{'lonHostID'}) {
1.448     albertel  510:        open(my $loadfile,'/proc/loadavg');
1.283     www       511:        $loadavg=<$loadfile>;
                    512:        $loadavg =~ s/\s.*//g;
1.285     matthew   513:        $loadavg = 100*$loadavg/$perlvar{'lonLoadLim'};
1.448     albertel  514:        close($loadfile);
1.283     www       515:     } else {
                    516:        $loadavg=&reply('load',$checkserver);
                    517:     }
1.285     matthew   518:     my $overload=$loadavg-100;
1.283     www       519:     if ($overload>0) {
1.285     matthew   520: 	$r->err_headers_out->{'Retry-After'}=$overload;
1.283     www       521:         $r->log_error('Overload of '.$overload.' on '.$checkserver);
1.554     www       522:         return 413;
1.283     www       523:     }    
                    524:     return '';
1.5       www       525: }
1.1       albertel  526: 
                    527: # ------------------------------ Find server with least workload from spare.tab
1.11      www       528: 
1.1       albertel  529: sub spareserver {
1.670     albertel  530:     my ($loadpercent,$userloadpercent,$want_server_name) = @_;
1.784     albertel  531:     my $spare_server;
1.370     albertel  532:     if ($userloadpercent !~ /\d/) { $userloadpercent=0; }
1.784     albertel  533:     my $lowest_load=($loadpercent > $userloadpercent) ? $loadpercent 
                    534:                                                      :  $userloadpercent;
                    535:     
                    536:     foreach my $try_server (@{ $spareid{'primary'} }) {
                    537: 	($spare_server, $lowest_load) =
                    538: 	    &compare_server_load($try_server, $spare_server, $lowest_load);
                    539:     }
                    540: 
                    541:     my $found_server = ($spare_server ne '' && $lowest_load < 100);
                    542: 
                    543:     if (!$found_server) {
                    544: 	foreach my $try_server (@{ $spareid{'default'} }) {
                    545: 	    ($spare_server, $lowest_load) =
                    546: 		&compare_server_load($try_server, $spare_server, $lowest_load);
                    547: 	}
                    548:     }
                    549: 
                    550:     if (!$want_server_name) {
1.838     albertel  551: 	$spare_server="http://".&hostname($spare_server);
1.784     albertel  552:     }
                    553:     return $spare_server;
                    554: }
                    555: 
                    556: sub compare_server_load {
                    557:     my ($try_server, $spare_server, $lowest_load) = @_;
                    558: 
                    559:     my $loadans     = &reply('load',    $try_server);
                    560:     my $userloadans = &reply('userload',$try_server);
                    561: 
                    562:     if ($loadans !~ /\d/ && $userloadans !~ /\d/) {
                    563: 	next; #didn't get a number from the server
                    564:     }
                    565: 
                    566:     my $load;
                    567:     if ($loadans =~ /\d/) {
                    568: 	if ($userloadans =~ /\d/) {
                    569: 	    #both are numbers, pick the bigger one
                    570: 	    $load = ($loadans > $userloadans) ? $loadans 
                    571: 		                              : $userloadans;
1.411     albertel  572: 	} else {
1.784     albertel  573: 	    $load = $loadans;
1.411     albertel  574: 	}
1.784     albertel  575:     } else {
                    576: 	$load = $userloadans;
                    577:     }
                    578: 
                    579:     if (($load =~ /\d/) && ($load < $lowest_load)) {
                    580: 	$spare_server = $try_server;
                    581: 	$lowest_load  = $load;
1.370     albertel  582:     }
1.784     albertel  583:     return ($spare_server,$lowest_load);
1.202     matthew   584: }
                    585: # --------------------------------------------- Try to change a user's password
                    586: 
                    587: sub changepass {
1.799     raeburn   588:     my ($uname,$udom,$currentpass,$newpass,$server,$context)=@_;
1.202     matthew   589:     $currentpass = &escape($currentpass);
                    590:     $newpass     = &escape($newpass);
1.799     raeburn   591:     my $answer = reply("encrypt:passwd:$udom:$uname:$currentpass:$newpass:$context",
1.202     matthew   592: 		       $server);
                    593:     if (! $answer) {
                    594: 	&logthis("No reply on password change request to $server ".
                    595: 		 "by $uname in domain $udom.");
                    596:     } elsif ($answer =~ "^ok") {
                    597:         &logthis("$uname in $udom successfully changed their password ".
                    598: 		 "on $server.");
                    599:     } elsif ($answer =~ "^pwchange_failure") {
                    600: 	&logthis("$uname in $udom was unable to change their password ".
                    601: 		 "on $server.  The action was blocked by either lcpasswd ".
                    602: 		 "or pwchange");
                    603:     } elsif ($answer =~ "^non_authorized") {
                    604:         &logthis("$uname in $udom did not get their password correct when ".
                    605: 		 "attempting to change it on $server.");
                    606:     } elsif ($answer =~ "^auth_mode_error") {
                    607:         &logthis("$uname in $udom attempted to change their password despite ".
                    608: 		 "not being locally or internally authenticated on $server.");
                    609:     } elsif ($answer =~ "^unknown_user") {
                    610:         &logthis("$uname in $udom attempted to change their password ".
                    611: 		 "on $server but were unable to because $server is not ".
                    612: 		 "their home server.");
                    613:     } elsif ($answer =~ "^refused") {
                    614: 	&logthis("$server refused to change $uname in $udom password because ".
                    615: 		 "it was sent an unencrypted request to change the password.");
                    616:     }
                    617:     return $answer;
1.1       albertel  618: }
                    619: 
1.169     harris41  620: # ----------------------- Try to determine user's current authentication scheme
                    621: 
                    622: sub queryauthenticate {
                    623:     my ($uname,$udom)=@_;
1.456     albertel  624:     my $uhome=&homeserver($uname,$udom);
                    625:     if (!$uhome) {
                    626: 	&logthis("User $uname at $udom is unknown when looking for authentication mechanism");
                    627: 	return 'no_host';
                    628:     }
                    629:     my $answer=reply("encrypt:currentauth:$udom:$uname",$uhome);
                    630:     if ($answer =~ /^(unknown_user|refused|con_lost)/) {
                    631: 	&logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.169     harris41  632:     }
1.456     albertel  633:     return $answer;
1.169     harris41  634: }
                    635: 
1.1       albertel  636: # --------- Try to authenticate user from domain's lib servers (first this one)
1.11      www       637: 
1.1       albertel  638: sub authenticate {
                    639:     my ($uname,$upass,$udom)=@_;
1.807     albertel  640:     $upass=&escape($upass);
                    641:     $uname= &LONCAPA::clean_username($uname);
1.836     www       642:     my $uhome=&homeserver($uname,$udom,1);
                    643:     if ((!$uhome) || ($uhome eq 'no_host')) {
                    644: # Maybe the machine was offline and only re-appeared again recently?
                    645:         &reconlonc();
                    646: # One more
                    647: 	my $uhome=&homeserver($uname,$udom,1);
                    648: 	if ((!$uhome) || ($uhome eq 'no_host')) {
                    649: 	    &logthis("User $uname at $udom is unknown in authenticate");
                    650: 	}
1.471     albertel  651: 	return 'no_host';
1.1       albertel  652:     }
1.471     albertel  653:     my $answer=reply("encrypt:auth:$udom:$uname:$upass",$uhome);
                    654:     if ($answer eq 'authorized') {
                    655: 	&logthis("User $uname at $udom authorized by $uhome"); 
                    656: 	return $uhome; 
                    657:     }
                    658:     if ($answer eq 'non_authorized') {
                    659: 	&logthis("User $uname at $udom rejected by $uhome");
                    660: 	return 'no_host'; 
1.9       www       661:     }
1.471     albertel  662:     &logthis("User $uname at $udom threw error $answer when checking authentication mechanism");
1.1       albertel  663:     return 'no_host';
                    664: }
                    665: 
                    666: # ---------------------- Find the homebase for a user from domain's lib servers
1.11      www       667: 
1.599     albertel  668: my %homecache;
1.1       albertel  669: sub homeserver {
1.230     stredwic  670:     my ($uname,$udom,$ignoreBadCache)=@_;
1.1       albertel  671:     my $index="$uname:$udom";
1.426     albertel  672: 
1.599     albertel  673:     if (exists($homecache{$index})) { return $homecache{$index}; }
1.841     albertel  674: 
                    675:     my %servers = &get_servers($udom,'library');
                    676:     foreach my $tryserver (keys(%servers)) {
1.230     stredwic  677:         next if ($ignoreBadCache ne 'true' && 
1.231     stredwic  678: 		 exists($badServerCache{$tryserver}));
1.841     albertel  679: 
                    680: 	my $answer=reply("home:$udom:$uname",$tryserver);
                    681: 	if ($answer eq 'found') {
                    682: 	    delete($badServerCache{$tryserver}); 
                    683: 	    return $homecache{$index}=$tryserver;
                    684: 	} elsif ($answer eq 'no_host') {
                    685: 	    $badServerCache{$tryserver}=1;
                    686: 	}
1.1       albertel  687:     }    
                    688:     return 'no_host';
1.70      www       689: }
                    690: 
                    691: # ------------------------------------- Find the usernames behind a list of IDs
                    692: 
                    693: sub idget {
                    694:     my ($udom,@ids)=@_;
                    695:     my %returnhash=();
                    696:     
1.841     albertel  697:     my %servers = &get_servers($udom,'library');
                    698:     foreach my $tryserver (keys(%servers)) {
                    699: 	my $idlist=join('&',@ids);
                    700: 	$idlist=~tr/A-Z/a-z/; 
                    701: 	my $reply=&reply("idget:$udom:".$idlist,$tryserver);
                    702: 	my @answer=();
                    703: 	if (($reply ne 'con_lost') && ($reply!~/^error\:/)) {
                    704: 	    @answer=split(/\&/,$reply);
                    705: 	}                    ;
                    706: 	my $i;
                    707: 	for ($i=0;$i<=$#ids;$i++) {
                    708: 	    if ($answer[$i]) {
                    709: 		$returnhash{$ids[$i]}=$answer[$i];
                    710: 	    } 
                    711: 	}
                    712:     } 
1.70      www       713:     return %returnhash;
                    714: }
                    715: 
                    716: # ------------------------------------- Find the IDs behind a list of usernames
                    717: 
                    718: sub idrget {
                    719:     my ($udom,@unames)=@_;
                    720:     my %returnhash=();
1.800     albertel  721:     foreach my $uname (@unames) {
                    722:         $returnhash{$uname}=(&userenvironment($udom,$uname,'id'))[1];
1.191     harris41  723:     }
1.70      www       724:     return %returnhash;
                    725: }
                    726: 
                    727: # ------------------------------- Store away a list of names and associated IDs
                    728: 
                    729: sub idput {
                    730:     my ($udom,%ids)=@_;
                    731:     my %servers=();
1.800     albertel  732:     foreach my $uname (keys(%ids)) {
                    733: 	&cput('environment',{'id'=>$ids{$uname}},$udom,$uname);
                    734:         my $uhom=&homeserver($uname,$udom);
1.70      www       735:         if ($uhom ne 'no_host') {
1.800     albertel  736:             my $id=&escape($ids{$uname});
1.70      www       737:             $id=~tr/A-Z/a-z/;
1.800     albertel  738:             my $esc_unam=&escape($uname);
1.70      www       739: 	    if ($servers{$uhom}) {
1.800     albertel  740: 		$servers{$uhom}.='&'.$id.'='.$esc_unam;
1.70      www       741:             } else {
1.800     albertel  742:                 $servers{$uhom}=$id.'='.$esc_unam;
1.70      www       743:             }
                    744:         }
1.191     harris41  745:     }
1.800     albertel  746:     foreach my $server (keys(%servers)) {
                    747:         &critical('idput:'.$udom.':'.$servers{$server},$server);
1.191     harris41  748:     }
1.344     www       749: }
                    750: 
1.806     raeburn   751: # ------------------------------------------- get items from domain db files   
                    752: 
                    753: sub get_dom {
1.860     raeburn   754:     my ($namespace,$storearr,$udom,$uhome)=@_;
1.806     raeburn   755:     my $items='';
                    756:     foreach my $item (@$storearr) {
                    757:         $items.=&escape($item).'&';
                    758:     }
                    759:     $items=~s/\&$//;
1.860     raeburn   760:     if (!$udom) {
                    761:         $udom=$env{'user.domain'};
                    762:         if (defined(&domain($udom,'primary'))) {
                    763:             $uhome=&domain($udom,'primary');
                    764:         } else {
1.874     albertel  765:             undef($uhome);
1.860     raeburn   766:         }
                    767:     } else {
                    768:         if (!$uhome) {
                    769:             if (defined(&domain($udom,'primary'))) {
                    770:                 $uhome=&domain($udom,'primary');
                    771:             }
                    772:         }
                    773:     }
                    774:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   775:         my $rep=&reply("getdom:$udom:$namespace:$items",$uhome);
1.866     raeburn   776:         my %returnhash;
1.875     albertel  777:         if ($rep eq '' || $rep =~ /^error: 2 /) {
1.866     raeburn   778:             return %returnhash;
                    779:         }
1.806     raeburn   780:         my @pairs=split(/\&/,$rep);
                    781:         if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                    782:             return @pairs;
                    783:         }
                    784:         my $i=0;
                    785:         foreach my $item (@$storearr) {
                    786:             $returnhash{$item}=&thaw_unescape($pairs[$i]);
                    787:             $i++;
                    788:         }
                    789:         return %returnhash;
                    790:     } else {
1.880     banghart  791:         &logthis("get_dom failed - no homeserver and/or domain ($udom) ($uhome)");
1.806     raeburn   792:     }
                    793: }
                    794: 
                    795: # -------------------------------------------- put items in domain db files 
                    796: 
                    797: sub put_dom {
1.860     raeburn   798:     my ($namespace,$storehash,$udom,$uhome)=@_;
                    799:     if (!$udom) {
                    800:         $udom=$env{'user.domain'};
                    801:         if (defined(&domain($udom,'primary'))) {
                    802:             $uhome=&domain($udom,'primary');
                    803:         } else {
1.874     albertel  804:             undef($uhome);
1.860     raeburn   805:         }
                    806:     } else {
                    807:         if (!$uhome) {
                    808:             if (defined(&domain($udom,'primary'))) {
                    809:                 $uhome=&domain($udom,'primary');
                    810:             }
                    811:         }
                    812:     } 
                    813:     if ($udom && $uhome && ($uhome ne 'no_host')) {
1.806     raeburn   814:         my $items='';
                    815:         foreach my $item (keys(%$storehash)) {
                    816:             $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
                    817:         }
                    818:         $items=~s/\&$//;
                    819:         return &reply("putdom:$udom:$namespace:$items",$uhome);
                    820:     } else {
1.860     raeburn   821:         &logthis("put_dom failed - no homeserver and/or domain");
1.806     raeburn   822:     }
                    823: }
                    824: 
1.837     raeburn   825: sub retrieve_inst_usertypes {
                    826:     my ($udom) = @_;
                    827:     my (%returnhash,@order);
1.846     albertel  828:     if (defined(&domain($udom,'primary'))) {
                    829:         my $uhome=&domain($udom,'primary');
1.837     raeburn   830:         my $rep=&reply("inst_usertypes:$udom",$uhome);
                    831:         my ($hashitems,$orderitems) = split(/:/,$rep); 
                    832:         my @pairs=split(/\&/,$hashitems);
                    833:         foreach my $item (@pairs) {
                    834:             my ($key,$value)=split(/=/,$item,2);
                    835:             $key = &unescape($key);
                    836:             next if ($key =~ /^error: 2 /);
                    837:             $returnhash{$key}=&thaw_unescape($value);
                    838:         }
                    839:         my @esc_order = split(/\&/,$orderitems);
                    840:         foreach my $item (@esc_order) {
                    841:             push(@order,&unescape($item));
                    842:         }
                    843:     } else {
                    844:         &logthis("get_dom failed - no primary domain server for $udom");
                    845:     }
                    846:     return (\%returnhash,\@order);
                    847: }
                    848: 
1.868     raeburn   849: sub is_domainimage {
                    850:     my ($url) = @_;
                    851:     if ($url=~m-^/+res/+($match_domain)/+\1\-domainconfig/+(img|logo|domlogo)/+-) {
                    852:         if (&domain($1) ne '') {
                    853:             return '1';
                    854:         }
                    855:     }
                    856:     return;
                    857: }
                    858: 
1.899     raeburn   859: sub inst_directory_query {
                    860:     my ($srch) = @_;
                    861:     my $udom = $srch->{'srchdomain'};
                    862:     my %results;
                    863:     my $homeserver = &domain($udom,'primary');
                    864:     if ($homeserver ne '') {
                    865:         my $response=&reply("instdirsrch:$udom".':'.
                    866:                             &escape($srch->{'srchby'}).':'.
                    867:                             &escape($srch->{'srchterm'}).':'.
                    868:                             $srch->{'srchtype'},$homeserver);
1.900   ! albertel  869:         if ($response ne 'refused') {
1.899     raeburn   870:             my @matches = split/&/,$response;
                    871:             foreach my $match (@matches) {
                    872:                 my ($key,$value) = split(/=/,$match);
                    873:                 my %userhash = &str2hash(&unescape($value));
                    874:                 $results{&unescape($key).':'.$udom} = \%userhash;
                    875:             }
                    876:         }
                    877:     }
                    878:     return %results;
                    879: }
                    880: 
                    881: sub usersearch {
                    882:     my ($srch) = @_;
                    883:     my $dom = $srch->{'srchdomain'};
                    884:     my %results;
                    885:     my %libserv = &all_library();
                    886:     my $query = 'usersearch';
                    887:     foreach my $tryserver (keys(%libserv)) {
                    888:         if (&host_domain($tryserver) eq $dom) {
                    889:             my $host=&hostname($tryserver);
                    890:             my $queryid=
                    891:                 &reply("querysend:".&escape($query).':'.&escape($dom).':'.
                    892:                        &escape($srch->{'srchby'}).'%%'.
                    893:                        &escape($srch->{'srchtype'}).':'.
                    894:                        &escape($srch->{'srchterm'}),$tryserver);
                    895:             if ($queryid !~/^\Q$host\E\_/) {
                    896:                 &logthis('usersearch: invalid queryid: '.$queryid.' for host: '.$host.'in domain '.$dom.' and server: '.$tryserver);
                    897:                 return 'error: '.$queryid;
                    898:             }
                    899:             my $reply = &get_query_reply($queryid);
                    900:             my $maxtries = 1;
                    901:             my $tries = 1;
                    902:             while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                    903:                 $reply = &get_query_reply($queryid);
                    904:                 $tries ++;
                    905:             }
                    906:             if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                    907:                 &logthis('usersrch error: '.$reply.' for '.$dom.' - searching for : '.$srch->{'srchterm'}.' by '.$srch->{'srchby'}.' ('.$srch->{'srchtype'}.') -  maxtries: '.$maxtries.' tries: '.$tries);
                    908:             } else {
1.900   ! albertel  909:                 my @matches = split(/&/,$reply);
1.899     raeburn   910:                 foreach my $match (@matches) {
                    911:                     my @items = split(/:/,$match);
                    912:                     my ($uname,$udom,%userhash);
                    913:                     foreach my $entry (@items) {
                    914:                         my ($key,$value) = split(/=/,$entry);
                    915:                         $key = &unescape($key);
                    916:                         $value = &unescape($value);
                    917:                         $userhash{$key} = $value;
                    918:                         if ($key eq 'username') {
                    919:                             $uname = $value;
                    920:                         } elsif ($key eq 'domain') {
                    921:                             $udom = $value;
                    922:                         } 
                    923:                     }
                    924:                     $results{$uname.':'.$udom} = \%userhash;
                    925:                 }
                    926:             }
                    927:         }
                    928:     }
                    929:     return %results;
                    930: }
                    931: 
1.344     www       932: # --------------------------------------------------- Assign a key to a student
                    933: 
                    934: sub assign_access_key {
1.364     www       935: #
                    936: # a valid key looks like uname:udom#comments
                    937: # comments are being appended
                    938: #
1.498     www       939:     my ($ckey,$kdom,$knum,$cdom,$cnum,$udom,$uname,$logentry)=@_;
                    940:     $kdom=
1.620     albertel  941:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($kdom));
1.498     www       942:     $knum=
1.620     albertel  943:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($knum));
1.344     www       944:     $cdom=
1.620     albertel  945:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www       946:     $cnum=
1.620     albertel  947:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                    948:     $udom=$env{'user.name'} unless (defined($udom));
                    949:     $uname=$env{'user.domain'} unless (defined($uname));
1.498     www       950:     my %existing=&get('accesskeys',[$ckey],$kdom,$knum);
1.364     www       951:     if (($existing{$ckey}=~/^\#(.*)$/) || # - new key
1.479     albertel  952:         ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#(.*)$/)) { 
1.364     www       953:                                                   # assigned to this person
                    954:                                                   # - this should not happen,
1.345     www       955:                                                   # unless something went wrong
                    956:                                                   # the first time around
                    957: # ready to assign
1.364     www       958:         $logentry=$1.'; '.$logentry;
1.496     www       959:         if (&put('accesskeys',{$ckey=>$uname.':'.$udom.'#'.$logentry},
1.498     www       960:                                                  $kdom,$knum) eq 'ok') {
1.345     www       961: # key now belongs to user
1.346     www       962: 	    my $envkey='key.'.$cdom.'_'.$cnum;
1.345     www       963:             if (&put('environment',{$envkey => $ckey}) eq 'ok') {
                    964:                 &appenv('environment.'.$envkey => $ckey);
                    965:                 return 'ok';
                    966:             } else {
                    967:                 return 
                    968:   'error: Count not permanently assign key, will need to be re-entered later.';
                    969: 	    }
                    970:         } else {
                    971:             return 'error: Could not assign key, try again later.';
                    972:         }
1.364     www       973:     } elsif (!$existing{$ckey}) {
1.345     www       974: # the key does not exist
                    975: 	return 'error: The key does not exist';
                    976:     } else {
                    977: # the key is somebody else's
                    978: 	return 'error: The key is already in use';
                    979:     }
1.344     www       980: }
                    981: 
1.364     www       982: # ------------------------------------------ put an additional comment on a key
                    983: 
                    984: sub comment_access_key {
                    985: #
                    986: # a valid key looks like uname:udom#comments
                    987: # comments are being appended
                    988: #
                    989:     my ($ckey,$cdom,$cnum,$logentry)=@_;
                    990:     $cdom=
1.620     albertel  991:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.364     www       992:     $cnum=
1.620     albertel  993:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.364     www       994:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
                    995:     if ($existing{$ckey}) {
                    996:         $existing{$ckey}.='; '.$logentry;
                    997: # ready to assign
1.367     www       998:         if (&put('accesskeys',{$ckey=>$existing{$ckey}},
1.364     www       999:                                                  $cdom,$cnum) eq 'ok') {
                   1000: 	    return 'ok';
                   1001:         } else {
                   1002: 	    return 'error: Count not store comment.';
                   1003:         }
                   1004:     } else {
                   1005: # the key does not exist
                   1006: 	return 'error: The key does not exist';
                   1007:     }
                   1008: }
                   1009: 
1.344     www      1010: # ------------------------------------------------------ Generate a set of keys
                   1011: 
                   1012: sub generate_access_keys {
1.364     www      1013:     my ($number,$cdom,$cnum,$logentry)=@_;
1.344     www      1014:     $cdom=
1.620     albertel 1015:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1016:     $cnum=
1.620     albertel 1017:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
1.361     www      1018:     unless (&allowed('mky',$cdom)) { return 0; }
1.344     www      1019:     unless (($cdom) && ($cnum)) { return 0; }
                   1020:     if ($number>10000) { return 0; }
                   1021:     sleep(2); # make sure don't get same seed twice
                   1022:     srand(time()^($$+($$<<15))); # from "Programming Perl"
                   1023:     my $total=0;
                   1024:     for (my $i=1;$i<=$number;$i++) {
                   1025:        my $newkey=sprintf("%lx",int(100000*rand)).'-'.
                   1026:                   sprintf("%lx",int(100000*rand)).'-'.
                   1027:                   sprintf("%lx",int(100000*rand));
                   1028:        $newkey=~s/1/g/g; # folks mix up 1 and l
                   1029:        $newkey=~s/0/h/g; # and also 0 and O
                   1030:        my %existing=&get('accesskeys',[$newkey],$cdom,$cnum);
                   1031:        if ($existing{$newkey}) {
                   1032:            $i--;
                   1033:        } else {
1.364     www      1034: 	  if (&put('accesskeys',
                   1035:               { $newkey => '# generated '.localtime().
1.620     albertel 1036:                            ' by '.$env{'user.name'}.'@'.$env{'user.domain'}.
1.364     www      1037:                            '; '.$logentry },
                   1038: 		   $cdom,$cnum) eq 'ok') {
1.344     www      1039:               $total++;
                   1040: 	  }
                   1041:        }
                   1042:     }
1.620     albertel 1043:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.344     www      1044:          'Generated '.$total.' keys for '.$cnum.' at '.$cdom);
                   1045:     return $total;
                   1046: }
                   1047: 
                   1048: # ------------------------------------------------------- Validate an accesskey
                   1049: 
                   1050: sub validate_access_key {
                   1051:     my ($ckey,$cdom,$cnum,$udom,$uname)=@_;
                   1052:     $cdom=
1.620     albertel 1053:    $env{'course.'.$env{'request.course.id'}.'.domain'} unless (defined($cdom));
1.344     www      1054:     $cnum=
1.620     albertel 1055:    $env{'course.'.$env{'request.course.id'}.'.num'} unless (defined($cnum));
                   1056:     $udom=$env{'user.domain'} unless (defined($udom));
                   1057:     $uname=$env{'user.name'} unless (defined($uname));
1.345     www      1058:     my %existing=&get('accesskeys',[$ckey],$cdom,$cnum);
1.479     albertel 1059:     return ($existing{$ckey}=~/^\Q$uname\E\:\Q$udom\E\#/);
1.70      www      1060: }
                   1061: 
                   1062: # ------------------------------------- Find the section of student in a course
1.652     albertel 1063: sub devalidate_getsection_cache {
                   1064:     my ($udom,$unam,$courseid)=@_;
                   1065:     my $hashid="$udom:$unam:$courseid";
                   1066:     &devalidate_cache_new('getsection',$hashid);
                   1067: }
1.298     matthew  1068: 
1.815     albertel 1069: sub courseid_to_courseurl {
                   1070:     my ($courseid) = @_;
                   1071:     #already url style courseid
                   1072:     return $courseid if ($courseid =~ m{^/});
                   1073: 
                   1074:     if (exists($env{'course.'.$courseid.'.num'})) {
                   1075: 	my $cnum = $env{'course.'.$courseid.'.num'};
                   1076: 	my $cdom = $env{'course.'.$courseid.'.domain'};
                   1077: 	return "/$cdom/$cnum";
                   1078:     }
                   1079: 
                   1080:     my %courseinfo=&Apache::lonnet::coursedescription($courseid);
                   1081:     if (exists($courseinfo{'num'})) {
                   1082: 	return "/$courseinfo{'domain'}/$courseinfo{'num'}";
                   1083:     }
                   1084: 
                   1085:     return undef;
                   1086: }
                   1087: 
1.298     matthew  1088: sub getsection {
                   1089:     my ($udom,$unam,$courseid)=@_;
1.599     albertel 1090:     my $cachetime=1800;
1.551     albertel 1091: 
                   1092:     my $hashid="$udom:$unam:$courseid";
1.599     albertel 1093:     my ($result,$cached)=&is_cached_new('getsection',$hashid);
1.551     albertel 1094:     if (defined($cached)) { return $result; }
                   1095: 
1.298     matthew  1096:     my %Pending; 
                   1097:     my %Expired;
                   1098:     #
                   1099:     # Each role can either have not started yet (pending), be active, 
                   1100:     #    or have expired.
                   1101:     #
                   1102:     # If there is an active role, we are done.
                   1103:     #
                   1104:     # If there is more than one role which has not started yet, 
                   1105:     #     choose the one which will start sooner
                   1106:     # If there is one role which has not started yet, return it.
                   1107:     #
                   1108:     # If there is more than one expired role, choose the one which ended last.
                   1109:     # If there is a role which has expired, return it.
                   1110:     #
1.815     albertel 1111:     $courseid = &courseid_to_courseurl($courseid);
1.817     raeburn  1112:     my %roleshash = &dump('roles',$udom,$unam,$courseid);
                   1113:     foreach my $key (keys(%roleshash)) {
1.479     albertel 1114:         next if ($key !~/^\Q$courseid\E(?:\/)*(\w+)*\_st$/);
1.298     matthew  1115:         my $section=$1;
                   1116:         if ($key eq $courseid.'_st') { $section=''; }
1.817     raeburn  1117:         my ($dummy,$end,$start)=split(/\_/,&unescape($roleshash{$key}));
1.298     matthew  1118:         my $now=time;
1.548     albertel 1119:         if (defined($end) && $end && ($now > $end)) {
1.298     matthew  1120:             $Expired{$end}=$section;
                   1121:             next;
                   1122:         }
1.548     albertel 1123:         if (defined($start) && $start && ($now < $start)) {
1.298     matthew  1124:             $Pending{$start}=$section;
                   1125:             next;
                   1126:         }
1.599     albertel 1127:         return &do_cache_new('getsection',$hashid,$section,$cachetime);
1.298     matthew  1128:     }
                   1129:     #
                   1130:     # Presumedly there will be few matching roles from the above
                   1131:     # loop and the sorting time will be negligible.
                   1132:     if (scalar(keys(%Pending))) {
                   1133:         my ($time) = sort {$a <=> $b} keys(%Pending);
1.599     albertel 1134:         return &do_cache_new('getsection',$hashid,$Pending{$time},$cachetime);
1.298     matthew  1135:     } 
                   1136:     if (scalar(keys(%Expired))) {
                   1137:         my @sorted = sort {$a <=> $b} keys(%Expired);
                   1138:         my $time = pop(@sorted);
1.599     albertel 1139:         return &do_cache_new('getsection',$hashid,$Expired{$time},$cachetime);
1.298     matthew  1140:     }
1.599     albertel 1141:     return &do_cache_new('getsection',$hashid,'-1',$cachetime);
1.298     matthew  1142: }
1.70      www      1143: 
1.599     albertel 1144: sub save_cache {
                   1145:     &purge_remembered();
1.722     albertel 1146:     #&Apache::loncommon::validate_page();
1.620     albertel 1147:     undef(%env);
1.780     albertel 1148:     undef($env_loaded);
1.599     albertel 1149: }
1.452     albertel 1150: 
1.599     albertel 1151: my $to_remember=-1;
                   1152: my %remembered;
                   1153: my %accessed;
                   1154: my $kicks=0;
                   1155: my $hits=0;
1.849     albertel 1156: sub make_key {
                   1157:     my ($name,$id) = @_;
1.872     albertel 1158:     if (length($id) > 65 
                   1159: 	&& length(&escape($id)) > 200) {
                   1160: 	$id=length($id).':'.&Digest::MD5::md5_hex($id);
                   1161:     }
1.849     albertel 1162:     return &escape($name.':'.$id);
                   1163: }
                   1164: 
1.599     albertel 1165: sub devalidate_cache_new {
                   1166:     my ($name,$id,$debug) = @_;
                   1167:     if ($debug) { &Apache::lonnet::logthis("deleting $name:$id"); }
1.849     albertel 1168:     $id=&make_key($name,$id);
1.599     albertel 1169:     $memcache->delete($id);
                   1170:     delete($remembered{$id});
                   1171:     delete($accessed{$id});
                   1172: }
                   1173: 
                   1174: sub is_cached_new {
                   1175:     my ($name,$id,$debug) = @_;
1.849     albertel 1176:     $id=&make_key($name,$id);
1.599     albertel 1177:     if (exists($remembered{$id})) {
                   1178: 	if ($debug) { &Apache::lonnet::logthis("Earyl return $id of $remembered{$id} "); }
                   1179: 	$accessed{$id}=[&gettimeofday()];
                   1180: 	$hits++;
                   1181: 	return ($remembered{$id},1);
                   1182:     }
                   1183:     my $value = $memcache->get($id);
                   1184:     if (!(defined($value))) {
                   1185: 	if ($debug) { &Apache::lonnet::logthis("getting $id is not defined"); }
1.417     albertel 1186: 	return (undef,undef);
1.416     albertel 1187:     }
1.599     albertel 1188:     if ($value eq '__undef__') {
                   1189: 	if ($debug) { &Apache::lonnet::logthis("getting $id is __undef__"); }
                   1190: 	$value=undef;
                   1191:     }
                   1192:     &make_room($id,$value,$debug);
                   1193:     if ($debug) { &Apache::lonnet::logthis("getting $id is $value"); }
                   1194:     return ($value,1);
                   1195: }
                   1196: 
                   1197: sub do_cache_new {
                   1198:     my ($name,$id,$value,$time,$debug) = @_;
1.849     albertel 1199:     $id=&make_key($name,$id);
1.599     albertel 1200:     my $setvalue=$value;
                   1201:     if (!defined($setvalue)) {
                   1202: 	$setvalue='__undef__';
                   1203:     }
1.623     albertel 1204:     if (!defined($time) ) {
                   1205: 	$time=600;
                   1206:     }
1.599     albertel 1207:     if ($debug) { &Apache::lonnet::logthis("Setting $id to $value"); }
1.872     albertel 1208:     if (!($memcache->set($id,$setvalue,$time))) {
                   1209: 	&logthis("caching of id -> $id  failed");
                   1210:     }
1.600     albertel 1211:     # need to make a copy of $value
                   1212:     #&make_room($id,$value,$debug);
1.599     albertel 1213:     return $value;
                   1214: }
                   1215: 
                   1216: sub make_room {
                   1217:     my ($id,$value,$debug)=@_;
                   1218:     $remembered{$id}=$value;
                   1219:     if ($to_remember<0) { return; }
                   1220:     $accessed{$id}=[&gettimeofday()];
                   1221:     if (scalar(keys(%remembered)) <= $to_remember) { return; }
                   1222:     my $to_kick;
                   1223:     my $max_time=0;
                   1224:     foreach my $other (keys(%accessed)) {
                   1225: 	if (&tv_interval($accessed{$other}) > $max_time) {
                   1226: 	    $to_kick=$other;
                   1227: 	    $max_time=&tv_interval($accessed{$other});
                   1228: 	}
                   1229:     }
                   1230:     delete($remembered{$to_kick});
                   1231:     delete($accessed{$to_kick});
                   1232:     $kicks++;
                   1233:     if ($debug) { &logthis("kicking $to_kick $max_time $kicks\n"); }
1.541     albertel 1234:     return;
                   1235: }
                   1236: 
1.599     albertel 1237: sub purge_remembered {
1.604     albertel 1238:     #&logthis("Tossing ".scalar(keys(%remembered)));
                   1239:     #&logthis(sprintf("%-20s is %s",'%remembered',length(&freeze(\%remembered))));
1.599     albertel 1240:     undef(%remembered);
                   1241:     undef(%accessed);
1.428     albertel 1242: }
1.70      www      1243: # ------------------------------------- Read an entry from a user's environment
                   1244: 
                   1245: sub userenvironment {
                   1246:     my ($udom,$unam,@what)=@_;
                   1247:     my %returnhash=();
                   1248:     my @answer=split(/\&/,
                   1249:                 &reply('get:'.$udom.':'.$unam.':environment:'.join('&',@what),
                   1250:                       &homeserver($unam,$udom)));
                   1251:     my $i;
                   1252:     for ($i=0;$i<=$#what;$i++) {
                   1253: 	$returnhash{$what[$i]}=&unescape($answer[$i]);
                   1254:     }
                   1255:     return %returnhash;
1.1       albertel 1256: }
                   1257: 
1.617     albertel 1258: # ---------------------------------------------------------- Get a studentphoto
                   1259: sub studentphoto {
                   1260:     my ($udom,$unam,$ext) = @_;
                   1261:     my $home=&Apache::lonnet::homeserver($unam,$udom);
1.706     raeburn  1262:     if (defined($env{'request.course.id'})) {
1.708     raeburn  1263:         if ($env{'course.'.$env{'request.course.id'}.'.internal.showphoto'}) {
1.706     raeburn  1264:             if ($udom eq $env{'course.'.$env{'request.course.id'}.'.domain'}) {
                   1265:                 return(&retrievestudentphoto($udom,$unam,$ext)); 
                   1266:             } else {
                   1267:                 my ($result,$perm_reqd)=
1.707     albertel 1268: 		    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1269:                 if ($result eq 'ok') {
                   1270:                     if (!($perm_reqd eq 'yes')) {
                   1271:                         return(&retrievestudentphoto($udom,$unam,$ext));
                   1272:                     }
                   1273:                 }
                   1274:             }
                   1275:         }
                   1276:     } else {
                   1277:         my ($result,$perm_reqd) = 
1.707     albertel 1278: 	    &Apache::lonnet::auto_photo_permission($unam,$udom);
1.706     raeburn  1279:         if ($result eq 'ok') {
                   1280:             if (!($perm_reqd eq 'yes')) {
                   1281:                 return(&retrievestudentphoto($udom,$unam,$ext));
                   1282:             }
                   1283:         }
                   1284:     }
                   1285:     return '/adm/lonKaputt/lonlogo_broken.gif';
                   1286: }
                   1287: 
                   1288: sub retrievestudentphoto {
                   1289:     my ($udom,$unam,$ext,$type) = @_;
                   1290:     my $home=&Apache::lonnet::homeserver($unam,$udom);
                   1291:     my $ret=&Apache::lonnet::reply("studentphoto:$udom:$unam:$ext:$type",$home);
                   1292:     if ($ret eq 'ok') {
                   1293:         my $url="/uploaded/$udom/$unam/internal/studentphoto.$ext";
                   1294:         if ($type eq 'thumbnail') {
                   1295:             $url="/uploaded/$udom/$unam/internal/studentphoto_tn.$ext"; 
                   1296:         }
                   1297:         my $tokenurl=&Apache::lonnet::tokenwrapper($url);
                   1298:         return $tokenurl;
                   1299:     } else {
                   1300:         if ($type eq 'thumbnail') {
                   1301:             return '/adm/lonKaputt/genericstudent_tn.gif';
                   1302:         } else { 
                   1303:             return '/adm/lonKaputt/lonlogo_broken.gif';
                   1304:         }
1.617     albertel 1305:     }
                   1306: }
                   1307: 
1.263     www      1308: # -------------------------------------------------------------------- New chat
                   1309: 
                   1310: sub chatsend {
1.724     raeburn  1311:     my ($newentry,$anon,$group)=@_;
1.620     albertel 1312:     my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1313:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1314:     my $chome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.263     www      1315:     &reply('chatsend:'.$cdom.':'.$cnum.':'.
1.620     albertel 1316: 	   &escape($env{'user.domain'}.':'.$env{'user.name'}.':'.$anon.':'.
1.724     raeburn  1317: 		   &escape($newentry)).':'.$group,$chome);
1.292     www      1318: }
                   1319: 
                   1320: # ------------------------------------------ Find current version of a resource
                   1321: 
                   1322: sub getversion {
                   1323:     my $fname=&clutter(shift);
                   1324:     unless ($fname=~/^\/res\//) { return -1; }
                   1325:     return &currentversion(&filelocation('',$fname));
                   1326: }
                   1327: 
                   1328: sub currentversion {
                   1329:     my $fname=shift;
1.599     albertel 1330:     my ($result,$cached)=&is_cached_new('resversion',$fname);
1.440     www      1331:     if (defined($cached)) { return $result; }
1.292     www      1332:     my $author=$fname;
                   1333:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1334:     my ($udom,$uname)=split(/\//,$author);
                   1335:     my $home=homeserver($uname,$udom);
                   1336:     if ($home eq 'no_host') { 
                   1337:         return -1; 
                   1338:     }
                   1339:     my $answer=reply("currentversion:$fname",$home);
                   1340:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1341: 	return -1;
                   1342:     }
1.599     albertel 1343:     return &do_cache_new('resversion',$fname,$answer,600);
1.263     www      1344: }
                   1345: 
1.1       albertel 1346: # ----------------------------- Subscribe to a resource, return URL if possible
1.11      www      1347: 
1.1       albertel 1348: sub subscribe {
                   1349:     my $fname=shift;
1.761     raeburn  1350:     if ($fname=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) { return ''; }
1.532     albertel 1351:     $fname=~s/[\n\r]//g;
1.1       albertel 1352:     my $author=$fname;
                   1353:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1354:     my ($udom,$uname)=split(/\//,$author);
                   1355:     my $home=homeserver($uname,$udom);
1.335     albertel 1356:     if ($home eq 'no_host') {
                   1357:         return 'not_found';
1.1       albertel 1358:     }
                   1359:     my $answer=reply("sub:$fname",$home);
1.64      www      1360:     if (($answer eq 'con_lost') || ($answer eq 'rejected')) {
                   1361: 	$answer.=' by '.$home;
                   1362:     }
1.1       albertel 1363:     return $answer;
                   1364: }
                   1365:     
1.8       www      1366: # -------------------------------------------------------------- Replicate file
                   1367: 
                   1368: sub repcopy {
                   1369:     my $filename=shift;
1.23      www      1370:     $filename=~s/\/+/\//g;
1.607     raeburn  1371:     if ($filename=~m|^/home/httpd/html/adm/|) { return 'ok'; }
                   1372:     if ($filename=~m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 1373:     if ($filename=~m|^/home/httpd/html/userfiles/| or
1.609     banghart 1374: 	$filename=~m -^/*(uploaded|editupload)/-) { 
1.538     albertel 1375: 	return &repcopy_userfile($filename);
                   1376:     }
1.532     albertel 1377:     $filename=~s/[\n\r]//g;
1.8       www      1378:     my $transname="$filename.in.transfer";
1.828     www      1379: # FIXME: this should flock
1.607     raeburn  1380:     if ((-e $filename) || (-e $transname)) { return 'ok'; }
1.8       www      1381:     my $remoteurl=subscribe($filename);
1.64      www      1382:     if ($remoteurl =~ /^con_lost by/) {
                   1383: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1384:            return 'unavailable';
1.8       www      1385:     } elsif ($remoteurl eq 'not_found') {
1.441     albertel 1386: 	   #&logthis("Subscribe returned not_found: $filename");
1.607     raeburn  1387: 	   return 'not_found';
1.64      www      1388:     } elsif ($remoteurl =~ /^rejected by/) {
                   1389: 	   &logthis("Subscribe returned $remoteurl: $filename");
1.607     raeburn  1390:            return 'forbidden';
1.20      www      1391:     } elsif ($remoteurl eq 'directory') {
1.607     raeburn  1392:            return 'ok';
1.8       www      1393:     } else {
1.290     www      1394:         my $author=$filename;
                   1395:         $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                   1396:         my ($udom,$uname)=split(/\//,$author);
                   1397:         my $home=homeserver($uname,$udom);
                   1398:         unless ($home eq $perlvar{'lonHostID'}) {
1.8       www      1399:            my @parts=split(/\//,$filename);
                   1400:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1401:            if ($path ne "$perlvar{'lonDocRoot'}/res") {
                   1402:                &logthis("Malconfiguration for replication: $filename");
1.607     raeburn  1403: 	       return 'bad_request';
1.8       www      1404:            }
                   1405:            my $count;
                   1406:            for ($count=5;$count<$#parts;$count++) {
                   1407:                $path.="/$parts[$count]";
                   1408:                if ((-e $path)!=1) {
                   1409: 		   mkdir($path,0777);
                   1410:                }
                   1411:            }
                   1412:            my $ua=new LWP::UserAgent;
                   1413:            my $request=new HTTP::Request('GET',"$remoteurl");
                   1414:            my $response=$ua->request($request,$transname);
                   1415:            if ($response->is_error()) {
                   1416: 	       unlink($transname);
                   1417:                my $message=$response->status_line;
1.672     albertel 1418:                &logthis("<font color=\"blue\">WARNING:"
1.12      www      1419:                        ." LWP get: $message: $filename</font>");
1.607     raeburn  1420:                return 'unavailable';
1.8       www      1421:            } else {
1.16      www      1422: 	       if ($remoteurl!~/\.meta$/) {
                   1423:                   my $mrequest=new HTTP::Request('GET',$remoteurl.'.meta');
                   1424:                   my $mresponse=$ua->request($mrequest,$filename.'.meta');
                   1425:                   if ($mresponse->is_error()) {
                   1426: 		      unlink($filename.'.meta');
                   1427:                       &logthis(
1.672     albertel 1428:                      "<font color=\"yellow\">INFO: No metadata: $filename</font>");
1.16      www      1429:                   }
                   1430: 	       }
1.8       www      1431:                rename($transname,$filename);
1.607     raeburn  1432:                return 'ok';
1.8       www      1433:            }
1.290     www      1434:        }
1.8       www      1435:     }
1.330     www      1436: }
                   1437: 
                   1438: # ------------------------------------------------ Get server side include body
                   1439: sub ssi_body {
1.381     albertel 1440:     my ($filelink,%form)=@_;
1.606     matthew  1441:     if (! exists($form{'LONCAPA_INTERNAL_no_discussion'})) {
                   1442:         $form{'LONCAPA_INTERNAL_no_discussion'}='true';
                   1443:     }
1.330     www      1444:     my $output=($filelink=~/^http\:/?&externalssi($filelink):
1.381     albertel 1445:                                      &ssi($filelink,%form));
1.778     albertel 1446:     $output=~s|//(\s*<!--)? BEGIN LON-CAPA Internal.+?// END LON-CAPA Internal\s*(-->)?\s||gs;
1.451     albertel 1447:     $output=~s/^.*?\<body[^\>]*\>//si;
                   1448:     $output=~s/(.*)\<\/body\s*\>.*?$/$1/si;
1.330     www      1449:     return $output;
1.8       www      1450: }
                   1451: 
1.15      www      1452: # --------------------------------------------------------- Server Side Include
                   1453: 
1.782     albertel 1454: sub absolute_url {
                   1455:     my ($host_name) = @_;
                   1456:     my $protocol = ($ENV{'SERVER_PORT'} == 443?'https://':'http://');
                   1457:     if ($host_name eq '') {
                   1458: 	$host_name = $ENV{'SERVER_NAME'};
                   1459:     }
                   1460:     return $protocol.$host_name;
                   1461: }
                   1462: 
1.15      www      1463: sub ssi {
                   1464: 
1.23      www      1465:     my ($fn,%form)=@_;
1.15      www      1466: 
                   1467:     my $ua=new LWP::UserAgent;
1.23      www      1468:     
                   1469:     my $request;
1.711     albertel 1470: 
                   1471:     $form{'no_update_last_known'}=1;
1.895     albertel 1472:     &Apache::lonenc::check_encrypt(\$fn);
1.23      www      1473:     if (%form) {
1.782     albertel 1474:       $request=new HTTP::Request('POST',&absolute_url().$fn);
1.201     albertel 1475:       $request->content(join('&',map { &escape($_).'='.&escape($form{$_}) } keys %form));
1.23      www      1476:     } else {
1.782     albertel 1477:       $request=new HTTP::Request('GET',&absolute_url().$fn);
1.23      www      1478:     }
                   1479: 
1.15      www      1480:     $request->header(Cookie => $ENV{'HTTP_COOKIE'});
                   1481:     my $response=$ua->request($request);
                   1482: 
1.324     www      1483:     return $response->content;
                   1484: }
                   1485: 
                   1486: sub externalssi {
                   1487:     my ($url)=@_;
                   1488:     my $ua=new LWP::UserAgent;
                   1489:     my $request=new HTTP::Request('GET',$url);
                   1490:     my $response=$ua->request($request);
1.15      www      1491:     return $response->content;
                   1492: }
1.254     www      1493: 
1.492     albertel 1494: # -------------------------------- Allow a /uploaded/ URI to be vouched for
                   1495: 
                   1496: sub allowuploaded {
                   1497:     my ($srcurl,$url)=@_;
                   1498:     $url=&clutter(&declutter($url));
                   1499:     my $dir=$url;
                   1500:     $dir=~s/\/[^\/]+$//;
                   1501:     my %httpref=();
                   1502:     my $httpurl=&hreflocation('',$url);
                   1503:     $httpref{'httpref.'.$httpurl}=$srcurl;
                   1504:     &Apache::lonnet::appenv(%httpref);
1.254     www      1505: }
1.477     raeburn  1506: 
1.478     albertel 1507: # --------- File operations in /home/httpd/html/userfiles/$domain/1/2/3/$course
1.638     albertel 1508: # input: action, courseID, current domain, intended
1.637     raeburn  1509: #        path to file, source of file, instruction to parse file for objects,
                   1510: #        ref to hash for embedded objects,
                   1511: #        ref to hash for codebase of java objects.
                   1512: #
1.485     raeburn  1513: # output: url to file (if action was uploaddoc), 
                   1514: #         ok if successful, or diagnostic message otherwise (if action was propagate or copy)
1.477     raeburn  1515: #
1.478     albertel 1516: # Allows directory structure to be used within lonUsers/../userfiles/ for a 
                   1517: # course.
1.477     raeburn  1518: #
1.478     albertel 1519: # action = propagate - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1520: #          will be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles in
                   1521: #          course's home server.
1.477     raeburn  1522: #
1.478     albertel 1523: # action = copy - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file will
                   1524: #          be copied from $source (current location) to 
                   1525: #          /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1526: #         and will then be copied to
                   1527: #          /home/httpd/lonUsers/$domain/1/2/3/$course/userfiles/$file in
                   1528: #         course's home server.
1.485     raeburn  1529: #
1.481     raeburn  1530: # action = uploaddoc - /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
1.620     albertel 1531: #         will be retrived from $env{form.uploaddoc} (from DOCS interface) to
1.481     raeburn  1532: #         /home/httpd/html/userfiles/$domain/1/2/3/$course/$file
                   1533: #         and will then be copied to /home/httpd/lonUsers/1/2/3/$course/userfiles/$file
                   1534: #         in course's home server.
1.637     raeburn  1535: #
1.477     raeburn  1536: 
                   1537: sub process_coursefile {
1.638     albertel 1538:     my ($action,$docuname,$docudom,$file,$source,$parser,$allfiles,$codebase)=@_;
1.477     raeburn  1539:     my $fetchresult;
1.638     albertel 1540:     my $home=&homeserver($docuname,$docudom);
1.477     raeburn  1541:     if ($action eq 'propagate') {
1.638     albertel 1542:         $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
                   1543: 			     $home);
1.481     raeburn  1544:     } else {
1.477     raeburn  1545:         my $fpath = '';
                   1546:         my $fname = $file;
1.478     albertel 1547:         ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
1.477     raeburn  1548:         $fpath=$docudom.'/'.$docuname.'/'.$fpath;
1.637     raeburn  1549:         my $filepath = &build_filepath($fpath);
1.481     raeburn  1550:         if ($action eq 'copy') {
                   1551:             if ($source eq '') {
                   1552:                 $fetchresult = 'no source file';
                   1553:                 return $fetchresult;
                   1554:             } else {
                   1555:                 my $destination = $filepath.'/'.$fname;
                   1556:                 rename($source,$destination);
                   1557:                 $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1558:                                  $home);
1.481     raeburn  1559:             }
                   1560:         } elsif ($action eq 'uploaddoc') {
                   1561:             open(my $fh,'>'.$filepath.'/'.$fname);
1.620     albertel 1562:             print $fh $env{'form.'.$source};
1.481     raeburn  1563:             close($fh);
1.637     raeburn  1564:             if ($parser eq 'parse') {
                   1565:                 my $parse_result = &extract_embedded_items($filepath,$fname,$allfiles,$codebase);
                   1566:                 unless ($parse_result eq 'ok') {
                   1567:                     &logthis('Failed to parse '.$filepath.'/'.$fname.' for embedded media: '.$parse_result);
                   1568:                 }
                   1569:             }
1.477     raeburn  1570:             $fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1571:                                  $home);
1.481     raeburn  1572:             if ($fetchresult eq 'ok') {
                   1573:                 return '/uploaded/'.$fpath.'/'.$fname;
                   1574:             } else {
                   1575:                 &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1576:                         ' to host '.$home.': '.$fetchresult);
1.481     raeburn  1577:                 return '/adm/notfound.html';
                   1578:             }
1.477     raeburn  1579:         }
                   1580:     }
1.485     raeburn  1581:     unless ( $fetchresult eq 'ok') {
1.477     raeburn  1582:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
1.638     albertel 1583:              ' to host '.$home.': '.$fetchresult);
1.477     raeburn  1584:     }
                   1585:     return $fetchresult;
                   1586: }
                   1587: 
1.637     raeburn  1588: sub build_filepath {
                   1589:     my ($fpath) = @_;
                   1590:     my $filepath=$perlvar{'lonDocRoot'}.'/userfiles';
                   1591:     unless ($fpath eq '') {
                   1592:         my @parts=split('/',$fpath);
                   1593:         foreach my $part (@parts) {
                   1594:             $filepath.= '/'.$part;
                   1595:             if ((-e $filepath)!=1) {
                   1596:                 mkdir($filepath,0777);
                   1597:             }
                   1598:         }
                   1599:     }
                   1600:     return $filepath;
                   1601: }
                   1602: 
                   1603: sub store_edited_file {
1.638     albertel 1604:     my ($primary_url,$content,$docudom,$docuname,$fetchresult) = @_;
1.637     raeburn  1605:     my $file = $primary_url;
                   1606:     $file =~ s#^/uploaded/$docudom/$docuname/##;
                   1607:     my $fpath = '';
                   1608:     my $fname = $file;
                   1609:     ($fpath,$fname) = ($file =~ m|^(.*)/([^/]+)$|);
                   1610:     $fpath=$docudom.'/'.$docuname.'/'.$fpath;
                   1611:     my $filepath = &build_filepath($fpath);
                   1612:     open(my $fh,'>'.$filepath.'/'.$fname);
                   1613:     print $fh $content;
                   1614:     close($fh);
1.638     albertel 1615:     my $home=&homeserver($docuname,$docudom);
1.637     raeburn  1616:     $$fetchresult= &reply('fetchuserfile:'.$docudom.'/'.$docuname.'/'.$file,
1.638     albertel 1617: 			  $home);
1.637     raeburn  1618:     if ($$fetchresult eq 'ok') {
                   1619:         return '/uploaded/'.$fpath.'/'.$fname;
                   1620:     } else {
1.638     albertel 1621:         &logthis('Failed to transfer '.$docudom.'/'.$docuname.'/'.$file.
                   1622: 		 ' to host '.$home.': '.$$fetchresult);
1.637     raeburn  1623:         return '/adm/notfound.html';
                   1624:     }
                   1625: }
                   1626: 
1.531     albertel 1627: sub clean_filename {
1.831     albertel 1628:     my ($fname,$args)=@_;
1.315     www      1629: # Replace Windows backslashes by forward slashes
1.257     www      1630:     $fname=~s/\\/\//g;
1.831     albertel 1631:     if (!$args->{'keep_path'}) {
                   1632:         # Get rid of everything but the actual filename
                   1633: 	$fname=~s/^.*\/([^\/]+)$/$1/;
                   1634:     }
1.315     www      1635: # Replace spaces by underscores
                   1636:     $fname=~s/\s+/\_/g;
                   1637: # Replace all other weird characters by nothing
1.831     albertel 1638:     $fname=~s{[^/\w\.\-]}{}g;
1.540     albertel 1639: # Replace all .\d. sequences with _\d. so they no longer look like version
                   1640: # numbers
                   1641:     $fname=~s/\.(\d+)(?=\.)/_$1/g;
1.531     albertel 1642:     return $fname;
                   1643: }
                   1644: 
1.608     albertel 1645: # --------------- Take an uploaded file and put it into the userfiles directory
1.686     albertel 1646: # input: $formname - the contents of the file are in $env{"form.$formname"}
1.719     banghart 1647: #                    the desired filenam is in $env{"form.$formname.filename"}
1.686     albertel 1648: #        $coursedoc - if true up to the current course
                   1649: #                     if false
                   1650: #        $subdir - directory in userfile to store the file into
1.858     raeburn  1651: #        $parser - instruction to parse file for objects ($parser = parse)    
                   1652: #        $allfiles - reference to hash for embedded objects
                   1653: #        $codebase - reference to hash for codebase of java objects
                   1654: #        $desuname - username for permanent storage of uploaded file
                   1655: #        $dsetudom - domain for permanaent storage of uploaded file
1.860     raeburn  1656: #        $thumbwidth - width (pixels) of thumbnail to make for uploaded image 
                   1657: #        $thumbheight - height (pixels) of thumbnail to make for uploaded image
1.858     raeburn  1658: # 
1.686     albertel 1659: # output: url of file in userspace, or error: <message> 
                   1660: #             or /adm/notfound.html if failure to upload occurse
1.608     albertel 1661: 
                   1662: 
1.531     albertel 1663: sub userfileupload {
1.860     raeburn  1664:     my ($formname,$coursedoc,$subdir,$parser,$allfiles,$codebase,$destuname,
                   1665:         $destudom,$thumbwidth,$thumbheight)=@_;
1.531     albertel 1666:     if (!defined($subdir)) { $subdir='unknown'; }
1.620     albertel 1667:     my $fname=$env{'form.'.$formname.'.filename'};
1.531     albertel 1668:     $fname=&clean_filename($fname);
1.315     www      1669: # See if there is anything left
1.257     www      1670:     unless ($fname) { return 'error: no uploaded file'; }
1.620     albertel 1671:     chop($env{'form.'.$formname});
1.523     raeburn  1672:     if (($formname eq 'screenshot') && ($subdir eq 'helprequests')) { #files uploaded to help request form are handled differently
                   1673:         my $now = time;
                   1674:         my $filepath = 'tmp/helprequests/'.$now;
                   1675:         my @parts=split(/\//,$filepath);
                   1676:         my $fullpath = $perlvar{'lonDaemons'};
                   1677:         for (my $i=0;$i<@parts;$i++) {
                   1678:             $fullpath .= '/'.$parts[$i];
                   1679:             if ((-e $fullpath)!=1) {
                   1680:                 mkdir($fullpath,0777);
                   1681:             }
                   1682:         }
                   1683:         open(my $fh,'>'.$fullpath.'/'.$fname);
1.620     albertel 1684:         print $fh $env{'form.'.$formname};
1.523     raeburn  1685:         close($fh);
1.741     raeburn  1686:         return $fullpath.'/'.$fname;
                   1687:     } elsif (($formname eq 'coursecreatorxml') && ($subdir eq 'batchupload')) { #files uploaded to create course page are handled differently
                   1688:         my $filepath = 'tmp/addcourse/'.$destudom.'/web/'.$env{'user.name'}.
                   1689:                        '_'.$env{'user.domain'}.'/pending';
                   1690:         my @parts=split(/\//,$filepath);
                   1691:         my $fullpath = $perlvar{'lonDaemons'};
                   1692:         for (my $i=0;$i<@parts;$i++) {
                   1693:             $fullpath .= '/'.$parts[$i];
                   1694:             if ((-e $fullpath)!=1) {
                   1695:                 mkdir($fullpath,0777);
                   1696:             }
                   1697:         }
                   1698:         open(my $fh,'>'.$fullpath.'/'.$fname);
                   1699:         print $fh $env{'form.'.$formname};
                   1700:         close($fh);
                   1701:         return $fullpath.'/'.$fname;
1.523     raeburn  1702:     }
1.719     banghart 1703:     
1.258     www      1704: # Create the directory if not present
1.493     albertel 1705:     $fname="$subdir/$fname";
1.259     www      1706:     if ($coursedoc) {
1.638     albertel 1707: 	my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1708: 	my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.646     raeburn  1709:         if ($env{'form.folder'} =~ m/^(default|supplemental)/) {
1.638     albertel 1710:             return &finishuserfileupload($docuname,$docudom,
                   1711: 					 $formname,$fname,$parser,$allfiles,
1.860     raeburn  1712: 					 $codebase,$thumbwidth,$thumbheight);
1.481     raeburn  1713:         } else {
1.620     albertel 1714:             $fname=$env{'form.folder'}.'/'.$fname;
1.638     albertel 1715:             return &process_coursefile('uploaddoc',$docuname,$docudom,
                   1716: 				       $fname,$formname,$parser,
                   1717: 				       $allfiles,$codebase);
1.481     raeburn  1718:         }
1.719     banghart 1719:     } elsif (defined($destuname)) {
                   1720:         my $docuname=$destuname;
                   1721:         my $docudom=$destudom;
1.860     raeburn  1722: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1723: 				     $parser,$allfiles,$codebase,
                   1724:                                      $thumbwidth,$thumbheight);
1.719     banghart 1725:         
1.259     www      1726:     } else {
1.638     albertel 1727:         my $docuname=$env{'user.name'};
                   1728:         my $docudom=$env{'user.domain'};
1.714     raeburn  1729:         if (exists($env{'form.group'})) {
                   1730:             $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1731:             $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1732:         }
1.860     raeburn  1733: 	return &finishuserfileupload($docuname,$docudom,$formname,$fname,
                   1734: 				     $parser,$allfiles,$codebase,
                   1735:                                      $thumbwidth,$thumbheight);
1.259     www      1736:     }
1.271     www      1737: }
                   1738: 
                   1739: sub finishuserfileupload {
1.860     raeburn  1740:     my ($docuname,$docudom,$formname,$fname,$parser,$allfiles,$codebase,
                   1741:         $thumbwidth,$thumbheight) = @_;
1.477     raeburn  1742:     my $path=$docudom.'/'.$docuname.'/';
1.258     www      1743:     my $filepath=$perlvar{'lonDocRoot'};
1.860     raeburn  1744:     my ($fnamepath,$file,$fetchthumb);
1.494     albertel 1745:     $file=$fname;
                   1746:     if ($fname=~m|/|) {
                   1747:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   1748: 	$path.=$fnamepath.'/';
                   1749:     }
1.259     www      1750:     my @parts=split(/\//,$filepath.'/userfiles/'.$path);
1.258     www      1751:     my $count;
                   1752:     for ($count=4;$count<=$#parts;$count++) {
                   1753:         $filepath.="/$parts[$count]";
                   1754:         if ((-e $filepath)!=1) {
                   1755: 	    mkdir($filepath,0777);
                   1756:         }
                   1757:     }
                   1758: # Save the file
                   1759:     {
1.701     albertel 1760: 	if (!open(FH,'>'.$filepath.'/'.$file)) {
                   1761: 	    &logthis('Failed to create '.$filepath.'/'.$file);
                   1762: 	    print STDERR ('Failed to create '.$filepath.'/'.$file."\n");
                   1763: 	    return '/adm/notfound.html';
                   1764: 	}
                   1765: 	if (!print FH ($env{'form.'.$formname})) {
                   1766: 	    &logthis('Failed to write to '.$filepath.'/'.$file);
                   1767: 	    print STDERR ('Failed to write to '.$filepath.'/'.$file."\n");
                   1768: 	    return '/adm/notfound.html';
                   1769: 	}
1.570     albertel 1770: 	close(FH);
1.258     www      1771:     }
1.637     raeburn  1772:     if ($parser eq 'parse') {
1.638     albertel 1773:         my $parse_result = &extract_embedded_items($filepath,$file,$allfiles,
                   1774: 						   $codebase);
1.637     raeburn  1775:         unless ($parse_result eq 'ok') {
1.638     albertel 1776:             &logthis('Failed to parse '.$filepath.$file.
                   1777: 		     ' for embedded media: '.$parse_result); 
1.637     raeburn  1778:         }
                   1779:     }
1.860     raeburn  1780:     if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   1781:         my $input = $filepath.'/'.$file;
                   1782:         my $output = $filepath.'/'.'tn-'.$file;
                   1783:         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   1784:         system("convert -sample $thumbsize $input $output");
                   1785:         if (-e $filepath.'/'.'tn-'.$file) {
                   1786:             $fetchthumb  = 1; 
                   1787:         }
                   1788:     }
1.858     raeburn  1789:  
1.259     www      1790: # Notify homeserver to grep it
                   1791: #
1.638     albertel 1792:     my $docuhome=&homeserver($docuname,$docudom);
1.494     albertel 1793:     my $fetchresult= &reply('fetchuserfile:'.$path.$file,$docuhome);
1.295     www      1794:     if ($fetchresult eq 'ok') {
1.860     raeburn  1795:         if ($fetchthumb) {
                   1796:             my $thumbresult= &reply('fetchuserfile:'.$path.'tn-'.$file,$docuhome);
                   1797:             if ($thumbresult ne 'ok') {
                   1798:                 &logthis('Failed to transfer '.$path.'tn-'.$file.' to host '.
                   1799:                          $docuhome.': '.$thumbresult);
                   1800:             }
                   1801:         }
1.259     www      1802: #
1.258     www      1803: # Return the URL to it
1.494     albertel 1804:         return '/uploaded/'.$path.$file;
1.263     www      1805:     } else {
1.494     albertel 1806:         &logthis('Failed to transfer '.$path.$file.' to host '.$docuhome.
                   1807: 		 ': '.$fetchresult);
1.263     www      1808:         return '/adm/notfound.html';
1.858     raeburn  1809:     }
1.493     albertel 1810: }
                   1811: 
1.637     raeburn  1812: sub extract_embedded_items {
1.648     raeburn  1813:     my ($filepath,$file,$allfiles,$codebase,$content) = @_;
1.637     raeburn  1814:     my @state = ();
                   1815:     my %javafiles = (
                   1816:                       codebase => '',
                   1817:                       code => '',
                   1818:                       archive => ''
                   1819:                     );
                   1820:     my %mediafiles = (
                   1821:                       src => '',
                   1822:                       movie => '',
                   1823:                      );
1.648     raeburn  1824:     my $p;
                   1825:     if ($content) {
                   1826:         $p = HTML::LCParser->new($content);
                   1827:     } else {
                   1828:         $p = HTML::LCParser->new($filepath.'/'.$file);
                   1829:     }
1.641     albertel 1830:     while (my $t=$p->get_token()) {
1.640     albertel 1831: 	if ($t->[0] eq 'S') {
                   1832: 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
1.886     albertel 1833: 	    push(@state, $tagname);
1.648     raeburn  1834:             if (lc($tagname) eq 'allow') {
                   1835:                 &add_filetype($allfiles,$attr->{'src'},'src');
                   1836:             }
1.640     albertel 1837: 	    if (lc($tagname) eq 'img') {
                   1838: 		&add_filetype($allfiles,$attr->{'src'},'src');
                   1839: 	    }
1.886     albertel 1840: 	    if (lc($tagname) eq 'a') {
                   1841: 		&add_filetype($allfiles,$attr->{'href'},'href');
                   1842: 	    }
1.645     raeburn  1843:             if (lc($tagname) eq 'script') {
                   1844:                 if ($attr->{'archive'} =~ /\.jar$/i) {
                   1845:                     &add_filetype($allfiles,$attr->{'archive'},'archive');
                   1846:                 } else {
                   1847:                     &add_filetype($allfiles,$attr->{'src'},'src');
                   1848:                 }
                   1849:             }
                   1850:             if (lc($tagname) eq 'link') {
                   1851:                 if (lc($attr->{'rel'}) eq 'stylesheet') { 
                   1852:                     &add_filetype($allfiles,$attr->{'href'},'href');
                   1853:                 }
                   1854:             }
1.640     albertel 1855: 	    if (lc($tagname) eq 'object' ||
                   1856: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')) {
                   1857: 		foreach my $item (keys(%javafiles)) {
                   1858: 		    $javafiles{$item} = '';
                   1859: 		}
                   1860: 	    }
                   1861: 	    if (lc($state[-2]) eq 'object' && lc($tagname) eq 'param') {
                   1862: 		my $name = lc($attr->{'name'});
                   1863: 		foreach my $item (keys(%javafiles)) {
                   1864: 		    if ($name eq $item) {
                   1865: 			$javafiles{$item} = $attr->{'value'};
                   1866: 			last;
                   1867: 		    }
                   1868: 		}
                   1869: 		foreach my $item (keys(%mediafiles)) {
                   1870: 		    if ($name eq $item) {
                   1871: 			&add_filetype($allfiles, $attr->{'value'}, 'value');
                   1872: 			last;
                   1873: 		    }
                   1874: 		}
                   1875: 	    }
                   1876: 	    if (lc($tagname) eq 'embed' || lc($tagname) eq 'applet') {
                   1877: 		foreach my $item (keys(%javafiles)) {
                   1878: 		    if ($attr->{$item}) {
                   1879: 			$javafiles{$item} = $attr->{$item};
                   1880: 			last;
                   1881: 		    }
                   1882: 		}
                   1883: 		foreach my $item (keys(%mediafiles)) {
                   1884: 		    if ($attr->{$item}) {
                   1885: 			&add_filetype($allfiles,$attr->{$item},$item);
                   1886: 			last;
                   1887: 		    }
                   1888: 		}
                   1889: 	    }
                   1890: 	} elsif ($t->[0] eq 'E') {
                   1891: 	    my ($tagname) = ($t->[1]);
                   1892: 	    if ($javafiles{'codebase'} ne '') {
                   1893: 		$javafiles{'codebase'} .= '/';
                   1894: 	    }  
                   1895: 	    if (lc($tagname) eq 'applet' ||
                   1896: 		lc($tagname) eq 'object' ||
                   1897: 		(lc($tagname) eq 'embed' && lc($state[-2]) ne 'object')
                   1898: 		) {
                   1899: 		foreach my $item (keys(%javafiles)) {
                   1900: 		    if ($item ne 'codebase' && $javafiles{$item} ne '') {
                   1901: 			my $file=$javafiles{'codebase'}.$javafiles{$item};
                   1902: 			&add_filetype($allfiles,$file,$item);
                   1903: 		    }
                   1904: 		}
                   1905: 	    } 
                   1906: 	    pop @state;
                   1907: 	}
                   1908:     }
1.637     raeburn  1909:     return 'ok';
                   1910: }
                   1911: 
1.639     albertel 1912: sub add_filetype {
                   1913:     my ($allfiles,$file,$type)=@_;
                   1914:     if (exists($allfiles->{$file})) {
                   1915: 	unless (grep/^\Q$type\E$/, @{$allfiles->{$file}}) {
                   1916: 	    push(@{$allfiles->{$file}}, &escape($type));
                   1917: 	}
                   1918:     } else {
                   1919: 	@{$allfiles->{$file}} = (&escape($type));
1.637     raeburn  1920:     }
                   1921: }
                   1922: 
1.493     albertel 1923: sub removeuploadedurl {
                   1924:     my ($url)=@_;
                   1925:     my (undef,undef,$udom,$uname,$fname)=split('/',$url,5);
1.613     albertel 1926:     return &removeuserfile($uname,$udom,$fname);
1.490     albertel 1927: }
                   1928: 
                   1929: sub removeuserfile {
                   1930:     my ($docuname,$docudom,$fname)=@_;
                   1931:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1932:     my $result = &reply("removeuserfile:$docudom/$docuname/$fname",$home);
                   1933:     if ($result eq 'ok') {
                   1934:         if (($fname !~ /\.meta$/) && (&is_portfolio_file($fname))) {
                   1935:             my $metafile = $fname.'.meta';
                   1936:             my $metaresult = &removeuserfile($docuname,$docudom,$metafile); 
1.823     albertel 1937: 	    my $url = "/uploaded/$docudom/$docuname/$fname";
                   1938:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1939:             my $sqlresult = 
1.823     albertel 1940:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1941:                                         'portfolio_metadata',$group,
                   1942:                                         'delete');
1.798     raeburn  1943:         }
                   1944:     }
                   1945:     return $result;
1.257     www      1946: }
1.15      www      1947: 
1.530     albertel 1948: sub mkdiruserfile {
                   1949:     my ($docuname,$docudom,$dir)=@_;
                   1950:     my $home=&homeserver($docuname,$docudom);
                   1951:     return &reply("mkdiruserfile:".&escape("$docudom/$docuname/$dir"),$home);
                   1952: }
                   1953: 
1.531     albertel 1954: sub renameuserfile {
                   1955:     my ($docuname,$docudom,$old,$new)=@_;
                   1956:     my $home=&homeserver($docuname,$docudom);
1.798     raeburn  1957:     my $result = &reply("renameuserfile:$docudom:$docuname:".
                   1958:                         &escape("$old").':'.&escape("$new"),$home);
                   1959:     if ($result eq 'ok') {
                   1960:         if (($old !~ /\.meta$/) && (&is_portfolio_file($old))) {
                   1961:             my $oldmeta = $old.'.meta';
                   1962:             my $newmeta = $new.'.meta';
                   1963:             my $metaresult = 
                   1964:                 &renameuserfile($docuname,$docudom,$oldmeta,$newmeta);
1.823     albertel 1965: 	    my $url = "/uploaded/$docudom/$docuname/$old";
                   1966:             my ($file,$group) = (&parse_portfolio_url($url))[3,4];
1.821     raeburn  1967:             my $sqlresult = 
1.823     albertel 1968:                 &update_portfolio_table($docuname,$docudom,$file,
1.821     raeburn  1969:                                         'portfolio_metadata',$group,
                   1970:                                         'delete');
1.798     raeburn  1971:         }
                   1972:     }
                   1973:     return $result;
1.531     albertel 1974: }
                   1975: 
1.14      www      1976: # ------------------------------------------------------------------------- Log
                   1977: 
                   1978: sub log {
                   1979:     my ($dom,$nam,$hom,$what)=@_;
1.47      www      1980:     return critical("log:$dom:$nam:$what",$hom);
1.157     www      1981: }
                   1982: 
                   1983: # ------------------------------------------------------------------ Course Log
1.352     www      1984: #
                   1985: # This routine flushes several buffers of non-mission-critical nature
                   1986: #
1.157     www      1987: 
                   1988: sub flushcourselogs {
1.352     www      1989:     &logthis('Flushing log buffers');
                   1990: #
                   1991: # course logs
                   1992: # This is a log of all transactions in a course, which can be used
                   1993: # for data mining purposes
                   1994: #
                   1995: # It also collects the courseid database, which lists last transaction
                   1996: # times and course titles for all courseids
                   1997: #
                   1998:     my %courseidbuffer=();
1.800     albertel 1999:     foreach my $crsid (keys %courselogs) {
1.352     www      2000:         if (&reply('log:'.$coursedombuf{$crsid}.':'.$coursenumbuf{$crsid}.':'.
1.188     www      2001: 		          &escape($courselogs{$crsid}),
                   2002: 		          $coursehombuf{$crsid}) eq 'ok') {
1.157     www      2003: 	    delete $courselogs{$crsid};
                   2004:         } else {
                   2005:             &logthis('Failed to flush log buffer for '.$crsid);
                   2006:             if (length($courselogs{$crsid})>40000) {
1.672     albertel 2007:                &logthis("<font color=\"blue\">WARNING: Buffer for ".$crsid.
1.157     www      2008:                         " exceeded maximum size, deleting.</font>");
                   2009:                delete $courselogs{$crsid};
                   2010:             }
1.352     www      2011:         }
                   2012:         if ($courseidbuffer{$coursehombuf{$crsid}}) {
                   2013:            $courseidbuffer{$coursehombuf{$crsid}}.='&'.
1.516     raeburn  2014: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  2015:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.352     www      2016:         } else {
                   2017:            $courseidbuffer{$coursehombuf{$crsid}}=
1.516     raeburn  2018: 			 &escape($crsid).'='.&escape($coursedescrbuf{$crsid}).
1.741     raeburn  2019:                          ':'.&escape($courseinstcodebuf{$crsid}).':'.&escape($courseownerbuf{$crsid}).':'.&escape($coursetypebuf{$crsid});
1.571     raeburn  2020:         }
1.191     harris41 2021:     }
1.352     www      2022: #
                   2023: # Write course id database (reverse lookup) to homeserver of courses 
                   2024: # Is used in pickcourse
                   2025: #
1.840     albertel 2026:     foreach my $crs_home (keys(%courseidbuffer)) {
1.844     albertel 2027:         &courseidput(&host_domain($crs_home),$courseidbuffer{$crs_home},
1.840     albertel 2028: 		     $crs_home);
1.352     www      2029:     }
                   2030: #
                   2031: # File accesses
                   2032: # Writes to the dynamic metadata of resources to get hit counts, etc.
                   2033: #
1.449     matthew  2034:     foreach my $entry (keys(%accesshash)) {
1.458     matthew  2035:         if ($entry =~ /___count$/) {
                   2036:             my ($dom,$name);
1.807     albertel 2037:             ($dom,$name,undef)=
1.811     albertel 2038: 		($entry=~m{___($match_domain)/($match_name)/(.*)___count$});
1.458     matthew  2039:             if (! defined($dom) || $dom eq '' || 
                   2040:                 ! defined($name) || $name eq '') {
1.620     albertel 2041:                 my $cid = $env{'request.course.id'};
                   2042:                 $dom  = $env{'request.'.$cid.'.domain'};
                   2043:                 $name = $env{'request.'.$cid.'.num'};
1.458     matthew  2044:             }
1.450     matthew  2045:             my $value = $accesshash{$entry};
                   2046:             my (undef,$url,undef) = ($entry =~ /^(.*)___(.*)___count$/);
                   2047:             my %temphash=($url => $value);
1.449     matthew  2048:             my $result = &inc('nohist_accesscount',\%temphash,$dom,$name);
                   2049:             if ($result eq 'ok') {
                   2050:                 delete $accesshash{$entry};
                   2051:             } elsif ($result eq 'unknown_cmd') {
                   2052:                 # Target server has old code running on it.
1.450     matthew  2053:                 my %temphash=($entry => $value);
1.449     matthew  2054:                 if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2055:                     delete $accesshash{$entry};
                   2056:                 }
                   2057:             }
                   2058:         } else {
1.811     albertel 2059:             my ($dom,$name) = ($entry=~m{___($match_domain)/($match_name)/(.*)___(\w+)$});
1.450     matthew  2060:             my %temphash=($entry => $accesshash{$entry});
1.449     matthew  2061:             if (&put('nohist_resevaldata',\%temphash,$dom,$name) eq 'ok') {
                   2062:                 delete $accesshash{$entry};
                   2063:             }
1.185     www      2064:         }
1.191     harris41 2065:     }
1.352     www      2066: #
                   2067: # Roles
                   2068: # Reverse lookup of user roles for course faculty/staff and co-authorship
                   2069: #
1.800     albertel 2070:     foreach my $entry (keys(%userrolehash)) {
1.351     www      2071:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=
1.349     www      2072: 	    split(/\:/,$entry);
                   2073:         if (&Apache::lonnet::put('nohist_userroles',
1.351     www      2074:              { $role.':'.$uname.':'.$udom.':'.$rsec => $userrolehash{$entry} },
1.349     www      2075:                 $rudom,$runame) eq 'ok') {
                   2076: 	    delete $userrolehash{$entry};
                   2077:         }
                   2078:     }
1.662     raeburn  2079: #
                   2080: # Reverse lookup of domain roles (dc, ad, li, sc, au)
                   2081: #
                   2082:     my %domrolebuffer = ();
                   2083:     foreach my $entry (keys %domainrolehash) {
                   2084:         my ($role,$uname,$udom,$runame,$rudom,$rsec)=split/:/,$entry;
                   2085:         if ($domrolebuffer{$rudom}) {
                   2086:             $domrolebuffer{$rudom}.='&'.&escape($entry).
                   2087:                       '='.&escape($domainrolehash{$entry});
                   2088:         } else {
                   2089:             $domrolebuffer{$rudom}.=&escape($entry).
                   2090:                       '='.&escape($domainrolehash{$entry});
                   2091:         }
                   2092:         delete $domainrolehash{$entry};
                   2093:     }
                   2094:     foreach my $dom (keys(%domrolebuffer)) {
1.841     albertel 2095: 	my %servers = &get_servers($dom,'library');
                   2096: 	foreach my $tryserver (keys(%servers)) {
                   2097: 	    unless (&reply('domroleput:'.$dom.':'.
                   2098: 			   $domrolebuffer{$dom},$tryserver) eq 'ok') {
                   2099: 		&logthis('Put of domain roles failed for '.$dom.' and  '.$tryserver);
                   2100: 	    }
1.662     raeburn  2101:         }
                   2102:     }
1.186     www      2103:     $dumpcount++;
1.157     www      2104: }
                   2105: 
                   2106: sub courselog {
                   2107:     my $what=shift;
1.158     www      2108:     $what=time.':'.$what;
1.620     albertel 2109:     unless ($env{'request.course.id'}) { return ''; }
                   2110:     $coursedombuf{$env{'request.course.id'}}=
                   2111:        $env{'course.'.$env{'request.course.id'}.'.domain'};
                   2112:     $coursenumbuf{$env{'request.course.id'}}=
                   2113:        $env{'course.'.$env{'request.course.id'}.'.num'};
                   2114:     $coursehombuf{$env{'request.course.id'}}=
                   2115:        $env{'course.'.$env{'request.course.id'}.'.home'};
                   2116:     $coursedescrbuf{$env{'request.course.id'}}=
                   2117:        $env{'course.'.$env{'request.course.id'}.'.description'};
                   2118:     $courseinstcodebuf{$env{'request.course.id'}}=
                   2119:        $env{'course.'.$env{'request.course.id'}.'.internal.coursecode'};
                   2120:     $courseownerbuf{$env{'request.course.id'}}=
                   2121:        $env{'course.'.$env{'request.course.id'}.'.internal.courseowner'};
1.741     raeburn  2122:     $coursetypebuf{$env{'request.course.id'}}=
                   2123:        $env{'course.'.$env{'request.course.id'}.'.type'};
1.620     albertel 2124:     if (defined $courselogs{$env{'request.course.id'}}) {
                   2125: 	$courselogs{$env{'request.course.id'}}.='&'.$what;
1.157     www      2126:     } else {
1.620     albertel 2127: 	$courselogs{$env{'request.course.id'}}.=$what;
1.157     www      2128:     }
1.620     albertel 2129:     if (length($courselogs{$env{'request.course.id'}})>4048) {
1.157     www      2130: 	&flushcourselogs();
                   2131:     }
1.158     www      2132: }
                   2133: 
                   2134: sub courseacclog {
                   2135:     my $fnsymb=shift;
1.620     albertel 2136:     unless ($env{'request.course.id'}) { return ''; }
                   2137:     my $what=$fnsymb.':'.$env{'user.name'}.':'.$env{'user.domain'};
1.657     albertel 2138:     if ($fnsymb=~/(problem|exam|quiz|assess|survey|form|task|page)$/) {
1.187     www      2139:         $what.=':POST';
1.583     matthew  2140:         # FIXME: Probably ought to escape things....
1.800     albertel 2141: 	foreach my $key (keys(%env)) {
                   2142:             if ($key=~/^form\.(.*)/) {
                   2143: 		$what.=':'.$1.'='.$env{$key};
1.158     www      2144:             }
1.191     harris41 2145:         }
1.583     matthew  2146:     } elsif ($fnsymb =~ m:^/adm/searchcat:) {
                   2147:         # FIXME: We should not be depending on a form parameter that someone
                   2148:         # editing lonsearchcat.pm might change in the future.
1.620     albertel 2149:         if ($env{'form.phase'} eq 'course_search') {
1.583     matthew  2150:             $what.= ':POST';
                   2151:             # FIXME: Probably ought to escape things....
                   2152:             foreach my $element ('courseexp','crsfulltext','crsrelated',
                   2153:                                  'crsdiscuss') {
1.620     albertel 2154:                 $what.=':'.$element.'='.$env{'form.'.$element};
1.583     matthew  2155:             }
                   2156:         }
1.158     www      2157:     }
                   2158:     &courselog($what);
1.149     www      2159: }
                   2160: 
1.185     www      2161: sub countacc {
                   2162:     my $url=&declutter(shift);
1.458     matthew  2163:     return if (! defined($url) || $url eq '');
1.620     albertel 2164:     unless ($env{'request.course.id'}) { return ''; }
                   2165:     $accesshash{$env{'request.course.id'}.'___'.$url.'___course'}=1;
1.281     www      2166:     my $key=$$.$processmarker.'_'.$dumpcount.'___'.$url.'___count';
1.450     matthew  2167:     $accesshash{$key}++;
1.185     www      2168: }
1.349     www      2169: 
1.361     www      2170: sub linklog {
                   2171:     my ($from,$to)=@_;
                   2172:     $from=&declutter($from);
                   2173:     $to=&declutter($to);
                   2174:     $accesshash{$from.'___'.$to.'___comefrom'}=1;
                   2175:     $accesshash{$to.'___'.$from.'___goto'}=1;
                   2176: }
                   2177:   
1.349     www      2178: sub userrolelog {
                   2179:     my ($trole,$username,$domain,$area,$tstart,$tend)=@_;
1.661     raeburn  2180:     if (($trole=~/^ca/) || ($trole=~/^aa/) ||
1.662     raeburn  2181:         ($trole=~/^in/) || ($trole=~/^cc/) ||
1.661     raeburn  2182:         ($trole=~/^ep/) || ($trole=~/^cr/) ||
                   2183:         ($trole=~/^ta/)) {
1.350     www      2184:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2185:        $userrolehash
                   2186:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
1.349     www      2187:                     =$tend.':'.$tstart;
1.662     raeburn  2188:     }
1.898     albertel 2189:     if (($env{'request.role'} =~ /dc\./) &&
                   2190: 	(($trole=~/^au/) || ($trole=~/^in/) ||
                   2191: 	 ($trole=~/^cc/) || ($trole=~/^ep/) ||
                   2192: 	 ($trole=~/^cr/) || ($trole=~/^ta/))) {
                   2193:        $userrolehash
                   2194:          {$trole.':'.$username.':'.$domain.':'.$env{'user.name'}.':'.$env{'user.domain'}.':'}
                   2195:                     =$tend.':'.$tstart;
                   2196:     }
1.662     raeburn  2197:     if (($trole=~/^dc/) || ($trole=~/^ad/) ||
                   2198:         ($trole=~/^li/) || ($trole=~/^li/) ||
                   2199:         ($trole=~/^au/) || ($trole=~/^dg/) ||
                   2200:         ($trole=~/^sc/)) {
                   2201:        my (undef,$rudom,$runame,$rsec)=split(/\//,$area);
                   2202:        $domainrolehash
                   2203:          {$trole.':'.$username.':'.$domain.':'.$runame.':'.$rudom.':'.$rsec}
                   2204:                     = $tend.':'.$tstart;
                   2205:     }
1.898     albertel 2206:     &flushcourselogs();
1.351     www      2207: }
                   2208: 
                   2209: sub get_course_adv_roles {
                   2210:     my $cid=shift;
1.620     albertel 2211:     $cid=$env{'request.course.id'} unless (defined($cid));
1.351     www      2212:     my %coursehash=&coursedescription($cid);
1.470     www      2213:     my %nothide=();
1.800     albertel 2214:     foreach my $user (split(/\s*\,\s*/,$coursehash{'nothideprivileged'})) {
                   2215: 	$nothide{join(':',split(/[\@\:]/,$user))}=1;
1.470     www      2216:     }
1.351     www      2217:     my %returnhash=();
                   2218:     my %dumphash=
                   2219:             &dump('nohist_userroles',$coursehash{'domain'},$coursehash{'num'});
                   2220:     my $now=time;
1.800     albertel 2221:     foreach my $entry (keys %dumphash) {
                   2222: 	my ($tend,$tstart)=split(/\:/,$dumphash{$entry});
1.351     www      2223:         if (($tstart) && ($tstart<0)) { next; }
                   2224:         if (($tend) && ($tend<$now)) { next; }
                   2225:         if (($tstart) && ($now<$tstart)) { next; }
1.800     albertel 2226:         my ($role,$username,$domain,$section)=split(/\:/,$entry);
1.576     albertel 2227: 	if ($username eq '' || $domain eq '') { next; }
1.470     www      2228: 	if ((&privileged($username,$domain)) && 
                   2229: 	    (!$nothide{$username.':'.$domain})) { next; }
1.656     albertel 2230: 	if ($role eq 'cr') { next; }
1.351     www      2231:         my $key=&plaintext($role);
                   2232:         if ($section) { $key.=' (Sec/Grp '.$section.')'; }
                   2233:         if ($returnhash{$key}) {
                   2234: 	    $returnhash{$key}.=','.$username.':'.$domain;
                   2235:         } else {
                   2236:             $returnhash{$key}=$username.':'.$domain;
                   2237:         }
1.400     www      2238:      }
                   2239:     return %returnhash;
                   2240: }
                   2241: 
                   2242: sub get_my_roles {
1.858     raeburn  2243:     my ($uname,$udom,$context,$types,$roles,$roledoms)=@_;
1.620     albertel 2244:     unless (defined($uname)) { $uname=$env{'user.name'}; }
                   2245:     unless (defined($udom)) { $udom=$env{'user.domain'}; }
1.858     raeburn  2246:     my %dumphash;
                   2247:     if ($context eq 'userroles') { 
                   2248:         %dumphash = &dump('roles',$udom,$uname);
                   2249:     } else {
                   2250:         %dumphash=
1.400     www      2251:             &dump('nohist_userroles',$udom,$uname);
1.858     raeburn  2252:     }
1.400     www      2253:     my %returnhash=();
                   2254:     my $now=time;
1.800     albertel 2255:     foreach my $entry (keys(%dumphash)) {
1.867     raeburn  2256:         my ($role,$tend,$tstart);
                   2257:         if ($context eq 'userroles') {
                   2258: 	    ($role,$tend,$tstart)=split(/_/,$dumphash{$entry});
                   2259:         } else {
                   2260:             ($tend,$tstart)=split(/\:/,$dumphash{$entry});
                   2261:         }
1.400     www      2262:         if (($tstart) && ($tstart<0)) { next; }
1.832     raeburn  2263:         my $status = 'active';
                   2264:         if (($tend) && ($tend<$now)) {
                   2265:             $status = 'previous';
                   2266:         } 
                   2267:         if (($tstart) && ($now<$tstart)) {
                   2268:             $status = 'future';
                   2269:         }
                   2270:         if (ref($types) eq 'ARRAY') {
                   2271:             if (!grep(/^\Q$status\E$/,@{$types})) {
                   2272:                 next;
                   2273:             } 
                   2274:         } else {
                   2275:             if ($status ne 'active') {
                   2276:                 next;
                   2277:             }
                   2278:         }
1.867     raeburn  2279:         my ($rolecode,$username,$domain,$section,$area);
                   2280:         if ($context eq 'userroles') {
                   2281:             ($area,$rolecode) = split(/_/,$entry);
                   2282:             (undef,$domain,$username,$section) = split(/\//,$area);
                   2283:         } else {
                   2284:             ($role,$username,$domain,$section) = split(/\:/,$entry);
                   2285:         }
1.832     raeburn  2286:         if (ref($roledoms) eq 'ARRAY') {
                   2287:             if (!grep(/^\Q$domain\E$/,@{$roledoms})) {
                   2288:                 next;
                   2289:             }
                   2290:         }
                   2291:         if (ref($roles) eq 'ARRAY') {
                   2292:             if (!grep(/^\Q$role\E$/,@{$roles})) {
                   2293:                 next;
                   2294:             }
1.867     raeburn  2295:         }
1.400     www      2296: 	$returnhash{$username.':'.$domain.':'.$role}=$tstart.':'.$tend;
1.832     raeburn  2297:     }
1.373     www      2298:     return %returnhash;
1.399     www      2299: }
                   2300: 
                   2301: # ----------------------------------------------------- Frontpage Announcements
                   2302: #
                   2303: #
                   2304: 
                   2305: sub postannounce {
                   2306:     my ($server,$text)=@_;
1.844     albertel 2307:     unless (&allowed('psa',&host_domain($server))) { return 'refused'; }
1.399     www      2308:     unless ($text=~/\w/) { $text=''; }
                   2309:     return &reply('setannounce:'.&escape($text),$server);
                   2310: }
                   2311: 
                   2312: sub getannounce {
1.448     albertel 2313: 
                   2314:     if (open(my $fh,$perlvar{'lonDocRoot'}.'/announcement.txt')) {
1.399     www      2315: 	my $announcement='';
1.800     albertel 2316: 	while (my $line = <$fh>) { $announcement .= $line; }
1.448     albertel 2317: 	close($fh);
1.399     www      2318: 	if ($announcement=~/\w/) { 
                   2319: 	    return 
                   2320:    '<table bgcolor="#FF5555" cellpadding="5" cellspacing="3">'.
1.518     albertel 2321:    '<tr><td bgcolor="#FFFFFF"><tt>'.$announcement.'</tt></td></tr></table>'; 
1.399     www      2322: 	} else {
                   2323: 	    return '';
                   2324: 	}
                   2325:     } else {
                   2326: 	return '';
                   2327:     }
1.351     www      2328: }
1.353     www      2329: 
                   2330: # ---------------------------------------------------------- Course ID routines
                   2331: # Deal with domain's nohist_courseid.db files
                   2332: #
                   2333: 
                   2334: sub courseidput {
                   2335:     my ($domain,$what,$coursehome)=@_;
                   2336:     return &reply('courseidput:'.$domain.':'.$what,$coursehome);
                   2337: }
                   2338: 
                   2339: sub courseiddump {
1.791     raeburn  2340:     my ($domfilter,$descfilter,$sincefilter,$instcodefilter,$ownerfilter,$coursefilter,$hostidflag,$hostidref,$typefilter,$regexp_ok)=@_;
1.353     www      2341:     my %returnhash=();
1.355     www      2342:     unless ($domfilter) { $domfilter=''; }
1.845     albertel 2343:     my %libserv = &all_library();
                   2344:     foreach my $tryserver (keys(%libserv)) {
                   2345:         if ( (  $hostidflag == 1 
                   2346: 	        && grep(/^\Q$tryserver\E$/,@{$hostidref}) ) 
                   2347: 	     || (!defined($hostidflag)) ) {
                   2348: 
                   2349: 	    if ($domfilter eq ''
                   2350: 		|| (&host_domain($tryserver) eq $domfilter)) {
1.800     albertel 2351: 	        foreach my $line (
1.844     albertel 2352:                  split(/\&/,&reply('courseiddump:'.&host_domain($tryserver).':'.
1.571     raeburn  2353: 			       $sincefilter.':'.&escape($descfilter).':'.
1.791     raeburn  2354:                                &escape($instcodefilter).':'.&escape($ownerfilter).':'.&escape($coursefilter).':'.&escape($typefilter).':'.&escape($regexp_ok),
1.354     www      2355:                                $tryserver))) {
1.800     albertel 2356: 		    my ($key,$value)=split(/\=/,$line,2);
1.506     raeburn  2357:                     if (($key) && ($value)) {
1.516     raeburn  2358: 		        $returnhash{&unescape($key)}=$value;
1.506     raeburn  2359:                     }
1.353     www      2360:                 }
                   2361:             }
                   2362:         }
                   2363:     }
                   2364:     return %returnhash;
                   2365: }
                   2366: 
1.658     raeburn  2367: # ---------------------------------------------------------- DC e-mail
1.662     raeburn  2368: 
                   2369: sub dcmailput {
1.685     raeburn  2370:     my ($domain,$msgid,$message,$server)=@_;
1.662     raeburn  2371:     my $status = &Apache::lonnet::critical(
1.740     www      2372:        'dcmailput:'.$domain.':'.&escape($msgid).'='.
                   2373:        &escape($message),$server);
1.662     raeburn  2374:     return $status;
                   2375: }
                   2376: 
1.658     raeburn  2377: sub dcmaildump {
                   2378:     my ($dom,$startdate,$enddate,$senders) = @_;
1.685     raeburn  2379:     my %returnhash=();
1.846     albertel 2380: 
                   2381:     if (defined(&domain($dom,'primary'))) {
1.685     raeburn  2382:         my $cmd='dcmaildump:'.$dom.':'.&escape($startdate).':'.
                   2383:                                                          &escape($enddate).':';
                   2384: 	my @esc_senders=map { &escape($_)} @$senders;
                   2385: 	$cmd.=&escape(join('&',@esc_senders));
1.846     albertel 2386: 	foreach my $line (split(/\&/,&reply($cmd,&domain($dom,'primary')))) {
1.800     albertel 2387:             my ($key,$value) = split(/\=/,$line,2);
1.685     raeburn  2388:             if (($key) && ($value)) {
                   2389:                 $returnhash{&unescape($key)} = &unescape($value);
1.658     raeburn  2390:             }
                   2391:         }
                   2392:     }
                   2393:     return %returnhash;
                   2394: }
1.662     raeburn  2395: # ---------------------------------------------------------- Domain roles
                   2396: 
                   2397: sub get_domain_roles {
                   2398:     my ($dom,$roles,$startdate,$enddate)=@_;
                   2399:     if (undef($startdate) || $startdate eq '') {
                   2400:         $startdate = '.';
                   2401:     }
                   2402:     if (undef($enddate) || $enddate eq '') {
                   2403:         $enddate = '.';
                   2404:     }
                   2405:     my $rolelist = join(':',@{$roles});
                   2406:     my %personnel = ();
1.841     albertel 2407: 
                   2408:     my %servers = &get_servers($dom,'library');
                   2409:     foreach my $tryserver (keys(%servers)) {
                   2410: 	%{$personnel{$tryserver}}=();
                   2411: 	foreach my $line (split(/\&/,&reply('domrolesdump:'.$dom.':'.
                   2412: 					    &escape($startdate).':'.
                   2413: 					    &escape($enddate).':'.
                   2414: 					    &escape($rolelist), $tryserver))) {
                   2415: 	    my ($key,$value) = split(/\=/,$line,2);
                   2416: 	    if (($key) && ($value)) {
                   2417: 		$personnel{$tryserver}{&unescape($key)} = &unescape($value);
                   2418: 	    }
                   2419: 	}
1.662     raeburn  2420:     }
                   2421:     return %personnel;
                   2422: }
1.658     raeburn  2423: 
1.149     www      2424: # ----------------------------------------------------------- Check out an item
                   2425: 
1.504     albertel 2426: sub get_first_access {
                   2427:     my ($type,$argsymb)=@_;
1.790     albertel 2428:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2429:     if ($argsymb) { $symb=$argsymb; }
                   2430:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2431:     if ($type eq 'map') {
                   2432: 	$res=&symbread($map);
                   2433:     } else {
                   2434: 	$res=$symb;
                   2435:     }
                   2436:     my %times=&get('firstaccesstimes',["$courseid\0$res"],$udom,$uname);
                   2437:     return $times{"$courseid\0$res"};
1.504     albertel 2438: }
                   2439: 
                   2440: sub set_first_access {
                   2441:     my ($type)=@_;
1.790     albertel 2442:     my ($symb,$courseid,$udom,$uname)=&whichuser();
1.504     albertel 2443:     my ($map,$id,$res)=&decode_symb($symb);
1.588     albertel 2444:     if ($type eq 'map') {
                   2445: 	$res=&symbread($map);
                   2446:     } else {
                   2447: 	$res=$symb;
                   2448:     }
                   2449:     my $firstaccess=&get_first_access($type,$symb);
1.505     albertel 2450:     if (!$firstaccess) {
1.588     albertel 2451: 	return &put('firstaccesstimes',{"$courseid\0$res"=>time},$udom,$uname);
1.505     albertel 2452:     }
                   2453:     return 'already_set';
1.504     albertel 2454: }
                   2455: 
1.149     www      2456: sub checkout {
                   2457:     my ($symb,$tuname,$tudom,$tcrsid)=@_;
                   2458:     my $now=time;
                   2459:     my $lonhost=$perlvar{'lonHostID'};
                   2460:     my $infostr=&escape(
1.234     www      2461:                  'CHECKOUTTOKEN&'.
1.149     www      2462:                  $tuname.'&'.
                   2463:                  $tudom.'&'.
                   2464:                  $tcrsid.'&'.
                   2465:                  $symb.'&'.
                   2466: 		 $now.'&'.$ENV{'REMOTE_ADDR'});
                   2467:     my $token=&reply('tmpput:'.$infostr,$lonhost);
1.151     www      2468:     if ($token=~/^error\:/) { 
1.672     albertel 2469:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2470:                 "Checkout tmpput failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2471:                  "</font>");
                   2472:         return ''; 
                   2473:     }
                   2474: 
1.149     www      2475:     $token=~s/^(\d+)\_.*\_(\d+)$/$1\*$2\*$lonhost/;
                   2476:     $token=~tr/a-z/A-Z/;
                   2477: 
1.153     www      2478:     my %infohash=('resource.0.outtoken' => $token,
                   2479:                   'resource.0.checkouttime' => $now,
                   2480:                   'resource.0.outremote' => $ENV{'REMOTE_ADDR'});
1.149     www      2481: 
                   2482:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2483:        return '';
1.151     www      2484:     } else {
1.672     albertel 2485:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2486:                 "Checkout cstore failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2487:                  "</font>");
1.149     www      2488:     }    
                   2489: 
                   2490:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2491:                          &escape('Checkout '.$infostr.' - '.
                   2492:                                                  $token)) ne 'ok') {
                   2493: 	return '';
1.151     www      2494:     } else {
1.672     albertel 2495:         &logthis("<font color=\"blue\">WARNING: ".
1.151     www      2496:                 "Checkout log failed ".$tudom.' - '.$tuname.' - '.$symb.
                   2497:                  "</font>");
1.149     www      2498:     }
1.151     www      2499:     return $token;
1.149     www      2500: }
                   2501: 
                   2502: # ------------------------------------------------------------ Check in an item
                   2503: 
                   2504: sub checkin {
                   2505:     my $token=shift;
1.150     www      2506:     my $now=time;
                   2507:     my ($ta,$tb,$lonhost)=split(/\*/,$token);
                   2508:     $lonhost=~tr/A-Z/a-z/;
1.838     albertel 2509:     my $dtoken=$ta.'_'.&hostname($lonhost).'_'.$tb;
1.150     www      2510:     $dtoken=~s/\W/\_/g;
1.234     www      2511:     my ($dummy,$tuname,$tudom,$tcrsid,$symb,$chtim,$rmaddr)=
1.150     www      2512:                  split(/\&/,&unescape(&reply('tmpget:'.$dtoken,$lonhost)));
                   2513: 
1.154     www      2514:     unless (($tuname) && ($tudom)) {
                   2515:         &logthis('Check in '.$token.' ('.$dtoken.') failed');
                   2516:         return '';
                   2517:     }
                   2518:     
                   2519:     unless (&allowed('mgr',$tcrsid)) {
                   2520:         &logthis('Check in '.$token.' ('.$dtoken.') unauthorized: '.
1.620     albertel 2521:                  $env{'user.name'}.' - '.$env{'user.domain'});
1.154     www      2522:         return '';
                   2523:     }
                   2524: 
1.153     www      2525:     my %infohash=('resource.0.intoken' => $token,
                   2526:                   'resource.0.checkintime' => $now,
                   2527:                   'resource.0.inremote' => $ENV{'REMOTE_ADDR'});
1.150     www      2528: 
                   2529:     unless (&cstore(\%infohash,$symb,$tcrsid,$tudom,$tuname) eq 'ok') {
                   2530:        return '';
                   2531:     }    
                   2532: 
                   2533:     if (&log($tudom,$tuname,&homeserver($tuname,$tudom),
                   2534:                          &escape('Checkin - '.$token)) ne 'ok') {
                   2535: 	return '';
                   2536:     }
                   2537: 
                   2538:     return ($symb,$tuname,$tudom,$tcrsid);    
1.110     www      2539: }
                   2540: 
                   2541: # --------------------------------------------- Set Expire Date for Spreadsheet
                   2542: 
                   2543: sub expirespread {
                   2544:     my ($uname,$udom,$stype,$usymb)=@_;
1.620     albertel 2545:     my $cid=$env{'request.course.id'}; 
1.110     www      2546:     if ($cid) {
                   2547:        my $now=time;
                   2548:        my $key=$uname.':'.$udom.':'.$stype.':'.$usymb;
1.620     albertel 2549:        return &reply('put:'.$env{'course.'.$cid.'.domain'}.':'.
                   2550:                             $env{'course.'.$cid.'.num'}.
1.110     www      2551: 	        	    ':nohist_expirationdates:'.
                   2552:                             &escape($key).'='.$now,
1.620     albertel 2553:                             $env{'course.'.$cid.'.home'})
1.110     www      2554:     }
                   2555:     return 'ok';
1.14      www      2556: }
                   2557: 
1.109     www      2558: # ----------------------------------------------------- Devalidate Spreadsheets
                   2559: 
                   2560: sub devalidate {
1.325     www      2561:     my ($symb,$uname,$udom)=@_;
1.620     albertel 2562:     my $cid=$env{'request.course.id'}; 
1.109     www      2563:     if ($cid) {
1.391     matthew  2564:         # delete the stored spreadsheets for
                   2565:         # - the student level sheet of this user in course's homespace
                   2566:         # - the assessment level sheet for this resource 
                   2567:         #   for this user in user's homespace
1.553     albertel 2568: 	# - current conditional state info
1.325     www      2569: 	my $key=$uname.':'.$udom.':';
1.109     www      2570:         my $status=
1.299     matthew  2571: 	    &del('nohist_calculatedsheets',
1.391     matthew  2572: 		 [$key.'studentcalc:'],
1.620     albertel 2573: 		 $env{'course.'.$cid.'.domain'},
                   2574: 		 $env{'course.'.$cid.'.num'})
1.133     albertel 2575: 		.' '.
                   2576: 	    &del('nohist_calculatedsheets_'.$cid,
1.391     matthew  2577: 		 [$key.'assesscalc:'.$symb],$udom,$uname);
1.109     www      2578:         unless ($status eq 'ok ok') {
                   2579:            &logthis('Could not devalidate spreadsheet '.
1.325     www      2580:                     $uname.' at '.$udom.' for '.
1.109     www      2581: 		    $symb.': '.$status);
1.133     albertel 2582:         }
1.553     albertel 2583: 	&delenv('user.state.'.$cid);
1.109     www      2584:     }
                   2585: }
                   2586: 
1.265     albertel 2587: sub get_scalar {
                   2588:     my ($string,$end) = @_;
                   2589:     my $value;
                   2590:     if ($$string =~ s/^([^&]*?)($end)/$2/) {
                   2591: 	$value = $1;
                   2592:     } elsif ($$string =~ s/^([^&]*?)&//) {
                   2593: 	$value = $1;
                   2594:     }
                   2595:     return &unescape($value);
                   2596: }
                   2597: 
                   2598: sub array2str {
                   2599:   my (@array) = @_;
                   2600:   my $result=&arrayref2str(\@array);
                   2601:   $result=~s/^__ARRAY_REF__//;
                   2602:   $result=~s/__END_ARRAY_REF__$//;
                   2603:   return $result;
                   2604: }
                   2605: 
1.204     albertel 2606: sub arrayref2str {
                   2607:   my ($arrayref) = @_;
1.265     albertel 2608:   my $result='__ARRAY_REF__';
1.204     albertel 2609:   foreach my $elem (@$arrayref) {
1.265     albertel 2610:     if(ref($elem) eq 'ARRAY') {
                   2611:       $result.=&arrayref2str($elem).'&';
                   2612:     } elsif(ref($elem) eq 'HASH') {
                   2613:       $result.=&hashref2str($elem).'&';
                   2614:     } elsif(ref($elem)) {
                   2615:       #print("Got a ref of ".(ref($elem))." skipping.");
1.204     albertel 2616:     } else {
                   2617:       $result.=&escape($elem).'&';
                   2618:     }
                   2619:   }
                   2620:   $result=~s/\&$//;
1.265     albertel 2621:   $result .= '__END_ARRAY_REF__';
1.204     albertel 2622:   return $result;
                   2623: }
                   2624: 
1.168     albertel 2625: sub hash2str {
1.204     albertel 2626:   my (%hash) = @_;
                   2627:   my $result=&hashref2str(\%hash);
1.265     albertel 2628:   $result=~s/^__HASH_REF__//;
                   2629:   $result=~s/__END_HASH_REF__$//;
1.204     albertel 2630:   return $result;
                   2631: }
                   2632: 
                   2633: sub hashref2str {
                   2634:   my ($hashref)=@_;
1.265     albertel 2635:   my $result='__HASH_REF__';
1.800     albertel 2636:   foreach my $key (sort(keys(%$hashref))) {
                   2637:     if (ref($key) eq 'ARRAY') {
                   2638:       $result.=&arrayref2str($key).'=';
                   2639:     } elsif (ref($key) eq 'HASH') {
                   2640:       $result.=&hashref2str($key).'=';
                   2641:     } elsif (ref($key)) {
1.265     albertel 2642:       $result.='=';
1.800     albertel 2643:       #print("Got a ref of ".(ref($key))." skipping.");
1.204     albertel 2644:     } else {
1.800     albertel 2645: 	if ($key) {$result.=&escape($key).'=';} else { last; }
1.204     albertel 2646:     }
                   2647: 
1.800     albertel 2648:     if(ref($hashref->{$key}) eq 'ARRAY') {
                   2649:       $result.=&arrayref2str($hashref->{$key}).'&';
                   2650:     } elsif(ref($hashref->{$key}) eq 'HASH') {
                   2651:       $result.=&hashref2str($hashref->{$key}).'&';
                   2652:     } elsif(ref($hashref->{$key})) {
1.265     albertel 2653:        $result.='&';
1.800     albertel 2654:       #print("Got a ref of ".(ref($hashref->{$key}))." skipping.");
1.204     albertel 2655:     } else {
1.800     albertel 2656:       $result.=&escape($hashref->{$key}).'&';
1.204     albertel 2657:     }
                   2658:   }
1.168     albertel 2659:   $result=~s/\&$//;
1.265     albertel 2660:   $result .= '__END_HASH_REF__';
1.168     albertel 2661:   return $result;
                   2662: }
                   2663: 
                   2664: sub str2hash {
1.265     albertel 2665:     my ($string)=@_;
                   2666:     my ($hash)=&str2hashref('__HASH_REF__'.$string.'__END_HASH_REF__');
                   2667:     return %$hash;
                   2668: }
                   2669: 
                   2670: sub str2hashref {
1.168     albertel 2671:   my ($string) = @_;
1.265     albertel 2672: 
                   2673:   my %hash;
                   2674: 
                   2675:   if($string !~ /^__HASH_REF__/) {
                   2676:       if (! ($string eq '' || !defined($string))) {
                   2677: 	  $hash{'error'}='Not hash reference';
                   2678:       }
                   2679:       return (\%hash, $string);
                   2680:   }
                   2681: 
                   2682:   $string =~ s/^__HASH_REF__//;
                   2683: 
                   2684:   while($string !~ /^__END_HASH_REF__/) {
                   2685:       #key
                   2686:       my $key='';
                   2687:       if($string =~ /^__HASH_REF__/) {
                   2688:           ($key, $string)=&str2hashref($string);
                   2689:           if(defined($key->{'error'})) {
                   2690:               $hash{'error'}='Bad data';
                   2691:               return (\%hash, $string);
                   2692:           }
                   2693:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2694:           ($key, $string)=&str2arrayref($string);
                   2695:           if($key->[0] eq 'Array reference error') {
                   2696:               $hash{'error'}='Bad data';
                   2697:               return (\%hash, $string);
                   2698:           }
                   2699:       } else {
                   2700:           $string =~ s/^(.*?)=//;
1.267     albertel 2701: 	  $key=&unescape($1);
1.265     albertel 2702:       }
                   2703:       $string =~ s/^=//;
                   2704: 
                   2705:       #value
                   2706:       my $value='';
                   2707:       if($string =~ /^__HASH_REF__/) {
                   2708:           ($value, $string)=&str2hashref($string);
                   2709:           if(defined($value->{'error'})) {
                   2710:               $hash{'error'}='Bad data';
                   2711:               return (\%hash, $string);
                   2712:           }
                   2713:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2714:           ($value, $string)=&str2arrayref($string);
                   2715:           if($value->[0] eq 'Array reference error') {
                   2716:               $hash{'error'}='Bad data';
                   2717:               return (\%hash, $string);
                   2718:           }
                   2719:       } else {
                   2720: 	  $value=&get_scalar(\$string,'__END_HASH_REF__');
                   2721:       }
                   2722:       $string =~ s/^&//;
                   2723: 
                   2724:       $hash{$key}=$value;
1.204     albertel 2725:   }
1.265     albertel 2726: 
                   2727:   $string =~ s/^__END_HASH_REF__//;
                   2728: 
                   2729:   return (\%hash, $string);
1.204     albertel 2730: }
                   2731: 
                   2732: sub str2array {
1.265     albertel 2733:     my ($string)=@_;
                   2734:     my ($array)=&str2arrayref('__ARRAY_REF__'.$string.'__END_ARRAY_REF__');
                   2735:     return @$array;
                   2736: }
                   2737: 
                   2738: sub str2arrayref {
1.204     albertel 2739:   my ($string) = @_;
1.265     albertel 2740:   my @array;
                   2741: 
                   2742:   if($string !~ /^__ARRAY_REF__/) {
                   2743:       if (! ($string eq '' || !defined($string))) {
                   2744: 	  $array[0]='Array reference error';
                   2745:       }
                   2746:       return (\@array, $string);
                   2747:   }
                   2748: 
                   2749:   $string =~ s/^__ARRAY_REF__//;
                   2750: 
                   2751:   while($string !~ /^__END_ARRAY_REF__/) {
                   2752:       my $value='';
                   2753:       if($string =~ /^__HASH_REF__/) {
                   2754:           ($value, $string)=&str2hashref($string);
                   2755:           if(defined($value->{'error'})) {
                   2756:               $array[0] ='Array reference error';
                   2757:               return (\@array, $string);
                   2758:           }
                   2759:       } elsif($string =~ /^__ARRAY_REF__/) {
                   2760:           ($value, $string)=&str2arrayref($string);
                   2761:           if($value->[0] eq 'Array reference error') {
                   2762:               $array[0] ='Array reference error';
                   2763:               return (\@array, $string);
                   2764:           }
                   2765:       } else {
                   2766: 	  $value=&get_scalar(\$string,'__END_ARRAY_REF__');
                   2767:       }
                   2768:       $string =~ s/^&//;
                   2769: 
                   2770:       push(@array, $value);
1.191     harris41 2771:   }
1.265     albertel 2772: 
                   2773:   $string =~ s/^__END_ARRAY_REF__//;
                   2774: 
                   2775:   return (\@array, $string);
1.168     albertel 2776: }
                   2777: 
1.167     albertel 2778: # -------------------------------------------------------------------Temp Store
                   2779: 
1.168     albertel 2780: sub tmpreset {
                   2781:   my ($symb,$namespace,$domain,$stuname) = @_;
                   2782:   if (!$symb) {
                   2783:     $symb=&symbread();
1.620     albertel 2784:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2785:   }
                   2786:   $symb=escape($symb);
                   2787: 
1.620     albertel 2788:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.168     albertel 2789:   $namespace=~s/\//\_/g;
                   2790:   $namespace=~s/\W//g;
                   2791: 
1.620     albertel 2792:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2793:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2794:   if ($domain eq 'public' && $stuname eq 'public') {
                   2795:       $stuname=$ENV{'REMOTE_ADDR'};
                   2796:   }
1.168     albertel 2797:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2798:   my %hash;
                   2799:   if (tie(%hash,'GDBM_File',
                   2800: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2801: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2802:     foreach my $key (keys %hash) {
1.180     albertel 2803:       if ($key=~ /:$symb/) {
1.168     albertel 2804: 	delete($hash{$key});
                   2805:       }
                   2806:     }
                   2807:   }
                   2808: }
                   2809: 
1.167     albertel 2810: sub tmpstore {
1.168     albertel 2811:   my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2812: 
                   2813:   if (!$symb) {
                   2814:     $symb=&symbread();
1.620     albertel 2815:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2816:   }
                   2817:   $symb=escape($symb);
                   2818: 
                   2819:   if (!$namespace) {
                   2820:     # I don't think we would ever want to store this for a course.
                   2821:     # it seems this will only be used if we don't have a course.
1.620     albertel 2822:     #$namespace=$env{'request.course.id'};
1.168     albertel 2823:     #if (!$namespace) {
1.620     albertel 2824:       $namespace=$env{'request.state'};
1.168     albertel 2825:     #}
                   2826:   }
                   2827:   $namespace=~s/\//\_/g;
                   2828:   $namespace=~s/\W//g;
1.620     albertel 2829:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2830:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2831:   if ($domain eq 'public' && $stuname eq 'public') {
                   2832:       $stuname=$ENV{'REMOTE_ADDR'};
                   2833:   }
1.168     albertel 2834:   my $now=time;
                   2835:   my %hash;
                   2836:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2837:   if (tie(%hash,'GDBM_File',
                   2838: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2839: 	  &GDBM_WRCREAT(),0640)) {
1.168     albertel 2840:     $hash{"version:$symb"}++;
                   2841:     my $version=$hash{"version:$symb"};
                   2842:     my $allkeys=''; 
                   2843:     foreach my $key (keys(%$storehash)) {
                   2844:       $allkeys.=$key.':';
1.591     albertel 2845:       $hash{"$version:$symb:$key"}=&freeze_escape($$storehash{$key});
1.168     albertel 2846:     }
                   2847:     $hash{"$version:$symb:timestamp"}=$now;
                   2848:     $allkeys.='timestamp';
                   2849:     $hash{"$version:keys:$symb"}=$allkeys;
                   2850:     if (untie(%hash)) {
                   2851:       return 'ok';
                   2852:     } else {
                   2853:       return "error:$!";
                   2854:     }
                   2855:   } else {
                   2856:     return "error:$!";
                   2857:   }
                   2858: }
1.167     albertel 2859: 
1.168     albertel 2860: # -----------------------------------------------------------------Temp Restore
1.167     albertel 2861: 
1.168     albertel 2862: sub tmprestore {
                   2863:   my ($symb,$namespace,$domain,$stuname) = @_;
1.167     albertel 2864: 
1.168     albertel 2865:   if (!$symb) {
                   2866:     $symb=&symbread();
1.620     albertel 2867:     if (!$symb) { $symb= $env{'request.url'}; }
1.168     albertel 2868:   }
                   2869:   $symb=escape($symb);
                   2870: 
1.620     albertel 2871:   if (!$namespace) { $namespace=$env{'request.state'}; }
1.591     albertel 2872: 
1.620     albertel 2873:   if (!$domain) { $domain=$env{'user.domain'}; }
                   2874:   if (!$stuname) { $stuname=$env{'user.name'}; }
1.591     albertel 2875:   if ($domain eq 'public' && $stuname eq 'public') {
                   2876:       $stuname=$ENV{'REMOTE_ADDR'};
                   2877:   }
1.168     albertel 2878:   my %returnhash;
                   2879:   $namespace=~s/\//\_/g;
                   2880:   $namespace=~s/\W//g;
                   2881:   my %hash;
                   2882:   my $path=$perlvar{'lonDaemons'}.'/tmp';
                   2883:   if (tie(%hash,'GDBM_File',
                   2884: 	  $path.'/tmpstore_'.$stuname.'_'.$domain.'_'.$namespace.'.db',
1.256     albertel 2885: 	  &GDBM_READER(),0640)) {
1.168     albertel 2886:     my $version=$hash{"version:$symb"};
                   2887:     $returnhash{'version'}=$version;
                   2888:     my $scope;
                   2889:     for ($scope=1;$scope<=$version;$scope++) {
                   2890:       my $vkeys=$hash{"$scope:keys:$symb"};
                   2891:       my @keys=split(/:/,$vkeys);
                   2892:       my $key;
                   2893:       $returnhash{"$scope:keys"}=$vkeys;
                   2894:       foreach $key (@keys) {
1.591     albertel 2895: 	$returnhash{"$scope:$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
                   2896: 	$returnhash{"$key"}=&thaw_unescape($hash{"$scope:$symb:$key"});
1.167     albertel 2897:       }
                   2898:     }
1.168     albertel 2899:     if (!(untie(%hash))) {
                   2900:       return "error:$!";
                   2901:     }
                   2902:   } else {
                   2903:     return "error:$!";
                   2904:   }
                   2905:   return %returnhash;
1.167     albertel 2906: }
                   2907: 
1.9       www      2908: # ----------------------------------------------------------------------- Store
                   2909: 
                   2910: sub store {
1.124     www      2911:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2912:     my $home='';
                   2913: 
1.168     albertel 2914:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2915: 
1.213     www      2916:     $symb=&symbclean($symb);
1.122     albertel 2917:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2918: 
1.620     albertel 2919:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2920:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2921: 
                   2922:     &devalidate($symb,$stuname,$domain);
1.109     www      2923: 
                   2924:     $symb=escape($symb);
1.187     www      2925:     if (!$namespace) { 
1.620     albertel 2926:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2927:           return ''; 
                   2928:        } 
                   2929:     }
1.620     albertel 2930:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2931: 
                   2932:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2933:     $$storehash{'host'}=$perlvar{'lonHostID'};
                   2934: 
1.12      www      2935:     my $namevalue='';
1.800     albertel 2936:     foreach my $key (keys(%$storehash)) {
                   2937:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2938:     }
1.12      www      2939:     $namevalue=~s/\&$//;
1.187     www      2940:     &courselog($symb.':'.$stuname.':'.$domain.':STORE:'.$namevalue);
1.124     www      2941:     return reply("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.9       www      2942: }
                   2943: 
1.47      www      2944: # -------------------------------------------------------------- Critical Store
                   2945: 
                   2946: sub cstore {
1.124     www      2947:     my ($storehash,$symb,$namespace,$domain,$stuname) = @_;
                   2948:     my $home='';
                   2949: 
1.168     albertel 2950:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2951: 
1.213     www      2952:     $symb=&symbclean($symb);
1.122     albertel 2953:     if (!$symb) { unless ($symb=&symbread()) { return ''; } }
1.109     www      2954: 
1.620     albertel 2955:     if (!$domain) { $domain=$env{'user.domain'}; }
                   2956:     if (!$stuname) { $stuname=$env{'user.name'}; }
1.325     www      2957: 
                   2958:     &devalidate($symb,$stuname,$domain);
1.109     www      2959: 
                   2960:     $symb=escape($symb);
1.187     www      2961:     if (!$namespace) { 
1.620     albertel 2962:        unless ($namespace=$env{'request.course.id'}) { 
1.187     www      2963:           return ''; 
                   2964:        } 
                   2965:     }
1.620     albertel 2966:     if (!$home) { $home=$env{'user.home'}; }
1.447     www      2967: 
                   2968:     $$storehash{'ip'}=$ENV{'REMOTE_ADDR'};
                   2969:     $$storehash{'host'}=$perlvar{'lonHostID'};
1.122     albertel 2970: 
1.47      www      2971:     my $namevalue='';
1.800     albertel 2972:     foreach my $key (keys(%$storehash)) {
                   2973:         $namevalue.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
1.191     harris41 2974:     }
1.47      www      2975:     $namevalue=~s/\&$//;
1.187     www      2976:     &courselog($symb.':'.$stuname.':'.$domain.':CSTORE:'.$namevalue);
1.188     www      2977:     return critical
                   2978:                 ("store:$domain:$stuname:$namespace:$symb:$namevalue","$home");
1.47      www      2979: }
                   2980: 
1.9       www      2981: # --------------------------------------------------------------------- Restore
                   2982: 
                   2983: sub restore {
1.124     www      2984:     my ($symb,$namespace,$domain,$stuname) = @_;
                   2985:     my $home='';
                   2986: 
1.168     albertel 2987:     if ($stuname) { $home=&homeserver($stuname,$domain); }
1.124     www      2988: 
1.122     albertel 2989:     if (!$symb) {
                   2990:       unless ($symb=escape(&symbread())) { return ''; }
                   2991:     } else {
1.213     www      2992:       $symb=&escape(&symbclean($symb));
1.122     albertel 2993:     }
1.188     www      2994:     if (!$namespace) { 
1.620     albertel 2995:        unless ($namespace=$env{'request.course.id'}) { 
1.188     www      2996:           return ''; 
                   2997:        } 
                   2998:     }
1.620     albertel 2999:     if (!$domain) { $domain=$env{'user.domain'}; }
                   3000:     if (!$stuname) { $stuname=$env{'user.name'}; }
                   3001:     if (!$home) { $home=$env{'user.home'}; }
1.122     albertel 3002:     my $answer=&reply("restore:$domain:$stuname:$namespace:$symb","$home");
                   3003: 
1.12      www      3004:     my %returnhash=();
1.800     albertel 3005:     foreach my $line (split(/\&/,$answer)) {
                   3006: 	my ($name,$value)=split(/\=/,$line);
1.591     albertel 3007:         $returnhash{&unescape($name)}=&thaw_unescape($value);
1.191     harris41 3008:     }
1.75      www      3009:     my $version;
                   3010:     for ($version=1;$version<=$returnhash{'version'};$version++) {
1.800     albertel 3011:        foreach my $item (split(/\:/,$returnhash{$version.':keys'})) {
                   3012:           $returnhash{$item}=$returnhash{$version.':'.$item};
1.191     harris41 3013:        }
1.75      www      3014:     }
1.13      www      3015:     return %returnhash;
1.34      www      3016: }
                   3017: 
                   3018: # ---------------------------------------------------------- Course Description
                   3019: 
                   3020: sub coursedescription {
1.731     albertel 3021:     my ($courseid,$args)=@_;
1.34      www      3022:     $courseid=~s/^\///;
1.49      www      3023:     $courseid=~s/\_/\//g;
1.34      www      3024:     my ($cdomain,$cnum)=split(/\//,$courseid);
1.129     albertel 3025:     my $chome=&homeserver($cnum,$cdomain);
1.302     albertel 3026:     my $normalid=$cdomain.'_'.$cnum;
                   3027:     # need to always cache even if we get errors otherwise we keep 
                   3028:     # trying and trying and trying to get the course description.
                   3029:     my %envhash=();
                   3030:     my %returnhash=();
1.731     albertel 3031:     
                   3032:     my $expiretime=600;
                   3033:     if ($env{'request.course.id'} eq $normalid) {
                   3034: 	$expiretime=120;
                   3035:     }
                   3036: 
                   3037:     my $prefix='course.'.$cdomain.'_'.$cnum.'.';
                   3038:     if (!$args->{'freshen_cache'}
                   3039: 	&& ((time-$env{$prefix.'last_cache'}) < $expiretime) ) {
                   3040: 	foreach my $key (keys(%env)) {
                   3041: 	    next if ($key !~ /^\Q$prefix\E(.*)/);
                   3042: 	    my ($setting) = $1;
                   3043: 	    $returnhash{$setting} = $env{$key};
                   3044: 	}
                   3045: 	return %returnhash;
                   3046:     }
                   3047: 
                   3048:     # get the data agin
                   3049:     if (!$args->{'one_time'}) {
                   3050: 	$envhash{'course.'.$normalid.'.last_cache'}=time;
                   3051:     }
1.811     albertel 3052: 
1.34      www      3053:     if ($chome ne 'no_host') {
1.302     albertel 3054:        %returnhash=&dump('environment',$cdomain,$cnum);
1.129     albertel 3055:        if (!exists($returnhash{'con_lost'})) {
                   3056:            $returnhash{'home'}= $chome;
                   3057: 	   $returnhash{'domain'} = $cdomain;
                   3058: 	   $returnhash{'num'} = $cnum;
1.741     raeburn  3059:            if (!defined($returnhash{'type'})) {
                   3060:                $returnhash{'type'} = 'Course';
                   3061:            }
1.130     albertel 3062:            while (my ($name,$value) = each %returnhash) {
1.53      www      3063:                $envhash{'course.'.$normalid.'.'.$name}=$value;
1.129     albertel 3064:            }
1.270     www      3065:            $returnhash{'url'}=&clutter($returnhash{'url'});
1.34      www      3066:            $returnhash{'fn'}=$perlvar{'lonDaemons'}.'/tmp/'.
1.620     albertel 3067: 	       $env{'user.name'}.'_'.$cdomain.'_'.$cnum;
1.60      www      3068:            $envhash{'course.'.$normalid.'.home'}=$chome;
                   3069:            $envhash{'course.'.$normalid.'.domain'}=$cdomain;
                   3070:            $envhash{'course.'.$normalid.'.num'}=$cnum;
1.34      www      3071:        }
                   3072:     }
1.731     albertel 3073:     if (!$args->{'one_time'}) {
                   3074: 	&appenv(%envhash);
                   3075:     }
1.302     albertel 3076:     return %returnhash;
1.461     www      3077: }
                   3078: 
                   3079: # -------------------------------------------------See if a user is privileged
                   3080: 
                   3081: sub privileged {
                   3082:     my ($username,$domain)=@_;
                   3083:     my $rolesdump=&reply("dump:$domain:$username:roles",
                   3084: 			&homeserver($username,$domain));
                   3085:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return 0; }
                   3086:     my $now=time;
                   3087:     if ($rolesdump ne '') {
1.800     albertel 3088:         foreach my $entry (split(/&/,$rolesdump)) {
                   3089: 	    if ($entry!~/^rolesdef_/) {
                   3090: 		my ($area,$role)=split(/=/,$entry);
1.461     www      3091: 		$area=~s/\_\w\w$//;
                   3092: 		my ($trole,$tend,$tstart)=split(/_/,$role);
                   3093: 		if (($trole eq 'dc') || ($trole eq 'su')) {
                   3094: 		    my $active=1;
                   3095: 		    if ($tend) {
                   3096: 			if ($tend<$now) { $active=0; }
                   3097: 		    }
                   3098: 		    if ($tstart) {
                   3099: 			if ($tstart>$now) { $active=0; }
                   3100: 		    }
                   3101: 		    if ($active) { return 1; }
                   3102: 		}
                   3103: 	    }
                   3104: 	}
                   3105:     }
                   3106:     return 0;
1.9       www      3107: }
1.1       albertel 3108: 
1.103     harris41 3109: # -------------------------------------------------------- Get user privileges
1.11      www      3110: 
                   3111: sub rolesinit {
                   3112:     my ($domain,$username,$authhost)=@_;
                   3113:     my $rolesdump=reply("dump:$domain:$username:roles",$authhost);
1.12      www      3114:     if (($rolesdump eq 'con_lost') || ($rolesdump eq '')) { return ''; }
1.11      www      3115:     my %allroles=();
1.678     raeburn  3116:     my %allgroups=();   
1.11      www      3117:     my $now=time;
1.743     albertel 3118:     my %userroles = ('user.login.time' => $now);
1.678     raeburn  3119:     my $group_privs;
1.11      www      3120: 
                   3121:     if ($rolesdump ne '') {
1.800     albertel 3122:         foreach my $entry (split(/&/,$rolesdump)) {
                   3123: 	  if ($entry!~/^rolesdef_/) {
                   3124:             my ($area,$role)=split(/=/,$entry);
1.587     albertel 3125: 	    $area=~s/\_\w\w$//;
1.678     raeburn  3126:             my ($trole,$tend,$tstart,$group_privs);
1.587     albertel 3127: 	    if ($role=~/^cr/) { 
1.807     albertel 3128: 		if ($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|) {
                   3129: 		    ($trole,my $trest)=($role=~m|^(cr/$match_domain/$match_username/[a-zA-Z0-9]+)_(.*)$|);
1.655     albertel 3130: 		    ($tend,$tstart)=split('_',$trest);
                   3131: 		} else {
                   3132: 		    $trole=$role;
                   3133: 		}
1.678     raeburn  3134:             } elsif ($role =~ m|^gr/|) {
                   3135:                 ($trole,$tend,$tstart) = split(/_/,$role);
                   3136:                 ($trole,$group_privs) = split(/\//,$trole);
                   3137:                 $group_privs = &unescape($group_privs);
1.587     albertel 3138: 	    } else {
                   3139: 		($trole,$tend,$tstart)=split(/_/,$role);
                   3140: 	    }
1.743     albertel 3141: 	    my %new_role = &set_arearole($trole,$area,$tstart,$tend,$domain,
                   3142: 					 $username);
                   3143: 	    @userroles{keys(%new_role)} = @new_role{keys(%new_role)};
1.567     raeburn  3144:             if (($tend!=0) && ($tend<$now)) { $trole=''; }
                   3145:             if (($tstart!=0) && ($tstart>$now)) { $trole=''; }
1.11      www      3146:             if (($area ne '') && ($trole ne '')) {
1.347     albertel 3147: 		my $spec=$trole.'.'.$area;
                   3148: 		my ($tdummy,$tdomain,$trest)=split(/\//,$area);
                   3149: 		if ($trole =~ /^cr\//) {
1.567     raeburn  3150:                     &custom_roleprivs(\%allroles,$trole,$tdomain,$trest,$spec,$area);
1.678     raeburn  3151:                 } elsif ($trole eq 'gr') {
                   3152:                     &group_roleprivs(\%allgroups,$area,$group_privs,$tend,$tstart);
1.347     albertel 3153: 		} else {
1.567     raeburn  3154:                     &standard_roleprivs(\%allroles,$trole,$tdomain,$spec,$trest,$area);
1.347     albertel 3155: 		}
1.12      www      3156:             }
1.662     raeburn  3157:           }
1.191     harris41 3158:         }
1.743     albertel 3159:         my ($author,$adv) = &set_userprivs(\%userroles,\%allroles,\%allgroups);
                   3160:         $userroles{'user.adv'}    = $adv;
                   3161: 	$userroles{'user.author'} = $author;
1.620     albertel 3162:         $env{'user.adv'}=$adv;
1.11      www      3163:     }
1.743     albertel 3164:     return \%userroles;  
1.11      www      3165: }
                   3166: 
1.567     raeburn  3167: sub set_arearole {
                   3168:     my ($trole,$area,$tstart,$tend,$domain,$username) = @_;
                   3169: # log the associated role with the area
                   3170:     &userrolelog($trole,$username,$domain,$area,$tstart,$tend);
1.743     albertel 3171:     return ('user.role.'.$trole.'.'.$area => $tstart.'.'.$tend);
1.567     raeburn  3172: }
                   3173: 
                   3174: sub custom_roleprivs {
                   3175:     my ($allroles,$trole,$tdomain,$trest,$spec,$area) = @_;
                   3176:     my ($rdummy,$rdomain,$rauthor,$rrole)=split(/\//,$trole);
                   3177:     my $homsvr=homeserver($rauthor,$rdomain);
1.838     albertel 3178:     if (&hostname($homsvr) ne '') {
1.567     raeburn  3179:         my ($rdummy,$roledef)=
                   3180:             &get('roles',["rolesdef_$rrole"],$rdomain,$rauthor);
                   3181:         if (($rdummy ne 'con_lost') && ($roledef ne '')) {
                   3182:             my ($syspriv,$dompriv,$coursepriv)=split(/\_/,$roledef);
                   3183:             if (defined($syspriv)) {
                   3184:                 $$allroles{'cm./'}.=':'.$syspriv;
                   3185:                 $$allroles{$spec.'./'}.=':'.$syspriv;
                   3186:             }
                   3187:             if ($tdomain ne '') {
                   3188:                 if (defined($dompriv)) {
                   3189:                     $$allroles{'cm./'.$tdomain.'/'}.=':'.$dompriv;
                   3190:                     $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$dompriv;
                   3191:                 }
                   3192:                 if (($trest ne '') && (defined($coursepriv))) {
                   3193:                     $$allroles{'cm.'.$area}.=':'.$coursepriv;
                   3194:                     $$allroles{$spec.'.'.$area}.=':'.$coursepriv;
                   3195:                 }
                   3196:             }
                   3197:         }
                   3198:     }
                   3199: }
                   3200: 
1.678     raeburn  3201: sub group_roleprivs {
                   3202:     my ($allgroups,$area,$group_privs,$tend,$tstart) = @_;
                   3203:     my $access = 1;
                   3204:     my $now = time;
                   3205:     if (($tend!=0) && ($tend<$now)) { $access = 0; }
                   3206:     if (($tstart!=0) && ($tstart>$now)) { $access=0; }
                   3207:     if ($access) {
1.811     albertel 3208:         my ($course,$group) = ($area =~ m|(/$match_domain/$match_courseid)/([^/]+)$|);
1.678     raeburn  3209:         $$allgroups{$course}{$group} .=':'.$group_privs;
                   3210:     }
                   3211: }
1.567     raeburn  3212: 
                   3213: sub standard_roleprivs {
                   3214:     my ($allroles,$trole,$tdomain,$spec,$trest,$area) = @_;
                   3215:     if (defined($pr{$trole.':s'})) {
                   3216:         $$allroles{'cm./'}.=':'.$pr{$trole.':s'};
                   3217:         $$allroles{$spec.'./'}.=':'.$pr{$trole.':s'};
                   3218:     }
                   3219:     if ($tdomain ne '') {
                   3220:         if (defined($pr{$trole.':d'})) {
                   3221:             $$allroles{'cm./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3222:             $$allroles{$spec.'./'.$tdomain.'/'}.=':'.$pr{$trole.':d'};
                   3223:         }
                   3224:         if (($trest ne '') && (defined($pr{$trole.':c'}))) {
                   3225:             $$allroles{'cm.'.$area}.=':'.$pr{$trole.':c'};
                   3226:             $$allroles{$spec.'.'.$area}.=':'.$pr{$trole.':c'};
                   3227:         }
                   3228:     }
                   3229: }
                   3230: 
                   3231: sub set_userprivs {
1.678     raeburn  3232:     my ($userroles,$allroles,$allgroups) = @_; 
1.567     raeburn  3233:     my $author=0;
                   3234:     my $adv=0;
1.678     raeburn  3235:     my %grouproles = ();
                   3236:     if (keys(%{$allgroups}) > 0) {
                   3237:         foreach my $role (keys %{$allroles}) {
1.681     raeburn  3238:             my ($trole,$area,$sec,$extendedarea);
1.881     raeburn  3239:             if ($role =~ m-^(\w+|cr/$match_domain/$match_username/\w+)\.(/$match_domain/$match_courseid)(/?\w*)\.-) {
1.678     raeburn  3240:                 $trole = $1;
                   3241:                 $area = $2;
1.681     raeburn  3242:                 $sec = $3;
                   3243:                 $extendedarea = $area.$sec;
                   3244:                 if (exists($$allgroups{$area})) {
                   3245:                     foreach my $group (keys(%{$$allgroups{$area}})) {
                   3246:                         my $spec = $trole.'.'.$extendedarea;
                   3247:                         $grouproles{$spec.'.'.$area.'/'.$group} = 
                   3248:                                                 $$allgroups{$area}{$group};
1.678     raeburn  3249:                     }
                   3250:                 }
                   3251:             }
                   3252:         }
                   3253:     }
1.800     albertel 3254:     foreach my $group (keys(%grouproles)) {
                   3255:         $$allroles{$group} = $grouproles{$group};
1.678     raeburn  3256:     }
1.800     albertel 3257:     foreach my $role (keys(%{$allroles})) {
                   3258:         my %thesepriv;
                   3259:         if (($role=~/^au/) || ($role=~/^ca/)) { $author=1; }
                   3260:         foreach my $item (split(/:/,$$allroles{$role})) {
                   3261:             if ($item ne '') {
                   3262:                 my ($privilege,$restrictions)=split(/&/,$item);
1.567     raeburn  3263:                 if ($restrictions eq '') {
                   3264:                     $thesepriv{$privilege}='F';
                   3265:                 } elsif ($thesepriv{$privilege} ne 'F') {
                   3266:                     $thesepriv{$privilege}.=$restrictions;
                   3267:                 }
                   3268:                 if ($thesepriv{'adv'} eq 'F') { $adv=1; }
                   3269:             }
                   3270:         }
                   3271:         my $thesestr='';
1.800     albertel 3272:         foreach my $priv (keys(%thesepriv)) {
                   3273: 	    $thesestr.=':'.$priv.'&'.$thesepriv{$priv};
                   3274: 	}
                   3275:         $userroles->{'user.priv.'.$role} = $thesestr;
1.567     raeburn  3276:     }
                   3277:     return ($author,$adv);
                   3278: }
                   3279: 
1.12      www      3280: # --------------------------------------------------------------- get interface
                   3281: 
                   3282: sub get {
1.131     albertel 3283:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3284:    my $items='';
1.800     albertel 3285:    foreach my $item (@$storearr) {
                   3286:        $items.=&escape($item).'&';
1.191     harris41 3287:    }
1.12      www      3288:    $items=~s/\&$//;
1.620     albertel 3289:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3290:    if (!$uname) { $uname=$env{'user.name'}; }
1.131     albertel 3291:    my $uhome=&homeserver($uname,$udomain);
                   3292: 
1.133     albertel 3293:    my $rep=&reply("get:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3294:    my @pairs=split(/\&/,$rep);
1.273     albertel 3295:    if ( $#pairs==0 && $pairs[0] =~ /^(con_lost|error|no_such_host)/i) {
                   3296:      return @pairs;
                   3297:    }
1.15      www      3298:    my %returnhash=();
1.42      www      3299:    my $i=0;
1.800     albertel 3300:    foreach my $item (@$storearr) {
                   3301:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3302:       $i++;
1.191     harris41 3303:    }
1.15      www      3304:    return %returnhash;
1.27      www      3305: }
                   3306: 
                   3307: # --------------------------------------------------------------- del interface
                   3308: 
                   3309: sub del {
1.133     albertel 3310:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.27      www      3311:    my $items='';
1.800     albertel 3312:    foreach my $item (@$storearr) {
                   3313:        $items.=&escape($item).'&';
1.191     harris41 3314:    }
1.27      www      3315:    $items=~s/\&$//;
1.620     albertel 3316:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3317:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3318:    my $uhome=&homeserver($uname,$udomain);
                   3319: 
                   3320:    return &reply("del:$udomain:$uname:$namespace:$items",$uhome);
1.15      www      3321: }
                   3322: 
                   3323: # -------------------------------------------------------------- dump interface
                   3324: 
                   3325: sub dump {
1.755     albertel 3326:     my ($namespace,$udomain,$uname,$regexp,$range)=@_;
                   3327:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3328:     if (!$uname) { $uname=$env{'user.name'}; }
                   3329:     my $uhome=&homeserver($uname,$udomain);
                   3330:     if ($regexp) {
                   3331: 	$regexp=&escape($regexp);
                   3332:     } else {
                   3333: 	$regexp='.';
                   3334:     }
                   3335:     my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3336:     my @pairs=split(/\&/,$rep);
                   3337:     my %returnhash=();
                   3338:     foreach my $item (@pairs) {
                   3339: 	my ($key,$value)=split(/=/,$item,2);
                   3340: 	$key = &unescape($key);
                   3341: 	next if ($key =~ /^error: 2 /);
                   3342: 	$returnhash{$key}=&thaw_unescape($value);
                   3343:     }
                   3344:     return %returnhash;
1.407     www      3345: }
                   3346: 
1.717     albertel 3347: # --------------------------------------------------------- dumpstore interface
                   3348: 
                   3349: sub dumpstore {
                   3350:    my ($namespace,$udomain,$uname,$regexp,$range)=@_;
1.822     albertel 3351:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3352:    if (!$uname) { $uname=$env{'user.name'}; }
                   3353:    my $uhome=&homeserver($uname,$udomain);
                   3354:    if ($regexp) {
                   3355:        $regexp=&escape($regexp);
                   3356:    } else {
                   3357:        $regexp='.';
                   3358:    }
                   3359:    my $rep=&reply("dump:$udomain:$uname:$namespace:$regexp:$range",$uhome);
                   3360:    my @pairs=split(/\&/,$rep);
                   3361:    my %returnhash=();
                   3362:    foreach my $item (@pairs) {
                   3363:        my ($key,$value)=split(/=/,$item,2);
                   3364:        next if ($key =~ /^error: 2 /);
                   3365:        $returnhash{$key}=&thaw_unescape($value);
                   3366:    }
                   3367:    return %returnhash;
1.717     albertel 3368: }
                   3369: 
1.407     www      3370: # -------------------------------------------------------------- keys interface
                   3371: 
                   3372: sub getkeys {
                   3373:    my ($namespace,$udomain,$uname)=@_;
1.620     albertel 3374:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3375:    if (!$uname) { $uname=$env{'user.name'}; }
1.407     www      3376:    my $uhome=&homeserver($uname,$udomain);
                   3377:    my $rep=reply("keys:$udomain:$uname:$namespace",$uhome);
                   3378:    my @keyarray=();
1.800     albertel 3379:    foreach my $key (split(/\&/,$rep)) {
1.812     raeburn  3380:       next if ($key =~ /^error: 2 /);
1.800     albertel 3381:       push(@keyarray,&unescape($key));
1.407     www      3382:    }
                   3383:    return @keyarray;
1.318     matthew  3384: }
                   3385: 
1.319     matthew  3386: # --------------------------------------------------------------- currentdump
                   3387: sub currentdump {
1.328     matthew  3388:    my ($courseid,$sdom,$sname)=@_;
1.620     albertel 3389:    $courseid = $env{'request.course.id'} if (! defined($courseid));
                   3390:    $sdom     = $env{'user.domain'}       if (! defined($sdom));
                   3391:    $sname    = $env{'user.name'}         if (! defined($sname));
1.326     matthew  3392:    my $uhome = &homeserver($sname,$sdom);
                   3393:    my $rep=reply('currentdump:'.$sdom.':'.$sname.':'.$courseid,$uhome);
1.318     matthew  3394:    return if ($rep =~ /^(error:|no_such_host)/);
1.319     matthew  3395:    #
1.318     matthew  3396:    my %returnhash=();
1.319     matthew  3397:    #
                   3398:    if ($rep eq "unknown_cmd") { 
                   3399:        # an old lond will not know currentdump
                   3400:        # Do a dump and make it look like a currentdump
1.822     albertel 3401:        my @tmp = &dumpstore($courseid,$sdom,$sname,'.');
1.319     matthew  3402:        return if ($tmp[0] =~ /^(error:|no_such_host)/);
                   3403:        my %hash = @tmp;
                   3404:        @tmp=();
1.424     matthew  3405:        %returnhash = %{&convert_dump_to_currentdump(\%hash)};
1.319     matthew  3406:    } else {
                   3407:        my @pairs=split(/\&/,$rep);
1.800     albertel 3408:        foreach my $pair (@pairs) {
                   3409:            my ($key,$value)=split(/=/,$pair,2);
1.319     matthew  3410:            my ($symb,$param) = split(/:/,$key);
                   3411:            $returnhash{&unescape($symb)}->{&unescape($param)} = 
1.557     albertel 3412:                                                         &thaw_unescape($value);
1.319     matthew  3413:        }
1.191     harris41 3414:    }
1.12      www      3415:    return %returnhash;
1.424     matthew  3416: }
                   3417: 
                   3418: sub convert_dump_to_currentdump{
                   3419:     my %hash = %{shift()};
                   3420:     my %returnhash;
                   3421:     # Code ripped from lond, essentially.  The only difference
                   3422:     # here is the unescaping done by lonnet::dump().  Conceivably
                   3423:     # we might run in to problems with parameter names =~ /^v\./
                   3424:     while (my ($key,$value) = each(%hash)) {
                   3425:         my ($v,$symb,$param) = split(/:/,$key);
1.822     albertel 3426: 	$symb  = &unescape($symb);
                   3427: 	$param = &unescape($param);
1.424     matthew  3428:         next if ($v eq 'version' || $symb eq 'keys');
                   3429:         next if (exists($returnhash{$symb}) &&
                   3430:                  exists($returnhash{$symb}->{$param}) &&
                   3431:                  $returnhash{$symb}->{'v.'.$param} > $v);
                   3432:         $returnhash{$symb}->{$param}=$value;
                   3433:         $returnhash{$symb}->{'v.'.$param}=$v;
                   3434:     }
                   3435:     #
                   3436:     # Remove all of the keys in the hashes which keep track of
                   3437:     # the version of the parameter.
                   3438:     while (my ($symb,$param_hash) = each(%returnhash)) {
                   3439:         # use a foreach because we are going to delete from the hash.
                   3440:         foreach my $key (keys(%$param_hash)) {
                   3441:             delete($param_hash->{$key}) if ($key =~ /^v\./);
                   3442:         }
                   3443:     }
                   3444:     return \%returnhash;
1.12      www      3445: }
                   3446: 
1.627     albertel 3447: # ------------------------------------------------------ critical inc interface
                   3448: 
                   3449: sub cinc {
                   3450:     return &inc(@_,'critical');
                   3451: }
                   3452: 
1.449     matthew  3453: # --------------------------------------------------------------- inc interface
                   3454: 
                   3455: sub inc {
1.627     albertel 3456:     my ($namespace,$store,$udomain,$uname,$critical) = @_;
1.620     albertel 3457:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3458:     if (!$uname) { $uname=$env{'user.name'}; }
1.449     matthew  3459:     my $uhome=&homeserver($uname,$udomain);
                   3460:     my $items='';
                   3461:     if (! ref($store)) {
                   3462:         # got a single value, so use that instead
                   3463:         $items = &escape($store).'=&';
                   3464:     } elsif (ref($store) eq 'SCALAR') {
                   3465:         $items = &escape($$store).'=&';        
                   3466:     } elsif (ref($store) eq 'ARRAY') {
                   3467:         $items = join('=&',map {&escape($_);} @{$store});
                   3468:     } elsif (ref($store) eq 'HASH') {
                   3469:         while (my($key,$value) = each(%{$store})) {
                   3470:             $items.= &escape($key).'='.&escape($value).'&';
                   3471:         }
                   3472:     }
                   3473:     $items=~s/\&$//;
1.627     albertel 3474:     if ($critical) {
                   3475: 	return &critical("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3476:     } else {
                   3477: 	return &reply("inc:$udomain:$uname:$namespace:$items",$uhome);
                   3478:     }
1.449     matthew  3479: }
                   3480: 
1.12      www      3481: # --------------------------------------------------------------- put interface
                   3482: 
                   3483: sub put {
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.12      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.12      www      3492:    $items=~s/\&$//;
1.134     albertel 3493:    return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.47      www      3494: }
                   3495: 
1.631     albertel 3496: # ------------------------------------------------------------ newput interface
                   3497: 
                   3498: sub newput {
                   3499:    my ($namespace,$storehash,$udomain,$uname)=@_;
                   3500:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3501:    if (!$uname) { $uname=$env{'user.name'}; }
                   3502:    my $uhome=&homeserver($uname,$udomain);
                   3503:    my $items='';
                   3504:    foreach my $key (keys(%$storehash)) {
                   3505:        $items.=&escape($key).'='.&freeze_escape($$storehash{$key}).'&';
                   3506:    }
                   3507:    $items=~s/\&$//;
                   3508:    return &reply("newput:$udomain:$uname:$namespace:$items",$uhome);
                   3509: }
                   3510: 
                   3511: # ---------------------------------------------------------  putstore interface
                   3512: 
1.524     raeburn  3513: sub putstore {
1.715     albertel 3514:    my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
1.620     albertel 3515:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3516:    if (!$uname) { $uname=$env{'user.name'}; }
1.524     raeburn  3517:    my $uhome=&homeserver($uname,$udomain);
                   3518:    my $items='';
1.715     albertel 3519:    foreach my $key (keys(%$storehash)) {
                   3520:        $items.= &escape($key).'='.&freeze_escape($storehash->{$key}).'&';
1.524     raeburn  3521:    }
1.715     albertel 3522:    $items=~s/\&$//;
1.716     albertel 3523:    my $esc_symb=&escape($symb);
                   3524:    my $esc_v=&escape($version);
1.715     albertel 3525:    my $reply =
1.716     albertel 3526:        &reply("putstore:$udomain:$uname:$namespace:$esc_symb:$esc_v:$items",
1.715     albertel 3527: 	      $uhome);
                   3528:    if ($reply eq 'unknown_cmd') {
1.716     albertel 3529:        # gfall back to way things use to be done
1.715     albertel 3530:        return &old_putstore($namespace,$symb,$version,$storehash,$udomain,
                   3531: 			    $uname);
1.524     raeburn  3532:    }
1.715     albertel 3533:    return $reply;
                   3534: }
                   3535: 
                   3536: sub old_putstore {
1.716     albertel 3537:     my ($namespace,$symb,$version,$storehash,$udomain,$uname)=@_;
                   3538:     if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3539:     if (!$uname) { $uname=$env{'user.name'}; }
                   3540:     my $uhome=&homeserver($uname,$udomain);
                   3541:     my %newstorehash;
1.800     albertel 3542:     foreach my $item (keys(%$storehash)) {
                   3543: 	my $key = $version.':'.&escape($symb).':'.$item;
                   3544: 	$newstorehash{$key} = $storehash->{$item};
1.716     albertel 3545:     }
                   3546:     my $items='';
                   3547:     my %allitems = ();
1.800     albertel 3548:     foreach my $item (keys(%newstorehash)) {
                   3549: 	if ($item =~ m/^([^\:]+):([^\:]+):([^\:]+)$/) {
1.716     albertel 3550: 	    my $key = $1.':keys:'.$2;
                   3551: 	    $allitems{$key} .= $3.':';
                   3552: 	}
1.800     albertel 3553: 	$items.=$item.'='.&freeze_escape($newstorehash{$item}).'&';
1.716     albertel 3554:     }
1.800     albertel 3555:     foreach my $item (keys(%allitems)) {
                   3556: 	$allitems{$item} =~ s/\:$//;
                   3557: 	$items.= $item.'='.$allitems{$item}.'&';
1.716     albertel 3558:     }
                   3559:     $items=~s/\&$//;
                   3560:     return &reply("put:$udomain:$uname:$namespace:$items",$uhome);
1.524     raeburn  3561: }
                   3562: 
1.47      www      3563: # ------------------------------------------------------ critical put interface
                   3564: 
                   3565: sub cput {
1.134     albertel 3566:    my ($namespace,$storehash,$udomain,$uname)=@_;
1.620     albertel 3567:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3568:    if (!$uname) { $uname=$env{'user.name'}; }
1.134     albertel 3569:    my $uhome=&homeserver($uname,$udomain);
1.47      www      3570:    my $items='';
1.800     albertel 3571:    foreach my $item (keys(%$storehash)) {
                   3572:        $items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.191     harris41 3573:    }
1.47      www      3574:    $items=~s/\&$//;
1.134     albertel 3575:    return &critical("put:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3576: }
                   3577: 
                   3578: # -------------------------------------------------------------- eget interface
                   3579: 
                   3580: sub eget {
1.133     albertel 3581:    my ($namespace,$storearr,$udomain,$uname)=@_;
1.12      www      3582:    my $items='';
1.800     albertel 3583:    foreach my $item (@$storearr) {
                   3584:        $items.=&escape($item).'&';
1.191     harris41 3585:    }
1.12      www      3586:    $items=~s/\&$//;
1.620     albertel 3587:    if (!$udomain) { $udomain=$env{'user.domain'}; }
                   3588:    if (!$uname) { $uname=$env{'user.name'}; }
1.133     albertel 3589:    my $uhome=&homeserver($uname,$udomain);
                   3590:    my $rep=&reply("eget:$udomain:$uname:$namespace:$items",$uhome);
1.12      www      3591:    my @pairs=split(/\&/,$rep);
                   3592:    my %returnhash=();
1.42      www      3593:    my $i=0;
1.800     albertel 3594:    foreach my $item (@$storearr) {
                   3595:       $returnhash{$item}=&thaw_unescape($pairs[$i]);
1.42      www      3596:       $i++;
1.191     harris41 3597:    }
1.12      www      3598:    return %returnhash;
                   3599: }
                   3600: 
1.667     albertel 3601: # ------------------------------------------------------------ tmpput interface
                   3602: sub tmpput {
1.802     raeburn  3603:     my ($storehash,$server,$context)=@_;
1.667     albertel 3604:     my $items='';
1.800     albertel 3605:     foreach my $item (keys(%$storehash)) {
                   3606: 	$items.=&escape($item).'='.&freeze_escape($$storehash{$item}).'&';
1.667     albertel 3607:     }
                   3608:     $items=~s/\&$//;
1.802     raeburn  3609:     if (defined($context)) {
                   3610:         $items .= ':'.&escape($context);
                   3611:     }
1.667     albertel 3612:     return &reply("tmpput:$items",$server);
                   3613: }
                   3614: 
                   3615: # ------------------------------------------------------------ tmpget interface
                   3616: sub tmpget {
1.688     albertel 3617:     my ($token,$server)=@_;
                   3618:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3619:     my $rep=&reply("tmpget:$token",$server);
1.667     albertel 3620:     my %returnhash;
                   3621:     foreach my $item (split(/\&/,$rep)) {
                   3622: 	my ($key,$value)=split(/=/,$item);
                   3623: 	$returnhash{&unescape($key)}=&thaw_unescape($value);
                   3624:     }
                   3625:     return %returnhash;
                   3626: }
                   3627: 
1.688     albertel 3628: # ------------------------------------------------------------ tmpget interface
                   3629: sub tmpdel {
                   3630:     my ($token,$server)=@_;
                   3631:     if (!defined($server)) { $server = $perlvar{'lonHostID'}; }
                   3632:     return &reply("tmpdel:$token",$server);
                   3633: }
                   3634: 
1.765     albertel 3635: # -------------------------------------------------- portfolio access checking
                   3636: 
                   3637: sub portfolio_access {
1.766     albertel 3638:     my ($requrl) = @_;
1.765     albertel 3639:     my (undef,$udom,$unum,$file_name,$group) = &parse_portfolio_url($requrl);
                   3640:     my $result = &get_portfolio_access($udom,$unum,$file_name,$group);
1.814     raeburn  3641:     if ($result) {
                   3642:         my %setters;
                   3643:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3644:             my ($startblock,$endblock) =
                   3645:                 &Apache::loncommon::blockcheck(\%setters,'port',$unum,$udom);
                   3646:             if ($startblock && $endblock) {
                   3647:                 return 'B';
                   3648:             }
                   3649:         } else {
                   3650:             my ($startblock,$endblock) =
                   3651:                 &Apache::loncommon::blockcheck(\%setters,'port');
                   3652:             if ($startblock && $endblock) {
                   3653:                 return 'B';
                   3654:             }
                   3655:         }
                   3656:     }
1.765     albertel 3657:     if ($result eq 'ok') {
1.766     albertel 3658:        return 'F';
1.765     albertel 3659:     } elsif ($result =~ /^[^:]+:guest_/) {
1.766     albertel 3660:        return 'A';
1.765     albertel 3661:     }
1.766     albertel 3662:     return '';
1.765     albertel 3663: }
                   3664: 
                   3665: sub get_portfolio_access {
1.767     albertel 3666:     my ($udom,$unum,$file_name,$group,$access_hash) = @_;
                   3667: 
                   3668:     if (!ref($access_hash)) {
                   3669: 	my $current_perms = &get_portfile_permissions($udom,$unum);
                   3670: 	my %access_controls = &get_access_controls($current_perms,$group,
                   3671: 						   $file_name);
                   3672: 	$access_hash = $access_controls{$file_name};
                   3673:     }
                   3674: 
1.765     albertel 3675:     my ($public,$guest,@domains,@users,@courses,@groups);
                   3676:     my $now = time;
                   3677:     if (ref($access_hash) eq 'HASH') {
                   3678:         foreach my $key (keys(%{$access_hash})) {
                   3679:             my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   3680:             if ($start > $now) {
                   3681:                 next;
                   3682:             }
                   3683:             if ($end && $end<$now) {
                   3684:                 next;
                   3685:             }
                   3686:             if ($scope eq 'public') {
                   3687:                 $public = $key;
                   3688:                 last;
                   3689:             } elsif ($scope eq 'guest') {
                   3690:                 $guest = $key;
                   3691:             } elsif ($scope eq 'domains') {
                   3692:                 push(@domains,$key);
                   3693:             } elsif ($scope eq 'users') {
                   3694:                 push(@users,$key);
                   3695:             } elsif ($scope eq 'course') {
                   3696:                 push(@courses,$key);
                   3697:             } elsif ($scope eq 'group') {
                   3698:                 push(@groups,$key);
                   3699:             }
                   3700:         }
                   3701:         if ($public) {
                   3702:             return 'ok';
                   3703:         }
                   3704:         if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
                   3705:             if ($guest) {
                   3706:                 return $guest;
                   3707:             }
                   3708:         } else {
                   3709:             if (@domains > 0) {
                   3710:                 foreach my $domkey (@domains) {
                   3711:                     if (ref($access_hash->{$domkey}{'dom'}) eq 'ARRAY') {
                   3712:                         if (grep(/^\Q$env{'user.domain'}\E$/,@{$access_hash->{$domkey}{'dom'}})) {
                   3713:                             return 'ok';
                   3714:                         }
                   3715:                     }
                   3716:                 }
                   3717:             }
                   3718:             if (@users > 0) {
                   3719:                 foreach my $userkey (@users) {
1.865     raeburn  3720:                     if (ref($access_hash->{$userkey}{'users'}) eq 'ARRAY') {
                   3721:                         foreach my $item (@{$access_hash->{$userkey}{'users'}}) {
                   3722:                             if (ref($item) eq 'HASH') {
                   3723:                                 if (($item->{'uname'} eq $env{'user.name'}) &&
                   3724:                                     ($item->{'udom'} eq $env{'user.domain'})) {
                   3725:                                     return 'ok';
                   3726:                                 }
                   3727:                             }
                   3728:                         }
                   3729:                     } 
1.765     albertel 3730:                 }
                   3731:             }
                   3732:             my %roleshash;
                   3733:             my @courses_and_groups = @courses;
                   3734:             push(@courses_and_groups,@groups); 
                   3735:             if (@courses_and_groups > 0) {
                   3736:                 my (%allgroups,%allroles); 
                   3737:                 my ($start,$end,$role,$sec,$group);
                   3738:                 foreach my $envkey (%env) {
1.811     albertel 3739:                     if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3740:                         my $cid = $2.'_'.$3; 
                   3741:                         if ($1 eq 'gr') {
                   3742:                             $group = $4;
                   3743:                             $allgroups{$cid}{$group} = $env{$envkey};
                   3744:                         } else {
                   3745:                             if ($4 eq '') {
                   3746:                                 $sec = 'none';
                   3747:                             } else {
                   3748:                                 $sec = $4;
                   3749:                             }
                   3750:                             $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3751:                         }
1.811     albertel 3752:                     } elsif ($envkey =~ m-^user\.role\./cr/($match_domain/$match_username/\w*)./($match_domain)/($match_courseid)/?([^/]*)$-) {
1.765     albertel 3753:                         my $cid = $2.'_'.$3;
                   3754:                         if ($4 eq '') {
                   3755:                             $sec = 'none';
                   3756:                         } else {
                   3757:                             $sec = $4;
                   3758:                         }
                   3759:                         $allroles{$cid}{$1}{$sec} = $env{$envkey};
                   3760:                     }
                   3761:                 }
                   3762:                 if (keys(%allroles) == 0) {
                   3763:                     return;
                   3764:                 }
                   3765:                 foreach my $key (@courses_and_groups) {
                   3766:                     my %content = %{$$access_hash{$key}};
                   3767:                     my $cnum = $content{'number'};
                   3768:                     my $cdom = $content{'domain'};
                   3769:                     my $cid = $cdom.'_'.$cnum;
                   3770:                     if (!exists($allroles{$cid})) {
                   3771:                         next;
                   3772:                     }    
                   3773:                     foreach my $role_id (keys(%{$content{'roles'}})) {
                   3774:                         my @sections = @{$content{'roles'}{$role_id}{'section'}};
                   3775:                         my @groups = @{$content{'roles'}{$role_id}{'group'}};
                   3776:                         my @status = @{$content{'roles'}{$role_id}{'access'}};
                   3777:                         my @roles = @{$content{'roles'}{$role_id}{'role'}};
                   3778:                         foreach my $role (keys(%{$allroles{$cid}})) {
                   3779:                             if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
                   3780:                                 foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
                   3781:                                     if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
                   3782:                                         if (grep/^all$/,@sections) {
                   3783:                                             return 'ok';
                   3784:                                         } else {
                   3785:                                             if (grep/^$sec$/,@sections) {
                   3786:                                                 return 'ok';
                   3787:                                             }
                   3788:                                         }
                   3789:                                     }
                   3790:                                 }
                   3791:                                 if (keys(%{$allgroups{$cid}}) == 0) {
                   3792:                                     if (grep/^none$/,@groups) {
                   3793:                                         return 'ok';
                   3794:                                     }
                   3795:                                 } else {
                   3796:                                     if (grep/^all$/,@groups) {
                   3797:                                         return 'ok';
                   3798:                                     } 
                   3799:                                     foreach my $group (keys(%{$allgroups{$cid}})) {
                   3800:                                         if (grep/^$group$/,@groups) {
                   3801:                                             return 'ok';
                   3802:                                         }
                   3803:                                     }
                   3804:                                 } 
                   3805:                             }
                   3806:                         }
                   3807:                     }
                   3808:                 }
                   3809:             }
                   3810:             if ($guest) {
                   3811:                 return $guest;
                   3812:             }
                   3813:         }
                   3814:     }
                   3815:     return;
                   3816: }
                   3817: 
                   3818: sub course_group_datechecker {
                   3819:     my ($dates,$now,$status) = @_;
                   3820:     my ($start,$end) = split(/\./,$dates);
                   3821:     if (!$start && !$end) {
                   3822:         return 'ok';
                   3823:     }
                   3824:     if (grep/^active$/,@{$status}) {
                   3825:         if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
                   3826:             return 'ok';
                   3827:         }
                   3828:     }
                   3829:     if (grep/^previous$/,@{$status}) {
                   3830:         if ($end > $now ) {
                   3831:             return 'ok';
                   3832:         }
                   3833:     }
                   3834:     if (grep/^future$/,@{$status}) {
                   3835:         if ($start > $now) {
                   3836:             return 'ok';
                   3837:         }
                   3838:     }
                   3839:     return; 
                   3840: }
                   3841: 
                   3842: sub parse_portfolio_url {
                   3843:     my ($url) = @_;
                   3844: 
                   3845:     my ($type,$udom,$unum,$group,$file_name);
                   3846:     
1.823     albertel 3847:     if ($url =~  m-^/*(?:uploaded|editupload)/($match_domain)/($match_username)/portfolio(/.+)$-) {
1.765     albertel 3848: 	$type = 1;
                   3849:         $udom = $1;
                   3850:         $unum = $2;
                   3851:         $file_name = $3;
1.823     albertel 3852:     } elsif ($url =~ m-^/*(?:uploaded|editupload)/($match_domain)/($match_courseid)/groups/([^/]+)/portfolio/(.+)$-) {
1.765     albertel 3853: 	$type = 2;
                   3854:         $udom = $1;
                   3855:         $unum = $2;
                   3856:         $group = $3;
                   3857:         $file_name = $3.'/'.$4;
                   3858:     }
                   3859:     if (wantarray) {
                   3860: 	return ($type,$udom,$unum,$file_name,$group);
                   3861:     }
                   3862:     return $type;
                   3863: }
                   3864: 
                   3865: sub is_portfolio_url {
                   3866:     my ($url) = @_;
                   3867:     return scalar(&parse_portfolio_url($url));
                   3868: }
                   3869: 
1.798     raeburn  3870: sub is_portfolio_file {
                   3871:     my ($file) = @_;
1.820     raeburn  3872:     if (($file =~ /^portfolio/) || ($file =~ /^groups\/\w+\/portfolio/)) {
1.798     raeburn  3873:         return 1;
                   3874:     }
                   3875:     return;
                   3876: }
                   3877: 
                   3878: 
1.341     www      3879: # ---------------------------------------------- Custom access rule evaluation
                   3880: 
                   3881: sub customaccess {
                   3882:     my ($priv,$uri)=@_;
1.807     albertel 3883:     my ($urole,$urealm)=split(/\./,$env{'request.role'},2);
1.819     www      3884:     my (undef,$udom,$ucrs,$usec)=split(/\//,$urealm);
1.807     albertel 3885:     $udom = &LONCAPA::clean_domain($udom);
                   3886:     $ucrs = &LONCAPA::clean_username($ucrs);
1.341     www      3887:     my $access=0;
1.800     albertel 3888:     foreach my $right (split(/\s*\,\s*/,&metadata($uri,'rule_rights'))) {
1.893     albertel 3889: 	my ($effect,$realm,$role,$type)=split(/\:/,$right);
                   3890: 	if ($type eq 'user') {
                   3891: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
1.896     albertel 3892: 		my ($tdom,$tuname)=split(m{/},$scope);
1.893     albertel 3893: 		if ($tdom) {
                   3894: 		    if ($tdom ne $env{'user.domain'}) { next; }
                   3895: 		}
1.896     albertel 3896: 		if ($tuname) {
                   3897: 		    if ($tuname ne $env{'user.name'}) { next; }
1.893     albertel 3898: 		}
                   3899: 		$access=($effect eq 'allow');
                   3900: 		last;
                   3901: 	    }
                   3902: 	} else {
                   3903: 	    if ($role) {
                   3904: 		if ($role ne $urole) { next; }
                   3905: 	    }
                   3906: 	    foreach my $scope (split(/\s*\,\s*/,$realm)) {
                   3907: 		my ($tdom,$tcrs,$tsec)=split(/\_/,$scope);
                   3908: 		if ($tdom) {
                   3909: 		    if ($tdom ne $udom) { next; }
                   3910: 		}
                   3911: 		if ($tcrs) {
                   3912: 		    if ($tcrs ne $ucrs) { next; }
                   3913: 		}
                   3914: 		if ($tsec) {
                   3915: 		    if ($tsec ne $usec) { next; }
                   3916: 		}
                   3917: 		$access=($effect eq 'allow');
                   3918: 		last;
                   3919: 	    }
                   3920: 	    if ($realm eq '' && $role eq '') {
                   3921: 		$access=($effect eq 'allow');
                   3922: 	    }
1.402     bowersj2 3923: 	}
1.341     www      3924:     }
                   3925:     return $access;
                   3926: }
                   3927: 
1.103     harris41 3928: # ------------------------------------------------- Check for a user privilege
1.12      www      3929: 
                   3930: sub allowed {
1.810     raeburn  3931:     my ($priv,$uri,$symb,$role)=@_;
1.705     albertel 3932:     my $ver_orguri=$uri;
1.439     www      3933:     $uri=&deversion($uri);
1.152     www      3934:     my $orguri=$uri;
1.52      www      3935:     $uri=&declutter($uri);
1.809     raeburn  3936: 
1.810     raeburn  3937:     if ($priv eq 'evb') {
                   3938: # Evade communication block restrictions for specified role in a course
                   3939:         if ($env{'user.priv.'.$role} =~/evb\&([^\:]*)/) {
                   3940:             return $1;
                   3941:         } else {
                   3942:             return;
                   3943:         }
                   3944:     }
                   3945: 
1.620     albertel 3946:     if (defined($env{'allowed.'.$priv})) { return $env{'allowed.'.$priv}; }
1.54      www      3947: # Free bre access to adm and meta resources
1.775     albertel 3948:     if (((($uri=~/^adm\//) && ($uri !~ m{/(?:smppg|bulletinboard)$})) 
1.769     albertel 3949: 	 || (($uri=~/\.meta$/) && ($uri!~m|^uploaded/|) )) 
                   3950: 	&& ($priv eq 'bre')) {
1.14      www      3951: 	return 'F';
1.159     www      3952:     }
                   3953: 
1.545     banghart 3954: # Free bre access to user's own portfolio contents
1.714     raeburn  3955:     my ($space,$domain,$name,@dir)=split('/',$uri);
1.647     raeburn  3956:     if (($space=~/^(uploaded|editupload)$/) && ($env{'user.name'} eq $name) && 
1.714     raeburn  3957: 	($env{'user.domain'} eq $domain) && ('portfolio' eq $dir[0])) {
1.814     raeburn  3958:         my %setters;
                   3959:         my ($startblock,$endblock) = 
                   3960:             &Apache::loncommon::blockcheck(\%setters,'port');
                   3961:         if ($startblock && $endblock) {
                   3962:             return 'B';
                   3963:         } else {
                   3964:             return 'F';
                   3965:         }
1.545     banghart 3966:     }
                   3967: 
1.762     raeburn  3968: # bre access to group portfolio for rgf priv in group, or mdg or vcg in course.
1.714     raeburn  3969:     if (($space=~/^(uploaded|editupload)$/) && ($dir[0] eq 'groups') 
                   3970:          && ($dir[2] eq 'portfolio') && ($priv eq 'bre')) {
                   3971:         if (exists($env{'request.course.id'})) {
                   3972:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   3973:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
                   3974:             if (($domain eq $cdom) && ($name eq $cnum)) {
                   3975:                 my $courseprivid=$env{'request.course.id'};
                   3976:                 $courseprivid=~s/\_/\//;
                   3977:                 if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid
                   3978:                     .'/'.$dir[1]} =~/rgf\&([^\:]*)/) {
                   3979:                     return $1; 
1.762     raeburn  3980:                 } else {
                   3981:                     if ($env{'request.course.sec'}) {
                   3982:                         $courseprivid.='/'.$env{'request.course.sec'};
                   3983:                     }
                   3984:                     if ($env{'user.priv.'.$env{'request.role'}.'./'.
                   3985:                         $courseprivid} =~/(mdg|vcg)\&([^\:]*)/) {
                   3986:                         return $2;
                   3987:                     }
1.714     raeburn  3988:                 }
                   3989:             }
                   3990:         }
                   3991:     }
                   3992: 
1.159     www      3993: # Free bre to public access
                   3994: 
                   3995:     if ($priv eq 'bre') {
1.238     www      3996:         my $copyright=&metadata($uri,'copyright');
1.620     albertel 3997: 	if (($copyright eq 'public') && (!$env{'request.course.id'})) { 
1.301     www      3998:            return 'F'; 
                   3999:         }
1.238     www      4000:         if ($copyright eq 'priv') {
                   4001:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4002: 	    unless (($env{'user.name'} eq $2) && ($env{'user.domain'} eq $1)) {
1.238     www      4003: 		return '';
                   4004:             }
                   4005:         }
                   4006:         if ($copyright eq 'domain') {
                   4007:             $uri=~/([^\/]+)\/([^\/]+)\//;
1.620     albertel 4008: 	    unless (($env{'user.domain'} eq $1) ||
                   4009:                  ($env{'course.'.$env{'request.course.id'}.'.domain'} eq $1)) {
1.238     www      4010: 		return '';
                   4011:             }
1.262     matthew  4012:         }
1.620     albertel 4013:         if ($env{'request.role'}=~ /li\.\//) {
1.262     matthew  4014:             # Library role, so allow browsing of resources in this domain.
                   4015:             return 'F';
1.238     www      4016:         }
1.341     www      4017:         if ($copyright eq 'custom') {
                   4018: 	    unless (&customaccess($priv,$uri)) { return ''; }
                   4019:         }
1.14      www      4020:     }
1.264     matthew  4021:     # Domain coordinator is trying to create a course
1.620     albertel 4022:     if (($priv eq 'ccc') && ($env{'request.role'} =~ /^dc\./)) {
1.264     matthew  4023:         # uri is the requested domain in this case.
                   4024:         # comparison to 'request.role.domain' shows if the user has selected
1.678     raeburn  4025:         # a role of dc for the domain in question.
1.620     albertel 4026:         return 'F' if ($uri eq $env{'request.role.domain'});
1.264     matthew  4027:     }
1.29      www      4028: 
1.52      www      4029:     my $thisallowed='';
                   4030:     my $statecond=0;
                   4031:     my $courseprivid='';
                   4032: 
                   4033: # Course
                   4034: 
1.620     albertel 4035:     if ($env{'user.priv.'.$env{'request.role'}.'./'}=~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4036:        $thisallowed.=$1;
                   4037:     }
1.29      www      4038: 
1.52      www      4039: # Domain
                   4040: 
1.620     albertel 4041:     if ($env{'user.priv.'.$env{'request.role'}.'./'.(split(/\//,$uri))[0].'/'}
1.479     albertel 4042:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4043:        $thisallowed.=$1;
                   4044:     }
1.52      www      4045: 
                   4046: # Course: uri itself is a course
1.66      www      4047:     my $courseuri=$uri;
                   4048:     $courseuri=~s/\_(\d)/\/$1/;
1.83      www      4049:     $courseuri=~s/^([^\/])/\/$1/;
1.81      www      4050: 
1.620     albertel 4051:     if ($env{'user.priv.'.$env{'request.role'}.'.'.$courseuri}
1.479     albertel 4052:        =~/\Q$priv\E\&([^\:]*)/) {
1.12      www      4053:        $thisallowed.=$1;
                   4054:     }
1.29      www      4055: 
1.665     albertel 4056: # URI is an uploaded document for this course, default permissions don't matter
1.611     albertel 4057: # not allowing 'edit' access (editupload) to uploaded course docs
1.492     albertel 4058:     if (($priv eq 'bre') && ($uri=~m|^uploaded/|)) {
1.665     albertel 4059: 	$thisallowed='';
1.671     raeburn  4060:         my ($match)=&is_on_map($uri);
                   4061:         if ($match) {
                   4062:             if ($env{'user.priv.'.$env{'request.role'}.'./'}
                   4063:                   =~/\Q$priv\E\&([^\:]*)/) {
                   4064:                 $thisallowed.=$1;
                   4065:             }
                   4066:         } else {
1.705     albertel 4067:             my $refuri = $env{'httpref.'.$orguri} || $env{'httpref.'.$ver_orguri};
1.671     raeburn  4068:             if ($refuri) {
                   4069:                 if ($refuri =~ m|^/adm/|) {
1.669     raeburn  4070:                     $thisallowed='F';
1.671     raeburn  4071:                 } else {
                   4072:                     $refuri=&declutter($refuri);
                   4073:                     my ($match) = &is_on_map($refuri);
                   4074:                     if ($match) {
                   4075:                         $thisallowed='F';
                   4076:                     }
1.669     raeburn  4077:                 }
1.671     raeburn  4078:             }
                   4079:         }
1.314     www      4080:     }
1.492     albertel 4081: 
1.766     albertel 4082:     if ($priv eq 'bre'
                   4083: 	&& $thisallowed ne 'F' 
                   4084: 	&& $thisallowed ne '2'
                   4085: 	&& &is_portfolio_url($uri)) {
                   4086: 	$thisallowed = &portfolio_access($uri);
                   4087:     }
                   4088:     
1.52      www      4089: # Full access at system, domain or course-wide level? Exit.
1.29      www      4090: 
                   4091:     if ($thisallowed=~/F/) {
                   4092: 	return 'F';
                   4093:     }
                   4094: 
1.52      www      4095: # If this is generating or modifying users, exit with special codes
1.29      www      4096: 
1.643     www      4097:     if (':csu:cdc:ccc:cin:cta:cep:ccr:cst:cad:cli:cau:cdg:cca:caa:'=~/\:\Q$priv\E\:/) {
                   4098: 	if (($priv eq 'cca') || ($priv eq 'caa')) {
1.642     albertel 4099: 	    my ($audom,$auname)=split('/',$uri);
1.643     www      4100: # no author name given, so this just checks on the general right to make a co-author in this domain
                   4101: 	    unless ($auname) { return $thisallowed; }
                   4102: # an author name is given, so we are about to actually make a co-author for a certain account
1.642     albertel 4103: 	    if (($auname ne $env{'user.name'} && $env{'request.role'} !~ /^dc\./) ||
                   4104: 		(($audom ne $env{'user.domain'} && $env{'request.role'} !~ /^dc\./) &&
                   4105: 		 ($audom ne $env{'request.role.domain'}))) { return ''; }
                   4106: 	}
1.52      www      4107: 	return $thisallowed;
                   4108:     }
                   4109: #
1.103     harris41 4110: # Gathered so far: system, domain and course wide privileges
1.52      www      4111: #
                   4112: # Course: See if uri or referer is an individual resource that is part of 
                   4113: # the course
                   4114: 
1.620     albertel 4115:     if ($env{'request.course.id'}) {
1.232     www      4116: 
1.620     albertel 4117:        $courseprivid=$env{'request.course.id'};
                   4118:        if ($env{'request.course.sec'}) {
                   4119:           $courseprivid.='/'.$env{'request.course.sec'};
1.52      www      4120:        }
                   4121:        $courseprivid=~s/\_/\//;
                   4122:        my $checkreferer=1;
1.232     www      4123:        my ($match,$cond)=&is_on_map($uri);
                   4124:        if ($match) {
                   4125:            $statecond=$cond;
1.620     albertel 4126:            if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4127:                =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4128:                $thisallowed.=$1;
                   4129:                $checkreferer=0;
                   4130:            }
1.29      www      4131:        }
1.83      www      4132:        
1.148     www      4133:        if ($checkreferer) {
1.620     albertel 4134: 	  my $refuri=$env{'httpref.'.$orguri};
1.148     www      4135:             unless ($refuri) {
1.800     albertel 4136:                 foreach my $key (keys(%env)) {
                   4137: 		    if ($key=~/^httpref\..*\*/) {
                   4138: 			my $pattern=$key;
1.156     www      4139:                         $pattern=~s/^httpref\.\/res\///;
1.148     www      4140:                         $pattern=~s/\*/\[\^\/\]\+/g;
                   4141:                         $pattern=~s/\//\\\//g;
1.152     www      4142:                         if ($orguri=~/$pattern/) {
1.800     albertel 4143: 			    $refuri=$env{$key};
1.148     www      4144:                         }
                   4145:                     }
1.191     harris41 4146:                 }
1.148     www      4147:             }
1.232     www      4148: 
1.148     www      4149:          if ($refuri) { 
1.152     www      4150: 	  $refuri=&declutter($refuri);
1.232     www      4151:           my ($match,$cond)=&is_on_map($refuri);
                   4152:             if ($match) {
                   4153:               my $refstatecond=$cond;
1.620     albertel 4154:               if ($env{'user.priv.'.$env{'request.role'}.'./'.$courseprivid}
1.479     albertel 4155:                   =~/\Q$priv\E\&([^\:]*)/) {
1.52      www      4156:                   $thisallowed.=$1;
1.53      www      4157:                   $uri=$refuri;
                   4158:                   $statecond=$refstatecond;
1.52      www      4159:               }
                   4160:           }
1.148     www      4161:         }
1.29      www      4162:        }
1.52      www      4163:    }
1.29      www      4164: 
1.52      www      4165: #
1.103     harris41 4166: # Gathered now: all privileges that could apply, and condition number
1.52      www      4167: # 
                   4168: #
                   4169: # Full or no access?
                   4170: #
1.29      www      4171: 
1.52      www      4172:     if ($thisallowed=~/F/) {
                   4173: 	return 'F';
                   4174:     }
1.29      www      4175: 
1.52      www      4176:     unless ($thisallowed) {
                   4177:         return '';
                   4178:     }
1.29      www      4179: 
1.52      www      4180: # Restrictions exist, deal with them
                   4181: #
                   4182: #   C:according to course preferences
                   4183: #   R:according to resource settings
                   4184: #   L:unless locked
                   4185: #   X:according to user session state
                   4186: #
                   4187: 
                   4188: # Possibly locked functionality, check all courses
1.54      www      4189: # Locks might take effect only after 10 minutes cache expiration for other
                   4190: # courses, and 2 minutes for current course
1.52      www      4191: 
                   4192:     my $envkey;
                   4193:     if ($thisallowed=~/L/) {
1.620     albertel 4194:         foreach $envkey (keys %env) {
1.54      www      4195:            if ($envkey=~/^user\.role\.(st|ta)\.([^\.]*)/) {
                   4196:                my $courseid=$2;
                   4197:                my $roleid=$1.'.'.$2;
1.92      www      4198:                $courseid=~s/^\///;
1.54      www      4199:                my $expiretime=600;
1.620     albertel 4200:                if ($env{'request.role'} eq $roleid) {
1.54      www      4201: 		  $expiretime=120;
                   4202:                }
                   4203: 	       my ($cdom,$cnum,$csec)=split(/\//,$courseid);
                   4204:                my $prefix='course.'.$cdom.'_'.$cnum.'.';
1.620     albertel 4205:                if ((time-$env{$prefix.'last_cache'})>$expiretime) {
1.731     albertel 4206: 		   &coursedescription($courseid,{'freshen_cache' => 1});
1.54      www      4207:                }
1.620     albertel 4208:                if (($env{$prefix.'res.'.$uri.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4209:                 || ($env{$prefix.'res.'.$uri.'.lock.sections'} eq 'all')) {
                   4210: 		   if ($env{$prefix.'res.'.$uri.'.lock.expire'}>time) {
                   4211:                        &log($env{'user.domain'},$env{'user.name'},
                   4212:                             $env{'user.home'},
1.57      www      4213:                             'Locked by res: '.$priv.' for '.$uri.' due to '.
1.52      www      4214:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4215:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4216: 		       return '';
                   4217:                    }
                   4218:                }
1.620     albertel 4219:                if (($env{$prefix.'priv.'.$priv.'.lock.sections'}=~/\,\Q$csec\E\,/)
                   4220:                 || ($env{$prefix.'priv.'.$priv.'.lock.sections'} eq 'all')) {
                   4221: 		   if ($env{'priv.'.$priv.'.lock.expire'}>time) {
                   4222:                        &log($env{'user.domain'},$env{'user.name'},
                   4223:                             $env{'user.home'},
1.57      www      4224:                             'Locked by priv: '.$priv.' for '.$uri.' due to '.
1.52      www      4225:                             $cdom.'/'.$cnum.'/'.$csec.' expire '.
1.620     albertel 4226:                             $env{$prefix.'priv.'.$priv.'.lock.expire'});
1.52      www      4227: 		       return '';
                   4228:                    }
                   4229:                }
                   4230: 	   }
1.29      www      4231:        }
1.52      www      4232:     }
                   4233:    
                   4234: #
                   4235: # Rest of the restrictions depend on selected course
                   4236: #
                   4237: 
1.620     albertel 4238:     unless ($env{'request.course.id'}) {
1.766     albertel 4239: 	if ($thisallowed eq 'A') {
                   4240: 	    return 'A';
1.814     raeburn  4241:         } elsif ($thisallowed eq 'B') {
                   4242:             return 'B';
1.766     albertel 4243: 	} else {
                   4244: 	    return '1';
                   4245: 	}
1.52      www      4246:     }
1.29      www      4247: 
1.52      www      4248: #
                   4249: # Now user is definitely in a course
                   4250: #
1.53      www      4251: 
                   4252: 
                   4253: # Course preferences
                   4254: 
                   4255:    if ($thisallowed=~/C/) {
1.620     albertel 4256:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
                   4257:        my $unamedom=$env{'user.name'}.':'.$env{'user.domain'};
                   4258:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.roles.denied'}
1.479     albertel 4259: 	   =~/\Q$rolecode\E/) {
1.689     albertel 4260: 	   if ($priv ne 'pch') { 
                   4261: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4262: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode.' in '.
                   4263: 			$env{'request.course.id'});
                   4264: 	   }
1.237     www      4265:            return '';
                   4266:        }
                   4267: 
1.620     albertel 4268:        if ($env{'course.'.$env{'request.course.id'}.'.'.$priv.'.users.denied'}
1.479     albertel 4269: 	   =~/\Q$unamedom\E/) {
1.689     albertel 4270: 	   if ($priv ne 'pch') { 
                   4271: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.
                   4272: 			'Denied by user: '.$priv.' for '.$uri.' as '.$unamedom.' in '.
                   4273: 			$env{'request.course.id'});
                   4274: 	   }
1.54      www      4275:            return '';
                   4276:        }
1.53      www      4277:    }
                   4278: 
                   4279: # Resource preferences
                   4280: 
                   4281:    if ($thisallowed=~/R/) {
1.620     albertel 4282:        my $rolecode=(split(/\./,$env{'request.role'}))[0];
1.479     albertel 4283:        if (&metadata($uri,'roledeny')=~/\Q$rolecode\E/) {
1.689     albertel 4284: 	   if ($priv ne 'pch') { 
                   4285: 	       &logthis($env{'user.domain'}.':'.$env{'user.name'}.':'.$env{'user.home'}.':'.
                   4286: 			'Denied by role: '.$priv.' for '.$uri.' as '.$rolecode);
                   4287: 	   }
                   4288: 	   return '';
1.54      www      4289:        }
1.53      www      4290:    }
1.30      www      4291: 
1.246     www      4292: # Restricted by state or randomout?
1.30      www      4293: 
1.52      www      4294:    if ($thisallowed=~/X/) {
1.620     albertel 4295:       if ($env{'acc.randomout'}) {
1.579     albertel 4296: 	 if (!$symb) { $symb=&symbread($uri,1); }
1.620     albertel 4297:          if (($symb) && ($env{'acc.randomout'}=~/\&\Q$symb\E\&/)) { 
1.248     www      4298:             return ''; 
                   4299:          }
1.247     www      4300:       }
                   4301:       if (&condval($statecond)) {
1.52      www      4302: 	 return '2';
                   4303:       } else {
                   4304:          return '';
                   4305:       }
                   4306:    }
1.30      www      4307: 
1.766     albertel 4308:     if ($thisallowed eq 'A') {
                   4309: 	return 'A';
1.814     raeburn  4310:     } elsif ($thisallowed eq 'B') {
                   4311:         return 'B';
1.766     albertel 4312:     }
1.52      www      4313:    return 'F';
1.232     www      4314: }
                   4315: 
1.710     albertel 4316: sub split_uri_for_cond {
                   4317:     my $uri=&deversion(&declutter(shift));
                   4318:     my @uriparts=split(/\//,$uri);
                   4319:     my $filename=pop(@uriparts);
                   4320:     my $pathname=join('/',@uriparts);
                   4321:     return ($pathname,$filename);
                   4322: }
1.232     www      4323: # --------------------------------------------------- Is a resource on the map?
                   4324: 
                   4325: sub is_on_map {
1.710     albertel 4326:     my ($pathname,$filename) = &split_uri_for_cond(shift);
1.289     bowersj2 4327:     #Trying to find the conditional for the file
1.620     albertel 4328:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
1.289     bowersj2 4329: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
1.232     www      4330:     if ($match) {
1.289     bowersj2 4331: 	return (1,$1);
                   4332:     } else {
1.434     www      4333: 	return (0,0);
1.289     bowersj2 4334:     }
1.12      www      4335: }
                   4336: 
1.427     www      4337: # --------------------------------------------------------- Get symb from alias
                   4338: 
                   4339: sub get_symb_from_alias {
                   4340:     my $symb=shift;
                   4341:     my ($map,$resid,$url)=&decode_symb($symb);
                   4342: # Already is a symb
                   4343:     if ($url) { return $symb; }
                   4344: # Must be an alias
                   4345:     my $aliassymb='';
                   4346:     my %bighash;
1.620     albertel 4347:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.427     www      4348:                             &GDBM_READER(),0640)) {
                   4349:         my $rid=$bighash{'mapalias_'.$symb};
                   4350: 	if ($rid) {
                   4351: 	    my ($mapid,$resid)=split(/\./,$rid);
1.429     albertel 4352: 	    $aliassymb=&encode_symb($bighash{'map_id_'.$mapid},
                   4353: 				    $resid,$bighash{'src_'.$rid});
1.427     www      4354: 	}
                   4355:         untie %bighash;
                   4356:     }
                   4357:     return $aliassymb;
                   4358: }
                   4359: 
1.12      www      4360: # ----------------------------------------------------------------- Define Role
                   4361: 
                   4362: sub definerole {
                   4363:   if (allowed('mcr','/')) {
                   4364:     my ($rolename,$sysrole,$domrole,$courole)=@_;
1.800     albertel 4365:     foreach my $role (split(':',$sysrole)) {
                   4366: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4367:         if ($pr{'cr:s'}!~/\Q$crole\E/) { return "refused:s:$crole"; }
                   4368:         if ($pr{'cr:s'}=~/\Q$crole\E\&/) {
                   4369: 	    if ($pr{'cr:s'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4370:                return "refused:s:$crole&$cqual"; 
                   4371:             }
                   4372:         }
1.191     harris41 4373:     }
1.800     albertel 4374:     foreach my $role (split(':',$domrole)) {
                   4375: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4376:         if ($pr{'cr:d'}!~/\Q$crole\E/) { return "refused:d:$crole"; }
                   4377:         if ($pr{'cr:d'}=~/\Q$crole\E\&/) {
                   4378: 	    if ($pr{'cr:d'}!~/\Q$crole\W\&\w*\Q$cqual\E/) { 
1.21      www      4379:                return "refused:d:$crole&$cqual"; 
                   4380:             }
                   4381:         }
1.191     harris41 4382:     }
1.800     albertel 4383:     foreach my $role (split(':',$courole)) {
                   4384: 	my ($crole,$cqual)=split(/\&/,$role);
1.479     albertel 4385:         if ($pr{'cr:c'}!~/\Q$crole\E/) { return "refused:c:$crole"; }
                   4386:         if ($pr{'cr:c'}=~/\Q$crole\E\&/) {
                   4387: 	    if ($pr{'cr:c'}!~/\Q$crole\E\&\w*\Q$cqual\E/) { 
1.21      www      4388:                return "refused:c:$crole&$cqual"; 
                   4389:             }
                   4390:         }
1.191     harris41 4391:     }
1.620     albertel 4392:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
                   4393:                 "$env{'user.domain'}:$env{'user.name'}:".
1.21      www      4394: 	        "rolesdef_$rolename=".
                   4395:                 escape($sysrole.'_'.$domrole.'_'.$courole);
1.620     albertel 4396:     return reply($command,$env{'user.home'});
1.12      www      4397:   } else {
                   4398:     return 'refused';
                   4399:   }
1.105     harris41 4400: }
                   4401: 
                   4402: # ---------------- Make a metadata query against the network of library servers
                   4403: 
                   4404: sub metadata_query {
1.244     matthew  4405:     my ($query,$custom,$customshow,$server_array)=@_;
1.120     harris41 4406:     my %rhash;
1.845     albertel 4407:     my %libserv = &all_library();
1.244     matthew  4408:     my @server_list = (defined($server_array) ? @$server_array
                   4409:                                               : keys(%libserv) );
                   4410:     for my $server (@server_list) {
1.118     harris41 4411: 	unless ($custom or $customshow) {
                   4412: 	    my $reply=&reply("querysend:".&escape($query),$server);
                   4413: 	    $rhash{$server}=$reply;
                   4414: 	}
                   4415: 	else {
                   4416: 	    my $reply=&reply("querysend:".&escape($query).':'.
                   4417: 			     &escape($custom).':'.&escape($customshow),
                   4418: 			     $server);
                   4419: 	    $rhash{$server}=$reply;
                   4420: 	}
1.112     harris41 4421:     }
1.118     harris41 4422:     return \%rhash;
1.240     www      4423: }
                   4424: 
                   4425: # ----------------------------------------- Send log queries and wait for reply
                   4426: 
                   4427: sub log_query {
                   4428:     my ($uname,$udom,$query,%filters)=@_;
                   4429:     my $uhome=&homeserver($uname,$udom);
                   4430:     if ($uhome eq 'no_host') { return 'error: no_host'; }
1.838     albertel 4431:     my $uhost=&hostname($uhome);
1.800     albertel 4432:     my $command=&escape(join(':',map{$_.'='.$filters{$_}} keys(%filters)));
1.240     www      4433:     my $queryid=&reply("querysend:".$query.':'.$udom.':'.$uname.':'.$command,
                   4434:                        $uhome);
1.479     albertel 4435:     unless ($queryid=~/^\Q$uhost\E\_/) { return 'error: '.$queryid; }
1.242     www      4436:     return get_query_reply($queryid);
                   4437: }
                   4438: 
1.818     raeburn  4439: # -------------------------- Update MySQL table for portfolio file
                   4440: 
                   4441: sub update_portfolio_table {
1.821     raeburn  4442:     my ($uname,$udom,$file_name,$query,$group,$action) = @_;
1.818     raeburn  4443:     my $homeserver = &homeserver($uname,$udom);
                   4444:     my $queryid=
1.821     raeburn  4445:         &reply("querysend:".$query.':'.&escape($uname.':'.$udom.':'.$group).
                   4446:                ':'.&escape($file_name).':'.$action,$homeserver);
1.818     raeburn  4447:     my $reply = &get_query_reply($queryid);
                   4448:     return $reply;
                   4449: }
                   4450: 
1.899     raeburn  4451: # -------------------------- Update MySQL allusers table
                   4452: 
                   4453: sub update_allusers_table {
                   4454:     my ($uname,$udom,$names) = @_;
                   4455:     my $homeserver = &homeserver($uname,$udom);
                   4456:     my $queryid=
                   4457:         &reply('querysend:allusers:'.&escape($uname).':'.&escape($udom).':'.
                   4458:                'lastname='.&escape($names->{'lastname'}).'%%'.
                   4459:                'firstname='.&escape($names->{'firstname'}).'%%'.
                   4460:                'middlename='.&escape($names->{'middlename'}).'%%'.
                   4461:                'generation='.&escape($names->{'generation'}).'%%'.
                   4462:                'permanentemail='.&escape($names->{'permanentemail'}).'%%'.
                   4463:                'id='.&escape($names->{'id'}),$homeserver);
                   4464:     my $reply = &get_query_reply($queryid);
                   4465:     return $reply;
                   4466: }
                   4467: 
1.508     raeburn  4468: # ------- Request retrieval of institutional classlists for course(s)
1.506     raeburn  4469: 
                   4470: sub fetch_enrollment_query {
1.511     raeburn  4471:     my ($context,$affiliatesref,$replyref,$dom,$cnum) = @_;
1.508     raeburn  4472:     my $homeserver;
1.547     raeburn  4473:     my $maxtries = 1;
1.508     raeburn  4474:     if ($context eq 'automated') {
                   4475:         $homeserver = $perlvar{'lonHostID'};
1.547     raeburn  4476:         $maxtries = 10; # will wait for up to 2000s for retrieval of classlist data before timeout
1.508     raeburn  4477:     } else {
                   4478:         $homeserver = &homeserver($cnum,$dom);
                   4479:     }
1.838     albertel 4480:     my $host=&hostname($homeserver);
1.506     raeburn  4481:     my $cmd = '';
1.800     albertel 4482:     foreach my $affiliate (keys %{$affiliatesref}) {
                   4483:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.506     raeburn  4484:     }
                   4485:     $cmd =~ s/%%$//;
                   4486:     $cmd = &escape($cmd);
                   4487:     my $query = 'fetchenrollment';
1.620     albertel 4488:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$env{'user.name'}.':'.$cmd,$homeserver);
1.526     raeburn  4489:     unless ($queryid=~/^\Q$host\E\_/) { 
                   4490:         &logthis('fetch_enrollment_query: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' context: '.$context.' '.$cnum); 
                   4491:         return 'error: '.$queryid;
                   4492:     }
1.506     raeburn  4493:     my $reply = &get_query_reply($queryid);
1.547     raeburn  4494:     my $tries = 1;
                   4495:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4496:         $reply = &get_query_reply($queryid);
                   4497:         $tries ++;
                   4498:     }
1.526     raeburn  4499:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
1.620     albertel 4500:         &logthis('fetch_enrollment_query error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' context: '.$context.' '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
1.526     raeburn  4501:     } else {
1.515     raeburn  4502:         my @responses = split/:/,$reply;
                   4503:         if ($homeserver eq $perlvar{'lonHostID'}) {
1.800     albertel 4504:             foreach my $line (@responses) {
                   4505:                 my ($key,$value) = split(/=/,$line,2);
1.515     raeburn  4506:                 $$replyref{$key} = $value;
                   4507:             }
                   4508:         } else {
1.506     raeburn  4509:             my $pathname = $perlvar{'lonDaemons'}.'/tmp';
1.800     albertel 4510:             foreach my $line (@responses) {
                   4511:                 my ($key,$value) = split(/=/,$line);
1.506     raeburn  4512:                 $$replyref{$key} = $value;
                   4513:                 if ($value > 0) {
1.800     albertel 4514:                     foreach my $item (@{$$affiliatesref{$key}}) {
                   4515:                         my $filename = $dom.'_'.$key.'_'.$item.'_classlist.xml';
1.506     raeburn  4516:                         my $destname = $pathname.'/'.$filename;
                   4517:                         my $xml_classlist = &reply("autoretrieve:".$filename,$homeserver);
1.526     raeburn  4518:                         if ($xml_classlist =~ /^error/) {
                   4519:                             &logthis('fetch_enrollment_query - autoretrieve error: '.$xml_classlist.' for '.$filename.' from server: '.$homeserver.' '.$context.' '.$cnum);
                   4520:                         } else {
1.506     raeburn  4521:                             if ( open(FILE,">$destname") ) {
                   4522:                                 print FILE &unescape($xml_classlist);
                   4523:                                 close(FILE);
1.526     raeburn  4524:                             } else {
                   4525:                                 &logthis('fetch_enrollment_query - error opening classlist file '.$destname.' '.$context.' '.$cnum);
1.506     raeburn  4526:                             }
                   4527:                         }
                   4528:                     }
                   4529:                 }
                   4530:             }
                   4531:         }
                   4532:         return 'ok';
                   4533:     }
                   4534:     return 'error';
                   4535: }
                   4536: 
1.242     www      4537: sub get_query_reply {
                   4538:     my $queryid=shift;
1.240     www      4539:     my $replyfile=$perlvar{'lonDaemons'}.'/tmp/'.$queryid;
                   4540:     my $reply='';
                   4541:     for (1..100) {
                   4542: 	sleep 2;
                   4543:         if (-e $replyfile.'.end') {
1.448     albertel 4544: 	    if (open(my $fh,$replyfile)) {
1.240     www      4545:                $reply.=<$fh>;
1.448     albertel 4546:                close($fh);
1.240     www      4547: 	   } else { return 'error: reply_file_error'; }
1.242     www      4548:            return &unescape($reply);
                   4549: 	}
1.240     www      4550:     }
1.242     www      4551:     return 'timeout:'.$queryid;
1.240     www      4552: }
                   4553: 
                   4554: sub courselog_query {
1.241     www      4555: #
                   4556: # possible filters:
                   4557: # url: url or symb
                   4558: # username
                   4559: # domain
                   4560: # action: view, submit, grade
                   4561: # start: timestamp
                   4562: # end: timestamp
                   4563: #
1.240     www      4564:     my (%filters)=@_;
1.620     albertel 4565:     unless ($env{'request.course.id'}) { return 'no_course'; }
1.241     www      4566:     if ($filters{'url'}) {
                   4567: 	$filters{'url'}=&symbclean(&declutter($filters{'url'}));
                   4568:         $filters{'url'}=~s/\.(\w+)$/(\\.\\d+)*\\.$1/;
                   4569:         $filters{'url'}=~s/\.(\w+)\_\_\_/(\\.\\d+)*\\.$1/;
                   4570:     }
1.620     albertel 4571:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   4572:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.240     www      4573:     return &log_query($cname,$cdom,'courselog',%filters);
                   4574: }
                   4575: 
                   4576: sub userlog_query {
1.858     raeburn  4577: #
                   4578: # possible filters:
                   4579: # action: log check role
                   4580: # start: timestamp
                   4581: # end: timestamp
                   4582: #
1.240     www      4583:     my ($uname,$udom,%filters)=@_;
                   4584:     return &log_query($uname,$udom,'userlog',%filters);
1.12      www      4585: }
                   4586: 
1.506     raeburn  4587: #--------- Call auto-enrollment subs in localenroll.pm for homeserver for course 
                   4588: 
                   4589: sub auto_run {
1.508     raeburn  4590:     my ($cnum,$cdom) = @_;
1.876     raeburn  4591:     my $response = 0;
                   4592:     my $settings;
                   4593:     my %domconfig = &get_dom('configuration',['autoenroll'],$cdom);
                   4594:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   4595:         $settings = $domconfig{'autoenroll'};
                   4596:         if ($settings->{'run'} eq '1') {
                   4597:             $response = 1;
                   4598:         }
                   4599:     } else {
                   4600:         my $homeserver = &homeserver($cnum,$cdom);
                   4601:         $response = &reply('autorun:'.$cdom,$homeserver);
                   4602:     }
1.506     raeburn  4603:     return $response;
                   4604: }
1.776     albertel 4605: 
1.506     raeburn  4606: sub auto_get_sections {
1.508     raeburn  4607:     my ($cnum,$cdom,$inst_coursecode) = @_;
                   4608:     my $homeserver = &homeserver($cnum,$cdom);
1.506     raeburn  4609:     my @secs = ();
1.511     raeburn  4610:     my $response=&unescape(&reply('autogetsections:'.$inst_coursecode.':'.$cdom,$homeserver));
1.506     raeburn  4611:     unless ($response eq 'refused') {
                   4612:         @secs = split/:/,$response;
                   4613:     }
                   4614:     return @secs;
                   4615: }
1.776     albertel 4616: 
1.506     raeburn  4617: sub auto_new_course {
1.508     raeburn  4618:     my ($cnum,$cdom,$inst_course_id,$owner) = @_;
                   4619:     my $homeserver = &homeserver($cnum,$cdom);
1.515     raeburn  4620:     my $response=&unescape(&reply('autonewcourse:'.$inst_course_id.':'.$owner.':'.$cdom,$homeserver));
1.506     raeburn  4621:     return $response;
                   4622: }
1.776     albertel 4623: 
1.506     raeburn  4624: sub auto_validate_courseID {
1.508     raeburn  4625:     my ($cnum,$cdom,$inst_course_id) = @_;
                   4626:     my $homeserver = &homeserver($cnum,$cdom);
1.511     raeburn  4627:     my $response=&unescape(&reply('autovalidatecourse:'.$inst_course_id.':'.$cdom,$homeserver));
1.506     raeburn  4628:     return $response;
                   4629: }
1.776     albertel 4630: 
1.506     raeburn  4631: sub auto_create_password {
1.873     raeburn  4632:     my ($cnum,$cdom,$authparam,$udom) = @_;
                   4633:     my ($homeserver,$response);
1.506     raeburn  4634:     my $create_passwd = 0;
                   4635:     my $authchk = '';
1.873     raeburn  4636:     if ($udom =~ /^$match_domain$/) {
                   4637:         $homeserver = &domain($udom,'primary');
                   4638:     }
                   4639:     if ($homeserver eq '') {
                   4640:         if (($cdom =~ /^$match_domain$/) && ($cnum =~ /^$match_courseid$/)) {
                   4641:             $homeserver = &homeserver($cnum,$cdom);
                   4642:         }
                   4643:     }
                   4644:     if ($homeserver eq '') {
                   4645:         $authchk = 'nodomain';
1.506     raeburn  4646:     } else {
1.873     raeburn  4647:         $response=&unescape(&reply('autocreatepassword:'.$authparam.':'.$cdom,$homeserver));
                   4648:         if ($response eq 'refused') {
                   4649:             $authchk = 'refused';
                   4650:         } else {
                   4651:             ($authparam,$create_passwd,$authchk) = split/:/,$response;
                   4652:         }
1.506     raeburn  4653:     }
                   4654:     return ($authparam,$create_passwd,$authchk);
                   4655: }
                   4656: 
1.706     raeburn  4657: sub auto_photo_permission {
                   4658:     my ($cnum,$cdom,$students) = @_;
                   4659:     my $homeserver = &homeserver($cnum,$cdom);
1.707     albertel 4660:     my ($outcome,$perm_reqd,$conditions) = 
                   4661: 	split(/:/,&unescape(&reply('autophotopermission:'.$cdom,$homeserver)),3);
1.709     albertel 4662:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4663: 	return (undef,undef);
                   4664:     }
1.706     raeburn  4665:     return ($outcome,$perm_reqd,$conditions);
                   4666: }
                   4667: 
                   4668: sub auto_checkphotos {
                   4669:     my ($uname,$udom,$pid) = @_;
                   4670:     my $homeserver = &homeserver($uname,$udom);
                   4671:     my ($result,$resulttype);
                   4672:     my $outcome = &unescape(&reply('autophotocheck:'.&escape($udom).':'.
1.707     albertel 4673: 				   &escape($uname).':'.&escape($pid),
                   4674: 				   $homeserver));
1.709     albertel 4675:     if ($outcome =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4676: 	return (undef,undef);
                   4677:     }
1.706     raeburn  4678:     if ($outcome) {
                   4679:         ($result,$resulttype) = split(/:/,$outcome);
                   4680:     } 
                   4681:     return ($result,$resulttype);
                   4682: }
                   4683: 
                   4684: sub auto_photochoice {
                   4685:     my ($cnum,$cdom) = @_;
                   4686:     my $homeserver = &homeserver($cnum,$cdom);
                   4687:     my ($update,$comment) = split(/:/,&unescape(&reply('autophotochoice:'.
1.707     albertel 4688: 						       &escape($cdom),
                   4689: 						       $homeserver)));
1.709     albertel 4690:     if ($update =~ /^(con_lost|unknown_cmd|no_such_host)$/) {
                   4691: 	return (undef,undef);
                   4692:     }
1.706     raeburn  4693:     return ($update,$comment);
                   4694: }
                   4695: 
                   4696: sub auto_photoupdate {
                   4697:     my ($affiliatesref,$dom,$cnum,$photo) = @_;
                   4698:     my $homeserver = &homeserver($cnum,$dom);
1.838     albertel 4699:     my $host=&hostname($homeserver);
1.706     raeburn  4700:     my $cmd = '';
                   4701:     my $maxtries = 1;
1.800     albertel 4702:     foreach my $affiliate (keys(%{$affiliatesref})) {
                   4703:         $cmd .= $affiliate.'='.join(",",@{$$affiliatesref{$affiliate}}).'%%';
1.706     raeburn  4704:     }
                   4705:     $cmd =~ s/%%$//;
                   4706:     $cmd = &escape($cmd);
                   4707:     my $query = 'institutionalphotos';
                   4708:     my $queryid=&reply("querysend:".$query.':'.$dom.':'.$cnum.':'.$cmd,$homeserver);
                   4709:     unless ($queryid=~/^\Q$host\E\_/) {
                   4710:         &logthis('institutionalphotos: invalid queryid: '.$queryid.' for host: '.$host.' and homeserver: '.$homeserver.' and course: '.$cnum);
                   4711:         return 'error: '.$queryid;
                   4712:     }
                   4713:     my $reply = &get_query_reply($queryid);
                   4714:     my $tries = 1;
                   4715:     while (($reply=~/^timeout/) && ($tries < $maxtries)) {
                   4716:         $reply = &get_query_reply($queryid);
                   4717:         $tries ++;
                   4718:     }
                   4719:     if ( ($reply =~/^timeout/) || ($reply =~/^error/) ) {
                   4720:         &logthis('institutionalphotos error: '.$reply.' for '.$dom.' '.$env{'user.name'}.' for '.$queryid.' course: '.$cnum.' maxtries: '.$maxtries.' tries: '.$tries);
                   4721:     } else {
                   4722:         my @responses = split(/:/,$reply);
                   4723:         my $outcome = shift(@responses); 
                   4724:         foreach my $item (@responses) {
                   4725:             my ($key,$value) = split(/=/,$item);
                   4726:             $$photo{$key} = $value;
                   4727:         }
                   4728:         return $outcome;
                   4729:     }
                   4730:     return 'error';
                   4731: }
                   4732: 
1.521     raeburn  4733: sub auto_instcode_format {
1.793     albertel 4734:     my ($caller,$codedom,$instcodes,$codes,$codetitles,$cat_titles,
                   4735: 	$cat_order) = @_;
1.521     raeburn  4736:     my $courses = '';
1.772     raeburn  4737:     my @homeservers;
1.521     raeburn  4738:     if ($caller eq 'global') {
1.841     albertel 4739: 	my %servers = &get_servers($codedom,'library');
                   4740: 	foreach my $tryserver (keys(%servers)) {
                   4741: 	    if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4742: 		push(@homeservers,$tryserver);
                   4743: 	    }
1.584     raeburn  4744:         }
1.521     raeburn  4745:     } else {
1.772     raeburn  4746:         push(@homeservers,&homeserver($caller,$codedom));
1.521     raeburn  4747:     }
1.793     albertel 4748:     foreach my $code (keys(%{$instcodes})) {
                   4749:         $courses .= &escape($code).'='.&escape($$instcodes{$code}).'&';
1.521     raeburn  4750:     }
                   4751:     chop($courses);
1.772     raeburn  4752:     my $ok_response = 0;
                   4753:     my $response;
                   4754:     while (@homeservers > 0 && $ok_response == 0) {
                   4755:         my $server = shift(@homeservers); 
                   4756:         $response=&reply('autoinstcodeformat:'.$codedom.':'.$courses,$server);
                   4757:         if ($response !~ /(con_lost|error|no_such_host|refused)/) {
                   4758:             my ($codes_str,$codetitles_str,$cat_titles_str,$cat_order_str) = 
1.793     albertel 4759: 		split/:/,$response;
1.772     raeburn  4760:             %{$codes} = (%{$codes},&str2hash($codes_str));
                   4761:             push(@{$codetitles},&str2array($codetitles_str));
                   4762:             %{$cat_titles} = (%{$cat_titles},&str2hash($cat_titles_str));
                   4763:             %{$cat_order} = (%{$cat_order},&str2hash($cat_order_str));
                   4764:             $ok_response = 1;
                   4765:         }
                   4766:     }
                   4767:     if ($ok_response) {
1.521     raeburn  4768:         return 'ok';
1.772     raeburn  4769:     } else {
                   4770:         return $response;
1.521     raeburn  4771:     }
                   4772: }
                   4773: 
1.792     raeburn  4774: sub auto_instcode_defaults {
                   4775:     my ($domain,$returnhash,$code_order) = @_;
                   4776:     my @homeservers;
1.841     albertel 4777: 
                   4778:     my %servers = &get_servers($domain,'library');
                   4779:     foreach my $tryserver (keys(%servers)) {
                   4780: 	if (!grep(/^\Q$tryserver\E$/,@homeservers)) {
                   4781: 	    push(@homeservers,$tryserver);
                   4782: 	}
1.792     raeburn  4783:     }
1.841     albertel 4784: 
1.792     raeburn  4785:     my $response;
1.841     albertel 4786:     foreach my $server (@homeservers) {
1.792     raeburn  4787:         $response=&reply('autoinstcodedefaults:'.$domain,$server);
1.841     albertel 4788:         next if ($response =~ /(con_lost|error|no_such_host|refused)/);
                   4789: 	
                   4790: 	foreach my $pair (split(/\&/,$response)) {
                   4791: 	    my ($name,$value)=split(/\=/,$pair);
                   4792: 	    if ($name eq 'code_order') {
                   4793: 		@{$code_order} = split(/\&/,&unescape($value));
                   4794: 	    } else {
                   4795: 		$returnhash->{&unescape($name)}=&unescape($value);
                   4796: 	    }
                   4797: 	}
                   4798: 	return 'ok';
1.792     raeburn  4799:     }
1.841     albertel 4800: 
                   4801:     return $response;
1.792     raeburn  4802: } 
                   4803: 
1.777     albertel 4804: sub auto_validate_class_sec {
1.773     raeburn  4805:     my ($cdom,$cnum,$owner,$inst_class) = @_;
                   4806:     my $homeserver = &homeserver($cnum,$cdom);
                   4807:     my $response=&reply('autovalidateclass_sec:'.$inst_class.':'.
1.774     banghart 4808:                         &escape($owner).':'.$cdom,$homeserver);
1.773     raeburn  4809:     return $response;
                   4810: }
                   4811: 
1.679     raeburn  4812: # ------------------------------------------------------- Course Group routines
                   4813: 
                   4814: sub get_coursegroups {
1.809     raeburn  4815:     my ($cdom,$cnum,$group,$namespace) = @_;
                   4816:     return(&dump($namespace,$cdom,$cnum,$group));
1.805     raeburn  4817: }
                   4818: 
1.679     raeburn  4819: sub modify_coursegroup {
                   4820:     my ($cdom,$cnum,$groupsettings) = @_;
                   4821:     return(&put('coursegroups',$groupsettings,$cdom,$cnum));
                   4822: }
                   4823: 
1.809     raeburn  4824: sub toggle_coursegroup_status {
                   4825:     my ($cdom,$cnum,$group,$action) = @_;
                   4826:     my ($from_namespace,$to_namespace);
                   4827:     if ($action eq 'delete') {
                   4828:         $from_namespace = 'coursegroups';
                   4829:         $to_namespace = 'deleted_groups';
                   4830:     } else {
                   4831:         $from_namespace = 'deleted_groups';
                   4832:         $to_namespace = 'coursegroups';
                   4833:     }
                   4834:     my %curr_group = &get_coursegroups($cdom,$cnum,$group,$from_namespace);
1.805     raeburn  4835:     if (my $tmp = &error(%curr_group)) {
                   4836:         &Apache::lonnet::logthis('Error retrieving group: '.$tmp.' in '.$cnum.':'.$cdom);
                   4837:         return ('read error',$tmp);
                   4838:     } else {
                   4839:         my %savedsettings = %curr_group; 
1.809     raeburn  4840:         my $result = &put($to_namespace,\%savedsettings,$cdom,$cnum);
1.805     raeburn  4841:         my $deloutcome;
                   4842:         if ($result eq 'ok') {
1.809     raeburn  4843:             $deloutcome = &del($from_namespace,[$group],$cdom,$cnum);
1.805     raeburn  4844:         } else {
                   4845:             return ('write error',$result);
                   4846:         }
                   4847:         if ($deloutcome eq 'ok') {
                   4848:             return 'ok';
                   4849:         } else {
                   4850:             return ('delete error',$deloutcome);
                   4851:         }
                   4852:     }
                   4853: }
                   4854: 
1.679     raeburn  4855: sub modify_group_roles {
                   4856:     my ($cdom,$cnum,$group_id,$user,$end,$start,$userprivs) = @_;
                   4857:     my $url = '/'.$cdom.'/'.$cnum.'/'.$group_id;
                   4858:     my $role = 'gr/'.&escape($userprivs);
                   4859:     my ($uname,$udom) = split(/:/,$user);
                   4860:     my $result = &assignrole($udom,$uname,$url,$role,$end,$start);
1.684     raeburn  4861:     if ($result eq 'ok') {
                   4862:         &devalidate_getgroups_cache($udom,$uname,$cdom,$cnum);
                   4863:     }
1.679     raeburn  4864:     return $result;
                   4865: }
                   4866: 
                   4867: sub modify_coursegroup_membership {
                   4868:     my ($cdom,$cnum,$membership) = @_;
                   4869:     my $result = &put('groupmembership',$membership,$cdom,$cnum);
                   4870:     return $result;
                   4871: }
                   4872: 
1.682     raeburn  4873: sub get_active_groups {
                   4874:     my ($udom,$uname,$cdom,$cnum) = @_;
                   4875:     my $now = time;
                   4876:     my %groups = ();
                   4877:     foreach my $key (keys(%env)) {
1.811     albertel 4878:         if ($key =~ m-user\.role\.gr\./($match_domain)/($match_courseid)/(\w+)$-) {
1.682     raeburn  4879:             my ($start,$end) = split(/\./,$env{$key});
                   4880:             if (($end!=0) && ($end<$now)) { next; }
                   4881:             if (($start!=0) && ($start>$now)) { next; }
                   4882:             if ($1 eq $cdom && $2 eq $cnum) {
                   4883:                 $groups{$3} = $env{$key} ;
                   4884:             }
                   4885:         }
                   4886:     }
                   4887:     return %groups;
                   4888: }
                   4889: 
1.683     raeburn  4890: sub get_group_membership {
                   4891:     my ($cdom,$cnum,$group) = @_;
                   4892:     return(&dump('groupmembership',$cdom,$cnum,$group));
                   4893: }
                   4894: 
                   4895: sub get_users_groups {
                   4896:     my ($udom,$uname,$courseid) = @_;
1.733     raeburn  4897:     my @usersgroups;
1.683     raeburn  4898:     my $cachetime=1800;
                   4899: 
                   4900:     my $hashid="$udom:$uname:$courseid";
1.733     raeburn  4901:     my ($grouplist,$cached)=&is_cached_new('getgroups',$hashid);
                   4902:     if (defined($cached)) {
1.734     albertel 4903:         @usersgroups = split(/:/,$grouplist);
1.733     raeburn  4904:     } else {  
                   4905:         $grouplist = '';
1.816     raeburn  4906:         my $courseurl = &courseid_to_courseurl($courseid);
                   4907:         my %roleshash = &dump('roles',$udom,$uname,$courseurl);
1.817     raeburn  4908:         my $access_end = $env{'course.'.$courseid.
                   4909:                               '.default_enrollment_end_date'};
                   4910:         my $now = time;
                   4911:         foreach my $key (keys(%roleshash)) {
                   4912:             if ($key =~ /^\Q$courseurl\E\/(\w+)\_gr$/) {
                   4913:                 my $group = $1;
                   4914:                 if ($roleshash{$key} =~ /_(\d+)_(\d+)$/) {
                   4915:                     my $start = $2;
                   4916:                     my $end = $1;
                   4917:                     if ($start == -1) { next; } # deleted from group
                   4918:                     if (($start!=0) && ($start>$now)) { next; }
                   4919:                     if (($end!=0) && ($end<$now)) {
                   4920:                         if ($access_end && $access_end < $now) {
                   4921:                             if ($access_end - $end < 86400) {
                   4922:                                 push(@usersgroups,$group);
1.733     raeburn  4923:                             }
                   4924:                         }
1.817     raeburn  4925:                         next;
1.733     raeburn  4926:                     }
1.817     raeburn  4927:                     push(@usersgroups,$group);
1.683     raeburn  4928:                 }
                   4929:             }
                   4930:         }
1.817     raeburn  4931:         @usersgroups = &sort_course_groups($courseid,@usersgroups);
                   4932:         $grouplist = join(':',@usersgroups);
                   4933:         &do_cache_new('getgroups',$hashid,$grouplist,$cachetime);
1.683     raeburn  4934:     }
1.733     raeburn  4935:     return @usersgroups;
1.683     raeburn  4936: }
                   4937: 
                   4938: sub devalidate_getgroups_cache {
                   4939:     my ($udom,$uname,$cdom,$cnum)=@_;
                   4940:     my $courseid = $cdom.'_'.$cnum;
1.807     albertel 4941: 
1.683     raeburn  4942:     my $hashid="$udom:$uname:$courseid";
                   4943:     &devalidate_cache_new('getgroups',$hashid);
                   4944: }
                   4945: 
1.12      www      4946: # ------------------------------------------------------------------ Plain Text
                   4947: 
                   4948: sub plaintext {
1.742     raeburn  4949:     my ($short,$type,$cid) = @_;
1.758     albertel 4950:     if ($short =~ /^cr/) {
                   4951: 	return (split('/',$short))[-1];
                   4952:     }
1.742     raeburn  4953:     if (!defined($cid)) {
                   4954:         $cid = $env{'request.course.id'};
                   4955:     }
                   4956:     if (defined($cid) && defined($env{'course.'.$cid.'.'.$short.'.plaintext'})) {
                   4957:         return &Apache::lonlocal::mt($env{'course.'.$cid.'.'.$short.
                   4958:                                           '.plaintext'});
                   4959:     }
                   4960:     my %rolenames = (
                   4961:                       Course => 'std',
                   4962:                       Group => 'alt1',
                   4963:                     );
                   4964:     if (defined($type) && 
                   4965:          defined($rolenames{$type}) && 
                   4966:          defined($prp{$short}{$rolenames{$type}})) {
                   4967:         return &Apache::lonlocal::mt($prp{$short}{$rolenames{$type}});
                   4968:     } else {
                   4969:         return &Apache::lonlocal::mt($prp{$short}{'std'});
                   4970:     }
1.12      www      4971: }
                   4972: 
                   4973: # ----------------------------------------------------------------- Assign Role
                   4974: 
                   4975: sub assignrole {
1.357     www      4976:     my ($udom,$uname,$url,$role,$end,$start,$deleteflag)=@_;
1.21      www      4977:     my $mrole;
                   4978:     if ($role =~ /^cr\//) {
1.393     www      4979:         my $cwosec=$url;
1.811     albertel 4980:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.393     www      4981: 	unless (&allowed('ccr',$cwosec)) {
1.104     www      4982:            &logthis('Refused custom assignrole: '.
                   4983:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 4984: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      4985:            return 'refused'; 
                   4986:         }
1.21      www      4987:         $mrole='cr';
1.678     raeburn  4988:     } elsif ($role =~ /^gr\//) {
                   4989:         my $cwogrp=$url;
1.811     albertel 4990:         $cwogrp=~s{^/($match_domain)/($match_courseid)/.*}{$1/$2};
1.678     raeburn  4991:         unless (&allowed('mdg',$cwogrp)) {
                   4992:             &logthis('Refused group assignrole: '.
                   4993:               $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
                   4994:                     $env{'user.name'}.' at '.$env{'user.domain'});
                   4995:             return 'refused';
                   4996:         }
                   4997:         $mrole='gr';
1.21      www      4998:     } else {
1.82      www      4999:         my $cwosec=$url;
1.811     albertel 5000:         $cwosec=~s/^\/($match_domain)\/($match_courseid)\/.*/$1\/$2/;
1.373     www      5001:         unless ((&allowed('c'.$role,$cwosec)) || &allowed('c'.$role,$udom)) { 
1.104     www      5002:            &logthis('Refused assignrole: '.
                   5003:              $udom.' '.$uname.' '.$url.' '.$role.' '.$end.' '.$start.' by '.
1.620     albertel 5004: 		    $env{'user.name'}.' at '.$env{'user.domain'});
1.104     www      5005:            return 'refused'; 
                   5006:         }
1.21      www      5007:         $mrole=$role;
                   5008:     }
1.620     albertel 5009:     my $command="encrypt:rolesput:$env{'user.domain'}:$env{'user.name'}:".
1.21      www      5010:                 "$udom:$uname:$url".'_'."$mrole=$role";
1.81      www      5011:     if ($end) { $command.='_'.$end; }
1.21      www      5012:     if ($start) {
                   5013: 	if ($end) { 
1.81      www      5014:            $command.='_'.$start; 
1.21      www      5015:         } else {
1.81      www      5016:            $command.='_0_'.$start;
1.21      www      5017:         }
                   5018:     }
1.739     raeburn  5019:     my $origstart = $start;
                   5020:     my $origend = $end;
1.357     www      5021: # actually delete
                   5022:     if ($deleteflag) {
1.373     www      5023: 	if ((&allowed('dro',$udom)) || (&allowed('dro',$url))) {
1.357     www      5024: # modify command to delete the role
1.620     albertel 5025:            $command="encrypt:rolesdel:$env{'user.domain'}:$env{'user.name'}:".
1.357     www      5026:                 "$udom:$uname:$url".'_'."$mrole";
1.620     albertel 5027: 	   &logthis("$env{'user.name'} at $env{'user.domain'} deletes $mrole in $url for $uname at $udom"); 
1.357     www      5028: # set start and finish to negative values for userrolelog
                   5029:            $start=-1;
                   5030:            $end=-1;
                   5031:         }
                   5032:     }
                   5033: # send command
1.349     www      5034:     my $answer=&reply($command,&homeserver($uname,$udom));
1.357     www      5035: # log new user role if status is ok
1.349     www      5036:     if ($answer eq 'ok') {
1.663     raeburn  5037: 	&userrolelog($role,$uname,$udom,$url,$start,$end);
1.739     raeburn  5038: # for course roles, perform group memberships changes triggered by role change.
                   5039:         unless ($role =~ /^gr/) {
                   5040:             &Apache::longroup::group_changes($udom,$uname,$url,$role,$origend,
                   5041:                                              $origstart);
                   5042:         }
1.349     www      5043:     }
                   5044:     return $answer;
1.169     harris41 5045: }
                   5046: 
                   5047: # -------------------------------------------------- Modify user authentication
1.197     www      5048: # Overrides without validation
                   5049: 
1.169     harris41 5050: sub modifyuserauth {
                   5051:     my ($udom,$uname,$umode,$upass)=@_;
                   5052:     my $uhome=&homeserver($uname,$udom);
1.197     www      5053:     unless (&allowed('mau',$udom)) { return 'refused'; }
                   5054:     &logthis('Call to modify user authentication '.$udom.', '.$uname.', '.
1.620     albertel 5055:              $umode.' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5056:              ' in domain '.$env{'request.role.domain'});  
1.169     harris41 5057:     my $reply=&reply('encrypt:changeuserauth:'.$udom.':'.$uname.':'.$umode.':'.
                   5058: 		     &escape($upass),$uhome);
1.620     albertel 5059:     &log($env{'user.domain'},$env{'user.name'},$env{'user.home'},
1.197     www      5060:         'Authentication changed for '.$udom.', '.$uname.', '.$umode.
                   5061:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
                   5062:     &log($udom,,$uname,$uhome,
1.620     albertel 5063:         'Authentication changed by '.$env{'user.domain'}.', '.
                   5064:                                      $env{'user.name'}.', '.$umode.
1.197     www      5065:          '(Remote '.$ENV{'REMOTE_ADDR'}.'): '.$reply);
1.169     harris41 5066:     unless ($reply eq 'ok') {
1.197     www      5067:         &logthis('Authentication mode error: '.$reply);
1.169     harris41 5068: 	return 'error: '.$reply;
                   5069:     }   
1.170     harris41 5070:     return 'ok';
1.80      www      5071: }
                   5072: 
1.81      www      5073: # --------------------------------------------------------------- Modify a user
1.80      www      5074: 
1.81      www      5075: sub modifyuser {
1.206     matthew  5076:     my ($udom,    $uname, $uid,
                   5077:         $umode,   $upass, $first,
                   5078:         $middle,  $last,  $gene,
1.387     www      5079:         $forceid, $desiredhome, $email)=@_;
1.807     albertel 5080:     $udom= &LONCAPA::clean_domain($udom);
                   5081:     $uname=&LONCAPA::clean_username($uname);
1.81      www      5082:     &logthis('Call to modify user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5083:              $umode.', '.$first.', '.$middle.', '.
1.206     matthew  5084: 	     $last.', '.$gene.'(forceid: '.$forceid.')'.
                   5085:              (defined($desiredhome) ? ' desiredhome = '.$desiredhome :
                   5086:                                      ' desiredhome not specified'). 
1.620     albertel 5087:              ' by '.$env{'user.name'}.' at '.$env{'user.domain'}.
                   5088:              ' in domain '.$env{'request.role.domain'});
1.230     stredwic 5089:     my $uhome=&homeserver($uname,$udom,'true');
1.80      www      5090: # ----------------------------------------------------------------- Create User
1.406     albertel 5091:     if (($uhome eq 'no_host') && 
                   5092: 	(($umode && $upass) || ($umode eq 'localauth'))) {
1.80      www      5093:         my $unhome='';
1.844     albertel 5094:         if (defined($desiredhome) && &host_domain($desiredhome) eq $udom) { 
1.209     matthew  5095:             $unhome = $desiredhome;
1.620     albertel 5096: 	} elsif($env{'course.'.$env{'request.course.id'}.'.domain'} eq $udom) {
                   5097: 	    $unhome=$env{'course.'.$env{'request.course.id'}.'.home'};
1.209     matthew  5098:         } else { # load balancing routine for determining $unhome
1.81      www      5099:             my $loadm=10000000;
1.841     albertel 5100: 	    my %servers = &get_servers($udom,'library');
                   5101: 	    foreach my $tryserver (keys(%servers)) {
                   5102: 		my $answer=reply('load',$tryserver);
                   5103: 		if (($answer=~/\d+/) && ($answer<$loadm)) {
                   5104: 		    $loadm=$answer;
                   5105: 		    $unhome=$tryserver;
                   5106: 		}
1.80      www      5107: 	    }
                   5108:         }
                   5109:         if (($unhome eq '') || ($unhome eq 'no_host')) {
1.206     matthew  5110: 	    return 'error: unable to find a home server for '.$uname.
                   5111:                    ' in domain '.$udom;
1.80      www      5112:         }
                   5113:         my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':'.$umode.':'.
                   5114:                          &escape($upass),$unhome);
                   5115: 	unless ($reply eq 'ok') {
                   5116:             return 'error: '.$reply;
                   5117:         }   
1.230     stredwic 5118:         $uhome=&homeserver($uname,$udom,'true');
1.80      www      5119:         if (($uhome eq '') || ($uhome eq 'no_host') || ($uhome ne $unhome)) {
1.386     matthew  5120: 	    return 'error: unable verify users home machine.';
1.80      www      5121:         }
1.209     matthew  5122:     }   # End of creation of new user
1.80      www      5123: # ---------------------------------------------------------------------- Add ID
                   5124:     if ($uid) {
                   5125:        $uid=~tr/A-Z/a-z/;
                   5126:        my %uidhash=&idrget($udom,$uname);
1.196     www      5127:        if (($uidhash{$uname}) && ($uidhash{$uname}!~/error\:/) 
                   5128:          && (!$forceid)) {
1.80      www      5129: 	  unless ($uid eq $uidhash{$uname}) {
1.386     matthew  5130: 	      return 'error: user id "'.$uid.'" does not match '.
                   5131:                   'current user id "'.$uidhash{$uname}.'".';
1.80      www      5132:           }
                   5133:        } else {
                   5134: 	  &idput($udom,($uname => $uid));
                   5135:        }
                   5136:     }
                   5137: # -------------------------------------------------------------- Add names, etc
1.313     matthew  5138:     my @tmp=&get('environment',
1.899     raeburn  5139: 		   ['firstname','middlename','lastname','generation','id',
                   5140:                     'permanentemail'],
1.134     albertel 5141: 		   $udom,$uname);
1.313     matthew  5142:     my %names;
                   5143:     if ($tmp[0] =~ m/^error:.*/) { 
                   5144:         %names=(); 
                   5145:     } else {
                   5146:         %names = @tmp;
                   5147:     }
1.388     www      5148: #
                   5149: # Make sure to not trash student environment if instructor does not bother
                   5150: # to supply name and email information
                   5151: #
                   5152:     if ($first)  { $names{'firstname'}  = $first; }
1.385     matthew  5153:     if (defined($middle)) { $names{'middlename'} = $middle; }
1.388     www      5154:     if ($last)   { $names{'lastname'}   = $last; }
1.385     matthew  5155:     if (defined($gene))   { $names{'generation'} = $gene; }
1.592     www      5156:     if ($email) {
                   5157:        $email=~s/[^\w\@\.\-\,]//gs;
                   5158:        if ($email=~/\@/) { $names{'notification'} = $email;
                   5159: 			   $names{'critnotification'} = $email;
                   5160: 			   $names{'permanentemail'} = $email; }
                   5161:     }
1.899     raeburn  5162:     if ($uid) { $names{'id'}  = $uid; }
1.134     albertel 5163:     my $reply = &put('environment', \%names, $udom,$uname);
                   5164:     if ($reply ne 'ok') { return 'error: '.$reply; }
1.899     raeburn  5165:     my $sqlresult = &update_allusers_table($uname,$udom,\%names);
1.680     www      5166:     &devalidate_cache_new('namescache',$uname.':'.$udom);
1.81      www      5167:     &logthis('Success modifying user '.$udom.', '.$uname.', '.$uid.', '.
1.80      www      5168:              $umode.', '.$first.', '.$middle.', '.
                   5169: 	     $last.', '.$gene.' by '.
1.620     albertel 5170:              $env{'user.name'}.' at '.$env{'user.domain'});
1.134     albertel 5171:     return 'ok';
1.80      www      5172: }
                   5173: 
1.81      www      5174: # -------------------------------------------------------------- Modify student
1.80      www      5175: 
1.81      www      5176: sub modifystudent {
                   5177:     my ($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$usec,
1.515     raeburn  5178:         $end,$start,$forceid,$desiredhome,$email,$type,$locktype,$cid)=@_;
1.455     albertel 5179:     if (!$cid) {
1.620     albertel 5180: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5181: 	    return 'not_in_class';
                   5182: 	}
1.80      www      5183:     }
                   5184: # --------------------------------------------------------------- Make the user
1.81      www      5185:     my $reply=&modifyuser
1.209     matthew  5186: 	($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene,$forceid,
1.387     www      5187:          $desiredhome,$email);
1.80      www      5188:     unless ($reply eq 'ok') { return $reply; }
1.297     matthew  5189:     # This will cause &modify_student_enrollment to get the uid from the
                   5190:     # students environment
                   5191:     $uid = undef if (!$forceid);
1.455     albertel 5192:     $reply = &modify_student_enrollment($udom,$uname,$uid,$first,$middle,$last,
1.515     raeburn  5193: 					$gene,$usec,$end,$start,$type,$locktype,$cid);
1.297     matthew  5194:     return $reply;
                   5195: }
                   5196: 
                   5197: sub modify_student_enrollment {
1.515     raeburn  5198:     my ($udom,$uname,$uid,$first,$middle,$last,$gene,$usec,$end,$start,$type,$locktype,$cid) = @_;
1.455     albertel 5199:     my ($cdom,$cnum,$chome);
                   5200:     if (!$cid) {
1.620     albertel 5201: 	unless ($cid=$env{'request.course.id'}) {
1.455     albertel 5202: 	    return 'not_in_class';
                   5203: 	}
1.620     albertel 5204: 	$cdom=$env{'course.'.$cid.'.domain'};
                   5205: 	$cnum=$env{'course.'.$cid.'.num'};
1.455     albertel 5206:     } else {
                   5207: 	($cdom,$cnum)=split(/_/,$cid);
                   5208:     }
1.620     albertel 5209:     $chome=$env{'course.'.$cid.'.home'};
1.455     albertel 5210:     if (!$chome) {
1.457     raeburn  5211: 	$chome=&homeserver($cnum,$cdom);
1.297     matthew  5212:     }
1.455     albertel 5213:     if (!$chome) { return 'unknown_course'; }
1.297     matthew  5214:     # Make sure the user exists
1.81      www      5215:     my $uhome=&homeserver($uname,$udom);
                   5216:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5217: 	return 'error: no such user';
                   5218:     }
1.297     matthew  5219:     # Get student data if we were not given enough information
                   5220:     if (!defined($first)  || $first  eq '' || 
                   5221:         !defined($last)   || $last   eq '' || 
                   5222:         !defined($uid)    || $uid    eq '' || 
                   5223:         !defined($middle) || $middle eq '' || 
                   5224:         !defined($gene)   || $gene   eq '') {
1.294     matthew  5225:         # They did not supply us with enough data to enroll the student, so
                   5226:         # we need to pick up more information.
1.297     matthew  5227:         my %tmp = &get('environment',
1.294     matthew  5228:                        ['firstname','middlename','lastname', 'generation','id']
1.297     matthew  5229:                        ,$udom,$uname);
                   5230: 
1.800     albertel 5231:         #foreach my $key (keys(%tmp)) {
                   5232:         #    &logthis("key $key = ".$tmp{$key});
1.455     albertel 5233:         #}
1.294     matthew  5234:         $first  = $tmp{'firstname'}  if (!defined($first)  || $first  eq '');
                   5235:         $middle = $tmp{'middlename'} if (!defined($middle) || $middle eq '');
                   5236:         $last   = $tmp{'lastname'}   if (!defined($last)   || $last eq '');
1.297     matthew  5237:         $gene   = $tmp{'generation'} if (!defined($gene)   || $gene eq '');
1.294     matthew  5238:         $uid    = $tmp{'id'}         if (!defined($uid)    || $uid  eq '');
                   5239:     }
1.556     albertel 5240:     my $fullname = &format_name($first,$middle,$last,$gene,'lastname');
1.487     albertel 5241:     my $reply=cput('classlist',
                   5242: 		   {"$uname:$udom" => 
1.515     raeburn  5243: 			join(':',$end,$start,$uid,$usec,$fullname,$type,$locktype) },
1.487     albertel 5244: 		   $cdom,$cnum);
1.81      www      5245:     unless (($reply eq 'ok') || ($reply eq 'delayed')) {
                   5246: 	return 'error: '.$reply;
1.652     albertel 5247:     } else {
                   5248: 	&devalidate_getsection_cache($udom,$uname,$cid);
1.81      www      5249:     }
1.297     matthew  5250:     # Add student role to user
1.83      www      5251:     my $uurl='/'.$cid;
1.81      www      5252:     $uurl=~s/\_/\//g;
                   5253:     if ($usec) {
                   5254: 	$uurl.='/'.$usec;
                   5255:     }
                   5256:     return &assignrole($udom,$uname,$uurl,'st',$end,$start);
1.21      www      5257: }
                   5258: 
1.556     albertel 5259: sub format_name {
                   5260:     my ($firstname,$middlename,$lastname,$generation,$first)=@_;
                   5261:     my $name;
                   5262:     if ($first ne 'lastname') {
                   5263: 	$name=$firstname.' '.$middlename.' '.$lastname.' '.$generation;
                   5264:     } else {
                   5265: 	if ($lastname=~/\S/) {
                   5266: 	    $name.= $lastname.' '.$generation.', '.$firstname.' '.$middlename;
                   5267: 	    $name=~s/\s+,/,/;
                   5268: 	} else {
                   5269: 	    $name.= $firstname.' '.$middlename.' '.$generation;
                   5270: 	}
                   5271:     }
                   5272:     $name=~s/^\s+//;
                   5273:     $name=~s/\s+$//;
                   5274:     $name=~s/\s+/ /g;
                   5275:     return $name;
                   5276: }
                   5277: 
1.84      www      5278: # ------------------------------------------------- Write to course preferences
                   5279: 
                   5280: sub writecoursepref {
                   5281:     my ($courseid,%prefs)=@_;
                   5282:     $courseid=~s/^\///;
                   5283:     $courseid=~s/\_/\//g;
                   5284:     my ($cdomain,$cnum)=split(/\//,$courseid);
                   5285:     my $chome=homeserver($cnum,$cdomain);
                   5286:     if (($chome eq '') || ($chome eq 'no_host')) { 
                   5287: 	return 'error: no such course';
                   5288:     }
                   5289:     my $cstring='';
1.800     albertel 5290:     foreach my $pref (keys(%prefs)) {
                   5291: 	$cstring.=&escape($pref).'='.&escape($prefs{$pref}).'&';
1.191     harris41 5292:     }
1.84      www      5293:     $cstring=~s/\&$//;
                   5294:     return reply('put:'.$cdomain.':'.$cnum.':environment:'.$cstring,$chome);
                   5295: }
                   5296: 
                   5297: # ---------------------------------------------------------- Make/modify course
                   5298: 
                   5299: sub createcourse {
1.741     raeburn  5300:     my ($udom,$description,$url,$course_server,$nonstandard,$inst_code,
                   5301:         $course_owner,$crstype)=@_;
1.84      www      5302:     $url=&declutter($url);
                   5303:     my $cid='';
1.264     matthew  5304:     unless (&allowed('ccc',$udom)) {
1.84      www      5305:         return 'refused';
                   5306:     }
                   5307: # ------------------------------------------------------------------- Create ID
1.674     www      5308:    my $uname=int(1+rand(9)).
                   5309:        ('a'..'z','A'..'Z','0'..'9')[int(rand(62))].
                   5310:        substr($$.time,0,5).unpack("H8",pack("I32",time)).
1.84      www      5311:        unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
                   5312: # ----------------------------------------------- Make sure that does not exist
1.230     stredwic 5313:    my $uhome=&homeserver($uname,$udom,'true');
1.84      www      5314:    unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5315:        $uname=substr($$.time,0,5).unpack("H8",pack("I32",time)).
                   5316:         unpack("H2",pack("I32",int(rand(255)))).$perlvar{'lonHostID'};
1.230     stredwic 5317:        $uhome=&homeserver($uname,$udom,'true');       
1.84      www      5318:        unless (($uhome eq '') || ($uhome eq 'no_host')) {
                   5319:            return 'error: unable to generate unique course-ID';
                   5320:        } 
                   5321:    }
1.264     matthew  5322: # ------------------------------------------------ Check supplied server name
1.620     albertel 5323:     $course_server = $env{'user.homeserver'} if (! defined($course_server));
1.845     albertel 5324:     if (! &is_library($course_server)) {
1.264     matthew  5325:         return 'error:bad server name '.$course_server;
                   5326:     }
1.84      www      5327: # ------------------------------------------------------------- Make the course
                   5328:     my $reply=&reply('encrypt:makeuser:'.$udom.':'.$uname.':none::',
1.264     matthew  5329:                       $course_server);
1.84      www      5330:     unless ($reply eq 'ok') { return 'error: '.$reply; }
1.230     stredwic 5331:     $uhome=&homeserver($uname,$udom,'true');
1.84      www      5332:     if (($uhome eq '') || ($uhome eq 'no_host')) { 
                   5333: 	return 'error: no such course';
                   5334:     }
1.271     www      5335: # ----------------------------------------------------------------- Course made
1.516     raeburn  5336: # log existence
                   5337:     &courseidput($udom,&escape($udom.'_'.$uname).'='.&escape($description).
1.741     raeburn  5338:                  ':'.&escape($inst_code).':'.&escape($course_owner).':'.
                   5339:                   &escape($crstype),$uhome);
1.358     www      5340:     &flushcourselogs();
                   5341: # set toplevel url
1.271     www      5342:     my $topurl=$url;
                   5343:     unless ($nonstandard) {
                   5344: # ------------------------------------------ For standard courses, make top url
                   5345:         my $mapurl=&clutter($url);
1.278     www      5346:         if ($mapurl eq '/res/') { $mapurl=''; }
1.620     albertel 5347:         $env{'form.initmap'}=(<<ENDINITMAP);
1.271     www      5348: <map>
                   5349: <resource id="1" type="start"></resource>
                   5350: <resource id="2" src="$mapurl"></resource>
                   5351: <resource id="3" type="finish"></resource>
                   5352: <link index="1" from="1" to="2"></link>
                   5353: <link index="2" from="2" to="3"></link>
                   5354: </map>
                   5355: ENDINITMAP
                   5356:         $topurl=&declutter(
1.638     albertel 5357:         &finishuserfileupload($uname,$udom,'initmap','default.sequence')
1.271     www      5358:                           );
                   5359:     }
                   5360: # ----------------------------------------------------------- Write preferences
1.84      www      5361:     &writecoursepref($udom.'_'.$uname,
                   5362:                      ('description' => $description,
1.271     www      5363:                       'url'         => $topurl));
1.84      www      5364:     return '/'.$udom.'/'.$uname;
                   5365: }
                   5366: 
1.813     albertel 5367: sub is_course {
                   5368:     my ($cdom,$cnum) = @_;
                   5369:     my %courses = &courseiddump($cdom,'.',1,'.','.',$cnum,undef,
                   5370: 				undef,'.');
                   5371:     if (exists($courses{$cdom.'_'.$cnum})) {
                   5372:         return 1;
                   5373:     }
                   5374:     return 0;
                   5375: }
                   5376: 
1.21      www      5377: # ---------------------------------------------------------- Assign Custom Role
                   5378: 
                   5379: sub assigncustomrole {
1.357     www      5380:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start,$deleteflag)=@_;
1.21      www      5381:     return &assignrole($udom,$uname,$url,'cr/'.$rdom.'/'.$rnam.'/'.$rolename,
1.357     www      5382:                        $end,$start,$deleteflag);
1.21      www      5383: }
                   5384: 
                   5385: # ----------------------------------------------------------------- Revoke Role
                   5386: 
                   5387: sub revokerole {
1.357     www      5388:     my ($udom,$uname,$url,$role,$deleteflag)=@_;
1.21      www      5389:     my $now=time;
1.357     www      5390:     return &assignrole($udom,$uname,$url,$role,$now,$deleteflag);
1.21      www      5391: }
                   5392: 
                   5393: # ---------------------------------------------------------- Revoke Custom Role
                   5394: 
                   5395: sub revokecustomrole {
1.357     www      5396:     my ($udom,$uname,$url,$rdom,$rnam,$rolename,$deleteflag)=@_;
1.21      www      5397:     my $now=time;
1.357     www      5398:     return &assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$now,
                   5399:            $deleteflag);
1.17      www      5400: }
                   5401: 
1.533     banghart 5402: # ------------------------------------------------------------ Disk usage
1.535     albertel 5403: sub diskusage {
1.533     banghart 5404:     my ($udom,$uname,$directoryRoot)=@_;
                   5405:     $directoryRoot =~ s/\/$//;
1.535     albertel 5406:     my $listing=&reply('du:'.$directoryRoot,homeserver($uname,$udom));
1.514     albertel 5407:     return $listing;
1.512     banghart 5408: }
                   5409: 
1.566     banghart 5410: sub is_locked {
                   5411:     my ($file_name, $domain, $user) = @_;
                   5412:     my @check;
                   5413:     my $is_locked;
                   5414:     push @check, $file_name;
1.613     albertel 5415:     my %locked = &get('file_permissions',\@check,
1.620     albertel 5416: 		      $env{'user.domain'},$env{'user.name'});
1.615     albertel 5417:     my ($tmp)=keys(%locked);
                   5418:     if ($tmp=~/^error:/) { undef(%locked); }
1.745     raeburn  5419:     
1.566     banghart 5420:     if (ref($locked{$file_name}) eq 'ARRAY') {
1.745     raeburn  5421:         $is_locked = 'false';
                   5422:         foreach my $entry (@{$locked{$file_name}}) {
                   5423:            if (ref($entry) eq 'ARRAY') { 
1.746     raeburn  5424:                $is_locked = 'true';
                   5425:                last;
1.745     raeburn  5426:            }
                   5427:        }
1.566     banghart 5428:     } else {
                   5429:         $is_locked = 'false';
                   5430:     }
                   5431: }
                   5432: 
1.759     albertel 5433: sub declutter_portfile {
                   5434:     my ($file) = @_;
1.833     albertel 5435:     $file =~ s{^(/portfolio/|portfolio/)}{/};
1.759     albertel 5436:     return $file;
                   5437: }
                   5438: 
1.559     banghart 5439: # ------------------------------------------------------------- Mark as Read Only
                   5440: 
                   5441: sub mark_as_readonly {
                   5442:     my ($domain,$user,$files,$what) = @_;
1.613     albertel 5443:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5444:     my ($tmp)=keys(%current_permissions);
                   5445:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.560     banghart 5446:     foreach my $file (@{$files}) {
1.759     albertel 5447: 	$file = &declutter_portfile($file);
1.561     banghart 5448:         push(@{$current_permissions{$file}},$what);
1.559     banghart 5449:     }
1.613     albertel 5450:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5451:     return;
                   5452: }
                   5453: 
1.572     banghart 5454: # ------------------------------------------------------------Save Selected Files
                   5455: 
                   5456: sub save_selected_files {
                   5457:     my ($user, $path, @files) = @_;
                   5458:     my $filename = $user."savedfiles";
1.573     banghart 5459:     my @other_files = &files_not_in_path($user, $path);
1.871     albertel 5460:     open (OUT, '>'.$tmpdir.$filename);
1.573     banghart 5461:     foreach my $file (@files) {
1.620     albertel 5462:         print (OUT $env{'form.currentpath'}.$file."\n");
1.573     banghart 5463:     }
                   5464:     foreach my $file (@other_files) {
1.574     banghart 5465:         print (OUT $file."\n");
1.572     banghart 5466:     }
1.574     banghart 5467:     close (OUT);
1.572     banghart 5468:     return 'ok';
                   5469: }
                   5470: 
1.574     banghart 5471: sub clear_selected_files {
                   5472:     my ($user) = @_;
                   5473:     my $filename = $user."savedfiles";
                   5474:     open (OUT, '>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5475:     print (OUT undef);
                   5476:     close (OUT);
                   5477:     return ("ok");    
                   5478: }
                   5479: 
1.572     banghart 5480: sub files_in_path {
                   5481:     my ($user, $path) = @_;
                   5482:     my $filename = $user."savedfiles";
                   5483:     my %return_files;
1.574     banghart 5484:     open (IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
1.573     banghart 5485:     while (my $line_in = <IN>) {
1.574     banghart 5486:         chomp ($line_in);
                   5487:         my @paths_and_file = split (m!/!, $line_in);
                   5488:         my $file_part = pop (@paths_and_file);
                   5489:         my $path_part = join ('/', @paths_and_file);
1.573     banghart 5490:         $path_part.='/';
                   5491:         my $path_and_file = $path_part.$file_part;
                   5492:         if ($path_part eq $path) {
                   5493:             $return_files{$file_part}= 'selected';
                   5494:         }
                   5495:     }
1.574     banghart 5496:     close (IN);
                   5497:     return (\%return_files);
1.572     banghart 5498: }
                   5499: 
                   5500: # called in portfolio select mode, to show files selected NOT in current directory
                   5501: sub files_not_in_path {
                   5502:     my ($user, $path) = @_;
                   5503:     my $filename = $user."savedfiles";
                   5504:     my @return_files;
                   5505:     my $path_part;
1.800     albertel 5506:     open(IN, '<'.$Apache::lonnet::perlvar{'lonDaemons'}.'/tmp/'.$filename);
                   5507:     while (my $line = <IN>) {
1.572     banghart 5508:         #ok, I know it's clunky, but I want it to work
1.800     albertel 5509:         my @paths_and_file = split(m|/|, $line);
                   5510:         my $file_part = pop(@paths_and_file);
                   5511:         chomp($file_part);
                   5512:         my $path_part = join('/', @paths_and_file);
1.572     banghart 5513:         $path_part .= '/';
                   5514:         my $path_and_file = $path_part.$file_part;
                   5515:         if ($path_part ne $path) {
1.800     albertel 5516:             push(@return_files, ($path_and_file));
1.572     banghart 5517:         }
                   5518:     }
1.800     albertel 5519:     close(OUT);
1.574     banghart 5520:     return (@return_files);
1.572     banghart 5521: }
                   5522: 
1.745     raeburn  5523: #----------------------------------------------Get portfolio file permissions
1.629     banghart 5524: 
1.745     raeburn  5525: sub get_portfile_permissions {
                   5526:     my ($domain,$user) = @_;
1.613     albertel 5527:     my %current_permissions = &dump('file_permissions',$domain,$user);
1.615     albertel 5528:     my ($tmp)=keys(%current_permissions);
                   5529:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5530:     return \%current_permissions;
                   5531: }
                   5532: 
                   5533: #---------------------------------------------Get portfolio file access controls
                   5534: 
1.749     raeburn  5535: sub get_access_controls {
1.745     raeburn  5536:     my ($current_permissions,$group,$file) = @_;
1.769     albertel 5537:     my %access;
                   5538:     my $real_file = $file;
                   5539:     $file =~ s/\.meta$//;
1.745     raeburn  5540:     if (defined($file)) {
1.749     raeburn  5541:         if (ref($$current_permissions{$file."\0".'accesscontrol'}) eq 'HASH') {
                   5542:             foreach my $control (keys(%{$$current_permissions{$file."\0".'accesscontrol'}})) {
1.769     albertel 5543:                 $access{$real_file}{$control} = $$current_permissions{$file."\0".$control};
1.749     raeburn  5544:             }
                   5545:         }
1.745     raeburn  5546:     } else {
1.749     raeburn  5547:         foreach my $key (keys(%{$current_permissions})) {
                   5548:             if ($key =~ /\0accesscontrol$/) {
                   5549:                 if (defined($group)) {
                   5550:                     if ($key !~ m-^\Q$group\E/-) {
                   5551:                         next;
                   5552:                     }
                   5553:                 }
                   5554:                 my ($fullpath) = split(/\0/,$key);
                   5555:                 if (ref($$current_permissions{$key}) eq 'HASH') {
                   5556:                     foreach my $control (keys(%{$$current_permissions{$key}})) {
                   5557:                         $access{$fullpath}{$control}=$$current_permissions{$fullpath."\0".$control};
                   5558:                     }
                   5559:                 }
                   5560:             }
                   5561:         }
                   5562:     }
                   5563:     return %access;
                   5564: }
                   5565: 
                   5566: sub modify_access_controls {
                   5567:     my ($file_name,$changes,$domain,$user)=@_;
                   5568:     my ($outcome,$deloutcome);
                   5569:     my %store_permissions;
                   5570:     my %new_values;
                   5571:     my %new_control;
                   5572:     my %translation;
                   5573:     my @deletions = ();
                   5574:     my $now = time;
                   5575:     if (exists($$changes{'activate'})) {
                   5576:         if (ref($$changes{'activate'}) eq 'HASH') {
                   5577:             my @newitems = sort(keys(%{$$changes{'activate'}}));
                   5578:             my $numnew = scalar(@newitems);
                   5579:             for (my $i=0; $i<$numnew; $i++) {
                   5580:                 my $newkey = $newitems[$i];
                   5581:                 my $newid = &Apache::loncommon::get_cgi_id();
1.797     raeburn  5582:                 if ($newkey =~ /^\d+:/) { 
                   5583:                     $newkey =~ s/^(\d+)/$newid/;
                   5584:                     $translation{$1} = $newid;
                   5585:                 } elsif ($newkey =~ /^\d+_\d+_\d+:/) {
                   5586:                     $newkey =~ s/^(\d+_\d+_\d+)/$newid/;
                   5587:                     $translation{$1} = $newid;
                   5588:                 }
1.749     raeburn  5589:                 $new_values{$file_name."\0".$newkey} = 
                   5590:                                           $$changes{'activate'}{$newitems[$i]};
                   5591:                 $new_control{$newkey} = $now;
                   5592:             }
                   5593:         }
                   5594:     }
                   5595:     my %todelete;
                   5596:     my %changed_items;
                   5597:     foreach my $action ('delete','update') {
                   5598:         if (exists($$changes{$action})) {
                   5599:             if (ref($$changes{$action}) eq 'HASH') {
                   5600:                 foreach my $key (keys(%{$$changes{$action}})) {
                   5601:                     my ($itemnum) = ($key =~ /^([^:]+):/);
                   5602:                     if ($action eq 'delete') { 
                   5603:                         $todelete{$itemnum} = 1;
                   5604:                     } else {
                   5605:                         $changed_items{$itemnum} = $key;
                   5606:                     }
                   5607:                 }
1.745     raeburn  5608:             }
                   5609:         }
1.749     raeburn  5610:     }
                   5611:     # get lock on access controls for file.
                   5612:     my $lockhash = {
                   5613:                   $file_name."\0".'locked_access_records' => $env{'user.name'}.
                   5614:                                                        ':'.$env{'user.domain'},
                   5615:                    }; 
                   5616:     my $tries = 0;
                   5617:     my $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5618:    
                   5619:     while (($gotlock ne 'ok') && $tries <3) {
                   5620:         $tries ++;
                   5621:         sleep 1;
                   5622:         $gotlock = &newput('file_permissions',$lockhash,$domain,$user);
                   5623:     }
                   5624:     if ($gotlock eq 'ok') {
                   5625:         my %curr_permissions = &dump('file_permissions',$domain,$user,$file_name);
                   5626:         my ($tmp)=keys(%curr_permissions);
                   5627:         if ($tmp=~/^error:/) { undef(%curr_permissions); }
                   5628:         if (exists($curr_permissions{$file_name."\0".'accesscontrol'})) {
                   5629:             my $curr_controls = $curr_permissions{$file_name."\0".'accesscontrol'};
                   5630:             if (ref($curr_controls) eq 'HASH') {
                   5631:                 foreach my $control_item (keys(%{$curr_controls})) {
                   5632:                     my ($itemnum) = ($control_item =~ /^([^:]+):/);
                   5633:                     if (defined($todelete{$itemnum})) {
                   5634:                         push(@deletions,$file_name."\0".$control_item);
                   5635:                     } else {
                   5636:                         if (defined($changed_items{$itemnum})) {
                   5637:                             $new_control{$changed_items{$itemnum}} = $now;
                   5638:                             push(@deletions,$file_name."\0".$control_item);
                   5639:                             $new_values{$file_name."\0".$changed_items{$itemnum}} = $$changes{'update'}{$changed_items{$itemnum}};
                   5640:                         } else {
                   5641:                             $new_control{$control_item} = $$curr_controls{$control_item};
                   5642:                         }
                   5643:                     }
1.745     raeburn  5644:                 }
                   5645:             }
                   5646:         }
1.749     raeburn  5647:         $deloutcome = &del('file_permissions',\@deletions,$domain,$user);
                   5648:         $new_values{$file_name."\0".'accesscontrol'} = \%new_control;
                   5649:         $outcome = &put('file_permissions',\%new_values,$domain,$user);
                   5650:         #  remove lock
                   5651:         my @del_lock = ($file_name."\0".'locked_access_records');
                   5652:         my $dellockoutcome = &del('file_permissions',\@del_lock,$domain,$user);
1.818     raeburn  5653:         my ($file,$group);
                   5654:         if (&is_course($domain,$user)) {
                   5655:             ($group,$file) = split(/\//,$file_name,2);
                   5656:         } else {
                   5657:             $file = $file_name;
                   5658:         }
                   5659:         my $sqlresult =
                   5660:             &update_portfolio_table($user,$domain,$file,'portfolio_access',
                   5661:                                     $group);
1.749     raeburn  5662:     } else {
                   5663:         $outcome = "error: could not obtain lockfile\n";  
1.745     raeburn  5664:     }
1.749     raeburn  5665:     return ($outcome,$deloutcome,\%new_values,\%translation);
1.745     raeburn  5666: }
                   5667: 
1.827     raeburn  5668: sub make_public_indefinitely {
                   5669:     my ($requrl) = @_;
                   5670:     my $now = time;
                   5671:     my $action = 'activate';
                   5672:     my $aclnum = 0;
                   5673:     if (&is_portfolio_url($requrl)) {
                   5674:         my (undef,$udom,$unum,$file_name,$group) =
                   5675:             &parse_portfolio_url($requrl);
                   5676:         my $current_perms = &get_portfile_permissions($udom,$unum);
                   5677:         my %access_controls = &get_access_controls($current_perms,
                   5678:                                                    $group,$file_name);
                   5679:         foreach my $key (keys(%{$access_controls{$file_name}})) {
                   5680:             my ($num,$scope,$end,$start) = 
                   5681:                 ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   5682:             if ($scope eq 'public') {
                   5683:                 if ($start <= $now && $end == 0) {
                   5684:                     $action = 'none';
                   5685:                 } else {
                   5686:                     $action = 'update';
                   5687:                     $aclnum = $num;
                   5688:                 }
                   5689:                 last;
                   5690:             }
                   5691:         }
                   5692:         if ($action eq 'none') {
                   5693:              return 'ok';
                   5694:         } else {
                   5695:             my %changes;
                   5696:             my $newend = 0;
                   5697:             my $newstart = $now;
                   5698:             my $newkey = $aclnum.':public_'.$newend.'_'.$newstart;
                   5699:             $changes{$action}{$newkey} = {
                   5700:                 type => 'public',
                   5701:                 time => {
                   5702:                     start => $newstart,
                   5703:                     end   => $newend,
                   5704:                 },
                   5705:             };
                   5706:             my ($outcome,$deloutcome,$new_values,$translation) =
                   5707:                 &modify_access_controls($file_name,\%changes,$udom,$unum);
                   5708:             return $outcome;
                   5709:         }
                   5710:     } else {
                   5711:         return 'invalid';
                   5712:     }
                   5713: }
                   5714: 
1.745     raeburn  5715: #------------------------------------------------------Get Marked as Read Only
                   5716: 
                   5717: sub get_marked_as_readonly {
                   5718:     my ($domain,$user,$what,$group) = @_;
                   5719:     my $current_permissions = &get_portfile_permissions($domain,$user);
1.563     banghart 5720:     my @readonly_files;
1.629     banghart 5721:     my $cmp1=$what;
                   5722:     if (ref($what)) { $cmp1=join('',@{$what}) };
1.745     raeburn  5723:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5724:         if (defined($group)) {
                   5725:             if ($file_name !~ m-^\Q$group\E/-) {
                   5726:                 next;
                   5727:             }
                   5728:         }
1.561     banghart 5729:         if (ref($value) eq "ARRAY"){
                   5730:             foreach my $stored_what (@{$value}) {
1.629     banghart 5731:                 my $cmp2=$stored_what;
1.759     albertel 5732:                 if (ref($stored_what) eq 'ARRAY') {
1.746     raeburn  5733:                     $cmp2=join('',@{$stored_what});
1.745     raeburn  5734:                 }
1.629     banghart 5735:                 if ($cmp1 eq $cmp2) {
1.561     banghart 5736:                     push(@readonly_files, $file_name);
1.745     raeburn  5737:                     last;
1.563     banghart 5738:                 } elsif (!defined($what)) {
                   5739:                     push(@readonly_files, $file_name);
1.745     raeburn  5740:                     last;
1.561     banghart 5741:                 }
                   5742:             }
1.745     raeburn  5743:         }
1.561     banghart 5744:     }
                   5745:     return @readonly_files;
                   5746: }
1.577     banghart 5747: #-----------------------------------------------------------Get Marked as Read Only Hash
1.561     banghart 5748: 
1.577     banghart 5749: sub get_marked_as_readonly_hash {
1.745     raeburn  5750:     my ($current_permissions,$group,$what) = @_;
1.577     banghart 5751:     my %readonly_files;
1.745     raeburn  5752:     while (my ($file_name,$value) = each(%{$current_permissions})) {
                   5753:         if (defined($group)) {
                   5754:             if ($file_name !~ m-^\Q$group\E/-) {
                   5755:                 next;
                   5756:             }
                   5757:         }
1.577     banghart 5758:         if (ref($value) eq "ARRAY"){
                   5759:             foreach my $stored_what (@{$value}) {
1.745     raeburn  5760:                 if (ref($stored_what) eq 'ARRAY') {
1.750     banghart 5761:                     foreach my $lock_descriptor(@{$stored_what}) {
                   5762:                         if ($lock_descriptor eq 'graded') {
                   5763:                             $readonly_files{$file_name} = 'graded';
                   5764:                         } elsif ($lock_descriptor eq 'handback') {
                   5765:                             $readonly_files{$file_name} = 'handback';
                   5766:                         } else {
                   5767:                             if (!exists($readonly_files{$file_name})) {
                   5768:                                 $readonly_files{$file_name} = 'locked';
                   5769:                             }
                   5770:                         }
1.745     raeburn  5771:                     }
1.750     banghart 5772:                 } 
1.577     banghart 5773:             }
                   5774:         } 
                   5775:     }
                   5776:     return %readonly_files;
                   5777: }
1.559     banghart 5778: # ------------------------------------------------------------ Unmark as Read Only
                   5779: 
                   5780: sub unmark_as_readonly {
1.629     banghart 5781:     # unmarks $file_name (if $file_name is defined), or all files locked by $what 
                   5782:     # for portfolio submissions, $what contains [$symb,$crsid] 
1.745     raeburn  5783:     my ($domain,$user,$what,$file_name,$group) = @_;
1.759     albertel 5784:     $file_name = &declutter_portfile($file_name);
1.634     albertel 5785:     my $symb_crs = $what;
                   5786:     if (ref($what)) { $symb_crs=join('',@$what); }
1.745     raeburn  5787:     my %current_permissions = &dump('file_permissions',$domain,$user,$group);
1.615     albertel 5788:     my ($tmp)=keys(%current_permissions);
                   5789:     if ($tmp=~/^error:/) { undef(%current_permissions); }
1.745     raeburn  5790:     my @readonly_files = &get_marked_as_readonly($domain,$user,$what,$group);
1.650     albertel 5791:     foreach my $file (@readonly_files) {
1.759     albertel 5792: 	my $clean_file = &declutter_portfile($file);
                   5793: 	if (defined($file_name) && ($file_name ne $clean_file)) { next; }
1.650     albertel 5794: 	my $current_locks = $current_permissions{$file};
1.563     banghart 5795:         my @new_locks;
                   5796:         my @del_keys;
                   5797:         if (ref($current_locks) eq "ARRAY"){
                   5798:             foreach my $locker (@{$current_locks}) {
1.632     albertel 5799:                 my $compare=$locker;
1.749     raeburn  5800:                 if (ref($locker) eq 'ARRAY') {
1.745     raeburn  5801:                     $compare=join('',@{$locker});
1.746     raeburn  5802:                     if ($compare ne $symb_crs) {
                   5803:                         push(@new_locks, $locker);
                   5804:                     }
1.563     banghart 5805:                 }
                   5806:             }
1.650     albertel 5807:             if (scalar(@new_locks) > 0) {
1.563     banghart 5808:                 $current_permissions{$file} = \@new_locks;
                   5809:             } else {
                   5810:                 push(@del_keys, $file);
1.613     albertel 5811:                 &del('file_permissions',\@del_keys, $domain, $user);
1.650     albertel 5812:                 delete($current_permissions{$file});
1.563     banghart 5813:             }
                   5814:         }
1.561     banghart 5815:     }
1.613     albertel 5816:     &put('file_permissions',\%current_permissions,$domain,$user);
1.559     banghart 5817:     return;
                   5818: }
1.512     banghart 5819: 
1.17      www      5820: # ------------------------------------------------------------ Directory lister
                   5821: 
                   5822: sub dirlist {
1.253     stredwic 5823:     my ($uri,$userdomain,$username,$alternateDirectoryRoot)=@_;
                   5824: 
1.18      www      5825:     $uri=~s/^\///;
                   5826:     $uri=~s/\/$//;
1.253     stredwic 5827:     my ($udom, $uname);
                   5828:     (undef,$udom,$uname)=split(/\//,$uri);
                   5829:     if(defined($userdomain)) {
                   5830:         $udom = $userdomain;
                   5831:     }
                   5832:     if(defined($username)) {
                   5833:         $uname = $username;
                   5834:     }
                   5835: 
                   5836:     my $dirRoot = $perlvar{'lonDocRoot'};
                   5837:     if(defined($alternateDirectoryRoot)) {
                   5838:         $dirRoot = $alternateDirectoryRoot;
                   5839:         $dirRoot =~ s/\/$//;
1.751     banghart 5840:     }
1.253     stredwic 5841: 
                   5842:     if($udom) {
                   5843:         if($uname) {
1.800     albertel 5844:             my $listing = &reply('ls2:'.$dirRoot.'/'.$uri,
                   5845: 				 &homeserver($uname,$udom));
1.605     matthew  5846:             my @listing_results;
                   5847:             if ($listing eq 'unknown_cmd') {
1.800     albertel 5848:                 $listing = &reply('ls:'.$dirRoot.'/'.$uri,
                   5849: 				  &homeserver($uname,$udom));
1.605     matthew  5850:                 @listing_results = split(/:/,$listing);
                   5851:             } else {
                   5852:                 @listing_results = map { &unescape($_); } split(/:/,$listing);
                   5853:             }
                   5854:             return @listing_results;
1.253     stredwic 5855:         } elsif(!defined($alternateDirectoryRoot)) {
1.800     albertel 5856:             my %allusers;
1.841     albertel 5857: 	    my %servers = &get_servers($udom,'library');
                   5858: 	    foreach my $tryserver (keys(%servers)) {
                   5859: 		my $listing = &reply('ls2:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5860: 				     $udom, $tryserver);
                   5861: 		my @listing_results;
                   5862: 		if ($listing eq 'unknown_cmd') {
                   5863: 		    $listing = &reply('ls:'.$perlvar{'lonDocRoot'}.'/res/'.
                   5864: 				      $udom, $tryserver);
                   5865: 		    @listing_results = split(/:/,$listing);
                   5866: 		} else {
                   5867: 		    @listing_results =
                   5868: 			map { &unescape($_); } split(/:/,$listing);
                   5869: 		}
                   5870: 		if ($listing_results[0] ne 'no_such_dir' && 
                   5871: 		    $listing_results[0] ne 'empty'       &&
                   5872: 		    $listing_results[0] ne 'con_lost') {
                   5873: 		    foreach my $line (@listing_results) {
                   5874: 			my ($entry) = split(/&/,$line,2);
                   5875: 			$allusers{$entry} = 1;
                   5876: 		    }
                   5877: 		}
1.253     stredwic 5878:             }
                   5879:             my $alluserstr='';
1.800     albertel 5880:             foreach my $user (sort(keys(%allusers))) {
                   5881:                 $alluserstr.=$user.'&user:';
1.253     stredwic 5882:             }
                   5883:             $alluserstr=~s/:$//;
                   5884:             return split(/:/,$alluserstr);
                   5885:         } else {
1.800     albertel 5886:             return ('missing user name');
1.253     stredwic 5887:         }
                   5888:     } elsif(!defined($alternateDirectoryRoot)) {
1.841     albertel 5889:         my @all_domains = sort(&all_domains());
                   5890:          foreach my $domain (@all_domains) {
                   5891:              $domain = $perlvar{'lonDocRoot'}.'/res/'.$domain.'/&domain';
                   5892:          }
                   5893:          return @all_domains;
                   5894:      } else {
1.800     albertel 5895:         return ('missing domain');
1.275     stredwic 5896:     }
                   5897: }
                   5898: 
                   5899: # --------------------------------------------- GetFileTimestamp
                   5900: # This function utilizes dirlist and returns the date stamp for
                   5901: # when it was last modified.  It will also return an error of -1
                   5902: # if an error occurs
                   5903: 
1.410     matthew  5904: ##
                   5905: ## FIXME: This subroutine assumes its caller knows something about the
                   5906: ## directory structure of the home server for the student ($root).
                   5907: ## Not a good assumption to make.  Since this is for looking up files
                   5908: ## in user directories, the full path should be constructed by lond, not
                   5909: ## whatever machine we request data from.
                   5910: ##
1.275     stredwic 5911: sub GetFileTimestamp {
                   5912:     my ($studentDomain,$studentName,$filename,$root)=@_;
1.807     albertel 5913:     $studentDomain = &LONCAPA::clean_domain($studentDomain);
                   5914:     $studentName   = &LONCAPA::clean_username($studentName);
1.275     stredwic 5915:     my $subdir=$studentName.'__';
                   5916:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                   5917:     my $proname="$studentDomain/$subdir/$studentName";
                   5918:     $proname .= '/'.$filename;
1.375     matthew  5919:     my ($fileStat) = &Apache::lonnet::dirlist($proname, $studentDomain, 
                   5920:                                               $studentName, $root);
1.275     stredwic 5921:     my @stats = split('&', $fileStat);
                   5922:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.375     matthew  5923:         # @stats contains first the filename, then the stat output
                   5924:         return $stats[10]; # so this is 10 instead of 9.
1.275     stredwic 5925:     } else {
                   5926:         return -1;
1.253     stredwic 5927:     }
1.26      www      5928: }
                   5929: 
1.712     albertel 5930: sub stat_file {
                   5931:     my ($uri) = @_;
1.787     albertel 5932:     $uri = &clutter_with_no_wrapper($uri);
1.722     albertel 5933: 
1.712     albertel 5934:     my ($udom,$uname,$file,$dir);
                   5935:     if ($uri =~ m-^/(uploaded|editupload)/-) {
                   5936: 	($udom,$uname,$file) =
1.811     albertel 5937: 	    ($uri =~ m-/(?:uploaded|editupload)/?($match_domain)/?($match_name)/?(.*)-);
1.712     albertel 5938: 	$file = 'userfiles/'.$file;
1.740     www      5939: 	$dir = &propath($udom,$uname);
1.712     albertel 5940:     }
                   5941:     if ($uri =~ m-^/res/-) {
                   5942: 	($udom,$uname) = 
1.807     albertel 5943: 	    ($uri =~ m-/(?:res)/?($match_domain)/?($match_username)/-);
1.712     albertel 5944: 	$file = $uri;
                   5945:     }
                   5946: 
                   5947:     if (!$udom || !$uname || !$file) {
                   5948: 	# unable to handle the uri
                   5949: 	return ();
                   5950:     }
                   5951: 
                   5952:     my ($result) = &dirlist($file,$udom,$uname,$dir);
                   5953:     my @stats = split('&', $result);
1.721     banghart 5954:     
1.712     albertel 5955:     if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
                   5956: 	shift(@stats); #filename is first
                   5957: 	return @stats;
                   5958:     }
                   5959:     return ();
                   5960: }
                   5961: 
1.26      www      5962: # -------------------------------------------------------- Value of a Condition
                   5963: 
1.713     albertel 5964: # gets the value of a specific preevaluated condition
                   5965: #    stored in the string  $env{user.state.<cid>}
                   5966: # or looks up a condition reference in the bighash and if if hasn't
                   5967: # already been evaluated recurses into docondval to get the value of
                   5968: # the condition, then memoizing it to 
                   5969: #   $env{user.state.<cid>.<condition>}
1.40      www      5970: sub directcondval {
                   5971:     my $number=shift;
1.620     albertel 5972:     if (!defined($env{'user.state.'.$env{'request.course.id'}})) {
1.555     albertel 5973: 	&Apache::lonuserstate::evalstate();
                   5974:     }
1.713     albertel 5975:     if (exists($env{'user.state.'.$env{'request.course.id'}.".$number"})) {
                   5976: 	return $env{'user.state.'.$env{'request.course.id'}.".$number"};
                   5977:     } elsif ($number =~ /^_/) {
                   5978: 	my $sub_condition;
                   5979: 	if (tie(my %bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
                   5980: 		&GDBM_READER(),0640)) {
                   5981: 	    $sub_condition=$bighash{'conditions'.$number};
                   5982: 	    untie(%bighash);
                   5983: 	}
                   5984: 	my $value = &docondval($sub_condition);
                   5985: 	&appenv('user.state.'.$env{'request.course.id'}.".$number" => $value);
                   5986: 	return $value;
                   5987:     }
1.620     albertel 5988:     if ($env{'user.state.'.$env{'request.course.id'}}) {
                   5989:        return substr($env{'user.state.'.$env{'request.course.id'}},$number,1);
1.40      www      5990:     } else {
                   5991:        return 2;
                   5992:     }
                   5993: }
                   5994: 
1.713     albertel 5995: # get the collection of conditions for this resource
1.26      www      5996: sub condval {
                   5997:     my $condidx=shift;
1.54      www      5998:     my $allpathcond='';
1.713     albertel 5999:     foreach my $cond (split(/\|/,$condidx)) {
                   6000: 	if (defined($env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond})) {
                   6001: 	    $allpathcond.=
                   6002: 		'('.$env{'acc.cond.'.$env{'request.course.id'}.'.'.$cond}.')|';
                   6003: 	}
1.191     harris41 6004:     }
1.54      www      6005:     $allpathcond=~s/\|$//;
1.713     albertel 6006:     return &docondval($allpathcond);
                   6007: }
                   6008: 
                   6009: #evaluates an expression of conditions
                   6010: sub docondval {
                   6011:     my ($allpathcond) = @_;
                   6012:     my $result=0;
                   6013:     if ($env{'request.course.id'}
                   6014: 	&& defined($allpathcond)) {
                   6015: 	my $operand='|';
                   6016: 	my @stack;
                   6017: 	foreach my $chunk ($allpathcond=~/(\d+|_\d+\.\d+|\(|\)|\&|\|)/g) {
                   6018: 	    if ($chunk eq '(') {
                   6019: 		push @stack,($operand,$result);
                   6020: 	    } elsif ($chunk eq ')') {
                   6021: 		my $before=pop @stack;
                   6022: 		if (pop @stack eq '&') {
                   6023: 		    $result=$result>$before?$before:$result;
                   6024: 		} else {
                   6025: 		    $result=$result>$before?$result:$before;
                   6026: 		}
                   6027: 	    } elsif (($chunk eq '&') || ($chunk eq '|')) {
                   6028: 		$operand=$chunk;
                   6029: 	    } else {
                   6030: 		my $new=directcondval($chunk);
                   6031: 		if ($operand eq '&') {
                   6032: 		    $result=$result>$new?$new:$result;
                   6033: 		} else {
                   6034: 		    $result=$result>$new?$result:$new;
                   6035: 		}
                   6036: 	    }
                   6037: 	}
1.26      www      6038:     }
                   6039:     return $result;
1.421     albertel 6040: }
                   6041: 
                   6042: # ---------------------------------------------------- Devalidate courseresdata
                   6043: 
                   6044: sub devalidatecourseresdata {
                   6045:     my ($coursenum,$coursedomain)=@_;
                   6046:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6047:     &devalidate_cache_new('courseres',$hashid);
1.28      www      6048: }
                   6049: 
1.763     www      6050: 
1.200     www      6051: # --------------------------------------------------- Course Resourcedata Query
1.878     foxr     6052: #
                   6053: #  Parameters:
                   6054: #      $coursenum    - Number of the course.
                   6055: #      $coursedomain - Domain at which the course was created.
                   6056: #  Returns:
                   6057: #     A hash of the course parameters along (I think) with timestamps
                   6058: #     and version info.
1.877     foxr     6059: 
1.624     albertel 6060: sub get_courseresdata {
                   6061:     my ($coursenum,$coursedomain)=@_;
1.200     www      6062:     my $coursehom=&homeserver($coursenum,$coursedomain);
                   6063:     my $hashid=$coursenum.':'.$coursedomain;
1.599     albertel 6064:     my ($result,$cached)=&is_cached_new('courseres',$hashid);
1.624     albertel 6065:     my %dumpreply;
1.417     albertel 6066:     unless (defined($cached)) {
1.624     albertel 6067: 	%dumpreply=&dump('resourcedata',$coursedomain,$coursenum);
1.417     albertel 6068: 	$result=\%dumpreply;
1.251     albertel 6069: 	my ($tmp) = keys(%dumpreply);
                   6070: 	if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
1.599     albertel 6071: 	    &do_cache_new('courseres',$hashid,$result,600);
1.306     albertel 6072: 	} elsif ($tmp =~ /^(con_lost|no_such_host)/) {
                   6073: 	    return $tmp;
1.416     albertel 6074: 	} elsif ($tmp =~ /^(error)/) {
1.417     albertel 6075: 	    $result=undef;
1.599     albertel 6076: 	    &do_cache_new('courseres',$hashid,$result,600);
1.250     albertel 6077: 	}
                   6078:     }
1.624     albertel 6079:     return $result;
                   6080: }
                   6081: 
1.633     albertel 6082: sub devalidateuserresdata {
                   6083:     my ($uname,$udom)=@_;
                   6084:     my $hashid="$udom:$uname";
                   6085:     &devalidate_cache_new('userres',$hashid);
                   6086: }
                   6087: 
1.624     albertel 6088: sub get_userresdata {
                   6089:     my ($uname,$udom)=@_;
                   6090:     #most student don\'t have any data set, check if there is some data
                   6091:     if (&EXT_cache_status($udom,$uname)) { return undef; }
                   6092: 
                   6093:     my $hashid="$udom:$uname";
                   6094:     my ($result,$cached)=&is_cached_new('userres',$hashid);
                   6095:     if (!defined($cached)) {
                   6096: 	my %resourcedata=&dump('resourcedata',$udom,$uname);
                   6097: 	$result=\%resourcedata;
                   6098: 	&do_cache_new('userres',$hashid,$result,600);
                   6099:     }
                   6100:     my ($tmp)=keys(%$result);
                   6101:     if (($tmp!~/^error\:/) && ($tmp!~/^con_lost/)) {
                   6102: 	return $result;
                   6103:     }
                   6104:     #error 2 occurs when the .db doesn't exist
                   6105:     if ($tmp!~/error: 2 /) {
1.672     albertel 6106: 	&logthis("<font color=\"blue\">WARNING:".
1.624     albertel 6107: 		 " Trying to get resource data for ".
                   6108: 		 $uname." at ".$udom.": ".
                   6109: 		 $tmp."</font>");
                   6110:     } elsif ($tmp=~/error: 2 /) {
1.633     albertel 6111: 	#&EXT_cache_set($udom,$uname);
                   6112: 	&do_cache_new('userres',$hashid,undef,600);
1.636     albertel 6113: 	undef($tmp); # not really an error so don't send it back
1.624     albertel 6114:     }
                   6115:     return $tmp;
                   6116: }
1.879     foxr     6117: #----------------------------------------------- resdata - return resource data
                   6118: #  Purpose:
                   6119: #    Return resource data for either users or for a course.
                   6120: #  Parameters:
                   6121: #     $name      - Course/user name.
                   6122: #     $domain    - Name of the domain the user/course is registered on.
                   6123: #     $type      - Type of thing $name is (must be 'course' or 'user'
                   6124: #     @which     - Array of names of resources desired.
                   6125: #  Returns:
                   6126: #     The value of the first reasource in @which that is found in the
                   6127: #     resource hash.
                   6128: #  Exceptional Conditions:
                   6129: #     If the $type passed in is not valid (not the string 'course' or 
                   6130: #     'user', an undefined  reference is returned.
                   6131: #     If none of the resources are found, an undef is returned
1.624     albertel 6132: sub resdata {
                   6133:     my ($name,$domain,$type,@which)=@_;
                   6134:     my $result;
                   6135:     if ($type eq 'course') {
                   6136: 	$result=&get_courseresdata($name,$domain);
                   6137:     } elsif ($type eq 'user') {
                   6138: 	$result=&get_userresdata($name,$domain);
                   6139:     }
                   6140:     if (!ref($result)) { return $result; }    
1.251     albertel 6141:     foreach my $item (@which) {
1.417     albertel 6142: 	if (defined($result->{$item})) {
                   6143: 	    return $result->{$item};
1.251     albertel 6144: 	}
1.250     albertel 6145:     }
1.291     albertel 6146:     return undef;
1.200     www      6147: }
                   6148: 
1.379     matthew  6149: #
                   6150: # EXT resource caching routines
                   6151: #
                   6152: 
                   6153: sub clear_EXT_cache_status {
1.383     albertel 6154:     &delenv('cache.EXT.');
1.379     matthew  6155: }
                   6156: 
                   6157: sub EXT_cache_status {
                   6158:     my ($target_domain,$target_user) = @_;
1.383     albertel 6159:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.620     albertel 6160:     if (exists($env{$cachename}) && ($env{$cachename}+600) > time) {
1.379     matthew  6161:         # We know already the user has no data
                   6162:         return 1;
                   6163:     } else {
                   6164:         return 0;
                   6165:     }
                   6166: }
                   6167: 
                   6168: sub EXT_cache_set {
                   6169:     my ($target_domain,$target_user) = @_;
1.383     albertel 6170:     my $cachename = 'cache.EXT.'.$target_user.'.'.$target_domain;
1.633     albertel 6171:     #&appenv($cachename => time);
1.379     matthew  6172: }
                   6173: 
1.28      www      6174: # --------------------------------------------------------- Value of a Variable
1.58      www      6175: sub EXT {
1.715     albertel 6176: 
1.395     albertel 6177:     my ($varname,$symbparm,$udom,$uname,$usection,$recurse)=@_;
1.68      www      6178:     unless ($varname) { return ''; }
1.218     albertel 6179:     #get real user name/domain, courseid and symb
                   6180:     my $courseid;
1.359     albertel 6181:     my $publicuser;
1.427     www      6182:     if ($symbparm) {
                   6183: 	$symbparm=&get_symb_from_alias($symbparm);
                   6184:     }
1.218     albertel 6185:     if (!($uname && $udom)) {
1.790     albertel 6186:       (my $cursymb,$courseid,$udom,$uname,$publicuser)= &whichuser($symbparm);
1.218     albertel 6187:       if (!$symbparm) {	$symbparm=$cursymb; }
                   6188:     } else {
1.620     albertel 6189: 	$courseid=$env{'request.course.id'};
1.218     albertel 6190:     }
1.48      www      6191:     my ($realm,$space,$qualifier,@therest)=split(/\./,$varname);
                   6192:     my $rest;
1.320     albertel 6193:     if (defined($therest[0])) {
1.48      www      6194:        $rest=join('.',@therest);
                   6195:     } else {
                   6196:        $rest='';
                   6197:     }
1.320     albertel 6198: 
1.57      www      6199:     my $qualifierrest=$qualifier;
                   6200:     if ($rest) { $qualifierrest.='.'.$rest; }
                   6201:     my $spacequalifierrest=$space;
                   6202:     if ($qualifierrest) { $spacequalifierrest.='.'.$qualifierrest; }
1.28      www      6203:     if ($realm eq 'user') {
1.48      www      6204: # --------------------------------------------------------------- user.resource
                   6205: 	if ($space eq 'resource') {
1.651     albertel 6206: 	    if ( (defined($Apache::lonhomework::parsing_a_problem)
                   6207: 		  || defined($Apache::lonhomework::parsing_a_task))
                   6208: 		 &&
1.744     albertel 6209: 		 ($symbparm eq &symbread()) ) {	
                   6210: 		# if we are in the middle of processing the resource the
                   6211: 		# get the value we are planning on committing
                   6212:                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                   6213:                     return $Apache::lonhomework::results{$qualifierrest};
                   6214:                 } else {
                   6215:                     return $Apache::lonhomework::history{$qualifierrest};
                   6216:                 }
1.335     albertel 6217: 	    } else {
1.359     albertel 6218: 		my %restored;
1.620     albertel 6219: 		if ($publicuser || $env{'request.state'} eq 'construct') {
1.359     albertel 6220: 		    %restored=&tmprestore($symbparm,$courseid,$udom,$uname);
                   6221: 		} else {
                   6222: 		    %restored=&restore($symbparm,$courseid,$udom,$uname);
                   6223: 		}
1.335     albertel 6224: 		return $restored{$qualifierrest};
                   6225: 	    }
1.48      www      6226: # ----------------------------------------------------------------- user.access
                   6227:         } elsif ($space eq 'access') {
1.218     albertel 6228: 	    # FIXME - not supporting calls for a specific user
1.48      www      6229:             return &allowed($qualifier,$rest);
                   6230: # ------------------------------------------ user.preferences, user.environment
                   6231:         } elsif (($space eq 'preferences') || ($space eq 'environment')) {
1.620     albertel 6232: 	    if (($uname eq $env{'user.name'}) &&
                   6233: 		($udom eq $env{'user.domain'})) {
                   6234: 		return $env{join('.',('environment',$qualifierrest))};
1.218     albertel 6235: 	    } else {
1.359     albertel 6236: 		my %returnhash;
                   6237: 		if (!$publicuser) {
                   6238: 		    %returnhash=&userenvironment($udom,$uname,
                   6239: 						 $qualifierrest);
                   6240: 		}
1.218     albertel 6241: 		return $returnhash{$qualifierrest};
                   6242: 	    }
1.48      www      6243: # ----------------------------------------------------------------- user.course
                   6244:         } elsif ($space eq 'course') {
1.218     albertel 6245: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6246:             return $env{join('.',('request.course',$qualifier))};
1.48      www      6247: # ------------------------------------------------------------------- user.role
                   6248:         } elsif ($space eq 'role') {
1.218     albertel 6249: 	    # FIXME - not supporting calls for a specific user
1.620     albertel 6250:             my ($role,$where)=split(/\./,$env{'request.role'});
1.48      www      6251:             if ($qualifier eq 'value') {
                   6252: 		return $role;
                   6253:             } elsif ($qualifier eq 'extent') {
                   6254:                 return $where;
                   6255:             }
                   6256: # ----------------------------------------------------------------- user.domain
                   6257:         } elsif ($space eq 'domain') {
1.218     albertel 6258:             return $udom;
1.48      www      6259: # ------------------------------------------------------------------- user.name
                   6260:         } elsif ($space eq 'name') {
1.218     albertel 6261:             return $uname;
1.48      www      6262: # ---------------------------------------------------- Any other user namespace
1.29      www      6263:         } else {
1.359     albertel 6264: 	    my %reply;
                   6265: 	    if (!$publicuser) {
                   6266: 		%reply=&get($space,[$qualifierrest],$udom,$uname);
                   6267: 	    }
                   6268: 	    return $reply{$qualifierrest};
1.48      www      6269:         }
1.236     www      6270:     } elsif ($realm eq 'query') {
                   6271: # ---------------------------------------------- pull stuff out of query string
1.384     albertel 6272:         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   6273: 						[$spacequalifierrest]);
1.620     albertel 6274: 	return $env{'form.'.$spacequalifierrest}; 
1.236     www      6275:    } elsif ($realm eq 'request') {
1.48      www      6276: # ------------------------------------------------------------- request.browser
                   6277:         if ($space eq 'browser') {
1.430     www      6278: 	    if ($qualifier eq 'textremote') {
1.676     albertel 6279: 		if (&Apache::lonlocal::mt('textual_remote_display') eq 'on') {
1.430     www      6280: 		    return 1;
                   6281: 		} else {
                   6282: 		    return 0;
                   6283: 		}
                   6284: 	    } else {
1.620     albertel 6285: 		return $env{'browser.'.$qualifier};
1.430     www      6286: 	    }
1.57      www      6287: # ------------------------------------------------------------ request.filename
                   6288:         } else {
1.620     albertel 6289:             return $env{'request.'.$spacequalifierrest};
1.29      www      6290:         }
1.28      www      6291:     } elsif ($realm eq 'course') {
1.48      www      6292: # ---------------------------------------------------------- course.description
1.620     albertel 6293:         return $env{'course.'.$courseid.'.'.$spacequalifierrest};
1.57      www      6294:     } elsif ($realm eq 'resource') {
1.165     www      6295: 
1.620     albertel 6296: 	if (defined($courseid) && $courseid eq $env{'request.course.id'}) {
1.539     albertel 6297: 	    if (!$symbparm) { $symbparm=&symbread(); }
                   6298: 	}
1.693     albertel 6299: 
                   6300: 	if ($space eq 'title') {
                   6301: 	    if (!$symbparm) { $symbparm = $env{'request.filename'}; }
                   6302: 	    return &gettitle($symbparm);
                   6303: 	}
                   6304: 	
                   6305: 	if ($space eq 'map') {
                   6306: 	    my ($map) = &decode_symb($symbparm);
                   6307: 	    return &symbread($map);
                   6308: 	}
                   6309: 
                   6310: 	my ($section, $group, @groups);
1.593     albertel 6311: 	my ($courselevelm,$courselevel);
1.539     albertel 6312: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6313: 	    $courseid eq $env{'request.course.id'}) {
1.165     www      6314: 
1.218     albertel 6315: 	    #print '<br>'.$space.' - '.$qualifier.' - '.$spacequalifierrest;
1.165     www      6316: 
1.60      www      6317: # ----------------------------------------------------- Cascading lookup scheme
1.218     albertel 6318: 	    my $symbp=$symbparm;
1.735     albertel 6319: 	    my $mapp=&deversion((&decode_symb($symbp))[0]);
1.218     albertel 6320: 
                   6321: 	    my $symbparm=$symbp.'.'.$spacequalifierrest;
                   6322: 	    my $mapparm=$mapp.'___(all).'.$spacequalifierrest;
                   6323: 
1.620     albertel 6324: 	    if (($env{'user.name'} eq $uname) &&
                   6325: 		($env{'user.domain'} eq $udom)) {
                   6326: 		$section=$env{'request.course.sec'};
1.733     raeburn  6327:                 @groups = split(/:/,$env{'request.course.groups'});  
                   6328:                 @groups=&sort_course_groups($courseid,@groups); 
1.218     albertel 6329: 	    } else {
1.539     albertel 6330: 		if (! defined($usection)) {
1.551     albertel 6331: 		    $section=&getsection($udom,$uname,$courseid);
1.539     albertel 6332: 		} else {
                   6333: 		    $section = $usection;
                   6334: 		}
1.733     raeburn  6335:                 @groups = &get_users_groups($udom,$uname,$courseid);
1.218     albertel 6336: 	    }
                   6337: 
                   6338: 	    my $seclevel=$courseid.'.['.$section.'].'.$spacequalifierrest;
                   6339: 	    my $seclevelr=$courseid.'.['.$section.'].'.$symbparm;
                   6340: 	    my $seclevelm=$courseid.'.['.$section.'].'.$mapparm;
                   6341: 
1.593     albertel 6342: 	    $courselevel=$courseid.'.'.$spacequalifierrest;
1.218     albertel 6343: 	    my $courselevelr=$courseid.'.'.$symbparm;
1.593     albertel 6344: 	    $courselevelm=$courseid.'.'.$mapparm;
1.69      www      6345: 
1.60      www      6346: # ----------------------------------------------------------- first, check user
1.624     albertel 6347: 
                   6348: 	    my $userreply=&resdata($uname,$udom,'user',
                   6349: 				       ($courselevelr,$courselevelm,
                   6350: 					$courselevel));
                   6351: 	    if (defined($userreply)) { return $userreply; }
1.95      www      6352: 
1.594     albertel 6353: # ------------------------------------------------ second, check some of course
1.684     raeburn  6354:             my $coursereply;
1.691     raeburn  6355:             if (@groups > 0) {
                   6356:                 $coursereply = &check_group_parms($courseid,\@groups,$symbparm,
                   6357:                                        $mapparm,$spacequalifierrest);
1.684     raeburn  6358:                 if (defined($coursereply)) { return $coursereply; }
                   6359:             }
1.96      www      6360: 
1.684     raeburn  6361: 	    $coursereply=&resdata($env{'course.'.$courseid.'.num'},
1.624     albertel 6362: 				     $env{'course.'.$courseid.'.domain'},
                   6363: 				     'course',
                   6364: 				     ($seclevelr,$seclevelm,$seclevel,
                   6365: 				      $courselevelr));
1.287     albertel 6366: 	    if (defined($coursereply)) { return $coursereply; }
1.200     www      6367: 
1.60      www      6368: # ------------------------------------------------------ third, check map parms
1.218     albertel 6369: 	    my %parmhash=();
                   6370: 	    my $thisparm='';
                   6371: 	    if (tie(%parmhash,'GDBM_File',
1.620     albertel 6372: 		    $env{'request.course.fn'}.'_parms.db',
1.256     albertel 6373: 		    &GDBM_READER(),0640)) {
1.218     albertel 6374: 		$thisparm=$parmhash{$symbparm};
                   6375: 		untie(%parmhash);
                   6376: 	    }
                   6377: 	    if ($thisparm) { return $thisparm; }
                   6378: 	}
1.594     albertel 6379: # ------------------------------------------ fourth, look in resource metadata
1.71      www      6380: 
1.218     albertel 6381: 	$spacequalifierrest=~s/\./\_/;
1.282     albertel 6382: 	my $filename;
                   6383: 	if (!$symbparm) { $symbparm=&symbread(); }
                   6384: 	if ($symbparm) {
1.409     www      6385: 	    $filename=(&decode_symb($symbparm))[2];
1.282     albertel 6386: 	} else {
1.620     albertel 6387: 	    $filename=$env{'request.filename'};
1.282     albertel 6388: 	}
                   6389: 	my $metadata=&metadata($filename,$spacequalifierrest);
1.288     albertel 6390: 	if (defined($metadata)) { return $metadata; }
1.282     albertel 6391: 	$metadata=&metadata($filename,'parameter_'.$spacequalifierrest);
1.288     albertel 6392: 	if (defined($metadata)) { return $metadata; }
1.142     www      6393: 
1.594     albertel 6394: # ---------------------------------------------- fourth, look in rest pf course
1.593     albertel 6395: 	if ($symbparm && defined($courseid) && 
1.620     albertel 6396: 	    $courseid eq $env{'request.course.id'}) {
1.624     albertel 6397: 	    my $coursereply=&resdata($env{'course.'.$courseid.'.num'},
                   6398: 				     $env{'course.'.$courseid.'.domain'},
                   6399: 				     'course',
                   6400: 				     ($courselevelm,$courselevel));
1.593     albertel 6401: 	    if (defined($coursereply)) { return $coursereply; }
                   6402: 	}
1.145     www      6403: # ------------------------------------------------------------------ Cascade up
1.218     albertel 6404: 	unless ($space eq '0') {
1.336     albertel 6405: 	    my @parts=split(/_/,$space);
                   6406: 	    my $id=pop(@parts);
                   6407: 	    my $part=join('_',@parts);
                   6408: 	    if ($part eq '') { $part='0'; }
                   6409: 	    my $partgeneral=&EXT('resource.'.$part.'.'.$qualifierrest,
1.395     albertel 6410: 				 $symbparm,$udom,$uname,$section,1);
1.337     albertel 6411: 	    if (defined($partgeneral)) { return $partgeneral; }
1.218     albertel 6412: 	}
1.395     albertel 6413: 	if ($recurse) { return undef; }
                   6414: 	my $pack_def=&packages_tab_default($filename,$varname);
                   6415: 	if (defined($pack_def)) { return $pack_def; }
1.71      www      6416: 
1.48      www      6417: # ---------------------------------------------------- Any other user namespace
                   6418:     } elsif ($realm eq 'environment') {
                   6419: # ----------------------------------------------------------------- environment
1.620     albertel 6420: 	if (($uname eq $env{'user.name'})&&($udom eq $env{'user.domain'})) {
                   6421: 	    return $env{'environment.'.$spacequalifierrest};
1.219     albertel 6422: 	} else {
1.770     albertel 6423: 	    if ($uname eq 'anonymous' && $udom eq '') {
                   6424: 		return '';
                   6425: 	    }
1.219     albertel 6426: 	    my %returnhash=&userenvironment($udom,$uname,
                   6427: 					    $spacequalifierrest);
                   6428: 	    return $returnhash{$spacequalifierrest};
                   6429: 	}
1.28      www      6430:     } elsif ($realm eq 'system') {
1.48      www      6431: # ----------------------------------------------------------------- system.time
                   6432: 	if ($space eq 'time') {
                   6433: 	    return time;
                   6434:         }
1.696     albertel 6435:     } elsif ($realm eq 'server') {
                   6436: # ----------------------------------------------------------------- system.time
                   6437: 	if ($space eq 'name') {
                   6438: 	    return $ENV{'SERVER_NAME'};
                   6439:         }
1.28      www      6440:     }
1.48      www      6441:     return '';
1.61      www      6442: }
                   6443: 
1.691     raeburn  6444: sub check_group_parms {
                   6445:     my ($courseid,$groups,$symbparm,$mapparm,$what) = @_;
                   6446:     my @groupitems = ();
                   6447:     my $resultitem;
                   6448:     my @levels = ($symbparm,$mapparm,$what);
                   6449:     foreach my $group (@{$groups}) {
                   6450:         foreach my $level (@levels) {
                   6451:              my $item = $courseid.'.['.$group.'].'.$level;
                   6452:              push(@groupitems,$item);
                   6453:         }
                   6454:     }
                   6455:     my $coursereply = &resdata($env{'course.'.$courseid.'.num'},
                   6456:                             $env{'course.'.$courseid.'.domain'},
                   6457:                                      'course',@groupitems);
                   6458:     return $coursereply;
                   6459: }
                   6460: 
                   6461: sub sort_course_groups { # Sort groups based on defined rankings. Default is sort().
1.733     raeburn  6462:     my ($courseid,@groups) = @_;
                   6463:     @groups = sort(@groups);
1.691     raeburn  6464:     return @groups;
                   6465: }
                   6466: 
1.395     albertel 6467: sub packages_tab_default {
                   6468:     my ($uri,$varname)=@_;
                   6469:     my (undef,$part,$name)=split(/\./,$varname);
1.738     albertel 6470: 
                   6471:     my (@extension,@specifics,$do_default);
                   6472:     foreach my $package (split(/,/,&metadata($uri,'packages'))) {
1.395     albertel 6473: 	my ($pack_type,$pack_part)=split(/_/,$package,2);
1.738     albertel 6474: 	if ($pack_type eq 'default') {
                   6475: 	    $do_default=1;
                   6476: 	} elsif ($pack_type eq 'extension') {
                   6477: 	    push(@extension,[$package,$pack_type,$pack_part]);
1.885     albertel 6478: 	} elsif ($pack_part eq $part || $pack_type eq 'part') {
1.848     albertel 6479: 	    # only look at packages defaults for packages that this id is
1.738     albertel 6480: 	    push(@specifics,[$package,$pack_type,$pack_part]);
                   6481: 	}
                   6482:     }
                   6483:     # first look for a package that matches the requested part id
                   6484:     foreach my $package (@specifics) {
                   6485: 	my (undef,$pack_type,$pack_part)=@{$package};
                   6486: 	next if ($pack_part ne $part);
                   6487: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6488: 	    return $packagetab{"$pack_type&$name&default"};
                   6489: 	}
                   6490:     }
                   6491:     # look for any possible matching non extension_ package
                   6492:     foreach my $package (@specifics) {
                   6493: 	my (undef,$pack_type,$pack_part)=@{$package};
1.468     albertel 6494: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6495: 	    return $packagetab{"$pack_type&$name&default"};
                   6496: 	}
1.585     albertel 6497: 	if ($pack_type eq 'part') { $pack_part='0'; }
1.468     albertel 6498: 	if (defined($packagetab{$pack_type."_".$pack_part."&$name&default"})) {
                   6499: 	    return $packagetab{$pack_type."_".$pack_part."&$name&default"};
1.395     albertel 6500: 	}
                   6501:     }
1.738     albertel 6502:     # look for any posible extension_ match
                   6503:     foreach my $package (@extension) {
                   6504: 	my ($package,$pack_type)=@{$package};
                   6505: 	if (defined($packagetab{"$pack_type&$name&default"})) {
                   6506: 	    return $packagetab{"$pack_type&$name&default"};
                   6507: 	}
                   6508: 	if (defined($packagetab{$package."&$name&default"})) {
                   6509: 	    return $packagetab{$package."&$name&default"};
                   6510: 	}
                   6511:     }
                   6512:     # look for a global default setting
                   6513:     if ($do_default && defined($packagetab{"default&$name&default"})) {
                   6514: 	return $packagetab{"default&$name&default"};
                   6515:     }
1.395     albertel 6516:     return undef;
                   6517: }
                   6518: 
1.334     albertel 6519: sub add_prefix_and_part {
                   6520:     my ($prefix,$part)=@_;
                   6521:     my $keyroot;
                   6522:     if (defined($prefix) && $prefix !~ /^__/) {
                   6523: 	# prefix that has a part already
                   6524: 	$keyroot=$prefix;
                   6525:     } elsif (defined($prefix)) {
                   6526: 	# prefix that is missing a part
                   6527: 	if (defined($part)) { $keyroot='_'.$part.substr($prefix,1); }
                   6528:     } else {
                   6529: 	# no prefix at all
                   6530: 	if (defined($part)) { $keyroot='_'.$part; }
                   6531:     }
                   6532:     return $keyroot;
                   6533: }
                   6534: 
1.71      www      6535: # ---------------------------------------------------------------- Get metadata
                   6536: 
1.599     albertel 6537: my %metaentry;
1.71      www      6538: sub metadata {
1.176     www      6539:     my ($uri,$what,$liburi,$prefix,$depthcount)=@_;
1.71      www      6540:     $uri=&declutter($uri);
1.288     albertel 6541:     # if it is a non metadata possible uri return quickly
1.529     albertel 6542:     if (($uri eq '') || 
                   6543: 	(($uri =~ m|^/*adm/|) && 
1.698     albertel 6544: 	     ($uri !~ m|^adm/includes|) && ($uri !~ m|/bulletinboard$|)) ||
1.423     albertel 6545:         ($uri =~ m|/$|) || ($uri =~ m|/.meta$|) || ($uri =~ /^~/) ||
1.807     albertel 6546: 	($uri =~ m|home/$match_username/public_html/|)) {
1.468     albertel 6547: 	return undef;
1.288     albertel 6548:     }
1.73      www      6549:     my $filename=$uri;
                   6550:     $uri=~s/\.meta$//;
1.172     www      6551: #
                   6552: # Is the metadata already cached?
1.177     www      6553: # Look at timestamp of caching
1.172     www      6554: # Everything is cached by the main uri, libraries are never directly cached
                   6555: #
1.428     albertel 6556:     if (!defined($liburi)) {
1.599     albertel 6557: 	my ($result,$cached)=&is_cached_new('meta',$uri);
1.428     albertel 6558: 	if (defined($cached)) { return $result->{':'.$what}; }
                   6559:     }
                   6560:     {
1.172     www      6561: #
                   6562: # Is this a recursive call for a library?
                   6563: #
1.599     albertel 6564: #	if (! exists($metacache{$uri})) {
                   6565: #	    $metacache{$uri}={};
                   6566: #	}
1.171     www      6567:         if ($liburi) {
                   6568: 	    $liburi=&declutter($liburi);
                   6569:             $filename=$liburi;
1.401     bowersj2 6570:         } else {
1.599     albertel 6571: 	    &devalidate_cache_new('meta',$uri);
                   6572: 	    undef(%metaentry);
1.401     bowersj2 6573: 	}
1.140     www      6574:         my %metathesekeys=();
1.73      www      6575:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
1.489     albertel 6576: 	my $metastring;
1.768     albertel 6577: 	if ($uri !~ m -^(editupload)/-) {
1.543     albertel 6578: 	    my $file=&filelocation('',&clutter($filename));
1.599     albertel 6579: 	    #push(@{$metaentry{$uri.'.file'}},$file);
1.543     albertel 6580: 	    $metastring=&getfile($file);
1.489     albertel 6581: 	}
1.208     albertel 6582:         my $parser=HTML::LCParser->new(\$metastring);
1.71      www      6583:         my $token;
1.140     www      6584:         undef %metathesekeys;
1.71      www      6585:         while ($token=$parser->get_token) {
1.339     albertel 6586: 	    if ($token->[0] eq 'S') {
                   6587: 		if (defined($token->[2]->{'package'})) {
1.172     www      6588: #
                   6589: # This is a package - get package info
                   6590: #
1.339     albertel 6591: 		    my $package=$token->[2]->{'package'};
                   6592: 		    my $keyroot=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6593: 		    if (defined($token->[2]->{'id'})) { 
                   6594: 			$keyroot.='_'.$token->[2]->{'id'}; 
                   6595: 		    }
1.599     albertel 6596: 		    if ($metaentry{':packages'}) {
                   6597: 			$metaentry{':packages'}.=','.$package.$keyroot;
1.339     albertel 6598: 		    } else {
1.599     albertel 6599: 			$metaentry{':packages'}=$package.$keyroot;
1.339     albertel 6600: 		    }
1.736     albertel 6601: 		    foreach my $pack_entry (keys(%packagetab)) {
1.432     albertel 6602: 			my $part=$keyroot;
                   6603: 			$part=~s/^\_//;
1.736     albertel 6604: 			if ($pack_entry=~/^\Q$package\E\&/ || 
                   6605: 			    $pack_entry=~/^\Q$package\E_0\&/) {
                   6606: 			    my ($pack,$name,$subp)=split(/\&/,$pack_entry);
1.395     albertel 6607: 			    # ignore package.tab specified default values
                   6608:                             # here &package_tab_default() will fetch those
                   6609: 			    if ($subp eq 'default') { next; }
1.736     albertel 6610: 			    my $value=$packagetab{$pack_entry};
1.432     albertel 6611: 			    my $unikey;
                   6612: 			    if ($pack =~ /_0$/) {
                   6613: 				$unikey='parameter_0_'.$name;
                   6614: 				$part=0;
                   6615: 			    } else {
                   6616: 				$unikey='parameter'.$keyroot.'_'.$name;
                   6617: 			    }
1.339     albertel 6618: 			    if ($subp eq 'display') {
                   6619: 				$value.=' [Part: '.$part.']';
                   6620: 			    }
1.599     albertel 6621: 			    $metaentry{':'.$unikey.'.part'}=$part;
1.395     albertel 6622: 			    $metathesekeys{$unikey}=1;
1.599     albertel 6623: 			    unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6624: 				$metaentry{':'.$unikey.'.'.$subp}=$value;
1.339     albertel 6625: 			    }
1.599     albertel 6626: 			    if (defined($metaentry{':'.$unikey.'.default'})) {
                   6627: 				$metaentry{':'.$unikey}=
                   6628: 				    $metaentry{':'.$unikey.'.default'};
1.356     albertel 6629: 			    }
1.339     albertel 6630: 			}
                   6631: 		    }
                   6632: 		} else {
1.172     www      6633: #
                   6634: # This is not a package - some other kind of start tag
1.339     albertel 6635: #
                   6636: 		    my $entry=$token->[1];
                   6637: 		    my $unikey;
                   6638: 		    if ($entry eq 'import') {
                   6639: 			$unikey='';
                   6640: 		    } else {
                   6641: 			$unikey=$entry;
                   6642: 		    }
                   6643: 		    $unikey.=&add_prefix_and_part($prefix,$token->[2]->{'part'});
                   6644: 
                   6645: 		    if (defined($token->[2]->{'id'})) { 
                   6646: 			$unikey.='_'.$token->[2]->{'id'}; 
                   6647: 		    }
1.175     www      6648: 
1.339     albertel 6649: 		    if ($entry eq 'import') {
1.175     www      6650: #
                   6651: # Importing a library here
1.339     albertel 6652: #
                   6653: 			if ($depthcount<20) {
                   6654: 			    my $location=$parser->get_text('/import');
                   6655: 			    my $dir=$filename;
                   6656: 			    $dir=~s|[^/]*$||;
                   6657: 			    $location=&filelocation($dir,$location);
1.736     albertel 6658: 			    my $metadata = 
                   6659: 				&metadata($uri,'keys', $location,$unikey,
                   6660: 					  $depthcount+1);
                   6661: 			    foreach my $meta (split(',',$metadata)) {
                   6662: 				$metaentry{':'.$meta}=$metaentry{':'.$meta};
                   6663: 				$metathesekeys{$meta}=1;
1.339     albertel 6664: 			    }
                   6665: 			}
                   6666: 		    } else { 
                   6667: 			
                   6668: 			if (defined($token->[2]->{'name'})) { 
                   6669: 			    $unikey.='_'.$token->[2]->{'name'}; 
                   6670: 			}
                   6671: 			$metathesekeys{$unikey}=1;
1.736     albertel 6672: 			foreach my $param (@{$token->[3]}) {
                   6673: 			    $metaentry{':'.$unikey.'.'.$param} =
                   6674: 				$token->[2]->{$param};
1.339     albertel 6675: 			}
                   6676: 			my $internaltext=&HTML::Entities::decode($parser->get_text('/'.$entry));
1.599     albertel 6677: 			my $default=$metaentry{':'.$unikey.'.default'};
1.339     albertel 6678: 			if ( $internaltext =~ /^\s*$/ && $default !~ /^\s*$/) {
                   6679: 		 # only ws inside the tag, and not in default, so use default
                   6680: 		 # as value
1.599     albertel 6681: 			    $metaentry{':'.$unikey}=$default;
1.339     albertel 6682: 			} else {
1.321     albertel 6683: 		  # either something interesting inside the tag or default
                   6684:                   # uninteresting
1.599     albertel 6685: 			    $metaentry{':'.$unikey}=$internaltext;
1.339     albertel 6686: 			}
1.172     www      6687: # end of not-a-package not-a-library import
1.339     albertel 6688: 		    }
1.172     www      6689: # end of not-a-package start tag
1.339     albertel 6690: 		}
1.172     www      6691: # the next is the end of "start tag"
1.339     albertel 6692: 	    }
                   6693: 	}
1.483     albertel 6694: 	my ($extension) = ($uri =~ /\.(\w+)$/);
1.883     albertel 6695: 	$extension = lc($extension);
                   6696: 	if ($extension eq 'htm') { $extension='html'; }
                   6697: 
1.737     albertel 6698: 	foreach my $key (keys(%packagetab)) {
1.483     albertel 6699: 	    #no specific packages #how's our extension
                   6700: 	    if ($key!~/^extension_\Q$extension\E&/) { next; }
1.488     albertel 6701: 	    &metadata_create_package_def($uri,$key,'extension_'.$extension,
1.483     albertel 6702: 					 \%metathesekeys);
                   6703: 	}
1.883     albertel 6704: 
                   6705: 	if (!exists($metaentry{':packages'})
                   6706: 	    || $packagetab{"import_defaults&extension_$extension"}) {
1.737     albertel 6707: 	    foreach my $key (keys(%packagetab)) {
1.483     albertel 6708: 		#no specific packages well let's get default then
                   6709: 		if ($key!~/^default&/) { next; }
1.488     albertel 6710: 		&metadata_create_package_def($uri,$key,'default',
1.483     albertel 6711: 					     \%metathesekeys);
                   6712: 	    }
                   6713: 	}
1.338     www      6714: # are there custom rights to evaluate
1.599     albertel 6715: 	if ($metaentry{':copyright'} eq 'custom') {
1.339     albertel 6716: 
1.338     www      6717:     #
                   6718:     # Importing a rights file here
1.339     albertel 6719:     #
                   6720: 	    unless ($depthcount) {
1.599     albertel 6721: 		my $location=$metaentry{':customdistributionfile'};
1.339     albertel 6722: 		my $dir=$filename;
                   6723: 		$dir=~s|[^/]*$||;
                   6724: 		$location=&filelocation($dir,$location);
1.736     albertel 6725: 		my $rights_metadata =
                   6726: 		    &metadata($uri,'keys',$location,'_rights',
                   6727: 			      $depthcount+1);
                   6728: 		foreach my $rights (split(',',$rights_metadata)) {
                   6729: 		    #$metaentry{':'.$rights}=$metacache{$uri}->{':'.$rights};
                   6730: 		    $metathesekeys{$rights}=1;
1.339     albertel 6731: 		}
                   6732: 	    }
                   6733: 	}
1.737     albertel 6734: 	# uniqifiy package listing
                   6735: 	my %seen;
                   6736: 	my @uniq_packages =
                   6737: 	    grep { ! $seen{$_} ++ } (split(',',$metaentry{':packages'}));
                   6738: 	$metaentry{':packages'} = join(',',@uniq_packages);
                   6739: 
                   6740: 	$metaentry{':keys'} = join(',',keys(%metathesekeys));
1.599     albertel 6741: 	&metadata_generate_part0(\%metathesekeys,\%metaentry,$uri);
                   6742: 	$metaentry{':allpossiblekeys'}=join(',',keys %metathesekeys);
1.699     albertel 6743: 	&do_cache_new('meta',$uri,\%metaentry,60*60);
1.177     www      6744: # this is the end of "was not already recently cached
1.71      www      6745:     }
1.599     albertel 6746:     return $metaentry{':'.$what};
1.261     albertel 6747: }
                   6748: 
1.488     albertel 6749: sub metadata_create_package_def {
1.483     albertel 6750:     my ($uri,$key,$package,$metathesekeys)=@_;
                   6751:     my ($pack,$name,$subp)=split(/\&/,$key);
                   6752:     if ($subp eq 'default') { next; }
                   6753:     
1.599     albertel 6754:     if (defined($metaentry{':packages'})) {
                   6755: 	$metaentry{':packages'}.=','.$package;
1.483     albertel 6756:     } else {
1.599     albertel 6757: 	$metaentry{':packages'}=$package;
1.483     albertel 6758:     }
                   6759:     my $value=$packagetab{$key};
                   6760:     my $unikey;
                   6761:     $unikey='parameter_0_'.$name;
1.599     albertel 6762:     $metaentry{':'.$unikey.'.part'}=0;
1.483     albertel 6763:     $$metathesekeys{$unikey}=1;
1.599     albertel 6764:     unless (defined($metaentry{':'.$unikey.'.'.$subp})) {
                   6765: 	$metaentry{':'.$unikey.'.'.$subp}=$value;
1.483     albertel 6766:     }
1.599     albertel 6767:     if (defined($metaentry{':'.$unikey.'.default'})) {
                   6768: 	$metaentry{':'.$unikey}=
                   6769: 	    $metaentry{':'.$unikey.'.default'};
1.483     albertel 6770:     }
                   6771: }
                   6772: 
1.261     albertel 6773: sub metadata_generate_part0 {
                   6774:     my ($metadata,$metacache,$uri) = @_;
                   6775:     my %allnames;
1.737     albertel 6776:     foreach my $metakey (keys(%$metadata)) {
1.261     albertel 6777: 	if ($metakey=~/^parameter\_(.*)/) {
1.428     albertel 6778: 	  my $part=$$metacache{':'.$metakey.'.part'};
                   6779: 	  my $name=$$metacache{':'.$metakey.'.name'};
1.356     albertel 6780: 	  if (! exists($$metadata{'parameter_0_'.$name.'.name'})) {
1.261     albertel 6781: 	    $allnames{$name}=$part;
                   6782: 	  }
                   6783: 	}
                   6784:     }
                   6785:     foreach my $name (keys(%allnames)) {
                   6786:       $$metadata{"parameter_0_$name"}=1;
1.428     albertel 6787:       my $key=":parameter_0_$name";
1.261     albertel 6788:       $$metacache{"$key.part"}='0';
                   6789:       $$metacache{"$key.name"}=$name;
1.428     albertel 6790:       $$metacache{"$key.type"}=$$metacache{':parameter_'.
1.261     albertel 6791: 					   $allnames{$name}.'_'.$name.
                   6792: 					   '.type'};
1.428     albertel 6793:       my $olddis=$$metacache{':parameter_'.$allnames{$name}.'_'.$name.
1.261     albertel 6794: 			     '.display'};
1.644     www      6795:       my $expr='[Part: '.$allnames{$name}.']';
1.479     albertel 6796:       $olddis=~s/\Q$expr\E/\[Part: 0\]/;
1.261     albertel 6797:       $$metacache{"$key.display"}=$olddis;
                   6798:     }
1.71      www      6799: }
                   6800: 
1.764     albertel 6801: # ------------------------------------------------------ Devalidate title cache
                   6802: 
                   6803: sub devalidate_title_cache {
                   6804:     my ($url)=@_;
                   6805:     if (!$env{'request.course.id'}) { return; }
                   6806:     my $symb=&symbread($url);
                   6807:     if (!$symb) { return; }
                   6808:     my $key=$env{'request.course.id'}."\0".$symb;
                   6809:     &devalidate_cache_new('title',$key);
                   6810: }
                   6811: 
1.301     www      6812: # ------------------------------------------------- Get the title of a resource
                   6813: 
                   6814: sub gettitle {
                   6815:     my $urlsymb=shift;
                   6816:     my $symb=&symbread($urlsymb);
1.534     albertel 6817:     if ($symb) {
1.620     albertel 6818: 	my $key=$env{'request.course.id'}."\0".$symb;
1.599     albertel 6819: 	my ($result,$cached)=&is_cached_new('title',$key);
1.575     albertel 6820: 	if (defined($cached)) { 
                   6821: 	    return $result;
                   6822: 	}
1.534     albertel 6823: 	my ($map,$resid,$url)=&decode_symb($symb);
                   6824: 	my $title='';
                   6825: 	my %bighash;
1.620     albertel 6826: 	if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.534     albertel 6827: 		&GDBM_READER(),0640)) {
                   6828: 	    my $mapid=$bighash{'map_pc_'.&clutter($map)};
                   6829: 	    $title=$bighash{'title_'.$mapid.'.'.$resid};
                   6830: 	    untie %bighash;
                   6831: 	}
                   6832: 	$title=~s/\&colon\;/\:/gs;
                   6833: 	if ($title) {
1.599     albertel 6834: 	    return &do_cache_new('title',$key,$title,600);
1.534     albertel 6835: 	}
                   6836: 	$urlsymb=$url;
                   6837:     }
                   6838:     my $title=&metadata($urlsymb,'title');
                   6839:     if (!$title) { $title=(split('/',$urlsymb))[-1]; }    
                   6840:     return $title;
1.301     www      6841: }
1.613     albertel 6842: 
1.614     albertel 6843: sub get_slot {
                   6844:     my ($which,$cnum,$cdom)=@_;
                   6845:     if (!$cnum || !$cdom) {
1.790     albertel 6846: 	(undef,my $courseid)=&whichuser();
1.620     albertel 6847: 	$cdom=$env{'course.'.$courseid.'.domain'};
                   6848: 	$cnum=$env{'course.'.$courseid.'.num'};
1.614     albertel 6849:     }
1.703     albertel 6850:     my $key=join("\0",'slots',$cdom,$cnum,$which);
                   6851:     my %slotinfo;
                   6852:     if (exists($remembered{$key})) {
                   6853: 	$slotinfo{$which} = $remembered{$key};
                   6854:     } else {
                   6855: 	%slotinfo=&get('slots',[$which],$cdom,$cnum);
                   6856: 	&Apache::lonhomework::showhash(%slotinfo);
                   6857: 	my ($tmp)=keys(%slotinfo);
                   6858: 	if ($tmp=~/^error:/) { return (); }
                   6859: 	$remembered{$key} = $slotinfo{$which};
                   6860:     }
1.616     albertel 6861:     if (ref($slotinfo{$which}) eq 'HASH') {
                   6862: 	return %{$slotinfo{$which}};
                   6863:     }
                   6864:     return $slotinfo{$which};
1.614     albertel 6865: }
1.31      www      6866: # ------------------------------------------------- Update symbolic store links
                   6867: 
                   6868: sub symblist {
                   6869:     my ($mapname,%newhash)=@_;
1.438     www      6870:     $mapname=&deversion(&declutter($mapname));
1.31      www      6871:     my %hash;
1.620     albertel 6872:     if (($env{'request.course.fn'}) && (%newhash)) {
                   6873:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 6874:                       &GDBM_WRCREAT(),0640)) {
1.711     albertel 6875: 	    foreach my $url (keys %newhash) {
                   6876: 		next if ($url eq 'last_known'
                   6877: 			 && $env{'form.no_update_last_known'});
                   6878: 		$hash{declutter($url)}=&encode_symb($mapname,
                   6879: 						    $newhash{$url}->[1],
                   6880: 						    $newhash{$url}->[0]);
1.191     harris41 6881:             }
1.31      www      6882:             if (untie(%hash)) {
                   6883: 		return 'ok';
                   6884:             }
                   6885:         }
                   6886:     }
                   6887:     return 'error';
1.212     www      6888: }
                   6889: 
                   6890: # --------------------------------------------------------------- Verify a symb
                   6891: 
                   6892: sub symbverify {
1.510     www      6893:     my ($symb,$thisurl)=@_;
                   6894:     my $thisfn=$thisurl;
1.439     www      6895:     $thisfn=&declutter($thisfn);
1.215     www      6896: # direct jump to resource in page or to a sequence - will construct own symbs
                   6897:     if ($thisfn=~/\.(page|sequence)$/) { return 1; }
                   6898: # check URL part
1.409     www      6899:     my ($map,$resid,$url)=&decode_symb($symb);
1.439     www      6900: 
1.431     www      6901:     unless ($url eq $thisfn) { return 0; }
1.213     www      6902: 
1.216     www      6903:     $symb=&symbclean($symb);
1.510     www      6904:     $thisurl=&deversion($thisurl);
1.439     www      6905:     $thisfn=&deversion($thisfn);
1.213     www      6906: 
                   6907:     my %bighash;
                   6908:     my $okay=0;
1.431     www      6909: 
1.620     albertel 6910:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 6911:                             &GDBM_READER(),0640)) {
1.510     www      6912:         my $ids=$bighash{'ids_'.&clutter($thisurl)};
1.216     www      6913:         unless ($ids) { 
1.510     www      6914:            $ids=$bighash{'ids_/'.$thisurl};
1.216     www      6915:         }
                   6916:         if ($ids) {
                   6917: # ------------------------------------------------------------------- Has ID(s)
1.800     albertel 6918: 	    foreach my $id (split(/\,/,$ids)) {
                   6919: 	       my ($mapid,$resid)=split(/\./,$id);
1.216     www      6920:                if (
                   6921:   &symbclean(&declutter($bighash{'map_id_'.$mapid}).'___'.$resid.'___'.$thisfn)
                   6922:    eq $symb) { 
1.620     albertel 6923: 		   if (($env{'request.role.adv'}) ||
1.800     albertel 6924: 		       $bighash{'encrypted_'.$id} eq $env{'request.enc'}) {
1.582     albertel 6925: 		       $okay=1; 
                   6926: 		   }
                   6927: 	       }
1.216     www      6928: 	   }
                   6929:         }
1.213     www      6930: 	untie(%bighash);
                   6931:     }
                   6932:     return $okay;
1.31      www      6933: }
                   6934: 
1.210     www      6935: # --------------------------------------------------------------- Clean-up symb
                   6936: 
                   6937: sub symbclean {
                   6938:     my $symb=shift;
1.568     albertel 6939:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
1.210     www      6940: # remove version from map
                   6941:     $symb=~s/\.(\d+)\.(\w+)\_\_\_/\.$2\_\_\_/;
1.215     www      6942: 
1.210     www      6943: # remove version from URL
                   6944:     $symb=~s/\.(\d+)\.(\w+)$/\.$2/;
1.213     www      6945: 
1.507     www      6946: # remove wrapper
                   6947: 
1.510     www      6948:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/wrapper\/(res\/)*/$1/;
1.694     albertel 6949:     $symb=~s/(\_\_\_\d+\_\_\_)adm\/coursedocs\/showdoc\/(res\/)*/$1/;
1.210     www      6950:     return $symb;
1.409     www      6951: }
                   6952: 
                   6953: # ---------------------------------------------- Split symb to find map and url
1.429     albertel 6954: 
                   6955: sub encode_symb {
                   6956:     my ($map,$resid,$url)=@_;
                   6957:     return &symbclean(&declutter($map).'___'.$resid.'___'.&declutter($url));
                   6958: }
1.409     www      6959: 
                   6960: sub decode_symb {
1.568     albertel 6961:     my $symb=shift;
                   6962:     if ($symb=~m|^/enc/|) { $symb=&Apache::lonenc::unencrypted($symb); }
                   6963:     my ($map,$resid,$url)=split(/___/,$symb);
1.413     www      6964:     return (&fixversion($map),$resid,&fixversion($url));
                   6965: }
                   6966: 
                   6967: sub fixversion {
                   6968:     my $fn=shift;
1.609     banghart 6969:     if ($fn=~/^(adm|uploaded|editupload|public)/) { return $fn; }
1.435     www      6970:     my %bighash;
                   6971:     my $uri=&clutter($fn);
1.620     albertel 6972:     my $key=$env{'request.course.id'}.'_'.$uri;
1.440     www      6973: # is this cached?
1.599     albertel 6974:     my ($result,$cached)=&is_cached_new('courseresversion',$key);
1.440     www      6975:     if (defined($cached)) { return $result; }
                   6976: # unfortunately not cached, or expired
1.620     albertel 6977:     if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.440     www      6978: 	    &GDBM_READER(),0640)) {
                   6979:  	if ($bighash{'version_'.$uri}) {
                   6980:  	    my $version=$bighash{'version_'.$uri};
1.444     www      6981:  	    unless (($version eq 'mostrecent') || 
                   6982: 		    ($version==&getversion($uri))) {
1.440     www      6983:  		$uri=~s/\.(\w+)$/\.$version\.$1/;
                   6984:  	    }
                   6985:  	}
                   6986:  	untie %bighash;
1.413     www      6987:     }
1.599     albertel 6988:     return &do_cache_new('courseresversion',$key,&declutter($uri),600);
1.438     www      6989: }
                   6990: 
                   6991: sub deversion {
                   6992:     my $url=shift;
                   6993:     $url=~s/\.\d+\.(\w+)$/\.$1/;
                   6994:     return $url;
1.210     www      6995: }
                   6996: 
1.31      www      6997: # ------------------------------------------------------ Return symb list entry
                   6998: 
                   6999: sub symbread {
1.249     www      7000:     my ($thisfn,$donotrecurse)=@_;
1.542     albertel 7001:     my $cache_str='request.symbread.cached.'.$thisfn;
1.620     albertel 7002:     if (defined($env{$cache_str})) { return $env{$cache_str}; }
1.242     www      7003: # no filename provided? try from environment
1.44      www      7004:     unless ($thisfn) {
1.620     albertel 7005:         if ($env{'request.symb'}) {
                   7006: 	    return $env{$cache_str}=&symbclean($env{'request.symb'});
1.539     albertel 7007: 	}
1.620     albertel 7008: 	$thisfn=$env{'request.filename'};
1.44      www      7009:     }
1.569     albertel 7010:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.242     www      7011: # is that filename actually a symb? Verify, clean, and return
                   7012:     if ($thisfn=~/\_\_\_\d+\_\_\_(.*)$/) {
1.539     albertel 7013: 	if (&symbverify($thisfn,$1)) {
1.620     albertel 7014: 	    return $env{$cache_str}=&symbclean($thisfn);
1.539     albertel 7015: 	}
1.242     www      7016:     }
1.44      www      7017:     $thisfn=declutter($thisfn);
1.31      www      7018:     my %hash;
1.37      www      7019:     my %bighash;
                   7020:     my $syval='';
1.620     albertel 7021:     if (($env{'request.course.fn'}) && ($thisfn)) {
1.481     raeburn  7022:         my $targetfn = $thisfn;
1.609     banghart 7023:         if ( ($thisfn =~ m/^(uploaded|editupload)\//) && ($thisfn !~ m/\.(page|sequence)$/) ) {
1.481     raeburn  7024:             $targetfn = 'adm/wrapper/'.$thisfn;
                   7025:         }
1.687     albertel 7026: 	if ($targetfn =~ m|^adm/wrapper/(ext/.*)|) {
                   7027: 	    $targetfn=$1;
                   7028: 	}
1.620     albertel 7029:         if (tie(%hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
1.256     albertel 7030:                       &GDBM_READER(),0640)) {
1.481     raeburn  7031: 	    $syval=$hash{$targetfn};
1.37      www      7032:             untie(%hash);
                   7033:         }
                   7034: # ---------------------------------------------------------- There was an entry
                   7035:         if ($syval) {
1.601     albertel 7036: 	    #unless ($syval=~/\_\d+$/) {
1.620     albertel 7037: 		#unless ($env{'form.request.prefix'}=~/\.(\d+)\_$/) {
1.601     albertel 7038: 		    #&appenv('request.ambiguous' => $thisfn);
1.620     albertel 7039: 		    #return $env{$cache_str}='';
1.601     albertel 7040: 		#}    
                   7041: 		#$syval.=$1;
                   7042: 	    #}
1.37      www      7043:         } else {
                   7044: # ------------------------------------------------------- Was not in symb table
1.620     albertel 7045:            if (tie(%bighash,'GDBM_File',$env{'request.course.fn'}.'.db',
1.256     albertel 7046:                             &GDBM_READER(),0640)) {
1.37      www      7047: # ---------------------------------------------- Get ID(s) for current resource
1.280     www      7048:               my $ids=$bighash{'ids_'.&clutter($thisfn)};
1.65      www      7049:               unless ($ids) { 
                   7050:                  $ids=$bighash{'ids_/'.$thisfn};
1.242     www      7051:               }
                   7052:               unless ($ids) {
                   7053: # alias?
                   7054: 		  $ids=$bighash{'mapalias_'.$thisfn};
1.65      www      7055:               }
1.37      www      7056:               if ($ids) {
                   7057: # ------------------------------------------------------------------- Has ID(s)
                   7058:                  my @possibilities=split(/\,/,$ids);
1.39      www      7059:                  if ($#possibilities==0) {
                   7060: # ----------------------------------------------- There is only one possibility
1.37      www      7061: 		     my ($mapid,$resid)=split(/\./,$ids);
1.626     albertel 7062: 		     $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7063: 						    $resid,$thisfn);
1.249     www      7064:                  } elsif (!$donotrecurse) {
1.39      www      7065: # ------------------------------------------ There is more than one possibility
                   7066:                      my $realpossible=0;
1.800     albertel 7067:                      foreach my $id (@possibilities) {
                   7068: 			 my $file=$bighash{'src_'.$id};
1.39      www      7069:                          if (&allowed('bre',$file)) {
1.800     albertel 7070:          		    my ($mapid,$resid)=split(/\./,$id);
1.39      www      7071:                             if ($bighash{'map_type_'.$mapid} ne 'page') {
                   7072: 				$realpossible++;
1.626     albertel 7073:                                 $syval=&encode_symb($bighash{'map_id_'.$mapid},
                   7074: 						    $resid,$thisfn);
1.39      www      7075:                             }
                   7076: 			 }
1.191     harris41 7077:                      }
1.39      www      7078: 		     if ($realpossible!=1) { $syval=''; }
1.249     www      7079:                  } else {
                   7080:                      $syval='';
1.37      www      7081:                  }
                   7082: 	      }
                   7083:               untie(%bighash)
1.481     raeburn  7084:            }
1.31      www      7085:         }
1.62      www      7086:         if ($syval) {
1.620     albertel 7087: 	    return $env{$cache_str}=$syval;
1.62      www      7088:         }
1.31      www      7089:     }
1.44      www      7090:     &appenv('request.ambiguous' => $thisfn);
1.620     albertel 7091:     return $env{$cache_str}='';
1.31      www      7092: }
                   7093: 
                   7094: # ---------------------------------------------------------- Return random seed
                   7095: 
1.32      www      7096: sub numval {
                   7097:     my $txt=shift;
                   7098:     $txt=~tr/A-J/0-9/;
                   7099:     $txt=~tr/a-j/0-9/;
                   7100:     $txt=~tr/K-T/0-9/;
                   7101:     $txt=~tr/k-t/0-9/;
                   7102:     $txt=~tr/U-Z/0-5/;
                   7103:     $txt=~tr/u-z/0-5/;
                   7104:     $txt=~s/\D//g;
1.564     albertel 7105:     if ($_64bit) { if ($txt > 2**32) { return -1; } }
1.32      www      7106:     return int($txt);
1.368     albertel 7107: }
                   7108: 
1.484     albertel 7109: sub numval2 {
                   7110:     my $txt=shift;
                   7111:     $txt=~tr/A-J/0-9/;
                   7112:     $txt=~tr/a-j/0-9/;
                   7113:     $txt=~tr/K-T/0-9/;
                   7114:     $txt=~tr/k-t/0-9/;
                   7115:     $txt=~tr/U-Z/0-5/;
                   7116:     $txt=~tr/u-z/0-5/;
                   7117:     $txt=~s/\D//g;
                   7118:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7119:     my $total;
                   7120:     foreach my $val (@txts) { $total+=$val; }
1.564     albertel 7121:     if ($_64bit) { if ($total > 2**32) { return -1; } }
1.484     albertel 7122:     return int($total);
                   7123: }
                   7124: 
1.575     albertel 7125: sub numval3 {
                   7126:     use integer;
                   7127:     my $txt=shift;
                   7128:     $txt=~tr/A-J/0-9/;
                   7129:     $txt=~tr/a-j/0-9/;
                   7130:     $txt=~tr/K-T/0-9/;
                   7131:     $txt=~tr/k-t/0-9/;
                   7132:     $txt=~tr/U-Z/0-5/;
                   7133:     $txt=~tr/u-z/0-5/;
                   7134:     $txt=~s/\D//g;
                   7135:     my @txts=split(/(\d\d\d\d\d\d\d\d\d)/,$txt);
                   7136:     my $total;
                   7137:     foreach my $val (@txts) { $total+=$val; }
                   7138:     if ($_64bit) { $total=(($total<<32)>>32); }
                   7139:     return $total;
                   7140: }
                   7141: 
1.675     albertel 7142: sub digest {
                   7143:     my ($data)=@_;
                   7144:     my $digest=&Digest::MD5::md5($data);
                   7145:     my ($a,$b,$c,$d)=unpack("iiii",$digest);
                   7146:     my ($e,$f);
                   7147:     {
                   7148:         use integer;
                   7149:         $e=($a+$b);
                   7150:         $f=($c+$d);
                   7151:         if ($_64bit) {
                   7152:             $e=(($e<<32)>>32);
                   7153:             $f=(($f<<32)>>32);
                   7154:         }
                   7155:     }
                   7156:     if (wantarray) {
                   7157: 	return ($e,$f);
                   7158:     } else {
                   7159: 	my $g;
                   7160: 	{
                   7161: 	    use integer;
                   7162: 	    $g=($e+$f);
                   7163: 	    if ($_64bit) {
                   7164: 		$g=(($g<<32)>>32);
                   7165: 	    }
                   7166: 	}
                   7167: 	return $g;
                   7168:     }
                   7169: }
                   7170: 
1.368     albertel 7171: sub latest_rnd_algorithm_id {
1.675     albertel 7172:     return '64bit5';
1.366     albertel 7173: }
1.32      www      7174: 
1.503     albertel 7175: sub get_rand_alg {
                   7176:     my ($courseid)=@_;
1.790     albertel 7177:     if (!$courseid) { $courseid=(&whichuser())[1]; }
1.503     albertel 7178:     if ($courseid) {
1.620     albertel 7179: 	return $env{"course.$courseid.rndseed"};
1.503     albertel 7180:     }
                   7181:     return &latest_rnd_algorithm_id();
                   7182: }
                   7183: 
1.562     albertel 7184: sub validCODE {
                   7185:     my ($CODE)=@_;
                   7186:     if (defined($CODE) && $CODE ne '' && $CODE =~ /^\w+$/) { return 1; }
                   7187:     return 0;
                   7188: }
                   7189: 
1.491     albertel 7190: sub getCODE {
1.620     albertel 7191:     if (&validCODE($env{'form.CODE'})) { return $env{'form.CODE'}; }
1.618     albertel 7192:     if ( (defined($Apache::lonhomework::parsing_a_problem) ||
                   7193: 	  defined($Apache::lonhomework::parsing_a_task) ) &&
                   7194: 	 &validCODE($Apache::lonhomework::history{'resource.CODE'})) {
1.491     albertel 7195: 	return $Apache::lonhomework::history{'resource.CODE'};
                   7196:     }
                   7197:     return undef;
                   7198: }
                   7199: 
1.31      www      7200: sub rndseed {
1.155     albertel 7201:     my ($symb,$courseid,$domain,$username)=@_;
1.790     albertel 7202:     my ($wsymb,$wcourseid,$wdomain,$wusername)=&whichuser();
1.896     albertel 7203:     if (!defined($symb)) {
1.366     albertel 7204: 	unless ($symb=$wsymb) { return time; }
                   7205:     }
                   7206:     if (!$courseid) { $courseid=$wcourseid; }
                   7207:     if (!$domain) { $domain=$wdomain; }
                   7208:     if (!$username) { $username=$wusername }
1.503     albertel 7209:     my $which=&get_rand_alg();
1.803     albertel 7210: 
1.491     albertel 7211:     if (defined(&getCODE())) {
1.675     albertel 7212: 	if ($which eq '64bit5') {
                   7213: 	    return &rndseed_CODE_64bit5($symb,$courseid,$domain,$username);
                   7214: 	} elsif ($which eq '64bit4') {
1.575     albertel 7215: 	    return &rndseed_CODE_64bit4($symb,$courseid,$domain,$username);
                   7216: 	} else {
                   7217: 	    return &rndseed_CODE_64bit($symb,$courseid,$domain,$username);
                   7218: 	}
1.675     albertel 7219:     } elsif ($which eq '64bit5') {
                   7220: 	return &rndseed_64bit5($symb,$courseid,$domain,$username);
1.575     albertel 7221:     } elsif ($which eq '64bit4') {
                   7222: 	return &rndseed_64bit4($symb,$courseid,$domain,$username);
1.501     albertel 7223:     } elsif ($which eq '64bit3') {
                   7224: 	return &rndseed_64bit3($symb,$courseid,$domain,$username);
1.443     albertel 7225:     } elsif ($which eq '64bit2') {
                   7226: 	return &rndseed_64bit2($symb,$courseid,$domain,$username);
1.366     albertel 7227:     } elsif ($which eq '64bit') {
                   7228: 	return &rndseed_64bit($symb,$courseid,$domain,$username);
                   7229:     }
                   7230:     return &rndseed_32bit($symb,$courseid,$domain,$username);
                   7231: }
                   7232: 
                   7233: sub rndseed_32bit {
                   7234:     my ($symb,$courseid,$domain,$username)=@_;
                   7235:     {
                   7236: 	use integer;
                   7237: 	my $symbchck=unpack("%32C*",$symb) << 27;
                   7238: 	my $symbseed=numval($symb) << 22;
                   7239: 	my $namechck=unpack("%32C*",$username) << 17;
                   7240: 	my $nameseed=numval($username) << 12;
                   7241: 	my $domainseed=unpack("%32C*",$domain) << 7;
                   7242: 	my $courseseed=unpack("%32C*",$courseid);
                   7243: 	my $num=$symbseed+$nameseed+$domainseed+$courseseed+$namechck+$symbchck;
1.790     albertel 7244: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7245: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7246: 	if ($_64bit) { $num=(($num<<32)>>32); }
1.366     albertel 7247: 	return $num;
                   7248:     }
                   7249: }
                   7250: 
                   7251: sub rndseed_64bit {
                   7252:     my ($symb,$courseid,$domain,$username)=@_;
                   7253:     {
                   7254: 	use integer;
                   7255: 	my $symbchck=unpack("%32S*",$symb) << 21;
                   7256: 	my $symbseed=numval($symb) << 10;
                   7257: 	my $namechck=unpack("%32S*",$username);
                   7258: 	
                   7259: 	my $nameseed=numval($username) << 21;
                   7260: 	my $domainseed=unpack("%32S*",$domain) << 10;
                   7261: 	my $courseseed=unpack("%32S*",$courseid);
                   7262: 	
                   7263: 	my $num1=$symbchck+$symbseed+$namechck;
                   7264: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7265: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7266: 	#&logthis("rndseed :$num:$symb");
1.564     albertel 7267: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.366     albertel 7268: 	return "$num1,$num2";
1.155     albertel 7269:     }
1.366     albertel 7270: }
                   7271: 
1.443     albertel 7272: sub rndseed_64bit2 {
                   7273:     my ($symb,$courseid,$domain,$username)=@_;
                   7274:     {
                   7275: 	use integer;
                   7276: 	# strings need to be an even # of cahracters long, it it is odd the
                   7277:         # last characters gets thrown away
                   7278: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7279: 	my $symbseed=numval($symb) << 10;
                   7280: 	my $namechck=unpack("%32S*",$username.' ');
                   7281: 	
                   7282: 	my $nameseed=numval($username) << 21;
1.501     albertel 7283: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7284: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7285: 	
                   7286: 	my $num1=$symbchck+$symbseed+$namechck;
                   7287: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7288: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7289: 	#&logthis("rndseed :$num:$symb");
1.803     albertel 7290: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
1.501     albertel 7291: 	return "$num1,$num2";
                   7292:     }
                   7293: }
                   7294: 
                   7295: sub rndseed_64bit3 {
                   7296:     my ($symb,$courseid,$domain,$username)=@_;
                   7297:     {
                   7298: 	use integer;
                   7299: 	# strings need to be an even # of cahracters long, it it is odd the
                   7300:         # last characters gets thrown away
                   7301: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7302: 	my $symbseed=numval2($symb) << 10;
                   7303: 	my $namechck=unpack("%32S*",$username.' ');
                   7304: 	
                   7305: 	my $nameseed=numval2($username) << 21;
1.443     albertel 7306: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7307: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7308: 	
                   7309: 	my $num1=$symbchck+$symbseed+$namechck;
                   7310: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7311: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7312: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.564     albertel 7313: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7314: 	
1.503     albertel 7315: 	return "$num1:$num2";
1.443     albertel 7316:     }
                   7317: }
                   7318: 
1.575     albertel 7319: sub rndseed_64bit4 {
                   7320:     my ($symb,$courseid,$domain,$username)=@_;
                   7321:     {
                   7322: 	use integer;
                   7323: 	# strings need to be an even # of cahracters long, it it is odd the
                   7324:         # last characters gets thrown away
                   7325: 	my $symbchck=unpack("%32S*",$symb.' ') << 21;
                   7326: 	my $symbseed=numval3($symb) << 10;
                   7327: 	my $namechck=unpack("%32S*",$username.' ');
                   7328: 	
                   7329: 	my $nameseed=numval3($username) << 21;
                   7330: 	my $domainseed=unpack("%32S*",$domain.' ') << 10;
                   7331: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7332: 	
                   7333: 	my $num1=$symbchck+$symbseed+$namechck;
                   7334: 	my $num2=$nameseed+$domainseed+$courseseed;
1.790     albertel 7335: 	#&logthis("$symbseed:$nameseed;$domainseed|$courseseed;$namechck:$symbchck");
                   7336: 	#&logthis("rndseed :$num1:$num2:$_64bit");
1.575     albertel 7337: 	if ($_64bit) { $num1=(($num1<<32)>>32); $num2=(($num2<<32)>>32); }
                   7338: 	
                   7339: 	return "$num1:$num2";
                   7340:     }
                   7341: }
                   7342: 
1.675     albertel 7343: sub rndseed_64bit5 {
                   7344:     my ($symb,$courseid,$domain,$username)=@_;
                   7345:     my ($num1,$num2)=&digest("$symb,$courseid,$domain,$username");
                   7346:     return "$num1:$num2";
                   7347: }
                   7348: 
1.366     albertel 7349: sub rndseed_CODE_64bit {
                   7350:     my ($symb,$courseid,$domain,$username)=@_;
1.155     albertel 7351:     {
1.366     albertel 7352: 	use integer;
1.443     albertel 7353: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
1.484     albertel 7354: 	my $symbseed=numval2($symb);
1.491     albertel 7355: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7356: 	my $CODEseed=numval(&getCODE());
1.443     albertel 7357: 	my $courseseed=unpack("%32S*",$courseid.' ');
1.484     albertel 7358: 	my $num1=$symbseed+$CODEchck;
                   7359: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7360: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7361: 	#&logthis("rndseed :$num1:$num2:$symb");
1.564     albertel 7362: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7363: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
1.503     albertel 7364: 	return "$num1:$num2";
1.366     albertel 7365:     }
                   7366: }
                   7367: 
1.575     albertel 7368: sub rndseed_CODE_64bit4 {
                   7369:     my ($symb,$courseid,$domain,$username)=@_;
                   7370:     {
                   7371: 	use integer;
                   7372: 	my $symbchck=unpack("%32S*",$symb.' ') << 16;
                   7373: 	my $symbseed=numval3($symb);
                   7374: 	my $CODEchck=unpack("%32S*",&getCODE().' ') << 16;
                   7375: 	my $CODEseed=numval3(&getCODE());
                   7376: 	my $courseseed=unpack("%32S*",$courseid.' ');
                   7377: 	my $num1=$symbseed+$CODEchck;
                   7378: 	my $num2=$CODEseed+$courseseed+$symbchck;
1.790     albertel 7379: 	#&logthis("$symbseed:$CODEchck|$CODEseed:$courseseed:$symbchck");
                   7380: 	#&logthis("rndseed :$num1:$num2:$symb");
1.575     albertel 7381: 	if ($_64bit) { $num1=(($num1<<32)>>32); }
                   7382: 	if ($_64bit) { $num2=(($num2<<32)>>32); }
                   7383: 	return "$num1:$num2";
                   7384:     }
                   7385: }
                   7386: 
1.675     albertel 7387: sub rndseed_CODE_64bit5 {
                   7388:     my ($symb,$courseid,$domain,$username)=@_;
                   7389:     my $code = &getCODE();
                   7390:     my ($num1,$num2)=&digest("$symb,$courseid,$code");
                   7391:     return "$num1:$num2";
                   7392: }
                   7393: 
1.366     albertel 7394: sub setup_random_from_rndseed {
                   7395:     my ($rndseed)=@_;
1.503     albertel 7396:     if ($rndseed =~/([,:])/) {
                   7397: 	my ($num1,$num2)=split(/[,:]/,$rndseed);
1.366     albertel 7398: 	&Math::Random::random_set_seed(abs($num1),abs($num2));
                   7399:     } else {
                   7400: 	&Math::Random::random_set_seed_from_phrase($rndseed);
1.98      albertel 7401:     }
1.36      albertel 7402: }
                   7403: 
1.474     albertel 7404: sub latest_receipt_algorithm_id {
1.835     albertel 7405:     return 'receipt3';
1.474     albertel 7406: }
                   7407: 
1.480     www      7408: sub recunique {
                   7409:     my $fucourseid=shift;
                   7410:     my $unique;
1.835     albertel 7411:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2' ||
                   7412: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7413: 	$unique=$env{"course.$fucourseid.internal.encseed"};
1.480     www      7414:     } else {
                   7415: 	$unique=$perlvar{'lonReceipt'};
                   7416:     }
                   7417:     return unpack("%32C*",$unique);
                   7418: }
                   7419: 
                   7420: sub recprefix {
                   7421:     my $fucourseid=shift;
                   7422:     my $prefix;
1.835     albertel 7423:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2'||
                   7424: 	$env{"course.$fucourseid.receiptalg"} eq 'receipt3' ) {
1.620     albertel 7425: 	$prefix=$env{"course.$fucourseid.internal.encpref"};
1.480     www      7426:     } else {
                   7427: 	$prefix=$perlvar{'lonHostID'};
                   7428:     }
                   7429:     return unpack("%32C*",$prefix);
                   7430: }
                   7431: 
1.76      www      7432: sub ireceipt {
1.474     albertel 7433:     my ($funame,$fudom,$fucourseid,$fusymb,$part)=@_;
1.835     albertel 7434: 
                   7435:     my $return =&recprefix($fucourseid).'-';
                   7436: 
                   7437:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt3' ||
                   7438: 	$env{'request.state'} eq 'construct') {
                   7439: 	$return .= (&digest("$funame,$fudom,$fucourseid,$fusymb,$part")%10000);
                   7440: 	return $return;
                   7441:     }
                   7442: 
1.76      www      7443:     my $cuname=unpack("%32C*",$funame);
                   7444:     my $cudom=unpack("%32C*",$fudom);
                   7445:     my $cucourseid=unpack("%32C*",$fucourseid);
                   7446:     my $cusymb=unpack("%32C*",$fusymb);
1.480     www      7447:     my $cunique=&recunique($fucourseid);
1.474     albertel 7448:     my $cpart=unpack("%32S*",$part);
1.835     albertel 7449:     if ($env{"course.$fucourseid.receiptalg"} eq 'receipt2') {
                   7450: 
1.790     albertel 7451: 	#&logthis("doing receipt2  using parts $cpart, uname $cuname and udom $cudom gets  ".($cpart%$cuname)." and ".($cpart%$cudom));
1.474     albertel 7452: 			       
                   7453: 	$return.= ($cunique%$cuname+
                   7454: 		   $cunique%$cudom+
                   7455: 		   $cusymb%$cuname+
                   7456: 		   $cusymb%$cudom+
                   7457: 		   $cucourseid%$cuname+
                   7458: 		   $cucourseid%$cudom+
                   7459: 		   $cpart%$cuname+
                   7460: 		   $cpart%$cudom);
                   7461:     } else {
                   7462: 	$return.= ($cunique%$cuname+
                   7463: 		   $cunique%$cudom+
                   7464: 		   $cusymb%$cuname+
                   7465: 		   $cusymb%$cudom+
                   7466: 		   $cucourseid%$cuname+
                   7467: 		   $cucourseid%$cudom);
                   7468:     }
                   7469:     return $return;
1.76      www      7470: }
                   7471: 
                   7472: sub receipt {
1.474     albertel 7473:     my ($part)=@_;
1.790     albertel 7474:     my ($symb,$courseid,$domain,$name) = &whichuser();
1.474     albertel 7475:     return &ireceipt($name,$domain,$courseid,$symb,$part);
1.76      www      7476: }
1.260     ng       7477: 
1.790     albertel 7478: sub whichuser {
                   7479:     my ($passedsymb)=@_;
                   7480:     my ($symb,$courseid,$domain,$name,$publicuser);
                   7481:     if (defined($env{'form.grade_symb'})) {
                   7482: 	my ($tmp_courseid)=&get_env_multiple('form.grade_courseid');
                   7483: 	my $allowed=&allowed('vgr',$tmp_courseid);
                   7484: 	if (!$allowed &&
                   7485: 	    exists($env{'request.course.sec'}) &&
                   7486: 	    $env{'request.course.sec'} !~ /^\s*$/) {
                   7487: 	    $allowed=&allowed('vgr',$tmp_courseid.
                   7488: 			      '/'.$env{'request.course.sec'});
                   7489: 	}
                   7490: 	if ($allowed) {
                   7491: 	    ($symb)=&get_env_multiple('form.grade_symb');
                   7492: 	    $courseid=$tmp_courseid;
                   7493: 	    ($domain)=&get_env_multiple('form.grade_domain');
                   7494: 	    ($name)=&get_env_multiple('form.grade_username');
                   7495: 	    return ($symb,$courseid,$domain,$name,$publicuser);
                   7496: 	}
                   7497:     }
                   7498:     if (!$passedsymb) {
                   7499: 	$symb=&symbread();
                   7500:     } else {
                   7501: 	$symb=$passedsymb;
                   7502:     }
                   7503:     $courseid=$env{'request.course.id'};
                   7504:     $domain=$env{'user.domain'};
                   7505:     $name=$env{'user.name'};
                   7506:     if ($name eq 'public' && $domain eq 'public') {
                   7507: 	if (!defined($env{'form.username'})) {
                   7508: 	    $env{'form.username'}.=time.rand(10000000);
                   7509: 	}
                   7510: 	$name.=$env{'form.username'};
                   7511:     }
                   7512:     return ($symb,$courseid,$domain,$name,$publicuser);
                   7513: 
                   7514: }
                   7515: 
1.36      albertel 7516: # ------------------------------------------------------------ Serves up a file
1.472     albertel 7517: # returns either the contents of the file or 
                   7518: # -1 if the file doesn't exist
1.481     raeburn  7519: #
                   7520: # if the target is a file that was uploaded via DOCS, 
                   7521: # a check will be made to see if a current copy exists on the local server,
                   7522: # if it does this will be served, otherwise a copy will be retrieved from
                   7523: # the home server for the course and stored in /home/httpd/html/userfiles on
                   7524: # the local server.   
1.472     albertel 7525: 
1.36      albertel 7526: sub getfile {
1.538     albertel 7527:     my ($file) = @_;
1.609     banghart 7528:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.538     albertel 7529:     &repcopy($file);
                   7530:     return &readfile($file);
                   7531: }
                   7532: 
                   7533: sub repcopy_userfile {
                   7534:     my ($file)=@_;
1.609     banghart 7535:     if ($file =~ m -^/*(uploaded|editupload)/-) { $file=&filelocation("",$file); }
1.610     albertel 7536:     if ($file =~ m|^/home/httpd/html/lonUsers/|) { return 'ok'; }
1.538     albertel 7537:     my ($cdom,$cnum,$filename) = 
1.811     albertel 7538: 	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+($match_domain)/+($match_name)/+(.*)|);
1.538     albertel 7539:     my $uri="/uploaded/$cdom/$cnum/$filename";
                   7540:     if (-e "$file") {
1.828     www      7541: # we already have a local copy, check it out
1.538     albertel 7542: 	my @fileinfo = stat($file);
1.828     www      7543: 	my $rtncode;
                   7544: 	my $info;
1.538     albertel 7545: 	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
1.482     albertel 7546: 	if ($lwpresp ne 'ok') {
1.828     www      7547: # there is no such file anymore, even though we had a local copy
1.482     albertel 7548: 	    if ($rtncode eq '404') {
1.538     albertel 7549: 		unlink($file);
1.482     albertel 7550: 	    }
                   7551: 	    return -1;
                   7552: 	}
                   7553: 	if ($info < $fileinfo[9]) {
1.828     www      7554: # nice, the file we have is up-to-date, just say okay
1.607     raeburn  7555: 	    return 'ok';
1.828     www      7556: 	} else {
                   7557: # the file is outdated, get rid of it
                   7558: 	    unlink($file);
1.482     albertel 7559: 	}
1.828     www      7560:     }
                   7561: # one way or the other, at this point, we don't have the file
                   7562: # construct the correct path for the file
                   7563:     my @parts = ($cdom,$cnum); 
                   7564:     if ($filename =~ m|^(.+)/[^/]+$|) {
                   7565: 	push @parts, split(/\//,$1);
                   7566:     }
                   7567:     my $path = $perlvar{'lonDocRoot'}.'/userfiles';
                   7568:     foreach my $part (@parts) {
                   7569: 	$path .= '/'.$part;
                   7570: 	if (!-e $path) {
                   7571: 	    mkdir($path,0770);
1.482     albertel 7572: 	}
                   7573:     }
1.828     www      7574: # now the path exists for sure
                   7575: # get a user agent
                   7576:     my $ua=new LWP::UserAgent;
                   7577:     my $transferfile=$file.'.in.transfer';
                   7578: # FIXME: this should flock
                   7579:     if (-e $transferfile) { return 'ok'; }
                   7580:     my $request;
                   7581:     $uri=~s/^\///;
1.838     albertel 7582:     $request=new HTTP::Request('GET','http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri);
1.828     www      7583:     my $response=$ua->request($request,$transferfile);
                   7584: # did it work?
                   7585:     if ($response->is_error()) {
                   7586: 	unlink($transferfile);
                   7587: 	&logthis("Userfile repcopy failed for $uri");
                   7588: 	return -1;
                   7589:     }
                   7590: # worked, rename the transfer file
                   7591:     rename($transferfile,$file);
1.607     raeburn  7592:     return 'ok';
1.481     raeburn  7593: }
                   7594: 
1.517     albertel 7595: sub tokenwrapper {
                   7596:     my $uri=shift;
1.552     albertel 7597:     $uri=~s|^http\://([^/]+)||;
                   7598:     $uri=~s|^/||;
1.620     albertel 7599:     $env{'user.environment'}=~/\/([^\/]+)\.id/;
1.517     albertel 7600:     my $token=$1;
1.552     albertel 7601:     my (undef,$udom,$uname,$file)=split('/',$uri,4);
                   7602:     if ($udom && $uname && $file) {
                   7603: 	$file=~s|(\?\.*)*$||;
1.620     albertel 7604:         &appenv("userfile.$udom/$uname/$file" => $env{'request.course.id'});
1.838     albertel 7605:         return 'http://'.&hostname(&homeserver($uname,$udom)).'/'.$uri.
1.517     albertel 7606:                (($uri=~/\?/)?'&':'?').'token='.$token.
                   7607:                                '&tokenissued='.$perlvar{'lonHostID'};
                   7608:     } else {
                   7609:         return '/adm/notfound.html';
                   7610:     }
                   7611: }
                   7612: 
1.828     www      7613: # call with reqtype HEAD: get last modification time
                   7614: # call with reqtype GET: get the file contents
                   7615: # Do not call this with reqtype GET for large files! It loads everything into memory
                   7616: #
1.481     raeburn  7617: sub getuploaded {
                   7618:     my ($reqtype,$uri,$cdom,$cnum,$info,$rtncode) = @_;
                   7619:     $uri=~s/^\///;
1.838     albertel 7620:     $uri = 'http://'.&hostname(&homeserver($cnum,$cdom)).'/raw/'.$uri;
1.481     raeburn  7621:     my $ua=new LWP::UserAgent;
                   7622:     my $request=new HTTP::Request($reqtype,$uri);
                   7623:     my $response=$ua->request($request);
                   7624:     $$rtncode = $response->code;
1.482     albertel 7625:     if (! $response->is_success()) {
                   7626: 	return 'failed';
                   7627:     }      
                   7628:     if ($reqtype eq 'HEAD') {
1.486     www      7629: 	$$info = &HTTP::Date::str2time( $response->header('Last-modified') );
1.482     albertel 7630:     } elsif ($reqtype eq 'GET') {
                   7631: 	$$info = $response->content;
1.472     albertel 7632:     }
1.482     albertel 7633:     return 'ok';
1.36      albertel 7634: }
                   7635: 
1.481     raeburn  7636: sub readfile {
                   7637:     my $file = shift;
                   7638:     if ( (! -e $file ) || ($file eq '') ) { return -1; };
                   7639:     my $fh;
                   7640:     open($fh,"<$file");
                   7641:     my $a='';
1.800     albertel 7642:     while (my $line = <$fh>) { $a .= $line; }
1.481     raeburn  7643:     return $a;
                   7644: }
                   7645: 
1.36      albertel 7646: sub filelocation {
1.590     banghart 7647:     my ($dir,$file) = @_;
                   7648:     my $location;
                   7649:     $file=~ s/^\s*(\S+)\s*$/$1/; ## strip off leading and trailing spaces
1.700     albertel 7650: 
                   7651:     if ($file =~ m-^/adm/-) {
                   7652: 	$file=~s-^/adm/wrapper/-/-;
                   7653: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
                   7654:     }
1.882     albertel 7655: 
1.590     banghart 7656:     if ($file=~m:^/~:) { # is a contruction space reference
                   7657:         $location = $file;
                   7658:         $location =~ s:/~(.*?)/(.*):/home/$1/public_html/$2:;
1.807     albertel 7659:     } elsif ($file=~m{^/home/$match_username/public_html/}) {
1.649     albertel 7660: 	# is a correct contruction space reference
                   7661:         $location = $file;
1.609     banghart 7662:     } elsif ($file=~/^\/*(uploaded|editupload)/) { # is an uploaded file
1.590     banghart 7663:         my ($udom,$uname,$filename)=
1.811     albertel 7664:   	    ($file=~m -^/+(?:uploaded|editupload)/+($match_domain)/+($match_name)/+(.*)$-);
1.590     banghart 7665:         my $home=&homeserver($uname,$udom);
                   7666:         my $is_me=0;
                   7667:         my @ids=&current_machine_ids();
                   7668:         foreach my $id (@ids) { if ($id eq $home) { $is_me=1; } }
                   7669:         if ($is_me) {
1.740     www      7670:   	    $location=&propath($udom,$uname).
1.590     banghart 7671:   	      '/userfiles/'.$filename;
                   7672:         } else {
                   7673:   	  $location=$Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles/'.
                   7674:   	      $udom.'/'.$uname.'/'.$filename;
                   7675:         }
1.882     albertel 7676:     } elsif ($file =~ m-^/adm/-) {
                   7677: 	$location = $perlvar{'lonDocRoot'}.'/'.$file;
1.590     banghart 7678:     } else {
                   7679:         $file=~s/^\Q$perlvar{'lonDocRoot'}\E//;
                   7680:         $file=~s:^/res/:/:;
                   7681:         if ( !( $file =~ m:^/:) ) {
                   7682:             $location = $dir. '/'.$file;
                   7683:         } else {
                   7684:             $location = '/home/httpd/html/res'.$file;
                   7685:         }
1.59      albertel 7686:     }
1.590     banghart 7687:     $location=~s://+:/:g; # remove duplicate /
                   7688:     while ($location=~m:/\.\./:) {$location=~ s:/[^/]+/\.\./:/:g;} #remove dir/..
                   7689:     while ($location=~m:/\./:) {$location=~ s:/\./:/:g;} #remove /./
                   7690:     return $location;
1.46      www      7691: }
1.36      albertel 7692: 
1.46      www      7693: sub hreflocation {
                   7694:     my ($dir,$file)=@_;
1.460     albertel 7695:     unless (($file=~m-^http://-i) || ($file=~m-^/-)) {
1.666     albertel 7696: 	$file=filelocation($dir,$file);
1.700     albertel 7697:     } elsif ($file=~m-^/adm/-) {
                   7698: 	$file=~s-^/adm/wrapper/-/-;
                   7699: 	$file=~s-^/adm/coursedocs/showdoc/-/-;
1.666     albertel 7700:     }
                   7701:     if ($file=~m-^\Q$perlvar{'lonDocRoot'}\E-) {
                   7702: 	$file=~s-^\Q$perlvar{'lonDocRoot'}\E--;
1.807     albertel 7703:     } elsif ($file=~m-/home/($match_username)/public_html/-) {
                   7704: 	$file=~s-^/home/($match_username)/public_html/-/~$1/-;
1.666     albertel 7705:     } elsif ($file=~m-^\Q$perlvar{'lonUsersDir'}\E-) {
1.811     albertel 7706: 	$file=~s-^/home/httpd/lonUsers/($match_domain)/./././($match_name)/userfiles/
1.666     albertel 7707: 	    -/uploaded/$1/$2/-x;
1.46      www      7708:     }
1.462     albertel 7709:     return $file;
1.465     albertel 7710: }
                   7711: 
                   7712: sub current_machine_domains {
1.853     albertel 7713:     return &machine_domains(&hostname($perlvar{'lonHostID'}));
                   7714: }
                   7715: 
                   7716: sub machine_domains {
                   7717:     my ($hostname) = @_;
1.465     albertel 7718:     my @domains;
1.838     albertel 7719:     my %hostname = &all_hostnames();
1.465     albertel 7720:     while( my($id, $name) = each(%hostname)) {
1.467     matthew  7721: #	&logthis("-$id-$name-$hostname-");
1.465     albertel 7722: 	if ($hostname eq $name) {
1.844     albertel 7723: 	    push(@domains,&host_domain($id));
1.465     albertel 7724: 	}
                   7725:     }
                   7726:     return @domains;
                   7727: }
                   7728: 
                   7729: sub current_machine_ids {
1.853     albertel 7730:     return &machine_ids(&hostname($perlvar{'lonHostID'}));
                   7731: }
                   7732: 
                   7733: sub machine_ids {
                   7734:     my ($hostname) = @_;
                   7735:     $hostname ||= &hostname($perlvar{'lonHostID'});
1.465     albertel 7736:     my @ids;
1.888     albertel 7737:     my %name_to_host = &all_names();
1.889     albertel 7738:     if (ref($name_to_host{$hostname}) eq 'ARRAY') {
                   7739: 	return @{ $name_to_host{$hostname} };
                   7740:     }
                   7741:     return;
1.31      www      7742: }
                   7743: 
1.824     raeburn  7744: sub additional_machine_domains {
                   7745:     my @domains;
                   7746:     open(my $fh,"<$perlvar{'lonTabDir'}/expected_domains.tab");
                   7747:     while( my $line = <$fh>) {
                   7748:         $line =~ s/\s//g;
                   7749:         push(@domains,$line);
                   7750:     }
                   7751:     return @domains;
                   7752: }
                   7753: 
                   7754: sub default_login_domain {
                   7755:     my $domain = $perlvar{'lonDefDomain'};
                   7756:     my $testdomain=(split(/\./,$ENV{'HTTP_HOST'}))[0];
                   7757:     foreach my $posdom (&current_machine_domains(),
                   7758:                         &additional_machine_domains()) {
                   7759:         if (lc($posdom) eq lc($testdomain)) {
                   7760:             $domain=$posdom;
                   7761:             last;
                   7762:         }
                   7763:     }
                   7764:     return $domain;
                   7765: }
                   7766: 
1.31      www      7767: # ------------------------------------------------------------- Declutters URLs
                   7768: 
                   7769: sub declutter {
                   7770:     my $thisfn=shift;
1.569     albertel 7771:     if ($thisfn=~m|^/enc/|) { $thisfn=&Apache::lonenc::unencrypted($thisfn); }
1.479     albertel 7772:     $thisfn=~s/^\Q$perlvar{'lonDocRoot'}\E//;
1.31      www      7773:     $thisfn=~s/^\///;
1.697     albertel 7774:     $thisfn=~s|^adm/wrapper/||;
                   7775:     $thisfn=~s|^adm/coursedocs/showdoc/||;
1.31      www      7776:     $thisfn=~s/^res\///;
1.235     www      7777:     $thisfn=~s/\?.+$//;
1.268     www      7778:     return $thisfn;
                   7779: }
                   7780: 
                   7781: # ------------------------------------------------------------- Clutter up URLs
                   7782: 
                   7783: sub clutter {
                   7784:     my $thisfn='/'.&declutter(shift);
1.887     albertel 7785:     if ($thisfn !~ m{^/(uploaded|editupload|adm|userfiles|ext|raw|priv|public)/}
1.884     albertel 7786: 	|| $thisfn =~ m{^/adm/(includes|pages)} ) { 
1.270     www      7787:        $thisfn='/res'.$thisfn; 
                   7788:     }
1.694     albertel 7789:     if ($thisfn !~m|/adm|) {
1.695     albertel 7790: 	if ($thisfn =~ m|/ext/|) {
1.694     albertel 7791: 	    $thisfn='/adm/wrapper'.$thisfn;
1.695     albertel 7792: 	} else {
                   7793: 	    my ($ext) = ($thisfn =~ /\.(\w+)$/);
                   7794: 	    my $embstyle=&Apache::loncommon::fileembstyle($ext);
1.698     albertel 7795: 	    if ($embstyle eq 'ssi'
                   7796: 		|| ($embstyle eq 'hdn')
                   7797: 		|| ($embstyle eq 'rat')
                   7798: 		|| ($embstyle eq 'prv')
                   7799: 		|| ($embstyle eq 'ign')) {
                   7800: 		#do nothing with these
                   7801: 	    } elsif (($embstyle eq 'img') 
1.695     albertel 7802: 		|| ($embstyle eq 'emb')
                   7803: 		|| ($embstyle eq 'wrp')) {
                   7804: 		$thisfn='/adm/wrapper'.$thisfn;
1.698     albertel 7805: 	    } elsif ($embstyle eq 'unk'
                   7806: 		     && $thisfn!~/\.(sequence|page)$/) {
1.695     albertel 7807: 		$thisfn='/adm/coursedocs/showdoc'.$thisfn;
1.698     albertel 7808: 	    } else {
1.718     www      7809: #		&logthis("Got a blank emb style");
1.695     albertel 7810: 	    }
1.694     albertel 7811: 	}
                   7812:     }
1.31      www      7813:     return $thisfn;
1.12      www      7814: }
                   7815: 
1.787     albertel 7816: sub clutter_with_no_wrapper {
                   7817:     my $uri = &clutter(shift);
                   7818:     if ($uri =~ m-^/adm/-) {
                   7819: 	$uri =~ s-^/adm/wrapper/-/-;
                   7820: 	$uri =~ s-^/adm/coursedocs/showdoc/-/-;
                   7821:     }
                   7822:     return $uri;
                   7823: }
                   7824: 
1.557     albertel 7825: sub freeze_escape {
                   7826:     my ($value)=@_;
                   7827:     if (ref($value)) {
                   7828: 	$value=&nfreeze($value);
                   7829: 	return '__FROZEN__'.&escape($value);
                   7830:     }
                   7831:     return &escape($value);
                   7832: }
                   7833: 
1.11      www      7834: 
1.557     albertel 7835: sub thaw_unescape {
                   7836:     my ($value)=@_;
                   7837:     if ($value =~ /^__FROZEN__/) {
                   7838: 	substr($value,0,10,undef);
                   7839: 	$value=&unescape($value);
                   7840: 	return &thaw($value);
                   7841:     }
                   7842:     return &unescape($value);
                   7843: }
                   7844: 
1.436     albertel 7845: sub correct_line_ends {
                   7846:     my ($result)=@_;
                   7847:     $$result =~s/\r\n/\n/mg;
                   7848:     $$result =~s/\r/\n/mg;
1.415     albertel 7849: }
1.1       albertel 7850: # ================================================================ Main Program
                   7851: 
1.184     www      7852: sub goodbye {
1.204     albertel 7853:    &logthis("Starting Shut down");
1.443     albertel 7854: #not converted to using infrastruture and probably shouldn't be
1.870     albertel 7855:    &logthis(sprintf("%-20s is %s",'%badServerCache',length(&nfreeze(\%badServerCache))));
1.443     albertel 7856: #converted
1.599     albertel 7857: #   &logthis(sprintf("%-20s is %s",'%metacache',scalar(%metacache)));
1.870     albertel 7858:    &logthis(sprintf("%-20s is %s",'%homecache',length(&nfreeze(\%homecache))));
                   7859: #   &logthis(sprintf("%-20s is %s",'%titlecache',length(&nfreeze(\%titlecache))));
                   7860: #   &logthis(sprintf("%-20s is %s",'%courseresdatacache',length(&nfreeze(\%courseresdatacache))));
1.425     albertel 7861: #1.1 only
1.870     albertel 7862: #   &logthis(sprintf("%-20s is %s",'%userresdatacache',length(&nfreeze(\%userresdatacache))));
                   7863: #   &logthis(sprintf("%-20s is %s",'%getsectioncache',length(&nfreeze(\%getsectioncache))));
                   7864: #   &logthis(sprintf("%-20s is %s",'%courseresversioncache',length(&nfreeze(\%courseresversioncache))));
                   7865: #   &logthis(sprintf("%-20s is %s",'%resversioncache',length(&nfreeze(\%resversioncache))));
                   7866:    &logthis(sprintf("%-20s is %s",'%remembered',length(&nfreeze(\%remembered))));
1.599     albertel 7867:    &logthis(sprintf("%-20s is %s",'kicks',$kicks));
                   7868:    &logthis(sprintf("%-20s is %s",'hits',$hits));
1.184     www      7869:    &flushcourselogs();
                   7870:    &logthis("Shutting down");
                   7871: }
                   7872: 
1.852     albertel 7873: sub get_dns {
1.869     albertel 7874:     my ($url,$func,$ignore_cache) = @_;
                   7875:     if (!$ignore_cache) {
                   7876: 	my ($content,$cached)=
                   7877: 	    &Apache::lonnet::is_cached_new('dns',$url);
                   7878: 	if ($cached) {
                   7879: 	    &$func($content);
                   7880: 	    return;
                   7881: 	}
                   7882:     }
                   7883: 
                   7884:     my %alldns;
1.852     albertel 7885:     open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   7886:     foreach my $dns (<$config>) {
                   7887: 	next if ($dns !~ /^\^(\S*)/x);
1.869     albertel 7888: 	$alldns{$1} = 1;
                   7889:     }
                   7890:     while (%alldns) {
                   7891: 	my ($dns) = keys(%alldns);
                   7892: 	delete($alldns{$dns});
1.852     albertel 7893: 	my $ua=new LWP::UserAgent;
                   7894: 	my $request=new HTTP::Request('GET',"http://$dns$url");
                   7895: 	my $response=$ua->request($request);
                   7896: 	next if ($response->is_error());
                   7897: 	my @content = split("\n",$response->content);
1.869     albertel 7898: 	&Apache::lonnet::do_cache_new('dns',$url,\@content,30*24*60*60);
1.852     albertel 7899: 	&$func(\@content);
1.869     albertel 7900: 	return;
1.852     albertel 7901:     }
                   7902:     close($config);
1.871     albertel 7903:     my $which = (split('/',$url))[3];
                   7904:     &logthis("unable to contact DNS defaulting to on disk file dns_$which.tab\n");
                   7905:     open($config,"<$perlvar{'lonTabDir'}/dns_$which.tab");
1.869     albertel 7906:     my @content = <$config>;
                   7907:     &$func(\@content);
                   7908:     return;
1.852     albertel 7909: }
1.327     albertel 7910: # ------------------------------------------------------------ Read domain file
                   7911: {
1.852     albertel 7912:     my $loaded;
1.846     albertel 7913:     my %domain;
                   7914: 
1.852     albertel 7915:     sub parse_domain_tab {
                   7916: 	my ($lines) = @_;
                   7917: 	foreach my $line (@$lines) {
                   7918: 	    next if ($line =~ /^(\#|\s*$ )/x);
1.403     www      7919: 
1.846     albertel 7920: 	    chomp($line);
1.852     albertel 7921: 	    my ($name,@elements) = split(/:/,$line,9);
1.846     albertel 7922: 	    my %this_domain;
                   7923: 	    foreach my $field ('description', 'auth_def', 'auth_arg_def',
                   7924: 			       'lang_def', 'city', 'longi', 'lati',
                   7925: 			       'primary') {
                   7926: 		$this_domain{$field} = shift(@elements);
                   7927: 	    }
                   7928: 	    $domain{$name} = \%this_domain;
1.852     albertel 7929: 	}
                   7930:     }
1.864     albertel 7931: 
                   7932:     sub reset_domain_info {
                   7933: 	undef($loaded);
                   7934: 	undef(%domain);
                   7935:     }
                   7936: 
1.852     albertel 7937:     sub load_domain_tab {
1.869     albertel 7938: 	my ($ignore_cache) = @_;
                   7939: 	&get_dns('/adm/dns/domain',\&parse_domain_tab,$ignore_cache);
1.852     albertel 7940: 	my $fh;
                   7941: 	if (open($fh,"<".$perlvar{'lonTabDir'}.'/domain.tab')) {
                   7942: 	    my @lines = <$fh>;
                   7943: 	    &parse_domain_tab(\@lines);
1.448     albertel 7944: 	}
1.852     albertel 7945: 	close($fh);
                   7946: 	$loaded = 1;
1.327     albertel 7947:     }
1.846     albertel 7948: 
                   7949:     sub domain {
1.852     albertel 7950: 	&load_domain_tab() if (!$loaded);
                   7951: 
1.846     albertel 7952: 	my ($name,$what) = @_;
                   7953: 	return if ( !exists($domain{$name}) );
                   7954: 
                   7955: 	if (!$what) {
                   7956: 	    return $domain{$name}{'description'};
                   7957: 	}
                   7958: 	return $domain{$name}{$what};
                   7959:     }
1.327     albertel 7960: }
                   7961: 
                   7962: 
1.1       albertel 7963: # ------------------------------------------------------------- Read hosts file
                   7964: {
1.838     albertel 7965:     my %hostname;
1.844     albertel 7966:     my %hostdom;
1.845     albertel 7967:     my %libserv;
1.852     albertel 7968:     my $loaded;
1.888     albertel 7969:     my %name_to_host;
1.852     albertel 7970: 
                   7971:     sub parse_hosts_tab {
                   7972: 	my ($file) = @_;
                   7973: 	foreach my $configline (@$file) {
                   7974: 	    next if ($configline =~ /^(\#|\s*$ )/x);
                   7975: 	    next if ($configline =~ /^\^/);
                   7976: 	    chomp($configline);
                   7977: 	    my ($id,$domain,$role,$name)=split(/:/,$configline);
                   7978: 	    $name=~s/\s//g;
                   7979: 	    if ($id && $domain && $role && $name) {
                   7980: 		$hostname{$id}=$name;
1.888     albertel 7981: 		push(@{$name_to_host{$name}}, $id);
1.852     albertel 7982: 		$hostdom{$id}=$domain;
                   7983: 		if ($role eq 'library') { $libserv{$id}=$name; }
                   7984: 	    }
                   7985: 	}
                   7986:     }
1.864     albertel 7987:     
                   7988:     sub reset_hosts_info {
1.897     albertel 7989: 	&purge_remembered();
1.864     albertel 7990: 	&reset_domain_info();
                   7991: 	&reset_hosts_ip_info();
1.892     albertel 7992: 	undef(%name_to_host);
1.864     albertel 7993: 	undef(%hostname);
                   7994: 	undef(%hostdom);
                   7995: 	undef(%libserv);
                   7996: 	undef($loaded);
                   7997:     }
1.1       albertel 7998: 
1.852     albertel 7999:     sub load_hosts_tab {
1.869     albertel 8000: 	my ($ignore_cache) = @_;
                   8001: 	&get_dns('/adm/dns/hosts',\&parse_hosts_tab,$ignore_cache);
1.852     albertel 8002: 	open(my $config,"<$perlvar{'lonTabDir'}/hosts.tab");
                   8003: 	my @config = <$config>;
                   8004: 	&parse_hosts_tab(\@config);
                   8005: 	close($config);
                   8006: 	$loaded=1;
1.1       albertel 8007:     }
1.852     albertel 8008: 
1.838     albertel 8009:     sub hostname {
1.852     albertel 8010: 	&load_hosts_tab() if (!$loaded);
                   8011: 
1.838     albertel 8012: 	my ($lonid) = @_;
                   8013: 	return $hostname{$lonid};
                   8014:     }
1.845     albertel 8015: 
1.838     albertel 8016:     sub all_hostnames {
1.852     albertel 8017: 	&load_hosts_tab() if (!$loaded);
                   8018: 
1.838     albertel 8019: 	return %hostname;
                   8020:     }
1.845     albertel 8021: 
1.888     albertel 8022:     sub all_names {
                   8023: 	&load_hosts_tab() if (!$loaded);
                   8024: 
                   8025: 	return %name_to_host;
                   8026:     }
                   8027: 
1.845     albertel 8028:     sub is_library {
1.852     albertel 8029: 	&load_hosts_tab() if (!$loaded);
                   8030: 
1.845     albertel 8031: 	return exists($libserv{$_[0]});
                   8032:     }
                   8033: 
                   8034:     sub all_library {
1.852     albertel 8035: 	&load_hosts_tab() if (!$loaded);
                   8036: 
1.845     albertel 8037: 	return %libserv;
                   8038:     }
                   8039: 
1.841     albertel 8040:     sub get_servers {
1.852     albertel 8041: 	&load_hosts_tab() if (!$loaded);
                   8042: 
1.841     albertel 8043: 	my ($domain,$type) = @_;
                   8044: 	my %possible_hosts = ($type eq 'library') ? %libserv
                   8045: 	                                          : %hostname;
                   8046: 	my %result;
1.842     albertel 8047: 	if (ref($domain) eq 'ARRAY') {
                   8048: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
1.843     albertel 8049: 		if (grep(/^\Q$hostdom{$host}\E$/,@$domain)) {
1.842     albertel 8050: 		    $result{$host} = $hostname;
                   8051: 		}
                   8052: 	    }
                   8053: 	} else {
                   8054: 	    while ( my ($host,$hostname) = each(%possible_hosts)) {
                   8055: 		if ($hostdom{$host} eq $domain) {
                   8056: 		    $result{$host} = $hostname;
                   8057: 		}
1.841     albertel 8058: 	    }
                   8059: 	}
                   8060: 	return %result;
                   8061:     }
1.845     albertel 8062: 
1.844     albertel 8063:     sub host_domain {
1.852     albertel 8064: 	&load_hosts_tab() if (!$loaded);
                   8065: 
1.844     albertel 8066: 	my ($lonid) = @_;
                   8067: 	return $hostdom{$lonid};
                   8068:     }
                   8069: 
1.841     albertel 8070:     sub all_domains {
1.852     albertel 8071: 	&load_hosts_tab() if (!$loaded);
                   8072: 
1.841     albertel 8073: 	my %seen;
                   8074: 	my @uniq = grep(!$seen{$_}++, values(%hostdom));
                   8075: 	return @uniq;
                   8076:     }
1.1       albertel 8077: }
                   8078: 
1.847     albertel 8079: { 
                   8080:     my %iphost;
1.856     albertel 8081:     my %name_to_ip;
                   8082:     my %lonid_to_ip;
1.869     albertel 8083: 
1.847     albertel 8084:     sub get_hosts_from_ip {
                   8085: 	my ($ip) = @_;
                   8086: 	my %iphosts = &get_iphost();
                   8087: 	if (ref($iphosts{$ip})) {
                   8088: 	    return @{$iphosts{$ip}};
                   8089: 	}
                   8090: 	return;
1.839     albertel 8091:     }
1.864     albertel 8092:     
                   8093:     sub reset_hosts_ip_info {
                   8094: 	undef(%iphost);
                   8095: 	undef(%name_to_ip);
                   8096: 	undef(%lonid_to_ip);
                   8097:     }
1.856     albertel 8098: 
                   8099:     sub get_host_ip {
                   8100: 	my ($lonid) = @_;
                   8101: 	if (exists($lonid_to_ip{$lonid})) {
                   8102: 	    return $lonid_to_ip{$lonid};
                   8103: 	}
                   8104: 	my $name=&hostname($lonid);
                   8105:    	my $ip = gethostbyname($name);
                   8106: 	return if (!$ip || length($ip) ne 4);
                   8107: 	$ip=inet_ntoa($ip);
                   8108: 	$name_to_ip{$name}   = $ip;
                   8109: 	$lonid_to_ip{$lonid} = $ip;
                   8110: 	return $ip;
                   8111:     }
1.847     albertel 8112:     
                   8113:     sub get_iphost {
1.869     albertel 8114: 	my ($ignore_cache) = @_;
1.894     albertel 8115: 
1.869     albertel 8116: 	if (!$ignore_cache) {
                   8117: 	    if (%iphost) {
                   8118: 		return %iphost;
                   8119: 	    }
                   8120: 	    my ($ip_info,$cached)=
                   8121: 		&Apache::lonnet::is_cached_new('iphost','iphost');
                   8122: 	    if ($cached) {
                   8123: 		%iphost      = %{$ip_info->[0]};
                   8124: 		%name_to_ip  = %{$ip_info->[1]};
                   8125: 		%lonid_to_ip = %{$ip_info->[2]};
                   8126: 		return %iphost;
                   8127: 	    }
                   8128: 	}
1.894     albertel 8129: 
                   8130: 	# get yesterday's info for fallback
                   8131: 	my %old_name_to_ip;
                   8132: 	my ($ip_info,$cached)=
                   8133: 	    &Apache::lonnet::is_cached_new('iphost','iphost');
                   8134: 	if ($cached) {
                   8135: 	    %old_name_to_ip = %{$ip_info->[1]};
                   8136: 	}
                   8137: 
1.888     albertel 8138: 	my %name_to_host = &all_names();
                   8139: 	foreach my $name (keys(%name_to_host)) {
1.847     albertel 8140: 	    my $ip;
                   8141: 	    if (!exists($name_to_ip{$name})) {
                   8142: 		$ip = gethostbyname($name);
                   8143: 		if (!$ip || length($ip) ne 4) {
1.894     albertel 8144: 		    if (defined($old_name_to_ip{$name})) {
                   8145: 			$ip = $old_name_to_ip{$name};
                   8146: 			&logthis("Can't find $name defaulting to old $ip");
                   8147: 		    } else {
                   8148: 			&logthis("Name $name no IP found");
                   8149: 			next;
                   8150: 		    }
                   8151: 		} else {
                   8152: 		    $ip=inet_ntoa($ip);
1.847     albertel 8153: 		}
                   8154: 		$name_to_ip{$name} = $ip;
                   8155: 	    } else {
                   8156: 		$ip = $name_to_ip{$name};
1.653     albertel 8157: 	    }
1.888     albertel 8158: 	    foreach my $id (@{ $name_to_host{$name} }) {
                   8159: 		$lonid_to_ip{$id} = $ip;
                   8160: 	    }
                   8161: 	    push(@{$iphost{$ip}},@{$name_to_host{$name}});
1.598     albertel 8162: 	}
1.869     albertel 8163: 	&Apache::lonnet::do_cache_new('iphost','iphost',
                   8164: 				      [\%iphost,\%name_to_ip,\%lonid_to_ip],
1.894     albertel 8165: 				      48*60*60);
1.869     albertel 8166: 
1.847     albertel 8167: 	return %iphost;
1.598     albertel 8168:     }
                   8169: }
                   8170: 
1.862     albertel 8171: BEGIN {
                   8172: 
                   8173: # ----------------------------------- Read loncapa.conf and loncapa_apache.conf
                   8174:     unless ($readit) {
                   8175: {
                   8176:     my $configvars = LONCAPA::Configuration::read_conf('loncapa.conf');
                   8177:     %perlvar = (%perlvar,%{$configvars});
                   8178: }
                   8179: 
                   8180: 
1.1       albertel 8181: # ------------------------------------------------------ Read spare server file
                   8182: {
1.448     albertel 8183:     open(my $config,"<$perlvar{'lonTabDir'}/spare.tab");
1.1       albertel 8184: 
                   8185:     while (my $configline=<$config>) {
                   8186:        chomp($configline);
1.284     matthew  8187:        if ($configline) {
1.784     albertel 8188: 	   my ($host,$type) = split(':',$configline,2);
1.785     albertel 8189: 	   if (!defined($type) || $type eq '') { $type = 'default' };
1.784     albertel 8190: 	   push(@{ $spareid{$type} }, $host);
1.1       albertel 8191:        }
                   8192:     }
1.448     albertel 8193:     close($config);
1.1       albertel 8194: }
1.11      www      8195: # ------------------------------------------------------------ Read permissions
                   8196: {
1.448     albertel 8197:     open(my $config,"<$perlvar{'lonTabDir'}/roles.tab");
1.11      www      8198: 
                   8199:     while (my $configline=<$config>) {
1.448     albertel 8200: 	chomp($configline);
                   8201: 	if ($configline) {
                   8202: 	    my ($role,$perm)=split(/ /,$configline);
                   8203: 	    if ($perm ne '') { $pr{$role}=$perm; }
                   8204: 	}
1.11      www      8205:     }
1.448     albertel 8206:     close($config);
1.11      www      8207: }
                   8208: 
                   8209: # -------------------------------------------- Read plain texts for permissions
                   8210: {
1.448     albertel 8211:     open(my $config,"<$perlvar{'lonTabDir'}/rolesplain.tab");
1.11      www      8212: 
                   8213:     while (my $configline=<$config>) {
1.448     albertel 8214: 	chomp($configline);
                   8215: 	if ($configline) {
1.742     raeburn  8216: 	    my ($short,@plain)=split(/:/,$configline);
                   8217:             %{$prp{$short}} = ();
                   8218: 	    if (@plain > 0) {
                   8219:                 $prp{$short}{'std'} = $plain[0];
                   8220:                 for (my $i=1; $i<@plain; $i++) {
                   8221:                     $prp{$short}{'alt'.$i} = $plain[$i];  
                   8222:                 }
                   8223:             }
1.448     albertel 8224: 	}
1.135     www      8225:     }
1.448     albertel 8226:     close($config);
1.135     www      8227: }
                   8228: 
                   8229: # ---------------------------------------------------------- Read package table
                   8230: {
1.448     albertel 8231:     open(my $config,"<$perlvar{'lonTabDir'}/packages.tab");
1.135     www      8232: 
                   8233:     while (my $configline=<$config>) {
1.483     albertel 8234: 	if ($configline !~ /\S/ || $configline=~/^#/) { next; }
1.448     albertel 8235: 	chomp($configline);
                   8236: 	my ($short,$plain)=split(/:/,$configline);
                   8237: 	my ($pack,$name)=split(/\&/,$short);
                   8238: 	if ($plain ne '') {
                   8239: 	    $packagetab{$pack.'&'.$name.'&name'}=$name; 
                   8240: 	    $packagetab{$short}=$plain; 
                   8241: 	}
1.11      www      8242:     }
1.448     albertel 8243:     close($config);
1.329     matthew  8244: }
                   8245: 
                   8246: # ------------- set up temporary directory
                   8247: {
                   8248:     $tmpdir = $perlvar{'lonDaemons'}.'/tmp/';
                   8249: 
1.11      www      8250: }
                   8251: 
1.794     albertel 8252: $memcache=new Cache::Memcached({'servers'           => ['127.0.0.1:11211'],
                   8253: 				'compress_threshold'=> 20_000,
                   8254:  			        });
1.185     www      8255: 
1.281     www      8256: $processmarker='_'.time.'_'.$perlvar{'lonHostID'};
1.186     www      8257: $dumpcount=0;
1.22      www      8258: 
1.163     harris41 8259: &logtouch();
1.672     albertel 8260: &logthis('<font color="yellow">INFO: Read configuration</font>');
1.195     www      8261: $readit=1;
1.564     albertel 8262:     {
                   8263: 	use integer;
                   8264: 	my $test=(2**32)+1;
1.568     albertel 8265: 	if ($test != 0) { $_64bit=1; } else { $_64bit=0; }
1.564     albertel 8266: 	&logthis(" Detected 64bit platform ($_64bit)");
                   8267:     }
1.195     www      8268: }
1.1       albertel 8269: }
1.179     www      8270: 
1.1       albertel 8271: 1;
1.191     harris41 8272: __END__
                   8273: 
1.243     albertel 8274: =pod
                   8275: 
1.191     harris41 8276: =head1 NAME
                   8277: 
1.243     albertel 8278: Apache::lonnet - Subroutines to ask questions about things in the network.
1.191     harris41 8279: 
                   8280: =head1 SYNOPSIS
                   8281: 
1.243     albertel 8282: Invoked by other LON-CAPA modules, when they need to talk to or about objects in the network.
1.191     harris41 8283: 
                   8284:  &Apache::lonnet::SUBROUTINENAME(ARGUMENTS);
                   8285: 
1.243     albertel 8286: Common parameters:
                   8287: 
                   8288: =over 4
                   8289: 
                   8290: =item *
                   8291: 
                   8292: $uname : an internal username (if $cname expecting a course Id specifically)
                   8293: 
                   8294: =item *
                   8295: 
                   8296: $udom : a domain (if $cdom expecting a course's domain specifically)
                   8297: 
                   8298: =item *
                   8299: 
                   8300: $symb : a resource instance identifier
                   8301: 
                   8302: =item *
                   8303: 
                   8304: $namespace : the name of a .db file that contains the data needed or
                   8305: being set.
                   8306: 
                   8307: =back
                   8308: 
1.394     bowersj2 8309: =head1 OVERVIEW
1.191     harris41 8310: 
1.394     bowersj2 8311: lonnet provides subroutines which interact with the
                   8312: lonc/lond (TCP) network layer of LON-CAPA. They can be used to ask
                   8313: about classes, users, and resources.
1.243     albertel 8314: 
                   8315: For many of these objects you can also use this to store data about
                   8316: them or modify them in various ways.
1.191     harris41 8317: 
1.394     bowersj2 8318: =head2 Symbs
1.191     harris41 8319: 
1.394     bowersj2 8320: To identify a specific instance of a resource, LON-CAPA uses symbols
                   8321: or "symbs"X<symb>. These identifiers are built from the URL of the
                   8322: map, the resource number of the resource in the map, and the URL of
                   8323: the resource itself. The latter is somewhat redundant, but might help
                   8324: if maps change.
                   8325: 
                   8326: An example is
                   8327: 
                   8328:  msu/korte/parts/part1.sequence___19___msu/korte/tests/part12.problem
                   8329: 
                   8330: The respective map entry is
                   8331: 
                   8332:  <resource id="19" src="/res/msu/korte/tests/part12.problem"
                   8333:   title="Problem 2">
                   8334:  </resource>
                   8335: 
                   8336: Symbs are used by the random number generator, as well as to store and
                   8337: restore data specific to a certain instance of for example a problem.
                   8338: 
                   8339: =head2 Storing And Retrieving Data
                   8340: 
                   8341: X<store()>X<cstore()>X<restore()>Three of the most important functions
                   8342: in C<lonnet.pm> are C<&Apache::lonnet::cstore()>,
                   8343: C<&Apache::lonnet:restore()>, and C<&Apache::lonnet::store()>, which
                   8344: is is the non-critical message twin of cstore. These functions are for
                   8345: handlers to store a perl hash to a user's permanent data space in an
                   8346: easy manner, and to retrieve it again on another call. It is expected
                   8347: that a handler would use this once at the beginning to retrieve data,
                   8348: and then again once at the end to send only the new data back.
                   8349: 
                   8350: The data is stored in the user's data directory on the user's
                   8351: homeserver under the ID of the course.
                   8352: 
                   8353: The hash that is returned by restore will have all of the previous
                   8354: value for all of the elements of the hash.
                   8355: 
                   8356: Example:
                   8357: 
                   8358:  #creating a hash
                   8359:  my %hash;
                   8360:  $hash{'foo'}='bar';
                   8361: 
                   8362:  #storing it
                   8363:  &Apache::lonnet::cstore(\%hash);
                   8364: 
                   8365:  #changing a value
                   8366:  $hash{'foo'}='notbar';
                   8367: 
                   8368:  #adding a new value
                   8369:  $hash{'bar'}='foo';
                   8370:  &Apache::lonnet::cstore(\%hash);
                   8371: 
                   8372:  #retrieving the hash
                   8373:  my %history=&Apache::lonnet::restore();
                   8374: 
                   8375:  #print the hash
                   8376:  foreach my $key (sort(keys(%history))) {
                   8377:    print("\%history{$key} = $history{$key}");
                   8378:  }
                   8379: 
                   8380: Will print out:
1.191     harris41 8381: 
1.394     bowersj2 8382:  %history{1:foo} = bar
                   8383:  %history{1:keys} = foo:timestamp
                   8384:  %history{1:timestamp} = 990455579
                   8385:  %history{2:bar} = foo
                   8386:  %history{2:foo} = notbar
                   8387:  %history{2:keys} = foo:bar:timestamp
                   8388:  %history{2:timestamp} = 990455580
                   8389:  %history{bar} = foo
                   8390:  %history{foo} = notbar
                   8391:  %history{timestamp} = 990455580
                   8392:  %history{version} = 2
                   8393: 
                   8394: Note that the special hash entries C<keys>, C<version> and
                   8395: C<timestamp> were added to the hash. C<version> will be equal to the
                   8396: total number of versions of the data that have been stored. The
                   8397: C<timestamp> attribute will be the UNIX time the hash was
                   8398: stored. C<keys> is available in every historical section to list which
                   8399: keys were added or changed at a specific historical revision of a
                   8400: hash.
                   8401: 
                   8402: B<Warning>: do not store the hash that restore returns directly. This
                   8403: will cause a mess since it will restore the historical keys as if the
                   8404: were new keys. I.E. 1:foo will become 1:1:foo etc.
1.191     harris41 8405: 
1.394     bowersj2 8406: Calling convention:
1.191     harris41 8407: 
1.394     bowersj2 8408:  my %record=&Apache::lonnet::restore($symb,$courseid,$domain,$uname,$home);
                   8409:  &Apache::lonnet::cstore(\%newrecord,$symb,$courseid,$domain,$uname,$home);
1.191     harris41 8410: 
1.394     bowersj2 8411: For more detailed information, see lonnet specific documentation.
1.191     harris41 8412: 
1.394     bowersj2 8413: =head1 RETURN MESSAGES
1.191     harris41 8414: 
1.394     bowersj2 8415: =over 4
1.191     harris41 8416: 
1.394     bowersj2 8417: =item * B<con_lost>: unable to contact remote host
1.191     harris41 8418: 
1.394     bowersj2 8419: =item * B<con_delayed>: unable to contact remote host, message will be delivered
                   8420: when the connection is brought back up
1.191     harris41 8421: 
1.394     bowersj2 8422: =item * B<con_failed>: unable to contact remote host and unable to save message
                   8423: for later delivery
1.191     harris41 8424: 
1.394     bowersj2 8425: =item * B<error:>: an error a occured, a description of the error follows the :
1.191     harris41 8426: 
1.394     bowersj2 8427: =item * B<no_such_host>: unable to fund a host associated with the user/domain
1.243     albertel 8428: that was requested
1.191     harris41 8429: 
1.243     albertel 8430: =back
1.191     harris41 8431: 
1.243     albertel 8432: =head1 PUBLIC SUBROUTINES
1.191     harris41 8433: 
1.243     albertel 8434: =head2 Session Environment Functions
1.191     harris41 8435: 
1.243     albertel 8436: =over 4
1.191     harris41 8437: 
1.394     bowersj2 8438: =item * 
                   8439: X<appenv()>
                   8440: B<appenv(%hash)>: the value of %hash is written to
                   8441: the user envirnoment file, and will be restored for each access this
1.620     albertel 8442: user makes during this session, also modifies the %env for the current
1.394     bowersj2 8443: process
1.191     harris41 8444: 
                   8445: =item *
1.394     bowersj2 8446: X<delenv()>
                   8447: B<delenv($regexp)>: removes all items from the session
                   8448: environment file that matches the regular expression in $regexp. The
1.620     albertel 8449: values are also delted from the current processes %env.
1.191     harris41 8450: 
1.795     albertel 8451: =item * get_env_multiple($name) 
                   8452: 
                   8453: gets $name from the %env hash, it seemlessly handles the cases where multiple
                   8454: values may be defined and end up as an array ref.
                   8455: 
                   8456: returns an array of values
                   8457: 
1.243     albertel 8458: =back
                   8459: 
                   8460: =head2 User Information
1.191     harris41 8461: 
1.243     albertel 8462: =over 4
1.191     harris41 8463: 
                   8464: =item *
1.394     bowersj2 8465: X<queryauthenticate()>
                   8466: B<queryauthenticate($uname,$udom)>: try to determine user's current 
1.191     harris41 8467: authentication scheme
                   8468: 
                   8469: =item *
1.394     bowersj2 8470: X<authenticate()>
                   8471: B<authenticate($uname,$upass,$udom)>: try to
                   8472: authenticate user from domain's lib servers (first use the current
                   8473: one). C<$upass> should be the users password.
1.191     harris41 8474: 
                   8475: =item *
1.394     bowersj2 8476: X<homeserver()>
                   8477: B<homeserver($uname,$udom)>: find the server which has
                   8478: the user's directory and files (there must be only one), this caches
                   8479: the answer, and also caches if there is a borken connection.
1.191     harris41 8480: 
                   8481: =item *
1.394     bowersj2 8482: X<idget()>
                   8483: B<idget($udom,@ids)>: find the usernames behind a list of IDs
                   8484: (IDs are a unique resource in a domain, there must be only 1 ID per
                   8485: username, and only 1 username per ID in a specific domain) (returns
                   8486: hash: id=>name,id=>name)
1.191     harris41 8487: 
                   8488: =item *
1.394     bowersj2 8489: X<idrget()>
                   8490: B<idrget($udom,@unames)>: find the IDs behind a list of
                   8491: usernames (returns hash: name=>id,name=>id)
1.191     harris41 8492: 
                   8493: =item *
1.394     bowersj2 8494: X<idput()>
                   8495: B<idput($udom,%ids)>: store away a list of names and associated IDs
1.191     harris41 8496: 
                   8497: =item *
1.394     bowersj2 8498: X<rolesinit()>
                   8499: B<rolesinit($udom,$username,$authhost)>: get user privileges
1.243     albertel 8500: 
                   8501: =item *
1.551     albertel 8502: X<getsection()>
                   8503: B<getsection($udom,$uname,$cname)>: finds the section of student in the
1.243     albertel 8504: course $cname, return section name/number or '' for "not in course"
                   8505: and '-1' for "no section"
                   8506: 
                   8507: =item *
1.394     bowersj2 8508: X<userenvironment()>
                   8509: B<userenvironment($udom,$uname,@what)>: gets the values of the keys
1.243     albertel 8510: passed in @what from the requested user's environment, returns a hash
                   8511: 
1.858     raeburn  8512: =item * 
                   8513: X<userlog_query()>
1.859     albertel 8514: B<userlog_query($uname,$udom,%filters)>: retrieves data from a user's
                   8515: activity.log file. %filters defines filters applied when parsing the
                   8516: log file. These can be start or end timestamps, or the type of action
                   8517: - log to look for Login or Logout events, check for Checkin or
                   8518: Checkout, role for role selection. The response is in the form
                   8519: timestamp1:hostid1:event1&timestamp2:hostid2:event2 where events are
                   8520: escaped strings of the action recorded in the activity.log file.
1.858     raeburn  8521: 
1.243     albertel 8522: =back
                   8523: 
                   8524: =head2 User Roles
                   8525: 
                   8526: =over 4
                   8527: 
                   8528: =item *
                   8529: 
1.810     raeburn  8530: allowed($priv,$uri,$symb,$role) : check for a user privilege; returns codes for allowed actions
1.243     albertel 8531:  F: full access
                   8532:  U,I,K: authentication modes (cxx only)
                   8533:  '': forbidden
                   8534:  1: user needs to choose course
                   8535:  2: browse allowed
1.766     albertel 8536:  A: passphrase authentication needed
1.243     albertel 8537: 
                   8538: =item *
                   8539: 
                   8540: definerole($rolename,$sysrole,$domrole,$courole) : define role; define a custom
                   8541: role rolename set privileges in format of lonTabs/roles.tab for system, domain,
                   8542: and course level
                   8543: 
                   8544: =item *
                   8545: 
                   8546: plaintext($short) : return value in %prp hash (rolesplain.tab); plain text
                   8547: explanation of a user role term
                   8548: 
1.832     raeburn  8549: =item *
                   8550: 
1.858     raeburn  8551: get_my_roles($uname,$udom,$context,$types,$roles,$roledoms) :
                   8552: All arguments are optional. Returns a hash of a roles, either for
                   8553: co-author/assistant author roles for a user's Construction Space
                   8554: (default), or if $context is 'user', roles for the user himself,
                   8555: In the hash, keys are set to colon-sparated $uname,$udom,and $role,
                   8556: and value is set to colon-separated start and end times for the role.
                   8557: If no username and domain are specified, will default to current
                   8558: user/domain. Types, roles, and roledoms are references to arrays,
                   8559: of role statuses (active, future or previous), roles 
                   8560: (e.g., cc,in, st etc.) and domains of the roles which can be used
                   8561: to restrict the list of roles reported. If no array ref is 
                   8562: provided for types, will default to return only active roles.
1.834     albertel 8563: 
1.243     albertel 8564: =back
                   8565: 
                   8566: =head2 User Modification
                   8567: 
                   8568: =over 4
                   8569: 
                   8570: =item *
                   8571: 
                   8572: assignrole($udom,$uname,$url,$role,$end,$start) : assign role; give a role to a
                   8573: user for the level given by URL.  Optional start and end dates (leave empty
                   8574: string or zero for "no date")
1.191     harris41 8575: 
                   8576: =item *
                   8577: 
1.243     albertel 8578: changepass($uname,$udom,$currentpass,$newpass,$server) : attempts to
                   8579: change a users, password, possible return values are: ok,
                   8580: pwchange_failure, non_authorized, auth_mode_error, unknown_user,
                   8581: refused
1.191     harris41 8582: 
                   8583: =item *
                   8584: 
1.243     albertel 8585: modifyuserauth($udom,$uname,$umode,$upass) : modify user authentication
1.191     harris41 8586: 
                   8587: =item *
                   8588: 
1.243     albertel 8589: modifyuser($udom,$uname,$uid,$umode,$upass,$first,$middle,$last,$gene) : 
                   8590: modify user
1.191     harris41 8591: 
                   8592: =item *
                   8593: 
1.286     matthew  8594: modifystudent
                   8595: 
                   8596: modify a students enrollment and identification information.
                   8597: The course id is resolved based on the current users environment.  
                   8598: This means the envoking user must be a course coordinator or otherwise
                   8599: associated with a course.
                   8600: 
1.297     matthew  8601: This call is essentially a wrapper for lonnet::modifyuser and
                   8602: lonnet::modify_student_enrollment
1.286     matthew  8603: 
                   8604: Inputs: 
                   8605: 
                   8606: =over 4
                   8607: 
                   8608: =item B<$udom> Students loncapa domain
                   8609: 
                   8610: =item B<$uname> Students loncapa login name
                   8611: 
                   8612: =item B<$uid> Students id/student number
                   8613: 
                   8614: =item B<$umode> Students authentication mode
                   8615: 
                   8616: =item B<$upass> Students password
                   8617: 
                   8618: =item B<$first> Students first name
                   8619: 
                   8620: =item B<$middle> Students middle name
                   8621: 
                   8622: =item B<$last> Students last name
                   8623: 
                   8624: =item B<$gene> Students generation
                   8625: 
                   8626: =item B<$usec> Students section in course
                   8627: 
                   8628: =item B<$end> Unix time of the roles expiration
                   8629: 
                   8630: =item B<$start> Unix time of the roles start date
                   8631: 
                   8632: =item B<$forceid> If defined, allow $uid to be changed
                   8633: 
                   8634: =item B<$desiredhome> server to use as home server for student
                   8635: 
                   8636: =back
1.297     matthew  8637: 
                   8638: =item *
                   8639: 
                   8640: modify_student_enrollment
                   8641: 
                   8642: Change a students enrollment status in a class.  The environment variable
                   8643: 'role.request.course' must be defined for this function to proceed.
                   8644: 
                   8645: Inputs:
                   8646: 
                   8647: =over 4
                   8648: 
                   8649: =item $udom, students domain
                   8650: 
                   8651: =item $uname, students name
                   8652: 
                   8653: =item $uid, students user id
                   8654: 
                   8655: =item $first, students first name
                   8656: 
                   8657: =item $middle
                   8658: 
                   8659: =item $last
                   8660: 
                   8661: =item $gene
                   8662: 
                   8663: =item $usec
                   8664: 
                   8665: =item $end
                   8666: 
                   8667: =item $start
                   8668: 
                   8669: =back
                   8670: 
1.191     harris41 8671: 
                   8672: =item *
                   8673: 
1.243     albertel 8674: assigncustomrole($udom,$uname,$url,$rdom,$rnam,$rolename,$end,$start) : assign
                   8675: custom role; give a custom role to a user for the level given by URL.  Specify
                   8676: name and domain of role author, and role name
1.191     harris41 8677: 
                   8678: =item *
                   8679: 
1.243     albertel 8680: revokerole($udom,$uname,$url,$role) : revoke a role for url
1.191     harris41 8681: 
                   8682: =item *
                   8683: 
1.243     albertel 8684: revokecustomrole($udom,$uname,$url,$role) : revoke a custom role
                   8685: 
                   8686: =back
                   8687: 
                   8688: =head2 Course Infomation
                   8689: 
                   8690: =over 4
1.191     harris41 8691: 
                   8692: =item *
                   8693: 
1.631     albertel 8694: coursedescription($courseid) : returns a hash of information about the
                   8695: specified course id, including all environment settings for the
                   8696: course, the description of the course will be in the hash under the
                   8697: key 'description'
1.191     harris41 8698: 
                   8699: =item *
                   8700: 
1.624     albertel 8701: resdata($name,$domain,$type,@which) : request for current parameter
                   8702: setting for a specific $type, where $type is either 'course' or 'user',
                   8703: @what should be a list of parameters to ask about. This routine caches
                   8704: answers for 5 minutes.
1.243     albertel 8705: 
1.877     foxr     8706: =item *
                   8707: 
                   8708: get_courseresdata($courseid, $domain) : dump the entire course resource
                   8709: data base, returning a hash that is keyed by the resource name and has
                   8710: values that are the resource value.  I believe that the timestamps and
                   8711: versions are also returned.
                   8712: 
                   8713: 
1.243     albertel 8714: =back
                   8715: 
                   8716: =head2 Course Modification
                   8717: 
                   8718: =over 4
1.191     harris41 8719: 
                   8720: =item *
                   8721: 
1.243     albertel 8722: writecoursepref($courseid,%prefs) : write preferences (environment
                   8723: database) for a course
1.191     harris41 8724: 
                   8725: =item *
                   8726: 
1.243     albertel 8727: createcourse($udom,$description,$url) : make/modify course
                   8728: 
                   8729: =back
                   8730: 
                   8731: =head2 Resource Subroutines
                   8732: 
                   8733: =over 4
1.191     harris41 8734: 
                   8735: =item *
                   8736: 
1.243     albertel 8737: subscribe($fname) : subscribe to a resource, returns URL if possible (probably should use repcopy instead)
1.191     harris41 8738: 
                   8739: =item *
                   8740: 
1.243     albertel 8741: repcopy($filename) : subscribes to the requested file, and attempts to
                   8742: replicate from the owning library server, Might return
1.607     raeburn  8743: 'unavailable', 'not_found', 'forbidden', 'ok', or
                   8744: 'bad_request', also attempts to grab the metadata for the
1.243     albertel 8745: resource. Expects the local filesystem pathname
                   8746: (/home/httpd/html/res/....)
                   8747: 
                   8748: =back
                   8749: 
                   8750: =head2 Resource Information
                   8751: 
                   8752: =over 4
1.191     harris41 8753: 
                   8754: =item *
                   8755: 
1.243     albertel 8756: EXT($varname,$symb,$udom,$uname) : evaluates and returns the value of
                   8757: a vairety of different possible values, $varname should be a request
                   8758: string, and the other parameters can be used to specify who and what
                   8759: one is asking about.
                   8760: 
                   8761: Possible values for $varname are environment.lastname (or other item
                   8762: from the envirnment hash), user.name (or someother aspect about the
                   8763: user), resource.0.maxtries (or some other part and parameter of a
                   8764: resource)
1.204     albertel 8765: 
                   8766: =item *
                   8767: 
1.243     albertel 8768: directcondval($number) : get current value of a condition; reads from a state
                   8769: string
1.204     albertel 8770: 
                   8771: =item *
                   8772: 
1.243     albertel 8773: condval($condidx) : value of condition index based on state
1.204     albertel 8774: 
                   8775: =item *
                   8776: 
1.243     albertel 8777: metadata($uri,$what,$liburi,$prefix,$depthcount) : request a
                   8778: resource's metadata, $what should be either a specific key, or either
                   8779: 'keys' (to get a list of possible keys) or 'packages' to get a list of
                   8780: packages that this resource currently uses, the last 3 arguments are only used internally for recursive metadata.
                   8781: 
                   8782: this function automatically caches all requests
1.191     harris41 8783: 
                   8784: =item *
                   8785: 
1.243     albertel 8786: metadata_query($query,$custom,$customshow) : make a metadata query against the
                   8787: network of library servers; returns file handle of where SQL and regex results
                   8788: will be stored for query
1.191     harris41 8789: 
                   8790: =item *
                   8791: 
1.243     albertel 8792: symbread($filename) : return symbolic list entry (filename argument optional);
                   8793: returns the data handle
1.191     harris41 8794: 
                   8795: =item *
                   8796: 
1.243     albertel 8797: symbverify($symb,$thisfn) : verifies that $symb actually exists and is
1.582     albertel 8798: a possible symb for the URL in $thisfn, and if is an encryypted
                   8799: resource that the user accessed using /enc/ returns a 1 on success, 0
                   8800: on failure, user must be in a course, as it assumes the existance of
1.620     albertel 8801: the course initial hash, and uses $env('request.course.id'}
1.243     albertel 8802: 
1.191     harris41 8803: 
                   8804: =item *
                   8805: 
1.243     albertel 8806: symbclean($symb) : removes versions numbers from a symb, returns the
                   8807: cleaned symb
1.191     harris41 8808: 
                   8809: =item *
                   8810: 
1.243     albertel 8811: is_on_map($uri) : checks if the $uri is somewhere on the current
                   8812: course map, user must be in a course for it to work.
1.191     harris41 8813: 
                   8814: =item *
                   8815: 
1.243     albertel 8816: numval($salt) : return random seed value (addend for rndseed)
1.191     harris41 8817: 
                   8818: =item *
                   8819: 
1.243     albertel 8820: rndseed($symb,$courseid,$udom,$uname) : create a random sum; returns
                   8821: a random seed, all arguments are optional, if they aren't sent it uses the
                   8822: environment to derive them. Note: if symb isn't sent and it can't get one
                   8823: from &symbread it will use the current time as its return value
1.191     harris41 8824: 
                   8825: =item *
                   8826: 
1.243     albertel 8827: ireceipt($funame,$fudom,$fucourseid,$fusymb) : return unique,
                   8828: unfakeable, receipt
1.191     harris41 8829: 
                   8830: =item *
                   8831: 
1.620     albertel 8832: receipt() : API to ireceipt working off of env values; given out to users
1.191     harris41 8833: 
                   8834: =item *
                   8835: 
1.243     albertel 8836: countacc($url) : count the number of accesses to a given URL
1.191     harris41 8837: 
                   8838: =item *
                   8839: 
1.243     albertel 8840: 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 8841: 
                   8842: =item *
                   8843: 
1.243     albertel 8844: 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 8845: 
                   8846: =item *
                   8847: 
1.243     albertel 8848: expirespread($uname,$udom,$stype,$usymb) : set expire date for spreadsheet
1.191     harris41 8849: 
                   8850: =item *
                   8851: 
1.243     albertel 8852: devalidate($symb) : devalidate temporary spreadsheet calculations,
                   8853: forcing spreadsheet to reevaluate the resource scores next time.
                   8854: 
                   8855: =back
                   8856: 
                   8857: =head2 Storing/Retreiving Data
                   8858: 
                   8859: =over 4
1.191     harris41 8860: 
                   8861: =item *
                   8862: 
1.243     albertel 8863: store($storehash,$symb,$namespace,$udom,$uname) : stores hash permanently
                   8864: for this url; hashref needs to be given and should be a \%hashname; the
                   8865: remaining args aren't required and if they aren't passed or are '' they will
1.620     albertel 8866: be derived from the env
1.191     harris41 8867: 
                   8868: =item *
                   8869: 
1.243     albertel 8870: cstore($storehash,$symb,$namespace,$udom,$uname) : same as store but
                   8871: uses critical subroutine
1.191     harris41 8872: 
                   8873: =item *
                   8874: 
1.243     albertel 8875: restore($symb,$namespace,$udom,$uname) : returns hash for this symb;
                   8876: all args are optional
1.191     harris41 8877: 
                   8878: =item *
                   8879: 
1.717     albertel 8880: dumpstore($namespace,$udom,$uname,$regexp,$range) : 
                   8881: dumps the complete (or key matching regexp) namespace into a hash
                   8882: ($udom, $uname, $regexp, $range are optional) for a namespace that is
                   8883: normally &store()ed into
                   8884: 
                   8885: $range should be either an integer '100' (give me the first 100
                   8886:                                            matching records)
                   8887:               or be  two integers sperated by a - with no spaces
                   8888:                  '30-50' (give me the 30th through the 50th matching
                   8889:                           records)
                   8890: 
                   8891: 
                   8892: =item *
                   8893: 
                   8894: putstore($namespace,$symb,$version,$storehash,$udomain,$uname) :
                   8895: replaces a &store() version of data with a replacement set of data
                   8896: for a particular resource in a namespace passed in the $storehash hash 
                   8897: reference
                   8898: 
                   8899: =item *
                   8900: 
1.243     albertel 8901: tmpstore($storehash,$symb,$namespace,$udom,$uname) : storage that
                   8902: works very similar to store/cstore, but all data is stored in a
                   8903: temporary location and can be reset using tmpreset, $storehash should
                   8904: be a hash reference, returns nothing on success
1.191     harris41 8905: 
                   8906: =item *
                   8907: 
1.243     albertel 8908: tmprestore($symb,$namespace,$udom,$uname) : storage that works very
                   8909: similar to restore, but all data is stored in a temporary location and
                   8910: can be reset using tmpreset. Returns a hash of values on success,
                   8911: error string otherwise.
1.191     harris41 8912: 
                   8913: =item *
                   8914: 
1.243     albertel 8915: tmpreset($symb,$namespace,$udom,$uname) : temporary storage reset,
                   8916: deltes all keys for $symb form the temporary storage hash.
1.191     harris41 8917: 
                   8918: =item *
                   8919: 
1.243     albertel 8920: get($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8921: reference filled in from namesp ($udom and $uname are optional)
1.191     harris41 8922: 
                   8923: =item *
                   8924: 
1.243     albertel 8925: del($namespace,$storearr,$udom,$uname) : deletes keys out of array from
                   8926: namesp ($udom and $uname are optional)
1.191     harris41 8927: 
                   8928: =item *
                   8929: 
1.702     albertel 8930: dump($namespace,$udom,$uname,$regexp,$range) : 
1.243     albertel 8931: dumps the complete (or key matching regexp) namespace into a hash
1.702     albertel 8932: ($udom, $uname, $regexp, $range are optional)
1.449     matthew  8933: 
1.702     albertel 8934: $range should be either an integer '100' (give me the first 100
                   8935:                                            matching records)
                   8936:               or be  two integers sperated by a - with no spaces
                   8937:                  '30-50' (give me the 30th through the 50th matching
                   8938:                           records)
1.449     matthew  8939: =item *
                   8940: 
                   8941: inc($namespace,$store,$udom,$uname) : increments $store in $namespace.
                   8942: $store can be a scalar, an array reference, or if the amount to be 
                   8943: incremented is > 1, a hash reference.
                   8944: 
                   8945: ($udom and $uname are optional)
1.191     harris41 8946: 
                   8947: =item *
                   8948: 
1.243     albertel 8949: put($namespace,$storehash,$udom,$uname) : stores hash in namesp
                   8950: ($udom and $uname are optional)
1.191     harris41 8951: 
                   8952: =item *
                   8953: 
1.243     albertel 8954: cput($namespace,$storehash,$udom,$uname) : critical put
                   8955: ($udom and $uname are optional)
1.191     harris41 8956: 
                   8957: =item *
                   8958: 
1.748     albertel 8959: newput($namespace,$storehash,$udom,$uname) :
                   8960: 
                   8961: Attempts to store the items in the $storehash, but only if they don't
                   8962: currently exist, if this succeeds you can be certain that you have 
                   8963: successfully created a new key value pair in the $namespace db.
                   8964: 
                   8965: 
                   8966: Args:
                   8967:  $namespace: name of database to store values to
                   8968:  $storehash: hashref to store to the db
                   8969:  $udom: (optional) domain of user containing the db
                   8970:  $uname: (optional) name of user caontaining the db
                   8971: 
                   8972: Returns:
                   8973:  'ok' -> succeeded in storing all keys of $storehash
                   8974:  'key_exists: <key>' -> failed to anything out of $storehash, as at
                   8975:                         least <key> already existed in the db (other
                   8976:                         requested keys may also already exist)
                   8977:  'error: <msg>' -> unable to tie the DB or other erorr occured
                   8978:  'con_lost' -> unable to contact request server
                   8979:  'refused' -> action was not allowed by remote machine
                   8980: 
                   8981: 
                   8982: =item *
                   8983: 
1.243     albertel 8984: eget($namespace,$storearr,$udom,$uname) : returns hash with keys from array
                   8985: reference filled in from namesp (encrypts the return communication)
                   8986: ($udom and $uname are optional)
1.191     harris41 8987: 
                   8988: =item *
                   8989: 
1.243     albertel 8990: log($udom,$name,$home,$message) : write to permanent log for user; use
                   8991: critical subroutine
                   8992: 
1.806     raeburn  8993: =item *
                   8994: 
1.860     raeburn  8995: get_dom($namespace,$storearr,$udom,$uhome) : returns hash with keys from
                   8996: array reference filled in from namespace found in domain level on either
                   8997: specified domain server ($uhome) or primary domain server ($udom and $uhome are optional).
1.806     raeburn  8998: 
                   8999: =item *
                   9000: 
1.860     raeburn  9001: put_dom($namespace,$storehash,$udom,$uhome) :  stores hash in namespace at 
                   9002: domain level either on specified domain server ($uhome) or primary domain 
                   9003: server ($udom and $uhome are optional)
1.806     raeburn  9004: 
1.243     albertel 9005: =back
                   9006: 
                   9007: =head2 Network Status Functions
                   9008: 
                   9009: =over 4
1.191     harris41 9010: 
                   9011: =item *
                   9012: 
                   9013: dirlist($uri) : return directory list based on URI
                   9014: 
                   9015: =item *
                   9016: 
1.243     albertel 9017: spareserver() : find server with least workload from spare.tab
                   9018: 
                   9019: =back
                   9020: 
                   9021: =head2 Apache Request
                   9022: 
                   9023: =over 4
1.191     harris41 9024: 
                   9025: =item *
                   9026: 
1.243     albertel 9027: ssi($url,%hash) : server side include, does a complete request cycle on url to
                   9028: localhost, posts hash
                   9029: 
                   9030: =back
                   9031: 
                   9032: =head2 Data to String to Data
                   9033: 
                   9034: =over 4
1.191     harris41 9035: 
                   9036: =item *
                   9037: 
1.243     albertel 9038: hash2str(%hash) : convert a hash into a string complete with escaping and '='
                   9039: and '&' separators, supports elements that are arrayrefs and hashrefs
1.191     harris41 9040: 
                   9041: =item *
                   9042: 
1.243     albertel 9043: hashref2str($hashref) : convert a hashref into a string complete with
                   9044: escaping and '=' and '&' separators, supports elements that are
                   9045: arrayrefs and hashrefs
1.191     harris41 9046: 
                   9047: =item *
                   9048: 
1.243     albertel 9049: arrayref2str($arrayref) : convert an arrayref into a string complete
                   9050: with escaping and '&' separators, supports elements that are arrayrefs
                   9051: and hashrefs
1.191     harris41 9052: 
                   9053: =item *
                   9054: 
1.243     albertel 9055: str2hash($string) : convert string to hash using unescaping and
                   9056: splitting on '=' and '&', supports elements that are arrayrefs and
                   9057: hashrefs
1.191     harris41 9058: 
                   9059: =item *
                   9060: 
1.243     albertel 9061: str2array($string) : convert string to hash using unescaping and
                   9062: splitting on '&', supports elements that are arrayrefs and hashrefs
                   9063: 
                   9064: =back
                   9065: 
                   9066: =head2 Logging Routines
                   9067: 
                   9068: =over 4
                   9069: 
                   9070: These routines allow one to make log messages in the lonnet.log and
                   9071: lonnet.perm logfiles.
1.191     harris41 9072: 
                   9073: =item *
                   9074: 
1.243     albertel 9075: logtouch() : make sure the logfile, lonnet.log, exists
1.191     harris41 9076: 
                   9077: =item *
                   9078: 
1.243     albertel 9079: logthis() : append message to the normal lonnet.log file, it gets
                   9080: preiodically rolled over and deleted.
1.191     harris41 9081: 
                   9082: =item *
                   9083: 
1.243     albertel 9084: logperm() : append a permanent message to lonnet.perm.log, this log
                   9085: file never gets deleted by any automated portion of the system, only
                   9086: messages of critical importance should go in here.
                   9087: 
                   9088: =back
                   9089: 
                   9090: =head2 General File Helper Routines
                   9091: 
                   9092: =over 4
1.191     harris41 9093: 
                   9094: =item *
                   9095: 
1.481     raeburn  9096: getfile($file,$caller) : two cases - requests for files in /res or in /uploaded.
                   9097: (a) files in /uploaded
                   9098:   (i) If a local copy of the file exists - 
                   9099:       compares modification date of local copy with last-modified date for 
                   9100:       definitive version stored on home server for course. If local copy is 
                   9101:       stale, requests a new version from the home server and stores it. 
                   9102:       If the original has been removed from the home server, then local copy 
                   9103:       is unlinked.
                   9104:   (ii) If local copy does not exist -
                   9105:       requests the file from the home server and stores it. 
                   9106:   
                   9107:   If $caller is 'uploadrep':  
                   9108:     This indicates a call from lonuploadrep.pm (PerlHeaderParserHandler phase)
                   9109:     for request for files originally uploaded via DOCS. 
                   9110:      - returns 'ok' if fresh local copy now available, -1 otherwise.
                   9111:   
                   9112:   Otherwise:
                   9113:      This indicates a call from the content generation phase of the request.
                   9114:      -  returns the entire contents of the file or -1.
                   9115:      
                   9116: (b) files in /res
                   9117:    - returns the entire contents of a file or -1; 
                   9118:    it properly subscribes to and replicates the file if neccessary.
1.191     harris41 9119: 
1.712     albertel 9120: 
                   9121: =item *
                   9122: 
                   9123: stat_file($url) : $url is expected to be a /res/ or /uploaded/ style file
                   9124:                   reference
                   9125: 
                   9126: returns either a stat() list of data about the file or an empty list
                   9127: if the file doesn't exist or couldn't find out about it (connection
                   9128: problems or user unknown)
                   9129: 
1.191     harris41 9130: =item *
                   9131: 
1.243     albertel 9132: filelocation($dir,$file) : returns file system location of a file
                   9133: based on URI; meant to be "fairly clean" absolute reference, $dir is a
                   9134: directory that relative $file lookups are to looked in ($dir of /a/dir
                   9135: and a file of ../bob will become /a/bob)
1.191     harris41 9136: 
                   9137: =item *
                   9138: 
                   9139: hreflocation($dir,$file) : returns file system location or a URL; same as
                   9140: filelocation except for hrefs
                   9141: 
                   9142: =item *
                   9143: 
                   9144: declutter() : declutters URLs (remove docroot, beginning slashes, 'res' etc)
                   9145: 
1.243     albertel 9146: =back
                   9147: 
1.608     albertel 9148: =head2 Usererfile file routines (/uploaded*)
                   9149: 
                   9150: =over 4
                   9151: 
                   9152: =item *
                   9153: 
                   9154: userfileupload(): main rotine for putting a file in a user or course's
                   9155:                   filespace, arguments are,
                   9156: 
1.620     albertel 9157:  formname - required - this is the name of the element in $env where the
1.608     albertel 9158:            filename, and the contents of the file to create/modifed exist
1.620     albertel 9159:            the filename is in $env{'form.'.$formname.'.filename'} and the
                   9160:            contents of the file is located in $env{'form.'.$formname}
1.608     albertel 9161:  coursedoc - if true, store the file in the course of the active role
                   9162:              of the current user
                   9163:  subdir - required - subdirectory to put the file in under ../userfiles/
                   9164:          if undefined, it will be placed in "unknown"
                   9165: 
                   9166:  (This routine calls clean_filename() to remove any dangerous
                   9167:  characters from the filename, and then calls finuserfileupload() to
                   9168:  complete the transaction)
                   9169: 
                   9170:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9171:  and /adm/notfound.html if unsuccessful
                   9172: 
                   9173: =item *
                   9174: 
                   9175: clean_filename(): routine for cleaing a filename up for storage in
                   9176:                  userfile space, argument is:
                   9177: 
                   9178:  filename - proposed filename
                   9179: 
                   9180: returns: the new clean filename
                   9181: 
                   9182: =item *
                   9183: 
                   9184: finishuserfileupload(): routine that creaes and sends the file to
                   9185: userspace, probably shouldn't be called directly
                   9186: 
                   9187:   docuname: username or courseid of destination for the file
                   9188:   docudom: domain of user/course of destination for the file
                   9189:   formname: same as for userfileupload()
                   9190:   fname: filename (inculding subdirectories) for the file
                   9191: 
                   9192:  returns either the url of the uploaded file (/uploaded/....) if successful
                   9193:  and /adm/notfound.html if unsuccessful
                   9194: 
                   9195: =item *
                   9196: 
                   9197: renameuserfile(): renames an existing userfile to a new name
                   9198: 
                   9199:   Args:
                   9200:    docuname: username or courseid of destination for the file
                   9201:    docudom: domain of user/course of destination for the file
                   9202:    old: current file name (including any subdirs under userfiles)
                   9203:    new: desired file name (including any subdirs under userfiles)
                   9204: 
                   9205: =item *
                   9206: 
                   9207: mkdiruserfile(): creates a directory is a userfiles dir
                   9208: 
                   9209:   Args:
                   9210:    docuname: username or courseid of destination for the file
                   9211:    docudom: domain of user/course of destination for the file
                   9212:    dir: dir to create (including any subdirs under userfiles)
                   9213: 
                   9214: =item *
                   9215: 
                   9216: removeuserfile(): removes a file that exists in userfiles
                   9217: 
                   9218:   Args:
                   9219:    docuname: username or courseid of destination for the file
                   9220:    docudom: domain of user/course of destination for the file
                   9221:    fname: filname to delete (including any subdirs under userfiles)
                   9222: 
                   9223: =item *
                   9224: 
                   9225: removeuploadedurl(): convience function for removeuserfile()
                   9226: 
                   9227:   Args:
                   9228:    url:  a full /uploaded/... url to delete
                   9229: 
1.747     albertel 9230: =item * 
                   9231: 
                   9232: get_portfile_permissions():
                   9233:   Args:
                   9234:     domain: domain of user or course contain the portfolio files
                   9235:     user: name of user or num of course contain the portfolio files
                   9236:   Returns:
                   9237:     hashref of a dump of the proper file_permissions.db
                   9238:    
                   9239: 
                   9240: =item * 
                   9241: 
                   9242: get_access_controls():
                   9243: 
                   9244: Args:
                   9245:   current_permissions: the hash ref returned from get_portfile_permissions()
                   9246:   group: (optional) the group you want the files associated with
                   9247:   file: (optional) the file you want access info on
                   9248: 
                   9249: Returns:
1.749     raeburn  9250:     a hash (keys are file names) of hashes containing
                   9251:         keys are: path to file/file_name\0uniqueID:scope_end_start (see below)
                   9252:         values are XML containing access control settings (see below) 
1.747     albertel 9253: 
                   9254: Internal notes:
                   9255: 
1.749     raeburn  9256:  access controls are stored in file_permissions.db as key=value pairs.
                   9257:     key -> path to file/file_name\0uniqueID:scope_end_start
                   9258:         where scope -> public,guest,course,group,domains or users.
                   9259:               end -> UNIX time for end of access (0 -> no end date)
                   9260:               start -> UNIX time for start of access
                   9261: 
                   9262:     value -> XML description of access control
                   9263:            <scope type=""> (type =1 of: public,guest,course,group,domains,users">
                   9264:             <start></start>
                   9265:             <end></end>
                   9266: 
                   9267:             <password></password>  for scope type = guest
                   9268: 
                   9269:             <domain></domain>     for scope type = course or group
                   9270:             <number></number>
                   9271:             <roles id="">
                   9272:              <role></role>
                   9273:              <access></access>
                   9274:              <section></section>
                   9275:              <group></group>
                   9276:             </roles>
                   9277: 
                   9278:             <dom></dom>         for scope type = domains
                   9279: 
                   9280:             <users>             for scope type = users
                   9281:              <user>
                   9282:               <uname></uname>
                   9283:               <udom></udom>
                   9284:              </user>
                   9285:             </users>
                   9286:            </scope> 
                   9287:               
                   9288:  Access data is also aggregated for each file in an additional key=value pair:
                   9289:  key -> path to file/file_name\0accesscontrol 
                   9290:  value -> reference to hash
                   9291:           hash contains key = value pairs
                   9292:           where key = uniqueID:scope_end_start
                   9293:                 value = UNIX time record was last updated
                   9294: 
                   9295:           Used to improve speed of look-ups of access controls for each file.  
                   9296:  
                   9297:  Locks on files (resulting from submission of portfolio file to a homework problem stored in array of arrays.
                   9298: 
                   9299: modify_access_controls():
                   9300: 
                   9301: Modifies access controls for a portfolio file
                   9302: Args
                   9303: 1. file name
                   9304: 2. reference to hash of required changes,
                   9305: 3. domain
                   9306: 4. username
                   9307:   where domain,username are the domain of the portfolio owner 
                   9308:   (either a user or a course) 
                   9309: 
                   9310: Returns:
                   9311: 1. result of additions or updates ('ok' or 'error', with error message). 
                   9312: 2. result of deletions ('ok' or 'error', with error message).
                   9313: 3. reference to hash of any new or updated access controls.
                   9314: 4. reference to hash used to map incoming IDs to uniqueIDs assigned to control.
                   9315:    key = integer (inbound ID)
                   9316:    value = uniqueID  
1.747     albertel 9317: 
1.608     albertel 9318: =back
                   9319: 
1.243     albertel 9320: =head2 HTTP Helper Routines
                   9321: 
                   9322: =over 4
                   9323: 
1.191     harris41 9324: =item *
                   9325: 
                   9326: escape() : unpack non-word characters into CGI-compatible hex codes
                   9327: 
                   9328: =item *
                   9329: 
                   9330: unescape() : pack CGI-compatible hex codes into actual non-word ASCII character
                   9331: 
1.243     albertel 9332: =back
                   9333: 
                   9334: =head1 PRIVATE SUBROUTINES
                   9335: 
                   9336: =head2 Underlying communication routines (Shouldn't call)
                   9337: 
                   9338: =over 4
                   9339: 
                   9340: =item *
                   9341: 
                   9342: subreply() : tries to pass a message to lonc, returns con_lost if incapable
                   9343: 
                   9344: =item *
                   9345: 
                   9346: reply() : uses subreply to send a message to remote machine, logs all failures
                   9347: 
                   9348: =item *
                   9349: 
                   9350: critical() : passes a critical message to another server; if cannot
                   9351: get through then place message in connection buffer directory and
                   9352: returns con_delayed, if incapable of saving message, returns
                   9353: con_failed
                   9354: 
                   9355: =item *
                   9356: 
                   9357: reconlonc() : tries to reconnect lonc client processes.
                   9358: 
                   9359: =back
                   9360: 
                   9361: =head2 Resource Access Logging
                   9362: 
                   9363: =over 4
                   9364: 
                   9365: =item *
                   9366: 
                   9367: flushcourselogs() : flush (save) buffer logs and access logs
                   9368: 
                   9369: =item *
                   9370: 
                   9371: courselog($what) : save message for course in hash
                   9372: 
                   9373: =item *
                   9374: 
                   9375: courseacclog($what) : save message for course using &courselog().  Perform
                   9376: special processing for specific resource types (problems, exams, quizzes, etc).
                   9377: 
1.191     harris41 9378: =item *
                   9379: 
                   9380: goodbye() : flush course logs and log shutting down; it is called in srm.conf
                   9381: as a PerlChildExitHandler
1.243     albertel 9382: 
                   9383: =back
                   9384: 
                   9385: =head2 Other
                   9386: 
                   9387: =over 4
                   9388: 
                   9389: =item *
                   9390: 
                   9391: symblist($mapname,%newhash) : update symbolic storage links
1.191     harris41 9392: 
                   9393: =back
                   9394: 
                   9395: =cut
1.877     foxr     9396: 

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